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