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
++)
104 char *name = gdk_atom_name (atoms[i]);
105 if (name) printf( "Format available: %s.\n", name ); */
107 if (atoms
[i
] == clipboard
->m_targetRequested
)
109 clipboard
->m_waiting
= FALSE
;
110 clipboard
->m_formatSupported
= TRUE
;
115 clipboard
->m_waiting
= FALSE
;
119 //-----------------------------------------------------------------------------
120 // "selection_received" for the actual data
121 //-----------------------------------------------------------------------------
124 selection_received( GtkWidget
*WXUNUSED(widget
),
125 GtkSelectionData
*selection_data
,
126 #if (GTK_MINOR_VERSION > 0)
127 guint32
WXUNUSED(time
),
129 wxClipboard
*clipboard
)
133 clipboard
->m_waiting
= FALSE
;
137 wxDataObject
*data_object
= clipboard
->m_receivedData
;
141 clipboard
->m_waiting
= FALSE
;
145 if (selection_data
->length
<= 0)
147 clipboard
->m_waiting
= FALSE
;
151 /* make sure we got the data in the correct format */
152 if (data_object
->GetFormat() != selection_data
->target
)
154 clipboard
->m_waiting
= FALSE
;
158 /* make sure we got the data in the correct form (selection type).
159 if so, copy data to target object */
161 switch (data_object
->GetFormat().GetType())
165 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
167 clipboard
->m_waiting
= FALSE
;
171 wxTextDataObject
*text_object
= (wxTextDataObject
*) data_object
;
173 wxString text
= (const char*) selection_data
->data
;
175 text_object
->SetText( text
);
182 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
184 clipboard
->m_waiting
= FALSE
;
188 wxBitmapDataObject
*bitmap_object
= (wxBitmapDataObject
*) data_object
;
190 bitmap_object
->SetPngData( (const void*) selection_data
->data
, (size_t) selection_data
->length
);
197 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
199 clipboard
->m_waiting
= FALSE
;
203 wxPrivateDataObject
*private_object
= (wxPrivateDataObject
*) data_object
;
205 private_object
->SetData( (const char*) selection_data
->data
, (size_t) selection_data
->length
);
212 clipboard
->m_waiting
= FALSE
;
217 wxTheClipboard
->m_formatSupported
= TRUE
;
218 clipboard
->m_waiting
= FALSE
;
221 //-----------------------------------------------------------------------------
223 //-----------------------------------------------------------------------------
226 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
228 if (!wxTheClipboard
) return TRUE
;
230 if (event
->selection
== GDK_SELECTION_PRIMARY
)
232 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
235 if (event
->selection
== g_clipboardAtom
)
237 wxTheClipboard
->m_ownsClipboard
= FALSE
;
241 wxTheClipboard
->m_waiting
= FALSE
;
245 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
246 (!wxTheClipboard
->m_ownsClipboard
))
248 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
249 if (wxTheClipboard
->m_data
)
251 delete wxTheClipboard
->m_data
;
252 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
256 wxTheClipboard
->m_waiting
= FALSE
;
260 //-----------------------------------------------------------------------------
261 // selection handler for supplying data
262 //-----------------------------------------------------------------------------
265 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
267 if (!wxTheClipboard
) return;
269 if (!wxTheClipboard
->m_data
) return;
271 wxDataObject
*data
= wxTheClipboard
->m_data
;
273 if (!data
->IsSupportedFormat( selection_data
->target
)) return;
275 if (data
->GetFormat().GetType() == wxDF_TEXT
)
277 wxTextDataObject
*text_object
= (wxTextDataObject
*) data
;
278 wxString
text( text_object
->GetText() );
281 const wxWX2MBbuf s
= text
.mbc_str();
283 #else // more efficient in non-Unicode
284 const char *s
= text
.c_str();
285 int len
= (int) text
.Length();
287 gtk_selection_data_set(
289 GDK_SELECTION_TYPE_STRING
,
291 (unsigned char*) (const char*) s
,
297 if (data
->GetFormat().GetType() == wxDF_BITMAP
)
299 wxBitmapDataObject
*bitmap_object
= (wxBitmapDataObject
*) data
;
301 if (bitmap_object
->GetDataSize(wxDF_BITMAP
) == 0) return;
303 gtk_selection_data_set(
305 GDK_SELECTION_TYPE_STRING
,
307 (unsigned char*) bitmap_object
->GetData(),
308 (int) bitmap_object
->GetDataSize(wxDF_BITMAP
) );
313 int size
= data
->GetDataSize( selection_data
->target
);
315 if (size
== 0) return;
317 char *d
= new char[size
];
319 data
->GetDataHere( selection_data
->target
, (void*) d
);
321 gtk_selection_data_set(
323 GDK_SELECTION_TYPE_STRING
,
329 //-----------------------------------------------------------------------------
331 //-----------------------------------------------------------------------------
333 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
335 wxClipboard::wxClipboard()
339 m_ownsClipboard
= FALSE
;
340 m_ownsPrimarySelection
= FALSE
;
342 m_data
= (wxDataObject
*) NULL
;
343 m_receivedData
= (wxDataObject
*) NULL
;
345 /* we use m_targetsWidget to query what formats are available */
347 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
348 gtk_widget_realize( m_targetsWidget
);
350 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
351 "selection_received",
352 GTK_SIGNAL_FUNC( targets_selection_received
),
355 /* we use m_clipboardWidget to get and to offer data */
357 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
358 gtk_widget_realize( m_clipboardWidget
);
360 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
361 "selection_received",
362 GTK_SIGNAL_FUNC( selection_received
),
365 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
366 "selection_clear_event",
367 GTK_SIGNAL_FUNC( selection_clear_clip
),
370 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
371 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
373 m_formatSupported
= FALSE
;
374 m_targetRequested
= 0;
376 m_usePrimary
= FALSE
;
379 wxClipboard::~wxClipboard()
383 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
384 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
387 void wxClipboard::Clear()
392 /* disable GUI threads */
393 wxapp_uninstall_thread_wakeup();
396 /* As we have data we also own the clipboard. Once we no longer own
397 it, clear_selection is called which will set m_data to zero */
398 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
402 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
404 while (m_waiting
) gtk_main_iteration();
407 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
411 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
413 while (m_waiting
) gtk_main_iteration();
419 m_data
= (wxDataObject
*) NULL
;
423 /* re-enable GUI threads */
424 wxapp_install_thread_wakeup();
428 m_targetRequested
= 0;
429 m_formatSupported
= FALSE
;
432 bool wxClipboard::Open()
434 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
441 bool wxClipboard::SetData( wxDataObject
*data
)
443 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
445 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
449 return AddData( data
);
452 bool wxClipboard::AddData( wxDataObject
*data
)
454 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
456 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
458 // we can only store one wxDataObject
463 /* get native format id of new data object */
464 GdkAtom format
= data
->GetFormat();
466 wxCHECK_MSG( format
, FALSE
, wxT("data has invalid format") );
468 /* This should happen automatically, but to be on the safe side */
469 m_ownsClipboard
= FALSE
;
470 m_ownsPrimarySelection
= FALSE
;
472 /* Add handlers if someone requests data */
474 #if (GTK_MINOR_VERSION > 0)
476 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
477 GDK_SELECTION_PRIMARY
,
479 0 ); /* what is info ? */
481 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
484 0 ); /* what is info ? */
486 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
488 GTK_SIGNAL_FUNC(selection_handler
),
493 gtk_selection_add_handler( m_clipboardWidget
,
499 gtk_selection_add_handler( m_clipboardWidget
,
500 GDK_SELECTION_PRIMARY
,
507 /* disable GUI threads */
508 wxapp_uninstall_thread_wakeup();
511 /* Tell the world we offer clipboard data */
512 if (!gtk_selection_owner_set( m_clipboardWidget
,
517 /* re-enable GUI threads */
518 wxapp_install_thread_wakeup();
522 m_ownsClipboard
= TRUE
;
524 if (!gtk_selection_owner_set( m_clipboardWidget
,
525 GDK_SELECTION_PRIMARY
,
529 /* re-enable GUI threads */
530 wxapp_install_thread_wakeup();
534 m_ownsPrimarySelection
= TRUE
;
537 /* re-enable GUI threads */
538 wxapp_install_thread_wakeup();
544 void wxClipboard::Close()
546 wxCHECK_RET( m_open
, wxT("clipboard not open") );
551 bool wxClipboard::IsSupported( wxDataFormat format
)
553 /* store requested format to be asked for by callbacks */
555 m_targetRequested
= format
;
557 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
559 m_formatSupported
= FALSE
;
561 /* perform query. this will set m_formatSupported to
562 TRUE if m_targetRequested is supported.
563 alsom we have to wait for the "answer" from the
564 clipboard owner which is an asynchronous process.
565 therefore we set m_waiting = TRUE here and wait
566 until the callback "targets_selection_received"
571 gtk_selection_convert( m_targetsWidget
,
572 m_usePrimary
? GDK_SELECTION_PRIMARY
: g_clipboardAtom
,
576 while (m_waiting
) gtk_main_iteration();
578 if (!m_formatSupported
) return FALSE
;
583 bool wxClipboard::GetData( wxDataObject
*data
)
585 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
587 /* is data supported by clipboard ? */
589 if (!IsSupported( data
->GetFormat() )) return FALSE
;
591 /* store pointer to data object to be filled up by callbacks */
593 m_receivedData
= data
;
595 /* store requested format to be asked for by callbacks */
597 m_targetRequested
= data
->GetFormat();
599 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
603 m_formatSupported
= FALSE
;
605 /* ask for clipboard contents. this will set
606 m_formatSupported to TRUE if m_targetRequested
608 also, we have to wait for the "answer" from the
609 clipboard owner which is an asynchronous process.
610 therefore we set m_waiting = TRUE here and wait
611 until the callback "targets_selection_received"
616 gtk_selection_convert( m_clipboardWidget
,
617 m_usePrimary
? GDK_SELECTION_PRIMARY
: g_clipboardAtom
,
621 while (m_waiting
) gtk_main_iteration();
623 /* this is a true error as we checked for the presence of such data before */
625 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
630 //-----------------------------------------------------------------------------
632 //-----------------------------------------------------------------------------
634 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule
,wxModule
)
636 bool wxClipboardModule::OnInit()
638 wxTheClipboard
= new wxClipboard();
643 void wxClipboardModule::OnExit()
645 if (wxTheClipboard
) delete wxTheClipboard
;
646 wxTheClipboard
= (wxClipboard
*) NULL
;