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 //-----------------------------------------------------------------------------
28 wxClipboard
*wxTheClipboard
= (wxClipboard
*) NULL
;
30 GdkAtom g_clipboardAtom
= 0;
31 GdkAtom g_targetsAtom
= 0;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 /* The contents of a selection are returned in a GtkSelectionData
38 structure. selection/target identify the request.
39 type specifies the type of the return; if length < 0, and
40 the data should be ignored. This structure has object semantics -
41 no fields should be modified directly, they should not be created
42 directly, and pointers to them should not be stored beyond the duration of
43 a callback. (If the last is changed, we'll need to add reference
46 struct _GtkSelectionData
58 //-----------------------------------------------------------------------------
59 // "selection_received" for targets
60 //-----------------------------------------------------------------------------
63 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
64 GtkSelectionData
*selection_data
,
65 #if (GTK_MINOR_VERSION > 0)
66 guint32
WXUNUSED(time
),
68 wxClipboard
*clipboard
)
72 clipboard
->m_waiting
= FALSE
;
76 if (selection_data
->length
<= 0)
78 clipboard
->m_waiting
= FALSE
;
82 /* make sure we got the data in the correct form */
83 if (selection_data
->type
!= GDK_SELECTION_TYPE_ATOM
)
85 clipboard
->m_waiting
= FALSE
;
89 // the atoms we received, holding a list of targets (= formats)
90 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
92 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
95 char *name = gdk_atom_name (atoms[i]);
96 if (name) printf( "Format available: %s.\n", name );
99 if (atoms
[i
] == clipboard
->m_targetRequested
)
101 clipboard
->m_waiting
= FALSE
;
102 clipboard
->m_formatSupported
= TRUE
;
107 clipboard
->m_waiting
= FALSE
;
111 //-----------------------------------------------------------------------------
112 // "selection_received" for the actual data
113 //-----------------------------------------------------------------------------
116 selection_received( GtkWidget
*WXUNUSED(widget
),
117 GtkSelectionData
*selection_data
,
118 #if (GTK_MINOR_VERSION > 0)
119 guint32
WXUNUSED(time
),
121 wxClipboard
*clipboard
)
125 clipboard
->m_waiting
= FALSE
;
129 wxDataObject
*data_object
= clipboard
->m_receivedData
;
133 clipboard
->m_waiting
= FALSE
;
137 if (selection_data
->length
<= 0)
139 clipboard
->m_waiting
= FALSE
;
143 /* make sure we got the data in the correct format */
144 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
146 clipboard
->m_waiting
= FALSE
;
150 /* make sure we got the data in the correct form (selection type).
151 if so, copy data to target object */
153 switch (data_object
->GetFormat().GetType())
157 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
159 clipboard
->m_waiting
= FALSE
;
163 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
165 wxString text
= (const char*) selection_data
->data
;
167 text_object
->SetText( text
);
174 if (selection_data
->type
!= GDK_SELECTION_TYPE_BITMAP
)
176 clipboard
->m_waiting
= FALSE
;
185 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
187 clipboard
->m_waiting
= FALSE
;
191 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
193 private_object
->SetData( (const char*) selection_data
->data
, (size_t) selection_data
->length
);
200 clipboard
->m_waiting
= FALSE
;
205 wxTheClipboard
->m_formatSupported
= TRUE
;
206 clipboard
->m_waiting
= FALSE
;
209 //-----------------------------------------------------------------------------
211 //-----------------------------------------------------------------------------
214 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
216 if (!wxTheClipboard
) return TRUE
;
218 if (event
->selection
== GDK_SELECTION_PRIMARY
)
220 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
223 if (event
->selection
== g_clipboardAtom
)
225 wxTheClipboard
->m_ownsClipboard
= FALSE
;
232 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
233 (!wxTheClipboard
->m_ownsClipboard
))
235 /* the clipboard is no longer in our hands. we can the clipboard data. */
237 if (wxTheClipboard
->m_dataBroker
)
239 delete wxTheClipboard
->m_dataBroker
;
240 wxTheClipboard
->m_dataBroker
= (wxDataBroker
*) NULL
;
247 //-----------------------------------------------------------------------------
248 // selection handler for supplying data
249 //-----------------------------------------------------------------------------
252 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
254 if (!wxTheClipboard
) return;
256 if (!wxTheClipboard
->m_dataBroker
) return;
258 wxNode
*node
= wxTheClipboard
->m_dataBroker
->m_dataObjects
.First();
262 wxDataObject
*data_object
= (wxDataObject
*)node
->Data();
264 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
270 switch (data_object
->GetFormat().GetType())
274 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
276 wxString text
= text_object
->GetText();
278 char *s
= WXSTRINGCAST text
;
279 int len
= (int) text
.Length();
281 gtk_selection_data_set(
283 GDK_SELECTION_TYPE_STRING
,
293 // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object;
295 // how do we do that ?
302 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
304 if (private_object
->GetSize() == 0) return;
306 gtk_selection_data_set(
308 GDK_SELECTION_TYPE_STRING
,
310 (unsigned char*) private_object
->GetData(),
311 (int) private_object
->GetSize() );
322 //-----------------------------------------------------------------------------
324 //-----------------------------------------------------------------------------
326 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
328 wxClipboard::wxClipboard()
332 m_ownsClipboard
= FALSE
;
333 m_ownsPrimarySelection
= FALSE
;
335 m_dataBroker
= (wxDataBroker
*) NULL
;
337 m_receivedData
= (wxDataObject
*) NULL
;
339 /* we use m_targetsWidget to query what formats are available */
341 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
342 gtk_widget_realize( m_targetsWidget
);
344 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
345 "selection_received",
346 GTK_SIGNAL_FUNC( targets_selection_received
),
349 /* we use m_clipboardWidget to get and to offer data */
351 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
352 gtk_widget_realize( m_clipboardWidget
);
354 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
355 "selection_received",
356 GTK_SIGNAL_FUNC( selection_received
),
359 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
360 "selection_clear_event",
361 GTK_SIGNAL_FUNC( selection_clear_clip
),
364 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
365 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
367 m_formatSupported
= FALSE
;
368 m_targetRequested
= 0;
371 wxClipboard::~wxClipboard()
375 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
376 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
379 void wxClipboard::Clear()
383 /* As we have data we also own the clipboard. Once we no longer own
384 it, clear_selection is called which will set m_data to zero */
386 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
388 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
391 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
393 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
399 m_dataBroker
= (wxDataBroker
*) NULL
;
403 m_targetRequested
= 0;
405 m_formatSupported
= FALSE
;
408 bool wxClipboard::Open()
410 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
417 bool wxClipboard::SetData( wxDataObject
*data
)
419 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
421 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
425 return AddData( data
);
428 bool wxClipboard::AddData( wxDataObject
*data
)
430 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
432 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
434 /* if clipboard has been cleared before, create new data broker */
436 if (!m_dataBroker
) m_dataBroker
= new wxDataBroker();
438 /* add new data to list of offered data objects */
440 m_dataBroker
->Add( data
);
442 /* get native format id of new data object */
444 GdkAtom format
= data
->GetFormat().GetAtom();
446 wxCHECK_MSG( format
, FALSE
, "data has invalid format" );
448 /* This should happen automatically, but to be on the safe side */
450 m_ownsClipboard
= FALSE
;
451 m_ownsPrimarySelection
= FALSE
;
453 /* Add handlers if someone requests data */
456 #if (GTK_MINOR_VERSION > 0)
458 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
459 GDK_SELECTION_PRIMARY
,
461 0 ); /* what is info ? */
463 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
466 0 ); /* what is info ? */
468 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
470 GTK_SIGNAL_FUNC(selection_handler
),
475 gtk_selection_add_handler( m_clipboardWidget
,
481 gtk_selection_add_handler( m_clipboardWidget
,
482 GDK_SELECTION_PRIMARY
,
488 /* Tell the world we offer clipboard data */
490 if (!gtk_selection_owner_set( m_clipboardWidget
,
496 m_ownsClipboard
= TRUE
;
498 if (!gtk_selection_owner_set( m_clipboardWidget
,
499 GDK_SELECTION_PRIMARY
,
504 m_ownsPrimarySelection
= TRUE
;
509 void wxClipboard::Close()
511 wxCHECK_RET( m_open
, "clipboard not open" );
516 bool wxClipboard::IsSupported( wxDataFormat format
)
518 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
520 /* store requested format to be asked for by callbacks */
522 m_targetRequested
= format
.GetAtom();
524 wxCHECK_MSG( m_targetRequested
, FALSE
, "invalid clipboard format" );
526 m_formatSupported
= FALSE
;
528 /* perform query. this will set m_formatSupported to
529 TRUE if m_targetRequested is supported.
530 alsom we have to wait for the "answer" from the
531 clipboard owner which is an asynchronous process.
532 therefore we set m_waiting = TRUE here and wait
533 until the callback "targets_selection_received"
538 gtk_selection_convert( m_targetsWidget
,
543 while (m_waiting
) gtk_main_iteration();
545 if (!m_formatSupported
) return FALSE
;
550 bool wxClipboard::GetData( wxDataObject
*data
)
552 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
554 /* is data supported by clipboard ? */
556 if (!IsSupported( data
->GetFormat() )) return FALSE
;
558 /* store pointer to data object to be filled up by callbacks */
560 m_receivedData
= data
;
562 /* store requested format to be asked for by callbacks */
564 m_targetRequested
= data
->GetFormat().GetAtom();
566 wxCHECK_MSG( m_targetRequested
, FALSE
, "invalid clipboard format" );
570 m_formatSupported
= FALSE
;
572 /* ask for clipboard contents. this will set
573 m_formatSupported to TRUE if m_targetRequested
575 also, we have to wait for the "answer" from the
576 clipboard owner which is an asynchronous process.
577 therefore we set m_waiting = TRUE here and wait
578 until the callback "targets_selection_received"
583 gtk_selection_convert( m_clipboardWidget
,
588 while (m_waiting
) gtk_main_iteration();
590 /* this is a true error as we checked for the presence of such data before */
592 wxCHECK_MSG( m_formatSupported
, FALSE
, "error retrieving data from clipboard" );
597 //-----------------------------------------------------------------------------
599 //-----------------------------------------------------------------------------
601 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
603 bool wxClipboardModule::OnInit()
605 wxTheClipboard
= new wxClipboard();
610 void wxClipboardModule::OnExit()
612 if (wxTheClipboard
) delete wxTheClipboard
;
613 wxTheClipboard
= (wxClipboard
*) NULL
;