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