]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/clipbrd.cpp
vi swap files added
[wxWidgets.git] / src / gtk1 / 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/utils.h"
19
20#include "glib.h"
21#include "gdk/gdk.h"
22#include "gtk/gtk.h"
23
24//-----------------------------------------------------------------------------
25// thread system
26//-----------------------------------------------------------------------------
27
28#if wxUSE_THREADS
29extern void wxapp_install_thread_wakeup();
30extern void wxapp_uninstall_thread_wakeup();
31#endif
32
33//-----------------------------------------------------------------------------
34// data
35//-----------------------------------------------------------------------------
36
37wxClipboard *wxTheClipboard = (wxClipboard*) NULL;
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)
80 {
81 clipboard->m_waiting = FALSE;
82 return;
83 }
84
85 if (selection_data->length <= 0)
86 {
87 clipboard->m_waiting = FALSE;
88 return;
89 }
90
91 /* make sure we got the data in the correct form */
92 if (selection_data->type != GDK_SELECTION_TYPE_ATOM)
93 {
94 clipboard->m_waiting = FALSE;
95 return;
96 }
97
98 // the atoms we received, holding a list of targets (= formats)
99 GdkAtom *atoms = (GdkAtom *)selection_data->data;
100
101 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
102 {
103/*
104 char *name = gdk_atom_name (atoms[i]);
105 if (name) printf( "Format available: %s.\n", name ); */
106
107 if (atoms[i] == clipboard->m_targetRequested)
108 {
109 clipboard->m_waiting = FALSE;
110 clipboard->m_formatSupported = TRUE;
111 return;
112 }
113 }
114
115 clipboard->m_waiting = FALSE;
116 return;
117}
118
119//-----------------------------------------------------------------------------
120// "selection_received" for the actual data
121//-----------------------------------------------------------------------------
122
123static void
124selection_received( GtkWidget *WXUNUSED(widget),
125 GtkSelectionData *selection_data,
126#if (GTK_MINOR_VERSION > 0)
127 guint32 WXUNUSED(time),
128#endif
129 wxClipboard *clipboard )
130{
131 if (!wxTheClipboard)
132 {
133 clipboard->m_waiting = FALSE;
134 return;
135 }
136
137 wxDataObject *data_object = clipboard->m_receivedData;
138
139 if (!data_object)
140 {
141 clipboard->m_waiting = FALSE;
142 return;
143 }
144
145 if (selection_data->length <= 0)
146 {
147 clipboard->m_waiting = FALSE;
148 return;
149 }
150
151 /* make sure we got the data in the correct format */
152 if (data_object->GetFormat() != selection_data->target)
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
161 switch (data_object->GetFormat().GetType())
162 {
163 case wxDF_TEXT:
164 {
165 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
166 {
167 clipboard->m_waiting = FALSE;
168 return;
169 }
170
171 wxTextDataObject *text_object = (wxTextDataObject *) data_object;
172
173 wxString text = (const char*) selection_data->data;
174
175 text_object->SetText( text );
176
177 break;
178 }
179
180 case wxDF_BITMAP:
181 {
182 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
183 {
184 clipboard->m_waiting = FALSE;
185 return;
186 }
187
188 wxBitmapDataObject *bitmap_object = (wxBitmapDataObject *) data_object;
189
190 bitmap_object->SetPngData( (const void*) selection_data->data, (size_t) selection_data->length );
191
192 break;
193 }
194
195 case wxDF_PRIVATE:
196 {
197 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
198 {
199 clipboard->m_waiting = FALSE;
200 return;
201 }
202
203 wxPrivateDataObject *private_object = (wxPrivateDataObject *) data_object;
204
205 private_object->SetData( (const char*) selection_data->data, (size_t) selection_data->length );
206
207 break;
208 }
209
210 default:
211 {
212 clipboard->m_waiting = FALSE;
213 return;
214 }
215 }
216
217 wxTheClipboard->m_formatSupported = TRUE;
218 clipboard->m_waiting = FALSE;
219}
220
221//-----------------------------------------------------------------------------
222// "selection_clear"
223//-----------------------------------------------------------------------------
224
225static gint
226selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
227{
228 if (!wxTheClipboard) return TRUE;
229
230 if (event->selection == GDK_SELECTION_PRIMARY)
231 {
232 wxTheClipboard->m_ownsPrimarySelection = FALSE;
233 }
234 else
235 if (event->selection == g_clipboardAtom)
236 {
237 wxTheClipboard->m_ownsClipboard = FALSE;
238 }
239 else
240 {
241 wxTheClipboard->m_waiting = FALSE;
242 return FALSE;
243 }
244
245 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
246 (!wxTheClipboard->m_ownsClipboard))
247 {
248 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
249 if (wxTheClipboard->m_data)
250 {
251 delete wxTheClipboard->m_data;
252 wxTheClipboard->m_data = (wxDataObject*) NULL;
253 }
254 }
255
256 wxTheClipboard->m_waiting = FALSE;
257 return TRUE;
258}
259
260//-----------------------------------------------------------------------------
261// selection handler for supplying data
262//-----------------------------------------------------------------------------
263
264static void
265selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, gpointer WXUNUSED(data) )
266{
267 if (!wxTheClipboard) return;
268
269 if (!wxTheClipboard->m_data) return;
270
271 wxDataObject *data = wxTheClipboard->m_data;
272
273 if (!data->IsSupportedFormat( selection_data->target )) return;
274
275 if (data->GetFormat().GetType() == wxDF_TEXT)
276 {
277 wxTextDataObject *text_object = (wxTextDataObject*) data;
278 wxString text( text_object->GetText() );
279
280#if wxUSE_UNICODE
281 const wxWX2MBbuf s = text.mbc_str();
282 int len = strlen(s);
283#else // more efficient in non-Unicode
284 const char *s = text.c_str();
285 int len = (int) text.Length();
286#endif
287 gtk_selection_data_set(
288 selection_data,
289 GDK_SELECTION_TYPE_STRING,
290 8*sizeof(gchar),
291 (unsigned char*) (const char*) s,
292 len );
293
294 return;
295 }
296
297 if (data->GetFormat().GetType() == wxDF_BITMAP)
298 {
299 wxBitmapDataObject *bitmap_object = (wxBitmapDataObject*) data;
300
301 if (bitmap_object->GetDataSize(wxDF_BITMAP) == 0) return;
302
303 gtk_selection_data_set(
304 selection_data,
305 GDK_SELECTION_TYPE_STRING,
306 8*sizeof(gchar),
307 (unsigned char*) bitmap_object->GetData(),
308 (int) bitmap_object->GetDataSize(wxDF_BITMAP) );
309
310 return;
311 }
312
313 int size = data->GetDataSize( selection_data->target );
314
315 if (size == 0) return;
316
317 char *d = new char[size];
318
319 data->GetDataHere( selection_data->target, (void*) d );
320
321 gtk_selection_data_set(
322 selection_data,
323 GDK_SELECTION_TYPE_STRING,
324 8*sizeof(gchar),
325 (unsigned char*) d,
326 size );
327}
328
329//-----------------------------------------------------------------------------
330// wxClipboard
331//-----------------------------------------------------------------------------
332
333IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
334
335wxClipboard::wxClipboard()
336{
337 m_open = FALSE;
338
339 m_ownsClipboard = FALSE;
340 m_ownsPrimarySelection = FALSE;
341
342 m_data = (wxDataObject*) NULL;
343 m_receivedData = (wxDataObject*) NULL;
344
345 /* we use m_targetsWidget to query what formats are available */
346
347 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
348 gtk_widget_realize( m_targetsWidget );
349
350 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
351 "selection_received",
352 GTK_SIGNAL_FUNC( targets_selection_received ),
353 (gpointer) this );
354
355 /* we use m_clipboardWidget to get and to offer data */
356
357 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
358 gtk_widget_realize( m_clipboardWidget );
359
360 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
361 "selection_received",
362 GTK_SIGNAL_FUNC( selection_received ),
363 (gpointer) this );
364
365 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
366 "selection_clear_event",
367 GTK_SIGNAL_FUNC( selection_clear_clip ),
368 (gpointer) NULL );
369
370 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
371 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
372
373 m_formatSupported = FALSE;
374 m_targetRequested = 0;
375
376 m_usePrimary = FALSE;
377}
378
379wxClipboard::~wxClipboard()
380{
381 Clear();
382
383 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
384 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
385}
386
387void wxClipboard::Clear()
388{
389 if (m_data)
390 {
391#if wxUSE_THREADS
392 /* disable GUI threads */
393 wxapp_uninstall_thread_wakeup();
394#endif
395
396 /* As we have data we also own the clipboard. Once we no longer own
397 it, clear_selection is called which will set m_data to zero */
398 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
399 {
400 m_waiting = TRUE;
401
402 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, GDK_CURRENT_TIME );
403
404 while (m_waiting) gtk_main_iteration();
405 }
406
407 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
408 {
409 m_waiting = TRUE;
410
411 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME );
412
413 while (m_waiting) gtk_main_iteration();
414 }
415
416 if (m_data)
417 {
418 delete m_data;
419 m_data = (wxDataObject*) NULL;
420 }
421
422#if wxUSE_THREADS
423 /* re-enable GUI threads */
424 wxapp_install_thread_wakeup();
425#endif
426 }
427
428 m_targetRequested = 0;
429 m_formatSupported = FALSE;
430}
431
432bool wxClipboard::Open()
433{
434 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
435
436 m_open = TRUE;
437
438 return TRUE;
439}
440
441bool wxClipboard::SetData( wxDataObject *data )
442{
443 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
444
445 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
446
447 Clear();
448
449 return AddData( data );
450}
451
452bool wxClipboard::AddData( wxDataObject *data )
453{
454 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
455
456 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
457
458 // we can only store one wxDataObject
459 Clear();
460
461 m_data = data;
462
463 /* get native format id of new data object */
464 GdkAtom format = data->GetFormat();
465
466 wxCHECK_MSG( format, FALSE, wxT("data has invalid format") );
467
468 /* This should happen automatically, but to be on the safe side */
469 m_ownsClipboard = FALSE;
470 m_ownsPrimarySelection = FALSE;
471
472 /* Add handlers if someone requests data */
473
474#if (GTK_MINOR_VERSION > 0)
475
476 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
477 GDK_SELECTION_PRIMARY,
478 format,
479 0 ); /* what is info ? */
480
481 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
482 g_clipboardAtom,
483 format,
484 0 ); /* what is info ? */
485
486 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
487 "selection_get",
488 GTK_SIGNAL_FUNC(selection_handler),
489 (gpointer) NULL );
490
491#else
492
493 gtk_selection_add_handler( m_clipboardWidget,
494 g_clipboardAtom,
495 format,
496 selection_handler,
497 (gpointer) NULL );
498
499 gtk_selection_add_handler( m_clipboardWidget,
500 GDK_SELECTION_PRIMARY,
501 format,
502 selection_handler,
503 (gpointer) NULL );
504#endif
505
506#if wxUSE_THREADS
507 /* disable GUI threads */
508 wxapp_uninstall_thread_wakeup();
509#endif
510
511 /* Tell the world we offer clipboard data */
512 if (!gtk_selection_owner_set( m_clipboardWidget,
513 g_clipboardAtom,
514 GDK_CURRENT_TIME ))
515 {
516#if wxUSE_THREADS
517 /* re-enable GUI threads */
518 wxapp_install_thread_wakeup();
519#endif
520 return FALSE;
521 }
522 m_ownsClipboard = TRUE;
523
524 if (!gtk_selection_owner_set( m_clipboardWidget,
525 GDK_SELECTION_PRIMARY,
526 GDK_CURRENT_TIME ))
527 {
528#if wxUSE_THREADS
529 /* re-enable GUI threads */
530 wxapp_install_thread_wakeup();
531#endif
532 return FALSE;
533 }
534 m_ownsPrimarySelection = TRUE;
535
536#if wxUSE_THREADS
537 /* re-enable GUI threads */
538 wxapp_install_thread_wakeup();
539#endif
540
541 return TRUE;
542}
543
544void wxClipboard::Close()
545{
546 wxCHECK_RET( m_open, wxT("clipboard not open") );
547
548 m_open = FALSE;
549}
550
551bool wxClipboard::IsSupported( wxDataFormat format )
552{
553 /* store requested format to be asked for by callbacks */
554
555 m_targetRequested = format;
556
557 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
558
559 m_formatSupported = FALSE;
560
561 /* perform query. this will set m_formatSupported to
562 TRUE if m_targetRequested is supported.
563 alsom we have to wait for the "answer" from the
564 clipboard owner which is an asynchronous process.
565 therefore we set m_waiting = TRUE here and wait
566 until the callback "targets_selection_received"
567 sets it to FALSE */
568
569 m_waiting = TRUE;
570
571 gtk_selection_convert( m_targetsWidget,
572 m_usePrimary ? GDK_SELECTION_PRIMARY : g_clipboardAtom,
573 g_targetsAtom,
574 GDK_CURRENT_TIME );
575
576 while (m_waiting) gtk_main_iteration();
577
578 if (!m_formatSupported) return FALSE;
579
580 return TRUE;
581}
582
583bool wxClipboard::GetData( wxDataObject *data )
584{
585 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
586
587 /* is data supported by clipboard ? */
588
589 if (!IsSupported( data->GetFormat() )) return FALSE;
590
591 /* store pointer to data object to be filled up by callbacks */
592
593 m_receivedData = data;
594
595 /* store requested format to be asked for by callbacks */
596
597 m_targetRequested = data->GetFormat();
598
599 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
600
601 /* start query */
602
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 gtk_selection_convert( m_clipboardWidget,
617 m_usePrimary ? GDK_SELECTION_PRIMARY : g_clipboardAtom,
618 m_targetRequested,
619 GDK_CURRENT_TIME );
620
621 while (m_waiting) gtk_main_iteration();
622
623 /* this is a true error as we checked for the presence of such data before */
624
625 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
626
627 return TRUE;
628}
629
630//-----------------------------------------------------------------------------
631// wxClipboardModule
632//-----------------------------------------------------------------------------
633
634IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
635
636bool wxClipboardModule::OnInit()
637{
638 wxTheClipboard = new wxClipboard();
639
640 return TRUE;
641}
642
643void wxClipboardModule::OnExit()
644{
645 if (wxTheClipboard) delete wxTheClipboard;
646 wxTheClipboard = (wxClipboard*) NULL;
647}
648
649#endif
650
651 // wxUSE_CLIPBOARD
652