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