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