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 //-----------------------------------------------------------------------------
29 extern void wxapp_install_thread_wakeup();
30 extern void wxapp_uninstall_thread_wakeup();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 wxClipboard
*wxTheClipboard
= (wxClipboard
*) NULL
;
39 GdkAtom g_clipboardAtom
= 0;
40 GdkAtom g_targetsAtom
= 0;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 /* The contents of a selection are returned in a GtkSelectionData
47 structure. selection/target identify the request.
48 type specifies the type of the return; if length < 0, and
49 the data should be ignored. This structure has object semantics -
50 no fields should be modified directly, they should not be created
51 directly, and pointers to them should not be stored beyond the duration of
52 a callback. (If the last is changed, we'll need to add reference
55 struct _GtkSelectionData
67 //-----------------------------------------------------------------------------
68 // "selection_received" for targets
69 //-----------------------------------------------------------------------------
72 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
73 GtkSelectionData
*selection_data
,
74 #if (GTK_MINOR_VERSION > 0)
75 guint32
WXUNUSED(time
),
77 wxClipboard
*clipboard
)
81 clipboard
->m_waiting
= FALSE
;
85 if (selection_data
->length
<= 0)
87 clipboard
->m_waiting
= FALSE
;
91 /* make sure we got the data in the correct form */
92 if (selection_data
->type
!= GDK_SELECTION_TYPE_ATOM
)
94 clipboard
->m_waiting
= FALSE
;
98 // the atoms we received, holding a list of targets (= formats)
99 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
101 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
103 /* char *name = gdk_atom_name (atoms[i]);
104 if (name) printf( "Format available: %s.\n", name ); */
106 if (atoms
[i
] == clipboard
->m_targetRequested
)
108 clipboard
->m_waiting
= FALSE
;
109 clipboard
->m_formatSupported
= TRUE
;
114 clipboard
->m_waiting
= FALSE
;
118 //-----------------------------------------------------------------------------
119 // "selection_received" for the actual data
120 //-----------------------------------------------------------------------------
123 selection_received( GtkWidget
*WXUNUSED(widget
),
124 GtkSelectionData
*selection_data
,
125 #if (GTK_MINOR_VERSION > 0)
126 guint32
WXUNUSED(time
),
128 wxClipboard
*clipboard
)
132 clipboard
->m_waiting
= FALSE
;
136 wxDataObject
*data_object
= clipboard
->m_receivedData
;
140 clipboard
->m_waiting
= FALSE
;
144 if (selection_data
->length
<= 0)
146 clipboard
->m_waiting
= FALSE
;
150 /* make sure we got the data in the correct format */
151 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
153 clipboard
->m_waiting
= FALSE
;
157 /* make sure we got the data in the correct form (selection type).
158 if so, copy data to target object */
160 switch (data_object
->GetFormat().GetType())
164 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
166 clipboard
->m_waiting
= FALSE
;
170 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
172 wxString text
= (const char*) selection_data
->data
;
174 text_object
->SetText( text
);
181 if (selection_data
->type
!= GDK_SELECTION_TYPE_BITMAP
)
183 clipboard
->m_waiting
= FALSE
;
192 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
194 clipboard
->m_waiting
= FALSE
;
198 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
200 private_object
->SetData( (const char*) selection_data
->data
, (size_t) selection_data
->length
);
207 clipboard
->m_waiting
= FALSE
;
212 wxTheClipboard
->m_formatSupported
= TRUE
;
213 clipboard
->m_waiting
= FALSE
;
216 //-----------------------------------------------------------------------------
218 //-----------------------------------------------------------------------------
221 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
223 if (!wxTheClipboard
) return TRUE
;
225 if (event
->selection
== GDK_SELECTION_PRIMARY
)
227 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
230 if (event
->selection
== g_clipboardAtom
)
232 wxTheClipboard
->m_ownsClipboard
= FALSE
;
236 wxTheClipboard
->m_waiting
= FALSE
;
240 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
241 (!wxTheClipboard
->m_ownsClipboard
))
243 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
244 if (wxTheClipboard
->m_dataBroker
)
246 delete wxTheClipboard
->m_dataBroker
;
247 wxTheClipboard
->m_dataBroker
= (wxDataBroker
*) NULL
;
251 wxTheClipboard
->m_waiting
= FALSE
;
255 //-----------------------------------------------------------------------------
256 // selection handler for supplying data
257 //-----------------------------------------------------------------------------
260 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
262 if (!wxTheClipboard
) return;
264 if (!wxTheClipboard
->m_dataBroker
) return;
266 wxNode
*node
= wxTheClipboard
->m_dataBroker
->m_dataObjects
.First();
270 wxDataObject
*data_object
= (wxDataObject
*)node
->Data();
272 if (data_object
->GetFormat().GetAtom() != selection_data
->target
)
278 switch (data_object
->GetFormat().GetType())
282 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
284 wxString text
= text_object
->GetText();
287 const wxWX2MBbuf s
= text
.mbc_str();
289 #else // more efficient in non-Unicode
290 const char *s
= text
.c_str();
291 int len
= (int) text
.Length();
294 gtk_selection_data_set(
296 GDK_SELECTION_TYPE_STRING
,
298 (unsigned char*) (const char*) s
,
306 // wxBitmapDataObject *private_object = (wxBitmapDataObject*) data_object;
308 // how do we do that ?
315 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
317 if (private_object
->GetSize() == 0) return;
319 gtk_selection_data_set(
321 GDK_SELECTION_TYPE_STRING
,
323 (unsigned char*) private_object
->GetData(),
324 (int) private_object
->GetSize() );
335 //-----------------------------------------------------------------------------
337 //-----------------------------------------------------------------------------
339 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
341 wxClipboard::wxClipboard()
345 m_ownsClipboard
= FALSE
;
346 m_ownsPrimarySelection
= FALSE
;
348 m_dataBroker
= (wxDataBroker
*) NULL
;
350 m_receivedData
= (wxDataObject
*) NULL
;
352 /* we use m_targetsWidget to query what formats are available */
354 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
355 gtk_widget_realize( m_targetsWidget
);
357 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
358 "selection_received",
359 GTK_SIGNAL_FUNC( targets_selection_received
),
362 /* we use m_clipboardWidget to get and to offer data */
364 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
365 gtk_widget_realize( m_clipboardWidget
);
367 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
368 "selection_received",
369 GTK_SIGNAL_FUNC( selection_received
),
372 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
373 "selection_clear_event",
374 GTK_SIGNAL_FUNC( selection_clear_clip
),
377 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
378 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
380 m_formatSupported
= FALSE
;
381 m_targetRequested
= 0;
383 m_usePrimary
= FALSE
;
386 wxClipboard::~wxClipboard()
390 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
391 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
394 void wxClipboard::Clear()
399 /* disable GUI threads */
400 wxapp_uninstall_thread_wakeup();
403 /* As we have data we also own the clipboard. Once we no longer own
404 it, clear_selection is called which will set m_data to zero */
405 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
409 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
411 while (m_waiting
) gtk_main_iteration();
414 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
418 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
420 while (m_waiting
) gtk_main_iteration();
426 m_dataBroker
= (wxDataBroker
*) NULL
;
430 /* re-enable GUI threads */
431 wxapp_install_thread_wakeup();
435 m_targetRequested
= 0;
437 m_formatSupported
= FALSE
;
440 bool wxClipboard::Open()
442 wxCHECK_MSG( !m_open
, FALSE
, _T("clipboard already open") );
449 bool wxClipboard::SetData( wxDataObject
*data
)
451 wxCHECK_MSG( m_open
, FALSE
, _T("clipboard not open") );
453 wxCHECK_MSG( data
, FALSE
, _T("data is invalid") );
457 return AddData( data
);
460 bool wxClipboard::AddData( wxDataObject
*data
)
462 wxCHECK_MSG( m_open
, FALSE
, _T("clipboard not open") );
464 wxCHECK_MSG( data
, FALSE
, _T("data is invalid") );
466 /* if clipboard has been cleared before, create new data broker */
467 if (!m_dataBroker
) m_dataBroker
= new wxDataBroker();
469 /* add new data to list of offered data objects */
470 m_dataBroker
->Add( data
);
472 /* get native format id of new data object */
473 GdkAtom format
= data
->GetFormat().GetAtom();
475 wxCHECK_MSG( format
, FALSE
, _T("data has invalid format") );
477 /* This should happen automatically, but to be on the safe side */
478 m_ownsClipboard
= FALSE
;
479 m_ownsPrimarySelection
= FALSE
;
481 /* Add handlers if someone requests data */
483 #if (GTK_MINOR_VERSION > 0)
485 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
486 GDK_SELECTION_PRIMARY
,
488 0 ); /* what is info ? */
490 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
493 0 ); /* what is info ? */
495 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
497 GTK_SIGNAL_FUNC(selection_handler
),
502 gtk_selection_add_handler( m_clipboardWidget
,
508 gtk_selection_add_handler( m_clipboardWidget
,
509 GDK_SELECTION_PRIMARY
,
516 /* disable GUI threads */
517 wxapp_uninstall_thread_wakeup();
520 /* Tell the world we offer clipboard data */
521 if (!gtk_selection_owner_set( m_clipboardWidget
,
526 /* re-enable GUI threads */
527 wxapp_install_thread_wakeup();
531 m_ownsClipboard
= TRUE
;
533 if (!gtk_selection_owner_set( m_clipboardWidget
,
534 GDK_SELECTION_PRIMARY
,
538 /* re-enable GUI threads */
539 wxapp_install_thread_wakeup();
543 m_ownsPrimarySelection
= TRUE
;
546 /* re-enable GUI threads */
547 wxapp_install_thread_wakeup();
553 void wxClipboard::Close()
555 wxCHECK_RET( m_open
, _T("clipboard not open") );
560 bool wxClipboard::IsSupported( wxDataFormat format
)
562 wxCHECK_MSG( m_open
, FALSE
, _T("clipboard not open") );
564 /* store requested format to be asked for by callbacks */
566 m_targetRequested
= format
.GetAtom();
568 wxCHECK_MSG( m_targetRequested
, FALSE
, _T("invalid clipboard format") );
570 m_formatSupported
= FALSE
;
572 /* perform query. this will set m_formatSupported to
573 TRUE if m_targetRequested is supported.
574 alsom we have to wait for the "answer" from the
575 clipboard owner which is an asynchronous process.
576 therefore we set m_waiting = TRUE here and wait
577 until the callback "targets_selection_received"
582 gtk_selection_convert( m_targetsWidget
,
583 m_usePrimary
?GDK_SELECTION_PRIMARY
:g_clipboardAtom
,
587 while (m_waiting
) gtk_main_iteration();
589 if (!m_formatSupported
) return FALSE
;
594 bool wxClipboard::GetData( wxDataObject
*data
)
596 wxCHECK_MSG( m_open
, FALSE
, _T("clipboard not open") );
598 /* is data supported by clipboard ? */
600 if (!IsSupported( data
->GetFormat() )) return FALSE
;
602 /* store pointer to data object to be filled up by callbacks */
604 m_receivedData
= data
;
606 /* store requested format to be asked for by callbacks */
608 m_targetRequested
= data
->GetFormat().GetAtom();
610 wxCHECK_MSG( m_targetRequested
, FALSE
, _T("invalid clipboard format") );
614 m_formatSupported
= FALSE
;
616 /* ask for clipboard contents. this will set
617 m_formatSupported to TRUE if m_targetRequested
619 also, we have to wait for the "answer" from the
620 clipboard owner which is an asynchronous process.
621 therefore we set m_waiting = TRUE here and wait
622 until the callback "targets_selection_received"
627 gtk_selection_convert( m_clipboardWidget
,
628 m_usePrimary
?GDK_SELECTION_PRIMARY
:g_clipboardAtom
,
632 while (m_waiting
) gtk_main_iteration();
634 /* this is a true error as we checked for the presence of such data before */
636 wxCHECK_MSG( m_formatSupported
, FALSE
, _T("error retrieving data from clipboard") );
641 //-----------------------------------------------------------------------------
643 //-----------------------------------------------------------------------------
645 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
647 bool wxClipboardModule::OnInit()
649 wxTheClipboard
= new wxClipboard();
654 void wxClipboardModule::OnExit()
656 if (wxTheClipboard
) delete wxTheClipboard
;
657 wxTheClipboard
= (wxClipboard
*) NULL
;