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