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