1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "clipbrd.h"
14 #include "wx/clipbrd.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
29 extern void wxapp_install_thread_wakeup();
30 extern void wxapp_uninstall_thread_wakeup();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 wxClipboard
*wxTheClipboard
= (wxClipboard
*) NULL
;
39 GdkAtom g_clipboardAtom
= 0;
40 GdkAtom g_targetsAtom
= 0;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 /* The contents of a selection are returned in a GtkSelectionData
47 structure. selection/target identify the request.
48 type specifies the type of the return; if length < 0, and
49 the data should be ignored. This structure has object semantics -
50 no fields should be modified directly, they should not be created
51 directly, and pointers to them should not be stored beyond the duration of
52 a callback. (If the last is changed, we'll need to add reference
55 struct _GtkSelectionData
67 //-----------------------------------------------------------------------------
68 // "selection_received" for targets
69 //-----------------------------------------------------------------------------
72 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
73 GtkSelectionData
*selection_data
,
74 #if (GTK_MINOR_VERSION > 0)
75 guint32
WXUNUSED(time
),
77 wxClipboard
*clipboard
)
81 clipboard
->m_waiting
= FALSE
;
85 if (selection_data
->length
<= 0)
87 clipboard
->m_waiting
= FALSE
;
91 /* make sure we got the data in the correct form */
92 if (selection_data
->type
!= GDK_SELECTION_TYPE_ATOM
)
94 clipboard
->m_waiting
= FALSE
;
98 // the atoms we received, holding a list of targets (= formats)
99 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
101 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
103 /* char *name = gdk_atom_name (atoms[i]);
104 if (name) printf( "Format available: %s.\n", name ); */
106 if (atoms
[i
] == clipboard
->m_targetRequested
)
108 clipboard
->m_waiting
= FALSE
;
109 clipboard
->m_formatSupported
= TRUE
;
114 clipboard
->m_waiting
= FALSE
;
118 //-----------------------------------------------------------------------------
119 // "selection_received" for the actual data
120 //-----------------------------------------------------------------------------
123 selection_received( GtkWidget
*WXUNUSED(widget
),
124 GtkSelectionData
*selection_data
,
125 #if (GTK_MINOR_VERSION > 0)
126 guint32
WXUNUSED(time
),
128 wxClipboard
*clipboard
)
132 clipboard
->m_waiting
= FALSE
;
136 wxDataObject
*data_object
= clipboard
->m_receivedData
;
140 clipboard
->m_waiting
= FALSE
;
144 if (selection_data
->length
<= 0)
146 clipboard
->m_waiting
= FALSE
;
150 /* make sure we got the data in the correct format */
151 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
153 clipboard
->m_waiting
= FALSE
;
157 /* make sure we got the data in the correct form (selection type).
158 if so, copy data to target object */
160 switch (data_object
->GetFormat().GetType())
164 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
166 clipboard
->m_waiting
= FALSE
;
170 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
172 wxString text
= (const char*) selection_data
->data
;
174 text_object
->SetText( text
);
181 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
183 clipboard
->m_waiting
= FALSE
;
187 wxBitmapDataObject
*bitmap_object
= (wxBitmapDataObject
*) data_object
;
189 bitmap_object
->SetPngData( (const char*) selection_data
->data
, (size_t) selection_data
->length
);
196 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
198 clipboard
->m_waiting
= FALSE
;
202 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
204 private_object
->SetData( (const char*) selection_data
->data
, (size_t) selection_data
->length
);
211 clipboard
->m_waiting
= FALSE
;
216 wxTheClipboard
->m_formatSupported
= TRUE
;
217 clipboard
->m_waiting
= FALSE
;
220 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
225 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
227 if (!wxTheClipboard
) return TRUE
;
229 if (event
->selection
== GDK_SELECTION_PRIMARY
)
231 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
234 if (event
->selection
== g_clipboardAtom
)
236 wxTheClipboard
->m_ownsClipboard
= FALSE
;
240 wxTheClipboard
->m_waiting
= FALSE
;
244 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
245 (!wxTheClipboard
->m_ownsClipboard
))
247 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
248 if (wxTheClipboard
->m_dataBroker
)
250 delete wxTheClipboard
->m_dataBroker
;
251 wxTheClipboard
->m_dataBroker
= (wxDataBroker
*) NULL
;
255 wxTheClipboard
->m_waiting
= FALSE
;
259 //-----------------------------------------------------------------------------
260 // selection handler for supplying data
261 //-----------------------------------------------------------------------------
264 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
266 if (!wxTheClipboard
) return;
268 if (!wxTheClipboard
->m_dataBroker
) return;
270 wxNode
*node
= wxTheClipboard
->m_dataBroker
->m_dataObjects
.First();
274 wxDataObject
*data_object
= (wxDataObject
*)node
->Data();
276 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
282 switch (data_object
->GetFormat().GetType())
286 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
288 wxString text
= text_object
->GetText();
291 const wxWX2MBbuf s
= text
.mbc_str();
293 #else // more efficient in non-Unicode
294 const char *s
= text
.c_str();
295 int len
= (int) text
.Length();
298 gtk_selection_data_set(
300 GDK_SELECTION_TYPE_STRING
,
302 (unsigned char*) (const char*) s
,
310 wxBitmapDataObject
*bitmap_object
= (wxBitmapDataObject
*) data_object
;
312 if (bitmap_object
->GetSize() == 0) return;
314 gtk_selection_data_set(
316 GDK_SELECTION_TYPE_STRING
,
318 (unsigned char*) bitmap_object
->GetData(),
319 (int) bitmap_object
->GetSize() );
326 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
328 if (private_object
->GetSize() == 0) return;
330 gtk_selection_data_set(
332 GDK_SELECTION_TYPE_STRING
,
334 (unsigned char*) private_object
->GetData(),
335 (int) private_object
->GetSize() );
346 //-----------------------------------------------------------------------------
348 //-----------------------------------------------------------------------------
350 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
352 wxClipboard::wxClipboard()
356 m_ownsClipboard
= FALSE
;
357 m_ownsPrimarySelection
= FALSE
;
359 m_dataBroker
= (wxDataBroker
*) NULL
;
361 m_receivedData
= (wxDataObject
*) NULL
;
363 /* we use m_targetsWidget to query what formats are available */
365 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
366 gtk_widget_realize( m_targetsWidget
);
368 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
369 "selection_received",
370 GTK_SIGNAL_FUNC( targets_selection_received
),
373 /* we use m_clipboardWidget to get and to offer data */
375 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
376 gtk_widget_realize( m_clipboardWidget
);
378 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
379 "selection_received",
380 GTK_SIGNAL_FUNC( selection_received
),
383 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
384 "selection_clear_event",
385 GTK_SIGNAL_FUNC( selection_clear_clip
),
388 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
389 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
391 m_formatSupported
= FALSE
;
392 m_targetRequested
= 0;
394 m_usePrimary
= FALSE
;
397 wxClipboard::~wxClipboard()
401 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
402 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
405 void wxClipboard::Clear()
410 /* disable GUI threads */
411 wxapp_uninstall_thread_wakeup();
414 /* As we have data we also own the clipboard. Once we no longer own
415 it, clear_selection is called which will set m_data to zero */
416 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
420 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
422 while (m_waiting
) gtk_main_iteration();
425 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
429 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
431 while (m_waiting
) gtk_main_iteration();
437 m_dataBroker
= (wxDataBroker
*) NULL
;
441 /* re-enable GUI threads */
442 wxapp_install_thread_wakeup();
446 m_targetRequested
= 0;
448 m_formatSupported
= FALSE
;
451 bool wxClipboard::Open()
453 wxCHECK_MSG( !m_open
, FALSE
, T("clipboard already open") );
460 bool wxClipboard::SetData( wxDataObject
*data
)
462 wxCHECK_MSG( m_open
, FALSE
, T("clipboard not open") );
464 wxCHECK_MSG( data
, FALSE
, T("data is invalid") );
468 return AddData( data
);
471 bool wxClipboard::AddData( wxDataObject
*data
)
473 wxCHECK_MSG( m_open
, FALSE
, T("clipboard not open") );
475 wxCHECK_MSG( data
, FALSE
, T("data is invalid") );
477 /* if clipboard has been cleared before, create new data broker */
478 if (!m_dataBroker
) m_dataBroker
= new wxDataBroker();
480 /* add new data to list of offered data objects */
481 m_dataBroker
->Add( data
);
483 /* get native format id of new data object */
484 GdkAtom format
= data
->GetFormat().GetAtom();
486 wxCHECK_MSG( format
, FALSE
, T("data has invalid format") );
488 /* This should happen automatically, but to be on the safe side */
489 m_ownsClipboard
= FALSE
;
490 m_ownsPrimarySelection
= FALSE
;
492 /* Add handlers if someone requests data */
494 #if (GTK_MINOR_VERSION > 0)
496 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
497 GDK_SELECTION_PRIMARY
,
499 0 ); /* what is info ? */
501 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
504 0 ); /* what is info ? */
506 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
508 GTK_SIGNAL_FUNC(selection_handler
),
513 gtk_selection_add_handler( m_clipboardWidget
,
519 gtk_selection_add_handler( m_clipboardWidget
,
520 GDK_SELECTION_PRIMARY
,
527 /* disable GUI threads */
528 wxapp_uninstall_thread_wakeup();
531 /* Tell the world we offer clipboard data */
532 if (!gtk_selection_owner_set( m_clipboardWidget
,
537 /* re-enable GUI threads */
538 wxapp_install_thread_wakeup();
542 m_ownsClipboard
= TRUE
;
544 if (!gtk_selection_owner_set( m_clipboardWidget
,
545 GDK_SELECTION_PRIMARY
,
549 /* re-enable GUI threads */
550 wxapp_install_thread_wakeup();
554 m_ownsPrimarySelection
= TRUE
;
557 /* re-enable GUI threads */
558 wxapp_install_thread_wakeup();
564 void wxClipboard::Close()
566 wxCHECK_RET( m_open
, T("clipboard not open") );
571 bool wxClipboard::IsSupported( wxDataFormat format
)
573 wxCHECK_MSG( m_open
, FALSE
, T("clipboard not open") );
575 /* store requested format to be asked for by callbacks */
577 m_targetRequested
= format
.GetAtom();
579 wxCHECK_MSG( m_targetRequested
, FALSE
, T("invalid clipboard format") );
581 m_formatSupported
= FALSE
;
583 /* perform query. this will set m_formatSupported to
584 TRUE if m_targetRequested is supported.
585 alsom we have to wait for the "answer" from the
586 clipboard owner which is an asynchronous process.
587 therefore we set m_waiting = TRUE here and wait
588 until the callback "targets_selection_received"
593 gtk_selection_convert( m_targetsWidget
,
594 m_usePrimary
?GDK_SELECTION_PRIMARY
:g_clipboardAtom
,
598 while (m_waiting
) gtk_main_iteration();
600 if (!m_formatSupported
) return FALSE
;
605 bool wxClipboard::GetData( wxDataObject
*data
)
607 wxCHECK_MSG( m_open
, FALSE
, T("clipboard not open") );
609 /* is data supported by clipboard ? */
611 if (!IsSupported( data
->GetFormat() )) return FALSE
;
613 /* store pointer to data object to be filled up by callbacks */
615 m_receivedData
= data
;
617 /* store requested format to be asked for by callbacks */
619 m_targetRequested
= data
->GetFormat().GetAtom();
621 wxCHECK_MSG( m_targetRequested
, FALSE
, T("invalid clipboard format") );
625 m_formatSupported
= FALSE
;
627 /* ask for clipboard contents. this will set
628 m_formatSupported to TRUE if m_targetRequested
630 also, we have to wait for the "answer" from the
631 clipboard owner which is an asynchronous process.
632 therefore we set m_waiting = TRUE here and wait
633 until the callback "targets_selection_received"
638 gtk_selection_convert( m_clipboardWidget
,
639 m_usePrimary
?GDK_SELECTION_PRIMARY
:g_clipboardAtom
,
643 while (m_waiting
) gtk_main_iteration();
645 /* this is a true error as we checked for the presence of such data before */
647 wxCHECK_MSG( m_formatSupported
, FALSE
, T("error retrieving data from clipboard") );
652 //-----------------------------------------------------------------------------
654 //-----------------------------------------------------------------------------
656 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
658 bool wxClipboardModule::OnInit()
660 wxTheClipboard
= new wxClipboard();
665 void wxClipboardModule::OnExit()
667 if (wxTheClipboard
) delete wxTheClipboard
;
668 wxTheClipboard
= (wxClipboard
*) NULL
;