]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/clipbrd.cpp
simplify/cleanup wxTheXXXList and wxGDIObject code (patch 1452023 from Paul Cornett)
[wxWidgets.git] / src / gtk1 / 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 GdkAtom g_timestampAtom = 0;
39
40 // the trace mask we use with wxLogTrace() - call
41 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
42 // (there will be a *lot* of them!)
43 static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
44
45 //-----------------------------------------------------------------------------
46 // reminder
47 //-----------------------------------------------------------------------------
48
49 /* The contents of a selection are returned in a GtkSelectionData
50 structure. selection/target identify the request.
51 type specifies the type of the return; if length < 0, and
52 the data should be ignored. This structure has object semantics -
53 no fields should be modified directly, they should not be created
54 directly, and pointers to them should not be stored beyond the duration of
55 a callback. (If the last is changed, we'll need to add reference
56 counting)
57
58 struct _GtkSelectionData
59 {
60 GdkAtom selection;
61 GdkAtom target;
62 GdkAtom type;
63 gint format;
64 guchar *data;
65 gint length;
66 };
67
68 */
69
70 //-----------------------------------------------------------------------------
71 // "selection_received" for targets
72 //-----------------------------------------------------------------------------
73
74 extern "C" {
75 static void
76 targets_selection_received( GtkWidget *WXUNUSED(widget),
77 GtkSelectionData *selection_data,
78 guint32 WXUNUSED(time),
79 wxClipboard *clipboard )
80 {
81 if ( wxTheClipboard && selection_data->length > 0 )
82 {
83 // make sure we got the data in the correct form
84 GdkAtom type = selection_data->type;
85 if ( type != GDK_SELECTION_TYPE_ATOM )
86 {
87 gchar* atom_name = gdk_atom_name(type);
88 if ( strcmp(atom_name, "TARGETS") )
89 {
90 wxLogTrace( TRACE_CLIPBOARD,
91 _T("got unsupported clipboard target") );
92
93 clipboard->m_waiting = FALSE;
94 g_free(atom_name);
95 return;
96 }
97 g_free(atom_name);
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 signal_data )
247 {
248 if (!wxTheClipboard) return;
249
250 if (!wxTheClipboard->m_data) return;
251
252 wxDataObject *data = wxTheClipboard->m_data;
253
254 // ICCCM says that TIMESTAMP is a required atom.
255 // In particular, it satisfies Klipper, which polls
256 // TIMESTAMP to see if the clipboards content has changed.
257 // It shall return the time which was used to set the data.
258 if (selection_data->target == g_timestampAtom)
259 {
260 guint timestamp = GPOINTER_TO_UINT (signal_data);
261 gtk_selection_data_set(selection_data,
262 GDK_SELECTION_TYPE_INTEGER,
263 32,
264 (guchar*)&(timestamp),
265 sizeof(timestamp));
266 wxLogTrace(TRACE_CLIPBOARD,
267 _T("Clipboard TIMESTAMP requested, returning timestamp=%u"),
268 timestamp);
269 return;
270 }
271
272 wxDataFormat format( selection_data->target );
273
274 #ifdef __WXDEBUG__
275 wxLogTrace(TRACE_CLIPBOARD,
276 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s timestamp=%u"),
277 format.GetId().c_str(),
278 wxString::FromAscii(gdk_atom_name(selection_data->target)).c_str(),
279 wxString::FromAscii(gdk_atom_name(selection_data->type)).c_str(),
280 wxString::FromAscii(gdk_atom_name(selection_data->selection)).c_str(),
281 GPOINTER_TO_UINT( signal_data )
282 );
283 #endif
284
285 if (!data->IsSupportedFormat( format )) return;
286
287 int size = data->GetDataSize( format );
288
289 if (size == 0) return;
290
291 void *d = malloc(size);
292
293 // Text data will be in UTF8 in Unicode mode.
294 data->GetDataHere( selection_data->target, d );
295
296 gtk_selection_data_set(
297 selection_data,
298 GDK_SELECTION_TYPE_STRING,
299 8 * sizeof(gchar),
300 (unsigned char*) d,
301 size );
302
303 free(d);
304 }
305 }
306
307 //-----------------------------------------------------------------------------
308 // wxClipboard
309 //-----------------------------------------------------------------------------
310
311 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
312
313 wxClipboard::wxClipboard()
314 {
315 m_open = FALSE;
316 m_waiting = FALSE;
317
318 m_ownsClipboard = FALSE;
319 m_ownsPrimarySelection = FALSE;
320
321 m_data = (wxDataObject*) NULL;
322 m_receivedData = (wxDataObject*) NULL;
323
324 /* we use m_targetsWidget to query what formats are available */
325
326 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
327 gtk_widget_realize( m_targetsWidget );
328
329 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
330 "selection_received",
331 GTK_SIGNAL_FUNC( targets_selection_received ),
332 (gpointer) this );
333
334 /* we use m_clipboardWidget to get and to offer data */
335
336 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
337 gtk_widget_realize( m_clipboardWidget );
338
339 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
340 "selection_received",
341 GTK_SIGNAL_FUNC( selection_received ),
342 (gpointer) this );
343
344 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
345 "selection_clear_event",
346 GTK_SIGNAL_FUNC( selection_clear_clip ),
347 (gpointer) NULL );
348
349 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
350 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
351 if (!g_timestampAtom) g_timestampAtom = gdk_atom_intern ("TIMESTAMP", FALSE);
352
353 m_formatSupported = FALSE;
354 m_targetRequested = 0;
355
356 m_usePrimary = FALSE;
357 }
358
359 wxClipboard::~wxClipboard()
360 {
361 Clear();
362
363 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
364 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
365 }
366
367 void wxClipboard::Clear()
368 {
369 if (m_data)
370 {
371 #if wxUSE_THREADS
372 /* disable GUI threads */
373 #endif
374
375 // As we have data we also own the clipboard. Once we no longer own
376 // it, clear_selection is called which will set m_data to zero
377 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
378 {
379 m_waiting = TRUE;
380
381 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
382 (guint32) GDK_CURRENT_TIME );
383
384 while (m_waiting) gtk_main_iteration();
385 }
386
387 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
388 {
389 m_waiting = TRUE;
390
391 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
392 (guint32) GDK_CURRENT_TIME );
393
394 while (m_waiting) gtk_main_iteration();
395 }
396
397 if (m_data)
398 {
399 delete m_data;
400 m_data = (wxDataObject*) NULL;
401 }
402
403 #if wxUSE_THREADS
404 /* re-enable GUI threads */
405 #endif
406 }
407
408 m_targetRequested = 0;
409 m_formatSupported = FALSE;
410 }
411
412 bool wxClipboard::Open()
413 {
414 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
415
416 m_open = TRUE;
417
418 return TRUE;
419 }
420
421 bool wxClipboard::SetData( wxDataObject *data )
422 {
423 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
424
425 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
426
427 Clear();
428
429 return AddData( data );
430 }
431
432 bool wxClipboard::AddData( wxDataObject *data )
433 {
434 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
435
436 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
437
438 // we can only store one wxDataObject
439 Clear();
440
441 m_data = data;
442
443 // get formats from wxDataObjects
444 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
445 m_data->GetAllFormats( array );
446
447 // primary selection or clipboard
448 GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
449 : g_clipboardAtom;
450
451 // by default provide TIMESTAMP as a target
452 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
453 clipboard,
454 g_timestampAtom,
455 0 );
456
457 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
458 {
459 wxLogTrace( TRACE_CLIPBOARD,
460 wxT("wxClipboard now supports atom %s"),
461 array[i].GetId().c_str() );
462
463 // printf( "added %s\n",
464 // gdk_atom_name( array[i].GetFormatId() ) );
465
466 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
467 clipboard,
468 array[i],
469 0 ); /* what is info ? */
470 }
471
472 delete[] array;
473
474 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
475 "selection_get",
476 GTK_SIGNAL_FUNC(selection_handler),
477 GUINT_TO_POINTER( gdk_event_get_time(gtk_get_current_event()) ) );
478
479 #if wxUSE_THREADS
480 /* disable GUI threads */
481 #endif
482
483 /* Tell the world we offer clipboard data */
484 bool res = (gtk_selection_owner_set( m_clipboardWidget,
485 clipboard,
486 (guint32) GDK_CURRENT_TIME ));
487
488 if (m_usePrimary)
489 m_ownsPrimarySelection = res;
490 else
491 m_ownsClipboard = res;
492
493 #if wxUSE_THREADS
494 /* re-enable GUI threads */
495 #endif
496
497 return res;
498 }
499
500 void wxClipboard::Close()
501 {
502 wxCHECK_RET( m_open, wxT("clipboard not open") );
503
504 m_open = FALSE;
505 }
506
507 bool wxClipboard::IsOpened() const
508 {
509 return m_open;
510 }
511
512 bool wxClipboard::IsSupported( const wxDataFormat& format )
513 {
514 /* reentrance problems */
515 if (m_waiting) return FALSE;
516
517 /* store requested format to be asked for by callbacks */
518 m_targetRequested = format;
519
520 wxLogTrace( TRACE_CLIPBOARD,
521 wxT("wxClipboard:IsSupported: requested format: %s"),
522 format.GetId().c_str() );
523
524 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
525
526 m_formatSupported = FALSE;
527
528 /* perform query. this will set m_formatSupported to
529 TRUE if m_targetRequested is supported.
530 also, we have to wait for the "answer" from the
531 clipboard owner which is an asynchronous process.
532 therefore we set m_waiting = TRUE here and wait
533 until the callback "targets_selection_received"
534 sets it to FALSE */
535
536 m_waiting = TRUE;
537
538 gtk_selection_convert( m_targetsWidget,
539 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
540 : g_clipboardAtom,
541 g_targetsAtom,
542 (guint32) GDK_CURRENT_TIME );
543
544 while (m_waiting) gtk_main_iteration();
545
546 return m_formatSupported;
547 }
548
549 bool wxClipboard::GetData( wxDataObject& data )
550 {
551 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
552
553 /* get formats from wxDataObjects */
554 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
555 data.GetAllFormats( array );
556
557 for (size_t i = 0; i < data.GetFormatCount(); i++)
558 {
559 wxDataFormat format( array[i] );
560
561 wxLogTrace( TRACE_CLIPBOARD,
562 wxT("wxClipboard::GetData: requested format: %s"),
563 format.GetId().c_str() );
564
565 /* is data supported by clipboard ? */
566
567 /* store requested format to be asked for by callbacks */
568 m_targetRequested = format;
569
570 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
571
572 m_formatSupported = FALSE;
573
574 /* perform query. this will set m_formatSupported to
575 TRUE if m_targetRequested 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 gtk_selection_convert( m_targetsWidget,
585 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
586 : g_clipboardAtom,
587 g_targetsAtom,
588 (guint32) GDK_CURRENT_TIME );
589
590 while (m_waiting) gtk_main_iteration();
591
592 if (!m_formatSupported) continue;
593
594 /* store pointer to data object to be filled up by callbacks */
595 m_receivedData = &data;
596
597 /* store requested format to be asked for by callbacks */
598 m_targetRequested = format;
599
600 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
601
602 /* start query */
603 m_formatSupported = FALSE;
604
605 /* ask for clipboard contents. this will set
606 m_formatSupported to TRUE if m_targetRequested
607 is supported.
608 also, we have to wait for the "answer" from the
609 clipboard owner which is an asynchronous process.
610 therefore we set m_waiting = TRUE here and wait
611 until the callback "targets_selection_received"
612 sets it to FALSE */
613
614 m_waiting = TRUE;
615
616 wxLogTrace( TRACE_CLIPBOARD,
617 wxT("wxClipboard::GetData: format found, start convert") );
618
619 gtk_selection_convert( m_clipboardWidget,
620 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
621 : g_clipboardAtom,
622 m_targetRequested,
623 (guint32) GDK_CURRENT_TIME );
624
625 while (m_waiting) gtk_main_iteration();
626
627 /* this is a true error as we checked for the presence of such data before */
628 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
629
630 /* return success */
631 delete[] array;
632 return TRUE;
633 }
634
635 wxLogTrace( TRACE_CLIPBOARD,
636 wxT("wxClipboard::GetData: format not found") );
637
638 /* return failure */
639 delete[] array;
640 return FALSE;
641 }
642
643 #endif
644 // wxUSE_CLIPBOARD
645