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