]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/clipbrd.cpp
dc4bbe9c9043661ffa6cedb84e192fb95177b002
[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/dataobj.h"
19 #include "wx/utils.h"
20 #include "wx/log.h"
21
22 #include "glib.h"
23 #include "gdk/gdk.h"
24 #include "gtk/gtk.h"
25
26 //-----------------------------------------------------------------------------
27 // thread system
28 //-----------------------------------------------------------------------------
29
30 #if wxUSE_THREADS
31 extern void wxapp_install_thread_wakeup();
32 extern void wxapp_uninstall_thread_wakeup();
33 #endif
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
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 && selection_data->length > 0 )
80 {
81 /* make sure we got the data in the correct form */
82
83 // VZ: I don't know what does this mean (and GTK+ authors apparently
84 // don't know either, Owen Taylor writes that "Motif seems to ask
85 // for TARGETS atom sometimes" (??)), but it seems that xterm
86 // (which is not a Motif app AFAIK) does this too, so it's
87 // absolutely essential to support this, otherwise we can't paste
88 // text from xterm!
89 GdkAtom type = selection_data->type;
90 if ( type != GDK_SELECTION_TYPE_ATOM )
91 {
92 if ( strcmp(gdk_atom_name(type), "TARGETS") != 0 )
93 {
94 // don't know what this is
95 clipboard->m_waiting = FALSE;
96 return;
97 }
98 //else: don't know what this is, but it seems to work in the same
99 // way as GDK_SELECTION_TYPE_ATOM does
100 }
101 //else: the data is the list of formats supported by the selection
102
103 /*
104 wxDataFormat clip( selection_data->selection );
105 wxLogDebug( wxT("selection received for targets, clipboard %s"), clip.GetId().c_str() );
106 */
107
108 // the atoms we received, holding a list of targets (= formats)
109 GdkAtom *atoms = (GdkAtom *)selection_data->data;
110
111 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
112 {
113 wxDataFormat format( atoms[i] );
114
115 /*
116 wxLogDebug( wxT("selection received for targets, format %s"), format.GetId().c_str() );
117 */
118
119 if (format == clipboard->m_targetRequested)
120 {
121 clipboard->m_waiting = FALSE;
122 clipboard->m_formatSupported = TRUE;
123 return;
124 }
125 }
126 }
127
128 clipboard->m_waiting = FALSE;
129 }
130
131 //-----------------------------------------------------------------------------
132 // "selection_received" for the actual data
133 //-----------------------------------------------------------------------------
134
135 static void
136 selection_received( GtkWidget *WXUNUSED(widget),
137 GtkSelectionData *selection_data,
138 #if (GTK_MINOR_VERSION > 0)
139 guint32 WXUNUSED(time),
140 #endif
141 wxClipboard *clipboard )
142 {
143 if (!wxTheClipboard)
144 {
145 clipboard->m_waiting = FALSE;
146 return;
147 }
148
149 wxDataObject *data_object = clipboard->m_receivedData;
150
151 if (!data_object)
152 {
153 clipboard->m_waiting = FALSE;
154 return;
155 }
156
157 if (selection_data->length <= 0)
158 {
159 clipboard->m_waiting = FALSE;
160 return;
161 }
162
163 wxDataFormat format( selection_data->target );
164
165 /* make sure we got the data in the correct format */
166 if (!data_object->IsSupportedFormat( format ) )
167 {
168 clipboard->m_waiting = FALSE;
169 return;
170 }
171
172 /* make sure we got the data in the correct form (selection type).
173 if so, copy data to target object */
174 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
175 {
176 clipboard->m_waiting = FALSE;
177 return;
178 }
179
180 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
181
182 wxTheClipboard->m_formatSupported = TRUE;
183 clipboard->m_waiting = FALSE;
184 }
185
186 //-----------------------------------------------------------------------------
187 // "selection_clear"
188 //-----------------------------------------------------------------------------
189
190 static gint
191 selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
192 {
193 if (!wxTheClipboard) return TRUE;
194
195 if (event->selection == GDK_SELECTION_PRIMARY)
196 {
197 wxTheClipboard->m_ownsPrimarySelection = FALSE;
198 }
199 else
200 if (event->selection == g_clipboardAtom)
201 {
202 wxTheClipboard->m_ownsClipboard = FALSE;
203 }
204 else
205 {
206 wxTheClipboard->m_waiting = FALSE;
207 return FALSE;
208 }
209
210 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
211 (!wxTheClipboard->m_ownsClipboard))
212 {
213 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
214 if (wxTheClipboard->m_data)
215 {
216 wxLogDebug( wxT("wxClipboard will get cleared" ) );
217
218 delete wxTheClipboard->m_data;
219 wxTheClipboard->m_data = (wxDataObject*) NULL;
220 }
221 }
222
223 wxTheClipboard->m_waiting = FALSE;
224 return TRUE;
225 }
226
227 //-----------------------------------------------------------------------------
228 // selection handler for supplying data
229 //-----------------------------------------------------------------------------
230
231 static void
232 selection_handler( GtkWidget *WXUNUSED(widget),
233 GtkSelectionData *selection_data,
234 guint WXUNUSED(info),
235 guint WXUNUSED(time),
236 gpointer WXUNUSED(data) )
237 {
238 if (!wxTheClipboard) return;
239
240 if (!wxTheClipboard->m_data) return;
241
242 wxDataObject *data = wxTheClipboard->m_data;
243
244 wxDataFormat format( selection_data->target );
245
246 if (!data->IsSupportedFormat( format )) return;
247
248 /* this will fail for composite formats */
249 if (format.GetType() == wxDF_TEXT)
250 {
251 wxTextDataObject *text_object = (wxTextDataObject*) data;
252 wxString text( text_object->GetText() );
253
254 #if wxUSE_UNICODE
255 const wxWX2MBbuf s = text.mbc_str();
256 int len = strlen(s);
257 #else // more efficient in non-Unicode
258 const char *s = text.c_str();
259 int len = (int) text.Length();
260 #endif
261 gtk_selection_data_set(
262 selection_data,
263 GDK_SELECTION_TYPE_STRING,
264 8*sizeof(gchar),
265 (unsigned char*) (const char*) s,
266 len );
267
268 return;
269 }
270
271 int size = data->GetDataSize( format );
272
273 if (size == 0) return;
274
275 char *d = new char[size];
276
277 data->GetDataHere( selection_data->target, (void*) d );
278
279 gtk_selection_data_set(
280 selection_data,
281 GDK_SELECTION_TYPE_STRING,
282 8*sizeof(gchar),
283 (unsigned char*) d,
284 size );
285 }
286
287 //-----------------------------------------------------------------------------
288 // wxClipboard
289 //-----------------------------------------------------------------------------
290
291 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
292
293 wxClipboard::wxClipboard()
294 {
295 m_open = FALSE;
296
297 m_ownsClipboard = FALSE;
298 m_ownsPrimarySelection = FALSE;
299
300 m_data = (wxDataObject*) NULL;
301 m_receivedData = (wxDataObject*) NULL;
302
303 /* we use m_targetsWidget to query what formats are available */
304
305 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
306 gtk_widget_realize( m_targetsWidget );
307
308 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
309 "selection_received",
310 GTK_SIGNAL_FUNC( targets_selection_received ),
311 (gpointer) this );
312
313 /* we use m_clipboardWidget to get and to offer data */
314
315 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
316 gtk_widget_realize( m_clipboardWidget );
317
318 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
319 "selection_received",
320 GTK_SIGNAL_FUNC( selection_received ),
321 (gpointer) this );
322
323 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
324 "selection_clear_event",
325 GTK_SIGNAL_FUNC( selection_clear_clip ),
326 (gpointer) NULL );
327
328 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
329 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
330
331 m_formatSupported = FALSE;
332 m_targetRequested = 0;
333
334 m_usePrimary = FALSE;
335 }
336
337 wxClipboard::~wxClipboard()
338 {
339 Clear();
340
341 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
342 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
343 }
344
345 void wxClipboard::Clear()
346 {
347 if (m_data)
348 {
349 #if wxUSE_THREADS
350 /* disable GUI threads */
351 wxapp_uninstall_thread_wakeup();
352 #endif
353
354 /* As we have data we also own the clipboard. Once we no longer own
355 it, clear_selection is called which will set m_data to zero */
356 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
357 {
358 m_waiting = TRUE;
359
360 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
361 (guint32) GDK_CURRENT_TIME );
362
363 while (m_waiting) gtk_main_iteration();
364 }
365
366 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
367 {
368 m_waiting = TRUE;
369
370 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
371 (guint32) GDK_CURRENT_TIME );
372
373 while (m_waiting) gtk_main_iteration();
374 }
375
376 if (m_data)
377 {
378 delete m_data;
379 m_data = (wxDataObject*) NULL;
380 }
381
382 #if wxUSE_THREADS
383 /* re-enable GUI threads */
384 wxapp_install_thread_wakeup();
385 #endif
386 }
387
388 m_targetRequested = 0;
389 m_formatSupported = FALSE;
390 }
391
392 bool wxClipboard::Open()
393 {
394 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
395
396 m_open = TRUE;
397
398 return TRUE;
399 }
400
401 bool wxClipboard::SetData( wxDataObject *data )
402 {
403 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
404
405 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
406
407 Clear();
408
409 return AddData( data );
410 }
411
412 bool wxClipboard::AddData( wxDataObject *data )
413 {
414 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
415
416 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
417
418 /* we can only store one wxDataObject */
419 Clear();
420
421 m_data = data;
422
423 /* get formats from wxDataObjects */
424 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
425 m_data->GetAllFormats( array );
426
427 /* primary selection or clipboard */
428 GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
429 : g_clipboardAtom;
430
431
432 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
433 {
434 wxLogDebug( wxT("wxClipboard now supports atom %s"), array[i].GetId().c_str() );
435
436 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
437 clipboard,
438 array[i],
439 0 ); /* what is info ? */
440 }
441
442 delete[] array;
443
444 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
445 "selection_get",
446 GTK_SIGNAL_FUNC(selection_handler),
447 (gpointer) NULL );
448
449 #if wxUSE_THREADS
450 /* disable GUI threads */
451 wxapp_uninstall_thread_wakeup();
452 #endif
453
454 /* Tell the world we offer clipboard data */
455 bool res = (gtk_selection_owner_set( m_clipboardWidget,
456 clipboard,
457 (guint32) GDK_CURRENT_TIME ));
458
459 if (m_usePrimary)
460 m_ownsPrimarySelection = res;
461 else
462 m_ownsClipboard = res;
463
464 #if wxUSE_THREADS
465 /* re-enable GUI threads */
466 wxapp_install_thread_wakeup();
467 #endif
468
469 return res;
470 }
471
472 void wxClipboard::Close()
473 {
474 wxCHECK_RET( m_open, wxT("clipboard not open") );
475
476 m_open = FALSE;
477 }
478
479 bool wxClipboard::IsOpened() const
480 {
481 return m_open;
482 }
483
484 bool wxClipboard::IsSupported( const wxDataFormat& format )
485 {
486 /* reentrance problems */
487 if (m_open) return TRUE;
488
489 /* store requested format to be asked for by callbacks */
490 m_targetRequested = format;
491
492 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
493
494 m_formatSupported = FALSE;
495
496 /* perform query. this will set m_formatSupported to
497 TRUE if m_targetRequested is supported.
498 also, we have to wait for the "answer" from the
499 clipboard owner which is an asynchronous process.
500 therefore we set m_waiting = TRUE here and wait
501 until the callback "targets_selection_received"
502 sets it to FALSE */
503
504 m_waiting = TRUE;
505
506 gtk_selection_convert( m_targetsWidget,
507 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
508 : g_clipboardAtom,
509 g_targetsAtom,
510 (guint32) GDK_CURRENT_TIME );
511
512 while (m_waiting) gtk_main_iteration();
513
514 if (!m_formatSupported) return FALSE;
515
516 return TRUE;
517 }
518
519 bool wxClipboard::GetData( wxDataObject& data )
520 {
521 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
522
523 /* get formats from wxDataObjects */
524 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
525 data.GetAllFormats( array );
526
527 for (size_t i = 0; i < data.GetFormatCount(); i++)
528 {
529 wxDataFormat format( array[i] );
530
531 wxLogDebug( wxT("wxClipboard::GetData: requested format: %s"), format.GetId().c_str() );
532
533 /* is data supported by clipboard ? */
534
535 /* store requested format to be asked for by callbacks */
536 m_targetRequested = format;
537
538 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
539
540 m_formatSupported = FALSE;
541
542 /* perform query. this will set m_formatSupported to
543 TRUE if m_targetRequested is supported.
544 also, we have to wait for the "answer" from the
545 clipboard owner which is an asynchronous process.
546 therefore we set m_waiting = TRUE here and wait
547 until the callback "targets_selection_received"
548 sets it to FALSE */
549
550 m_waiting = TRUE;
551
552 gtk_selection_convert( m_targetsWidget,
553 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
554 : g_clipboardAtom,
555 g_targetsAtom,
556 (guint32) GDK_CURRENT_TIME );
557
558 while (m_waiting) gtk_main_iteration();
559
560 if (!m_formatSupported) continue;
561
562 /* store pointer to data object to be filled up by callbacks */
563 m_receivedData = &data;
564
565 /* store requested format to be asked for by callbacks */
566 m_targetRequested = format;
567
568 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
569
570 /* start query */
571 m_formatSupported = FALSE;
572
573 /* ask for clipboard contents. this will set
574 m_formatSupported to TRUE if m_targetRequested
575 is supported.
576 also, we have to wait for the "answer" from the
577 clipboard owner which is an asynchronous process.
578 therefore we set m_waiting = TRUE here and wait
579 until the callback "targets_selection_received"
580 sets it to FALSE */
581
582 m_waiting = TRUE;
583
584 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
585
586 gtk_selection_convert( m_clipboardWidget,
587 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
588 : g_clipboardAtom,
589 m_targetRequested,
590 (guint32) GDK_CURRENT_TIME );
591
592 while (m_waiting) gtk_main_iteration();
593
594 /* this is a true error as we checked for the presence of such data before */
595 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
596
597 /* return success */
598 delete[] array;
599 return TRUE;
600 }
601
602 wxLogDebug( wxT("wxClipboard::GetData: format not found") );
603
604 /* return failure */
605 delete[] array;
606 return FALSE;
607 }
608
609 #endif
610 // wxUSE_CLIPBOARD
611