Nuke GTK1 from src/gtk
[wxWidgets.git] / src / gtk / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/clipbrd.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/clipbrd.h"
14
15 #if wxUSE_CLIPBOARD
16
17 #include "wx/dataobj.h"
18 #include "wx/utils.h"
19 #include "wx/log.h"
20
21 #include <glib.h>
22 #include <gdk/gdk.h>
23 #include <gtk/gtk.h>
24
25 //-----------------------------------------------------------------------------
26 // thread system
27 //-----------------------------------------------------------------------------
28
29 #if wxUSE_THREADS
30 #endif
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 GdkAtom g_clipboardAtom = 0;
37 GdkAtom g_targetsAtom = 0;
38
39 #if defined(__WXGTK20__) && wxUSE_UNICODE
40 extern GdkAtom g_altTextAtom;
41 #endif
42
43 // the trace mask we use with wxLogTrace() - call
44 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
45 // (there will be a *lot* of them!)
46 static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
47
48 //-----------------------------------------------------------------------------
49 // reminder
50 //-----------------------------------------------------------------------------
51
52 /* The contents of a selection are returned in a GtkSelectionData
53 structure. selection/target identify the request.
54 type specifies the type of the return; if length < 0, and
55 the data should be ignored. This structure has object semantics -
56 no fields should be modified directly, they should not be created
57 directly, and pointers to them should not be stored beyond the duration of
58 a callback. (If the last is changed, we'll need to add reference
59 counting)
60
61 struct _GtkSelectionData
62 {
63 GdkAtom selection;
64 GdkAtom target;
65 GdkAtom type;
66 gint format;
67 guchar *data;
68 gint length;
69 };
70
71 */
72
73 //-----------------------------------------------------------------------------
74 // "selection_received" for targets
75 //-----------------------------------------------------------------------------
76
77 extern "C" {
78 static void
79 targets_selection_received( GtkWidget *WXUNUSED(widget),
80 GtkSelectionData *selection_data,
81 guint32 WXUNUSED(time),
82 wxClipboard *clipboard )
83 {
84 if ( wxTheClipboard && selection_data->length > 0 )
85 {
86 // make sure we got the data in the correct form
87 GdkAtom type = selection_data->type;
88 if ( type != GDK_SELECTION_TYPE_ATOM )
89 {
90 if ( strcmp(gdk_atom_name(type), "TARGETS") )
91 {
92 wxLogTrace( TRACE_CLIPBOARD,
93 _T("got unsupported clipboard target") );
94
95 clipboard->m_waiting = FALSE;
96 return;
97 }
98 }
99
100 #ifdef __WXDEBUG__
101 wxDataFormat clip( selection_data->selection );
102 wxLogTrace( TRACE_CLIPBOARD,
103 wxT("selection received for targets, clipboard %s"),
104 clip.GetId().c_str() );
105 #endif // __WXDEBUG__
106
107 // the atoms we received, holding a list of targets (= formats)
108 GdkAtom *atoms = (GdkAtom *)selection_data->data;
109
110 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
111 {
112 wxDataFormat format( atoms[i] );
113
114 wxLogTrace( TRACE_CLIPBOARD,
115 wxT("selection received for targets, format %s"),
116 format.GetId().c_str() );
117
118 // printf( "format %s requested %s\n",
119 // gdk_atom_name( atoms[i] ),
120 // gdk_atom_name( clipboard->m_targetRequested ) );
121
122 if (format == clipboard->m_targetRequested)
123 {
124 clipboard->m_waiting = FALSE;
125 clipboard->m_formatSupported = TRUE;
126 return;
127 }
128 }
129 }
130
131 clipboard->m_waiting = FALSE;
132 }
133 }
134
135 //-----------------------------------------------------------------------------
136 // "selection_received" for the actual data
137 //-----------------------------------------------------------------------------
138
139 extern "C" {
140 static void
141 selection_received( GtkWidget *WXUNUSED(widget),
142 GtkSelectionData *selection_data,
143 guint32 WXUNUSED(time),
144 wxClipboard *clipboard )
145 {
146 if (!wxTheClipboard)
147 {
148 clipboard->m_waiting = FALSE;
149 return;
150 }
151
152 wxDataObject *data_object = clipboard->m_receivedData;
153
154 if (!data_object)
155 {
156 clipboard->m_waiting = FALSE;
157 return;
158 }
159
160 if (selection_data->length <= 0)
161 {
162 clipboard->m_waiting = FALSE;
163 return;
164 }
165
166 wxDataFormat format( selection_data->target );
167
168 // make sure we got the data in the correct format
169 if (!data_object->IsSupportedFormat( format ) )
170 {
171 clipboard->m_waiting = FALSE;
172 return;
173 }
174
175 #if 0
176 This seems to cause problems somehow
177 // make sure we got the data in the correct form (selection type).
178 // if so, copy data to target object
179 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
180 {
181 clipboard->m_waiting = FALSE;
182 return;
183 }
184 #endif
185
186 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
187
188 wxTheClipboard->m_formatSupported = TRUE;
189 clipboard->m_waiting = FALSE;
190 }
191 }
192
193 //-----------------------------------------------------------------------------
194 // "selection_clear"
195 //-----------------------------------------------------------------------------
196
197 extern "C" {
198 static gint
199 selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
200 {
201 if (!wxTheClipboard) return TRUE;
202
203 if (event->selection == GDK_SELECTION_PRIMARY)
204 {
205 wxTheClipboard->m_ownsPrimarySelection = FALSE;
206 }
207 else
208 if (event->selection == g_clipboardAtom)
209 {
210 wxTheClipboard->m_ownsClipboard = FALSE;
211 }
212 else
213 {
214 wxTheClipboard->m_waiting = FALSE;
215 return FALSE;
216 }
217
218 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
219 (!wxTheClipboard->m_ownsClipboard))
220 {
221 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
222 if (wxTheClipboard->m_data)
223 {
224 wxLogTrace(TRACE_CLIPBOARD, wxT("wxClipboard will get cleared" ));
225
226 delete wxTheClipboard->m_data;
227 wxTheClipboard->m_data = (wxDataObject*) NULL;
228 }
229 }
230
231 wxTheClipboard->m_waiting = FALSE;
232 return TRUE;
233 }
234 }
235
236 //-----------------------------------------------------------------------------
237 // selection handler for supplying data
238 //-----------------------------------------------------------------------------
239
240 extern "C" {
241 static void
242 selection_handler( GtkWidget *WXUNUSED(widget),
243 GtkSelectionData *selection_data,
244 guint WXUNUSED(info),
245 guint WXUNUSED(time),
246 gpointer WXUNUSED(data) )
247 {
248 if (!wxTheClipboard) return;
249
250 if (!wxTheClipboard->m_data) return;
251
252 wxDataObject *data = wxTheClipboard->m_data;
253
254 wxDataFormat format( selection_data->target );
255
256 #ifdef __WXDEBUG__
257 wxLogTrace(TRACE_CLIPBOARD,
258 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s"),
259 format.GetId().c_str(),
260 wxString::FromAscii(gdk_atom_name(selection_data->target)).c_str(),
261 wxString::FromAscii(gdk_atom_name(selection_data->type)).c_str(),
262 wxString::FromAscii(gdk_atom_name(selection_data->selection)).c_str()
263 );
264 #endif
265
266 if (!data->IsSupportedFormat( format )) return;
267
268 int size = data->GetDataSize( format );
269
270 if (size == 0) return;
271
272 void *d = malloc(size);
273
274 // Text data will be in UTF8 in Unicode mode.
275 data->GetDataHere( selection_data->target, d );
276
277 // NB: GTK+ requires special treatment of UTF8_STRING data, the text
278 // would show as UTF-8 data interpreted as latin1 (?) in other
279 // GTK+ apps if we used gtk_selection_data_set()
280 if (format == wxDataFormat(wxDF_UNICODETEXT))
281 {
282 gtk_selection_data_set_text(
283 selection_data,
284 (const gchar*)d,
285 size-1 );
286 }
287 else
288 {
289 gtk_selection_data_set(
290 selection_data,
291 GDK_SELECTION_TYPE_STRING,
292 8*sizeof(gchar),
293 (unsigned char*) d,
294 size-1 );
295 }
296
297 free(d);
298 }
299 }
300
301 //-----------------------------------------------------------------------------
302 // wxClipboard
303 //-----------------------------------------------------------------------------
304
305 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
306
307 wxClipboard::wxClipboard()
308 {
309 m_open = FALSE;
310 m_waiting = FALSE;
311
312 m_ownsClipboard = FALSE;
313 m_ownsPrimarySelection = FALSE;
314
315 m_data = (wxDataObject*) NULL;
316 m_receivedData = (wxDataObject*) NULL;
317
318 /* we use m_targetsWidget to query what formats are available */
319
320 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
321 gtk_widget_realize( m_targetsWidget );
322
323 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
324 "selection_received",
325 GTK_SIGNAL_FUNC( targets_selection_received ),
326 (gpointer) this );
327
328 /* we use m_clipboardWidget to get and to offer data */
329
330 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
331 gtk_widget_realize( m_clipboardWidget );
332
333 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
334 "selection_received",
335 GTK_SIGNAL_FUNC( selection_received ),
336 (gpointer) this );
337
338 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
339 "selection_clear_event",
340 GTK_SIGNAL_FUNC( selection_clear_clip ),
341 (gpointer) NULL );
342
343 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
344 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
345
346 m_formatSupported = FALSE;
347 m_targetRequested = 0;
348
349 m_usePrimary = FALSE;
350 }
351
352 wxClipboard::~wxClipboard()
353 {
354 Clear();
355
356 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
357 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
358 }
359
360 void wxClipboard::Clear()
361 {
362 if (m_data)
363 {
364 #if wxUSE_THREADS
365 /* disable GUI threads */
366 #endif
367
368 // As we have data we also own the clipboard. Once we no longer own
369 // it, clear_selection is called which will set m_data to zero
370 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
371 {
372 m_waiting = TRUE;
373
374 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
375 (guint32) GDK_CURRENT_TIME );
376
377 while (m_waiting) gtk_main_iteration();
378 }
379
380 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
381 {
382 m_waiting = TRUE;
383
384 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
385 (guint32) GDK_CURRENT_TIME );
386
387 while (m_waiting) gtk_main_iteration();
388 }
389
390 if (m_data)
391 {
392 delete m_data;
393 m_data = (wxDataObject*) NULL;
394 }
395
396 #if wxUSE_THREADS
397 /* re-enable GUI threads */
398 #endif
399 }
400
401 m_targetRequested = 0;
402 m_formatSupported = FALSE;
403 }
404
405 bool wxClipboard::Open()
406 {
407 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
408
409 m_open = TRUE;
410
411 return TRUE;
412 }
413
414 bool wxClipboard::SetData( wxDataObject *data )
415 {
416 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
417
418 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
419
420 Clear();
421
422 return AddData( data );
423 }
424
425 bool wxClipboard::AddData( wxDataObject *data )
426 {
427 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
428
429 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
430
431 // we can only store one wxDataObject
432 Clear();
433
434 m_data = data;
435
436 // get formats from wxDataObjects
437 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
438 m_data->GetAllFormats( array );
439
440 // primary selection or clipboard
441 GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
442 : g_clipboardAtom;
443
444
445 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
446 {
447 wxLogTrace( TRACE_CLIPBOARD,
448 wxT("wxClipboard now supports atom %s"),
449 array[i].GetId().c_str() );
450
451 // printf( "added %s\n",
452 // gdk_atom_name( array[i].GetFormatId() ) );
453
454 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
455 clipboard,
456 array[i],
457 0 ); /* what is info ? */
458 }
459
460 delete[] array;
461
462 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
463 "selection_get",
464 GTK_SIGNAL_FUNC(selection_handler),
465 (gpointer) NULL );
466
467 #if wxUSE_THREADS
468 /* disable GUI threads */
469 #endif
470
471 /* Tell the world we offer clipboard data */
472 bool res = (gtk_selection_owner_set( m_clipboardWidget,
473 clipboard,
474 (guint32) GDK_CURRENT_TIME ));
475
476 if (m_usePrimary)
477 m_ownsPrimarySelection = res;
478 else
479 m_ownsClipboard = res;
480
481 #if wxUSE_THREADS
482 /* re-enable GUI threads */
483 #endif
484
485 return res;
486 }
487
488 void wxClipboard::Close()
489 {
490 wxCHECK_RET( m_open, wxT("clipboard not open") );
491
492 m_open = FALSE;
493 }
494
495 bool wxClipboard::IsOpened() const
496 {
497 return m_open;
498 }
499
500 bool wxClipboard::IsSupported( const wxDataFormat& format )
501 {
502 /* reentrance problems */
503 if (m_waiting) return FALSE;
504
505 /* store requested format to be asked for by callbacks */
506 m_targetRequested = format;
507
508 wxLogTrace( TRACE_CLIPBOARD,
509 wxT("wxClipboard:IsSupported: requested format: %s"),
510 format.GetId().c_str() );
511
512 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
513
514 m_formatSupported = FALSE;
515
516 /* perform query. this will set m_formatSupported to
517 TRUE if m_targetRequested is supported.
518 also, we have to wait for the "answer" from the
519 clipboard owner which is an asynchronous process.
520 therefore we set m_waiting = TRUE here and wait
521 until the callback "targets_selection_received"
522 sets it to FALSE */
523
524 m_waiting = TRUE;
525
526 gtk_selection_convert( m_targetsWidget,
527 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
528 : g_clipboardAtom,
529 g_targetsAtom,
530 (guint32) GDK_CURRENT_TIME );
531
532 while (m_waiting) gtk_main_iteration();
533
534 #if defined(__WXGTK20__) && wxUSE_UNICODE
535 if (!m_formatSupported && format == wxDataFormat(wxDF_UNICODETEXT))
536 {
537 // Another try with plain STRING format
538 extern GdkAtom g_altTextAtom;
539 return IsSupported(g_altTextAtom);
540 }
541 #endif
542
543 return m_formatSupported;
544 }
545
546 bool wxClipboard::GetData( wxDataObject& data )
547 {
548 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
549
550 /* get formats from wxDataObjects */
551 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
552 data.GetAllFormats( array );
553
554 for (size_t i = 0; i < data.GetFormatCount(); i++)
555 {
556 wxDataFormat format( array[i] );
557
558 wxLogTrace( TRACE_CLIPBOARD,
559 wxT("wxClipboard::GetData: requested format: %s"),
560 format.GetId().c_str() );
561
562 /* is data supported by clipboard ? */
563
564 /* store requested format to be asked for by callbacks */
565 m_targetRequested = format;
566
567 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
568
569 m_formatSupported = FALSE;
570
571 /* perform query. this will set m_formatSupported to
572 TRUE if m_targetRequested is supported.
573 also, we have to wait for the "answer" from the
574 clipboard owner which is an asynchronous process.
575 therefore we set m_waiting = TRUE here and wait
576 until the callback "targets_selection_received"
577 sets it to FALSE */
578
579 m_waiting = TRUE;
580
581 gtk_selection_convert( m_targetsWidget,
582 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
583 : g_clipboardAtom,
584 g_targetsAtom,
585 (guint32) GDK_CURRENT_TIME );
586
587 while (m_waiting) gtk_main_iteration();
588
589 if (!m_formatSupported) continue;
590
591 /* store pointer to data object to be filled up by callbacks */
592 m_receivedData = &data;
593
594 /* store requested format to be asked for by callbacks */
595 m_targetRequested = format;
596
597 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
598
599 /* start query */
600 m_formatSupported = FALSE;
601
602 /* ask for clipboard contents. this will set
603 m_formatSupported to TRUE if m_targetRequested
604 is supported.
605 also, we have to wait for the "answer" from the
606 clipboard owner which is an asynchronous process.
607 therefore we set m_waiting = TRUE here and wait
608 until the callback "targets_selection_received"
609 sets it to FALSE */
610
611 m_waiting = TRUE;
612
613 wxLogTrace( TRACE_CLIPBOARD,
614 wxT("wxClipboard::GetData: format found, start convert") );
615
616 gtk_selection_convert( m_clipboardWidget,
617 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
618 : g_clipboardAtom,
619 m_targetRequested,
620 (guint32) GDK_CURRENT_TIME );
621
622 while (m_waiting) gtk_main_iteration();
623
624 /* this is a true error as we checked for the presence of such data before */
625 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
626
627 /* return success */
628 delete[] array;
629 return TRUE;
630 }
631
632 wxLogTrace( TRACE_CLIPBOARD,
633 wxT("wxClipboard::GetData: format not found") );
634
635 /* return failure */
636 delete[] array;
637 return FALSE;
638 }
639
640 #endif
641 // wxUSE_CLIPBOARD
642