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
++)
94 /* char *name = gdk_atom_name (atoms[i]);
95 if (name) printf( "Format available: %s.\n", name ); */
97 if (atoms
[i
] == clipboard
->m_targetRequested
)
99 clipboard
->m_waiting
= FALSE
;
100 clipboard
->m_formatSupported
= TRUE
;
105 clipboard
->m_waiting
= FALSE
;
109 //-----------------------------------------------------------------------------
110 // "selection_received" for the actual data
111 //-----------------------------------------------------------------------------
114 selection_received( GtkWidget
*WXUNUSED(widget
),
115 GtkSelectionData
*selection_data
,
116 #if (GTK_MINOR_VERSION > 0)
117 guint32
WXUNUSED(time
),
119 wxClipboard
*clipboard
)
123 clipboard
->m_waiting
= FALSE
;
127 wxDataObject
*data_object
= clipboard
->m_receivedData
;
131 clipboard
->m_waiting
= FALSE
;
135 if (selection_data
->length
<= 0)
137 clipboard
->m_waiting
= FALSE
;
141 /* make sure we got the data in the correct format */
142 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
144 clipboard
->m_waiting
= FALSE
;
148 /* make sure we got the data in the correct form (selection type).
149 if so, copy data to target object */
151 switch (data_object
->GetFormat().GetType())
155 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
157 clipboard
->m_waiting
= FALSE
;
161 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
163 wxString text
= (const char*) selection_data
->data
;
165 text_object
->SetText( text
);
172 if (selection_data
->type
!= GDK_SELECTION_TYPE_BITMAP
)
174 clipboard
->m_waiting
= FALSE
;
183 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
185 clipboard
->m_waiting
= FALSE
;
189 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
191 private_object
->SetData( (const char*) selection_data
->data
, (size_t) selection_data
->length
);
198 clipboard
->m_waiting
= FALSE
;
203 wxTheClipboard
->m_formatSupported
= TRUE
;
204 clipboard
->m_waiting
= FALSE
;
207 //-----------------------------------------------------------------------------
209 //-----------------------------------------------------------------------------
212 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
214 if (!wxTheClipboard
) return TRUE
;
216 if (event
->selection
== GDK_SELECTION_PRIMARY
)
218 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
221 if (event
->selection
== g_clipboardAtom
)
223 wxTheClipboard
->m_ownsClipboard
= FALSE
;
230 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
231 (!wxTheClipboard
->m_ownsClipboard
))
233 /* the clipboard is no longer in our hands. we can the clipboard data. */
235 if (wxTheClipboard
->m_dataBroker
)
237 delete wxTheClipboard
->m_dataBroker
;
238 wxTheClipboard
->m_dataBroker
= (wxDataBroker
*) NULL
;
245 //-----------------------------------------------------------------------------
246 // selection handler for supplying data
247 //-----------------------------------------------------------------------------
250 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
252 if (!wxTheClipboard
) return;
254 if (!wxTheClipboard
->m_dataBroker
) return;
256 wxNode
*node
= wxTheClipboard
->m_dataBroker
->m_dataObjects
.First();
260 wxDataObject
*data_object
= (wxDataObject
*)node
->Data();
262 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
268 switch (data_object
->GetFormat().GetType())
272 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
274 wxString text
= text_object
->GetText();
276 char *s
= WXSTRINGCAST text
;
277 int len
= (int) text
.Length();
279 gtk_selection_data_set(
281 GDK_SELECTION_TYPE_STRING
,
291 // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object;
293 // how do we do that ?
300 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
302 if (private_object
->GetSize() == 0) return;
304 gtk_selection_data_set(
306 GDK_SELECTION_TYPE_STRING
,
308 (unsigned char*) private_object
->GetData(),
309 (int) private_object
->GetSize() );
320 //-----------------------------------------------------------------------------
322 //-----------------------------------------------------------------------------
324 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
326 wxClipboard::wxClipboard()
330 m_ownsClipboard
= FALSE
;
331 m_ownsPrimarySelection
= FALSE
;
333 m_dataBroker
= (wxDataBroker
*) NULL
;
335 m_receivedData
= (wxDataObject
*) NULL
;
337 /* we use m_targetsWidget to query what formats are available */
339 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
340 gtk_widget_realize( m_targetsWidget
);
342 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
343 "selection_received",
344 GTK_SIGNAL_FUNC( targets_selection_received
),
347 /* we use m_clipboardWidget to get and to offer data */
349 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
350 gtk_widget_realize( m_clipboardWidget
);
352 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
353 "selection_received",
354 GTK_SIGNAL_FUNC( selection_received
),
357 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
358 "selection_clear_event",
359 GTK_SIGNAL_FUNC( selection_clear_clip
),
362 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
363 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
365 m_formatSupported
= FALSE
;
366 m_targetRequested
= 0;
369 wxClipboard::~wxClipboard()
373 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
374 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
377 void wxClipboard::Clear()
381 /* As we have data we also own the clipboard. Once we no longer own
382 it, clear_selection is called which will set m_data to zero */
384 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
386 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
389 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
391 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
397 m_dataBroker
= (wxDataBroker
*) NULL
;
401 m_targetRequested
= 0;
403 m_formatSupported
= FALSE
;
406 bool wxClipboard::Open()
408 wxCHECK_MSG( !m_open
, FALSE
, "clipboard already open" );
415 bool wxClipboard::SetData( wxDataObject
*data
)
417 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
419 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
423 return AddData( data
);
426 bool wxClipboard::AddData( wxDataObject
*data
)
428 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
430 wxCHECK_MSG( data
, FALSE
, "data is invalid" );
432 /* if clipboard has been cleared before, create new data broker */
434 if (!m_dataBroker
) m_dataBroker
= new wxDataBroker();
436 /* add new data to list of offered data objects */
438 m_dataBroker
->Add( data
);
440 /* get native format id of new data object */
442 GdkAtom format
= data
->GetFormat().GetAtom();
444 wxCHECK_MSG( format
, FALSE
, "data has invalid format" );
446 /* This should happen automatically, but to be on the safe side */
448 m_ownsClipboard
= FALSE
;
449 m_ownsPrimarySelection
= FALSE
;
451 /* Add handlers if someone requests data */
454 #if (GTK_MINOR_VERSION > 0)
456 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
457 GDK_SELECTION_PRIMARY
,
459 0 ); /* what is info ? */
461 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
464 0 ); /* what is info ? */
466 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
468 GTK_SIGNAL_FUNC(selection_handler
),
473 gtk_selection_add_handler( m_clipboardWidget
,
479 gtk_selection_add_handler( m_clipboardWidget
,
480 GDK_SELECTION_PRIMARY
,
486 /* Tell the world we offer clipboard data */
488 if (!gtk_selection_owner_set( m_clipboardWidget
,
494 m_ownsClipboard
= TRUE
;
496 if (!gtk_selection_owner_set( m_clipboardWidget
,
497 GDK_SELECTION_PRIMARY
,
502 m_ownsPrimarySelection
= TRUE
;
507 void wxClipboard::Close()
509 wxCHECK_RET( m_open
, "clipboard not open" );
514 bool wxClipboard::IsSupported( wxDataFormat format
)
516 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
518 /* store requested format to be asked for by callbacks */
520 m_targetRequested
= format
.GetAtom();
522 wxCHECK_MSG( m_targetRequested
, FALSE
, "invalid clipboard format" );
524 m_formatSupported
= FALSE
;
526 /* perform query. this will set m_formatSupported to
527 TRUE if m_targetRequested is supported.
528 alsom we have to wait for the "answer" from the
529 clipboard owner which is an asynchronous process.
530 therefore we set m_waiting = TRUE here and wait
531 until the callback "targets_selection_received"
536 gtk_selection_convert( m_targetsWidget
,
541 while (m_waiting
) gtk_main_iteration();
543 if (!m_formatSupported
) return FALSE
;
548 bool wxClipboard::GetData( wxDataObject
*data
)
550 wxCHECK_MSG( m_open
, FALSE
, "clipboard not open" );
552 /* is data supported by clipboard ? */
554 if (!IsSupported( data
->GetFormat() )) return FALSE
;
556 /* store pointer to data object to be filled up by callbacks */
558 m_receivedData
= data
;
560 /* store requested format to be asked for by callbacks */
562 m_targetRequested
= data
->GetFormat().GetAtom();
564 wxCHECK_MSG( m_targetRequested
, FALSE
, "invalid clipboard format" );
568 m_formatSupported
= FALSE
;
570 /* ask for clipboard contents. this will set
571 m_formatSupported to TRUE if m_targetRequested
573 also, we have to wait for the "answer" from the
574 clipboard owner which is an asynchronous process.
575 therefore we set m_waiting = TRUE here and wait
576 until the callback "targets_selection_received"
581 gtk_selection_convert( m_clipboardWidget
,
586 while (m_waiting
) gtk_main_iteration();
588 /* this is a true error as we checked for the presence of such data before */
590 wxCHECK_MSG( m_formatSupported
, FALSE
, "error retrieving data from clipboard" );
595 //-----------------------------------------------------------------------------
597 //-----------------------------------------------------------------------------
599 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
601 bool wxClipboardModule::OnInit()
603 wxTheClipboard
= new wxClipboard();
608 void wxClipboardModule::OnExit()
610 if (wxTheClipboard
) delete wxTheClipboard
;
611 wxTheClipboard
= (wxClipboard
*) NULL
;