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