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