]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/clipbrd.cpp
fixed return value of wxMDIChildFrame::HandleGetMinMaxInfo()
[wxWidgets.git] / src / gtk / 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// the trace mask we use with wxLogTrace() - call
44// wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
45// (there will be a *lot* of them!)
46static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
47
48//-----------------------------------------------------------------------------
49// reminder
50//-----------------------------------------------------------------------------
51
52/* The contents of a selection are returned in a GtkSelectionData
53 structure. selection/target identify the request.
54 type specifies the type of the return; if length < 0, and
55 the data should be ignored. This structure has object semantics -
56 no fields should be modified directly, they should not be created
57 directly, and pointers to them should not be stored beyond the duration of
58 a callback. (If the last is changed, we'll need to add reference
59 counting)
60
61struct _GtkSelectionData
62{
63 GdkAtom selection;
64 GdkAtom target;
65 GdkAtom type;
66 gint format;
67 guchar *data;
68 gint length;
69};
70
71*/
72
73//-----------------------------------------------------------------------------
74// "selection_received" for targets
75//-----------------------------------------------------------------------------
76
77static void
78targets_selection_received( GtkWidget *WXUNUSED(widget),
79 GtkSelectionData *selection_data,
80 guint32 WXUNUSED(time),
81 wxClipboard *clipboard )
82{
83 if ( wxTheClipboard && selection_data->length > 0 )
84 {
85 // make sure we got the data in the correct form
86 GdkAtom type = selection_data->type;
87 if ( type != GDK_SELECTION_TYPE_ATOM )
88 {
89 if ( strcmp(gdk_atom_name(type), "TARGETS") )
90 {
91 wxLogTrace( TRACE_CLIPBOARD,
92 _T("got unsupported clipboard target") );
93
94 clipboard->m_waiting = FALSE;
95 return;
96 }
97 }
98
99#ifdef __WXDEBUG__
100 wxDataFormat clip( selection_data->selection );
101 wxLogTrace( TRACE_CLIPBOARD,
102 wxT("selection received for targets, clipboard %s"),
103 clip.GetId().c_str() );
104#endif // __WXDEBUG__
105
106 // the atoms we received, holding a list of targets (= formats)
107 GdkAtom *atoms = (GdkAtom *)selection_data->data;
108
109 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
110 {
111 wxDataFormat format( atoms[i] );
112
113 wxLogTrace( TRACE_CLIPBOARD,
114 wxT("selection received for targets, format %s"),
115 format.GetId().c_str() );
116
117// printf( "format %s requested %s\n",
118// gdk_atom_name( atoms[i] ),
119// gdk_atom_name( clipboard->m_targetRequested ) );
120
121 if (format == clipboard->m_targetRequested)
122 {
123 clipboard->m_waiting = FALSE;
124 clipboard->m_formatSupported = TRUE;
125 return;
126 }
127 }
128 }
129
130 clipboard->m_waiting = FALSE;
131}
132
133//-----------------------------------------------------------------------------
134// "selection_received" for the actual data
135//-----------------------------------------------------------------------------
136
137static void
138selection_received( GtkWidget *WXUNUSED(widget),
139 GtkSelectionData *selection_data,
140 guint32 WXUNUSED(time),
141 wxClipboard *clipboard )
142{
143 if (!wxTheClipboard)
144 {
145 clipboard->m_waiting = FALSE;
146 return;
147 }
148
149 wxDataObject *data_object = clipboard->m_receivedData;
150
151 if (!data_object)
152 {
153 clipboard->m_waiting = FALSE;
154 return;
155 }
156
157 if (selection_data->length <= 0)
158 {
159 clipboard->m_waiting = FALSE;
160 return;
161 }
162
163 wxDataFormat format( selection_data->target );
164
165 // make sure we got the data in the correct format
166 if (!data_object->IsSupportedFormat( format ) )
167 {
168 clipboard->m_waiting = FALSE;
169 return;
170 }
171
172 /* make sure we got the data in the correct form (selection type).
173 if so, copy data to target object */
174 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
175 {
176 clipboard->m_waiting = FALSE;
177 return;
178 }
179
180 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
181
182 wxTheClipboard->m_formatSupported = TRUE;
183 clipboard->m_waiting = FALSE;
184}
185
186//-----------------------------------------------------------------------------
187// "selection_clear"
188//-----------------------------------------------------------------------------
189
190static gint
191selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
192{
193 if (!wxTheClipboard) return TRUE;
194
195 if (event->selection == GDK_SELECTION_PRIMARY)
196 {
197 wxTheClipboard->m_ownsPrimarySelection = FALSE;
198 }
199 else
200 if (event->selection == g_clipboardAtom)
201 {
202 wxTheClipboard->m_ownsClipboard = FALSE;
203 }
204 else
205 {
206 wxTheClipboard->m_waiting = FALSE;
207 return FALSE;
208 }
209
210 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
211 (!wxTheClipboard->m_ownsClipboard))
212 {
213 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
214 if (wxTheClipboard->m_data)
215 {
216 wxLogTrace(TRACE_CLIPBOARD, wxT("wxClipboard will get cleared" ));
217
218 delete wxTheClipboard->m_data;
219 wxTheClipboard->m_data = (wxDataObject*) NULL;
220 }
221 }
222
223 wxTheClipboard->m_waiting = FALSE;
224 return TRUE;
225}
226
227//-----------------------------------------------------------------------------
228// selection handler for supplying data
229//-----------------------------------------------------------------------------
230
231static void
232selection_handler( GtkWidget *WXUNUSED(widget),
233 GtkSelectionData *selection_data,
234 guint WXUNUSED(info),
235 guint WXUNUSED(time),
236 gpointer WXUNUSED(data) )
237{
238 if (!wxTheClipboard) return;
239
240 if (!wxTheClipboard->m_data) return;
241
242 wxDataObject *data = wxTheClipboard->m_data;
243
244 wxDataFormat format( selection_data->target );
245
246 if (!data->IsSupportedFormat( format )) return;
247
248 int size = data->GetDataSize( format );
249
250 if (size == 0) return;
251
252 void *d = malloc(size);
253
254 // Text data will be in UTF8 in Unicode mode.
255 data->GetDataHere( selection_data->target, d );
256
257 gtk_selection_data_set(
258 selection_data,
259 GDK_SELECTION_TYPE_STRING,
260 8*sizeof(gchar),
261 (unsigned char*) d,
262 size );
263
264 free(d);
265}
266
267//-----------------------------------------------------------------------------
268// wxClipboard
269//-----------------------------------------------------------------------------
270
271IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
272
273wxClipboard::wxClipboard()
274{
275 m_open = FALSE;
276
277 m_ownsClipboard = FALSE;
278 m_ownsPrimarySelection = FALSE;
279
280 m_data = (wxDataObject*) NULL;
281 m_receivedData = (wxDataObject*) NULL;
282
283 /* we use m_targetsWidget to query what formats are available */
284
285 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
286 gtk_widget_realize( m_targetsWidget );
287
288 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
289 "selection_received",
290 GTK_SIGNAL_FUNC( targets_selection_received ),
291 (gpointer) this );
292
293 /* we use m_clipboardWidget to get and to offer data */
294
295 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
296 gtk_widget_realize( m_clipboardWidget );
297
298 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
299 "selection_received",
300 GTK_SIGNAL_FUNC( selection_received ),
301 (gpointer) this );
302
303 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
304 "selection_clear_event",
305 GTK_SIGNAL_FUNC( selection_clear_clip ),
306 (gpointer) NULL );
307
308 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
309 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
310
311 m_formatSupported = FALSE;
312 m_targetRequested = 0;
313
314 m_usePrimary = FALSE;
315}
316
317wxClipboard::~wxClipboard()
318{
319 Clear();
320
321 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
322 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
323}
324
325void wxClipboard::Clear()
326{
327 if (m_data)
328 {
329#if wxUSE_THREADS
330 /* disable GUI threads */
331#endif
332
333 // As we have data we also own the clipboard. Once we no longer own
334 // it, clear_selection is called which will set m_data to zero
335 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
336 {
337 m_waiting = TRUE;
338
339 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
340 (guint32) GDK_CURRENT_TIME );
341
342 while (m_waiting) gtk_main_iteration();
343 }
344
345 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
346 {
347 m_waiting = TRUE;
348
349 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
350 (guint32) GDK_CURRENT_TIME );
351
352 while (m_waiting) gtk_main_iteration();
353 }
354
355 if (m_data)
356 {
357 delete m_data;
358 m_data = (wxDataObject*) NULL;
359 }
360
361#if wxUSE_THREADS
362 /* re-enable GUI threads */
363#endif
364 }
365
366 m_targetRequested = 0;
367 m_formatSupported = FALSE;
368}
369
370bool wxClipboard::Open()
371{
372 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
373
374 m_open = TRUE;
375
376 return TRUE;
377}
378
379bool wxClipboard::SetData( wxDataObject *data )
380{
381 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
382
383 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
384
385 Clear();
386
387 return AddData( data );
388}
389
390bool wxClipboard::AddData( 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 // we can only store one wxDataObject
397 Clear();
398
399 m_data = data;
400
401 // get formats from wxDataObjects
402 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
403 m_data->GetAllFormats( array );
404
405 // primary selection or clipboard
406 GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
407 : g_clipboardAtom;
408
409
410 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
411 {
412 wxLogTrace( TRACE_CLIPBOARD,
413 wxT("wxClipboard now supports atom %s"),
414 array[i].GetId().c_str() );
415
416// printf( "added %s\n",
417// gdk_atom_name( array[i].GetFormatId() ) );
418
419 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
420 clipboard,
421 array[i],
422 0 ); /* what is info ? */
423 }
424
425 delete[] array;
426
427 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
428 "selection_get",
429 GTK_SIGNAL_FUNC(selection_handler),
430 (gpointer) NULL );
431
432#if wxUSE_THREADS
433 /* disable GUI threads */
434#endif
435
436 /* Tell the world we offer clipboard data */
437 bool res = (gtk_selection_owner_set( m_clipboardWidget,
438 clipboard,
439 (guint32) GDK_CURRENT_TIME ));
440
441 if (m_usePrimary)
442 m_ownsPrimarySelection = res;
443 else
444 m_ownsClipboard = res;
445
446#if wxUSE_THREADS
447 /* re-enable GUI threads */
448#endif
449
450 return res;
451}
452
453void wxClipboard::Close()
454{
455 wxCHECK_RET( m_open, wxT("clipboard not open") );
456
457 m_open = FALSE;
458}
459
460bool wxClipboard::IsOpened() const
461{
462 return m_open;
463}
464
465bool wxClipboard::IsSupported( const wxDataFormat& format )
466{
467 /* reentrance problems */
468 if (m_waiting) return FALSE;
469
470 /* store requested format to be asked for by callbacks */
471 m_targetRequested = format;
472
473#if 0
474 wxLogTrace( TRACE_CLIPBOARD,
475 wxT("wxClipboard:IsSupported: requested format: %s"),
476 format.GetId().c_str() );
477#endif
478
479 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
480
481 m_formatSupported = FALSE;
482
483 /* perform query. this will set m_formatSupported to
484 TRUE if m_targetRequested is supported.
485 also, we have to wait for the "answer" from the
486 clipboard owner which is an asynchronous process.
487 therefore we set m_waiting = TRUE here and wait
488 until the callback "targets_selection_received"
489 sets it to FALSE */
490
491 m_waiting = TRUE;
492
493 gtk_selection_convert( m_targetsWidget,
494 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
495 : g_clipboardAtom,
496 g_targetsAtom,
497 (guint32) GDK_CURRENT_TIME );
498
499 while (m_waiting) gtk_main_iteration();
500
501 if (!m_formatSupported) return FALSE;
502
503 return TRUE;
504}
505
506bool wxClipboard::GetData( wxDataObject& data )
507{
508 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
509
510 /* get formats from wxDataObjects */
511 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
512 data.GetAllFormats( array );
513
514 for (size_t i = 0; i < data.GetFormatCount(); i++)
515 {
516 wxDataFormat format( array[i] );
517
518 wxLogTrace( TRACE_CLIPBOARD,
519 wxT("wxClipboard::GetData: requested format: %s"),
520 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 (guint32) 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 wxLogTrace( TRACE_CLIPBOARD,
574 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 wxLogTrace( TRACE_CLIPBOARD,
593 wxT("wxClipboard::GetData: format not found") );
594
595 /* return failure */
596 delete[] array;
597 return FALSE;
598}
599
600#endif
601 // wxUSE_CLIPBOARD
602