1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/clipbrd.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/clipbrd.h"
17 #include "wx/dataobj.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 GdkAtom g_clipboardAtom
= 0;
37 GdkAtom g_targetsAtom
= 0;
39 // the trace mask we use with wxLogTrace() - call
40 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
41 // (there will be a *lot* of them!)
42 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 /* The contents of a selection are returned in a GtkSelectionData
49 structure. selection/target identify the request.
50 type specifies the type of the return; if length < 0, and
51 the data should be ignored. This structure has object semantics -
52 no fields should be modified directly, they should not be created
53 directly, and pointers to them should not be stored beyond the duration of
54 a callback. (If the last is changed, we'll need to add reference
57 struct _GtkSelectionData
69 //-----------------------------------------------------------------------------
70 // "selection_received" for targets
71 //-----------------------------------------------------------------------------
75 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
76 GtkSelectionData
*selection_data
,
77 guint32
WXUNUSED(time
),
78 wxClipboard
*clipboard
)
80 if ( wxTheClipboard
&& selection_data
->length
> 0 )
82 // make sure we got the data in the correct form
83 GdkAtom type
= selection_data
->type
;
84 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
86 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
88 wxLogTrace( TRACE_CLIPBOARD
,
89 _T("got unsupported clipboard target") );
91 clipboard
->m_waiting
= FALSE
;
97 wxDataFormat
clip( selection_data
->selection
);
98 wxLogTrace( TRACE_CLIPBOARD
,
99 wxT("selection received for targets, clipboard %s"),
100 clip
.GetId().c_str() );
101 #endif // __WXDEBUG__
103 // the atoms we received, holding a list of targets (= formats)
104 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
106 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
108 wxDataFormat
format( atoms
[i
] );
110 wxLogTrace( TRACE_CLIPBOARD
,
111 wxT("selection received for targets, format %s"),
112 format
.GetId().c_str() );
114 // printf( "format %s requested %s\n",
115 // gdk_atom_name( atoms[i] ),
116 // gdk_atom_name( clipboard->m_targetRequested ) );
118 if (format
== clipboard
->m_targetRequested
)
120 clipboard
->m_waiting
= FALSE
;
121 clipboard
->m_formatSupported
= TRUE
;
127 clipboard
->m_waiting
= FALSE
;
131 //-----------------------------------------------------------------------------
132 // "selection_received" for the actual data
133 //-----------------------------------------------------------------------------
137 selection_received( GtkWidget
*WXUNUSED(widget
),
138 GtkSelectionData
*selection_data
,
139 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
;
172 This seems to cause problems somehow
173 // make sure we got the data in the correct form (selection type).
174 // if so, copy data to target object
175 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
177 clipboard
->m_waiting
= FALSE
;
182 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
184 wxTheClipboard
->m_formatSupported
= TRUE
;
185 clipboard
->m_waiting
= FALSE
;
189 //-----------------------------------------------------------------------------
191 //-----------------------------------------------------------------------------
195 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
197 if (!wxTheClipboard
) return TRUE
;
199 if (event
->selection
== GDK_SELECTION_PRIMARY
)
201 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
204 if (event
->selection
== g_clipboardAtom
)
206 wxTheClipboard
->m_ownsClipboard
= FALSE
;
210 wxTheClipboard
->m_waiting
= FALSE
;
214 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
215 (!wxTheClipboard
->m_ownsClipboard
))
217 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
218 if (wxTheClipboard
->m_data
)
220 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
222 delete wxTheClipboard
->m_data
;
223 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
227 wxTheClipboard
->m_waiting
= FALSE
;
232 //-----------------------------------------------------------------------------
233 // selection handler for supplying data
234 //-----------------------------------------------------------------------------
238 selection_handler( GtkWidget
*WXUNUSED(widget
),
239 GtkSelectionData
*selection_data
,
240 guint
WXUNUSED(info
),
241 guint
WXUNUSED(time
),
242 gpointer
WXUNUSED(data
) )
244 if (!wxTheClipboard
) return;
246 if (!wxTheClipboard
->m_data
) return;
248 wxDataObject
*data
= wxTheClipboard
->m_data
;
250 wxDataFormat
format( selection_data
->target
);
253 wxLogTrace(TRACE_CLIPBOARD
,
254 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s"),
255 format
.GetId().c_str(),
256 wxString::FromAscii(gdk_atom_name(selection_data
->target
)).c_str(),
257 wxString::FromAscii(gdk_atom_name(selection_data
->type
)).c_str(),
258 wxString::FromAscii(gdk_atom_name(selection_data
->selection
)).c_str()
262 if (!data
->IsSupportedFormat( format
)) return;
264 int size
= data
->GetDataSize( format
);
266 if (size
== 0) return;
268 void *d
= malloc(size
);
270 // Text data will be in UTF8 in Unicode mode.
271 data
->GetDataHere( selection_data
->target
, d
);
273 gtk_selection_data_set(
275 GDK_SELECTION_TYPE_STRING
,
284 //-----------------------------------------------------------------------------
286 //-----------------------------------------------------------------------------
288 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
290 wxClipboard::wxClipboard()
295 m_ownsClipboard
= FALSE
;
296 m_ownsPrimarySelection
= FALSE
;
298 m_data
= (wxDataObject
*) NULL
;
299 m_receivedData
= (wxDataObject
*) NULL
;
301 /* we use m_targetsWidget to query what formats are available */
303 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
304 gtk_widget_realize( m_targetsWidget
);
306 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
307 "selection_received",
308 GTK_SIGNAL_FUNC( targets_selection_received
),
311 /* we use m_clipboardWidget to get and to offer data */
313 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
314 gtk_widget_realize( m_clipboardWidget
);
316 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
317 "selection_received",
318 GTK_SIGNAL_FUNC( selection_received
),
321 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
322 "selection_clear_event",
323 GTK_SIGNAL_FUNC( selection_clear_clip
),
326 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
327 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
329 m_formatSupported
= FALSE
;
330 m_targetRequested
= 0;
332 m_usePrimary
= FALSE
;
335 wxClipboard::~wxClipboard()
339 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
340 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
343 void wxClipboard::Clear()
348 /* disable GUI threads */
351 // As we have data we also own the clipboard. Once we no longer own
352 // it, clear_selection is called which will set m_data to zero
353 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
357 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
358 (guint32
) GDK_CURRENT_TIME
);
360 while (m_waiting
) gtk_main_iteration();
363 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
367 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
368 (guint32
) GDK_CURRENT_TIME
);
370 while (m_waiting
) gtk_main_iteration();
376 m_data
= (wxDataObject
*) NULL
;
380 /* re-enable GUI threads */
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 wxLogTrace( TRACE_CLIPBOARD
,
431 wxT("wxClipboard now supports atom %s"),
432 array
[i
].GetId().c_str() );
434 // printf( "added %s\n",
435 // gdk_atom_name( array[i].GetFormatId() ) );
437 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
440 0 ); /* what is info ? */
445 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
447 GTK_SIGNAL_FUNC(selection_handler
),
451 /* disable GUI threads */
454 /* Tell the world we offer clipboard data */
455 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
457 (guint32
) GDK_CURRENT_TIME
));
460 m_ownsPrimarySelection
= res
;
462 m_ownsClipboard
= res
;
465 /* re-enable GUI threads */
471 void wxClipboard::Close()
473 wxCHECK_RET( m_open
, wxT("clipboard not open") );
478 bool wxClipboard::IsOpened() const
483 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
485 /* reentrance problems */
486 if (m_waiting
) return FALSE
;
488 /* store requested format to be asked for by callbacks */
489 m_targetRequested
= format
;
491 wxLogTrace( TRACE_CLIPBOARD
,
492 wxT("wxClipboard:IsSupported: requested format: %s"),
493 format
.GetId().c_str() );
495 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
497 m_formatSupported
= FALSE
;
499 /* perform query. this will set m_formatSupported to
500 TRUE if m_targetRequested is supported.
501 also, we have to wait for the "answer" from the
502 clipboard owner which is an asynchronous process.
503 therefore we set m_waiting = TRUE here and wait
504 until the callback "targets_selection_received"
509 gtk_selection_convert( m_targetsWidget
,
510 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
513 (guint32
) GDK_CURRENT_TIME
);
515 while (m_waiting
) gtk_main_iteration();
517 return m_formatSupported
;
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") );