clipboards tests
[wxWidgets.git] / src / gtk1 / 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 return FALSE;
228 }
229
230 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
231 (!wxTheClipboard->m_ownsClipboard))
232 {
233 /* the clipboard is no longer in our hands. we can the clipboard data. */
234
235 if (wxTheClipboard->m_dataBroker)
236 {
237 delete wxTheClipboard->m_dataBroker;
238 wxTheClipboard->m_dataBroker = (wxDataBroker*) NULL;
239 }
240 }
241
242 return TRUE;
243 }
244
245 //-----------------------------------------------------------------------------
246 // selection handler for supplying data
247 //-----------------------------------------------------------------------------
248
249 static void
250 selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, gpointer WXUNUSED(data) )
251 {
252 if (!wxTheClipboard) return;
253
254 if (!wxTheClipboard->m_dataBroker) return;
255
256 wxNode *node = wxTheClipboard->m_dataBroker->m_dataObjects.First();
257
258 while (node)
259 {
260 wxDataObject *data_object = (wxDataObject *)node->Data();
261
262 if (data_object->GetFormat().GetAtom() != selection_data->target)
263 {
264 node = node->Next();
265 break;
266 }
267
268 switch (data_object->GetFormat().GetType())
269 {
270 case wxDF_TEXT:
271 {
272 wxTextDataObject *text_object = (wxTextDataObject*) data_object;
273
274 wxString text = text_object->GetText();
275
276 char *s = WXSTRINGCAST text;
277 int len = (int) text.Length();
278
279 gtk_selection_data_set(
280 selection_data,
281 GDK_SELECTION_TYPE_STRING,
282 8*sizeof(gchar),
283 (unsigned char*) s,
284 len );
285
286 break;
287 }
288
289 case wxDF_BITMAP:
290 {
291 // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object;
292
293 // how do we do that ?
294
295 break;
296 }
297
298 case wxDF_PRIVATE:
299 {
300 wxPrivateDataObject *private_object = (wxPrivateDataObject*) data_object;
301
302 if (private_object->GetSize() == 0) return;
303
304 gtk_selection_data_set(
305 selection_data,
306 GDK_SELECTION_TYPE_STRING,
307 8*sizeof(gchar),
308 (unsigned char*) private_object->GetData(),
309 (int) private_object->GetSize() );
310 }
311
312 default:
313 break;
314 }
315
316 node = node->Next();
317 }
318 }
319
320 //-----------------------------------------------------------------------------
321 // wxClipboard
322 //-----------------------------------------------------------------------------
323
324 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
325
326 wxClipboard::wxClipboard()
327 {
328 m_open = FALSE;
329
330 m_ownsClipboard = FALSE;
331 m_ownsPrimarySelection = FALSE;
332
333 m_dataBroker = (wxDataBroker*) NULL;
334
335 m_receivedData = (wxDataObject*) NULL;
336
337 /* we use m_targetsWidget to query what formats are available */
338
339 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
340 gtk_widget_realize( m_targetsWidget );
341
342 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
343 "selection_received",
344 GTK_SIGNAL_FUNC( targets_selection_received ),
345 (gpointer) this );
346
347 /* we use m_clipboardWidget to get and to offer data */
348
349 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
350 gtk_widget_realize( m_clipboardWidget );
351
352 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
353 "selection_received",
354 GTK_SIGNAL_FUNC( selection_received ),
355 (gpointer) this );
356
357 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
358 "selection_clear_event",
359 GTK_SIGNAL_FUNC( selection_clear_clip ),
360 (gpointer) NULL );
361
362 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
363 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
364
365 m_formatSupported = FALSE;
366 m_targetRequested = 0;
367 }
368
369 wxClipboard::~wxClipboard()
370 {
371 Clear();
372
373 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
374 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
375 }
376
377 void wxClipboard::Clear()
378 {
379 if (m_dataBroker)
380 {
381 /* As we have data we also own the clipboard. Once we no longer own
382 it, clear_selection is called which will set m_data to zero */
383
384 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
385 {
386 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, GDK_CURRENT_TIME );
387 }
388
389 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
390 {
391 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME );
392 }
393
394 if (m_dataBroker)
395 {
396 delete m_dataBroker;
397 m_dataBroker = (wxDataBroker*) NULL;
398 }
399 }
400
401 m_targetRequested = 0;
402
403 m_formatSupported = FALSE;
404 }
405
406 bool wxClipboard::Open()
407 {
408 wxCHECK_MSG( !m_open, FALSE, "clipboard already open" );
409
410 m_open = TRUE;
411
412 return TRUE;
413 }
414
415 bool wxClipboard::SetData( wxDataObject *data )
416 {
417 wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
418
419 wxCHECK_MSG( data, FALSE, "data is invalid" );
420
421 Clear();
422
423 return AddData( data );
424 }
425
426 bool wxClipboard::AddData( wxDataObject *data )
427 {
428 wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
429
430 wxCHECK_MSG( data, FALSE, "data is invalid" );
431
432 /* if clipboard has been cleared before, create new data broker */
433
434 if (!m_dataBroker) m_dataBroker = new wxDataBroker();
435
436 /* add new data to list of offered data objects */
437
438 m_dataBroker->Add( data );
439
440 /* get native format id of new data object */
441
442 GdkAtom format = data->GetFormat().GetAtom();
443
444 wxCHECK_MSG( format, FALSE, "data has invalid format" );
445
446 /* This should happen automatically, but to be on the safe side */
447
448 m_ownsClipboard = FALSE;
449 m_ownsPrimarySelection = FALSE;
450
451 /* Add handlers if someone requests data */
452
453
454 #if (GTK_MINOR_VERSION > 0)
455
456 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
457 GDK_SELECTION_PRIMARY,
458 format,
459 0 ); /* what is info ? */
460
461 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
462 g_clipboardAtom,
463 format,
464 0 ); /* what is info ? */
465
466 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
467 "selection_get",
468 GTK_SIGNAL_FUNC(selection_handler),
469 (gpointer) NULL );
470
471 #else
472
473 gtk_selection_add_handler( m_clipboardWidget,
474 g_clipboardAtom,
475 format,
476 selection_handler,
477 (gpointer) NULL );
478
479 gtk_selection_add_handler( m_clipboardWidget,
480 GDK_SELECTION_PRIMARY,
481 format,
482 selection_handler,
483 (gpointer) NULL );
484 #endif
485
486 /* Tell the world we offer clipboard data */
487
488 if (!gtk_selection_owner_set( m_clipboardWidget,
489 g_clipboardAtom,
490 GDK_CURRENT_TIME ))
491 {
492 return FALSE;
493 }
494 m_ownsClipboard = TRUE;
495
496 if (!gtk_selection_owner_set( m_clipboardWidget,
497 GDK_SELECTION_PRIMARY,
498 GDK_CURRENT_TIME ))
499 {
500 return FALSE;
501 }
502 m_ownsPrimarySelection = TRUE;
503
504 return TRUE;
505 }
506
507 void wxClipboard::Close()
508 {
509 wxCHECK_RET( m_open, "clipboard not open" );
510
511 m_open = FALSE;
512 }
513
514 bool wxClipboard::IsSupported( wxDataFormat format )
515 {
516 wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
517
518 /* store requested format to be asked for by callbacks */
519
520 m_targetRequested = format.GetAtom();
521
522 wxCHECK_MSG( m_targetRequested, FALSE, "invalid clipboard format" );
523
524 m_formatSupported = FALSE;
525
526 /* perform query. this will set m_formatSupported to
527 TRUE if m_targetRequested is supported.
528 alsom we have to wait for the "answer" from the
529 clipboard owner which is an asynchronous process.
530 therefore we set m_waiting = TRUE here and wait
531 until the callback "targets_selection_received"
532 sets it to FALSE */
533
534 m_waiting = TRUE;
535
536 gtk_selection_convert( m_targetsWidget,
537 g_clipboardAtom,
538 g_targetsAtom,
539 GDK_CURRENT_TIME );
540
541 while (m_waiting) gtk_main_iteration();
542
543 if (!m_formatSupported) return FALSE;
544
545 return TRUE;
546 }
547
548 bool wxClipboard::GetData( wxDataObject *data )
549 {
550 wxCHECK_MSG( m_open, FALSE, "clipboard not open" );
551
552 /* is data supported by clipboard ? */
553
554 if (!IsSupported( data->GetFormat() )) return FALSE;
555
556 /* store pointer to data object to be filled up by callbacks */
557
558 m_receivedData = data;
559
560 /* store requested format to be asked for by callbacks */
561
562 m_targetRequested = data->GetFormat().GetAtom();
563
564 wxCHECK_MSG( m_targetRequested, FALSE, "invalid clipboard format" );
565
566 /* start query */
567
568 m_formatSupported = FALSE;
569
570 /* ask for clipboard contents. this will set
571 m_formatSupported to TRUE if m_targetRequested
572 is supported.
573 also, we have to wait for the "answer" from the
574 clipboard owner which is an asynchronous process.
575 therefore we set m_waiting = TRUE here and wait
576 until the callback "targets_selection_received"
577 sets it to FALSE */
578
579 m_waiting = TRUE;
580
581 gtk_selection_convert( m_clipboardWidget,
582 g_clipboardAtom,
583 m_targetRequested,
584 GDK_CURRENT_TIME );
585
586 while (m_waiting) gtk_main_iteration();
587
588 /* this is a true error as we checked for the presence of such data before */
589
590 wxCHECK_MSG( m_formatSupported, FALSE, "error retrieving data from clipboard" );
591
592 return TRUE;
593 }
594
595 //-----------------------------------------------------------------------------
596 // wxClipboardModule
597 //-----------------------------------------------------------------------------
598
599 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
600
601 bool wxClipboardModule::OnInit()
602 {
603 wxTheClipboard = new wxClipboard();
604
605 return TRUE;
606 }
607
608 void wxClipboardModule::OnExit()
609 {
610 if (wxTheClipboard) delete wxTheClipboard;
611 wxTheClipboard = (wxClipboard*) NULL;
612 }
613
614 #endif
615
616 // wxUSE_CLIPBOARD
617