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