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