1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/clipbrd.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "clipbrd.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/clipbrd.h"
21 #include "wx/dataobj.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 GdkAtom g_clipboardAtom
= 0;
41 GdkAtom g_targetsAtom
= 0;
43 // the trace mask we use with wxLogTrace() - call
44 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
45 // (there will be a *lot* of them!)
46 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 /* The contents of a selection are returned in a GtkSelectionData
53 structure. selection/target identify the request.
54 type specifies the type of the return; if length < 0, and
55 the data should be ignored. This structure has object semantics -
56 no fields should be modified directly, they should not be created
57 directly, and pointers to them should not be stored beyond the duration of
58 a callback. (If the last is changed, we'll need to add reference
61 struct _GtkSelectionData
73 //-----------------------------------------------------------------------------
74 // "selection_received" for targets
75 //-----------------------------------------------------------------------------
78 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
79 GtkSelectionData
*selection_data
,
80 guint32
WXUNUSED(time
),
81 wxClipboard
*clipboard
)
83 if ( wxTheClipboard
&& selection_data
->length
> 0 )
85 // make sure we got the data in the correct form
86 GdkAtom type
= selection_data
->type
;
87 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
89 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
91 wxLogTrace( TRACE_CLIPBOARD
,
92 _T("got unsupported clipboard target") );
94 clipboard
->m_waiting
= FALSE
;
100 wxDataFormat
clip( selection_data
->selection
);
101 wxLogTrace( TRACE_CLIPBOARD
,
102 wxT("selection received for targets, clipboard %s"),
103 clip
.GetId().c_str() );
104 #endif // __WXDEBUG__
106 // the atoms we received, holding a list of targets (= formats)
107 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
109 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
111 wxDataFormat
format( atoms
[i
] );
113 wxLogTrace( TRACE_CLIPBOARD
,
114 wxT("selection received for targets, format %s"),
115 format
.GetId().c_str() );
117 // printf( "format %s requested %s\n",
118 // gdk_atom_name( atoms[i] ),
119 // gdk_atom_name( clipboard->m_targetRequested ) );
121 if (format
== clipboard
->m_targetRequested
)
123 clipboard
->m_waiting
= FALSE
;
124 clipboard
->m_formatSupported
= TRUE
;
130 clipboard
->m_waiting
= FALSE
;
133 //-----------------------------------------------------------------------------
134 // "selection_received" for the actual data
135 //-----------------------------------------------------------------------------
138 selection_received( GtkWidget
*WXUNUSED(widget
),
139 GtkSelectionData
*selection_data
,
140 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 wxLogTrace(TRACE_CLIPBOARD
, 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
),
233 GtkSelectionData
*selection_data
,
234 guint
WXUNUSED(info
),
235 guint
WXUNUSED(time
),
236 gpointer
WXUNUSED(data
) )
238 if (!wxTheClipboard
) return;
240 if (!wxTheClipboard
->m_data
) return;
242 wxDataObject
*data
= wxTheClipboard
->m_data
;
244 wxDataFormat
format( selection_data
->target
);
246 if (!data
->IsSupportedFormat( format
)) return;
248 int size
= data
->GetDataSize( format
);
250 if (size
== 0) return;
252 void *d
= malloc(size
);
254 // Text data will be in UTF8 in Unicode mode.
255 data
->GetDataHere( selection_data
->target
, d
);
257 gtk_selection_data_set(
259 GDK_SELECTION_TYPE_STRING
,
267 //-----------------------------------------------------------------------------
269 //-----------------------------------------------------------------------------
271 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
273 wxClipboard::wxClipboard()
277 m_ownsClipboard
= FALSE
;
278 m_ownsPrimarySelection
= FALSE
;
280 m_data
= (wxDataObject
*) NULL
;
281 m_receivedData
= (wxDataObject
*) NULL
;
283 /* we use m_targetsWidget to query what formats are available */
285 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
286 gtk_widget_realize( m_targetsWidget
);
288 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
289 "selection_received",
290 GTK_SIGNAL_FUNC( targets_selection_received
),
293 /* we use m_clipboardWidget to get and to offer data */
295 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
296 gtk_widget_realize( m_clipboardWidget
);
298 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
299 "selection_received",
300 GTK_SIGNAL_FUNC( selection_received
),
303 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
304 "selection_clear_event",
305 GTK_SIGNAL_FUNC( selection_clear_clip
),
308 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
309 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
311 m_formatSupported
= FALSE
;
312 m_targetRequested
= 0;
314 m_usePrimary
= FALSE
;
317 wxClipboard::~wxClipboard()
321 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
322 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
325 void wxClipboard::Clear()
330 /* disable GUI threads */
333 // As we have data we also own the clipboard. Once we no longer own
334 // it, clear_selection is called which will set m_data to zero
335 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
339 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
340 (guint32
) GDK_CURRENT_TIME
);
342 while (m_waiting
) gtk_main_iteration();
345 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
349 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
350 (guint32
) GDK_CURRENT_TIME
);
352 while (m_waiting
) gtk_main_iteration();
358 m_data
= (wxDataObject
*) NULL
;
362 /* re-enable GUI threads */
366 m_targetRequested
= 0;
367 m_formatSupported
= FALSE
;
370 bool wxClipboard::Open()
372 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
379 bool wxClipboard::SetData( wxDataObject
*data
)
381 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
383 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
387 return AddData( data
);
390 bool wxClipboard::AddData( wxDataObject
*data
)
392 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
394 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
396 // we can only store one wxDataObject
401 // get formats from wxDataObjects
402 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
403 m_data
->GetAllFormats( array
);
405 // primary selection or clipboard
406 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
410 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
412 wxLogTrace( TRACE_CLIPBOARD
,
413 wxT("wxClipboard now supports atom %s"),
414 array
[i
].GetId().c_str() );
416 // printf( "added %s\n",
417 // gdk_atom_name( array[i].GetFormatId() ) );
419 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
422 0 ); /* what is info ? */
427 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
429 GTK_SIGNAL_FUNC(selection_handler
),
433 /* disable GUI threads */
436 /* Tell the world we offer clipboard data */
437 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
439 (guint32
) GDK_CURRENT_TIME
));
442 m_ownsPrimarySelection
= res
;
444 m_ownsClipboard
= res
;
447 /* re-enable GUI threads */
453 void wxClipboard::Close()
455 wxCHECK_RET( m_open
, wxT("clipboard not open") );
460 bool wxClipboard::IsOpened() const
465 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
467 /* reentrance problems */
468 if (m_waiting
) return FALSE
;
470 /* store requested format to be asked for by callbacks */
471 m_targetRequested
= format
;
474 wxLogTrace( TRACE_CLIPBOARD
,
475 wxT("wxClipboard:IsSupported: requested format: %s"),
476 format
.GetId().c_str() );
479 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
481 m_formatSupported
= FALSE
;
483 /* perform query. this will set m_formatSupported to
484 TRUE if m_targetRequested is supported.
485 also, we have to wait for the "answer" from the
486 clipboard owner which is an asynchronous process.
487 therefore we set m_waiting = TRUE here and wait
488 until the callback "targets_selection_received"
493 gtk_selection_convert( m_targetsWidget
,
494 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
497 (guint32
) GDK_CURRENT_TIME
);
499 while (m_waiting
) gtk_main_iteration();
501 if (!m_formatSupported
) return FALSE
;
506 bool wxClipboard::GetData( wxDataObject
& data
)
508 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
510 /* get formats from wxDataObjects */
511 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
512 data
.GetAllFormats( array
);
514 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
516 wxDataFormat
format( array
[i
] );
518 wxLogTrace( TRACE_CLIPBOARD
,
519 wxT("wxClipboard::GetData: requested format: %s"),
520 format
.GetId().c_str() );
522 /* is data supported by clipboard ? */
524 /* store requested format to be asked for by callbacks */
525 m_targetRequested
= format
;
527 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
529 m_formatSupported
= FALSE
;
531 /* perform query. this will set m_formatSupported to
532 TRUE if m_targetRequested is supported.
533 also, we have to wait for the "answer" from the
534 clipboard owner which is an asynchronous process.
535 therefore we set m_waiting = TRUE here and wait
536 until the callback "targets_selection_received"
541 gtk_selection_convert( m_targetsWidget
,
542 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
545 (guint32
) GDK_CURRENT_TIME
);
547 while (m_waiting
) gtk_main_iteration();
549 if (!m_formatSupported
) continue;
551 /* store pointer to data object to be filled up by callbacks */
552 m_receivedData
= &data
;
554 /* store requested format to be asked for by callbacks */
555 m_targetRequested
= format
;
557 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
560 m_formatSupported
= FALSE
;
562 /* ask for clipboard contents. this will set
563 m_formatSupported to TRUE if m_targetRequested
565 also, we have to wait for the "answer" from the
566 clipboard owner which is an asynchronous process.
567 therefore we set m_waiting = TRUE here and wait
568 until the callback "targets_selection_received"
573 wxLogTrace( TRACE_CLIPBOARD
,
574 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 wxLogTrace( TRACE_CLIPBOARD
,
593 wxT("wxClipboard::GetData: format not found") );