]> git.saurik.com Git - wxWidgets.git/blame - src/x11/clipbrd.cpp
more fixes to radio menu items: fixed Check() for them; allow separators inside the...
[wxWidgets.git] / src / x11 / clipbrd.cpp
CommitLineData
83df96d6
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: clipbrd.cpp
3// Purpose: Clipboard functionality
9691c806
RR
4// Author: Robert Roebling
5// Created:
83df96d6 6// RCS-ID: $Id$
9691c806 7// Copyright: (c) Robert Roebling
83df96d6
JS
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
83df96d6
JS
12#pragma implementation "clipbrd.h"
13#endif
14
9691c806 15#include "wx/clipbrd.h"
83df96d6
JS
16
17#if wxUSE_CLIPBOARD
18
83df96d6 19#include "wx/dataobj.h"
9691c806
RR
20#include "wx/utils.h"
21#include "wx/log.h"
83df96d6 22
9691c806
RR
23#include "wx/x11/private.h"
24
25//-----------------------------------------------------------------------------
26// data
27//-----------------------------------------------------------------------------
28
70b8ab77 29#if !wxUSE_NANOX
9691c806
RR
30Atom g_clipboardAtom = 0;
31Atom g_targetsAtom = 0;
70b8ab77 32#endif
83df96d6 33
9691c806
RR
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!)
37static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
83df96d6 38
9691c806
RR
39//-----------------------------------------------------------------------------
40// reminder
41//-----------------------------------------------------------------------------
83df96d6 42
9691c806
RR
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)
83df96d6 51
9691c806 52struct _GtkSelectionData
83df96d6 53{
9691c806
RR
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
83df96d6 69
9691c806
RR
70static void
71targets_selection_received( GtkWidget *WXUNUSED(widget),
72 GtkSelectionData *selection_data,
73#if (GTK_MINOR_VERSION > 0)
74 guint32 WXUNUSED(time),
75#endif
76 wxClipboard *clipboard )
83df96d6 77{
9691c806 78 if ( wxTheClipboard && selection_data->length > 0 )
83df96d6 79 {
9691c806
RR
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") );
83df96d6 88
9691c806
RR
89 clipboard->m_waiting = FALSE;
90 return;
91 }
92 }
83df96d6 93
9691c806
RR
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__
83df96d6 100
9691c806
RR
101 // the atoms we received, holding a list of targets (= formats)
102 GdkAtom *atoms = (GdkAtom *)selection_data->data;
83df96d6 103
9691c806
RR
104 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
105 {
106 wxDataFormat format( atoms[i] );
83df96d6 107
9691c806
RR
108 wxLogTrace( TRACE_CLIPBOARD,
109 wxT("selection received for targets, format %s"),
110 format.GetId().c_str() );
83df96d6 111
9691c806
RR
112 if (format == clipboard->m_targetRequested)
113 {
114 clipboard->m_waiting = FALSE;
115 clipboard->m_formatSupported = TRUE;
116 return;
117 }
118 }
119 }
83df96d6 120
9691c806
RR
121 clipboard->m_waiting = FALSE;
122}
83df96d6 123
9691c806
RR
124//-----------------------------------------------------------------------------
125// "selection_received" for the actual data
126//-----------------------------------------------------------------------------
83df96d6 127
9691c806
RR
128static void
129selection_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 }
83df96d6 141
9691c806 142 wxDataObject *data_object = clipboard->m_receivedData;
83df96d6 143
9691c806
RR
144 if (!data_object)
145 {
146 clipboard->m_waiting = FALSE;
147 return;
148 }
83df96d6 149
9691c806
RR
150 if (selection_data->length <= 0)
151 {
152 clipboard->m_waiting = FALSE;
153 return;
154 }
83df96d6 155
9691c806 156 wxDataFormat format( selection_data->target );
83df96d6 157
9691c806
RR
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 }
83df96d6 164
9691c806
RR
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 }
83df96d6 172
9691c806 173 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
83df96d6 174
9691c806
RR
175 wxTheClipboard->m_formatSupported = TRUE;
176 clipboard->m_waiting = FALSE;
83df96d6
JS
177}
178
9691c806
RR
179//-----------------------------------------------------------------------------
180// "selection_clear"
181//-----------------------------------------------------------------------------
83df96d6 182
9691c806
RR
183static gint
184selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
185{
186 if (!wxTheClipboard) return TRUE;
83df96d6 187
9691c806
RR
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 }
83df96d6 202
9691c806
RR
203 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
204 (!wxTheClipboard->m_ownsClipboard))
83df96d6 205 {
9691c806
RR
206 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
207 if (wxTheClipboard->m_data)
83df96d6 208 {
9691c806
RR
209 wxLogTrace(TRACE_CLIPBOARD, wxT("wxClipboard will get cleared" ));
210
211 delete wxTheClipboard->m_data;
212 wxTheClipboard->m_data = (wxDataObject*) NULL;
213 }
83df96d6
JS
214 }
215
9691c806
RR
216 wxTheClipboard->m_waiting = FALSE;
217 return TRUE;
83df96d6
JS
218}
219
9691c806
RR
220//-----------------------------------------------------------------------------
221// selection handler for supplying data
222//-----------------------------------------------------------------------------
83df96d6 223
9691c806
RR
224static void
225selection_handler( GtkWidget *WXUNUSED(widget),
226 GtkSelectionData *selection_data,
227 guint WXUNUSED(info),
228 guint WXUNUSED(time),
229 gpointer WXUNUSED(data) )
83df96d6 230{
9691c806 231 if (!wxTheClipboard) return;
83df96d6 232
9691c806
RR
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 )
83df96d6 252 {
9691c806
RR
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;
83df96d6 261 }
9691c806
RR
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);
83df96d6
JS
272}
273
9691c806
RR
274#endif
275
83df96d6
JS
276//-----------------------------------------------------------------------------
277// wxClipboard
278//-----------------------------------------------------------------------------
279
280IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
281
282wxClipboard::wxClipboard()
283{
284 m_open = FALSE;
9691c806
RR
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 */
70b8ab77 295#if !wxUSE_NANOX
9691c806
RR
296 if (!g_clipboardAtom) g_clipboardAtom = XInternAtom( (Display*) wxGetDisplay(), "CLIPBOARD", False );
297 if (!g_targetsAtom) g_targetsAtom = XInternAtom( (Display*) wxGetDisplay(), "TARGETS", False );
70b8ab77
JS
298#endif
299
9691c806
RR
300 m_formatSupported = FALSE;
301 m_targetRequested = 0;
302
303 m_usePrimary = FALSE;
83df96d6
JS
304}
305
306wxClipboard::~wxClipboard()
307{
9691c806
RR
308 Clear();
309
310// if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
311// if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
83df96d6
JS
312}
313
314void wxClipboard::Clear()
315{
9691c806 316 if (m_data)
83df96d6 317 {
9691c806
RR
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
83df96d6 355 }
9691c806
RR
356
357 m_targetRequested = 0;
358 m_formatSupported = FALSE;
83df96d6
JS
359}
360
361bool wxClipboard::Open()
362{
9691c806
RR
363 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
364
83df96d6
JS
365 m_open = TRUE;
366
9691c806 367 return TRUE;
83df96d6
JS
368}
369
370bool wxClipboard::SetData( wxDataObject *data )
371{
9691c806
RR
372 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
373
374 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
83df96d6
JS
375
376 Clear();
377
378 return AddData( data );
379}
380
381bool wxClipboard::AddData( wxDataObject *data )
382{
70b8ab77
JS
383#if wxUSE_NANOX
384 return FALSE;
385#else
9691c806
RR
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 );
83df96d6 398
cb78397f 399#if 0
9691c806
RR
400 /* primary selection or clipboard */
401 Atom clipboard = m_usePrimary ? (Atom) 1 // 1 = primary selection
402 : g_clipboardAtom;
cb78397f 403#endif // 0
9691c806
RR
404
405
406 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
83df96d6 407 {
9691c806
RR
408 wxLogTrace( TRACE_CLIPBOARD,
409 wxT("wxClipboard now supports atom %s"),
410 array[i].GetId().c_str() );
411
83df96d6 412#if 0
9691c806
RR
413 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
414 clipboard,
415 array[i],
416 0 ); /* what is info ? */
417#endif
83df96d6 418 }
9691c806
RR
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;
70b8ab77 451#endif
83df96d6
JS
452}
453
454void wxClipboard::Close()
455{
9691c806
RR
456 wxCHECK_RET( m_open, wxT("clipboard not open") );
457
83df96d6 458 m_open = FALSE;
83df96d6
JS
459}
460
9691c806 461bool wxClipboard::IsOpened() const
83df96d6 462{
9691c806 463 return m_open;
83df96d6
JS
464}
465
9691c806 466bool wxClipboard::IsSupported( const wxDataFormat& format )
83df96d6 467{
9691c806
RR
468 /* reentrance problems */
469 if (m_waiting) return FALSE;
470
471 /* store requested format to be asked for by callbacks */
472 m_targetRequested = format;
83df96d6
JS
473
474#if 0
9691c806
RR
475 wxLogTrace( TRACE_CLIPBOARD,
476 wxT("wxClipboard:IsSupported: requested format: %s"),
477 format.GetId().c_str() );
478#endif
83df96d6 479
9691c806 480 wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") );
83df96d6 481
9691c806 482 m_formatSupported = FALSE;
83df96d6 483
9691c806
RR
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 */
83df96d6 491
9691c806 492 m_waiting = TRUE;
83df96d6 493
9691c806
RR
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 );
83df96d6 500
9691c806
RR
501 while (m_waiting) gtk_main_iteration();
502#endif
83df96d6 503
9691c806 504 if (!m_formatSupported) return FALSE;
83df96d6 505
9691c806 506 return TRUE;
83df96d6
JS
507}
508
9691c806 509bool wxClipboard::GetData( wxDataObject& data )
83df96d6 510{
9691c806 511 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
83df96d6 512
9691c806
RR
513 /* get formats from wxDataObjects */
514 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
515 data.GetAllFormats( array );
83df96d6 516
9691c806
RR
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;
83df96d6 597 }
9691c806
RR
598
599 wxLogTrace( TRACE_CLIPBOARD,
600 wxT("wxClipboard::GetData: format not found") );
601
602 /* return failure */
603 delete[] array;
604 return FALSE;
83df96d6 605}
9691c806 606
83df96d6 607#endif
9691c806 608 // wxUSE_CLIPBOARD
83df96d6 609