Compilation fixes for Nano-X
[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 #ifdef __GNUG__
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 /* primary selection or clipboard */
400 Atom clipboard = m_usePrimary ? (Atom) 1 // 1 = primary selection
401 : g_clipboardAtom;
402
403
404 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
405 {
406 wxLogTrace( TRACE_CLIPBOARD,
407 wxT("wxClipboard now supports atom %s"),
408 array[i].GetId().c_str() );
409
410 #if 0
411 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
412 clipboard,
413 array[i],
414 0 ); /* what is info ? */
415 #endif
416 }
417
418 delete[] array;
419
420 #if 0
421 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
422 "selection_get",
423 GTK_SIGNAL_FUNC(selection_handler),
424 (gpointer) NULL );
425 #endif
426
427 #if wxUSE_THREADS
428 /* disable GUI threads */
429 #endif
430
431 bool res = FALSE;
432 #if 0
433 /* Tell the world we offer clipboard data */
434 res = (gtk_selection_owner_set( m_clipboardWidget,
435 clipboard,
436 (guint32) GDK_CURRENT_TIME ));
437 #endif
438
439 if (m_usePrimary)
440 m_ownsPrimarySelection = res;
441 else
442 m_ownsClipboard = res;
443
444 #if wxUSE_THREADS
445 /* re-enable GUI threads */
446 #endif
447
448 return res;
449 #endif
450 }
451
452 void wxClipboard::Close()
453 {
454 wxCHECK_RET( m_open, wxT("clipboard not open") );
455
456 m_open = FALSE;
457 }
458
459 bool wxClipboard::IsOpened() const
460 {
461 return m_open;
462 }
463
464 bool wxClipboard::IsSupported( const wxDataFormat& format )
465 {
466 /* reentrance problems */
467 if (m_waiting) return FALSE;
468
469 /* store requested format to be asked for by callbacks */
470 m_targetRequested = format;
471
472 #if 0
473 wxLogTrace( TRACE_CLIPBOARD,
474 wxT("wxClipboard:IsSupported: requested format: %s"),
475 format.GetId().c_str() );
476 #endif
477
478 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
479
480 m_formatSupported = FALSE;
481
482 /* perform query. this will set m_formatSupported to
483 TRUE if m_targetRequested is supported.
484 also, we have to wait for the "answer" from the
485 clipboard owner which is an asynchronous process.
486 therefore we set m_waiting = TRUE here and wait
487 until the callback "targets_selection_received"
488 sets it to FALSE */
489
490 m_waiting = TRUE;
491
492 #if 0
493 gtk_selection_convert( m_targetsWidget,
494 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
495 : g_clipboardAtom,
496 g_targetsAtom,
497 (guint32) GDK_CURRENT_TIME );
498
499 while (m_waiting) gtk_main_iteration();
500 #endif
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 #if 0
543 gtk_selection_convert( m_targetsWidget,
544 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
545 : g_clipboardAtom,
546 g_targetsAtom,
547 (guint32) GDK_CURRENT_TIME );
548
549 while (m_waiting) gtk_main_iteration();
550 #endif
551
552 if (!m_formatSupported) continue;
553
554 /* store pointer to data object to be filled up by callbacks */
555 m_receivedData = &data;
556
557 /* store requested format to be asked for by callbacks */
558 m_targetRequested = format;
559
560 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
561
562 /* start query */
563 m_formatSupported = FALSE;
564
565 /* ask for clipboard contents. this will set
566 m_formatSupported to TRUE if m_targetRequested
567 is supported.
568 also, we have to wait for the "answer" from the
569 clipboard owner which is an asynchronous process.
570 therefore we set m_waiting = TRUE here and wait
571 until the callback "targets_selection_received"
572 sets it to FALSE */
573
574 m_waiting = TRUE;
575
576 wxLogTrace( TRACE_CLIPBOARD,
577 wxT("wxClipboard::GetData: format found, start convert") );
578
579 #if 0
580 gtk_selection_convert( m_clipboardWidget,
581 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
582 : g_clipboardAtom,
583 m_targetRequested,
584 (guint32) GDK_CURRENT_TIME );
585
586 while (m_waiting) gtk_main_iteration();
587 #endif
588
589 /* this is a true error as we checked for the presence of such data before */
590 wxCHECK_MSG( m_formatSupported, FALSE, wxT("error retrieving data from clipboard") );
591
592 /* return success */
593 delete[] array;
594 return TRUE;
595 }
596
597 wxLogTrace( TRACE_CLIPBOARD,
598 wxT("wxClipboard::GetData: format not found") );
599
600 /* return failure */
601 delete[] array;
602 return FALSE;
603 }
604
605 #endif
606 // wxUSE_CLIPBOARD
607