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"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 wxClipboard
*wxTheClipboard
= (wxClipboard
*) NULL
;
28 GdkAtom g_textAtom
= 0;
29 GdkAtom g_clipboardAtom
= 0;
30 GdkAtom g_targetsAtom
= 0;
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 /* The contents of a selection are returned in a GtkSelectionData
37 structure. selection/target identify the request.
38 type specifies the type of the return; if length < 0, and
39 the data should be ignored. This structure has object semantics -
40 no fields should be modified directly, they should not be created
41 directly, and pointers to them should not be stored beyond the duration of
42 a callback. (If the last is changed, we'll need to add reference
45 struct _GtkSelectionData
57 //-----------------------------------------------------------------------------
58 // "selection_received" for targets
59 //-----------------------------------------------------------------------------
62 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
63 GtkSelectionData
*selection_data
,
64 wxClipboard
*clipboard
)
66 if (!wxTheClipboard
) return;
68 if (selection_data
->length
<= 0) return;
70 // make sure we got the data in the correct form
71 if (selection_data
->type
!= GDK_SELECTION_TYPE_ATOM
) return;
73 // the atoms we received, holding a list of targets (= formats)
74 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
76 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
78 if (atoms
[i
] == clipboard
->m_targetRequested
)
80 clipboard
->m_formatSupported
= TRUE
;
88 //-----------------------------------------------------------------------------
89 // "selection_received" for the actual data
90 //-----------------------------------------------------------------------------
93 selection_received( GtkWidget
*WXUNUSED(widget
),
94 GtkSelectionData
*selection_data
,
95 wxClipboard
*clipboard
)
97 if (!wxTheClipboard
) return;
99 wxDataObject
*data_object
= clipboard
->m_receivedData
;
101 if (!data_object
) return;
103 if (selection_data
->length
<= 0) return;
105 // make sure we got the data in the correct format
107 if (data_object
->GetFormat().GetAtom() != selection_data
->target
) return;
109 // make sure we got the data in the correct form (selection type).
110 // if so, copy data to target object
112 switch (data_object
->GetFormat().GetType())
116 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
) return;
118 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
120 wxString text
= (const char*) selection_data
->data
;
122 text_object
->SetText( text
);
129 if (selection_data
->type
!= GDK_SELECTION_TYPE_BITMAP
) return;
138 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
) return;
140 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
142 private_object
->SetData( (const char*) selection_data
->data
, (size_t) selection_data
->length
);
153 wxTheClipboard
->m_formatSupported
= TRUE
;
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
161 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
163 if (!wxTheClipboard
) return TRUE
;
165 if (event
->selection
== GDK_SELECTION_PRIMARY
)
167 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
170 if (event
->selection
== g_clipboardAtom
)
172 wxTheClipboard
->m_ownsClipboard
= FALSE
;
179 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
180 (!wxTheClipboard
->m_ownsClipboard
))
182 /* the clipboard is no longer in our hands. we can the clipboard data. */
184 if (wxTheClipboard
->m_dataBroker
)
186 delete wxTheClipboard
->m_dataBroker
;
187 wxTheClipboard
->m_dataBroker
= (wxDataBroker
*) NULL
;
194 //-----------------------------------------------------------------------------
195 // selection handler for supplying data
196 //-----------------------------------------------------------------------------
199 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
201 if (!wxTheClipboard
) return;
203 if (!wxTheClipboard
->m_dataBroker
) return;
205 wxNode
*node
= wxTheClipboard
->m_dataBroker
->m_dataObjects
.First();
209 wxDataObject
*data_object
= (wxDataObject
*)node
->Data();
211 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
217 switch (data_object
->GetFormat().GetType())
221 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
223 wxString text
= text_object
->GetText();
225 char *s
= WXSTRINGCAST text
;
226 int len
= (int) text
.Length();
228 gtk_selection_data_set(
230 GDK_SELECTION_TYPE_STRING
,
240 // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object;
242 // how do we do that ?
249 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
251 if (private_object
->GetSize() == 0) return;
253 gtk_selection_data_set(
255 GDK_SELECTION_TYPE_STRING
,
257 (unsigned char*) private_object
->GetData(),
258 (int) private_object
->GetSize() );
269 //-----------------------------------------------------------------------------
271 //-----------------------------------------------------------------------------
273 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
275 wxClipboard::wxClipboard()
279 m_ownsClipboard
= FALSE
;
280 m_ownsPrimarySelection
= FALSE
;
282 m_dataBroker
= (wxDataBroker
*) NULL
;
284 m_receivedData
= (wxDataObject
*) NULL
;
286 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
287 gtk_widget_realize( m_clipboardWidget
);
289 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
290 "selection_clear_event",
291 GTK_SIGNAL_FUNC( selection_clear_clip
),
294 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
295 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "TEXT", FALSE
);
296 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
298 m_formatSupported
= FALSE
;
299 m_targetRequested
= 0;
302 wxClipboard::~wxClipboard()
306 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
309 void wxClipboard::Clear()
313 /* As we have data we also own the clipboard. Once we no longer own
314 it, clear_selection is called which will set m_data to zero */
316 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
318 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
321 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
323 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
329 m_dataBroker
= (wxDataBroker
*) NULL
;
333 m_targetRequested
= 0;
335 m_formatSupported
= FALSE
;
338 bool wxClipboard::Open()
340 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
347 bool wxClipboard::SetData( wxDataBroker
*data
)
349 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
355 if (!m_dataBroker
) return FALSE
;
357 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
359 wxNode
*node
= m_dataBroker
->m_dataObjects
.First();
362 wxDataObject
*dobj
= (wxDataObject
*)node
->Data();
364 GdkAtom format
= dobj
->GetFormat().GetAtom();
366 if (format
!= (GdkAtom
) 0)
368 /* This should happen automatically */
370 m_ownsClipboard
= FALSE
;
371 m_ownsPrimarySelection
= FALSE
;
373 /* Add handlers if someone requests data */
375 gtk_selection_add_handler( m_clipboardWidget
,
381 gtk_selection_add_handler( m_clipboardWidget
,
382 GDK_SELECTION_PRIMARY
,
387 /* Tell the world we offer clipboard data */
389 if (!gtk_selection_owner_set( m_clipboardWidget
,
395 m_ownsClipboard
= TRUE
;
397 if (!gtk_selection_owner_set( m_clipboardWidget
,
398 GDK_SELECTION_PRIMARY
,
403 m_ownsPrimarySelection
= TRUE
;
412 void wxClipboard::Close()
414 wxCHECK_RET( m_open
, "clipboard not open" );
419 bool wxClipboard::GetData( wxDataObject
*data
)
421 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
423 m_receivedData
= data
;
425 wxCHECK_MSG( m_receivedData
, FALSE
, "invalid data object" );
427 /* STEP ONE: check if there is such data in the clipboard */
429 m_targetRequested
= data
->GetFormat().GetAtom();
431 wxCHECK_MSG( m_targetRequested
, FALSE
, "invalid clipboard format" );
433 if (m_targetRequested
== 0) return FALSE
;
435 /* add handler for target (= format) query */
437 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
438 "selection_received",
439 GTK_SIGNAL_FUNC( targets_selection_received
),
442 m_formatSupported
= FALSE
;
444 /* perform query. this will set m_formatSupported to
445 * TRUE if m_targetRequested is supported */
447 gtk_selection_convert( m_clipboardWidget
,
452 gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget
),
453 GTK_SIGNAL_FUNC( targets_selection_received
),
456 if (!m_formatSupported
) return FALSE
;
458 /* STEP TWO: get the data from the clipboard */
460 m_formatSupported
= FALSE
;
462 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
463 "selection_received",
464 GTK_SIGNAL_FUNC( selection_received
),
467 gtk_selection_convert( m_clipboardWidget
,
472 gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget
),
473 GTK_SIGNAL_FUNC( selection_received
),
476 /* this is a true error as we checked for the presence of such data before */
478 wxCHECK_MSG( m_formatSupported
, FALSE
, "error retrieving data from clipboard" );
483 //-----------------------------------------------------------------------------
485 //-----------------------------------------------------------------------------
487 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
489 bool wxClipboardModule::OnInit()
491 wxTheClipboard
= new wxClipboard();
496 void wxClipboardModule::OnExit()
498 if (wxTheClipboard
) delete wxTheClipboard
;
499 wxTheClipboard
= (wxClipboard
*) NULL
;