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