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
)
79 if ( wxTheClipboard
&& selection_data
->length
> 0 )
81 /* make sure we got the data in the correct form */
82 GdkAtom type
= selection_data
->type
;
83 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
85 clipboard
->m_waiting
= FALSE
;
90 wxDataFormat clip( selection_data->selection );
91 wxLogDebug( wxT("selection received for targets, clipboard %s"), clip.GetId().c_str() );
94 // the atoms we received, holding a list of targets (= formats)
95 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
97 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
99 wxDataFormat
format( atoms
[i
] );
102 wxLogDebug( wxT("selection received for targets, format %s"), format.GetId().c_str() );
105 if (format
== clipboard
->m_targetRequested
)
107 clipboard
->m_waiting
= FALSE
;
108 clipboard
->m_formatSupported
= TRUE
;
114 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 wxLogDebug( wxT("wxClipboard will get cleared" ) );
204 delete wxTheClipboard
->m_data
;
205 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
209 wxTheClipboard
->m_waiting
= FALSE
;
213 //-----------------------------------------------------------------------------
214 // selection handler for supplying data
215 //-----------------------------------------------------------------------------
218 selection_handler( GtkWidget
*WXUNUSED(widget
),
219 GtkSelectionData
*selection_data
,
220 guint
WXUNUSED(info
),
221 guint
WXUNUSED(time
),
222 gpointer
WXUNUSED(data
) )
224 if (!wxTheClipboard
) return;
226 if (!wxTheClipboard
->m_data
) return;
228 wxDataObject
*data
= wxTheClipboard
->m_data
;
230 wxDataFormat
format( selection_data
->target
);
232 if (!data
->IsSupportedFormat( format
)) return;
234 /* this will fail for composite formats */
235 if (format
.GetType() == wxDF_TEXT
)
237 wxTextDataObject
*text_object
= (wxTextDataObject
*) data
;
238 wxString
text( text_object
->GetText() );
241 const wxWX2MBbuf s
= text
.mbc_str();
243 #else // more efficient in non-Unicode
244 const char *s
= text
.c_str();
245 int len
= (int) text
.Length();
247 gtk_selection_data_set(
249 GDK_SELECTION_TYPE_STRING
,
251 (unsigned char*) (const char*) s
,
257 int size
= data
->GetDataSize( format
);
259 if (size
== 0) return;
261 char *d
= new char[size
];
263 data
->GetDataHere( selection_data
->target
, (void*) d
);
265 gtk_selection_data_set(
267 GDK_SELECTION_TYPE_STRING
,
273 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
277 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
279 wxClipboard::wxClipboard()
283 m_ownsClipboard
= FALSE
;
284 m_ownsPrimarySelection
= FALSE
;
286 m_data
= (wxDataObject
*) NULL
;
287 m_receivedData
= (wxDataObject
*) NULL
;
289 /* we use m_targetsWidget to query what formats are available */
291 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
292 gtk_widget_realize( m_targetsWidget
);
294 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
295 "selection_received",
296 GTK_SIGNAL_FUNC( targets_selection_received
),
299 /* we use m_clipboardWidget to get and to offer data */
301 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
302 gtk_widget_realize( m_clipboardWidget
);
304 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
305 "selection_received",
306 GTK_SIGNAL_FUNC( selection_received
),
309 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
310 "selection_clear_event",
311 GTK_SIGNAL_FUNC( selection_clear_clip
),
314 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
315 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
317 m_formatSupported
= FALSE
;
318 m_targetRequested
= 0;
320 m_usePrimary
= FALSE
;
323 wxClipboard::~wxClipboard()
327 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
328 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
331 void wxClipboard::Clear()
336 /* disable GUI threads */
337 wxapp_uninstall_thread_wakeup();
340 /* As we have data we also own the clipboard. Once we no longer own
341 it, clear_selection is called which will set m_data to zero */
342 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
346 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
347 (guint32
) GDK_CURRENT_TIME
);
349 while (m_waiting
) gtk_main_iteration();
352 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
356 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
357 (guint32
) GDK_CURRENT_TIME
);
359 while (m_waiting
) gtk_main_iteration();
365 m_data
= (wxDataObject
*) NULL
;
369 /* re-enable GUI threads */
370 wxapp_install_thread_wakeup();
374 m_targetRequested
= 0;
375 m_formatSupported
= FALSE
;
378 bool wxClipboard::Open()
380 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
387 bool wxClipboard::SetData( wxDataObject
*data
)
389 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
391 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
395 return AddData( data
);
398 bool wxClipboard::AddData( wxDataObject
*data
)
400 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
402 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
404 /* we can only store one wxDataObject */
409 /* get formats from wxDataObjects */
410 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
411 m_data
->GetAllFormats( array
);
413 /* primary selection or clipboard */
414 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
418 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
420 wxLogDebug( wxT("wxClipboard now supports atom %s"), array
[i
].GetId().c_str() );
422 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
425 0 ); /* what is info ? */
430 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
432 GTK_SIGNAL_FUNC(selection_handler
),
436 /* disable GUI threads */
437 wxapp_uninstall_thread_wakeup();
440 /* Tell the world we offer clipboard data */
441 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
443 (guint32
) GDK_CURRENT_TIME
));
446 m_ownsPrimarySelection
= res
;
448 m_ownsClipboard
= res
;
451 /* re-enable GUI threads */
452 wxapp_install_thread_wakeup();
458 void wxClipboard::Close()
460 wxCHECK_RET( m_open
, wxT("clipboard not open") );
465 bool wxClipboard::IsOpened() const
470 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
472 /* reentrance problems */
473 if (m_waiting
) return FALSE
;
475 /* store requested format to be asked for by callbacks */
476 m_targetRequested
= format
;
479 wxLogDebug( wxT("wxClipboard:IsSupported: requested format: %s"), format.GetId().c_str() );
482 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
484 m_formatSupported
= FALSE
;
486 /* perform query. this will set m_formatSupported to
487 TRUE if m_targetRequested is supported.
488 also, we have to wait for the "answer" from the
489 clipboard owner which is an asynchronous process.
490 therefore we set m_waiting = TRUE here and wait
491 until the callback "targets_selection_received"
496 gtk_selection_convert( m_targetsWidget
,
497 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
500 (guint32
) GDK_CURRENT_TIME
);
502 while (m_waiting
) gtk_main_iteration();
504 if (!m_formatSupported
) return FALSE
;
509 bool wxClipboard::GetData( wxDataObject
& data
)
511 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
513 /* get formats from wxDataObjects */
514 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
515 data
.GetAllFormats( array
);
517 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
519 wxDataFormat
format( array
[i
] );
521 wxLogDebug( wxT("wxClipboard::GetData: requested format: %s"), format
.GetId().c_str() );
523 /* is data supported by clipboard ? */
525 /* store requested format to be asked for by callbacks */
526 m_targetRequested
= format
;
528 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
530 m_formatSupported
= FALSE
;
532 /* perform query. this will set m_formatSupported to
533 TRUE if m_targetRequested is supported.
534 also, we have to wait for the "answer" from the
535 clipboard owner which is an asynchronous process.
536 therefore we set m_waiting = TRUE here and wait
537 until the callback "targets_selection_received"
542 gtk_selection_convert( m_targetsWidget
,
543 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
546 (guint32
) GDK_CURRENT_TIME
);
548 while (m_waiting
) gtk_main_iteration();
550 if (!m_formatSupported
) continue;
552 /* store pointer to data object to be filled up by callbacks */
553 m_receivedData
= &data
;
555 /* store requested format to be asked for by callbacks */
556 m_targetRequested
= format
;
558 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
561 m_formatSupported
= FALSE
;
563 /* ask for clipboard contents. this will set
564 m_formatSupported to TRUE if m_targetRequested
566 also, we have to wait for the "answer" from the
567 clipboard owner which is an asynchronous process.
568 therefore we set m_waiting = TRUE here and wait
569 until the callback "targets_selection_received"
574 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
576 gtk_selection_convert( m_clipboardWidget
,
577 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
580 (guint32
) GDK_CURRENT_TIME
);
582 while (m_waiting
) gtk_main_iteration();
584 /* this is a true error as we checked for the presence of such data before */
585 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
592 wxLogDebug( wxT("wxClipboard::GetData: format not found") );