]>
Commit | Line | Data |
---|---|---|
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 | ||
40 | GdkAtom g_clipboardAtom = 0; | |
41 | GdkAtom 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!) | |
46 | static 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 | ||
61 | struct _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 | ||
77 | static void | |
78 | targets_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 | ||
137 | static void | |
138 | selection_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 | ||
190 | static gint | |
191 | selection_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 | ||
231 | static void | |
232 | selection_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 | ||
271 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject) | |
272 | ||
273 | wxClipboard::wxClipboard() | |
274 | { | |
275 | m_open = FALSE; | |
276 | m_waiting = FALSE; | |
277 | ||
278 | m_ownsClipboard = FALSE; | |
279 | m_ownsPrimarySelection = FALSE; | |
280 | ||
281 | m_data = (wxDataObject*) NULL; | |
282 | m_receivedData = (wxDataObject*) NULL; | |
283 | ||
284 | /* we use m_targetsWidget to query what formats are available */ | |
285 | ||
286 | m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP ); | |
287 | gtk_widget_realize( m_targetsWidget ); | |
288 | ||
289 | gtk_signal_connect( GTK_OBJECT(m_targetsWidget), | |
290 | "selection_received", | |
291 | GTK_SIGNAL_FUNC( targets_selection_received ), | |
292 | (gpointer) this ); | |
293 | ||
294 | /* we use m_clipboardWidget to get and to offer data */ | |
295 | ||
296 | m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); | |
297 | gtk_widget_realize( m_clipboardWidget ); | |
298 | ||
299 | gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), | |
300 | "selection_received", | |
301 | GTK_SIGNAL_FUNC( selection_received ), | |
302 | (gpointer) this ); | |
303 | ||
304 | gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), | |
305 | "selection_clear_event", | |
306 | GTK_SIGNAL_FUNC( selection_clear_clip ), | |
307 | (gpointer) NULL ); | |
308 | ||
309 | if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE ); | |
310 | if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE); | |
311 | ||
312 | m_formatSupported = FALSE; | |
313 | m_targetRequested = 0; | |
314 | ||
315 | m_usePrimary = FALSE; | |
316 | } | |
317 | ||
318 | wxClipboard::~wxClipboard() | |
319 | { | |
320 | Clear(); | |
321 | ||
322 | if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget ); | |
323 | if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget ); | |
324 | } | |
325 | ||
326 | void wxClipboard::Clear() | |
327 | { | |
328 | if (m_data) | |
329 | { | |
330 | #if wxUSE_THREADS | |
331 | /* disable GUI threads */ | |
332 | #endif | |
333 | ||
334 | // As we have data we also own the clipboard. Once we no longer own | |
335 | // it, clear_selection is called which will set m_data to zero | |
336 | if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window) | |
337 | { | |
338 | m_waiting = TRUE; | |
339 | ||
340 | gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, | |
341 | (guint32) GDK_CURRENT_TIME ); | |
342 | ||
343 | while (m_waiting) gtk_main_iteration(); | |
344 | } | |
345 | ||
346 | if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window) | |
347 | { | |
348 | m_waiting = TRUE; | |
349 | ||
350 | gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, | |
351 | (guint32) GDK_CURRENT_TIME ); | |
352 | ||
353 | while (m_waiting) gtk_main_iteration(); | |
354 | } | |
355 | ||
356 | if (m_data) | |
357 | { | |
358 | delete m_data; | |
359 | m_data = (wxDataObject*) NULL; | |
360 | } | |
361 | ||
362 | #if wxUSE_THREADS | |
363 | /* re-enable GUI threads */ | |
364 | #endif | |
365 | } | |
366 | ||
367 | m_targetRequested = 0; | |
368 | m_formatSupported = FALSE; | |
369 | } | |
370 | ||
371 | bool wxClipboard::Open() | |
372 | { | |
373 | wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") ); | |
374 | ||
375 | m_open = TRUE; | |
376 | ||
377 | return TRUE; | |
378 | } | |
379 | ||
380 | bool wxClipboard::SetData( wxDataObject *data ) | |
381 | { | |
382 | wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); | |
383 | ||
384 | wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); | |
385 | ||
386 | Clear(); | |
387 | ||
388 | return AddData( data ); | |
389 | } | |
390 | ||
391 | bool wxClipboard::AddData( wxDataObject *data ) | |
392 | { | |
393 | wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); | |
394 | ||
395 | wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); | |
396 | ||
397 | // we can only store one wxDataObject | |
398 | Clear(); | |
399 | ||
400 | m_data = data; | |
401 | ||
402 | // get formats from wxDataObjects | |
403 | wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; | |
404 | m_data->GetAllFormats( array ); | |
405 | ||
406 | // primary selection or clipboard | |
407 | GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY | |
408 | : g_clipboardAtom; | |
409 | ||
410 | ||
411 | for (size_t i = 0; i < m_data->GetFormatCount(); i++) | |
412 | { | |
413 | wxLogTrace( TRACE_CLIPBOARD, | |
414 | wxT("wxClipboard now supports atom %s"), | |
415 | array[i].GetId().c_str() ); | |
416 | ||
417 | // printf( "added %s\n", | |
418 | // gdk_atom_name( array[i].GetFormatId() ) ); | |
419 | ||
420 | gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget), | |
421 | clipboard, | |
422 | array[i], | |
423 | 0 ); /* what is info ? */ | |
424 | } | |
425 | ||
426 | delete[] array; | |
427 | ||
428 | gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), | |
429 | "selection_get", | |
430 | GTK_SIGNAL_FUNC(selection_handler), | |
431 | (gpointer) NULL ); | |
432 | ||
433 | #if wxUSE_THREADS | |
434 | /* disable GUI threads */ | |
435 | #endif | |
436 | ||
437 | /* Tell the world we offer clipboard data */ | |
438 | bool res = (gtk_selection_owner_set( m_clipboardWidget, | |
439 | clipboard, | |
440 | (guint32) GDK_CURRENT_TIME )); | |
441 | ||
442 | if (m_usePrimary) | |
443 | m_ownsPrimarySelection = res; | |
444 | else | |
445 | m_ownsClipboard = res; | |
446 | ||
447 | #if wxUSE_THREADS | |
448 | /* re-enable GUI threads */ | |
449 | #endif | |
450 | ||
451 | return res; | |
452 | } | |
453 | ||
454 | void wxClipboard::Close() | |
455 | { | |
456 | wxCHECK_RET( m_open, wxT("clipboard not open") ); | |
457 | ||
458 | m_open = FALSE; | |
459 | } | |
460 | ||
461 | bool wxClipboard::IsOpened() const | |
462 | { | |
463 | return m_open; | |
464 | } | |
465 | ||
466 | bool wxClipboard::IsSupported( const wxDataFormat& format ) | |
467 | { | |
468 | /* reentrance problems */ | |
469 | if (m_waiting) return FALSE; | |
470 | ||
471 | /* store requested format to be asked for by callbacks */ | |
472 | m_targetRequested = format; | |
473 | ||
474 | #if 0 | |
475 | wxLogTrace( TRACE_CLIPBOARD, | |
476 | wxT("wxClipboard:IsSupported: requested format: %s"), | |
477 | format.GetId().c_str() ); | |
478 | #endif | |
479 | ||
480 | wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); | |
481 | ||
482 | m_formatSupported = FALSE; | |
483 | ||
484 | /* perform query. this will set m_formatSupported to | |
485 | TRUE if m_targetRequested is supported. | |
486 | also, we have to wait for the "answer" from the | |
487 | clipboard owner which is an asynchronous process. | |
488 | therefore we set m_waiting = TRUE here and wait | |
489 | until the callback "targets_selection_received" | |
490 | sets it to FALSE */ | |
491 | ||
492 | m_waiting = TRUE; | |
493 | ||
494 | gtk_selection_convert( m_targetsWidget, | |
495 | m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY | |
496 | : g_clipboardAtom, | |
497 | g_targetsAtom, | |
498 | (guint32) GDK_CURRENT_TIME ); | |
499 | ||
500 | while (m_waiting) gtk_main_iteration(); | |
501 | ||
502 | if (!m_formatSupported) return FALSE; | |
503 | ||
504 | return TRUE; | |
505 | } | |
506 | ||
507 | bool wxClipboard::GetData( wxDataObject& data ) | |
508 | { | |
509 | wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); | |
510 | ||
511 | /* get formats from wxDataObjects */ | |
512 | wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ]; | |
513 | data.GetAllFormats( array ); | |
514 | ||
515 | for (size_t i = 0; i < data.GetFormatCount(); i++) | |
516 | { | |
517 | wxDataFormat format( array[i] ); | |
518 | ||
519 | wxLogTrace( TRACE_CLIPBOARD, | |
520 | wxT("wxClipboard::GetData: requested format: %s"), | |
521 | format.GetId().c_str() ); | |
522 | ||
523 | /* is data supported by clipboard ? */ | |
524 | ||
525 | /* store requested format to be asked for by callbacks */ | |
526 | m_targetRequested = format; | |
527 | ||
528 | wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); | |
529 | ||
530 | m_formatSupported = FALSE; | |
531 | ||
532 | /* perform query. this will set m_formatSupported to | |
533 | TRUE if m_targetRequested is supported. | |
534 | also, we have to wait for the "answer" from the | |
535 | clipboard owner which is an asynchronous process. | |
536 | therefore we set m_waiting = TRUE here and wait | |
537 | until the callback "targets_selection_received" | |
538 | sets it to FALSE */ | |
539 | ||
540 | m_waiting = TRUE; | |
541 | ||
542 | gtk_selection_convert( m_targetsWidget, | |
543 | m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY | |
544 | : g_clipboardAtom, | |
545 | g_targetsAtom, | |
546 | (guint32) GDK_CURRENT_TIME ); | |
547 | ||
548 | while (m_waiting) gtk_main_iteration(); | |
549 | ||
550 | if (!m_formatSupported) continue; | |
551 | ||
552 | /* store pointer to data object to be filled up by callbacks */ | |
553 | m_receivedData = &data; | |
554 | ||
555 | /* store requested format to be asked for by callbacks */ | |
556 | m_targetRequested = format; | |
557 | ||
558 | wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); | |
559 | ||
560 | /* start query */ | |
561 | m_formatSupported = FALSE; | |
562 | ||
563 | /* ask for clipboard contents. this will set | |
564 | m_formatSupported to TRUE if m_targetRequested | |
565 | is supported. | |
566 | also, we have to wait for the "answer" from the | |
567 | clipboard owner which is an asynchronous process. | |
568 | therefore we set m_waiting = TRUE here and wait | |
569 | until the callback "targets_selection_received" | |
570 | sets it to FALSE */ | |
571 | ||
572 | m_waiting = TRUE; | |
573 | ||
574 | wxLogTrace( TRACE_CLIPBOARD, | |
575 | 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 | wxLogTrace( TRACE_CLIPBOARD, | |
594 | wxT("wxClipboard::GetData: format not found") ); | |
595 | ||
596 | /* return failure */ | |
597 | delete[] array; | |
598 | return FALSE; | |
599 | } | |
600 | ||
601 | #endif | |
602 | // wxUSE_CLIPBOARD | |
603 |