]>
Commit | Line | Data |
---|---|---|
dc86cb34 | 1 | ///////////////////////////////////////////////////////////////////////////// |
61b04ac6 | 2 | // Name: gtk/clipbrd.cpp |
dc86cb34 RR |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
61b04ac6 | 7 | // Licence: wxWindows licence |
dc86cb34 RR |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
dc86cb34 RR |
11 | #pragma implementation "clipbrd.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
dc86cb34 RR |
17 | #include "wx/clipbrd.h" |
18 | ||
06cfab17 | 19 | #if wxUSE_CLIPBOARD |
ac57418f | 20 | |
e1ee679c | 21 | #include "wx/dataobj.h" |
034be888 | 22 | #include "wx/utils.h" |
b068c4e8 | 23 | #include "wx/log.h" |
034be888 | 24 | |
071a2d78 RR |
25 | #include <glib.h> |
26 | #include <gdk/gdk.h> | |
27 | #include <gtk/gtk.h> | |
83624f79 | 28 | |
b453e1b2 RR |
29 | //----------------------------------------------------------------------------- |
30 | // thread system | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | #if wxUSE_THREADS | |
b453e1b2 RR |
34 | #endif |
35 | ||
dc86cb34 RR |
36 | //----------------------------------------------------------------------------- |
37 | // data | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
fd0eed64 | 40 | GdkAtom g_clipboardAtom = 0; |
b527aac5 | 41 | GdkAtom g_targetsAtom = 0; |
fd0eed64 | 42 | |
61b04ac6 VZ |
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!) | |
cbf97a6c | 46 | static const wxChar *TRACE_CLIPBOARD = _T("clipboard"); |
61b04ac6 | 47 | |
dc86cb34 | 48 | //----------------------------------------------------------------------------- |
b527aac5 | 49 | // reminder |
dc86cb34 RR |
50 | //----------------------------------------------------------------------------- |
51 | ||
b527aac5 | 52 | /* The contents of a selection are returned in a GtkSelectionData |
270c23f7 | 53 | structure. selection/target identify the request. |
b527aac5 RR |
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 | |
dc86cb34 | 62 | { |
b527aac5 RR |
63 | GdkAtom selection; |
64 | GdkAtom target; | |
65 | GdkAtom type; | |
61b04ac6 | 66 | gint format; |
b527aac5 | 67 | guchar *data; |
61b04ac6 | 68 | gint length; |
b527aac5 RR |
69 | }; |
70 | ||
71 | */ | |
dc86cb34 | 72 | |
b527aac5 RR |
73 | //----------------------------------------------------------------------------- |
74 | // "selection_received" for targets | |
75 | //----------------------------------------------------------------------------- | |
76 | ||
77 | static void | |
270c23f7 VZ |
78 | targets_selection_received( GtkWidget *WXUNUSED(widget), |
79 | GtkSelectionData *selection_data, | |
034be888 | 80 | guint32 WXUNUSED(time), |
66633398 | 81 | wxClipboard *clipboard ) |
dc86cb34 | 82 | { |
270c23f7 | 83 | if ( wxTheClipboard && selection_data->length > 0 ) |
034be888 | 84 | { |
ca11abde | 85 | // make sure we got the data in the correct form |
270c23f7 VZ |
86 | GdkAtom type = selection_data->type; |
87 | if ( type != GDK_SELECTION_TYPE_ATOM ) | |
88 | { | |
61b04ac6 VZ |
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 | } | |
270c23f7 | 97 | } |
b527aac5 | 98 | |
61b04ac6 | 99 | #ifdef __WXDEBUG__ |
270c23f7 | 100 | wxDataFormat clip( selection_data->selection ); |
61b04ac6 VZ |
101 | wxLogTrace( TRACE_CLIPBOARD, |
102 | wxT("selection received for targets, clipboard %s"), | |
103 | clip.GetId().c_str() ); | |
104 | #endif // __WXDEBUG__ | |
270c23f7 VZ |
105 | |
106 | // the atoms we received, holding a list of targets (= formats) | |
107 | GdkAtom *atoms = (GdkAtom *)selection_data->data; | |
11e1c70d | 108 | |
270c23f7 | 109 | for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++) |
8b53e5a2 | 110 | { |
270c23f7 VZ |
111 | wxDataFormat format( atoms[i] ); |
112 | ||
61b04ac6 VZ |
113 | wxLogTrace( TRACE_CLIPBOARD, |
114 | wxT("selection received for targets, format %s"), | |
115 | format.GetId().c_str() ); | |
270c23f7 | 116 | |
ca11abde RR |
117 | // printf( "format %s requested %s\n", |
118 | // gdk_atom_name( atoms[i] ), | |
119 | // gdk_atom_name( clipboard->m_targetRequested ) ); | |
120 | ||
270c23f7 VZ |
121 | if (format == clipboard->m_targetRequested) |
122 | { | |
123 | clipboard->m_waiting = FALSE; | |
124 | clipboard->m_formatSupported = TRUE; | |
125 | return; | |
126 | } | |
8b53e5a2 RR |
127 | } |
128 | } | |
b527aac5 | 129 | |
034be888 | 130 | clipboard->m_waiting = FALSE; |
dc86cb34 RR |
131 | } |
132 | ||
133 | //----------------------------------------------------------------------------- | |
b527aac5 | 134 | // "selection_received" for the actual data |
dc86cb34 RR |
135 | //----------------------------------------------------------------------------- |
136 | ||
270c23f7 VZ |
137 | static void |
138 | selection_received( GtkWidget *WXUNUSED(widget), | |
139 | GtkSelectionData *selection_data, | |
034be888 | 140 | guint32 WXUNUSED(time), |
66633398 | 141 | wxClipboard *clipboard ) |
dc86cb34 | 142 | { |
034be888 RR |
143 | if (!wxTheClipboard) |
144 | { | |
145 | clipboard->m_waiting = FALSE; | |
146 | return; | |
147 | } | |
270c23f7 | 148 | |
8b53e5a2 | 149 | wxDataObject *data_object = clipboard->m_receivedData; |
1dd989e1 | 150 | |
034be888 RR |
151 | if (!data_object) |
152 | { | |
153 | clipboard->m_waiting = FALSE; | |
154 | return; | |
155 | } | |
270c23f7 | 156 | |
034be888 RR |
157 | if (selection_data->length <= 0) |
158 | { | |
159 | clipboard->m_waiting = FALSE; | |
160 | return; | |
161 | } | |
270c23f7 | 162 | |
b068c4e8 | 163 | wxDataFormat format( selection_data->target ); |
270c23f7 | 164 | |
ca11abde | 165 | // make sure we got the data in the correct format |
b068c4e8 | 166 | if (!data_object->IsSupportedFormat( format ) ) |
034be888 RR |
167 | { |
168 | clipboard->m_waiting = FALSE; | |
169 | return; | |
170 | } | |
e2acb9ae | 171 | |
034be888 RR |
172 | /* make sure we got the data in the correct form (selection type). |
173 | if so, copy data to target object */ | |
e5d6aa22 | 174 | if (selection_data->type != GDK_SELECTION_TYPE_STRING) |
8b53e5a2 | 175 | { |
e5d6aa22 RR |
176 | clipboard->m_waiting = FALSE; |
177 | return; | |
8b53e5a2 | 178 | } |
270c23f7 | 179 | |
e5d6aa22 | 180 | data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data ); |
270c23f7 | 181 | |
8b53e5a2 | 182 | wxTheClipboard->m_formatSupported = TRUE; |
034be888 | 183 | clipboard->m_waiting = FALSE; |
dc86cb34 | 184 | } |
fd0eed64 RR |
185 | |
186 | //----------------------------------------------------------------------------- | |
187 | // "selection_clear" | |
188 | //----------------------------------------------------------------------------- | |
189 | ||
190 | static gint | |
aeeb6a44 | 191 | selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event ) |
fd0eed64 | 192 | { |
8b53e5a2 | 193 | if (!wxTheClipboard) return TRUE; |
270c23f7 | 194 | |
aeeb6a44 RR |
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 | { | |
e5ea3f7a | 206 | wxTheClipboard->m_waiting = FALSE; |
aeeb6a44 RR |
207 | return FALSE; |
208 | } | |
270c23f7 | 209 | |
aeeb6a44 RR |
210 | if ((!wxTheClipboard->m_ownsPrimarySelection) && |
211 | (!wxTheClipboard->m_ownsClipboard)) | |
212 | { | |
db2d879a | 213 | /* the clipboard is no longer in our hands. we can the delete clipboard data. */ |
1dd989e1 | 214 | if (wxTheClipboard->m_data) |
66633398 | 215 | { |
47cf53ef | 216 | wxLogTrace(TRACE_CLIPBOARD, wxT("wxClipboard will get cleared" )); |
270c23f7 | 217 | |
66633398 VZ |
218 | delete wxTheClipboard->m_data; |
219 | wxTheClipboard->m_data = (wxDataObject*) NULL; | |
220 | } | |
aeeb6a44 | 221 | } |
270c23f7 | 222 | |
e5ea3f7a | 223 | wxTheClipboard->m_waiting = FALSE; |
8b53e5a2 | 224 | return TRUE; |
fd0eed64 RR |
225 | } |
226 | ||
227 | //----------------------------------------------------------------------------- | |
228 | // selection handler for supplying data | |
229 | //----------------------------------------------------------------------------- | |
230 | ||
231 | static void | |
19d89516 VZ |
232 | selection_handler( GtkWidget *WXUNUSED(widget), |
233 | GtkSelectionData *selection_data, | |
234 | guint WXUNUSED(info), | |
235 | guint WXUNUSED(time), | |
236 | gpointer WXUNUSED(data) ) | |
fd0eed64 | 237 | { |
8b53e5a2 | 238 | if (!wxTheClipboard) return; |
270c23f7 | 239 | |
1dd989e1 | 240 | if (!wxTheClipboard->m_data) return; |
270c23f7 | 241 | |
1dd989e1 | 242 | wxDataObject *data = wxTheClipboard->m_data; |
270c23f7 | 243 | |
b068c4e8 RR |
244 | wxDataFormat format( selection_data->target ); |
245 | ||
246 | if (!data->IsSupportedFormat( format )) return; | |
270c23f7 | 247 | |
b068c4e8 | 248 | int size = data->GetDataSize( format ); |
270c23f7 | 249 | |
1dd989e1 | 250 | if (size == 0) return; |
270c23f7 | 251 | |
33754c4d VZ |
252 | void *d = malloc(size); |
253 | ||
ca11abde | 254 | // Text data will be in UTF8 in Unicode mode. |
33754c4d | 255 | data->GetDataHere( selection_data->target, d ); |
270c23f7 | 256 | |
270c23f7 VZ |
257 | gtk_selection_data_set( |
258 | selection_data, | |
259 | GDK_SELECTION_TYPE_STRING, | |
260 | 8*sizeof(gchar), | |
66633398 | 261 | (unsigned char*) d, |
270c23f7 | 262 | size ); |
33754c4d VZ |
263 | |
264 | free(d); | |
fd0eed64 | 265 | } |
dc86cb34 RR |
266 | |
267 | //----------------------------------------------------------------------------- | |
268 | // wxClipboard | |
269 | //----------------------------------------------------------------------------- | |
270 | ||
271 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject) | |
272 | ||
273 | wxClipboard::wxClipboard() | |
274 | { | |
8b53e5a2 RR |
275 | m_open = FALSE; |
276 | ||
aeeb6a44 RR |
277 | m_ownsClipboard = FALSE; |
278 | m_ownsPrimarySelection = FALSE; | |
279 | ||
1dd989e1 | 280 | m_data = (wxDataObject*) NULL; |
8b53e5a2 | 281 | m_receivedData = (wxDataObject*) NULL; |
99c67c77 | 282 | |
034be888 | 283 | /* we use m_targetsWidget to query what formats are available */ |
270c23f7 | 284 | |
034be888 RR |
285 | m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP ); |
286 | gtk_widget_realize( m_targetsWidget ); | |
287 | ||
270c23f7 | 288 | gtk_signal_connect( GTK_OBJECT(m_targetsWidget), |
034be888 | 289 | "selection_received", |
270c23f7 | 290 | GTK_SIGNAL_FUNC( targets_selection_received ), |
66633398 | 291 | (gpointer) this ); |
270c23f7 | 292 | |
034be888 | 293 | /* we use m_clipboardWidget to get and to offer data */ |
270c23f7 | 294 | |
8b53e5a2 RR |
295 | m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); |
296 | gtk_widget_realize( m_clipboardWidget ); | |
297 | ||
270c23f7 | 298 | gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), |
034be888 | 299 | "selection_received", |
270c23f7 | 300 | GTK_SIGNAL_FUNC( selection_received ), |
66633398 | 301 | (gpointer) this ); |
034be888 | 302 | |
270c23f7 | 303 | gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), |
8b53e5a2 | 304 | "selection_clear_event", |
270c23f7 | 305 | GTK_SIGNAL_FUNC( selection_clear_clip ), |
66633398 | 306 | (gpointer) NULL ); |
270c23f7 | 307 | |
8b53e5a2 | 308 | if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE ); |
8b53e5a2 | 309 | if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE); |
270c23f7 | 310 | |
8b53e5a2 RR |
311 | m_formatSupported = FALSE; |
312 | m_targetRequested = 0; | |
270c23f7 | 313 | |
7e2c43b8 | 314 | m_usePrimary = FALSE; |
dc86cb34 RR |
315 | } |
316 | ||
317 | wxClipboard::~wxClipboard() | |
b527aac5 | 318 | { |
270c23f7 VZ |
319 | Clear(); |
320 | ||
8b53e5a2 | 321 | if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget ); |
034be888 | 322 | if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget ); |
b527aac5 RR |
323 | } |
324 | ||
325 | void wxClipboard::Clear() | |
dc86cb34 | 326 | { |
1dd989e1 RR |
327 | if (m_data) |
328 | { | |
b453e1b2 RR |
329 | #if wxUSE_THREADS |
330 | /* disable GUI threads */ | |
b453e1b2 | 331 | #endif |
270c23f7 | 332 | |
ca11abde RR |
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 | |
aeeb6a44 | 335 | if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window) |
8b53e5a2 | 336 | { |
e5ea3f7a | 337 | m_waiting = TRUE; |
270c23f7 | 338 | |
b02da6b1 VZ |
339 | gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, |
340 | (guint32) GDK_CURRENT_TIME ); | |
270c23f7 | 341 | |
e5ea3f7a | 342 | while (m_waiting) gtk_main_iteration(); |
8b53e5a2 | 343 | } |
270c23f7 | 344 | |
aeeb6a44 RR |
345 | if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window) |
346 | { | |
e5ea3f7a | 347 | m_waiting = TRUE; |
270c23f7 | 348 | |
b02da6b1 VZ |
349 | gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, |
350 | (guint32) GDK_CURRENT_TIME ); | |
270c23f7 | 351 | |
e5ea3f7a | 352 | while (m_waiting) gtk_main_iteration(); |
aeeb6a44 | 353 | } |
270c23f7 | 354 | |
1dd989e1 | 355 | if (m_data) |
270c23f7 | 356 | { |
66633398 VZ |
357 | delete m_data; |
358 | m_data = (wxDataObject*) NULL; | |
359 | } | |
270c23f7 | 360 | |
b453e1b2 RR |
361 | #if wxUSE_THREADS |
362 | /* re-enable GUI threads */ | |
b453e1b2 | 363 | #endif |
8b53e5a2 | 364 | } |
270c23f7 | 365 | |
8b53e5a2 | 366 | m_targetRequested = 0; |
8b53e5a2 RR |
367 | m_formatSupported = FALSE; |
368 | } | |
369 | ||
370 | bool wxClipboard::Open() | |
371 | { | |
223d09f6 | 372 | wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") ); |
270c23f7 | 373 | |
8b53e5a2 | 374 | m_open = TRUE; |
270c23f7 | 375 | |
8b53e5a2 | 376 | return TRUE; |
dc86cb34 RR |
377 | } |
378 | ||
75ce0581 | 379 | bool wxClipboard::SetData( wxDataObject *data ) |
dc86cb34 | 380 | { |
223d09f6 | 381 | wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); |
270c23f7 | 382 | |
223d09f6 | 383 | wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); |
270c23f7 | 384 | |
0d2a2b60 | 385 | Clear(); |
75ce0581 RR |
386 | |
387 | return AddData( data ); | |
388 | } | |
389 | ||
390 | bool wxClipboard::AddData( wxDataObject *data ) | |
391 | { | |
223d09f6 | 392 | wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); |
270c23f7 | 393 | |
223d09f6 | 394 | wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); |
270c23f7 | 395 | |
ca11abde | 396 | // we can only store one wxDataObject |
1dd989e1 | 397 | Clear(); |
270c23f7 | 398 | |
1dd989e1 RR |
399 | m_data = data; |
400 | ||
ca11abde | 401 | // get formats from wxDataObjects |
b068c4e8 RR |
402 | wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; |
403 | m_data->GetAllFormats( array ); | |
11e1c70d | 404 | |
ca11abde | 405 | // primary selection or clipboard |
11e1c70d RR |
406 | GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY |
407 | : g_clipboardAtom; | |
408 | ||
270c23f7 | 409 | |
b068c4e8 RR |
410 | for (size_t i = 0; i < m_data->GetFormatCount(); i++) |
411 | { | |
61b04ac6 VZ |
412 | wxLogTrace( TRACE_CLIPBOARD, |
413 | wxT("wxClipboard now supports atom %s"), | |
414 | array[i].GetId().c_str() ); | |
11e1c70d | 415 | |
ca11abde RR |
416 | // printf( "added %s\n", |
417 | // gdk_atom_name( array[i].GetFormatId() ) ); | |
418 | ||
11e1c70d | 419 | gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget), |
270c23f7 VZ |
420 | clipboard, |
421 | array[i], | |
11e1c70d | 422 | 0 ); /* what is info ? */ |
b068c4e8 RR |
423 | } |
424 | ||
425 | delete[] array; | |
270c23f7 VZ |
426 | |
427 | gtk_signal_connect( GTK_OBJECT(m_clipboardWidget), | |
d345e841 | 428 | "selection_get", |
270c23f7 | 429 | GTK_SIGNAL_FUNC(selection_handler), |
66633398 | 430 | (gpointer) NULL ); |
d345e841 | 431 | |
b453e1b2 | 432 | #if wxUSE_THREADS |
11e1c70d | 433 | /* disable GUI threads */ |
b453e1b2 | 434 | #endif |
270c23f7 | 435 | |
75ce0581 | 436 | /* Tell the world we offer clipboard data */ |
11e1c70d RR |
437 | bool res = (gtk_selection_owner_set( m_clipboardWidget, |
438 | clipboard, | |
b02da6b1 | 439 | (guint32) GDK_CURRENT_TIME )); |
270c23f7 | 440 | |
11e1c70d RR |
441 | if (m_usePrimary) |
442 | m_ownsPrimarySelection = res; | |
443 | else | |
444 | m_ownsClipboard = res; | |
270c23f7 | 445 | |
b453e1b2 RR |
446 | #if wxUSE_THREADS |
447 | /* re-enable GUI threads */ | |
b453e1b2 | 448 | #endif |
270c23f7 | 449 | |
11e1c70d | 450 | return res; |
8b53e5a2 | 451 | } |
db1b4961 | 452 | |
8b53e5a2 RR |
453 | void wxClipboard::Close() |
454 | { | |
223d09f6 | 455 | wxCHECK_RET( m_open, wxT("clipboard not open") ); |
270c23f7 | 456 | |
8b53e5a2 | 457 | m_open = FALSE; |
dc86cb34 RR |
458 | } |
459 | ||
f536e0f2 VZ |
460 | bool wxClipboard::IsOpened() const |
461 | { | |
462 | return m_open; | |
463 | } | |
464 | ||
e1ee679c | 465 | bool wxClipboard::IsSupported( const wxDataFormat& format ) |
b527aac5 | 466 | { |
e5d6aa22 | 467 | /* reentrance problems */ |
eff869aa | 468 | if (m_waiting) return FALSE; |
270c23f7 | 469 | |
e5d6aa22 | 470 | /* store requested format to be asked for by callbacks */ |
1dd989e1 | 471 | m_targetRequested = format; |
270c23f7 | 472 | |
72a7edf0 | 473 | #if 0 |
61b04ac6 VZ |
474 | wxLogTrace( TRACE_CLIPBOARD, |
475 | wxT("wxClipboard:IsSupported: requested format: %s"), | |
476 | format.GetId().c_str() ); | |
72a7edf0 | 477 | #endif |
eff869aa | 478 | |
223d09f6 | 479 | wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); |
270c23f7 | 480 | |
8b53e5a2 | 481 | m_formatSupported = FALSE; |
270c23f7 VZ |
482 | |
483 | /* perform query. this will set m_formatSupported to | |
034be888 | 484 | TRUE if m_targetRequested is supported. |
270c23f7 | 485 | also, we have to wait for the "answer" from the |
034be888 RR |
486 | clipboard owner which is an asynchronous process. |
487 | therefore we set m_waiting = TRUE here and wait | |
270c23f7 | 488 | until the callback "targets_selection_received" |
034be888 RR |
489 | sets it to FALSE */ |
490 | ||
491 | m_waiting = TRUE; | |
ca35e608 | 492 | |
034be888 | 493 | gtk_selection_convert( m_targetsWidget, |
66633398 | 494 | m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY |
270c23f7 | 495 | : g_clipboardAtom, |
66633398 | 496 | g_targetsAtom, |
b02da6b1 | 497 | (guint32) GDK_CURRENT_TIME ); |
ca35e608 | 498 | |
034be888 | 499 | while (m_waiting) gtk_main_iteration(); |
270c23f7 | 500 | |
8b53e5a2 | 501 | if (!m_formatSupported) return FALSE; |
270c23f7 | 502 | |
75ce0581 | 503 | return TRUE; |
270c23f7 VZ |
504 | } |
505 | ||
e1ee679c | 506 | bool wxClipboard::GetData( wxDataObject& data ) |
75ce0581 | 507 | { |
223d09f6 | 508 | wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); |
270c23f7 | 509 | |
b068c4e8 RR |
510 | /* get formats from wxDataObjects */ |
511 | wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ]; | |
512 | data.GetAllFormats( array ); | |
270c23f7 | 513 | |
b068c4e8 RR |
514 | for (size_t i = 0; i < data.GetFormatCount(); i++) |
515 | { | |
e5d6aa22 | 516 | wxDataFormat format( array[i] ); |
270c23f7 | 517 | |
61b04ac6 VZ |
518 | wxLogTrace( TRACE_CLIPBOARD, |
519 | wxT("wxClipboard::GetData: requested format: %s"), | |
520 | format.GetId().c_str() ); | |
270c23f7 | 521 | |
b068c4e8 | 522 | /* is data supported by clipboard ? */ |
270c23f7 | 523 | |
11e1c70d RR |
524 | /* store requested format to be asked for by callbacks */ |
525 | m_targetRequested = format; | |
270c23f7 | 526 | |
11e1c70d | 527 | wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); |
270c23f7 | 528 | |
11e1c70d | 529 | m_formatSupported = FALSE; |
270c23f7 VZ |
530 | |
531 | /* perform query. this will set m_formatSupported to | |
11e1c70d | 532 | TRUE if m_targetRequested is supported. |
270c23f7 | 533 | also, we have to wait for the "answer" from the |
11e1c70d RR |
534 | clipboard owner which is an asynchronous process. |
535 | therefore we set m_waiting = TRUE here and wait | |
270c23f7 | 536 | until the callback "targets_selection_received" |
11e1c70d RR |
537 | sets it to FALSE */ |
538 | ||
539 | m_waiting = TRUE; | |
540 | ||
541 | gtk_selection_convert( m_targetsWidget, | |
542 | m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY | |
270c23f7 | 543 | : g_clipboardAtom, |
11e1c70d | 544 | g_targetsAtom, |
b02da6b1 | 545 | (guint32) GDK_CURRENT_TIME ); |
11e1c70d RR |
546 | |
547 | while (m_waiting) gtk_main_iteration(); | |
270c23f7 | 548 | |
11e1c70d | 549 | if (!m_formatSupported) continue; |
270c23f7 | 550 | |
b068c4e8 RR |
551 | /* store pointer to data object to be filled up by callbacks */ |
552 | m_receivedData = &data; | |
270c23f7 | 553 | |
b068c4e8 | 554 | /* store requested format to be asked for by callbacks */ |
e5d6aa22 | 555 | m_targetRequested = format; |
270c23f7 | 556 | |
b068c4e8 | 557 | wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); |
270c23f7 | 558 | |
b068c4e8 RR |
559 | /* start query */ |
560 | m_formatSupported = FALSE; | |
270c23f7 VZ |
561 | |
562 | /* ask for clipboard contents. this will set | |
563 | m_formatSupported to TRUE if m_targetRequested | |
b068c4e8 | 564 | is supported. |
270c23f7 | 565 | also, we have to wait for the "answer" from the |
b068c4e8 RR |
566 | clipboard owner which is an asynchronous process. |
567 | therefore we set m_waiting = TRUE here and wait | |
270c23f7 | 568 | until the callback "targets_selection_received" |
b068c4e8 RR |
569 | sets it to FALSE */ |
570 | ||
571 | m_waiting = TRUE; | |
572 | ||
47cf53ef VZ |
573 | wxLogTrace( TRACE_CLIPBOARD, |
574 | wxT("wxClipboard::GetData: format found, start convert") ); | |
270c23f7 | 575 | |
b068c4e8 | 576 | gtk_selection_convert( m_clipboardWidget, |
66633398 | 577 | m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY |
270c23f7 | 578 | : g_clipboardAtom, |
66633398 | 579 | m_targetRequested, |
b02da6b1 | 580 | (guint32) GDK_CURRENT_TIME ); |
270c23f7 | 581 | |
b068c4e8 | 582 | while (m_waiting) gtk_main_iteration(); |
b527aac5 | 583 | |
b068c4e8 RR |
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") ); | |
270c23f7 | 586 | |
66633398 | 587 | /* return success */ |
b068c4e8 RR |
588 | delete[] array; |
589 | return TRUE; | |
590 | } | |
270c23f7 | 591 | |
47cf53ef VZ |
592 | wxLogTrace( TRACE_CLIPBOARD, |
593 | wxT("wxClipboard::GetData: format not found") ); | |
270c23f7 | 594 | |
b068c4e8 RR |
595 | /* return failure */ |
596 | delete[] array; | |
597 | return FALSE; | |
b527aac5 RR |
598 | } |
599 | ||
ac57418f | 600 | #endif |
ac57418f RR |
601 | // wxUSE_CLIPBOARD |
602 |