]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/clipbrd.cpp
Added distrib file for deprecated classes
[wxWidgets.git] / src / gtk / clipbrd.cpp
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
37 GdkAtom g_clipboardAtom = 0;
38 GdkAtom 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!)
43 static 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
58 struct _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
74 static void
75 targets_selection_received( GtkWidget *WXUNUSED(widget),
76 GtkSelectionData *selection_data,
77 guint32 WXUNUSED(time),
78 wxClipboard *clipboard )
79 {
80 if ( wxTheClipboard && selection_data->length > 0 )
81 {
82 // make sure we got the data in the correct form
83 GdkAtom type = selection_data->type;
84 if ( type != GDK_SELECTION_TYPE_ATOM )
85 {
86 if ( strcmp(gdk_atom_name(type), "TARGETS") )
87 {
88 wxLogTrace( TRACE_CLIPBOARD,
89 _T("got unsupported clipboard target") );
90
91 clipboard->m_waiting = FALSE;
92 return;
93 }
94 }
95
96 #ifdef __WXDEBUG__
97 wxDataFormat clip( selection_data->selection );
98 wxLogTrace( TRACE_CLIPBOARD,
99 wxT("selection received for targets, clipboard %s"),
100 clip.GetId().c_str() );
101 #endif // __WXDEBUG__
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 wxLogTrace( TRACE_CLIPBOARD,
111 wxT("selection received for targets, format %s"),
112 format.GetId().c_str() );
113
114 // printf( "format %s requested %s\n",
115 // gdk_atom_name( atoms[i] ),
116 // gdk_atom_name( clipboard->m_targetRequested ) );
117
118 if (format == clipboard->m_targetRequested)
119 {
120 clipboard->m_waiting = FALSE;
121 clipboard->m_formatSupported = TRUE;
122 return;
123 }
124 }
125 }
126
127 clipboard->m_waiting = FALSE;
128 }
129
130 //-----------------------------------------------------------------------------
131 // "selection_received" for the actual data
132 //-----------------------------------------------------------------------------
133
134 static void
135 selection_received( GtkWidget *WXUNUSED(widget),
136 GtkSelectionData *selection_data,
137 guint32 WXUNUSED(time),
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
187 static gint
188 selection_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
228 static void
229 selection_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 // Text data will be in UTF8 in Unicode mode.
252 data->GetDataHere( selection_data->target, d );
253
254 gtk_selection_data_set(
255 selection_data,
256 GDK_SELECTION_TYPE_STRING,
257 8*sizeof(gchar),
258 (unsigned char*) d,
259 size );
260
261 free(d);
262 }
263
264 //-----------------------------------------------------------------------------
265 // wxClipboard
266 //-----------------------------------------------------------------------------
267
268 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
269
270 wxClipboard::wxClipboard()
271 {
272 m_open = FALSE;
273
274 m_ownsClipboard = FALSE;
275 m_ownsPrimarySelection = FALSE;
276
277 m_data = (wxDataObject*) NULL;
278 m_receivedData = (wxDataObject*) NULL;
279
280 /* we use m_targetsWidget to query what formats are available */
281
282 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
283 gtk_widget_realize( m_targetsWidget );
284
285 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
286 "selection_received",
287 GTK_SIGNAL_FUNC( targets_selection_received ),
288 (gpointer) this );
289
290 /* we use m_clipboardWidget to get and to offer data */
291
292 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
293 gtk_widget_realize( m_clipboardWidget );
294
295 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
296 "selection_received",
297 GTK_SIGNAL_FUNC( selection_received ),
298 (gpointer) this );
299
300 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
301 "selection_clear_event",
302 GTK_SIGNAL_FUNC( selection_clear_clip ),
303 (gpointer) NULL );
304
305 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
306 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
307
308 m_formatSupported = FALSE;
309 m_targetRequested = 0;
310
311 m_usePrimary = FALSE;
312 }
313
314 wxClipboard::~wxClipboard()
315 {
316 Clear();
317
318 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
319 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
320 }
321
322 void wxClipboard::Clear()
323 {
324 if (m_data)
325 {
326 #if wxUSE_THREADS
327 /* disable GUI threads */
328 #endif
329
330 // As we have data we also own the clipboard. Once we no longer own
331 // it, clear_selection is called which will set m_data to zero
332 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
333 {
334 m_waiting = TRUE;
335
336 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
337 (guint32) GDK_CURRENT_TIME );
338
339 while (m_waiting) gtk_main_iteration();
340 }
341
342 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
343 {
344 m_waiting = TRUE;
345
346 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
347 (guint32) GDK_CURRENT_TIME );
348
349 while (m_waiting) gtk_main_iteration();
350 }
351
352 if (m_data)
353 {
354 delete m_data;
355 m_data = (wxDataObject*) NULL;
356 }
357
358 #if wxUSE_THREADS
359 /* re-enable GUI threads */
360 #endif
361 }
362
363 m_targetRequested = 0;
364 m_formatSupported = FALSE;
365 }
366
367 bool wxClipboard::Open()
368 {
369 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
370
371 m_open = TRUE;
372
373 return TRUE;
374 }
375
376 bool wxClipboard::SetData( wxDataObject *data )
377 {
378 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
379
380 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
381
382 Clear();
383
384 return AddData( data );
385 }
386
387 bool wxClipboard::AddData( wxDataObject *data )
388 {
389 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
390
391 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
392
393 // we can only store one wxDataObject
394 Clear();
395
396 m_data = data;
397
398 // get formats from wxDataObjects
399 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
400 m_data->GetAllFormats( array );
401
402 // primary selection or clipboard
403 GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
404 : g_clipboardAtom;
405
406
407 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
408 {
409 wxLogTrace( TRACE_CLIPBOARD,
410 wxT("wxClipboard now supports atom %s"),
411 array[i].GetId().c_str() );
412
413 // printf( "added %s\n",
414 // gdk_atom_name( array[i].GetFormatId() ) );
415
416 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
417 clipboard,
418 array[i],
419 0 ); /* what is info ? */
420 }
421
422 delete[] array;
423
424 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
425 "selection_get",
426 GTK_SIGNAL_FUNC(selection_handler),
427 (gpointer) NULL );
428
429 #if wxUSE_THREADS
430 /* disable GUI threads */
431 #endif
432
433 /* Tell the world we offer clipboard data */
434 bool res = (gtk_selection_owner_set( m_clipboardWidget,
435 clipboard,
436 (guint32) GDK_CURRENT_TIME ));
437
438 if (m_usePrimary)
439 m_ownsPrimarySelection = res;
440 else
441 m_ownsClipboard = res;
442
443 #if wxUSE_THREADS
444 /* re-enable GUI threads */
445 #endif
446
447 return res;
448 }
449
450 void wxClipboard::Close()
451 {
452 wxCHECK_RET( m_open, wxT("clipboard not open") );
453
454 m_open = FALSE;
455 }
456
457 bool wxClipboard::IsOpened() const
458 {
459 return m_open;
460 }
461
462 bool wxClipboard::IsSupported( const wxDataFormat& format )
463 {
464 /* reentrance problems */
465 if (m_waiting) return FALSE;
466
467 /* store requested format to be asked for by callbacks */
468 m_targetRequested = format;
469
470 #if 0
471 wxLogTrace( TRACE_CLIPBOARD,
472 wxT("wxClipboard:IsSupported: requested format: %s"),
473 format.GetId().c_str() );
474 #endif
475
476 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
477
478 m_formatSupported = FALSE;
479
480 /* perform query. this will set m_formatSupported to
481 TRUE if m_targetRequested is supported.
482 also, we have to wait for the "answer" from the
483 clipboard owner which is an asynchronous process.
484 therefore we set m_waiting = TRUE here and wait
485 until the callback "targets_selection_received"
486 sets it to FALSE */
487
488 m_waiting = TRUE;
489
490 gtk_selection_convert( m_targetsWidget,
491 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
492 : g_clipboardAtom,
493 g_targetsAtom,
494 (guint32) GDK_CURRENT_TIME );
495
496 while (m_waiting) gtk_main_iteration();
497
498 if (!m_formatSupported) return FALSE;
499
500 return TRUE;
501 }
502
503 bool wxClipboard::GetData( wxDataObject& data )
504 {
505 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
506
507 /* get formats from wxDataObjects */
508 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
509 data.GetAllFormats( array );
510
511 for (size_t i = 0; i < data.GetFormatCount(); i++)
512 {
513 wxDataFormat format( array[i] );
514
515 wxLogTrace( TRACE_CLIPBOARD,
516 wxT("wxClipboard::GetData: requested format: %s"),
517 format.GetId().c_str() );
518
519 /* is data supported by clipboard ? */
520
521 /* store requested format to be asked for by callbacks */
522 m_targetRequested = format;
523
524 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
525
526 m_formatSupported = FALSE;
527
528 /* perform query. this will set m_formatSupported to
529 TRUE if m_targetRequested is supported.
530 also, we have to wait for the "answer" from the
531 clipboard owner which is an asynchronous process.
532 therefore we set m_waiting = TRUE here and wait
533 until the callback "targets_selection_received"
534 sets it to FALSE */
535
536 m_waiting = TRUE;
537
538 gtk_selection_convert( m_targetsWidget,
539 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
540 : g_clipboardAtom,
541 g_targetsAtom,
542 (guint32) GDK_CURRENT_TIME );
543
544 while (m_waiting) gtk_main_iteration();
545
546 if (!m_formatSupported) continue;
547
548 /* store pointer to data object to be filled up by callbacks */
549 m_receivedData = &data;
550
551 /* store requested format to be asked for by callbacks */
552 m_targetRequested = format;
553
554 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
555
556 /* start query */
557 m_formatSupported = FALSE;
558
559 /* ask for clipboard contents. this will set
560 m_formatSupported to TRUE if m_targetRequested
561 is supported.
562 also, we have to wait for the "answer" from the
563 clipboard owner which is an asynchronous process.
564 therefore we set m_waiting = TRUE here and wait
565 until the callback "targets_selection_received"
566 sets it to FALSE */
567
568 m_waiting = TRUE;
569
570 wxLogTrace( TRACE_CLIPBOARD,
571 wxT("wxClipboard::GetData: format found, start convert") );
572
573 gtk_selection_convert( m_clipboardWidget,
574 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
575 : g_clipboardAtom,
576 m_targetRequested,
577 (guint32) GDK_CURRENT_TIME );
578
579 while (m_waiting) gtk_main_iteration();
580
581 /* this is a true error as we checked for the presence of such data before */
582 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
583
584 /* return success */
585 delete[] array;
586 return TRUE;
587 }
588
589 wxLogTrace( TRACE_CLIPBOARD,
590 wxT("wxClipboard::GetData: format not found") );
591
592 /* return failure */
593 delete[] array;
594 return FALSE;
595 }
596
597 #endif
598 // wxUSE_CLIPBOARD
599