]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/clipbrd.cpp
wxChoice/wxComboBox background colour change (to get back to where we were);
[wxWidgets.git] / src / gtk1 / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/clipbrd.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "clipbrd.h"
12 #endif
13
14 #include "wx/clipbrd.h"
15
16 #if wxUSE_CLIPBOARD
17
18 #include "wx/dataobj.h"
19 #include "wx/utils.h"
20 #include "wx/log.h"
21
22 #include <glib.h>
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25
26 //-----------------------------------------------------------------------------
27 // thread system
28 //-----------------------------------------------------------------------------
29
30 #if wxUSE_THREADS
31 extern void wxapp_install_thread_wakeup();
32 extern void wxapp_uninstall_thread_wakeup();
33 #endif
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 GdkAtom g_clipboardAtom = 0;
40 GdkAtom g_targetsAtom = 0;
41
42 // the trace mask we use with wxLogTrace() - call
43 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
44 // (there will be a *lot* of them!)
45 static const char *TRACE_CLIPBOARD = _T("clipboard");
46
47 //-----------------------------------------------------------------------------
48 // reminder
49 //-----------------------------------------------------------------------------
50
51 /* The contents of a selection are returned in a GtkSelectionData
52 structure. selection/target identify the request.
53 type specifies the type of the return; if length < 0, and
54 the data should be ignored. This structure has object semantics -
55 no fields should be modified directly, they should not be created
56 directly, and pointers to them should not be stored beyond the duration of
57 a callback. (If the last is changed, we'll need to add reference
58 counting)
59
60 struct _GtkSelectionData
61 {
62 GdkAtom selection;
63 GdkAtom target;
64 GdkAtom type;
65 gint format;
66 guchar *data;
67 gint length;
68 };
69
70 */
71
72 //-----------------------------------------------------------------------------
73 // "selection_received" for targets
74 //-----------------------------------------------------------------------------
75
76 static void
77 targets_selection_received( GtkWidget *WXUNUSED(widget),
78 GtkSelectionData *selection_data,
79 #if (GTK_MINOR_VERSION > 0)
80 guint32 WXUNUSED(time),
81 #endif
82 wxClipboard *clipboard )
83 {
84 if ( wxTheClipboard && selection_data->length > 0 )
85 {
86 /* make sure we got the data in the correct form */
87 GdkAtom type = selection_data->type;
88 if ( type != GDK_SELECTION_TYPE_ATOM )
89 {
90 if ( strcmp(gdk_atom_name(type), "TARGETS") )
91 {
92 wxLogTrace( TRACE_CLIPBOARD,
93 _T("got unsupported clipboard target") );
94
95 clipboard->m_waiting = FALSE;
96 return;
97 }
98 }
99
100 #ifdef __WXDEBUG__
101 wxDataFormat clip( selection_data->selection );
102 wxLogTrace( TRACE_CLIPBOARD,
103 wxT("selection received for targets, clipboard %s"),
104 clip.GetId().c_str() );
105 #endif // __WXDEBUG__
106
107 // the atoms we received, holding a list of targets (= formats)
108 GdkAtom *atoms = (GdkAtom *)selection_data->data;
109
110 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
111 {
112 wxDataFormat format( atoms[i] );
113
114 wxLogTrace( TRACE_CLIPBOARD,
115 wxT("selection received for targets, format %s"),
116 format.GetId().c_str() );
117
118 if (format == clipboard->m_targetRequested)
119 {
120 clipboard->m_waiting = FALSE;
121 clipboard->m_formatSupported = TRUE;
122 return;
123 }
124 }
125 }
126
127 clipboard->m_waiting = FALSE;
128 }
129
130 //-----------------------------------------------------------------------------
131 // "selection_received" for the actual data
132 //-----------------------------------------------------------------------------
133
134 static void
135 selection_received( GtkWidget *WXUNUSED(widget),
136 GtkSelectionData *selection_data,
137 #if (GTK_MINOR_VERSION > 0)
138 guint32 WXUNUSED(time),
139 #endif
140 wxClipboard *clipboard )
141 {
142 if (!wxTheClipboard)
143 {
144 clipboard->m_waiting = FALSE;
145 return;
146 }
147
148 wxDataObject *data_object = clipboard->m_receivedData;
149
150 if (!data_object)
151 {
152 clipboard->m_waiting = FALSE;
153 return;
154 }
155
156 if (selection_data->length <= 0)
157 {
158 clipboard->m_waiting = FALSE;
159 return;
160 }
161
162 wxDataFormat format( selection_data->target );
163
164 /* make sure we got the data in the correct format */
165 if (!data_object->IsSupportedFormat( format ) )
166 {
167 clipboard->m_waiting = FALSE;
168 return;
169 }
170
171 /* make sure we got the data in the correct form (selection type).
172 if so, copy data to target object */
173 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
174 {
175 clipboard->m_waiting = FALSE;
176 return;
177 }
178
179 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
180
181 wxTheClipboard->m_formatSupported = TRUE;
182 clipboard->m_waiting = FALSE;
183 }
184
185 //-----------------------------------------------------------------------------
186 // "selection_clear"
187 //-----------------------------------------------------------------------------
188
189 static gint
190 selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
191 {
192 if (!wxTheClipboard) return TRUE;
193
194 if (event->selection == GDK_SELECTION_PRIMARY)
195 {
196 wxTheClipboard->m_ownsPrimarySelection = FALSE;
197 }
198 else
199 if (event->selection == g_clipboardAtom)
200 {
201 wxTheClipboard->m_ownsClipboard = FALSE;
202 }
203 else
204 {
205 wxTheClipboard->m_waiting = FALSE;
206 return FALSE;
207 }
208
209 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
210 (!wxTheClipboard->m_ownsClipboard))
211 {
212 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
213 if (wxTheClipboard->m_data)
214 {
215 wxLogDebug( wxT("wxClipboard will get cleared" ) );
216
217 delete wxTheClipboard->m_data;
218 wxTheClipboard->m_data = (wxDataObject*) NULL;
219 }
220 }
221
222 wxTheClipboard->m_waiting = FALSE;
223 return TRUE;
224 }
225
226 //-----------------------------------------------------------------------------
227 // selection handler for supplying data
228 //-----------------------------------------------------------------------------
229
230 static void
231 selection_handler( GtkWidget *WXUNUSED(widget),
232 GtkSelectionData *selection_data,
233 guint WXUNUSED(info),
234 guint WXUNUSED(time),
235 gpointer WXUNUSED(data) )
236 {
237 if (!wxTheClipboard) return;
238
239 if (!wxTheClipboard->m_data) return;
240
241 wxDataObject *data = wxTheClipboard->m_data;
242
243 wxDataFormat format( selection_data->target );
244
245 if (!data->IsSupportedFormat( format )) return;
246
247 /* this will fail for composite formats */
248 if (format.GetType() == wxDF_TEXT)
249 {
250 wxTextDataObject *text_object = (wxTextDataObject*) data;
251 wxString text( text_object->GetText() );
252
253 #if wxUSE_UNICODE
254 const wxWX2MBbuf s = text.mbc_str();
255 int len = strlen(s);
256 #else // more efficient in non-Unicode
257 const char *s = text.c_str();
258 int len = (int) text.Length();
259 #endif
260 gtk_selection_data_set(
261 selection_data,
262 GDK_SELECTION_TYPE_STRING,
263 8*sizeof(gchar),
264 (unsigned char*) (const char*) s,
265 len );
266
267 return;
268 }
269
270 int size = data->GetDataSize( format );
271
272 if (size == 0) return;
273
274 char *d = new char[size];
275
276 data->GetDataHere( selection_data->target, (void*) d );
277
278 gtk_selection_data_set(
279 selection_data,
280 GDK_SELECTION_TYPE_STRING,
281 8*sizeof(gchar),
282 (unsigned char*) d,
283 size );
284 }
285
286 //-----------------------------------------------------------------------------
287 // wxClipboard
288 //-----------------------------------------------------------------------------
289
290 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
291
292 wxClipboard::wxClipboard()
293 {
294 m_open = FALSE;
295
296 m_ownsClipboard = FALSE;
297 m_ownsPrimarySelection = FALSE;
298
299 m_data = (wxDataObject*) NULL;
300 m_receivedData = (wxDataObject*) NULL;
301
302 /* we use m_targetsWidget to query what formats are available */
303
304 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
305 gtk_widget_realize( m_targetsWidget );
306
307 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
308 "selection_received",
309 GTK_SIGNAL_FUNC( targets_selection_received ),
310 (gpointer) this );
311
312 /* we use m_clipboardWidget to get and to offer data */
313
314 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
315 gtk_widget_realize( m_clipboardWidget );
316
317 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
318 "selection_received",
319 GTK_SIGNAL_FUNC( selection_received ),
320 (gpointer) this );
321
322 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
323 "selection_clear_event",
324 GTK_SIGNAL_FUNC( selection_clear_clip ),
325 (gpointer) NULL );
326
327 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
328 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
329
330 m_formatSupported = FALSE;
331 m_targetRequested = 0;
332
333 m_usePrimary = FALSE;
334 }
335
336 wxClipboard::~wxClipboard()
337 {
338 Clear();
339
340 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
341 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
342 }
343
344 void wxClipboard::Clear()
345 {
346 if (m_data)
347 {
348 #if wxUSE_THREADS
349 /* disable GUI threads */
350 wxapp_uninstall_thread_wakeup();
351 #endif
352
353 /* As we have data we also own the clipboard. Once we no longer own
354 it, clear_selection is called which will set m_data to zero */
355 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
356 {
357 m_waiting = TRUE;
358
359 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
360 (guint32) GDK_CURRENT_TIME );
361
362 while (m_waiting) gtk_main_iteration();
363 }
364
365 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
366 {
367 m_waiting = TRUE;
368
369 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
370 (guint32) GDK_CURRENT_TIME );
371
372 while (m_waiting) gtk_main_iteration();
373 }
374
375 if (m_data)
376 {
377 delete m_data;
378 m_data = (wxDataObject*) NULL;
379 }
380
381 #if wxUSE_THREADS
382 /* re-enable GUI threads */
383 wxapp_install_thread_wakeup();
384 #endif
385 }
386
387 m_targetRequested = 0;
388 m_formatSupported = FALSE;
389 }
390
391 bool wxClipboard::Open()
392 {
393 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
394
395 m_open = TRUE;
396
397 return TRUE;
398 }
399
400 bool wxClipboard::SetData( wxDataObject *data )
401 {
402 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
403
404 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
405
406 Clear();
407
408 return AddData( data );
409 }
410
411 bool wxClipboard::AddData( wxDataObject *data )
412 {
413 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
414
415 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
416
417 /* we can only store one wxDataObject */
418 Clear();
419
420 m_data = data;
421
422 /* get formats from wxDataObjects */
423 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
424 m_data->GetAllFormats( array );
425
426 /* primary selection or clipboard */
427 GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
428 : g_clipboardAtom;
429
430
431 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
432 {
433 wxLogTrace( TRACE_CLIPBOARD,
434 wxT("wxClipboard now supports atom %s"),
435 array[i].GetId().c_str() );
436
437 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
438 clipboard,
439 array[i],
440 0 ); /* what is info ? */
441 }
442
443 delete[] array;
444
445 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
446 "selection_get",
447 GTK_SIGNAL_FUNC(selection_handler),
448 (gpointer) NULL );
449
450 #if wxUSE_THREADS
451 /* disable GUI threads */
452 wxapp_uninstall_thread_wakeup();
453 #endif
454
455 /* Tell the world we offer clipboard data */
456 bool res = (gtk_selection_owner_set( m_clipboardWidget,
457 clipboard,
458 (guint32) GDK_CURRENT_TIME ));
459
460 if (m_usePrimary)
461 m_ownsPrimarySelection = res;
462 else
463 m_ownsClipboard = res;
464
465 #if wxUSE_THREADS
466 /* re-enable GUI threads */
467 wxapp_install_thread_wakeup();
468 #endif
469
470 return res;
471 }
472
473 void wxClipboard::Close()
474 {
475 wxCHECK_RET( m_open, wxT("clipboard not open") );
476
477 m_open = FALSE;
478 }
479
480 bool wxClipboard::IsOpened() const
481 {
482 return m_open;
483 }
484
485 bool wxClipboard::IsSupported( const wxDataFormat& format )
486 {
487 /* reentrance problems */
488 if (m_waiting) return FALSE;
489
490 /* store requested format to be asked for by callbacks */
491 m_targetRequested = format;
492
493 #if 0
494 wxLogTrace( TRACE_CLIPBOARD,
495 wxT("wxClipboard:IsSupported: requested format: %s"),
496 format.GetId().c_str() );
497 #endif
498
499 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
500
501 m_formatSupported = FALSE;
502
503 /* perform query. this will set m_formatSupported to
504 TRUE if m_targetRequested is supported.
505 also, we have to wait for the "answer" from the
506 clipboard owner which is an asynchronous process.
507 therefore we set m_waiting = TRUE here and wait
508 until the callback "targets_selection_received"
509 sets it to FALSE */
510
511 m_waiting = TRUE;
512
513 gtk_selection_convert( m_targetsWidget,
514 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
515 : g_clipboardAtom,
516 g_targetsAtom,
517 (guint32) GDK_CURRENT_TIME );
518
519 while (m_waiting) gtk_main_iteration();
520
521 if (!m_formatSupported) return FALSE;
522
523 return TRUE;
524 }
525
526 bool wxClipboard::GetData( wxDataObject& data )
527 {
528 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
529
530 /* get formats from wxDataObjects */
531 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
532 data.GetAllFormats( array );
533
534 for (size_t i = 0; i < data.GetFormatCount(); i++)
535 {
536 wxDataFormat format( array[i] );
537
538 wxLogTrace( TRACE_CLIPBOARD,
539 wxT("wxClipboard::GetData: requested format: %s"),
540 format.GetId().c_str() );
541
542 /* is data supported by clipboard ? */
543
544 /* store requested format to be asked for by callbacks */
545 m_targetRequested = format;
546
547 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
548
549 m_formatSupported = FALSE;
550
551 /* perform query. this will set m_formatSupported to
552 TRUE if m_targetRequested is supported.
553 also, we have to wait for the "answer" from the
554 clipboard owner which is an asynchronous process.
555 therefore we set m_waiting = TRUE here and wait
556 until the callback "targets_selection_received"
557 sets it to FALSE */
558
559 m_waiting = TRUE;
560
561 gtk_selection_convert( m_targetsWidget,
562 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
563 : g_clipboardAtom,
564 g_targetsAtom,
565 (guint32) GDK_CURRENT_TIME );
566
567 while (m_waiting) gtk_main_iteration();
568
569 if (!m_formatSupported) continue;
570
571 /* store pointer to data object to be filled up by callbacks */
572 m_receivedData = &data;
573
574 /* store requested format to be asked for by callbacks */
575 m_targetRequested = format;
576
577 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
578
579 /* start query */
580 m_formatSupported = FALSE;
581
582 /* ask for clipboard contents. this will set
583 m_formatSupported to TRUE if m_targetRequested
584 is supported.
585 also, we have to wait for the "answer" from the
586 clipboard owner which is an asynchronous process.
587 therefore we set m_waiting = TRUE here and wait
588 until the callback "targets_selection_received"
589 sets it to FALSE */
590
591 m_waiting = TRUE;
592
593 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
594
595 gtk_selection_convert( m_clipboardWidget,
596 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
597 : g_clipboardAtom,
598 m_targetRequested,
599 (guint32) GDK_CURRENT_TIME );
600
601 while (m_waiting) gtk_main_iteration();
602
603 /* this is a true error as we checked for the presence of such data before */
604 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
605
606 /* return success */
607 delete[] array;
608 return TRUE;
609 }
610
611 wxLogDebug( wxT("wxClipboard::GetData: format not found") );
612
613 /* return failure */
614 delete[] array;
615 return FALSE;
616 }
617
618 #endif
619 // wxUSE_CLIPBOARD
620