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 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 GdkAtom g_clipboardAtom
= 0;
38 GdkAtom g_targetsAtom
= 0;
40 // the trace mask we use with wxLogTrace() - call
41 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
42 // (there will be a *lot* of them!)
43 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 /* The contents of a selection are returned in a GtkSelectionData
50 structure. selection/target identify the request.
51 type specifies the type of the return; if length < 0, and
52 the data should be ignored. This structure has object semantics -
53 no fields should be modified directly, they should not be created
54 directly, and pointers to them should not be stored beyond the duration of
55 a callback. (If the last is changed, we'll need to add reference
58 struct _GtkSelectionData
70 //-----------------------------------------------------------------------------
71 // "selection_received" for targets
72 //-----------------------------------------------------------------------------
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
;
130 //-----------------------------------------------------------------------------
131 // "selection_received" for the actual data
132 //-----------------------------------------------------------------------------
135 selection_received( GtkWidget
*WXUNUSED(widget
),
136 GtkSelectionData
*selection_data
,
137 guint32
WXUNUSED(time
),
138 wxClipboard
*clipboard
)
142 clipboard
->m_waiting
= FALSE
;
146 wxDataObject
*data_object
= clipboard
->m_receivedData
;
150 clipboard
->m_waiting
= FALSE
;
154 if (selection_data
->length
<= 0)
156 clipboard
->m_waiting
= FALSE
;
160 wxDataFormat
format( selection_data
->target
);
162 // make sure we got the data in the correct format
163 if (!data_object
->IsSupportedFormat( format
) )
165 clipboard
->m_waiting
= FALSE
;
169 /* make sure we got the data in the correct form (selection type).
170 if so, copy data to target object */
171 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
173 clipboard
->m_waiting
= FALSE
;
177 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
179 wxTheClipboard
->m_formatSupported
= TRUE
;
180 clipboard
->m_waiting
= FALSE
;
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
188 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
190 if (!wxTheClipboard
) return TRUE
;
192 if (event
->selection
== GDK_SELECTION_PRIMARY
)
194 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
197 if (event
->selection
== g_clipboardAtom
)
199 wxTheClipboard
->m_ownsClipboard
= FALSE
;
203 wxTheClipboard
->m_waiting
= FALSE
;
207 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
208 (!wxTheClipboard
->m_ownsClipboard
))
210 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
211 if (wxTheClipboard
->m_data
)
213 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
215 delete wxTheClipboard
->m_data
;
216 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
220 wxTheClipboard
->m_waiting
= FALSE
;
224 //-----------------------------------------------------------------------------
225 // selection handler for supplying data
226 //-----------------------------------------------------------------------------
229 selection_handler( GtkWidget
*WXUNUSED(widget
),
230 GtkSelectionData
*selection_data
,
231 guint
WXUNUSED(info
),
232 guint
WXUNUSED(time
),
233 gpointer
WXUNUSED(data
) )
235 if (!wxTheClipboard
) return;
237 if (!wxTheClipboard
->m_data
) return;
239 wxDataObject
*data
= wxTheClipboard
->m_data
;
241 wxDataFormat
format( selection_data
->target
);
243 if (!data
->IsSupportedFormat( format
)) return;
245 int size
= data
->GetDataSize( format
);
247 if (size
== 0) return;
249 void *d
= malloc(size
);
251 // Text data will be in UTF8 in Unicode mode.
252 data
->GetDataHere( selection_data
->target
, d
);
254 gtk_selection_data_set(
256 GDK_SELECTION_TYPE_STRING
,
264 //-----------------------------------------------------------------------------
266 //-----------------------------------------------------------------------------
268 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
270 wxClipboard::wxClipboard()
274 m_ownsClipboard
= FALSE
;
275 m_ownsPrimarySelection
= FALSE
;
277 m_data
= (wxDataObject
*) NULL
;
278 m_receivedData
= (wxDataObject
*) NULL
;
280 /* we use m_targetsWidget to query what formats are available */
282 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
283 gtk_widget_realize( m_targetsWidget
);
285 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
286 "selection_received",
287 GTK_SIGNAL_FUNC( targets_selection_received
),
290 /* we use m_clipboardWidget to get and to offer data */
292 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
293 gtk_widget_realize( m_clipboardWidget
);
295 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
296 "selection_received",
297 GTK_SIGNAL_FUNC( selection_received
),
300 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
301 "selection_clear_event",
302 GTK_SIGNAL_FUNC( selection_clear_clip
),
305 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
306 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
308 m_formatSupported
= FALSE
;
309 m_targetRequested
= 0;
311 m_usePrimary
= FALSE
;
314 wxClipboard::~wxClipboard()
318 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
319 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
322 void wxClipboard::Clear()
327 /* disable GUI threads */
330 // As we have data we also own the clipboard. Once we no longer own
331 // it, clear_selection is called which will set m_data to zero
332 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
336 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
337 (guint32
) GDK_CURRENT_TIME
);
339 while (m_waiting
) gtk_main_iteration();
342 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
346 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
347 (guint32
) GDK_CURRENT_TIME
);
349 while (m_waiting
) gtk_main_iteration();
355 m_data
= (wxDataObject
*) NULL
;
359 /* re-enable GUI threads */
363 m_targetRequested
= 0;
364 m_formatSupported
= FALSE
;
367 bool wxClipboard::Open()
369 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
376 bool wxClipboard::SetData( wxDataObject
*data
)
378 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
380 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
384 return AddData( data
);
387 bool wxClipboard::AddData( wxDataObject
*data
)
389 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
391 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
393 // we can only store one wxDataObject
398 // get formats from wxDataObjects
399 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
400 m_data
->GetAllFormats( array
);
402 // primary selection or clipboard
403 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
407 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
409 wxLogTrace( TRACE_CLIPBOARD
,
410 wxT("wxClipboard now supports atom %s"),
411 array
[i
].GetId().c_str() );
413 // printf( "added %s\n",
414 // gdk_atom_name( array[i].GetFormatId() ) );
416 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
419 0 ); /* what is info ? */
424 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
426 GTK_SIGNAL_FUNC(selection_handler
),
430 /* disable GUI threads */
433 /* Tell the world we offer clipboard data */
434 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
436 (guint32
) GDK_CURRENT_TIME
));
439 m_ownsPrimarySelection
= res
;
441 m_ownsClipboard
= res
;
444 /* re-enable GUI threads */
450 void wxClipboard::Close()
452 wxCHECK_RET( m_open
, wxT("clipboard not open") );
457 bool wxClipboard::IsOpened() const
462 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
464 /* reentrance problems */
465 if (m_waiting
) return FALSE
;
467 /* store requested format to be asked for by callbacks */
468 m_targetRequested
= format
;
471 wxLogTrace( TRACE_CLIPBOARD
,
472 wxT("wxClipboard:IsSupported: requested format: %s"),
473 format
.GetId().c_str() );
476 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
478 m_formatSupported
= FALSE
;
480 /* perform query. this will set m_formatSupported to
481 TRUE if m_targetRequested is supported.
482 also, we have to wait for the "answer" from the
483 clipboard owner which is an asynchronous process.
484 therefore we set m_waiting = TRUE here and wait
485 until the callback "targets_selection_received"
490 gtk_selection_convert( m_targetsWidget
,
491 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
494 (guint32
) GDK_CURRENT_TIME
);
496 while (m_waiting
) gtk_main_iteration();
498 if (!m_formatSupported
) return FALSE
;
503 bool wxClipboard::GetData( wxDataObject
& data
)
505 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
507 /* get formats from wxDataObjects */
508 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
509 data
.GetAllFormats( array
);
511 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
513 wxDataFormat
format( array
[i
] );
515 wxLogTrace( TRACE_CLIPBOARD
,
516 wxT("wxClipboard::GetData: requested format: %s"),
517 format
.GetId().c_str() );
519 /* is data supported by clipboard ? */
521 /* store requested format to be asked for by callbacks */
522 m_targetRequested
= format
;
524 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
526 m_formatSupported
= FALSE
;
528 /* perform query. this will set m_formatSupported to
529 TRUE if m_targetRequested is supported.
530 also, we have to wait for the "answer" from the
531 clipboard owner which is an asynchronous process.
532 therefore we set m_waiting = TRUE here and wait
533 until the callback "targets_selection_received"
538 gtk_selection_convert( m_targetsWidget
,
539 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
542 (guint32
) GDK_CURRENT_TIME
);
544 while (m_waiting
) gtk_main_iteration();
546 if (!m_formatSupported
) continue;
548 /* store pointer to data object to be filled up by callbacks */
549 m_receivedData
= &data
;
551 /* store requested format to be asked for by callbacks */
552 m_targetRequested
= format
;
554 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
557 m_formatSupported
= FALSE
;
559 /* ask for clipboard contents. this will set
560 m_formatSupported to TRUE if m_targetRequested
562 also, we have to wait for the "answer" from the
563 clipboard owner which is an asynchronous process.
564 therefore we set m_waiting = TRUE here and wait
565 until the callback "targets_selection_received"
570 wxLogTrace( TRACE_CLIPBOARD
,
571 wxT("wxClipboard::GetData: format found, start convert") );
573 gtk_selection_convert( m_clipboardWidget
,
574 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
577 (guint32
) GDK_CURRENT_TIME
);
579 while (m_waiting
) gtk_main_iteration();
581 /* this is a true error as we checked for the presence of such data before */
582 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
589 wxLogTrace( TRACE_CLIPBOARD
,
590 wxT("wxClipboard::GetData: format not found") );