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 */
83 // VZ: I don't know what does this mean (and GTK+ authors apparently
84 // don't know either, Owen Taylor writes that "Motif seems to ask
85 // for TARGETS atom sometimes" (??)), but it seems that xterm
86 // (which is not a Motif app AFAIK) does this too, so it's
87 // absolutely essential to support this, otherwise we can't paste
89 GdkAtom type
= selection_data
->type
;
90 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
92 if ( strcmp(gdk_atom_name(type
), "TARGETS") != 0 )
94 // don't know what this is
95 clipboard
->m_waiting
= FALSE
;
98 //else: don't know what this is, but it seems to work in the same
99 // way as GDK_SELECTION_TYPE_ATOM does
101 //else: the data is the list of formats supported by the selection
104 wxDataFormat clip( selection_data->selection );
105 wxLogDebug( wxT("selection received for targets, clipboard %s"), clip.GetId().c_str() );
108 // the atoms we received, holding a list of targets (= formats)
109 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
111 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
113 wxDataFormat
format( atoms
[i
] );
116 wxLogDebug( wxT("selection received for targets, format %s"), format.GetId().c_str() );
119 if (format
== clipboard
->m_targetRequested
)
121 clipboard
->m_waiting
= FALSE
;
122 clipboard
->m_formatSupported
= TRUE
;
128 clipboard
->m_waiting
= FALSE
;
131 //-----------------------------------------------------------------------------
132 // "selection_received" for the actual data
133 //-----------------------------------------------------------------------------
136 selection_received( GtkWidget
*WXUNUSED(widget
),
137 GtkSelectionData
*selection_data
,
138 #if (GTK_MINOR_VERSION > 0)
139 guint32
WXUNUSED(time
),
141 wxClipboard
*clipboard
)
145 clipboard
->m_waiting
= FALSE
;
149 wxDataObject
*data_object
= clipboard
->m_receivedData
;
153 clipboard
->m_waiting
= FALSE
;
157 if (selection_data
->length
<= 0)
159 clipboard
->m_waiting
= FALSE
;
163 wxDataFormat
format( selection_data
->target
);
165 /* make sure we got the data in the correct format */
166 if (!data_object
->IsSupportedFormat( format
) )
168 clipboard
->m_waiting
= FALSE
;
172 /* make sure we got the data in the correct form (selection type).
173 if so, copy data to target object */
174 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
176 clipboard
->m_waiting
= FALSE
;
180 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
182 wxTheClipboard
->m_formatSupported
= TRUE
;
183 clipboard
->m_waiting
= FALSE
;
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
191 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
193 if (!wxTheClipboard
) return TRUE
;
195 if (event
->selection
== GDK_SELECTION_PRIMARY
)
197 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
200 if (event
->selection
== g_clipboardAtom
)
202 wxTheClipboard
->m_ownsClipboard
= FALSE
;
206 wxTheClipboard
->m_waiting
= FALSE
;
210 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
211 (!wxTheClipboard
->m_ownsClipboard
))
213 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
214 if (wxTheClipboard
->m_data
)
216 wxLogDebug( wxT("wxClipboard will get cleared" ) );
218 delete wxTheClipboard
->m_data
;
219 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
223 wxTheClipboard
->m_waiting
= FALSE
;
227 //-----------------------------------------------------------------------------
228 // selection handler for supplying data
229 //-----------------------------------------------------------------------------
232 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
234 if (!wxTheClipboard
) return;
236 if (!wxTheClipboard
->m_data
) return;
238 wxDataObject
*data
= wxTheClipboard
->m_data
;
240 wxDataFormat
format( selection_data
->target
);
242 if (!data
->IsSupportedFormat( format
)) return;
244 /* this will fail for composite formats */
245 if (format
.GetType() == wxDF_TEXT
)
247 wxTextDataObject
*text_object
= (wxTextDataObject
*) data
;
248 wxString
text( text_object
->GetText() );
251 const wxWX2MBbuf s
= text
.mbc_str();
253 #else // more efficient in non-Unicode
254 const char *s
= text
.c_str();
255 int len
= (int) text
.Length();
257 gtk_selection_data_set(
259 GDK_SELECTION_TYPE_STRING
,
261 (unsigned char*) (const char*) s
,
267 int size
= data
->GetDataSize( format
);
269 if (size
== 0) return;
271 char *d
= new char[size
];
273 data
->GetDataHere( selection_data
->target
, (void*) d
);
275 gtk_selection_data_set(
277 GDK_SELECTION_TYPE_STRING
,
283 //-----------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
287 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
289 wxClipboard::wxClipboard()
293 m_ownsClipboard
= FALSE
;
294 m_ownsPrimarySelection
= FALSE
;
296 m_data
= (wxDataObject
*) NULL
;
297 m_receivedData
= (wxDataObject
*) NULL
;
299 /* we use m_targetsWidget to query what formats are available */
301 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
302 gtk_widget_realize( m_targetsWidget
);
304 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
305 "selection_received",
306 GTK_SIGNAL_FUNC( targets_selection_received
),
309 /* we use m_clipboardWidget to get and to offer data */
311 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
312 gtk_widget_realize( m_clipboardWidget
);
314 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
315 "selection_received",
316 GTK_SIGNAL_FUNC( selection_received
),
319 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
320 "selection_clear_event",
321 GTK_SIGNAL_FUNC( selection_clear_clip
),
324 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
325 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
327 m_formatSupported
= FALSE
;
328 m_targetRequested
= 0;
330 m_usePrimary
= FALSE
;
333 wxClipboard::~wxClipboard()
337 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
338 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
341 void wxClipboard::Clear()
346 /* disable GUI threads */
347 wxapp_uninstall_thread_wakeup();
350 /* As we have data we also own the clipboard. Once we no longer own
351 it, clear_selection is called which will set m_data to zero */
352 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
356 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
357 (guint32
) GDK_CURRENT_TIME
);
359 while (m_waiting
) gtk_main_iteration();
362 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
366 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
367 (guint32
) GDK_CURRENT_TIME
);
369 while (m_waiting
) gtk_main_iteration();
375 m_data
= (wxDataObject
*) NULL
;
379 /* re-enable GUI threads */
380 wxapp_install_thread_wakeup();
384 m_targetRequested
= 0;
385 m_formatSupported
= FALSE
;
388 bool wxClipboard::Open()
390 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
397 bool wxClipboard::SetData( wxDataObject
*data
)
399 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
401 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
405 return AddData( data
);
408 bool wxClipboard::AddData( wxDataObject
*data
)
410 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
412 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
414 /* we can only store one wxDataObject */
419 /* get formats from wxDataObjects */
420 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
421 m_data
->GetAllFormats( array
);
423 /* primary selection or clipboard */
424 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
428 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
430 wxLogDebug( wxT("wxClipboard now supports atom %s"), array
[i
].GetId().c_str() );
432 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
435 0 ); /* what is info ? */
440 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
442 GTK_SIGNAL_FUNC(selection_handler
),
446 /* disable GUI threads */
447 wxapp_uninstall_thread_wakeup();
450 /* Tell the world we offer clipboard data */
451 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
453 (guint32
) GDK_CURRENT_TIME
));
456 m_ownsPrimarySelection
= res
;
458 m_ownsClipboard
= res
;
461 /* re-enable GUI threads */
462 wxapp_install_thread_wakeup();
468 void wxClipboard::Close()
470 wxCHECK_RET( m_open
, wxT("clipboard not open") );
475 bool wxClipboard::IsOpened() const
480 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
482 /* reentrance problems */
483 if (m_open
) return TRUE
;
485 /* store requested format to be asked for by callbacks */
486 m_targetRequested
= format
;
488 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
490 m_formatSupported
= FALSE
;
492 /* perform query. this will set m_formatSupported to
493 TRUE if m_targetRequested is supported.
494 also, we have to wait for the "answer" from the
495 clipboard owner which is an asynchronous process.
496 therefore we set m_waiting = TRUE here and wait
497 until the callback "targets_selection_received"
502 gtk_selection_convert( m_targetsWidget
,
503 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
506 (guint32
) GDK_CURRENT_TIME
);
508 while (m_waiting
) gtk_main_iteration();
510 if (!m_formatSupported
) return FALSE
;
515 bool wxClipboard::GetData( wxDataObject
& data
)
517 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
519 /* get formats from wxDataObjects */
520 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
521 data
.GetAllFormats( array
);
523 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
525 wxDataFormat
format( array
[i
] );
527 wxLogDebug( wxT("wxClipboard::GetData: requested format: %s"), format
.GetId().c_str() );
529 /* is data supported by clipboard ? */
531 /* store requested format to be asked for by callbacks */
532 m_targetRequested
= format
;
534 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
536 m_formatSupported
= FALSE
;
538 /* perform query. this will set m_formatSupported to
539 TRUE if m_targetRequested is supported.
540 also, we have to wait for the "answer" from the
541 clipboard owner which is an asynchronous process.
542 therefore we set m_waiting = TRUE here and wait
543 until the callback "targets_selection_received"
548 gtk_selection_convert( m_targetsWidget
,
549 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
552 (guint32
) GDK_CURRENT_TIME
);
554 while (m_waiting
) gtk_main_iteration();
556 if (!m_formatSupported
) continue;
558 /* store pointer to data object to be filled up by callbacks */
559 m_receivedData
= &data
;
561 /* store requested format to be asked for by callbacks */
562 m_targetRequested
= format
;
564 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
567 m_formatSupported
= FALSE
;
569 /* ask for clipboard contents. this will set
570 m_formatSupported to TRUE if m_targetRequested
572 also, we have to wait for the "answer" from the
573 clipboard owner which is an asynchronous process.
574 therefore we set m_waiting = TRUE here and wait
575 until the callback "targets_selection_received"
580 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
582 gtk_selection_convert( m_clipboardWidget
,
583 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
586 (guint32
) GDK_CURRENT_TIME
);
588 while (m_waiting
) gtk_main_iteration();
590 /* this is a true error as we checked for the presence of such data before */
591 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
598 wxLogDebug( wxT("wxClipboard::GetData: format not found") );