]> git.saurik.com Git - wxWidgets.git/blame - src/x11/clipbrd.cpp
No changes, synchronised source names that appear commented at the top of files with...
[wxWidgets.git] / src / x11 / clipbrd.cpp
CommitLineData
83df96d6 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/x11/clipbrd.cpp
83df96d6 3// Purpose: Clipboard functionality
9691c806 4// Author: Robert Roebling
e4db172a 5// Created:
83df96d6 6// RCS-ID: $Id$
9691c806 7// Copyright: (c) Robert Roebling
e4db172a 8// Licence: wxWindows licence
83df96d6
JS
9/////////////////////////////////////////////////////////////////////////////
10
e4db172a
WS
11// for compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
83df96d6
JS
13
14#if wxUSE_CLIPBOARD
15
e4db172a
WS
16#include "wx/clipbrd.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/log.h"
de6185e2 20 #include "wx/utils.h"
28f92d74 21 #include "wx/dataobj.h"
e4db172a
WS
22#endif
23
9691c806
RR
24#include "wx/x11/private.h"
25
26//-----------------------------------------------------------------------------
27// data
28//-----------------------------------------------------------------------------
29
70b8ab77 30#if !wxUSE_NANOX
9691c806
RR
31Atom g_clipboardAtom = 0;
32Atom g_targetsAtom = 0;
70b8ab77 33#endif
83df96d6 34
b6349b16
VZ
35// avoid warnings about unused static variable (notice that we still use it
36// even in release build if the compiler doesn't support variadic macros)
37#if defined(__WXDEBUG__) || !defined(HAVE_VARIADIC_MACROS)
542a26ba 38
9691c806
RR
39// the trace mask we use with wxLogTrace() - call
40// wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
41// (there will be a *lot* of them!)
9a83f860 42static const wxChar *TRACE_CLIPBOARD = wxT("clipboard");
542a26ba 43
0b575029 44#endif // __WXDEBUG__
83df96d6 45
9691c806
RR
46//-----------------------------------------------------------------------------
47// reminder
48//-----------------------------------------------------------------------------
83df96d6 49
9691c806
RR
50/* The contents of a selection are returned in a GtkSelectionData
51 structure. selection/target identify the request.
52 type specifies the type of the return; if length < 0, and
53 the data should be ignored. This structure has object semantics -
54 no fields should be modified directly, they should not be created
55 directly, and pointers to them should not be stored beyond the duration of
56 a callback. (If the last is changed, we'll need to add reference
57 counting)
83df96d6 58
9691c806 59struct _GtkSelectionData
83df96d6 60{
9691c806
RR
61 GdkAtom selection;
62 GdkAtom target;
63 GdkAtom type;
64 gint format;
65 guchar *data;
66 gint length;
67};
68
69*/
70
71//-----------------------------------------------------------------------------
72// "selection_received" for targets
73//-----------------------------------------------------------------------------
74
75#if 0
83df96d6 76
9691c806
RR
77static void
78targets_selection_received( GtkWidget *WXUNUSED(widget),
79 GtkSelectionData *selection_data,
80#if (GTK_MINOR_VERSION > 0)
81 guint32 WXUNUSED(time),
82#endif
83 wxClipboard *clipboard )
83df96d6 84{
9691c806 85 if ( wxTheClipboard && selection_data->length > 0 )
83df96d6 86 {
9691c806
RR
87 /* make sure we got the data in the correct form */
88 GdkAtom type = selection_data->type;
89 if ( type != GDK_SELECTION_TYPE_ATOM )
90 {
91 if ( strcmp(gdk_atom_name(type), "TARGETS") )
92 {
93 wxLogTrace( TRACE_CLIPBOARD,
9a83f860 94 wxT("got unsupported clipboard target") );
83df96d6 95
de6185e2 96 clipboard->m_waiting = false;
9691c806
RR
97 return;
98 }
99 }
83df96d6 100
9691c806
RR
101#ifdef __WXDEBUG__
102 wxDataFormat clip( selection_data->selection );
103 wxLogTrace( TRACE_CLIPBOARD,
104 wxT("selection received for targets, clipboard %s"),
105 clip.GetId().c_str() );
106#endif // __WXDEBUG__
83df96d6 107
9691c806
RR
108 // the atoms we received, holding a list of targets (= formats)
109 GdkAtom *atoms = (GdkAtom *)selection_data->data;
83df96d6 110
9691c806
RR
111 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
112 {
113 wxDataFormat format( atoms[i] );
83df96d6 114
9691c806
RR
115 wxLogTrace( TRACE_CLIPBOARD,
116 wxT("selection received for targets, format %s"),
117 format.GetId().c_str() );
83df96d6 118
9691c806
RR
119 if (format == clipboard->m_targetRequested)
120 {
de6185e2 121 clipboard->m_waiting = false;
e4db172a 122 clipboard->m_formatSupported = true;
9691c806
RR
123 return;
124 }
125 }
126 }
83df96d6 127
de6185e2 128 clipboard->m_waiting = false;
9691c806 129}
83df96d6 130
9691c806
RR
131//-----------------------------------------------------------------------------
132// "selection_received" for the actual data
133//-----------------------------------------------------------------------------
83df96d6 134
9691c806
RR
135static void
136selection_received( GtkWidget *WXUNUSED(widget),
137 GtkSelectionData *selection_data,
138#if (GTK_MINOR_VERSION > 0)
139 guint32 WXUNUSED(time),
140#endif
141 wxClipboard *clipboard )
142{
143 if (!wxTheClipboard)
144 {
de6185e2 145 clipboard->m_waiting = false;
9691c806
RR
146 return;
147 }
83df96d6 148
9691c806 149 wxDataObject *data_object = clipboard->m_receivedData;
83df96d6 150
9691c806
RR
151 if (!data_object)
152 {
de6185e2 153 clipboard->m_waiting = false;
9691c806
RR
154 return;
155 }
83df96d6 156
9691c806
RR
157 if (selection_data->length <= 0)
158 {
de6185e2 159 clipboard->m_waiting = false;
9691c806
RR
160 return;
161 }
83df96d6 162
9691c806 163 wxDataFormat format( selection_data->target );
83df96d6 164
9691c806
RR
165 /* make sure we got the data in the correct format */
166 if (!data_object->IsSupportedFormat( format ) )
167 {
de6185e2 168 clipboard->m_waiting = false;
9691c806
RR
169 return;
170 }
83df96d6 171
9691c806
RR
172 /* make sure we got the data in the correct form (selection type).
173 if so, copy data to target object */
174 if (selection_data->type != GDK_SELECTION_TYPE_STRING)
175 {
de6185e2 176 clipboard->m_waiting = false;
9691c806
RR
177 return;
178 }
83df96d6 179
9691c806 180 data_object->SetData( format, (size_t) selection_data->length, (const char*) selection_data->data );
83df96d6 181
e4db172a 182 wxTheClipboard->m_formatSupported = true;
de6185e2 183 clipboard->m_waiting = false;
83df96d6
JS
184}
185
9691c806
RR
186//-----------------------------------------------------------------------------
187// "selection_clear"
188//-----------------------------------------------------------------------------
83df96d6 189
9691c806
RR
190static gint
191selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event )
192{
193 if (!wxTheClipboard) return TRUE;
83df96d6 194
9691c806
RR
195 if (event->selection == GDK_SELECTION_PRIMARY)
196 {
de6185e2 197 wxTheClipboard->m_ownsPrimarySelection = false;
9691c806
RR
198 }
199 else
200 if (event->selection == g_clipboardAtom)
201 {
de6185e2 202 wxTheClipboard->m_ownsClipboard = false;
9691c806
RR
203 }
204 else
205 {
de6185e2 206 wxTheClipboard->m_waiting = false;
9691c806
RR
207 return FALSE;
208 }
83df96d6 209
9691c806
RR
210 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
211 (!wxTheClipboard->m_ownsClipboard))
83df96d6 212 {
9691c806
RR
213 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
214 if (wxTheClipboard->m_data)
83df96d6 215 {
9691c806
RR
216 wxLogTrace(TRACE_CLIPBOARD, wxT("wxClipboard will get cleared" ));
217
5276b0a5 218 wxDELETE(wxTheClipboard->m_data);
9691c806 219 }
83df96d6
JS
220 }
221
de6185e2 222 wxTheClipboard->m_waiting = false;
9691c806 223 return TRUE;
83df96d6
JS
224}
225
9691c806
RR
226//-----------------------------------------------------------------------------
227// selection handler for supplying data
228//-----------------------------------------------------------------------------
83df96d6 229
9691c806
RR
230static void
231selection_handler( GtkWidget *WXUNUSED(widget),
232 GtkSelectionData *selection_data,
233 guint WXUNUSED(info),
234 guint WXUNUSED(time),
235 gpointer WXUNUSED(data) )
83df96d6 236{
9691c806 237 if (!wxTheClipboard) return;
83df96d6 238
9691c806
RR
239 if (!wxTheClipboard->m_data) return;
240
241 wxDataObject *data = wxTheClipboard->m_data;
242
243 wxDataFormat format( selection_data->target );
244
245 if (!data->IsSupportedFormat( format )) return;
246
247 int size = data->GetDataSize( format );
248
249 if (size == 0) return;
250
251 void *d = malloc(size);
252
253 data->GetDataHere( selection_data->target, d );
254
255 // transform Unicode text into multibyte before putting it on clipboard
256#if wxUSE_UNICODE
daad5a23 257 if ( format.GetType() == wxDF_TEXT || format.GetType() == wxDF_UNICODETEXT)
83df96d6 258 {
9691c806
RR
259 const wchar_t *wstr = (const wchar_t *)d;
260 size_t len = wxConvCurrent->WC2MB(NULL, wstr, 0);
261 char *str = malloc(len + 1);
262 wxConvCurrent->WC2MB(str, wstr, len);
263 str[len] = '\0';
264
265 free(d);
266 d = str;
83df96d6 267 }
9691c806
RR
268#endif // wxUSE_UNICODE
269
270 gtk_selection_data_set(
271 selection_data,
272 GDK_SELECTION_TYPE_STRING,
273 8*sizeof(gchar),
274 (unsigned char*) d,
275 size );
276
277 free(d);
83df96d6
JS
278}
279
9691c806
RR
280#endif
281
83df96d6
JS
282//-----------------------------------------------------------------------------
283// wxClipboard
284//-----------------------------------------------------------------------------
285
286IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
287
288wxClipboard::wxClipboard()
289{
de6185e2 290 m_open = false;
9691c806 291
de6185e2
WS
292 m_ownsClipboard = false;
293 m_ownsPrimarySelection = false;
9691c806 294
d3b9f782
VZ
295 m_data = NULL;
296 m_receivedData = NULL;
9691c806
RR
297
298 /* we use m_targetsWidget to query what formats are available */
299
300 /* we use m_clipboardWidget to get and to offer data */
70b8ab77 301#if !wxUSE_NANOX
9691c806
RR
302 if (!g_clipboardAtom) g_clipboardAtom = XInternAtom( (Display*) wxGetDisplay(), "CLIPBOARD", False );
303 if (!g_targetsAtom) g_targetsAtom = XInternAtom( (Display*) wxGetDisplay(), "TARGETS", False );
70b8ab77 304#endif
e4db172a 305
de6185e2 306 m_formatSupported = false;
9691c806 307 m_targetRequested = 0;
83df96d6
JS
308}
309
310wxClipboard::~wxClipboard()
311{
9691c806
RR
312 Clear();
313
314// if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
315// if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
83df96d6
JS
316}
317
318void wxClipboard::Clear()
319{
9691c806 320 if (m_data)
83df96d6 321 {
9691c806
RR
322#if wxUSE_THREADS
323 /* disable GUI threads */
324#endif
325
326 /* As we have data we also own the clipboard. Once we no longer own
327 it, clear_selection is called which will set m_data to zero */
328#if 0
329 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
330 {
e4db172a 331 m_waiting = true;
9691c806 332
d3b9f782 333 gtk_selection_owner_set( NULL, g_clipboardAtom,
9691c806
RR
334 (guint32) GDK_CURRENT_TIME );
335
336 while (m_waiting) gtk_main_iteration();
337 }
338
339 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
340 {
e4db172a 341 m_waiting = true;
9691c806 342
d3b9f782 343 gtk_selection_owner_set( NULL, GDK_SELECTION_PRIMARY,
9691c806
RR
344 (guint32) GDK_CURRENT_TIME );
345
346 while (m_waiting) gtk_main_iteration();
347 }
348#endif
349
5276b0a5 350 wxDELETE(m_data);
9691c806
RR
351
352#if wxUSE_THREADS
353 /* re-enable GUI threads */
354#endif
83df96d6 355 }
9691c806
RR
356
357 m_targetRequested = 0;
e4db172a 358 m_formatSupported = false;
83df96d6
JS
359}
360
361bool wxClipboard::Open()
362{
e4db172a 363 wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
9691c806 364
e4db172a 365 m_open = true;
83df96d6 366
e4db172a 367 return true;
83df96d6
JS
368}
369
370bool wxClipboard::SetData( wxDataObject *data )
371{
de6185e2 372 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
9691c806 373
de6185e2 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 383#if wxUSE_NANOX
de6185e2 384 return false;
70b8ab77 385#else
de6185e2 386 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
9691c806 387
de6185e2 388 wxCHECK_MSG( data, false, wxT("data is invalid") );
9691c806
RR
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
de6185e2 433 bool res = false;
9691c806
RR
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
de6185e2 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 468 /* reentrance problems */
e4db172a 469 if (m_waiting) return false;
9691c806
RR
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
e4db172a 480 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
83df96d6 481
e4db172a 482 m_formatSupported = false;
83df96d6 483
9691c806 484 /* perform query. this will set m_formatSupported to
e4db172a 485 true if m_targetRequested is supported.
9691c806
RR
486 also, we have to wait for the "answer" from the
487 clipboard owner which is an asynchronous process.
e4db172a 488 therefore we set m_waiting = true here and wait
9691c806 489 until the callback "targets_selection_received"
e4db172a 490 sets it to false */
83df96d6 491
e4db172a 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
e4db172a 504 if (!m_formatSupported) return false;
83df96d6 505
e4db172a 506 return true;
83df96d6
JS
507}
508
9691c806 509bool wxClipboard::GetData( wxDataObject& data )
83df96d6 510{
e4db172a 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
e4db172a 530 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
9691c806 531
e4db172a 532 m_formatSupported = false;
9691c806
RR
533
534 /* perform query. this will set m_formatSupported to
e4db172a 535 true if m_targetRequested is supported.
9691c806
RR
536 also, we have to wait for the "answer" from the
537 clipboard owner which is an asynchronous process.
e4db172a 538 therefore we set m_waiting = true here and wait
9691c806 539 until the callback "targets_selection_received"
de6185e2 540 sets it to false */
9691c806 541
e4db172a 542 m_waiting = true;
9691c806
RR
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
e4db172a 562 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
9691c806
RR
563
564 /* start query */
e4db172a 565 m_formatSupported = false;
9691c806
RR
566
567 /* ask for clipboard contents. this will set
e4db172a 568 m_formatSupported to true if m_targetRequested
9691c806
RR
569 is supported.
570 also, we have to wait for the "answer" from the
571 clipboard owner which is an asynchronous process.
e4db172a 572 therefore we set m_waiting = true here and wait
9691c806 573 until the callback "targets_selection_received"
e4db172a 574 sets it to false */
9691c806 575
e4db172a 576 m_waiting = true;
9691c806
RR
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 */
de6185e2 592 wxCHECK_MSG( m_formatSupported, false, wxT("error retrieving data from clipboard") );
9691c806
RR
593
594 /* return success */
595 delete[] array;
e4db172a 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;
e4db172a 604 return false;
83df96d6 605}
9691c806 606
83df96d6 607#endif
9691c806 608 // wxUSE_CLIPBOARD