1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/clipbrd.cpp
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 // the trace mask we use with wxLogTrace() - call
43 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
44 // (there will be a *lot* of them!)
45 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 /* The contents of a selection are returned in a GtkSelectionData
52 structure. selection/target identify the request.
53 type specifies the type of the return; if length < 0, and
54 the data should be ignored. This structure has object semantics -
55 no fields should be modified directly, they should not be created
56 directly, and pointers to them should not be stored beyond the duration of
57 a callback. (If the last is changed, we'll need to add reference
60 struct _GtkSelectionData
72 //-----------------------------------------------------------------------------
73 // "selection_received" for targets
74 //-----------------------------------------------------------------------------
77 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
78 GtkSelectionData
*selection_data
,
79 #if (GTK_MINOR_VERSION > 0)
80 guint32
WXUNUSED(time
),
82 wxClipboard
*clipboard
)
84 if ( wxTheClipboard
&& selection_data
->length
> 0 )
86 /* make sure we got the data in the correct form */
87 GdkAtom type
= selection_data
->type
;
88 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
90 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
92 wxLogTrace( TRACE_CLIPBOARD
,
93 _T("got unsupported clipboard target") );
95 clipboard
->m_waiting
= FALSE
;
101 wxDataFormat
clip( selection_data
->selection
);
102 wxLogTrace( TRACE_CLIPBOARD
,
103 wxT("selection received for targets, clipboard %s"),
104 clip
.GetId().c_str() );
105 #endif // __WXDEBUG__
107 // the atoms we received, holding a list of targets (= formats)
108 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
110 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
112 wxDataFormat
format( atoms
[i
] );
114 wxLogTrace( TRACE_CLIPBOARD
,
115 wxT("selection received for targets, format %s"),
116 format
.GetId().c_str() );
118 if (format
== clipboard
->m_targetRequested
)
120 clipboard
->m_waiting
= FALSE
;
121 clipboard
->m_formatSupported
= TRUE
;
127 clipboard
->m_waiting
= FALSE
;
130 //-----------------------------------------------------------------------------
131 // "selection_received" for the actual data
132 //-----------------------------------------------------------------------------
135 selection_received( GtkWidget
*WXUNUSED(widget
),
136 GtkSelectionData
*selection_data
,
137 #if (GTK_MINOR_VERSION > 0)
138 guint32
WXUNUSED(time
),
140 wxClipboard
*clipboard
)
144 clipboard
->m_waiting
= FALSE
;
148 wxDataObject
*data_object
= clipboard
->m_receivedData
;
152 clipboard
->m_waiting
= FALSE
;
156 if (selection_data
->length
<= 0)
158 clipboard
->m_waiting
= FALSE
;
162 wxDataFormat
format( selection_data
->target
);
164 /* make sure we got the data in the correct format */
165 if (!data_object
->IsSupportedFormat( format
) )
167 clipboard
->m_waiting
= FALSE
;
171 /* make sure we got the data in the correct form (selection type).
172 if so, copy data to target object */
173 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
175 clipboard
->m_waiting
= FALSE
;
179 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
181 wxTheClipboard
->m_formatSupported
= TRUE
;
182 clipboard
->m_waiting
= FALSE
;
185 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
190 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
192 if (!wxTheClipboard
) return TRUE
;
194 if (event
->selection
== GDK_SELECTION_PRIMARY
)
196 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
199 if (event
->selection
== g_clipboardAtom
)
201 wxTheClipboard
->m_ownsClipboard
= FALSE
;
205 wxTheClipboard
->m_waiting
= FALSE
;
209 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
210 (!wxTheClipboard
->m_ownsClipboard
))
212 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
213 if (wxTheClipboard
->m_data
)
215 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
217 delete wxTheClipboard
->m_data
;
218 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
222 wxTheClipboard
->m_waiting
= FALSE
;
226 //-----------------------------------------------------------------------------
227 // selection handler for supplying data
228 //-----------------------------------------------------------------------------
231 selection_handler( GtkWidget
*WXUNUSED(widget
),
232 GtkSelectionData
*selection_data
,
233 guint
WXUNUSED(info
),
234 guint
WXUNUSED(time
),
235 gpointer
WXUNUSED(data
) )
237 if (!wxTheClipboard
) return;
239 if (!wxTheClipboard
->m_data
) return;
241 wxDataObject
*data
= wxTheClipboard
->m_data
;
243 wxDataFormat
format( selection_data
->target
);
245 if (!data
->IsSupportedFormat( format
)) return;
247 int size
= data
->GetDataSize( format
);
249 if (size
== 0) return;
251 void *d
= malloc(size
);
253 data
->GetDataHere( selection_data
->target
, d
);
255 // transform Unicode text into multibyte before putting it on clipboard
257 if ( format
.GetType() == wxDF_TEXT
)
259 const wchar_t *wstr
= (const wchar_t *)d
;
260 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
261 char *str
= malloc(len
+ 1);
262 wxConvCurrent
->WC2MB(str
, wstr
, len
);
268 #endif // wxUSE_UNICODE
270 gtk_selection_data_set(
272 GDK_SELECTION_TYPE_STRING
,
280 //-----------------------------------------------------------------------------
282 //-----------------------------------------------------------------------------
284 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
286 wxClipboard::wxClipboard()
290 m_ownsClipboard
= FALSE
;
291 m_ownsPrimarySelection
= FALSE
;
293 m_data
= (wxDataObject
*) NULL
;
294 m_receivedData
= (wxDataObject
*) NULL
;
296 /* we use m_targetsWidget to query what formats are available */
298 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
299 gtk_widget_realize( m_targetsWidget
);
301 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
302 "selection_received",
303 GTK_SIGNAL_FUNC( targets_selection_received
),
306 /* we use m_clipboardWidget to get and to offer data */
308 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
309 gtk_widget_realize( m_clipboardWidget
);
311 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
312 "selection_received",
313 GTK_SIGNAL_FUNC( selection_received
),
316 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
317 "selection_clear_event",
318 GTK_SIGNAL_FUNC( selection_clear_clip
),
321 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
322 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
324 m_formatSupported
= FALSE
;
325 m_targetRequested
= 0;
327 m_usePrimary
= FALSE
;
330 wxClipboard::~wxClipboard()
334 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
335 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
338 void wxClipboard::Clear()
343 /* disable GUI threads */
344 wxapp_uninstall_thread_wakeup();
347 /* As we have data we also own the clipboard. Once we no longer own
348 it, clear_selection is called which will set m_data to zero */
349 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
353 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
354 (guint32
) GDK_CURRENT_TIME
);
356 while (m_waiting
) gtk_main_iteration();
359 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
363 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
364 (guint32
) GDK_CURRENT_TIME
);
366 while (m_waiting
) gtk_main_iteration();
372 m_data
= (wxDataObject
*) NULL
;
376 /* re-enable GUI threads */
377 wxapp_install_thread_wakeup();
381 m_targetRequested
= 0;
382 m_formatSupported
= FALSE
;
385 bool wxClipboard::Open()
387 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
394 bool wxClipboard::SetData( wxDataObject
*data
)
396 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
398 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
402 return AddData( data
);
405 bool wxClipboard::AddData( wxDataObject
*data
)
407 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
409 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
411 /* we can only store one wxDataObject */
416 /* get formats from wxDataObjects */
417 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
418 m_data
->GetAllFormats( array
);
420 /* primary selection or clipboard */
421 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
425 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
427 wxLogTrace( TRACE_CLIPBOARD
,
428 wxT("wxClipboard now supports atom %s"),
429 array
[i
].GetId().c_str() );
431 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
434 0 ); /* what is info ? */
439 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
441 GTK_SIGNAL_FUNC(selection_handler
),
445 /* disable GUI threads */
446 wxapp_uninstall_thread_wakeup();
449 /* Tell the world we offer clipboard data */
450 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
452 (guint32
) GDK_CURRENT_TIME
));
455 m_ownsPrimarySelection
= res
;
457 m_ownsClipboard
= res
;
460 /* re-enable GUI threads */
461 wxapp_install_thread_wakeup();
467 void wxClipboard::Close()
469 wxCHECK_RET( m_open
, wxT("clipboard not open") );
474 bool wxClipboard::IsOpened() const
479 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
481 /* reentrance problems */
482 if (m_waiting
) return FALSE
;
484 /* store requested format to be asked for by callbacks */
485 m_targetRequested
= format
;
488 wxLogTrace( TRACE_CLIPBOARD
,
489 wxT("wxClipboard:IsSupported: requested format: %s"),
490 format
.GetId().c_str() );
493 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
495 m_formatSupported
= FALSE
;
497 /* perform query. this will set m_formatSupported to
498 TRUE if m_targetRequested is supported.
499 also, we have to wait for the "answer" from the
500 clipboard owner which is an asynchronous process.
501 therefore we set m_waiting = TRUE here and wait
502 until the callback "targets_selection_received"
507 gtk_selection_convert( m_targetsWidget
,
508 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
511 (guint32
) GDK_CURRENT_TIME
);
513 while (m_waiting
) gtk_main_iteration();
515 if (!m_formatSupported
) return FALSE
;
520 bool wxClipboard::GetData( wxDataObject
& data
)
522 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
524 /* get formats from wxDataObjects */
525 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
526 data
.GetAllFormats( array
);
528 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
530 wxDataFormat
format( array
[i
] );
532 wxLogTrace( TRACE_CLIPBOARD
,
533 wxT("wxClipboard::GetData: requested format: %s"),
534 format
.GetId().c_str() );
536 /* is data supported by clipboard ? */
538 /* store requested format to be asked for by callbacks */
539 m_targetRequested
= format
;
541 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
543 m_formatSupported
= FALSE
;
545 /* perform query. this will set m_formatSupported to
546 TRUE if m_targetRequested is supported.
547 also, we have to wait for the "answer" from the
548 clipboard owner which is an asynchronous process.
549 therefore we set m_waiting = TRUE here and wait
550 until the callback "targets_selection_received"
555 gtk_selection_convert( m_targetsWidget
,
556 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
559 (guint32
) GDK_CURRENT_TIME
);
561 while (m_waiting
) gtk_main_iteration();
563 if (!m_formatSupported
) continue;
565 /* store pointer to data object to be filled up by callbacks */
566 m_receivedData
= &data
;
568 /* store requested format to be asked for by callbacks */
569 m_targetRequested
= format
;
571 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
574 m_formatSupported
= FALSE
;
576 /* ask for clipboard contents. this will set
577 m_formatSupported to TRUE if m_targetRequested
579 also, we have to wait for the "answer" from the
580 clipboard owner which is an asynchronous process.
581 therefore we set m_waiting = TRUE here and wait
582 until the callback "targets_selection_received"
587 wxLogTrace( TRACE_CLIPBOARD
,
588 wxT("wxClipboard::GetData: format found, start convert") );
590 gtk_selection_convert( m_clipboardWidget
,
591 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
594 (guint32
) GDK_CURRENT_TIME
);
596 while (m_waiting
) gtk_main_iteration();
598 /* this is a true error as we checked for the presence of such data before */
599 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
606 wxLogTrace( TRACE_CLIPBOARD
,
607 wxT("wxClipboard::GetData: format not found") );