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 #if defined(__WXGTK20__) && wxUSE_UNICODE
40 extern GdkAtom g_altTextAtom
;
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 //-----------------------------------------------------------------------------
79 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
80 GtkSelectionData
*selection_data
,
81 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 // printf( "format %s requested %s\n",
119 // gdk_atom_name( atoms[i] ),
120 // gdk_atom_name( clipboard->m_targetRequested ) );
122 if (format
== clipboard
->m_targetRequested
)
124 clipboard
->m_waiting
= FALSE
;
125 clipboard
->m_formatSupported
= TRUE
;
131 clipboard
->m_waiting
= FALSE
;
135 //-----------------------------------------------------------------------------
136 // "selection_received" for the actual data
137 //-----------------------------------------------------------------------------
141 selection_received( GtkWidget
*WXUNUSED(widget
),
142 GtkSelectionData
*selection_data
,
143 guint32
WXUNUSED(time
),
144 wxClipboard
*clipboard
)
148 clipboard
->m_waiting
= FALSE
;
152 wxDataObject
*data_object
= clipboard
->m_receivedData
;
156 clipboard
->m_waiting
= FALSE
;
160 if (selection_data
->length
<= 0)
162 clipboard
->m_waiting
= FALSE
;
166 wxDataFormat
format( selection_data
->target
);
168 // make sure we got the data in the correct format
169 if (!data_object
->IsSupportedFormat( format
) )
171 clipboard
->m_waiting
= FALSE
;
176 This seems to cause problems somehow
177 // make sure we got the data in the correct form (selection type).
178 // if so, copy data to target object
179 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
181 clipboard
->m_waiting
= FALSE
;
186 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
188 wxTheClipboard
->m_formatSupported
= TRUE
;
189 clipboard
->m_waiting
= FALSE
;
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
199 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
201 if (!wxTheClipboard
) return TRUE
;
203 if (event
->selection
== GDK_SELECTION_PRIMARY
)
205 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
208 if (event
->selection
== g_clipboardAtom
)
210 wxTheClipboard
->m_ownsClipboard
= FALSE
;
214 wxTheClipboard
->m_waiting
= FALSE
;
218 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
219 (!wxTheClipboard
->m_ownsClipboard
))
221 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
222 if (wxTheClipboard
->m_data
)
224 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
226 delete wxTheClipboard
->m_data
;
227 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
231 wxTheClipboard
->m_waiting
= FALSE
;
236 //-----------------------------------------------------------------------------
237 // selection handler for supplying data
238 //-----------------------------------------------------------------------------
242 selection_handler( GtkWidget
*WXUNUSED(widget
),
243 GtkSelectionData
*selection_data
,
244 guint
WXUNUSED(info
),
245 guint
WXUNUSED(time
),
246 gpointer
WXUNUSED(data
) )
248 if (!wxTheClipboard
) return;
250 if (!wxTheClipboard
->m_data
) return;
252 wxDataObject
*data
= wxTheClipboard
->m_data
;
254 wxDataFormat
format( selection_data
->target
);
257 wxLogTrace(TRACE_CLIPBOARD
,
258 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s"),
259 format
.GetId().c_str(),
260 wxString::FromAscii(gdk_atom_name(selection_data
->target
)).c_str(),
261 wxString::FromAscii(gdk_atom_name(selection_data
->type
)).c_str(),
262 wxString::FromAscii(gdk_atom_name(selection_data
->selection
)).c_str()
266 if (!data
->IsSupportedFormat( format
)) return;
268 int size
= data
->GetDataSize( format
);
270 if (size
== 0) return;
272 void *d
= malloc(size
);
274 // Text data will be in UTF8 in Unicode mode.
275 data
->GetDataHere( selection_data
->target
, d
);
278 // NB: GTK+ requires special treatment of UTF8_STRING data, the text
279 // would show as UTF-8 data interpreted as latin1 (?) in other
280 // GTK+ apps if we used gtk_selection_data_set()
281 if (format
== wxDataFormat(wxDF_UNICODETEXT
))
283 gtk_selection_data_set_text(
291 gtk_selection_data_set(
293 GDK_SELECTION_TYPE_STRING
,
303 //-----------------------------------------------------------------------------
305 //-----------------------------------------------------------------------------
307 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
309 wxClipboard::wxClipboard()
314 m_ownsClipboard
= FALSE
;
315 m_ownsPrimarySelection
= FALSE
;
317 m_data
= (wxDataObject
*) NULL
;
318 m_receivedData
= (wxDataObject
*) NULL
;
320 /* we use m_targetsWidget to query what formats are available */
322 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
323 gtk_widget_realize( m_targetsWidget
);
325 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
326 "selection_received",
327 GTK_SIGNAL_FUNC( targets_selection_received
),
330 /* we use m_clipboardWidget to get and to offer data */
332 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
333 gtk_widget_realize( m_clipboardWidget
);
335 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
336 "selection_received",
337 GTK_SIGNAL_FUNC( selection_received
),
340 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
341 "selection_clear_event",
342 GTK_SIGNAL_FUNC( selection_clear_clip
),
345 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
346 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
348 m_formatSupported
= FALSE
;
349 m_targetRequested
= 0;
351 m_usePrimary
= FALSE
;
354 wxClipboard::~wxClipboard()
358 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
359 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
362 void wxClipboard::Clear()
367 /* disable GUI threads */
370 // As we have data we also own the clipboard. Once we no longer own
371 // it, clear_selection is called which will set m_data to zero
372 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
376 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
377 (guint32
) GDK_CURRENT_TIME
);
379 while (m_waiting
) gtk_main_iteration();
382 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
386 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
387 (guint32
) GDK_CURRENT_TIME
);
389 while (m_waiting
) gtk_main_iteration();
395 m_data
= (wxDataObject
*) NULL
;
399 /* re-enable GUI threads */
403 m_targetRequested
= 0;
404 m_formatSupported
= FALSE
;
407 bool wxClipboard::Open()
409 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
416 bool wxClipboard::SetData( wxDataObject
*data
)
418 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
420 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
424 return AddData( data
);
427 bool wxClipboard::AddData( wxDataObject
*data
)
429 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
431 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
433 // we can only store one wxDataObject
438 // get formats from wxDataObjects
439 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
440 m_data
->GetAllFormats( array
);
442 // primary selection or clipboard
443 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
447 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
449 wxLogTrace( TRACE_CLIPBOARD
,
450 wxT("wxClipboard now supports atom %s"),
451 array
[i
].GetId().c_str() );
453 // printf( "added %s\n",
454 // gdk_atom_name( array[i].GetFormatId() ) );
456 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
459 0 ); /* what is info ? */
464 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
466 GTK_SIGNAL_FUNC(selection_handler
),
470 /* disable GUI threads */
473 /* Tell the world we offer clipboard data */
474 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
476 (guint32
) GDK_CURRENT_TIME
));
479 m_ownsPrimarySelection
= res
;
481 m_ownsClipboard
= res
;
484 /* re-enable GUI threads */
490 void wxClipboard::Close()
492 wxCHECK_RET( m_open
, wxT("clipboard not open") );
497 bool wxClipboard::IsOpened() const
502 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
504 /* reentrance problems */
505 if (m_waiting
) return FALSE
;
507 /* store requested format to be asked for by callbacks */
508 m_targetRequested
= format
;
510 wxLogTrace( TRACE_CLIPBOARD
,
511 wxT("wxClipboard:IsSupported: requested format: %s"),
512 format
.GetId().c_str() );
514 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
516 m_formatSupported
= FALSE
;
518 /* perform query. this will set m_formatSupported to
519 TRUE if m_targetRequested is supported.
520 also, we have to wait for the "answer" from the
521 clipboard owner which is an asynchronous process.
522 therefore we set m_waiting = TRUE here and wait
523 until the callback "targets_selection_received"
528 gtk_selection_convert( m_targetsWidget
,
529 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
532 (guint32
) GDK_CURRENT_TIME
);
534 while (m_waiting
) gtk_main_iteration();
536 #if defined(__WXGTK20__) && wxUSE_UNICODE
537 if (!m_formatSupported
&& format
== wxDataFormat(wxDF_UNICODETEXT
))
539 // Another try with plain STRING format
540 extern GdkAtom g_altTextAtom
;
541 return IsSupported(g_altTextAtom
);
545 return m_formatSupported
;
548 bool wxClipboard::GetData( wxDataObject
& data
)
550 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
552 /* get formats from wxDataObjects */
553 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
554 data
.GetAllFormats( array
);
556 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
558 wxDataFormat
format( array
[i
] );
560 wxLogTrace( TRACE_CLIPBOARD
,
561 wxT("wxClipboard::GetData: requested format: %s"),
562 format
.GetId().c_str() );
564 /* is data supported by clipboard ? */
566 /* store requested format to be asked for by callbacks */
567 m_targetRequested
= format
;
569 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
571 m_formatSupported
= FALSE
;
573 /* perform query. this will set m_formatSupported to
574 TRUE if m_targetRequested is supported.
575 also, we have to wait for the "answer" from the
576 clipboard owner which is an asynchronous process.
577 therefore we set m_waiting = TRUE here and wait
578 until the callback "targets_selection_received"
583 gtk_selection_convert( m_targetsWidget
,
584 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
587 (guint32
) GDK_CURRENT_TIME
);
589 while (m_waiting
) gtk_main_iteration();
591 if (!m_formatSupported
) continue;
593 /* store pointer to data object to be filled up by callbacks */
594 m_receivedData
= &data
;
596 /* store requested format to be asked for by callbacks */
597 m_targetRequested
= format
;
599 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
602 m_formatSupported
= FALSE
;
604 /* ask for clipboard contents. this will set
605 m_formatSupported to TRUE if m_targetRequested
607 also, we have to wait for the "answer" from the
608 clipboard owner which is an asynchronous process.
609 therefore we set m_waiting = TRUE here and wait
610 until the callback "targets_selection_received"
615 wxLogTrace( TRACE_CLIPBOARD
,
616 wxT("wxClipboard::GetData: format found, start convert") );
618 gtk_selection_convert( m_clipboardWidget
,
619 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
622 (guint32
) GDK_CURRENT_TIME
);
624 while (m_waiting
) gtk_main_iteration();
626 /* this is a true error as we checked for the presence of such data before */
627 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
634 wxLogTrace( TRACE_CLIPBOARD
,
635 wxT("wxClipboard::GetData: format not found") );