]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/clipbrd.cpp | |
3 | // Purpose: wxClipboard implementation for wxGTK | |
4 | // Author: Robert Roebling, Vadim Zeitlin | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // (c) 2007 Vadim Zeitlin | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #if wxUSE_CLIPBOARD | |
23 | ||
24 | #include "wx/clipbrd.h" | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/app.h" | |
28 | #include "wx/log.h" | |
29 | #include "wx/utils.h" | |
30 | #include "wx/dataobj.h" | |
31 | #endif | |
32 | ||
33 | #include "wx/scopedarray.h" | |
34 | #include "wx/scopeguard.h" | |
35 | #include "wx/evtloop.h" | |
36 | ||
37 | #include "wx/gtk/private.h" | |
38 | ||
39 | typedef wxScopedArray<wxDataFormat> wxDataFormatArray; | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // data | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | static GdkAtom g_clipboardAtom = 0; | |
46 | static GdkAtom g_targetsAtom = 0; | |
47 | static GdkAtom g_timestampAtom = 0; | |
48 | ||
49 | #if wxUSE_UNICODE | |
50 | extern GdkAtom g_altTextAtom; | |
51 | #endif | |
52 | ||
53 | // the trace mask we use with wxLogTrace() - call | |
54 | // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here | |
55 | // (there will be a *lot* of them!) | |
56 | #define TRACE_CLIPBOARD wxT("clipboard") | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // wxClipboardSync: used to perform clipboard operations synchronously | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // constructing this object on stack will wait wait until the latest clipboard | |
63 | // operation is finished on block exit | |
64 | // | |
65 | // notice that there can be no more than one such object alive at any moment, | |
66 | // i.e. reentrancies are not allowed | |
67 | class wxClipboardSync | |
68 | { | |
69 | public: | |
70 | wxClipboardSync(wxClipboard& clipboard) | |
71 | { | |
72 | wxASSERT_MSG( !ms_clipboard, wxT("reentrancy in clipboard code") ); | |
73 | ms_clipboard = &clipboard; | |
74 | } | |
75 | ||
76 | ~wxClipboardSync() | |
77 | { | |
78 | #if wxUSE_CONSOLE_EVENTLOOP | |
79 | // ensure that there is a running event loop: this might not be the | |
80 | // case if we're called before the main event loop startup | |
81 | wxEventLoopGuarantor ensureEventLoop; | |
82 | #endif | |
83 | while (ms_clipboard) | |
84 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_CLIPBOARD); | |
85 | } | |
86 | ||
87 | // this method must be called by GTK+ callbacks to indicate that we got the | |
88 | // result for our clipboard operation | |
89 | static void OnDone(wxClipboard * WXUNUSED_UNLESS_DEBUG(clipboard)) | |
90 | { | |
91 | wxASSERT_MSG( clipboard == ms_clipboard, | |
92 | wxT("got notification for alien clipboard") ); | |
93 | ||
94 | ms_clipboard = NULL; | |
95 | } | |
96 | ||
97 | // this method should be called if it's possible that no async clipboard | |
98 | // operation is currently in progress (like it can be the case when the | |
99 | // clipboard is cleared but not because we asked about it), it should only | |
100 | // be called if such situation is expected -- otherwise call OnDone() which | |
101 | // would assert in this case | |
102 | static void OnDoneIfInProgress(wxClipboard *clipboard) | |
103 | { | |
104 | if ( ms_clipboard ) | |
105 | OnDone(clipboard); | |
106 | } | |
107 | ||
108 | private: | |
109 | static wxClipboard *ms_clipboard; | |
110 | ||
111 | wxDECLARE_NO_COPY_CLASS(wxClipboardSync); | |
112 | }; | |
113 | ||
114 | wxClipboard *wxClipboardSync::ms_clipboard = NULL; | |
115 | ||
116 | // ============================================================================ | |
117 | // clipboard callbacks implementation | |
118 | // ============================================================================ | |
119 | ||
120 | //----------------------------------------------------------------------------- | |
121 | // "selection_received" for targets | |
122 | //----------------------------------------------------------------------------- | |
123 | ||
124 | extern "C" { | |
125 | static void | |
126 | targets_selection_received( GtkWidget *WXUNUSED(widget), | |
127 | GtkSelectionData *selection_data, | |
128 | guint32 WXUNUSED(time), | |
129 | wxClipboard *clipboard ) | |
130 | { | |
131 | if ( !clipboard ) | |
132 | return; | |
133 | ||
134 | wxON_BLOCK_EXIT1(wxClipboardSync::OnDone, clipboard); | |
135 | ||
136 | if (!selection_data) | |
137 | return; | |
138 | ||
139 | const int selection_data_length = gtk_selection_data_get_length(selection_data); | |
140 | if (selection_data_length <= 0) | |
141 | return; | |
142 | ||
143 | // make sure we got the data in the correct form | |
144 | GdkAtom type = gtk_selection_data_get_data_type(selection_data); | |
145 | if ( type != GDK_SELECTION_TYPE_ATOM ) | |
146 | { | |
147 | if ( strcmp(wxGtkString(gdk_atom_name(type)), "TARGETS") != 0 ) | |
148 | { | |
149 | wxLogTrace( TRACE_CLIPBOARD, | |
150 | wxT("got unsupported clipboard target") ); | |
151 | ||
152 | return; | |
153 | } | |
154 | } | |
155 | ||
156 | // it's not really a format, of course, but we can reuse its GetId() method | |
157 | // to format this atom as string | |
158 | wxDataFormat clip(gtk_selection_data_get_selection(selection_data)); | |
159 | wxLogTrace( TRACE_CLIPBOARD, | |
160 | wxT("Received available formats for clipboard %s"), | |
161 | clip.GetId().c_str() ); | |
162 | ||
163 | // the atoms we received, holding a list of targets (= formats) | |
164 | const GdkAtom* const atoms = (GdkAtom*)gtk_selection_data_get_data(selection_data); | |
165 | for (size_t i = 0; i < selection_data_length / sizeof(GdkAtom); i++) | |
166 | { | |
167 | const wxDataFormat format(atoms[i]); | |
168 | ||
169 | wxLogTrace(TRACE_CLIPBOARD, wxT("\t%s"), format.GetId().c_str()); | |
170 | ||
171 | if ( clipboard->GTKOnTargetReceived(format) ) | |
172 | return; | |
173 | } | |
174 | } | |
175 | } | |
176 | ||
177 | bool wxClipboard::GTKOnTargetReceived(const wxDataFormat& format) | |
178 | { | |
179 | if ( format != m_targetRequested ) | |
180 | return false; | |
181 | ||
182 | m_formatSupported = true; | |
183 | return true; | |
184 | } | |
185 | ||
186 | //----------------------------------------------------------------------------- | |
187 | // "selection_received" for the actual data | |
188 | //----------------------------------------------------------------------------- | |
189 | ||
190 | extern "C" { | |
191 | static void | |
192 | selection_received( GtkWidget *WXUNUSED(widget), | |
193 | GtkSelectionData *selection_data, | |
194 | guint32 WXUNUSED(time), | |
195 | wxClipboard *clipboard ) | |
196 | { | |
197 | if ( !clipboard ) | |
198 | return; | |
199 | ||
200 | wxON_BLOCK_EXIT1(wxClipboardSync::OnDone, clipboard); | |
201 | ||
202 | if (!selection_data || gtk_selection_data_get_length(selection_data) <= 0) | |
203 | return; | |
204 | ||
205 | clipboard->GTKOnSelectionReceived(*selection_data); | |
206 | } | |
207 | } | |
208 | ||
209 | //----------------------------------------------------------------------------- | |
210 | // "selection_clear" | |
211 | //----------------------------------------------------------------------------- | |
212 | ||
213 | extern "C" { | |
214 | static gint | |
215 | selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event ) | |
216 | { | |
217 | wxClipboard * const clipboard = wxTheClipboard; | |
218 | if ( !clipboard ) | |
219 | return TRUE; | |
220 | ||
221 | // notice the use of OnDoneIfInProgress() here instead of just OnDone(): | |
222 | // it's perfectly possible that we're receiving this notification from GTK+ | |
223 | // even though we hadn't cleared the clipboard ourselves but because | |
224 | // another application (or even another window in the same program) | |
225 | // acquired it | |
226 | wxON_BLOCK_EXIT1(wxClipboardSync::OnDoneIfInProgress, clipboard); | |
227 | ||
228 | wxClipboard::Kind kind; | |
229 | if (event->selection == GDK_SELECTION_PRIMARY) | |
230 | { | |
231 | wxLogTrace(TRACE_CLIPBOARD, wxT("Lost primary selection" )); | |
232 | ||
233 | kind = wxClipboard::Primary; | |
234 | } | |
235 | else if (event->selection == g_clipboardAtom) | |
236 | { | |
237 | wxLogTrace(TRACE_CLIPBOARD, wxT("Lost clipboard" )); | |
238 | ||
239 | kind = wxClipboard::Clipboard; | |
240 | } | |
241 | else // some other selection, we're not concerned | |
242 | { | |
243 | return FALSE; | |
244 | } | |
245 | ||
246 | // the clipboard is no longer in our hands, we don't need data any more | |
247 | clipboard->GTKClearData(kind); | |
248 | ||
249 | return TRUE; | |
250 | } | |
251 | } | |
252 | ||
253 | //----------------------------------------------------------------------------- | |
254 | // selection handler for supplying data | |
255 | //----------------------------------------------------------------------------- | |
256 | ||
257 | extern "C" { | |
258 | static void | |
259 | selection_handler( GtkWidget *WXUNUSED(widget), | |
260 | GtkSelectionData *selection_data, | |
261 | guint WXUNUSED(info), | |
262 | guint WXUNUSED(time), | |
263 | gpointer signal_data ) | |
264 | { | |
265 | wxClipboard * const clipboard = wxTheClipboard; | |
266 | if ( !clipboard ) | |
267 | return; | |
268 | ||
269 | wxDataObject * const data = clipboard->GTKGetDataObject( | |
270 | gtk_selection_data_get_selection(selection_data)); | |
271 | if ( !data ) | |
272 | return; | |
273 | ||
274 | // ICCCM says that TIMESTAMP is a required atom. | |
275 | // In particular, it satisfies Klipper, which polls | |
276 | // TIMESTAMP to see if the clipboards content has changed. | |
277 | // It shall return the time which was used to set the data. | |
278 | if (gtk_selection_data_get_target(selection_data) == g_timestampAtom) | |
279 | { | |
280 | guint timestamp = GPOINTER_TO_UINT (signal_data); | |
281 | gtk_selection_data_set(selection_data, | |
282 | GDK_SELECTION_TYPE_INTEGER, | |
283 | 32, | |
284 | (guchar*)&(timestamp), | |
285 | sizeof(timestamp)); | |
286 | wxLogTrace(TRACE_CLIPBOARD, | |
287 | wxT("Clipboard TIMESTAMP requested, returning timestamp=%u"), | |
288 | timestamp); | |
289 | return; | |
290 | } | |
291 | ||
292 | wxDataFormat format(gtk_selection_data_get_target(selection_data)); | |
293 | ||
294 | wxLogTrace(TRACE_CLIPBOARD, | |
295 | wxT("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s timestamp=%u"), | |
296 | format.GetId().c_str(), | |
297 | wxString::FromAscii(wxGtkString(gdk_atom_name(gtk_selection_data_get_target(selection_data)))).c_str(), | |
298 | wxString::FromAscii(wxGtkString(gdk_atom_name(gtk_selection_data_get_data_type(selection_data)))).c_str(), | |
299 | wxString::FromAscii(wxGtkString(gdk_atom_name(gtk_selection_data_get_selection(selection_data)))).c_str(), | |
300 | GPOINTER_TO_UINT( signal_data ) | |
301 | ); | |
302 | ||
303 | if ( !data->IsSupportedFormat( format ) ) | |
304 | return; | |
305 | ||
306 | int size = data->GetDataSize( format ); | |
307 | if ( !size ) | |
308 | return; | |
309 | ||
310 | wxCharBuffer buf(size - 1); // it adds 1 internally (for NUL) | |
311 | ||
312 | // text data must be returned in UTF8 if format is wxDF_UNICODETEXT | |
313 | if ( !data->GetDataHere(format, buf.data()) ) | |
314 | return; | |
315 | ||
316 | // use UTF8_STRING format if requested in Unicode build but just plain | |
317 | // STRING one in ANSI or if explicitly asked in Unicode | |
318 | #if wxUSE_UNICODE | |
319 | if (format == wxDataFormat(wxDF_UNICODETEXT)) | |
320 | { | |
321 | gtk_selection_data_set_text( | |
322 | selection_data, | |
323 | (const gchar*)buf.data(), | |
324 | size ); | |
325 | } | |
326 | else | |
327 | #endif // wxUSE_UNICODE | |
328 | { | |
329 | gtk_selection_data_set( | |
330 | selection_data, | |
331 | GDK_SELECTION_TYPE_STRING, | |
332 | 8*sizeof(gchar), | |
333 | (const guchar*)buf.data(), | |
334 | size ); | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | void wxClipboard::GTKOnSelectionReceived(const GtkSelectionData& sel) | |
340 | { | |
341 | wxCHECK_RET( m_receivedData, wxT("should be inside GetData()") ); | |
342 | ||
343 | const wxDataFormat format(gtk_selection_data_get_target(const_cast<GtkSelectionData*>(&sel))); | |
344 | wxLogTrace(TRACE_CLIPBOARD, wxT("Received selection %s"), | |
345 | format.GetId().c_str()); | |
346 | ||
347 | if ( !m_receivedData->IsSupportedFormat(format, wxDataObject::Set) ) | |
348 | return; | |
349 | ||
350 | m_receivedData->SetData(format, | |
351 | gtk_selection_data_get_length(const_cast<GtkSelectionData*>(&sel)), | |
352 | gtk_selection_data_get_data(const_cast<GtkSelectionData*>(&sel))); | |
353 | m_formatSupported = true; | |
354 | } | |
355 | ||
356 | //----------------------------------------------------------------------------- | |
357 | // asynchronous "selection_received" for targets | |
358 | //----------------------------------------------------------------------------- | |
359 | ||
360 | extern "C" { | |
361 | static void | |
362 | async_targets_selection_received( GtkWidget *WXUNUSED(widget), | |
363 | GtkSelectionData *selection_data, | |
364 | guint32 WXUNUSED(time), | |
365 | wxClipboard *clipboard ) | |
366 | { | |
367 | if ( !clipboard ) // Assert? | |
368 | return; | |
369 | ||
370 | if (!clipboard->m_sink) | |
371 | return; | |
372 | ||
373 | wxClipboardEvent *event = new wxClipboardEvent(wxEVT_CLIPBOARD_CHANGED); | |
374 | event->SetEventObject( clipboard ); | |
375 | ||
376 | int selection_data_length = 0; | |
377 | if (selection_data) | |
378 | selection_data_length = gtk_selection_data_get_length(selection_data); | |
379 | ||
380 | if (selection_data_length <= 0) | |
381 | { | |
382 | clipboard->m_sink->QueueEvent( event ); | |
383 | clipboard->m_sink.Release(); | |
384 | return; | |
385 | } | |
386 | ||
387 | // make sure we got the data in the correct form | |
388 | GdkAtom type = gtk_selection_data_get_data_type(selection_data); | |
389 | if ( type != GDK_SELECTION_TYPE_ATOM ) | |
390 | { | |
391 | if ( strcmp(wxGtkString(gdk_atom_name(type)), "TARGETS") != 0 ) | |
392 | { | |
393 | wxLogTrace( TRACE_CLIPBOARD, | |
394 | wxT("got unsupported clipboard target") ); | |
395 | ||
396 | clipboard->m_sink->QueueEvent( event ); | |
397 | clipboard->m_sink.Release(); | |
398 | return; | |
399 | } | |
400 | } | |
401 | ||
402 | // it's not really a format, of course, but we can reuse its GetId() method | |
403 | // to format this atom as string | |
404 | wxDataFormat clip(gtk_selection_data_get_selection(selection_data)); | |
405 | wxLogTrace( TRACE_CLIPBOARD, | |
406 | wxT("Received available formats for clipboard %s"), | |
407 | clip.GetId().c_str() ); | |
408 | ||
409 | // the atoms we received, holding a list of targets (= formats) | |
410 | const GdkAtom* const atoms = (GdkAtom*)gtk_selection_data_get_data(selection_data); | |
411 | for (size_t i = 0; i < selection_data_length / sizeof(GdkAtom); i++) | |
412 | { | |
413 | const wxDataFormat format(atoms[i]); | |
414 | ||
415 | wxLogTrace(TRACE_CLIPBOARD, wxT("\t%s"), format.GetId().c_str()); | |
416 | ||
417 | event->AddFormat( format ); | |
418 | } | |
419 | ||
420 | clipboard->m_sink->QueueEvent( event ); | |
421 | clipboard->m_sink.Release(); | |
422 | } | |
423 | } | |
424 | ||
425 | // ============================================================================ | |
426 | // wxClipboard implementation | |
427 | // ============================================================================ | |
428 | ||
429 | // ---------------------------------------------------------------------------- | |
430 | // wxClipboard ctor/dtor | |
431 | // ---------------------------------------------------------------------------- | |
432 | ||
433 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject) | |
434 | ||
435 | wxClipboard::wxClipboard() | |
436 | { | |
437 | m_open = false; | |
438 | ||
439 | m_dataPrimary = | |
440 | m_dataClipboard = | |
441 | m_receivedData = NULL; | |
442 | ||
443 | m_formatSupported = false; | |
444 | m_targetRequested = 0; | |
445 | ||
446 | // we use m_targetsWidget to query what formats are available | |
447 | m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP ); | |
448 | gtk_widget_realize( m_targetsWidget ); | |
449 | ||
450 | g_signal_connect (m_targetsWidget, "selection_received", | |
451 | G_CALLBACK (targets_selection_received), this); | |
452 | ||
453 | // we use m_targetsWidgetAsync to query what formats are available asynchronously | |
454 | m_targetsWidgetAsync = gtk_window_new( GTK_WINDOW_POPUP ); | |
455 | gtk_widget_realize( m_targetsWidgetAsync ); | |
456 | ||
457 | g_signal_connect (m_targetsWidgetAsync, "selection_received", | |
458 | G_CALLBACK (async_targets_selection_received), this); | |
459 | ||
460 | // we use m_clipboardWidget to get and to offer data | |
461 | m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP ); | |
462 | gtk_widget_realize( m_clipboardWidget ); | |
463 | ||
464 | g_signal_connect (m_clipboardWidget, "selection_received", | |
465 | G_CALLBACK (selection_received), this); | |
466 | ||
467 | g_signal_connect (m_clipboardWidget, "selection_clear_event", | |
468 | G_CALLBACK (selection_clear_clip), NULL); | |
469 | ||
470 | // initialize atoms we use if not done yet | |
471 | if ( !g_clipboardAtom ) | |
472 | g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE ); | |
473 | if ( !g_targetsAtom ) | |
474 | g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE); | |
475 | if ( !g_timestampAtom ) | |
476 | g_timestampAtom = gdk_atom_intern ("TIMESTAMP", FALSE); | |
477 | } | |
478 | ||
479 | wxClipboard::~wxClipboard() | |
480 | { | |
481 | Clear(); | |
482 | ||
483 | gtk_widget_destroy( m_clipboardWidget ); | |
484 | gtk_widget_destroy( m_targetsWidget ); | |
485 | } | |
486 | ||
487 | // ---------------------------------------------------------------------------- | |
488 | // wxClipboard helper functions | |
489 | // ---------------------------------------------------------------------------- | |
490 | ||
491 | GdkAtom wxClipboard::GTKGetClipboardAtom() const | |
492 | { | |
493 | return m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY | |
494 | : g_clipboardAtom; | |
495 | } | |
496 | ||
497 | void wxClipboard::GTKClearData(Kind kind) | |
498 | { | |
499 | wxDataObject *&data = Data(kind); | |
500 | wxDELETE(data); | |
501 | } | |
502 | ||
503 | bool wxClipboard::SetSelectionOwner(bool set) | |
504 | { | |
505 | bool rc = gtk_selection_owner_set | |
506 | ( | |
507 | set ? m_clipboardWidget : NULL, | |
508 | GTKGetClipboardAtom(), | |
509 | (guint32)GDK_CURRENT_TIME | |
510 | ); | |
511 | ||
512 | if ( !rc ) | |
513 | { | |
514 | wxLogTrace(TRACE_CLIPBOARD, wxT("Failed to %sset selection owner"), | |
515 | set ? wxT("") : wxT("un")); | |
516 | } | |
517 | ||
518 | return rc; | |
519 | } | |
520 | ||
521 | void wxClipboard::AddSupportedTarget(GdkAtom atom) | |
522 | { | |
523 | gtk_selection_add_target | |
524 | ( | |
525 | m_clipboardWidget, | |
526 | GTKGetClipboardAtom(), | |
527 | atom, | |
528 | 0 // info (same as client data) unused | |
529 | ); | |
530 | } | |
531 | ||
532 | bool wxClipboard::IsSupportedAsync(wxEvtHandler *sink) | |
533 | { | |
534 | if (m_sink.get()) | |
535 | return false; // currently busy, come back later | |
536 | ||
537 | wxCHECK_MSG( sink, false, wxT("no sink given") ); | |
538 | ||
539 | m_sink = sink; | |
540 | gtk_selection_convert( m_targetsWidgetAsync, | |
541 | GTKGetClipboardAtom(), | |
542 | g_targetsAtom, | |
543 | (guint32) GDK_CURRENT_TIME ); | |
544 | ||
545 | return true; | |
546 | } | |
547 | ||
548 | bool wxClipboard::DoIsSupported(const wxDataFormat& format) | |
549 | { | |
550 | wxCHECK_MSG( format, false, wxT("invalid clipboard format") ); | |
551 | ||
552 | wxLogTrace(TRACE_CLIPBOARD, wxT("Checking if format %s is available"), | |
553 | format.GetId().c_str()); | |
554 | ||
555 | // these variables will be used by our GTKOnTargetReceived() | |
556 | m_targetRequested = format; | |
557 | m_formatSupported = false; | |
558 | ||
559 | // block until m_formatSupported is set from targets_selection_received | |
560 | // callback | |
561 | { | |
562 | wxClipboardSync sync(*this); | |
563 | ||
564 | gtk_selection_convert( m_targetsWidget, | |
565 | GTKGetClipboardAtom(), | |
566 | g_targetsAtom, | |
567 | (guint32) GDK_CURRENT_TIME ); | |
568 | } | |
569 | ||
570 | return m_formatSupported; | |
571 | } | |
572 | ||
573 | // ---------------------------------------------------------------------------- | |
574 | // wxClipboard public API implementation | |
575 | // ---------------------------------------------------------------------------- | |
576 | ||
577 | void wxClipboard::Clear() | |
578 | { | |
579 | gtk_selection_clear_targets( m_clipboardWidget, GTKGetClipboardAtom() ); | |
580 | ||
581 | if ( gdk_selection_owner_get(GTKGetClipboardAtom()) == | |
582 | gtk_widget_get_window(m_clipboardWidget) ) | |
583 | { | |
584 | wxClipboardSync sync(*this); | |
585 | ||
586 | // this will result in selection_clear_clip callback being called and | |
587 | // it will free our data | |
588 | SetSelectionOwner(false); | |
589 | } | |
590 | ||
591 | m_targetRequested = 0; | |
592 | m_formatSupported = false; | |
593 | } | |
594 | ||
595 | bool wxClipboard::Open() | |
596 | { | |
597 | wxCHECK_MSG( !m_open, false, wxT("clipboard already open") ); | |
598 | ||
599 | m_open = true; | |
600 | ||
601 | return true; | |
602 | } | |
603 | ||
604 | bool wxClipboard::SetData( wxDataObject *data ) | |
605 | { | |
606 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); | |
607 | ||
608 | wxCHECK_MSG( data, false, wxT("data is invalid") ); | |
609 | ||
610 | Clear(); | |
611 | ||
612 | return AddData( data ); | |
613 | } | |
614 | ||
615 | bool wxClipboard::AddData( wxDataObject *data ) | |
616 | { | |
617 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); | |
618 | ||
619 | wxCHECK_MSG( data, false, wxT("data is invalid") ); | |
620 | ||
621 | // we can only store one wxDataObject so clear the old one | |
622 | Clear(); | |
623 | ||
624 | Data() = data; | |
625 | ||
626 | // get formats from wxDataObjects | |
627 | const size_t count = data->GetFormatCount(); | |
628 | wxDataFormatArray formats(new wxDataFormat[count]); | |
629 | data->GetAllFormats(formats.get()); | |
630 | ||
631 | // always provide TIMESTAMP as a target, see comments in selection_handler | |
632 | // for explanation | |
633 | AddSupportedTarget(g_timestampAtom); | |
634 | ||
635 | for ( size_t i = 0; i < count; i++ ) | |
636 | { | |
637 | const wxDataFormat format(formats[i]); | |
638 | ||
639 | wxLogTrace(TRACE_CLIPBOARD, wxT("Adding support for %s"), | |
640 | format.GetId().c_str()); | |
641 | ||
642 | AddSupportedTarget(format); | |
643 | } | |
644 | ||
645 | g_signal_connect (m_clipboardWidget, "selection_get", | |
646 | G_CALLBACK (selection_handler), | |
647 | GUINT_TO_POINTER (gtk_get_current_event_time()) ); | |
648 | ||
649 | // tell the world we offer clipboard data | |
650 | return SetSelectionOwner(); | |
651 | } | |
652 | ||
653 | void wxClipboard::Close() | |
654 | { | |
655 | wxCHECK_RET( m_open, wxT("clipboard not open") ); | |
656 | ||
657 | m_open = false; | |
658 | } | |
659 | ||
660 | bool wxClipboard::IsOpened() const | |
661 | { | |
662 | return m_open; | |
663 | } | |
664 | ||
665 | bool wxClipboard::IsSupported( const wxDataFormat& format ) | |
666 | { | |
667 | if ( DoIsSupported(format) ) | |
668 | return true; | |
669 | ||
670 | #if wxUSE_UNICODE | |
671 | if ( format == wxDF_UNICODETEXT ) | |
672 | { | |
673 | // also with plain STRING format | |
674 | return DoIsSupported(g_altTextAtom); | |
675 | } | |
676 | #endif // wxUSE_UNICODE | |
677 | ||
678 | return false; | |
679 | } | |
680 | ||
681 | bool wxClipboard::GetData( wxDataObject& data ) | |
682 | { | |
683 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); | |
684 | ||
685 | // get all supported formats from wxDataObjects: notice that we are setting | |
686 | // the object data, so we need them in "Set" direction | |
687 | const size_t count = data.GetFormatCount(wxDataObject::Set); | |
688 | wxDataFormatArray formats(new wxDataFormat[count]); | |
689 | data.GetAllFormats(formats.get(), wxDataObject::Set); | |
690 | ||
691 | for ( size_t i = 0; i < count; i++ ) | |
692 | { | |
693 | const wxDataFormat format(formats[i]); | |
694 | ||
695 | // is this format supported by clipboard ? | |
696 | if ( !DoIsSupported(format) ) | |
697 | continue; | |
698 | ||
699 | wxLogTrace(TRACE_CLIPBOARD, wxT("Requesting format %s"), | |
700 | format.GetId().c_str()); | |
701 | ||
702 | // these variables will be used by our GTKOnSelectionReceived() | |
703 | m_receivedData = &data; | |
704 | m_formatSupported = false; | |
705 | ||
706 | { | |
707 | wxClipboardSync sync(*this); | |
708 | ||
709 | gtk_selection_convert(m_clipboardWidget, | |
710 | GTKGetClipboardAtom(), | |
711 | format, | |
712 | (guint32) GDK_CURRENT_TIME ); | |
713 | } // wait until we get the results | |
714 | ||
715 | /* | |
716 | Normally this is a true error as we checked for the presence of such | |
717 | data before, but there are applications that may return an empty | |
718 | string (e.g. Gnumeric-1.6.1 on Linux if an empty cell is copied) | |
719 | which would produce a false error message here, so we check for the | |
720 | size of the string first. With ANSI, GetDataSize returns an extra | |
721 | value (for the closing null?), with unicode, the exact number of | |
722 | tokens is given (that is more than 1 for non-ASCII characters) | |
723 | (tested with Gnumeric-1.6.1 and OpenOffice.org-2.0.2) | |
724 | */ | |
725 | #if wxUSE_UNICODE | |
726 | if ( format != wxDF_UNICODETEXT || data.GetDataSize(format) > 0 ) | |
727 | #else // !UNICODE | |
728 | if ( format != wxDF_TEXT || data.GetDataSize(format) > 1 ) | |
729 | #endif // UNICODE / !UNICODE | |
730 | { | |
731 | wxCHECK_MSG( m_formatSupported, false, | |
732 | wxT("error retrieving data from clipboard") ); | |
733 | } | |
734 | ||
735 | return true; | |
736 | } | |
737 | ||
738 | wxLogTrace(TRACE_CLIPBOARD, wxT("GetData(): format not found")); | |
739 | ||
740 | return false; | |
741 | } | |
742 | ||
743 | wxDataObject* wxClipboard::GTKGetDataObject( GdkAtom atom ) | |
744 | { | |
745 | if ( atom == GDK_NONE ) | |
746 | return Data(); | |
747 | ||
748 | if ( atom == GDK_SELECTION_PRIMARY ) | |
749 | { | |
750 | wxLogTrace(TRACE_CLIPBOARD, wxT("Primary selection requested" )); | |
751 | ||
752 | return Data( wxClipboard::Primary ); | |
753 | } | |
754 | else if ( atom == g_clipboardAtom ) | |
755 | { | |
756 | wxLogTrace(TRACE_CLIPBOARD, wxT("Clipboard data requested" )); | |
757 | ||
758 | return Data( wxClipboard::Clipboard ); | |
759 | } | |
760 | else // some other selection, we're not concerned | |
761 | { | |
762 | return (wxDataObject*)NULL; | |
763 | } | |
764 | } | |
765 | ||
766 | #endif // wxUSE_CLIPBOARD |