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