]> git.saurik.com Git - wxWidgets.git/blob - src/x11/clipbrd.cpp
wx{V,H}SCROLL code is needed on all platforms
[wxWidgets.git] / src / x11 / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Robert Roebling
5 // Created:
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // for compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #if wxUSE_CLIPBOARD
15
16 #include "wx/clipbrd.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/log.h"
20 #include "wx/utils.h"
21 #include "wx/dataobj.h"
22 #endif
23
24 #include "wx/x11/private.h"
25
26 //-----------------------------------------------------------------------------
27 // data
28 //-----------------------------------------------------------------------------
29
30 #if !wxUSE_NANOX
31 Atom g_clipboardAtom = 0;
32 Atom g_targetsAtom = 0;
33 #endif
34
35 // the trace mask we use with wxLogTrace() - call
36 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
37 // (there will be a *lot* of them!)
38 static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
39
40 //-----------------------------------------------------------------------------
41 // reminder
42 //-----------------------------------------------------------------------------
43
44 /* The contents of a selection are returned in a GtkSelectionData
45 structure. selection/target identify the request.
46 type specifies the type of the return; if length < 0, and
47 the data should be ignored. This structure has object semantics -
48 no fields should be modified directly, they should not be created
49 directly, and pointers to them should not be stored beyond the duration of
50 a callback. (If the last is changed, we'll need to add reference
51 counting)
52
53 struct _GtkSelectionData
54 {
55 GdkAtom selection;
56 GdkAtom target;
57 GdkAtom type;
58 gint format;
59 guchar *data;
60 gint length;
61 };
62
63 */
64
65 //-----------------------------------------------------------------------------
66 // "selection_received" for targets
67 //-----------------------------------------------------------------------------
68
69 #if 0
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 && selection_data->length > 0 )
80 {
81 /* make sure we got the data in the correct form */
82 GdkAtom type = selection_data->type;
83 if ( type != GDK_SELECTION_TYPE_ATOM )
84 {
85 if ( strcmp(gdk_atom_name(type), "TARGETS") )
86 {
87 wxLogTrace( TRACE_CLIPBOARD,
88 _T("got unsupported clipboard target") );
89
90 clipboard->m_waiting = false;
91 return;
92 }
93 }
94
95 #ifdef __WXDEBUG__
96 wxDataFormat clip( selection_data->selection );
97 wxLogTrace( TRACE_CLIPBOARD,
98 wxT("selection received for targets, clipboard %s"),
99 clip.GetId().c_str() );
100 #endif // __WXDEBUG__
101
102 // the atoms we received, holding a list of targets (= formats)
103 GdkAtom *atoms = (GdkAtom *)selection_data->data;
104
105 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
106 {
107 wxDataFormat format( atoms[i] );
108
109 wxLogTrace( TRACE_CLIPBOARD,
110 wxT("selection received for targets, format %s"),
111 format.GetId().c_str() );
112
113 if (format == clipboard->m_targetRequested)
114 {
115 clipboard->m_waiting = false;
116 clipboard->m_formatSupported = true;
117 return;
118 }
119 }
120 }
121
122 clipboard->m_waiting = false;
123 }
124
125 //-----------------------------------------------------------------------------
126 // "selection_received" for the actual data
127 //-----------------------------------------------------------------------------
128
129 static void
130 selection_received( GtkWidget *WXUNUSED(widget),
131 GtkSelectionData *selection_data,
132 #if (GTK_MINOR_VERSION > 0)
133 guint32 WXUNUSED(time),
134 #endif
135 wxClipboard *clipboard )
136 {
137 if (!wxTheClipboard)
138 {
139 clipboard->m_waiting = false;
140 return;
141 }
142
143 wxDataObject *data_object = clipboard->m_receivedData;
144
145 if (!data_object)
146 {
147 clipboard->m_waiting = false;
148 return;
149 }
150
151 if (selection_data->length <= 0)
152 {
153 clipboard->m_waiting = false;
154 return;
155 }
156
157 wxDataFormat format( selection_data->target );
158
159 /* make sure we got the data in the correct format */
160 if (!data_object->IsSupportedFormat( format ) )
161 {
162 clipboard->m_waiting = false;
163 return;
164 }
165
166 /* make sure we got the data in the correct form (selection type).
167 if so, copy data to target object */
168 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
169 {
170 clipboard->m_waiting = false;
171 return;
172 }
173
174 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
175
176 wxTheClipboard->m_formatSupported = true;
177 clipboard->m_waiting = false;
178 }
179
180 //-----------------------------------------------------------------------------
181 // "selection_clear"
182 //-----------------------------------------------------------------------------
183
184 static gint
185 selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
186 {
187 if (!wxTheClipboard) return TRUE;
188
189 if (event->selection == GDK_SELECTION_PRIMARY)
190 {
191 wxTheClipboard->m_ownsPrimarySelection = false;
192 }
193 else
194 if (event->selection == g_clipboardAtom)
195 {
196 wxTheClipboard->m_ownsClipboard = false;
197 }
198 else
199 {
200 wxTheClipboard->m_waiting = false;
201 return FALSE;
202 }
203
204 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
205 (!wxTheClipboard->m_ownsClipboard))
206 {
207 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
208 if (wxTheClipboard->m_data)
209 {
210 wxLogTrace(TRACE_CLIPBOARD, wxT("wxClipboard will get cleared" ));
211
212 delete wxTheClipboard->m_data;
213 wxTheClipboard->m_data = (wxDataObject*) NULL;
214 }
215 }
216
217 wxTheClipboard->m_waiting = false;
218 return TRUE;
219 }
220
221 //-----------------------------------------------------------------------------
222 // selection handler for supplying data
223 //-----------------------------------------------------------------------------
224
225 static void
226 selection_handler( GtkWidget *WXUNUSED(widget),
227 GtkSelectionData *selection_data,
228 guint WXUNUSED(info),
229 guint WXUNUSED(time),
230 gpointer WXUNUSED(data) )
231 {
232 if (!wxTheClipboard) return;
233
234 if (!wxTheClipboard->m_data) return;
235
236 wxDataObject *data = wxTheClipboard->m_data;
237
238 wxDataFormat format( selection_data->target );
239
240 if (!data->IsSupportedFormat( format )) return;
241
242 int size = data->GetDataSize( format );
243
244 if (size == 0) return;
245
246 void *d = malloc(size);
247
248 data->GetDataHere( selection_data->target, d );
249
250 // transform Unicode text into multibyte before putting it on clipboard
251 #if wxUSE_UNICODE
252 if ( format.GetType() == wxDF_TEXT || format.GetType() == wxDF_UNICODETEXT)
253 {
254 const wchar_t *wstr = (const wchar_t *)d;
255 size_t len = wxConvCurrent->WC2MB(NULL, wstr, 0);
256 char *str = malloc(len + 1);
257 wxConvCurrent->WC2MB(str, wstr, len);
258 str[len] = '\0';
259
260 free(d);
261 d = str;
262 }
263 #endif // wxUSE_UNICODE
264
265 gtk_selection_data_set(
266 selection_data,
267 GDK_SELECTION_TYPE_STRING,
268 8*sizeof(gchar),
269 (unsigned char*) d,
270 size );
271
272 free(d);
273 }
274
275 #endif
276
277 //-----------------------------------------------------------------------------
278 // wxClipboard
279 //-----------------------------------------------------------------------------
280
281 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
282
283 wxClipboard::wxClipboard()
284 {
285 m_open = false;
286
287 m_ownsClipboard = false;
288 m_ownsPrimarySelection = false;
289
290 m_data = (wxDataObject*) NULL;
291 m_receivedData = (wxDataObject*) NULL;
292
293 /* we use m_targetsWidget to query what formats are available */
294
295 /* we use m_clipboardWidget to get and to offer data */
296 #if !wxUSE_NANOX
297 if (!g_clipboardAtom) g_clipboardAtom = XInternAtom( (Display*) wxGetDisplay(), "CLIPBOARD", False );
298 if (!g_targetsAtom) g_targetsAtom = XInternAtom( (Display*) wxGetDisplay(), "TARGETS", False );
299 #endif
300
301 m_formatSupported = false;
302 m_targetRequested = 0;
303
304 m_usePrimary = false;
305 }
306
307 wxClipboard::~wxClipboard()
308 {
309 Clear();
310
311 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
312 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
313 }
314
315 void wxClipboard::Clear()
316 {
317 if (m_data)
318 {
319 #if wxUSE_THREADS
320 /* disable GUI threads */
321 #endif
322
323 /* As we have data we also own the clipboard. Once we no longer own
324 it, clear_selection is called which will set m_data to zero */
325 #if 0
326 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
327 {
328 m_waiting = true;
329
330 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom,
331 (guint32) GDK_CURRENT_TIME );
332
333 while (m_waiting) gtk_main_iteration();
334 }
335
336 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
337 {
338 m_waiting = true;
339
340 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY,
341 (guint32) GDK_CURRENT_TIME );
342
343 while (m_waiting) gtk_main_iteration();
344 }
345 #endif
346
347 if (m_data)
348 {
349 delete m_data;
350 m_data = (wxDataObject*) NULL;
351 }
352
353 #if wxUSE_THREADS
354 /* re-enable GUI threads */
355 #endif
356 }
357
358 m_targetRequested = 0;
359 m_formatSupported = false;
360 }
361
362 bool wxClipboard::Open()
363 {
364 wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
365
366 m_open = true;
367
368 return true;
369 }
370
371 bool wxClipboard::SetData( wxDataObject *data )
372 {
373 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
374
375 wxCHECK_MSG( data, false, wxT("data is invalid") );
376
377 Clear();
378
379 return AddData( data );
380 }
381
382 bool wxClipboard::AddData( wxDataObject *data )
383 {
384 #if wxUSE_NANOX
385 return false;
386 #else
387 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
388
389 wxCHECK_MSG( data, false, wxT("data is invalid") );
390
391 /* we can only store one wxDataObject */
392 Clear();
393
394 m_data = data;
395
396 /* get formats from wxDataObjects */
397 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
398 m_data->GetAllFormats( array );
399
400 #if 0
401 /* primary selection or clipboard */
402 Atom clipboard = m_usePrimary ? (Atom) 1 // 1 = primary selection
403 : g_clipboardAtom;
404 #endif // 0
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 #if 0
414 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
415 clipboard,
416 array[i],
417 0 ); /* what is info ? */
418 #endif
419 }
420
421 delete[] array;
422
423 #if 0
424 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
425 "selection_get",
426 GTK_SIGNAL_FUNC(selection_handler),
427 (gpointer) NULL );
428 #endif
429
430 #if wxUSE_THREADS
431 /* disable GUI threads */
432 #endif
433
434 bool res = false;
435 #if 0
436 /* Tell the world we offer clipboard data */
437 res = (gtk_selection_owner_set( m_clipboardWidget,
438 clipboard,
439 (guint32) GDK_CURRENT_TIME ));
440 #endif
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 #endif
453 }
454
455 void wxClipboard::Close()
456 {
457 wxCHECK_RET( m_open, wxT("clipboard not open") );
458
459 m_open = false;
460 }
461
462 bool wxClipboard::IsOpened() const
463 {
464 return m_open;
465 }
466
467 bool wxClipboard::IsSupported( const wxDataFormat& format )
468 {
469 /* reentrance problems */
470 if (m_waiting) return false;
471
472 /* store requested format to be asked for by callbacks */
473 m_targetRequested = format;
474
475 #if 0
476 wxLogTrace( TRACE_CLIPBOARD,
477 wxT("wxClipboard:IsSupported: requested format: %s"),
478 format.GetId().c_str() );
479 #endif
480
481 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
482
483 m_formatSupported = false;
484
485 /* perform query. this will set m_formatSupported to
486 true if m_targetRequested is supported.
487 also, we have to wait for the "answer" from the
488 clipboard owner which is an asynchronous process.
489 therefore we set m_waiting = true here and wait
490 until the callback "targets_selection_received"
491 sets it to false */
492
493 m_waiting = true;
494
495 #if 0
496 gtk_selection_convert( m_targetsWidget,
497 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
498 : g_clipboardAtom,
499 g_targetsAtom,
500 (guint32) GDK_CURRENT_TIME );
501
502 while (m_waiting) gtk_main_iteration();
503 #endif
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 wxLogTrace( TRACE_CLIPBOARD,
523 wxT("wxClipboard::GetData: requested format: %s"),
524 format.GetId().c_str() );
525
526 /* is data supported by clipboard ? */
527
528 /* store requested format to be asked for by callbacks */
529 m_targetRequested = format;
530
531 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
532
533 m_formatSupported = false;
534
535 /* perform query. this will set m_formatSupported to
536 true if m_targetRequested is supported.
537 also, we have to wait for the "answer" from the
538 clipboard owner which is an asynchronous process.
539 therefore we set m_waiting = true here and wait
540 until the callback "targets_selection_received"
541 sets it to false */
542
543 m_waiting = true;
544
545 #if 0
546 gtk_selection_convert( m_targetsWidget,
547 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
548 : g_clipboardAtom,
549 g_targetsAtom,
550 (guint32) GDK_CURRENT_TIME );
551
552 while (m_waiting) gtk_main_iteration();
553 #endif
554
555 if (!m_formatSupported) continue;
556
557 /* store pointer to data object to be filled up by callbacks */
558 m_receivedData = &data;
559
560 /* store requested format to be asked for by callbacks */
561 m_targetRequested = format;
562
563 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
564
565 /* start query */
566 m_formatSupported = false;
567
568 /* ask for clipboard contents. this will set
569 m_formatSupported to true if m_targetRequested
570 is supported.
571 also, we have to wait for the "answer" from the
572 clipboard owner which is an asynchronous process.
573 therefore we set m_waiting = true here and wait
574 until the callback "targets_selection_received"
575 sets it to false */
576
577 m_waiting = true;
578
579 wxLogTrace( TRACE_CLIPBOARD,
580 wxT("wxClipboard::GetData: format found, start convert") );
581
582 #if 0
583 gtk_selection_convert( m_clipboardWidget,
584 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
585 : g_clipboardAtom,
586 m_targetRequested,
587 (guint32) GDK_CURRENT_TIME );
588
589 while (m_waiting) gtk_main_iteration();
590 #endif
591
592 /* this is a true error as we checked for the presence of such data before */
593 wxCHECK_MSG( m_formatSupported, false, wxT("error retrieving data from clipboard") );
594
595 /* return success */
596 delete[] array;
597 return true;
598 }
599
600 wxLogTrace( TRACE_CLIPBOARD,
601 wxT("wxClipboard::GetData: format not found") );
602
603 /* return failure */
604 delete[] array;
605 return false;
606 }
607
608 #endif
609 // wxUSE_CLIPBOARD