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