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