]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/clipbrd.cpp
Applied patch [ 1163322 ] Patch to prevent MDI app from opening the same file twice
[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#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11#pragma implementation "clipbrd.h"
12#endif
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#include "wx/clipbrd.h"
18
19#if wxUSE_CLIPBOARD
20
21#include "wx/dataobj.h"
22#include "wx/utils.h"
23#include "wx/log.h"
24
25#include <glib.h>
26#include <gdk/gdk.h>
27#include <gtk/gtk.h>
28
29//-----------------------------------------------------------------------------
30// thread system
31//-----------------------------------------------------------------------------
32
33#if wxUSE_THREADS
34#endif
35
36//-----------------------------------------------------------------------------
37// data
38//-----------------------------------------------------------------------------
39
40GdkAtom g_clipboardAtom = 0;
41GdkAtom g_targetsAtom = 0;
42
43#if defined(__WXGTK20__) && wxUSE_UNICODE
44extern GdkAtom g_altTextAtom;
45#endif
46
47// the trace mask we use with wxLogTrace() - call
48// wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
49// (there will be a *lot* of them!)
50static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
51
52//-----------------------------------------------------------------------------
53// reminder
54//-----------------------------------------------------------------------------
55
56/* The contents of a selection are returned in a GtkSelectionData
57 structure. selection/target identify the request.
58 type specifies the type of the return; if length < 0, and
59 the data should be ignored. This structure has object semantics -
60 no fields should be modified directly, they should not be created
61 directly, and pointers to them should not be stored beyond the duration of
62 a callback. (If the last is changed, we'll need to add reference
63 counting)
64
65struct _GtkSelectionData
66{
67 GdkAtom selection;
68 GdkAtom target;
69 GdkAtom type;
70 gint format;
71 guchar *data;
72 gint length;
73};
74
75*/
76
77//-----------------------------------------------------------------------------
78// "selection_received" for targets
79//-----------------------------------------------------------------------------
80
81extern "C" {
82static void
83targets_selection_received( GtkWidget *WXUNUSED(widget),
84 GtkSelectionData *selection_data,
85 guint32 WXUNUSED(time),
86 wxClipboard *clipboard )
87{
88 if ( wxTheClipboard && selection_data->length > 0 )
89 {
90 // make sure we got the data in the correct form
91 GdkAtom type = selection_data->type;
92 if ( type != GDK_SELECTION_TYPE_ATOM )
93 {
94 if ( strcmp(gdk_atom_name(type), "TARGETS") )
95 {
96 wxLogTrace( TRACE_CLIPBOARD,
97 _T("got unsupported clipboard target") );
98
99 clipboard->m_waiting = FALSE;
100 return;
101 }
102 }
103
104#ifdef __WXDEBUG__
105 wxDataFormat clip( selection_data->selection );
106 wxLogTrace( TRACE_CLIPBOARD,
107 wxT("selection received for targets, clipboard %s"),
108 clip.GetId().c_str() );
109#endif // __WXDEBUG__
110
111 // the atoms we received, holding a list of targets (= formats)
112 GdkAtom *atoms = (GdkAtom *)selection_data->data;
113
114 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
115 {
116 wxDataFormat format( atoms[i] );
117
118 wxLogTrace( TRACE_CLIPBOARD,
119 wxT("selection received for targets, format %s"),
120 format.GetId().c_str() );
121
122// printf( "format %s requested %s\n",
123// gdk_atom_name( atoms[i] ),
124// gdk_atom_name( clipboard->m_targetRequested ) );
125
126 if (format == clipboard->m_targetRequested)
127 {
128 clipboard->m_waiting = FALSE;
129 clipboard->m_formatSupported = TRUE;
130 return;
131 }
132 }
133 }
134
135 clipboard->m_waiting = FALSE;
136}
137}
138
139//-----------------------------------------------------------------------------
140// "selection_received" for the actual data
141//-----------------------------------------------------------------------------
142
143extern "C" {
144static void
145selection_received( GtkWidget *WXUNUSED(widget),
146 GtkSelectionData *selection_data,
147 guint32 WXUNUSED(time),
148 wxClipboard *clipboard )
149{
150 if (!wxTheClipboard)
151 {
152 clipboard->m_waiting = FALSE;
153 return;
154 }
155
156 wxDataObject *data_object = clipboard->m_receivedData;
157
158 if (!data_object)
159 {
160 clipboard->m_waiting = FALSE;
161 return;
162 }
163
164 if (selection_data->length <= 0)
165 {
166 clipboard->m_waiting = FALSE;
167 return;
168 }
169
170 wxDataFormat format( selection_data->target );
171
172 // make sure we got the data in the correct format
173 if (!data_object->IsSupportedFormat( format ) )
174 {
175 clipboard->m_waiting = FALSE;
176 return;
177 }
178
179#if 0
180 This seems to cause problems somehow
181 // make sure we got the data in the correct form (selection type).
182 // if so, copy data to target object
183 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
184 {
185 clipboard->m_waiting = FALSE;
186 return;
187 }
188#endif
189
190 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
191
192 wxTheClipboard->m_formatSupported = TRUE;
193 clipboard->m_waiting = FALSE;
194}
195}
196
197//-----------------------------------------------------------------------------
198// "selection_clear"
199//-----------------------------------------------------------------------------
200
201extern "C" {
202static gint
203selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
204{
205 if (!wxTheClipboard) return TRUE;
206
207 if (event->selection == GDK_SELECTION_PRIMARY)
208 {
209 wxTheClipboard->m_ownsPrimarySelection = FALSE;
210 }
211 else
212 if (event->selection == g_clipboardAtom)
213 {
214 wxTheClipboard->m_ownsClipboard = FALSE;
215 }
216 else
217 {
218 wxTheClipboard->m_waiting = FALSE;
219 return FALSE;
220 }
221
222 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
223 (!wxTheClipboard->m_ownsClipboard))
224 {
225 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
226 if (wxTheClipboard->m_data)
227 {
228 wxLogTrace(TRACE_CLIPBOARD, wxT("wxClipboard will get cleared" ));
229
230 delete wxTheClipboard->m_data;
231 wxTheClipboard->m_data = (wxDataObject*) NULL;
232 }
233 }
234
235 wxTheClipboard->m_waiting = FALSE;
236 return TRUE;
237}
238}
239
240//-----------------------------------------------------------------------------
241// selection handler for supplying data
242//-----------------------------------------------------------------------------
243
244extern "C" {
245static void
246selection_handler( GtkWidget *WXUNUSED(widget),
247 GtkSelectionData *selection_data,
248 guint WXUNUSED(info),
249 guint WXUNUSED(time),
250 gpointer WXUNUSED(data) )
251{
252 if (!wxTheClipboard) return;
253
254 if (!wxTheClipboard->m_data) return;
255
256 wxDataObject *data = wxTheClipboard->m_data;
257
258 wxDataFormat format( selection_data->target );
259
260#ifdef __WXDEBUG__
261 wxLogTrace(TRACE_CLIPBOARD,
262 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s"),
263 format.GetId().c_str(),
264 wxString::FromAscii(gdk_atom_name(selection_data->target)).c_str(),
265 wxString::FromAscii(gdk_atom_name(selection_data->type)).c_str(),
266 wxString::FromAscii(gdk_atom_name(selection_data->selection)).c_str()
267 );
268#endif
269
270 if (!data->IsSupportedFormat( format )) return;
271
272 int size = data->GetDataSize( format );
273
274 if (size == 0) return;
275
276 void *d = malloc(size);
277
278 // Text data will be in UTF8 in Unicode mode.
279 data->GetDataHere( selection_data->target, d );
280
281#ifdef __WXGTK20__
282 // NB: GTK+ requires special treatment of UTF8_STRING data, the text
283 // would show as UTF-8 data interpreted as latin1 (?) in other
284 // GTK+ apps if we used gtk_selection_data_set()
285 if (format == wxDataFormat(wxDF_UNICODETEXT))
286 {
287 gtk_selection_data_set_text(
288 selection_data,
289 (const gchar*)d,
290 size);
291 }
292 else
293#endif
294 {
295 gtk_selection_data_set(
296 selection_data,
297 GDK_SELECTION_TYPE_STRING,
298 8*sizeof(gchar),
299 (unsigned char*) d,
300 size );
301 }
302
303 free(d);
304}
305}
306
307//-----------------------------------------------------------------------------
308// wxClipboard
309//-----------------------------------------------------------------------------
310
311IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
312
313wxClipboard::wxClipboard()
314{
315 m_open = FALSE;
316 m_waiting = FALSE;
317
318 m_ownsClipboard = FALSE;
319 m_ownsPrimarySelection = FALSE;
320
321 m_data = (wxDataObject*) NULL;
322 m_receivedData = (wxDataObject*) NULL;
323
324 /* we use m_targetsWidget to query what formats are available */
325
326 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
327 gtk_widget_realize( m_targetsWidget );
328
329 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
330 "selection_received",
331 GTK_SIGNAL_FUNC( targets_selection_received ),
332 (gpointer) this );
333
334 /* we use m_clipboardWidget to get and to offer data */
335
336 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
337 gtk_widget_realize( m_clipboardWidget );
338
339 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
340 "selection_received",
341 GTK_SIGNAL_FUNC( selection_received ),
342 (gpointer) this );
343
344 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
345 "selection_clear_event",
346 GTK_SIGNAL_FUNC( selection_clear_clip ),
347 (gpointer) NULL );
348
349 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
350 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
351
352 m_formatSupported = FALSE;
353 m_targetRequested = 0;
354
355 m_usePrimary = FALSE;
356}
357
358wxClipboard::~wxClipboard()
359{
360 Clear();
361
362 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
363 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
364}
365
366void wxClipboard::Clear()
367{
368 if (m_data)
369 {
370#if wxUSE_THREADS
371 /* disable GUI threads */
372#endif
373
374 // As we have data we also own the clipboard. Once we no longer own
375 // it, clear_selection is called which will set m_data to zero
376 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
377 {
378 m_waiting = TRUE;
379
380 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
381 (guint32) GDK_CURRENT_TIME );
382
383 while (m_waiting) gtk_main_iteration();
384 }
385
386 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
387 {
388 m_waiting = TRUE;
389
390 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
391 (guint32) GDK_CURRENT_TIME );
392
393 while (m_waiting) gtk_main_iteration();
394 }
395
396 if (m_data)
397 {
398 delete m_data;
399 m_data = (wxDataObject*) NULL;
400 }
401
402#if wxUSE_THREADS
403 /* re-enable GUI threads */
404#endif
405 }
406
407 m_targetRequested = 0;
408 m_formatSupported = FALSE;
409}
410
411bool wxClipboard::Open()
412{
413 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
414
415 m_open = TRUE;
416
417 return TRUE;
418}
419
420bool wxClipboard::SetData( wxDataObject *data )
421{
422 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
423
424 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
425
426 Clear();
427
428 return AddData( data );
429}
430
431bool wxClipboard::AddData( wxDataObject *data )
432{
433 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
434
435 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
436
437 // we can only store one wxDataObject
438 Clear();
439
440 m_data = data;
441
442 // get formats from wxDataObjects
443 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
444 m_data->GetAllFormats( array );
445
446 // primary selection or clipboard
447 GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
448 : g_clipboardAtom;
449
450
451 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
452 {
453 wxLogTrace( TRACE_CLIPBOARD,
454 wxT("wxClipboard now supports atom %s"),
455 array[i].GetId().c_str() );
456
457// printf( "added %s\n",
458// gdk_atom_name( array[i].GetFormatId() ) );
459
460 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
461 clipboard,
462 array[i],
463 0 ); /* what is info ? */
464 }
465
466 delete[] array;
467
468 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
469 "selection_get",
470 GTK_SIGNAL_FUNC(selection_handler),
471 (gpointer) NULL );
472
473#if wxUSE_THREADS
474 /* disable GUI threads */
475#endif
476
477 /* Tell the world we offer clipboard data */
478 bool res = (gtk_selection_owner_set( m_clipboardWidget,
479 clipboard,
480 (guint32) GDK_CURRENT_TIME ));
481
482 if (m_usePrimary)
483 m_ownsPrimarySelection = res;
484 else
485 m_ownsClipboard = res;
486
487#if wxUSE_THREADS
488 /* re-enable GUI threads */
489#endif
490
491 return res;
492}
493
494void wxClipboard::Close()
495{
496 wxCHECK_RET( m_open, wxT("clipboard not open") );
497
498 m_open = FALSE;
499}
500
501bool wxClipboard::IsOpened() const
502{
503 return m_open;
504}
505
506bool wxClipboard::IsSupported( const wxDataFormat& format )
507{
508 /* reentrance problems */
509 if (m_waiting) return FALSE;
510
511 /* store requested format to be asked for by callbacks */
512 m_targetRequested = format;
513
514 wxLogTrace( TRACE_CLIPBOARD,
515 wxT("wxClipboard:IsSupported: requested format: %s"),
516 format.GetId().c_str() );
517
518 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
519
520 m_formatSupported = FALSE;
521
522 /* perform query. this will set m_formatSupported to
523 TRUE if m_targetRequested is supported.
524 also, we have to wait for the "answer" from the
525 clipboard owner which is an asynchronous process.
526 therefore we set m_waiting = TRUE here and wait
527 until the callback "targets_selection_received"
528 sets it to FALSE */
529
530 m_waiting = TRUE;
531
532 gtk_selection_convert( m_targetsWidget,
533 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
534 : g_clipboardAtom,
535 g_targetsAtom,
536 (guint32) GDK_CURRENT_TIME );
537
538 while (m_waiting) gtk_main_iteration();
539
540#if defined(__WXGTK20__) && wxUSE_UNICODE
541 if (!m_formatSupported && format == wxDataFormat(wxDF_UNICODETEXT))
542 {
543 // Another try with plain STRING format
544 extern GdkAtom g_altTextAtom;
545 return IsSupported(g_altTextAtom);
546 }
547#endif
548
549 return m_formatSupported;
550}
551
552bool wxClipboard::GetData( wxDataObject& data )
553{
554 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
555
556 /* get formats from wxDataObjects */
557 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
558 data.GetAllFormats( array );
559
560 for (size_t i = 0; i < data.GetFormatCount(); i++)
561 {
562 wxDataFormat format( array[i] );
563
564 wxLogTrace( TRACE_CLIPBOARD,
565 wxT("wxClipboard::GetData: requested format: %s"),
566 format.GetId().c_str() );
567
568 /* is data supported by clipboard ? */
569
570 /* store requested format to be asked for by callbacks */
571 m_targetRequested = format;
572
573 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
574
575 m_formatSupported = FALSE;
576
577 /* perform query. this will set m_formatSupported to
578 TRUE if m_targetRequested 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 gtk_selection_convert( m_targetsWidget,
588 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
589 : g_clipboardAtom,
590 g_targetsAtom,
591 (guint32) GDK_CURRENT_TIME );
592
593 while (m_waiting) gtk_main_iteration();
594
595 if (!m_formatSupported) continue;
596
597 /* store pointer to data object to be filled up by callbacks */
598 m_receivedData = &data;
599
600 /* store requested format to be asked for by callbacks */
601 m_targetRequested = format;
602
603 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
604
605 /* start query */
606 m_formatSupported = FALSE;
607
608 /* ask for clipboard contents. this will set
609 m_formatSupported to TRUE if m_targetRequested
610 is supported.
611 also, we have to wait for the "answer" from the
612 clipboard owner which is an asynchronous process.
613 therefore we set m_waiting = TRUE here and wait
614 until the callback "targets_selection_received"
615 sets it to FALSE */
616
617 m_waiting = TRUE;
618
619 wxLogTrace( TRACE_CLIPBOARD,
620 wxT("wxClipboard::GetData: format found, start convert") );
621
622 gtk_selection_convert( m_clipboardWidget,
623 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
624 : g_clipboardAtom,
625 m_targetRequested,
626 (guint32) GDK_CURRENT_TIME );
627
628 while (m_waiting) gtk_main_iteration();
629
630 /* this is a true error as we checked for the presence of such data before */
631 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
632
633 /* return success */
634 delete[] array;
635 return TRUE;
636 }
637
638 wxLogTrace( TRACE_CLIPBOARD,
639 wxT("wxClipboard::GetData: format not found") );
640
641 /* return failure */
642 delete[] array;
643 return FALSE;
644}
645
646#endif
647 // wxUSE_CLIPBOARD
648