]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/clipbrd.cpp
log messages given during program initialization are not discarded any more
[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 {
ca35e608
RR
94/* char *name = gdk_atom_name (atoms[i]);
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 {
227 return FALSE;
228 }
229
230 if ((!wxTheClipboard->m_ownsPrimarySelection) &&
231 (!wxTheClipboard->m_ownsClipboard))
232 {
0d2a2b60
RR
233 /* the clipboard is no longer in our hands. we can the clipboard data. */
234
235 if (wxTheClipboard->m_dataBroker)
236 {
237 delete wxTheClipboard->m_dataBroker;
238 wxTheClipboard->m_dataBroker = (wxDataBroker*) NULL;
239 }
aeeb6a44 240 }
fd0eed64 241
8b53e5a2 242 return TRUE;
fd0eed64
RR
243}
244
245//-----------------------------------------------------------------------------
246// selection handler for supplying data
247//-----------------------------------------------------------------------------
248
249static void
250selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, gpointer WXUNUSED(data) )
251{
8b53e5a2 252 if (!wxTheClipboard) return;
fd0eed64 253
0d2a2b60
RR
254 if (!wxTheClipboard->m_dataBroker) return;
255
256 wxNode *node = wxTheClipboard->m_dataBroker->m_dataObjects.First();
8b53e5a2
RR
257
258 while (node)
259 {
260 wxDataObject *data_object = (wxDataObject *)node->Data();
261
0d2a2b60 262 if (data_object->GetFormat().GetAtom() != selection_data->target)
8b53e5a2
RR
263 {
264 node = node->Next();
265 break;
266 }
267
0d2a2b60 268 switch (data_object->GetFormat().GetType())
8b53e5a2
RR
269 {
270 case wxDF_TEXT:
271 {
272 wxTextDataObject *text_object = (wxTextDataObject*) data_object;
273
274 wxString text = text_object->GetText();
275
93c5dd39
OK
276#if wxUSE_UNICODE
277 const wxWX2MBbuf s = text.mbc_str();
278 int len = strlen(s);
279#else // more efficient in non-Unicode
280 const char *s = text.c_str();
8b53e5a2 281 int len = (int) text.Length();
93c5dd39 282#endif
8b53e5a2
RR
283
284 gtk_selection_data_set(
285 selection_data,
286 GDK_SELECTION_TYPE_STRING,
287 8*sizeof(gchar),
93c5dd39 288 (unsigned char*) (const char*) s,
8b53e5a2
RR
289 len );
290
291 break;
292 }
293
294 case wxDF_BITMAP:
295 {
296 // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object;
297
298 // how do we do that ?
299
300 break;
301 }
302
303 case wxDF_PRIVATE:
304 {
305 wxPrivateDataObject *private_object = (wxPrivateDataObject*) data_object;
306
0d2a2b60 307 if (private_object->GetSize() == 0) return;
8b53e5a2
RR
308
309 gtk_selection_data_set(
310 selection_data,
311 GDK_SELECTION_TYPE_STRING,
312 8*sizeof(gchar),
313 (unsigned char*) private_object->GetData(),
0d2a2b60 314 (int) private_object->GetSize() );
8b53e5a2
RR
315 }
316
317 default:
318 break;
319 }
320
321 node = node->Next();
322 }
fd0eed64 323}
dc86cb34
RR
324
325//-----------------------------------------------------------------------------
326// wxClipboard
327//-----------------------------------------------------------------------------
328
329IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
330
331wxClipboard::wxClipboard()
332{
8b53e5a2
RR
333 m_open = FALSE;
334
aeeb6a44
RR
335 m_ownsClipboard = FALSE;
336 m_ownsPrimarySelection = FALSE;
337
0d2a2b60 338 m_dataBroker = (wxDataBroker*) NULL;
fd0eed64 339
8b53e5a2 340 m_receivedData = (wxDataObject*) NULL;
99c67c77 341
034be888
RR
342 /* we use m_targetsWidget to query what formats are available */
343
344 m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
345 gtk_widget_realize( m_targetsWidget );
346
347 gtk_signal_connect( GTK_OBJECT(m_targetsWidget),
348 "selection_received",
349 GTK_SIGNAL_FUNC( targets_selection_received ),
350 (gpointer) this );
351
352 /* we use m_clipboardWidget to get and to offer data */
353
8b53e5a2
RR
354 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
355 gtk_widget_realize( m_clipboardWidget );
356
034be888
RR
357 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
358 "selection_received",
359 GTK_SIGNAL_FUNC( selection_received ),
360 (gpointer) this );
361
8b53e5a2
RR
362 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
363 "selection_clear_event",
aeeb6a44 364 GTK_SIGNAL_FUNC( selection_clear_clip ),
8b53e5a2 365 (gpointer) NULL );
fd0eed64 366
8b53e5a2 367 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
8b53e5a2
RR
368 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
369
370 m_formatSupported = FALSE;
371 m_targetRequested = 0;
dc86cb34
RR
372}
373
374wxClipboard::~wxClipboard()
b527aac5 375{
8b53e5a2 376 Clear();
b527aac5 377
8b53e5a2 378 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
034be888 379 if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
b527aac5
RR
380}
381
382void wxClipboard::Clear()
dc86cb34 383{
0d2a2b60 384 if (m_dataBroker)
8b53e5a2
RR
385 {
386 /* As we have data we also own the clipboard. Once we no longer own
387 it, clear_selection is called which will set m_data to zero */
fd0eed64 388
aeeb6a44 389 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
8b53e5a2
RR
390 {
391 gtk_selection_owner_set( (GtkWidget*) NULL, g_clipboardAtom, GDK_CURRENT_TIME );
392 }
db1b4961 393
aeeb6a44
RR
394 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY ) == m_clipboardWidget->window)
395 {
396 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME );
397 }
398
0d2a2b60
RR
399 if (m_dataBroker)
400 {
401 delete m_dataBroker;
402 m_dataBroker = (wxDataBroker*) NULL;
403 }
8b53e5a2 404 }
b527aac5 405
8b53e5a2 406 m_targetRequested = 0;
b527aac5 407
8b53e5a2
RR
408 m_formatSupported = FALSE;
409}
410
411bool wxClipboard::Open()
412{
93c5dd39 413 wxCHECK_MSG( !m_open, FALSE, _T("clipboard already open") );
b527aac5 414
8b53e5a2 415 m_open = TRUE;
b527aac5 416
8b53e5a2 417 return TRUE;
dc86cb34
RR
418}
419
75ce0581 420bool wxClipboard::SetData( wxDataObject *data )
dc86cb34 421{
93c5dd39 422 wxCHECK_MSG( m_open, FALSE, _T("clipboard not open") );
75ce0581 423
93c5dd39 424 wxCHECK_MSG( data, FALSE, _T("data is invalid") );
db1b4961 425
0d2a2b60 426 Clear();
75ce0581
RR
427
428 return AddData( data );
429}
430
431bool wxClipboard::AddData( wxDataObject *data )
432{
93c5dd39 433 wxCHECK_MSG( m_open, FALSE, _T("clipboard not open") );
0d2a2b60 434
93c5dd39 435 wxCHECK_MSG( data, FALSE, _T("data is invalid") );
2830bf19 436
75ce0581 437 /* if clipboard has been cleared before, create new data broker */
8b53e5a2 438
75ce0581 439 if (!m_dataBroker) m_dataBroker = new wxDataBroker();
8b53e5a2 440
75ce0581
RR
441 /* add new data to list of offered data objects */
442
443 m_dataBroker->Add( data );
444
445 /* get native format id of new data object */
446
447 GdkAtom format = data->GetFormat().GetAtom();
8b53e5a2 448
93c5dd39 449 wxCHECK_MSG( format, FALSE, _T("data has invalid format") );
75ce0581
RR
450
451 /* This should happen automatically, but to be on the safe side */
8b53e5a2 452
75ce0581
RR
453 m_ownsClipboard = FALSE;
454 m_ownsPrimarySelection = FALSE;
aeeb6a44 455
75ce0581 456 /* Add handlers if someone requests data */
8b53e5a2 457
d345e841
RR
458
459#if (GTK_MINOR_VERSION > 0)
460
461 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
462 GDK_SELECTION_PRIMARY,
463 format,
464 0 ); /* what is info ? */
465
466 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
467 g_clipboardAtom,
468 format,
469 0 ); /* what is info ? */
470
471 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
472 "selection_get",
473 GTK_SIGNAL_FUNC(selection_handler),
474 (gpointer) NULL );
475
476#else
477
75ce0581 478 gtk_selection_add_handler( m_clipboardWidget,
8b53e5a2 479 g_clipboardAtom,
0d2a2b60 480 format,
8b53e5a2 481 selection_handler,
0d2a2b60 482 (gpointer) NULL );
8b53e5a2 483
75ce0581 484 gtk_selection_add_handler( m_clipboardWidget,
aeeb6a44 485 GDK_SELECTION_PRIMARY,
0d2a2b60 486 format,
aeeb6a44 487 selection_handler,
0d2a2b60 488 (gpointer) NULL );
d345e841 489#endif
aeeb6a44 490
75ce0581 491 /* Tell the world we offer clipboard data */
8b53e5a2 492
75ce0581 493 if (!gtk_selection_owner_set( m_clipboardWidget,
8b53e5a2
RR
494 g_clipboardAtom,
495 GDK_CURRENT_TIME ))
75ce0581
RR
496 {
497 return FALSE;
498 }
499 m_ownsClipboard = TRUE;
aeeb6a44 500
75ce0581 501 if (!gtk_selection_owner_set( m_clipboardWidget,
aeeb6a44
RR
502 GDK_SELECTION_PRIMARY,
503 GDK_CURRENT_TIME ))
75ce0581
RR
504 {
505 return FALSE;
aeeb6a44 506 }
75ce0581
RR
507 m_ownsPrimarySelection = TRUE;
508
8b53e5a2
RR
509 return TRUE;
510}
db1b4961 511
8b53e5a2
RR
512void wxClipboard::Close()
513{
93c5dd39 514 wxCHECK_RET( m_open, _T("clipboard not open") );
8b53e5a2
RR
515
516 m_open = FALSE;
dc86cb34
RR
517}
518
5f699c22 519bool wxClipboard::IsSupported( wxDataFormat format )
b527aac5 520{
93c5dd39 521 wxCHECK_MSG( m_open, FALSE, _T("clipboard not open") );
0d2a2b60 522
75ce0581 523 /* store requested format to be asked for by callbacks */
0d2a2b60 524
5f699c22 525 m_targetRequested = format.GetAtom();
b527aac5 526
93c5dd39 527 wxCHECK_MSG( m_targetRequested, FALSE, _T("invalid clipboard format") );
0d2a2b60 528
8b53e5a2 529 m_formatSupported = FALSE;
b527aac5 530
0d2a2b60 531 /* perform query. this will set m_formatSupported to
034be888
RR
532 TRUE if m_targetRequested is supported.
533 alsom 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;
ca35e608 540
034be888 541 gtk_selection_convert( m_targetsWidget,
8b53e5a2
RR
542 g_clipboardAtom,
543 g_targetsAtom,
544 GDK_CURRENT_TIME );
ca35e608 545
034be888
RR
546 while (m_waiting) gtk_main_iteration();
547
8b53e5a2 548 if (!m_formatSupported) return FALSE;
75ce0581
RR
549
550 return TRUE;
551}
552
5f699c22 553bool wxClipboard::GetData( wxDataObject *data )
75ce0581 554{
93c5dd39 555 wxCHECK_MSG( m_open, FALSE, _T("clipboard not open") );
75ce0581
RR
556
557 /* is data supported by clipboard ? */
558
5f699c22 559 if (!IsSupported( data->GetFormat() )) return FALSE;
75ce0581
RR
560
561 /* store pointer to data object to be filled up by callbacks */
562
5f699c22 563 m_receivedData = data;
75ce0581
RR
564
565 /* store requested format to be asked for by callbacks */
566
5f699c22 567 m_targetRequested = data->GetFormat().GetAtom();
b527aac5 568
93c5dd39 569 wxCHECK_MSG( m_targetRequested, FALSE, _T("invalid clipboard format") );
75ce0581
RR
570
571 /* start query */
8b53e5a2 572
8b53e5a2 573 m_formatSupported = FALSE;
b527aac5 574
034be888
RR
575 /* ask for clipboard contents. this will set
576 m_formatSupported to TRUE if m_targetRequested
577 is supported.
578 also, we have to wait for the "answer" from the
579 clipboard owner which is an asynchronous process.
580 therefore we set m_waiting = TRUE here and wait
581 until the callback "targets_selection_received"
582 sets it to FALSE */
b527aac5 583
034be888 584 m_waiting = TRUE;
75ce0581 585
8b53e5a2
RR
586 gtk_selection_convert( m_clipboardWidget,
587 g_clipboardAtom,
588 m_targetRequested,
589 GDK_CURRENT_TIME );
b527aac5 590
034be888 591 while (m_waiting) gtk_main_iteration();
b527aac5 592
0d2a2b60
RR
593 /* this is a true error as we checked for the presence of such data before */
594
93c5dd39 595 wxCHECK_MSG( m_formatSupported, FALSE, _T("error retrieving data from clipboard") );
8b53e5a2
RR
596
597 return TRUE;
b527aac5
RR
598}
599
b527aac5
RR
600//-----------------------------------------------------------------------------
601// wxClipboardModule
602//-----------------------------------------------------------------------------
603
604IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
605
606bool wxClipboardModule::OnInit()
607{
8b53e5a2 608 wxTheClipboard = new wxClipboard();
b527aac5 609
8b53e5a2 610 return TRUE;
b527aac5
RR
611}
612
613void wxClipboardModule::OnExit()
dc86cb34 614{
8b53e5a2
RR
615 if (wxTheClipboard) delete wxTheClipboard;
616 wxTheClipboard = (wxClipboard*) NULL;
dc86cb34 617}
ac57418f
RR
618
619#endif
620
621 // wxUSE_CLIPBOARD
622