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