]> git.saurik.com Git - wxWidgets.git/blame - src/x11/clipbrd.cpp
Use wxCONTROL_SPECIAL to denote first header button
[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!)
42static const wxChar *TRACE_CLIPBOARD = _T("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,
94 _T("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
218 delete wxTheClipboard->m_data;
d3b9f782 219 wxTheClipboard->m_data = NULL;
9691c806 220 }
83df96d6
JS
221 }
222
de6185e2 223 wxTheClipboard->m_waiting = false;
9691c806 224 return TRUE;
83df96d6
JS
225}
226
9691c806
RR
227//-----------------------------------------------------------------------------
228// selection handler for supplying data
229//-----------------------------------------------------------------------------
83df96d6 230
9691c806
RR
231static void
232selection_handler( GtkWidget *WXUNUSED(widget),
233 GtkSelectionData *selection_data,
234 guint WXUNUSED(info),
235 guint WXUNUSED(time),
236 gpointer WXUNUSED(data) )
83df96d6 237{
9691c806 238 if (!wxTheClipboard) return;
83df96d6 239
9691c806
RR
240 if (!wxTheClipboard->m_data) return;
241
242 wxDataObject *data = wxTheClipboard->m_data;
243
244 wxDataFormat format( selection_data->target );
245
246 if (!data->IsSupportedFormat( format )) return;
247
248 int size = data->GetDataSize( format );
249
250 if (size == 0) return;
251
252 void *d = malloc(size);
253
254 data->GetDataHere( selection_data->target, d );
255
256 // transform Unicode text into multibyte before putting it on clipboard
257#if wxUSE_UNICODE
daad5a23 258 if ( format.GetType() == wxDF_TEXT || format.GetType() == wxDF_UNICODETEXT)
83df96d6 259 {
9691c806
RR
260 const wchar_t *wstr = (const wchar_t *)d;
261 size_t len = wxConvCurrent->WC2MB(NULL, wstr, 0);
262 char *str = malloc(len + 1);
263 wxConvCurrent->WC2MB(str, wstr, len);
264 str[len] = '\0';
265
266 free(d);
267 d = str;
83df96d6 268 }
9691c806
RR
269#endif // wxUSE_UNICODE
270
271 gtk_selection_data_set(
272 selection_data,
273 GDK_SELECTION_TYPE_STRING,
274 8*sizeof(gchar),
275 (unsigned char*) d,
276 size );
277
278 free(d);
83df96d6
JS
279}
280
9691c806
RR
281#endif
282
83df96d6
JS
283//-----------------------------------------------------------------------------
284// wxClipboard
285//-----------------------------------------------------------------------------
286
287IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
288
289wxClipboard::wxClipboard()
290{
de6185e2 291 m_open = false;
9691c806 292
de6185e2
WS
293 m_ownsClipboard = false;
294 m_ownsPrimarySelection = false;
9691c806 295
d3b9f782
VZ
296 m_data = NULL;
297 m_receivedData = NULL;
9691c806
RR
298
299 /* we use m_targetsWidget to query what formats are available */
300
301 /* we use m_clipboardWidget to get and to offer data */
70b8ab77 302#if !wxUSE_NANOX
9691c806
RR
303 if (!g_clipboardAtom) g_clipboardAtom = XInternAtom( (Display*) wxGetDisplay(), "CLIPBOARD", False );
304 if (!g_targetsAtom) g_targetsAtom = XInternAtom( (Display*) wxGetDisplay(), "TARGETS", False );
70b8ab77 305#endif
e4db172a 306
de6185e2 307 m_formatSupported = false;
9691c806 308 m_targetRequested = 0;
83df96d6
JS
309}
310
311wxClipboard::~wxClipboard()
312{
9691c806
RR
313 Clear();
314
315// if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
316// if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
83df96d6
JS
317}
318
319void wxClipboard::Clear()
320{
9691c806 321 if (m_data)
83df96d6 322 {
9691c806
RR
323#if wxUSE_THREADS
324 /* disable GUI threads */
325#endif
326
327 /* As we have data we also own the clipboard. Once we no longer own
328 it, clear_selection is called which will set m_data to zero */
329#if 0
330 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
331 {
e4db172a 332 m_waiting = true;
9691c806 333
d3b9f782 334 gtk_selection_owner_set( NULL, g_clipboardAtom,
9691c806
RR
335 (guint32) GDK_CURRENT_TIME );
336
337 while (m_waiting) gtk_main_iteration();
338 }
339
340 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
341 {
e4db172a 342 m_waiting = true;
9691c806 343
d3b9f782 344 gtk_selection_owner_set( NULL, GDK_SELECTION_PRIMARY,
9691c806
RR
345 (guint32) GDK_CURRENT_TIME );
346
347 while (m_waiting) gtk_main_iteration();
348 }
349#endif
350
351 if (m_data)
352 {
353 delete m_data;
d3b9f782 354 m_data = NULL;
9691c806
RR
355 }
356
357#if wxUSE_THREADS
358 /* re-enable GUI threads */
359#endif
83df96d6 360 }
9691c806
RR
361
362 m_targetRequested = 0;
e4db172a 363 m_formatSupported = false;
83df96d6
JS
364}
365
366bool wxClipboard::Open()
367{
e4db172a 368 wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
9691c806 369
e4db172a 370 m_open = true;
83df96d6 371
e4db172a 372 return true;
83df96d6
JS
373}
374
375bool wxClipboard::SetData( wxDataObject *data )
376{
de6185e2 377 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
9691c806 378
de6185e2 379 wxCHECK_MSG( data, false, wxT("data is invalid") );
83df96d6
JS
380
381 Clear();
382
383 return AddData( data );
384}
385
386bool wxClipboard::AddData( wxDataObject *data )
387{
70b8ab77 388#if wxUSE_NANOX
de6185e2 389 return false;
70b8ab77 390#else
de6185e2 391 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
9691c806 392
de6185e2 393 wxCHECK_MSG( data, false, wxT("data is invalid") );
9691c806
RR
394
395 /* we can only store one wxDataObject */
396 Clear();
397
398 m_data = data;
399
400 /* get formats from wxDataObjects */
401 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
402 m_data->GetAllFormats( array );
83df96d6 403
cb78397f 404#if 0
9691c806
RR
405 /* primary selection or clipboard */
406 Atom clipboard = m_usePrimary ? (Atom) 1 // 1 = primary selection
407 : g_clipboardAtom;
cb78397f 408#endif // 0
9691c806
RR
409
410
411 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
83df96d6 412 {
9691c806
RR
413 wxLogTrace( TRACE_CLIPBOARD,
414 wxT("wxClipboard now supports atom %s"),
415 array[i].GetId().c_str() );
416
83df96d6 417#if 0
9691c806
RR
418 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
419 clipboard,
420 array[i],
421 0 ); /* what is info ? */
422#endif
83df96d6 423 }
9691c806
RR
424
425 delete[] array;
426
427#if 0
428 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
429 "selection_get",
430 GTK_SIGNAL_FUNC(selection_handler),
431 (gpointer) NULL );
432#endif
433
434#if wxUSE_THREADS
435 /* disable GUI threads */
436#endif
437
de6185e2 438 bool res = false;
9691c806
RR
439#if 0
440 /* Tell the world we offer clipboard data */
441 res = (gtk_selection_owner_set( m_clipboardWidget,
442 clipboard,
443 (guint32) GDK_CURRENT_TIME ));
444#endif
445
446 if (m_usePrimary)
447 m_ownsPrimarySelection = res;
448 else
449 m_ownsClipboard = res;
450
451#if wxUSE_THREADS
452 /* re-enable GUI threads */
453#endif
454
455 return res;
70b8ab77 456#endif
83df96d6
JS
457}
458
459void wxClipboard::Close()
460{
9691c806
RR
461 wxCHECK_RET( m_open, wxT("clipboard not open") );
462
de6185e2 463 m_open = false;
83df96d6
JS
464}
465
9691c806 466bool wxClipboard::IsOpened() const
83df96d6 467{
9691c806 468 return m_open;
83df96d6
JS
469}
470
9691c806 471bool wxClipboard::IsSupported( const wxDataFormat& format )
83df96d6 472{
9691c806 473 /* reentrance problems */
e4db172a 474 if (m_waiting) return false;
9691c806
RR
475
476 /* store requested format to be asked for by callbacks */
477 m_targetRequested = format;
83df96d6
JS
478
479#if 0
9691c806
RR
480 wxLogTrace( TRACE_CLIPBOARD,
481 wxT("wxClipboard:IsSupported: requested format: %s"),
482 format.GetId().c_str() );
483#endif
83df96d6 484
e4db172a 485 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
83df96d6 486
e4db172a 487 m_formatSupported = false;
83df96d6 488
9691c806 489 /* perform query. this will set m_formatSupported to
e4db172a 490 true if m_targetRequested is supported.
9691c806
RR
491 also, we have to wait for the "answer" from the
492 clipboard owner which is an asynchronous process.
e4db172a 493 therefore we set m_waiting = true here and wait
9691c806 494 until the callback "targets_selection_received"
e4db172a 495 sets it to false */
83df96d6 496
e4db172a 497 m_waiting = true;
83df96d6 498
9691c806
RR
499#if 0
500 gtk_selection_convert( m_targetsWidget,
501 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
502 : g_clipboardAtom,
503 g_targetsAtom,
504 (guint32) GDK_CURRENT_TIME );
83df96d6 505
9691c806
RR
506 while (m_waiting) gtk_main_iteration();
507#endif
83df96d6 508
e4db172a 509 if (!m_formatSupported) return false;
83df96d6 510
e4db172a 511 return true;
83df96d6
JS
512}
513
9691c806 514bool wxClipboard::GetData( wxDataObject& data )
83df96d6 515{
e4db172a 516 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
83df96d6 517
9691c806
RR
518 /* get formats from wxDataObjects */
519 wxDataFormat *array = new wxDataFormat[ data.GetFormatCount() ];
520 data.GetAllFormats( array );
83df96d6 521
9691c806
RR
522 for (size_t i = 0; i < data.GetFormatCount(); i++)
523 {
524 wxDataFormat format( array[i] );
525
526 wxLogTrace( TRACE_CLIPBOARD,
527 wxT("wxClipboard::GetData: requested format: %s"),
528 format.GetId().c_str() );
529
530 /* is data supported by clipboard ? */
531
532 /* store requested format to be asked for by callbacks */
533 m_targetRequested = format;
534
e4db172a 535 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
9691c806 536
e4db172a 537 m_formatSupported = false;
9691c806
RR
538
539 /* perform query. this will set m_formatSupported to
e4db172a 540 true if m_targetRequested is supported.
9691c806
RR
541 also, we have to wait for the "answer" from the
542 clipboard owner which is an asynchronous process.
e4db172a 543 therefore we set m_waiting = true here and wait
9691c806 544 until the callback "targets_selection_received"
de6185e2 545 sets it to false */
9691c806 546
e4db172a 547 m_waiting = true;
9691c806
RR
548
549#if 0
550 gtk_selection_convert( m_targetsWidget,
551 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
552 : g_clipboardAtom,
553 g_targetsAtom,
554 (guint32) GDK_CURRENT_TIME );
555
556 while (m_waiting) gtk_main_iteration();
557#endif
558
559 if (!m_formatSupported) continue;
560
561 /* store pointer to data object to be filled up by callbacks */
562 m_receivedData = &data;
563
564 /* store requested format to be asked for by callbacks */
565 m_targetRequested = format;
566
e4db172a 567 wxCHECK_MSG( m_targetRequested, false, wxT("invalid clipboard format") );
9691c806
RR
568
569 /* start query */
e4db172a 570 m_formatSupported = false;
9691c806
RR
571
572 /* ask for clipboard contents. this will set
e4db172a 573 m_formatSupported to true if m_targetRequested
9691c806
RR
574 is supported.
575 also, we have to wait for the "answer" from the
576 clipboard owner which is an asynchronous process.
e4db172a 577 therefore we set m_waiting = true here and wait
9691c806 578 until the callback "targets_selection_received"
e4db172a 579 sets it to false */
9691c806 580
e4db172a 581 m_waiting = true;
9691c806
RR
582
583 wxLogTrace( TRACE_CLIPBOARD,
584 wxT("wxClipboard::GetData: format found, start convert") );
585
586#if 0
587 gtk_selection_convert( m_clipboardWidget,
588 m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
589 : g_clipboardAtom,
590 m_targetRequested,
591 (guint32) GDK_CURRENT_TIME );
592
593 while (m_waiting) gtk_main_iteration();
594#endif
595
596 /* this is a true error as we checked for the presence of such data before */
de6185e2 597 wxCHECK_MSG( m_formatSupported, false, wxT("error retrieving data from clipboard") );
9691c806
RR
598
599 /* return success */
600 delete[] array;
e4db172a 601 return true;
83df96d6 602 }
9691c806
RR
603
604 wxLogTrace( TRACE_CLIPBOARD,
605 wxT("wxClipboard::GetData: format not found") );
606
607 /* return failure */
608 delete[] array;
e4db172a 609 return false;
83df96d6 610}
9691c806 611
83df96d6 612#endif
9691c806 613 // wxUSE_CLIPBOARD