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"
18 #include "wx/dataobj.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
31 extern void wxapp_install_thread_wakeup();
32 extern void wxapp_uninstall_thread_wakeup();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
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 wxDataFormat
format( atoms
[i
] );
105 if (format
== clipboard
->m_targetRequested
)
107 clipboard
->m_waiting
= FALSE
;
108 clipboard
->m_formatSupported
= TRUE
;
113 clipboard
->m_waiting
= FALSE
;
117 //-----------------------------------------------------------------------------
118 // "selection_received" for the actual data
119 //-----------------------------------------------------------------------------
122 selection_received( GtkWidget
*WXUNUSED(widget
),
123 GtkSelectionData
*selection_data
,
124 #if (GTK_MINOR_VERSION > 0)
125 guint32
WXUNUSED(time
),
127 wxClipboard
*clipboard
)
131 clipboard
->m_waiting
= FALSE
;
135 wxDataObject
*data_object
= clipboard
->m_receivedData
;
139 clipboard
->m_waiting
= FALSE
;
143 if (selection_data
->length
<= 0)
145 clipboard
->m_waiting
= FALSE
;
149 wxDataFormat
format( selection_data
->target
);
151 /* make sure we got the data in the correct format */
152 if (!data_object
->IsSupportedFormat( format
) )
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 */
160 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
162 clipboard
->m_waiting
= FALSE
;
166 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
168 wxTheClipboard
->m_formatSupported
= TRUE
;
169 clipboard
->m_waiting
= FALSE
;
172 //-----------------------------------------------------------------------------
174 //-----------------------------------------------------------------------------
177 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
179 if (!wxTheClipboard
) return TRUE
;
181 if (event
->selection
== GDK_SELECTION_PRIMARY
)
183 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
186 if (event
->selection
== g_clipboardAtom
)
188 wxTheClipboard
->m_ownsClipboard
= FALSE
;
192 wxTheClipboard
->m_waiting
= FALSE
;
196 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
197 (!wxTheClipboard
->m_ownsClipboard
))
199 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
200 if (wxTheClipboard
->m_data
)
202 delete wxTheClipboard
->m_data
;
203 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
207 wxTheClipboard
->m_waiting
= FALSE
;
211 //-----------------------------------------------------------------------------
212 // selection handler for supplying data
213 //-----------------------------------------------------------------------------
216 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
218 if (!wxTheClipboard
) return;
220 if (!wxTheClipboard
->m_data
) return;
222 wxDataObject
*data
= wxTheClipboard
->m_data
;
224 wxDataFormat
format( selection_data
->target
);
226 if (!data
->IsSupportedFormat( format
)) return;
228 /* this will fail for composite formats */
229 if (format
.GetType() == wxDF_TEXT
)
231 wxTextDataObject
*text_object
= (wxTextDataObject
*) data
;
232 wxString
text( text_object
->GetText() );
235 const wxWX2MBbuf s
= text
.mbc_str();
237 #else // more efficient in non-Unicode
238 const char *s
= text
.c_str();
239 int len
= (int) text
.Length();
241 gtk_selection_data_set(
243 GDK_SELECTION_TYPE_STRING
,
245 (unsigned char*) (const char*) s
,
251 int size
= data
->GetDataSize( format
);
253 if (size
== 0) return;
255 char *d
= new char[size
];
257 data
->GetDataHere( selection_data
->target
, (void*) d
);
259 gtk_selection_data_set(
261 GDK_SELECTION_TYPE_STRING
,
267 //-----------------------------------------------------------------------------
269 //-----------------------------------------------------------------------------
271 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
273 wxClipboard::wxClipboard()
277 m_ownsClipboard
= FALSE
;
278 m_ownsPrimarySelection
= FALSE
;
280 m_data
= (wxDataObject
*) NULL
;
281 m_receivedData
= (wxDataObject
*) NULL
;
283 /* we use m_targetsWidget to query what formats are available */
285 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
286 gtk_widget_realize( m_targetsWidget
);
288 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
289 "selection_received",
290 GTK_SIGNAL_FUNC( targets_selection_received
),
293 /* we use m_clipboardWidget to get and to offer data */
295 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
296 gtk_widget_realize( m_clipboardWidget
);
298 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
299 "selection_received",
300 GTK_SIGNAL_FUNC( selection_received
),
303 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
304 "selection_clear_event",
305 GTK_SIGNAL_FUNC( selection_clear_clip
),
308 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
309 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
311 m_formatSupported
= FALSE
;
312 m_targetRequested
= 0;
314 m_usePrimary
= FALSE
;
317 wxClipboard::~wxClipboard()
321 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
322 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
325 void wxClipboard::Clear()
330 /* disable GUI threads */
331 wxapp_uninstall_thread_wakeup();
334 /* As we have data we also own the clipboard. Once we no longer own
335 it, clear_selection is called which will set m_data to zero */
336 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
340 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
, GDK_CURRENT_TIME
);
342 while (m_waiting
) gtk_main_iteration();
345 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
349 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
351 while (m_waiting
) gtk_main_iteration();
357 m_data
= (wxDataObject
*) NULL
;
361 /* re-enable GUI threads */
362 wxapp_install_thread_wakeup();
366 m_targetRequested
= 0;
367 m_formatSupported
= FALSE
;
370 bool wxClipboard::Open()
372 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
379 bool wxClipboard::SetData( wxDataObject
*data
)
381 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
383 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
387 return AddData( data
);
390 bool wxClipboard::AddData( wxDataObject
*data
)
392 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
394 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
396 /* we can only store one wxDataObject */
401 /* This should happen automatically, but to be on the safe side */
402 m_ownsClipboard
= FALSE
;
403 m_ownsPrimarySelection
= FALSE
;
405 /* get formats from wxDataObjects */
406 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
407 m_data
->GetAllFormats( array
);
409 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
411 GdkAtom atom
= array
[i
];
412 wxLogDebug( wxT("Clipboard Supported atom %s"), gdk_atom_name( atom
) );
414 /* Add handlers if someone requests data. We currently always
415 offer data to the clipboard and the primary selection. Maybe
416 we should make that depend on the usePrimary flag */
418 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
419 GDK_SELECTION_PRIMARY
,
421 0 ); /* what is info ? */
423 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
426 0 ); /* what is info ? */
431 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
433 GTK_SIGNAL_FUNC(selection_handler
),
437 /* disable GUI threads */
438 wxapp_uninstall_thread_wakeup();
441 /* Tell the world we offer clipboard data */
442 if (!gtk_selection_owner_set( m_clipboardWidget
,
447 /* re-enable GUI threads */
448 wxapp_install_thread_wakeup();
452 m_ownsClipboard
= TRUE
;
454 if (!gtk_selection_owner_set( m_clipboardWidget
,
455 GDK_SELECTION_PRIMARY
,
459 /* re-enable GUI threads */
460 wxapp_install_thread_wakeup();
464 m_ownsPrimarySelection
= TRUE
;
467 /* re-enable GUI threads */
468 wxapp_install_thread_wakeup();
474 void wxClipboard::Close()
476 wxCHECK_RET( m_open
, wxT("clipboard not open") );
481 bool wxClipboard::IsOpened() const
486 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
488 /* reentrance problems */
489 if (m_open
) return TRUE
;
491 /* store requested format to be asked for by callbacks */
492 m_targetRequested
= format
;
494 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
496 m_formatSupported
= FALSE
;
498 /* perform query. this will set m_formatSupported to
499 TRUE if m_targetRequested is supported.
500 alsom we have to wait for the "answer" from the
501 clipboard owner which is an asynchronous process.
502 therefore we set m_waiting = TRUE here and wait
503 until the callback "targets_selection_received"
508 gtk_selection_convert( m_targetsWidget
,
509 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
514 while (m_waiting
) gtk_main_iteration();
516 if (!m_formatSupported
) return FALSE
;
521 bool wxClipboard::GetData( wxDataObject
& data
)
523 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
525 /* get formats from wxDataObjects */
526 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
527 data
.GetAllFormats( array
);
529 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
531 wxDataFormat
format( array
[i
] );
533 wxLogDebug( wxT("wxClipboard::GetData: request format %s"), format
.GetId().c_str() );
535 /* is data supported by clipboard ? */
536 if (!IsSupported( format
))
539 /* store pointer to data object to be filled up by callbacks */
540 m_receivedData
= &data
;
542 /* store requested format to be asked for by callbacks */
543 m_targetRequested
= format
;
545 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
548 m_formatSupported
= FALSE
;
550 /* ask for clipboard contents. this will set
551 m_formatSupported to TRUE if m_targetRequested
553 also, we have to wait for the "answer" from the
554 clipboard owner which is an asynchronous process.
555 therefore we set m_waiting = TRUE here and wait
556 until the callback "targets_selection_received"
561 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
563 gtk_selection_convert( m_clipboardWidget
,
564 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
569 while (m_waiting
) gtk_main_iteration();
571 /* this is a true error as we checked for the presence of such data before */
572 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );