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
;
227 wxTheClipboard
->m_waiting
= FALSE
;
231 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
232 (!wxTheClipboard
->m_ownsClipboard
))
234 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
235 if (wxTheClipboard
->m_dataBroker
)
237 delete wxTheClipboard
->m_dataBroker
;
238 wxTheClipboard
->m_dataBroker
= (wxDataBroker
*) NULL
;
242 wxTheClipboard
->m_waiting
= FALSE
;
246 //-----------------------------------------------------------------------------
247 // selection handler for supplying data
248 //-----------------------------------------------------------------------------
251 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
253 if (!wxTheClipboard
) return;
255 if (!wxTheClipboard
->m_dataBroker
) return;
257 wxNode
*node
= wxTheClipboard
->m_dataBroker
->m_dataObjects
.First();
261 wxDataObject
*data_object
= (wxDataObject
*)node
->Data();
263 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
269 switch (data_object
->GetFormat().GetType())
273 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
275 wxString text
= text_object
->GetText();
278 const wxWX2MBbuf s
= text
.mbc_str();
280 #else // more efficient in non-Unicode
281 const char *s
= text
.c_str();
282 int len
= (int) text
.Length();
285 gtk_selection_data_set(
287 GDK_SELECTION_TYPE_STRING
,
289 (unsigned char*) (const char*) s
,
297 // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object;
299 // how do we do that ?
306 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
308 if (private_object
->GetSize() == 0) return;
310 gtk_selection_data_set(
312 GDK_SELECTION_TYPE_STRING
,
314 (unsigned char*) private_object
->GetData(),
315 (int) private_object
->GetSize() );
326 //-----------------------------------------------------------------------------
328 //-----------------------------------------------------------------------------
330 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
332 wxClipboard::wxClipboard()
336 m_ownsClipboard
= FALSE
;
337 m_ownsPrimarySelection
= FALSE
;
339 m_dataBroker
= (wxDataBroker
*) NULL
;
341 m_receivedData
= (wxDataObject
*) NULL
;
343 /* we use m_targetsWidget to query what formats are available */
345 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
346 gtk_widget_realize( m_targetsWidget
);
348 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
349 "selection_received",
350 GTK_SIGNAL_FUNC( targets_selection_received
),
353 /* we use m_clipboardWidget to get and to offer data */
355 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
356 gtk_widget_realize( m_clipboardWidget
);
358 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
359 "selection_received",
360 GTK_SIGNAL_FUNC( selection_received
),
363 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
364 "selection_clear_event",
365 GTK_SIGNAL_FUNC( selection_clear_clip
),
368 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
369 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
371 m_formatSupported
= FALSE
;
372 m_targetRequested
= 0;
375 wxClipboard::~wxClipboard()
379 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
380 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
383 void wxClipboard::Clear()
387 /* As we have data we also own the clipboard. Once we no longer own
388 it, clear_selection is called which will set m_data to zero */
390 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
394 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
396 while (m_waiting
) gtk_main_iteration();
399 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
403 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
405 while (m_waiting
) gtk_main_iteration();
411 m_dataBroker
= (wxDataBroker
*) NULL
;
415 m_targetRequested
= 0;
417 m_formatSupported
= FALSE
;
420 bool wxClipboard::Open()
422 wxCHECK_MSG( !m_open
, FALSE
, _T("clipboard already open") );
429 bool wxClipboard::SetData( wxDataObject
*data
)
431 wxCHECK_MSG( m_open
, FALSE
, _T("clipboard not open") );
433 wxCHECK_MSG( data
, FALSE
, _T("data is invalid") );
437 return AddData( data
);
440 bool wxClipboard::AddData( wxDataObject
*data
)
442 wxCHECK_MSG( m_open
, FALSE
, _T("clipboard not open") );
444 wxCHECK_MSG( data
, FALSE
, _T("data is invalid") );
446 /* if clipboard has been cleared before, create new data broker */
447 if (!m_dataBroker
) m_dataBroker
= new wxDataBroker();
449 /* add new data to list of offered data objects */
450 m_dataBroker
->Add( data
);
452 /* get native format id of new data object */
453 GdkAtom format
= data
->GetFormat().GetAtom();
455 wxCHECK_MSG( format
, FALSE
, _T("data has invalid format") );
457 /* This should happen automatically, but to be on the safe side */
458 m_ownsClipboard
= FALSE
;
459 m_ownsPrimarySelection
= FALSE
;
461 /* Add handlers if someone requests data */
463 #if (GTK_MINOR_VERSION > 0)
465 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
466 GDK_SELECTION_PRIMARY
,
468 0 ); /* what is info ? */
470 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
473 0 ); /* what is info ? */
475 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
477 GTK_SIGNAL_FUNC(selection_handler
),
482 gtk_selection_add_handler( m_clipboardWidget
,
488 gtk_selection_add_handler( m_clipboardWidget
,
489 GDK_SELECTION_PRIMARY
,
495 // printf( "vorher.\n" );
496 /* Tell the world we offer clipboard data */
497 if (!gtk_selection_owner_set( m_clipboardWidget
,
503 m_ownsClipboard
= TRUE
;
505 // printf( "nachher.\n" );
509 if (!gtk_selection_owner_set( m_clipboardWidget
,
510 GDK_SELECTION_PRIMARY
,
515 m_ownsPrimarySelection
= TRUE
;
520 void wxClipboard::Close()
522 wxCHECK_RET( m_open
, _T("clipboard not open") );
527 bool wxClipboard::IsSupported( wxDataFormat format
)
529 wxCHECK_MSG( m_open
, FALSE
, _T("clipboard not open") );
531 /* store requested format to be asked for by callbacks */
533 m_targetRequested
= format
.GetAtom();
535 wxCHECK_MSG( m_targetRequested
, FALSE
, _T("invalid clipboard format") );
537 m_formatSupported
= FALSE
;
539 /* perform query. this will set m_formatSupported to
540 TRUE if m_targetRequested is supported.
541 alsom we have to wait for the "answer" from the
542 clipboard owner which is an asynchronous process.
543 therefore we set m_waiting = TRUE here and wait
544 until the callback "targets_selection_received"
549 gtk_selection_convert( m_targetsWidget
,
554 while (m_waiting
) gtk_main_iteration();
556 if (!m_formatSupported
) return FALSE
;
561 bool wxClipboard::GetData( wxDataObject
*data
)
563 wxCHECK_MSG( m_open
, FALSE
, _T("clipboard not open") );
565 /* is data supported by clipboard ? */
567 if (!IsSupported( data
->GetFormat() )) return FALSE
;
569 /* store pointer to data object to be filled up by callbacks */
571 m_receivedData
= data
;
573 /* store requested format to be asked for by callbacks */
575 m_targetRequested
= data
->GetFormat().GetAtom();
577 wxCHECK_MSG( m_targetRequested
, FALSE
, _T("invalid clipboard format") );
581 m_formatSupported
= FALSE
;
583 /* ask for clipboard contents. this will set
584 m_formatSupported to TRUE if m_targetRequested
586 also, we have to wait for the "answer" from the
587 clipboard owner which is an asynchronous process.
588 therefore we set m_waiting = TRUE here and wait
589 until the callback "targets_selection_received"
594 gtk_selection_convert( m_clipboardWidget
,
599 while (m_waiting
) gtk_main_iteration();
601 /* this is a true error as we checked for the presence of such data before */
603 wxCHECK_MSG( m_formatSupported
, FALSE
, _T("error retrieving data from clipboard") );
608 //-----------------------------------------------------------------------------
610 //-----------------------------------------------------------------------------
612 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
614 bool wxClipboardModule::OnInit()
616 wxTheClipboard
= new wxClipboard();
621 void wxClipboardModule::OnExit()
623 if (wxTheClipboard
) delete wxTheClipboard
;
624 wxTheClipboard
= (wxClipboard
*) NULL
;