]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/clipbrd.cpp
added operator[](unsigned int) const -- testing it now on Linux/axp,
[wxWidgets.git] / src / gtk1 / clipbrd.cpp
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
31 extern void wxapp_install_thread_wakeup();
32 extern void wxapp_uninstall_thread_wakeup();
33 #endif
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 GdkAtom g_clipboardAtom = 0;
40 GdkAtom 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
55 struct _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
71 static void
72 targets_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
130 static void
131 selection_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
185 static gint
186 selection_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
226 static void
227 selection_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
282 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
283
284 wxClipboard::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
328 wxClipboard::~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
336 void 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,
352 (guint32) GDK_CURRENT_TIME );
353
354 while (m_waiting) gtk_main_iteration();
355 }
356
357 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
358 {
359 m_waiting = TRUE;
360
361 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
362 (guint32) GDK_CURRENT_TIME );
363
364 while (m_waiting) gtk_main_iteration();
365 }
366
367 if (m_data)
368 {
369 delete m_data;
370 m_data = (wxDataObject*) NULL;
371 }
372
373 #if wxUSE_THREADS
374 /* re-enable GUI threads */
375 wxapp_install_thread_wakeup();
376 #endif
377 }
378
379 m_targetRequested = 0;
380 m_formatSupported = FALSE;
381 }
382
383 bool wxClipboard::Open()
384 {
385 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
386
387 m_open = TRUE;
388
389 return TRUE;
390 }
391
392 bool wxClipboard::SetData( wxDataObject *data )
393 {
394 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
395
396 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
397
398 Clear();
399
400 return AddData( data );
401 }
402
403 bool wxClipboard::AddData( wxDataObject *data )
404 {
405 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
406
407 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
408
409 /* we can only store one wxDataObject */
410 Clear();
411
412 m_data = data;
413
414 /* get formats from wxDataObjects */
415 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
416 m_data->GetAllFormats( array );
417
418 /* primary selection or clipboard */
419 GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
420 : g_clipboardAtom;
421
422
423 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
424 {
425 wxLogDebug( wxT("wxClipboard now supports atom %s"), 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 wxapp_uninstall_thread_wakeup();
443 #endif
444
445 /* Tell the world we offer clipboard data */
446 bool res = (gtk_selection_owner_set( m_clipboardWidget,
447 clipboard,
448 (guint32) GDK_CURRENT_TIME ));
449
450 if (m_usePrimary)
451 m_ownsPrimarySelection = res;
452 else
453 m_ownsClipboard = res;
454
455 #if wxUSE_THREADS
456 /* re-enable GUI threads */
457 wxapp_install_thread_wakeup();
458 #endif
459
460 return res;
461 }
462
463 void wxClipboard::Close()
464 {
465 wxCHECK_RET( m_open, wxT("clipboard not open") );
466
467 m_open = FALSE;
468 }
469
470 bool wxClipboard::IsOpened() const
471 {
472 return m_open;
473 }
474
475 bool wxClipboard::IsSupported( const wxDataFormat& format )
476 {
477 /* reentrance problems */
478 if (m_open) return TRUE;
479
480 /* store requested format to be asked for by callbacks */
481 m_targetRequested = format;
482
483 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
484
485 m_formatSupported = FALSE;
486
487 /* perform query. this will set m_formatSupported to
488 TRUE if m_targetRequested is supported.
489 alsom we have to wait for the "answer" from the
490 clipboard owner which is an asynchronous process.
491 therefore we set m_waiting = TRUE here and wait
492 until the callback "targets_selection_received"
493 sets it to FALSE */
494
495 m_waiting = TRUE;
496
497 gtk_selection_convert( m_targetsWidget,
498 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
499 : g_clipboardAtom,
500 g_targetsAtom,
501 (guint32) GDK_CURRENT_TIME );
502
503 while (m_waiting) gtk_main_iteration();
504
505 if (!m_formatSupported) return FALSE;
506
507 return TRUE;
508 }
509
510 bool wxClipboard::GetData( wxDataObject& data )
511 {
512 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
513
514 /* get formats from wxDataObjects */
515 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
516 data.GetAllFormats( array );
517
518 for (size_t i = 0; i < data.GetFormatCount(); i++)
519 {
520 wxDataFormat format( array[i] );
521
522 wxLogDebug( wxT("wxClipboard::GetData: requested format: %s"), format.GetId().c_str() );
523
524 /* is data supported by clipboard ? */
525
526 /* store requested format to be asked for by callbacks */
527 m_targetRequested = format;
528
529 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
530
531 m_formatSupported = FALSE;
532
533 /* perform query. this will set m_formatSupported to
534 TRUE if m_targetRequested is supported.
535 also, we have to wait for the "answer" from the
536 clipboard owner which is an asynchronous process.
537 therefore we set m_waiting = TRUE here and wait
538 until the callback "targets_selection_received"
539 sets it to FALSE */
540
541 m_waiting = TRUE;
542
543 gtk_selection_convert( m_targetsWidget,
544 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
545 : g_clipboardAtom,
546 g_targetsAtom,
547 (guint32) GDK_CURRENT_TIME );
548
549 while (m_waiting) gtk_main_iteration();
550
551 if (!m_formatSupported) continue;
552
553 /* store pointer to data object to be filled up by callbacks */
554 m_receivedData = &data;
555
556 /* store requested format to be asked for by callbacks */
557 m_targetRequested = format;
558
559 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
560
561 /* start query */
562 m_formatSupported = FALSE;
563
564 /* ask for clipboard contents. this will set
565 m_formatSupported to TRUE if m_targetRequested
566 is supported.
567 also, we have to wait for the "answer" from the
568 clipboard owner which is an asynchronous process.
569 therefore we set m_waiting = TRUE here and wait
570 until the callback "targets_selection_received"
571 sets it to FALSE */
572
573 m_waiting = TRUE;
574
575 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
576
577 gtk_selection_convert( m_clipboardWidget,
578 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
579 : g_clipboardAtom,
580 m_targetRequested,
581 (guint32) GDK_CURRENT_TIME );
582
583 while (m_waiting) gtk_main_iteration();
584
585 /* this is a true error as we checked for the presence of such data before */
586 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
587
588 /* return success */
589 delete[] array;
590 return TRUE;
591 }
592
593 wxLogDebug( wxT("wxClipboard::GetData: format not found") );
594
595 /* return failure */
596 delete[] array;
597 return FALSE;
598 }
599
600 #endif
601 // wxUSE_CLIPBOARD
602