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;
38 GdkAtom g_timestampAtom
= 0;
40 #if defined(__WXGTK20__) && wxUSE_UNICODE
41 extern GdkAtom g_altTextAtom
;
44 // the trace mask we use with wxLogTrace() - call
45 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
46 // (there will be a *lot* of them!)
47 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 /* The contents of a selection are returned in a GtkSelectionData
54 structure. selection/target identify the request.
55 type specifies the type of the return; if length < 0, and
56 the data should be ignored. This structure has object semantics -
57 no fields should be modified directly, they should not be created
58 directly, and pointers to them should not be stored beyond the duration of
59 a callback. (If the last is changed, we'll need to add reference
62 struct _GtkSelectionData
74 //-----------------------------------------------------------------------------
75 // "selection_received" for targets
76 //-----------------------------------------------------------------------------
80 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
81 GtkSelectionData
*selection_data
,
82 guint32
WXUNUSED(time
),
83 wxClipboard
*clipboard
)
85 if ( wxTheClipboard
&& selection_data
->length
> 0 )
87 // make sure we got the data in the correct form
88 GdkAtom type
= selection_data
->type
;
89 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
91 gchar
* atom_name
= gdk_atom_name(type
);
92 if ( strcmp(atom_name
, "TARGETS") )
94 wxLogTrace( TRACE_CLIPBOARD
,
95 _T("got unsupported clipboard target") );
97 clipboard
->m_waiting
= FALSE
;
105 wxDataFormat
clip( selection_data
->selection
);
106 wxLogTrace( TRACE_CLIPBOARD
,
107 wxT("selection received for targets, clipboard %s"),
108 clip
.GetId().c_str() );
109 #endif // __WXDEBUG__
111 // the atoms we received, holding a list of targets (= formats)
112 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
114 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
116 wxDataFormat
format( atoms
[i
] );
118 wxLogTrace( TRACE_CLIPBOARD
,
119 wxT("selection received for targets, format %s"),
120 format
.GetId().c_str() );
122 // printf( "format %s requested %s\n",
123 // gdk_atom_name( atoms[i] ),
124 // gdk_atom_name( clipboard->m_targetRequested ) );
126 if (format
== clipboard
->m_targetRequested
)
128 clipboard
->m_waiting
= FALSE
;
129 clipboard
->m_formatSupported
= TRUE
;
135 clipboard
->m_waiting
= FALSE
;
139 //-----------------------------------------------------------------------------
140 // "selection_received" for the actual data
141 //-----------------------------------------------------------------------------
145 selection_received( GtkWidget
*WXUNUSED(widget
),
146 GtkSelectionData
*selection_data
,
147 guint32
WXUNUSED(time
),
148 wxClipboard
*clipboard
)
152 clipboard
->m_waiting
= FALSE
;
156 wxDataObject
*data_object
= clipboard
->m_receivedData
;
160 clipboard
->m_waiting
= FALSE
;
164 if (selection_data
->length
<= 0)
166 clipboard
->m_waiting
= FALSE
;
170 wxDataFormat
format( selection_data
->target
);
172 // make sure we got the data in the correct format
173 if (!data_object
->IsSupportedFormat( format
) )
175 clipboard
->m_waiting
= FALSE
;
180 This seems to cause problems somehow
181 // make sure we got the data in the correct form (selection type).
182 // if so, copy data to target object
183 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
185 clipboard
->m_waiting
= FALSE
;
190 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
192 wxTheClipboard
->m_formatSupported
= TRUE
;
193 clipboard
->m_waiting
= FALSE
;
197 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
203 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
205 if (!wxTheClipboard
) return TRUE
;
207 if (event
->selection
== GDK_SELECTION_PRIMARY
)
209 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
212 if (event
->selection
== g_clipboardAtom
)
214 wxTheClipboard
->m_ownsClipboard
= FALSE
;
218 wxTheClipboard
->m_waiting
= FALSE
;
222 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
223 (!wxTheClipboard
->m_ownsClipboard
))
225 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
226 if (wxTheClipboard
->m_data
)
228 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
230 delete wxTheClipboard
->m_data
;
231 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
235 wxTheClipboard
->m_waiting
= FALSE
;
240 //-----------------------------------------------------------------------------
241 // selection handler for supplying data
242 //-----------------------------------------------------------------------------
246 selection_handler( GtkWidget
*WXUNUSED(widget
),
247 GtkSelectionData
*selection_data
,
248 guint
WXUNUSED(info
),
249 guint
WXUNUSED(time
),
250 gpointer signal_data
)
252 if (!wxTheClipboard
) return;
254 if (!wxTheClipboard
->m_data
) return;
256 wxDataObject
*data
= wxTheClipboard
->m_data
;
258 // ICCCM says that TIMESTAMP is a required atom.
259 // In particular, it satisfies Klipper, which polls
260 // TIMESTAMP to see if the clipboards content has changed.
261 // It shall return the time which was used to set the data.
262 if (selection_data
->target
== g_timestampAtom
)
264 uint timestamp
= GPOINTER_TO_UINT (signal_data
);
265 gtk_selection_data_set(selection_data
,
266 GDK_SELECTION_TYPE_INTEGER
,
268 (guchar
*)&(timestamp
),
270 wxLogTrace(TRACE_CLIPBOARD
,
271 _T("Clipboard TIMESTAMP requested, returning timestamp=%u"),
276 wxDataFormat
format( selection_data
->target
);
279 wxLogTrace(TRACE_CLIPBOARD
,
280 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s timestamp=%u"),
281 format
.GetId().c_str(),
282 wxString::FromAscii(gdk_atom_name(selection_data
->target
)).c_str(),
283 wxString::FromAscii(gdk_atom_name(selection_data
->type
)).c_str(),
284 wxString::FromAscii(gdk_atom_name(selection_data
->selection
)).c_str(),
285 GPOINTER_TO_UINT( signal_data
)
289 if (!data
->IsSupportedFormat( format
)) return;
291 int size
= data
->GetDataSize( format
);
293 if (size
== 0) return;
295 void *d
= malloc(size
);
297 // Text data will be in UTF8 in Unicode mode.
298 data
->GetDataHere( selection_data
->target
, d
);
300 // NB: GTK+ requires special treatment of UTF8_STRING data, the text
301 // would show as UTF-8 data interpreted as latin1 (?) in other
302 // GTK+ apps if we used gtk_selection_data_set()
303 if (format
== wxDataFormat(wxDF_UNICODETEXT
))
305 gtk_selection_data_set_text(
312 gtk_selection_data_set(
314 GDK_SELECTION_TYPE_STRING
,
324 //-----------------------------------------------------------------------------
326 //-----------------------------------------------------------------------------
328 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
330 wxClipboard::wxClipboard()
335 m_ownsClipboard
= FALSE
;
336 m_ownsPrimarySelection
= FALSE
;
338 m_data
= (wxDataObject
*) NULL
;
339 m_receivedData
= (wxDataObject
*) NULL
;
341 /* we use m_targetsWidget to query what formats are available */
343 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
344 gtk_widget_realize( m_targetsWidget
);
346 g_signal_connect (m_targetsWidget
, "selection_received",
347 G_CALLBACK (targets_selection_received
), this);
349 /* we use m_clipboardWidget to get and to offer data */
351 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
352 gtk_widget_realize( m_clipboardWidget
);
354 g_signal_connect (m_clipboardWidget
, "selection_received",
355 G_CALLBACK (selection_received
), this);
357 g_signal_connect (m_clipboardWidget
, "selection_clear_event",
358 G_CALLBACK (selection_clear_clip
), NULL
);
360 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
361 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
362 if (!g_timestampAtom
) g_timestampAtom
= gdk_atom_intern ("TIMESTAMP", FALSE
);
364 m_formatSupported
= FALSE
;
365 m_targetRequested
= 0;
367 m_usePrimary
= FALSE
;
370 wxClipboard::~wxClipboard()
374 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
375 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
378 void wxClipboard::Clear()
383 /* disable GUI threads */
386 // As we have data we also own the clipboard. Once we no longer own
387 // it, clear_selection is called which will set m_data to zero
388 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
392 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
393 (guint32
) GDK_CURRENT_TIME
);
395 while (m_waiting
) gtk_main_iteration();
398 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
402 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
403 (guint32
) GDK_CURRENT_TIME
);
405 while (m_waiting
) gtk_main_iteration();
411 m_data
= (wxDataObject
*) NULL
;
415 /* re-enable GUI threads */
419 m_targetRequested
= 0;
420 m_formatSupported
= FALSE
;
423 bool wxClipboard::Open()
425 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
432 bool wxClipboard::SetData( wxDataObject
*data
)
434 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
436 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
440 return AddData( data
);
443 bool wxClipboard::AddData( wxDataObject
*data
)
445 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
447 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
449 // we can only store one wxDataObject
454 // get formats from wxDataObjects
455 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
456 m_data
->GetAllFormats( array
);
458 // primary selection or clipboard
459 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
462 // by default provide TIMESTAMP as a target
463 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
468 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
470 wxLogTrace( TRACE_CLIPBOARD
,
471 wxT("wxClipboard now supports atom %s"),
472 array
[i
].GetId().c_str() );
474 // printf( "added %s\n",
475 // gdk_atom_name( array[i].GetFormatId() ) );
477 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
480 0 ); /* what is info ? */
485 g_signal_connect (m_clipboardWidget
, "selection_get",
486 G_CALLBACK (selection_handler
),
487 GUINT_TO_POINTER (gtk_get_current_event_time()) );
490 /* disable GUI threads */
493 /* Tell the world we offer clipboard data */
494 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
496 (guint32
) GDK_CURRENT_TIME
));
499 m_ownsPrimarySelection
= res
;
501 m_ownsClipboard
= res
;
504 /* re-enable GUI threads */
510 void wxClipboard::Close()
512 wxCHECK_RET( m_open
, wxT("clipboard not open") );
517 bool wxClipboard::IsOpened() const
522 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
524 /* reentrance problems */
525 if (m_waiting
) return FALSE
;
527 /* store requested format to be asked for by callbacks */
528 m_targetRequested
= format
;
530 wxLogTrace( TRACE_CLIPBOARD
,
531 wxT("wxClipboard:IsSupported: requested format: %s"),
532 format
.GetId().c_str() );
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 defined(__WXGTK20__) && wxUSE_UNICODE
557 if (!m_formatSupported
&& format
== wxDataFormat(wxDF_UNICODETEXT
))
559 // Another try with plain STRING format
560 extern GdkAtom g_altTextAtom
;
561 return IsSupported(g_altTextAtom
);
565 return m_formatSupported
;
568 bool wxClipboard::GetData( wxDataObject
& data
)
570 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
572 /* get formats from wxDataObjects */
573 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
574 data
.GetAllFormats( array
);
576 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
578 wxDataFormat
format( array
[i
] );
580 wxLogTrace( TRACE_CLIPBOARD
,
581 wxT("wxClipboard::GetData: requested format: %s"),
582 format
.GetId().c_str() );
584 /* is data supported by clipboard ? */
586 /* store requested format to be asked for by callbacks */
587 m_targetRequested
= format
;
589 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
591 m_formatSupported
= FALSE
;
593 /* perform query. this will set m_formatSupported to
594 TRUE if m_targetRequested is supported.
595 also, we have to wait for the "answer" from the
596 clipboard owner which is an asynchronous process.
597 therefore we set m_waiting = TRUE here and wait
598 until the callback "targets_selection_received"
603 gtk_selection_convert( m_targetsWidget
,
604 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
607 (guint32
) GDK_CURRENT_TIME
);
609 while (m_waiting
) gtk_main_iteration();
611 if (!m_formatSupported
) continue;
613 /* store pointer to data object to be filled up by callbacks */
614 m_receivedData
= &data
;
616 /* store requested format to be asked for by callbacks */
617 m_targetRequested
= format
;
619 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
622 m_formatSupported
= FALSE
;
624 /* ask for clipboard contents. this will set
625 m_formatSupported to TRUE if m_targetRequested
627 also, we have to wait for the "answer" from the
628 clipboard owner which is an asynchronous process.
629 therefore we set m_waiting = TRUE here and wait
630 until the callback "targets_selection_received"
635 wxLogTrace( TRACE_CLIPBOARD
,
636 wxT("wxClipboard::GetData: format found, start convert") );
638 gtk_selection_convert( m_clipboardWidget
,
639 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
642 (guint32
) GDK_CURRENT_TIME
);
644 while (m_waiting
) gtk_main_iteration();
646 /* this is a true error as we checked for the presence of such data before */
647 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
654 wxLogTrace( TRACE_CLIPBOARD
,
655 wxT("wxClipboard::GetData: format not found") );