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