1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
15 #include "wx/clipbrd.h"
22 #include "wx/dataobj.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 GdkAtom g_clipboardAtom
= 0;
33 GdkAtom g_targetsAtom
= 0;
34 GdkAtom g_timestampAtom
= 0;
36 // the trace mask we use with wxLogTrace() - call
37 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
38 // (there will be a *lot* of them!)
39 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 /* The contents of a selection are returned in a GtkSelectionData
46 structure. selection/target identify the request.
47 type specifies the type of the return; if length < 0, and
48 the data should be ignored. This structure has object semantics -
49 no fields should be modified directly, they should not be created
50 directly, and pointers to them should not be stored beyond the duration of
51 a callback. (If the last is changed, we'll need to add reference
54 struct _GtkSelectionData
66 //-----------------------------------------------------------------------------
67 // "selection_received" for targets
68 //-----------------------------------------------------------------------------
72 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
73 GtkSelectionData
*selection_data
,
74 guint32
WXUNUSED(time
),
75 wxClipboard
*clipboard
)
77 if ( wxTheClipboard
&& selection_data
->length
> 0 )
79 // make sure we got the data in the correct form
80 GdkAtom type
= selection_data
->type
;
81 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
83 gchar
* atom_name
= gdk_atom_name(type
);
84 if ( strcmp(atom_name
, "TARGETS") )
86 wxLogTrace( TRACE_CLIPBOARD
,
87 _T("got unsupported clipboard target") );
89 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 signal_data
)
244 if (!wxTheClipboard
) return;
246 if (!wxTheClipboard
->m_data
) return;
248 wxDataObject
*data
= wxTheClipboard
->m_data
;
250 // ICCCM says that TIMESTAMP is a required atom.
251 // In particular, it satisfies Klipper, which polls
252 // TIMESTAMP to see if the clipboards content has changed.
253 // It shall return the time which was used to set the data.
254 if (selection_data
->target
== g_timestampAtom
)
256 guint timestamp
= GPOINTER_TO_UINT (signal_data
);
257 gtk_selection_data_set(selection_data
,
258 GDK_SELECTION_TYPE_INTEGER
,
260 (guchar
*)&(timestamp
),
262 wxLogTrace(TRACE_CLIPBOARD
,
263 _T("Clipboard TIMESTAMP requested, returning timestamp=%u"),
268 wxDataFormat
format( selection_data
->target
);
271 wxLogTrace(TRACE_CLIPBOARD
,
272 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s timestamp=%u"),
273 format
.GetId().c_str(),
274 wxString::FromAscii(gdk_atom_name(selection_data
->target
)).c_str(),
275 wxString::FromAscii(gdk_atom_name(selection_data
->type
)).c_str(),
276 wxString::FromAscii(gdk_atom_name(selection_data
->selection
)).c_str(),
277 GPOINTER_TO_UINT( signal_data
)
281 if (!data
->IsSupportedFormat( format
)) return;
283 int size
= data
->GetDataSize( format
);
285 if (size
== 0) return;
287 void *d
= malloc(size
);
289 // Text data will be in UTF8 in Unicode mode.
290 data
->GetDataHere( selection_data
->target
, d
);
292 gtk_selection_data_set(
294 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
);
347 if (!g_timestampAtom
) g_timestampAtom
= gdk_atom_intern ("TIMESTAMP", FALSE
);
349 m_formatSupported
= false;
350 m_targetRequested
= 0;
352 m_usePrimary
= false;
355 wxClipboard::~wxClipboard()
359 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
360 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
363 void wxClipboard::Clear()
368 /* disable GUI threads */
371 // As we have data we also own the clipboard. Once we no longer own
372 // it, clear_selection is called which will set m_data to zero
373 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
377 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
378 (guint32
) GDK_CURRENT_TIME
);
380 while (m_waiting
) gtk_main_iteration();
383 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
387 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
388 (guint32
) GDK_CURRENT_TIME
);
390 while (m_waiting
) gtk_main_iteration();
396 m_data
= (wxDataObject
*) NULL
;
400 /* re-enable GUI threads */
404 m_targetRequested
= 0;
405 m_formatSupported
= false;
408 bool wxClipboard::Open()
410 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
417 bool wxClipboard::SetData( wxDataObject
*data
)
419 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
421 wxCHECK_MSG( data
, false, wxT("data is invalid") );
425 return AddData( data
);
428 bool wxClipboard::AddData( wxDataObject
*data
)
430 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
432 wxCHECK_MSG( data
, false, wxT("data is invalid") );
434 // we can only store one wxDataObject
439 // get formats from wxDataObjects
440 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
441 m_data
->GetAllFormats( array
);
443 // primary selection or clipboard
444 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
447 // by default provide TIMESTAMP as a target
448 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
453 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
455 wxLogTrace( TRACE_CLIPBOARD
,
456 wxT("wxClipboard now supports atom %s"),
457 array
[i
].GetId().c_str() );
459 // printf( "added %s\n",
460 // gdk_atom_name( array[i].GetFormatId() ) );
462 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
465 0 ); /* what is info ? */
470 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
472 GTK_SIGNAL_FUNC(selection_handler
),
473 GUINT_TO_POINTER( gdk_event_get_time(gtk_get_current_event()) ) );
476 /* disable GUI threads */
479 /* Tell the world we offer clipboard data */
480 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
482 (guint32
) GDK_CURRENT_TIME
));
485 m_ownsPrimarySelection
= res
;
487 m_ownsClipboard
= res
;
490 /* re-enable GUI threads */
496 void wxClipboard::Close()
498 wxCHECK_RET( m_open
, wxT("clipboard not open") );
503 bool wxClipboard::IsOpened() const
508 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
510 /* reentrance problems */
511 if (m_waiting
) return false;
513 /* store requested format to be asked for by callbacks */
514 m_targetRequested
= format
;
516 wxLogTrace( TRACE_CLIPBOARD
,
517 wxT("wxClipboard:IsSupported: requested format: %s"),
518 format
.GetId().c_str() );
520 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
522 m_formatSupported
= false;
524 /* perform query. this will set m_formatSupported to
525 true if m_targetRequested is supported.
526 also, we have to wait for the "answer" from the
527 clipboard owner which is an asynchronous process.
528 therefore we set m_waiting = true here and wait
529 until the callback "targets_selection_received"
534 gtk_selection_convert( m_targetsWidget
,
535 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
538 (guint32
) GDK_CURRENT_TIME
);
540 while (m_waiting
) gtk_main_iteration();
542 return m_formatSupported
;
545 bool wxClipboard::GetData( wxDataObject
& data
)
547 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
549 /* get formats from wxDataObjects */
550 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
551 data
.GetAllFormats( array
);
553 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
555 wxDataFormat
format( array
[i
] );
557 wxLogTrace( TRACE_CLIPBOARD
,
558 wxT("wxClipboard::GetData: requested format: %s"),
559 format
.GetId().c_str() );
561 /* is data supported by clipboard ? */
563 /* store requested format to be asked for by callbacks */
564 m_targetRequested
= format
;
566 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
568 m_formatSupported
= false;
570 /* perform query. this will set m_formatSupported to
571 true if m_targetRequested is supported.
572 also, we have to wait for the "answer" from the
573 clipboard owner which is an asynchronous process.
574 therefore we set m_waiting = true here and wait
575 until the callback "targets_selection_received"
580 gtk_selection_convert( m_targetsWidget
,
581 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
584 (guint32
) GDK_CURRENT_TIME
);
586 while (m_waiting
) gtk_main_iteration();
588 if (!m_formatSupported
) continue;
590 /* store pointer to data object to be filled up by callbacks */
591 m_receivedData
= &data
;
593 /* store requested format to be asked for by callbacks */
594 m_targetRequested
= format
;
596 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
599 m_formatSupported
= false;
601 /* ask for clipboard contents. this will set
602 m_formatSupported to true if m_targetRequested
604 also, we have to wait for the "answer" from the
605 clipboard owner which is an asynchronous process.
606 therefore we set m_waiting = true here and wait
607 until the callback "targets_selection_received"
612 wxLogTrace( TRACE_CLIPBOARD
,
613 wxT("wxClipboard::GetData: format found, start convert") );
615 gtk_selection_convert( m_clipboardWidget
,
616 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
619 (guint32
) GDK_CURRENT_TIME
);
621 while (m_waiting
) gtk_main_iteration();
623 /* this is a true error as we checked for the presence of such data before */
624 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
631 wxLogTrace( TRACE_CLIPBOARD
,
632 wxT("wxClipboard::GetData: format not found") );