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 char *name
= gdk_atom_name (atoms
[i
]);
79 if (name
) printf( "Format available: %s.\n", name
);
81 if (atoms
[i
] == clipboard
->m_targetRequested
)
83 clipboard
->m_formatSupported
= TRUE
;
91 //-----------------------------------------------------------------------------
92 // "selection_received" for the actual data
93 //-----------------------------------------------------------------------------
96 selection_received( GtkWidget
*WXUNUSED(widget
),
97 GtkSelectionData
*selection_data
,
98 wxClipboard
*clipboard
)
100 if (!wxTheClipboard
) return;
102 wxDataObject
*data_object
= clipboard
->m_receivedData
;
104 if (!data_object
) return;
106 if (selection_data
->length
<= 0) return;
108 // make sure we got the data in the correct format
110 if (data_object
->GetFormat().GetAtom() != selection_data
->target
) return;
112 // make sure we got the data in the correct form (selection type).
113 // if so, copy data to target object
115 switch (data_object
->GetFormat().GetType())
119 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
) return;
121 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
123 wxString text
= (const char*) selection_data
->data
;
125 text_object
->SetText( text
);
132 if (selection_data
->type
!= GDK_SELECTION_TYPE_BITMAP
) return;
141 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
) return;
143 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
145 private_object
->SetData( (const char*) selection_data
->data
, (size_t) selection_data
->length
);
156 wxTheClipboard
->m_formatSupported
= TRUE
;
159 //-----------------------------------------------------------------------------
161 //-----------------------------------------------------------------------------
164 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
166 if (!wxTheClipboard
) return TRUE
;
168 if (event
->selection
== GDK_SELECTION_PRIMARY
)
170 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
173 if (event
->selection
== g_clipboardAtom
)
175 wxTheClipboard
->m_ownsClipboard
= FALSE
;
182 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
183 (!wxTheClipboard
->m_ownsClipboard
))
185 /* the clipboard is no longer in our hands. we can the clipboard data. */
187 if (wxTheClipboard
->m_dataBroker
)
189 delete wxTheClipboard
->m_dataBroker
;
190 wxTheClipboard
->m_dataBroker
= (wxDataBroker
*) NULL
;
197 //-----------------------------------------------------------------------------
198 // selection handler for supplying data
199 //-----------------------------------------------------------------------------
202 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
204 if (!wxTheClipboard
) return;
206 if (!wxTheClipboard
->m_dataBroker
) return;
208 wxNode
*node
= wxTheClipboard
->m_dataBroker
->m_dataObjects
.First();
212 wxDataObject
*data_object
= (wxDataObject
*)node
->Data();
214 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
220 switch (data_object
->GetFormat().GetType())
224 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
226 wxString text
= text_object
->GetText();
228 char *s
= WXSTRINGCAST text
;
229 int len
= (int) text
.Length();
231 gtk_selection_data_set(
233 GDK_SELECTION_TYPE_STRING
,
243 // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object;
245 // how do we do that ?
252 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
254 if (private_object
->GetSize() == 0) return;
256 gtk_selection_data_set(
258 GDK_SELECTION_TYPE_STRING
,
260 (unsigned char*) private_object
->GetData(),
261 (int) private_object
->GetSize() );
272 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
276 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
278 wxClipboard::wxClipboard()
282 m_ownsClipboard
= FALSE
;
283 m_ownsPrimarySelection
= FALSE
;
285 m_dataBroker
= (wxDataBroker
*) NULL
;
287 m_receivedData
= (wxDataObject
*) NULL
;
289 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
290 gtk_widget_realize( m_clipboardWidget
);
292 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
293 "selection_clear_event",
294 GTK_SIGNAL_FUNC( selection_clear_clip
),
297 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
298 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "TEXT", FALSE
);
299 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
301 m_formatSupported
= FALSE
;
302 m_targetRequested
= 0;
305 wxClipboard::~wxClipboard()
309 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
312 void wxClipboard::Clear()
316 /* As we have data we also own the clipboard. Once we no longer own
317 it, clear_selection is called which will set m_data to zero */
319 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
321 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
324 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
326 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
332 m_dataBroker
= (wxDataBroker
*) NULL
;
336 m_targetRequested
= 0;
338 m_formatSupported
= FALSE
;
341 bool wxClipboard::Open()
343 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
350 bool wxClipboard::SetData( wxDataObject
*data
)
352 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
354 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
358 return AddData( data
);
361 bool wxClipboard::AddData( wxDataObject
*data
)
363 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
365 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
367 /* if clipboard has been cleared before, create new data broker */
369 if (!m_dataBroker
) m_dataBroker
= new wxDataBroker();
371 /* add new data to list of offered data objects */
373 m_dataBroker
->Add( data
);
375 /* get native format id of new data object */
377 GdkAtom format
= data
->GetFormat().GetAtom();
379 wxCHECK_MSG( format
, FALSE
, "data has invalid format" );
381 /* This should happen automatically, but to be on the safe side */
383 m_ownsClipboard
= FALSE
;
384 m_ownsPrimarySelection
= FALSE
;
386 /* Add handlers if someone requests data */
389 #if (GTK_MINOR_VERSION > 0)
391 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
392 GDK_SELECTION_PRIMARY
,
394 0 ); /* what is info ? */
396 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
399 0 ); /* what is info ? */
401 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
403 GTK_SIGNAL_FUNC(selection_handler
),
408 gtk_selection_add_handler( m_clipboardWidget
,
414 gtk_selection_add_handler( m_clipboardWidget
,
415 GDK_SELECTION_PRIMARY
,
421 /* Tell the world we offer clipboard data */
423 if (!gtk_selection_owner_set( m_clipboardWidget
,
429 m_ownsClipboard
= TRUE
;
431 if (!gtk_selection_owner_set( m_clipboardWidget
,
432 GDK_SELECTION_PRIMARY
,
437 m_ownsPrimarySelection
= TRUE
;
442 void wxClipboard::Close()
444 wxCHECK_RET( m_open
, "clipboard not open" );
449 bool wxClipboard::IsSupported( wxDataFormat format
)
451 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
453 /* store requested format to be asked for by callbacks */
455 m_targetRequested
= format
.GetAtom();
457 wxCHECK_MSG( m_targetRequested
, FALSE
, "invalid clipboard format" );
459 /* add handler for target (= format) query */
461 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
462 "selection_received",
463 GTK_SIGNAL_FUNC( targets_selection_received
),
466 m_formatSupported
= FALSE
;
468 /* perform query. this will set m_formatSupported to
469 * TRUE if m_targetRequested is supported */
471 gtk_selection_convert( m_clipboardWidget
,
476 gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget
),
477 GTK_SIGNAL_FUNC( targets_selection_received
),
480 if (!m_formatSupported
) return FALSE
;
485 bool wxClipboard::GetData( wxDataObject
*data
)
487 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
489 /* is data supported by clipboard ? */
491 if (!IsSupported( data
->GetFormat() )) return FALSE
;
493 /* store pointer to data object to be filled up by callbacks */
495 m_receivedData
= data
;
497 /* store requested format to be asked for by callbacks */
499 m_targetRequested
= data
->GetFormat().GetAtom();
501 wxCHECK_MSG( m_targetRequested
, FALSE
, "invalid clipboard format" );
505 m_formatSupported
= FALSE
;
507 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
508 "selection_received",
509 GTK_SIGNAL_FUNC( selection_received
),
512 /* ask for clipboard contents */
514 gtk_selection_convert( m_clipboardWidget
,
519 gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget
),
520 GTK_SIGNAL_FUNC( selection_received
),
523 /* this is a true error as we checked for the presence of such data before */
525 wxCHECK_MSG( m_formatSupported
, FALSE
, "error retrieving data from clipboard" );
530 //-----------------------------------------------------------------------------
532 //-----------------------------------------------------------------------------
534 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
536 bool wxClipboardModule::OnInit()
538 wxTheClipboard
= new wxClipboard();
543 void wxClipboardModule::OnExit()
545 if (wxTheClipboard
) delete wxTheClipboard
;
546 wxTheClipboard
= (wxClipboard
*) NULL
;