]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/clipbrd.cpp
fixed outdated filenames
[wxWidgets.git] / src / gtk1 / clipbrd.cpp
... / ...
CommitLineData
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#endif
32
33//-----------------------------------------------------------------------------
34// data
35//-----------------------------------------------------------------------------
36
37GdkAtom g_clipboardAtom = 0;
38GdkAtom g_targetsAtom = 0;
39
40// the trace mask we use with wxLogTrace() - call
41// wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
42// (there will be a *lot* of them!)
43static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
44
45//-----------------------------------------------------------------------------
46// reminder
47//-----------------------------------------------------------------------------
48
49/* The contents of a selection are returned in a GtkSelectionData
50 structure. selection/target identify the request.
51 type specifies the type of the return; if length < 0, and
52 the data should be ignored. This structure has object semantics -
53 no fields should be modified directly, they should not be created
54 directly, and pointers to them should not be stored beyond the duration of
55 a callback. (If the last is changed, we'll need to add reference
56 counting)
57
58struct _GtkSelectionData
59{
60 GdkAtom selection;
61 GdkAtom target;
62 GdkAtom type;
63 gint format;
64 guchar *data;
65 gint length;
66};
67
68*/
69
70//-----------------------------------------------------------------------------
71// "selection_received" for targets
72//-----------------------------------------------------------------------------
73
74static void
75targets_selection_received( GtkWidget *WXUNUSED(widget),
76 GtkSelectionData *selection_data,
77#if (GTK_MINOR_VERSION > 0)
78 guint32 WXUNUSED(time),
79#endif
80 wxClipboard *clipboard )
81{
82 if ( wxTheClipboard && selection_data->length > 0 )
83 {
84 /* make sure we got the data in the correct form */
85 GdkAtom type = selection_data->type;
86 if ( type != GDK_SELECTION_TYPE_ATOM )
87 {
88 if ( strcmp(gdk_atom_name(type), "TARGETS") )
89 {
90 wxLogTrace( TRACE_CLIPBOARD,
91 _T("got unsupported clipboard target") );
92
93 clipboard->m_waiting = FALSE;
94 return;
95 }
96 }
97
98#ifdef __WXDEBUG__
99 wxDataFormat clip( selection_data->selection );
100 wxLogTrace( TRACE_CLIPBOARD,
101 wxT("selection received for targets, clipboard %s"),
102 clip.GetId().c_str() );
103#endif // __WXDEBUG__
104
105 // the atoms we received, holding a list of targets (= formats)
106 GdkAtom *atoms = (GdkAtom *)selection_data->data;
107
108 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
109 {
110 wxDataFormat format( atoms[i] );
111
112 wxLogTrace( TRACE_CLIPBOARD,
113 wxT("selection received for targets, format %s"),
114 format.GetId().c_str() );
115
116 if (format == clipboard->m_targetRequested)
117 {
118 clipboard->m_waiting = FALSE;
119 clipboard->m_formatSupported = TRUE;
120 return;
121 }
122 }
123 }
124
125 clipboard->m_waiting = FALSE;
126}
127
128//-----------------------------------------------------------------------------
129// "selection_received" for the actual data
130//-----------------------------------------------------------------------------
131
132static void
133selection_received( GtkWidget *WXUNUSED(widget),
134 GtkSelectionData *selection_data,
135#if (GTK_MINOR_VERSION > 0)
136 guint32 WXUNUSED(time),
137#endif
138 wxClipboard *clipboard )
139{
140 if (!wxTheClipboard)
141 {
142 clipboard->m_waiting = FALSE;
143 return;
144 }
145
146 wxDataObject *data_object = clipboard->m_receivedData;
147
148 if (!data_object)
149 {
150 clipboard->m_waiting = FALSE;
151 return;
152 }
153
154 if (selection_data->length <= 0)
155 {
156 clipboard->m_waiting = FALSE;
157 return;
158 }
159
160 wxDataFormat format( selection_data->target );
161
162 /* make sure we got the data in the correct format */
163 if (!data_object->IsSupportedFormat( format ) )
164 {
165 clipboard->m_waiting = FALSE;
166 return;
167 }
168
169 /* make sure we got the data in the correct form (selection type).
170 if so, copy data to target object */
171 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
172 {
173 clipboard->m_waiting = FALSE;
174 return;
175 }
176
177 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
178
179 wxTheClipboard->m_formatSupported = TRUE;
180 clipboard->m_waiting = FALSE;
181}
182
183//-----------------------------------------------------------------------------
184// "selection_clear"
185//-----------------------------------------------------------------------------
186
187static gint
188selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
189{
190 if (!wxTheClipboard) return TRUE;
191
192 if (event->selection == GDK_SELECTION_PRIMARY)
193 {
194 wxTheClipboard->m_ownsPrimarySelection = FALSE;
195 }
196 else
197 if (event->selection == g_clipboardAtom)
198 {
199 wxTheClipboard->m_ownsClipboard = FALSE;
200 }
201 else
202 {
203 wxTheClipboard->m_waiting = FALSE;
204 return FALSE;
205 }
206
207 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
208 (!wxTheClipboard->m_ownsClipboard))
209 {
210 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
211 if (wxTheClipboard->m_data)
212 {
213 wxLogTrace(TRACE_CLIPBOARD, wxT("wxClipboard will get cleared" ));
214
215 delete wxTheClipboard->m_data;
216 wxTheClipboard->m_data = (wxDataObject*) NULL;
217 }
218 }
219
220 wxTheClipboard->m_waiting = FALSE;
221 return TRUE;
222}
223
224//-----------------------------------------------------------------------------
225// selection handler for supplying data
226//-----------------------------------------------------------------------------
227
228static void
229selection_handler( GtkWidget *WXUNUSED(widget),
230 GtkSelectionData *selection_data,
231 guint WXUNUSED(info),
232 guint WXUNUSED(time),
233 gpointer WXUNUSED(data) )
234{
235 if (!wxTheClipboard) return;
236
237 if (!wxTheClipboard->m_data) return;
238
239 wxDataObject *data = wxTheClipboard->m_data;
240
241 wxDataFormat format( selection_data->target );
242
243 if (!data->IsSupportedFormat( format )) return;
244
245 int size = data->GetDataSize( format );
246
247 if (size == 0) return;
248
249 void *d = malloc(size);
250
251 data->GetDataHere( selection_data->target, d );
252
253 // transform Unicode text into multibyte before putting it on clipboard
254#if wxUSE_UNICODE
255 if ( format.GetType() == wxDF_TEXT )
256 {
257 const wchar_t *wstr = (const wchar_t *)d;
258 size_t len = wxConvCurrent->WC2MB(NULL, wstr, 0);
259 char *str = malloc(len + 1);
260 wxConvCurrent->WC2MB(str, wstr, len);
261 str[len] = '\0';
262
263 free(d);
264 d = str;
265 }
266#endif // wxUSE_UNICODE
267
268 gtk_selection_data_set(
269 selection_data,
270 GDK_SELECTION_TYPE_STRING,
271 8*sizeof(gchar),
272 (unsigned char*) d,
273 size );
274
275 free(d);
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#endif
343
344 /* As we have data we also own the clipboard. Once we no longer own
345 it, clear_selection is called which will set m_data to zero */
346 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
347 {
348 m_waiting = TRUE;
349
350 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
351 (guint32) 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,
361 (guint32) GDK_CURRENT_TIME );
362
363 while (m_waiting) gtk_main_iteration();
364 }
365
366 if (m_data)
367 {
368 delete m_data;
369 m_data = (wxDataObject*) NULL;
370 }
371
372#if wxUSE_THREADS
373 /* re-enable GUI threads */
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 wxLogTrace( TRACE_CLIPBOARD,
424 wxT("wxClipboard now supports atom %s"),
425 array[i].GetId().c_str() );
426
427 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
428 clipboard,
429 array[i],
430 0 ); /* what is info ? */
431 }
432
433 delete[] array;
434
435 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
436 "selection_get",
437 GTK_SIGNAL_FUNC(selection_handler),
438 (gpointer) NULL );
439
440#if wxUSE_THREADS
441 /* disable GUI threads */
442#endif
443
444 /* Tell the world we offer clipboard data */
445 bool res = (gtk_selection_owner_set( m_clipboardWidget,
446 clipboard,
447 (guint32) GDK_CURRENT_TIME ));
448
449 if (m_usePrimary)
450 m_ownsPrimarySelection = res;
451 else
452 m_ownsClipboard = res;
453
454#if wxUSE_THREADS
455 /* re-enable GUI threads */
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_waiting) return FALSE;
477
478 /* store requested format to be asked for by callbacks */
479 m_targetRequested = format;
480
481#if 0
482 wxLogTrace( TRACE_CLIPBOARD,
483 wxT("wxClipboard:IsSupported: requested format: %s"),
484 format.GetId().c_str() );
485#endif
486
487 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
488
489 m_formatSupported = FALSE;
490
491 /* perform query. this will set m_formatSupported to
492 TRUE if m_targetRequested is supported.
493 also, we have to wait for the "answer" from the
494 clipboard owner which is an asynchronous process.
495 therefore we set m_waiting = TRUE here and wait
496 until the callback "targets_selection_received"
497 sets it to FALSE */
498
499 m_waiting = TRUE;
500
501 gtk_selection_convert( m_targetsWidget,
502 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
503 : g_clipboardAtom,
504 g_targetsAtom,
505 (guint32) GDK_CURRENT_TIME );
506
507 while (m_waiting) gtk_main_iteration();
508
509 if (!m_formatSupported) return FALSE;
510
511 return TRUE;
512}
513
514bool wxClipboard::GetData( wxDataObject& data )
515{
516 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
517
518 /* get formats from wxDataObjects */
519 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
520 data.GetAllFormats( array );
521
522 for (size_t i = 0; i < data.GetFormatCount(); i++)
523 {
524 wxDataFormat format( array[i] );
525
526 wxLogTrace( TRACE_CLIPBOARD,
527 wxT("wxClipboard::GetData: requested format: %s"),
528 format.GetId().c_str() );
529
530 /* is data supported by clipboard ? */
531
532 /* store requested format to be asked for by callbacks */
533 m_targetRequested = format;
534
535 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
536
537 m_formatSupported = FALSE;
538
539 /* perform query. this will set m_formatSupported to
540 TRUE if m_targetRequested is supported.
541 also, we have to wait for the "answer" from the
542 clipboard owner which is an asynchronous process.
543 therefore we set m_waiting = TRUE here and wait
544 until the callback "targets_selection_received"
545 sets it to FALSE */
546
547 m_waiting = TRUE;
548
549 gtk_selection_convert( m_targetsWidget,
550 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
551 : g_clipboardAtom,
552 g_targetsAtom,
553 (guint32) GDK_CURRENT_TIME );
554
555 while (m_waiting) gtk_main_iteration();
556
557 if (!m_formatSupported) continue;
558
559 /* store pointer to data object to be filled up by callbacks */
560 m_receivedData = &data;
561
562 /* store requested format to be asked for by callbacks */
563 m_targetRequested = format;
564
565 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
566
567 /* start query */
568 m_formatSupported = FALSE;
569
570 /* ask for clipboard contents. this will set
571 m_formatSupported to TRUE if m_targetRequested
572 is supported.
573 also, we have to wait for the "answer" from the
574 clipboard owner which is an asynchronous process.
575 therefore we set m_waiting = TRUE here and wait
576 until the callback "targets_selection_received"
577 sets it to FALSE */
578
579 m_waiting = TRUE;
580
581 wxLogTrace( TRACE_CLIPBOARD,
582 wxT("wxClipboard::GetData: format found, start convert") );
583
584 gtk_selection_convert( m_clipboardWidget,
585 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
586 : g_clipboardAtom,
587 m_targetRequested,
588 (guint32) GDK_CURRENT_TIME );
589
590 while (m_waiting) gtk_main_iteration();
591
592 /* this is a true error as we checked for the presence of such data before */
593 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
594
595 /* return success */
596 delete[] array;
597 return TRUE;
598 }
599
600 wxLogTrace( TRACE_CLIPBOARD,
601 wxT("wxClipboard::GetData: format not found") );
602
603 /* return failure */
604 delete[] array;
605 return FALSE;
606}
607
608#endif
609 // wxUSE_CLIPBOARD
610