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