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