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