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()
278 m_ownsClipboard
= FALSE
;
279 m_ownsPrimarySelection
= FALSE
;
281 m_data
= (wxDataObject
*) NULL
;
282 m_receivedData
= (wxDataObject
*) NULL
;
284 /* we use m_targetsWidget to query what formats are available */
286 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
287 gtk_widget_realize( m_targetsWidget
);
289 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
290 "selection_received",
291 GTK_SIGNAL_FUNC( targets_selection_received
),
294 /* we use m_clipboardWidget to get and to offer data */
296 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
297 gtk_widget_realize( m_clipboardWidget
);
299 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
300 "selection_received",
301 GTK_SIGNAL_FUNC( selection_received
),
304 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
305 "selection_clear_event",
306 GTK_SIGNAL_FUNC( selection_clear_clip
),
309 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
310 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
312 m_formatSupported
= FALSE
;
313 m_targetRequested
= 0;
315 m_usePrimary
= FALSE
;
318 wxClipboard::~wxClipboard()
322 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
323 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
326 void wxClipboard::Clear()
331 /* disable GUI threads */
334 // As we have data we also own the clipboard. Once we no longer own
335 // it, clear_selection is called which will set m_data to zero
336 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
340 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
341 (guint32
) GDK_CURRENT_TIME
);
343 while (m_waiting
) gtk_main_iteration();
346 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
350 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
351 (guint32
) GDK_CURRENT_TIME
);
353 while (m_waiting
) gtk_main_iteration();
359 m_data
= (wxDataObject
*) NULL
;
363 /* re-enable GUI threads */
367 m_targetRequested
= 0;
368 m_formatSupported
= FALSE
;
371 bool wxClipboard::Open()
373 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
380 bool wxClipboard::SetData( wxDataObject
*data
)
382 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
384 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
388 return AddData( data
);
391 bool wxClipboard::AddData( wxDataObject
*data
)
393 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
395 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
397 // we can only store one wxDataObject
402 // get formats from wxDataObjects
403 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
404 m_data
->GetAllFormats( array
);
406 // primary selection or clipboard
407 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
411 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
413 wxLogTrace( TRACE_CLIPBOARD
,
414 wxT("wxClipboard now supports atom %s"),
415 array
[i
].GetId().c_str() );
417 // printf( "added %s\n",
418 // gdk_atom_name( array[i].GetFormatId() ) );
420 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
423 0 ); /* what is info ? */
428 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
430 GTK_SIGNAL_FUNC(selection_handler
),
434 /* disable GUI threads */
437 /* Tell the world we offer clipboard data */
438 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
440 (guint32
) GDK_CURRENT_TIME
));
443 m_ownsPrimarySelection
= res
;
445 m_ownsClipboard
= res
;
448 /* re-enable GUI threads */
454 void wxClipboard::Close()
456 wxCHECK_RET( m_open
, wxT("clipboard not open") );
461 bool wxClipboard::IsOpened() const
466 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
468 /* reentrance problems */
469 if (m_waiting
) return FALSE
;
471 /* store requested format to be asked for by callbacks */
472 m_targetRequested
= format
;
475 wxLogTrace( TRACE_CLIPBOARD
,
476 wxT("wxClipboard:IsSupported: requested format: %s"),
477 format
.GetId().c_str() );
480 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
482 m_formatSupported
= FALSE
;
484 /* perform query. this will set m_formatSupported to
485 TRUE if m_targetRequested is supported.
486 also, we have to wait for the "answer" from the
487 clipboard owner which is an asynchronous process.
488 therefore we set m_waiting = TRUE here and wait
489 until the callback "targets_selection_received"
494 gtk_selection_convert( m_targetsWidget
,
495 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
498 (guint32
) GDK_CURRENT_TIME
);
500 while (m_waiting
) gtk_main_iteration();
502 if (!m_formatSupported
) return FALSE
;
507 bool wxClipboard::GetData( wxDataObject
& data
)
509 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
511 /* get formats from wxDataObjects */
512 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
513 data
.GetAllFormats( array
);
515 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
517 wxDataFormat
format( array
[i
] );
519 wxLogTrace( TRACE_CLIPBOARD
,
520 wxT("wxClipboard::GetData: requested format: %s"),
521 format
.GetId().c_str() );
523 /* is data supported by clipboard ? */
525 /* store requested format to be asked for by callbacks */
526 m_targetRequested
= format
;
528 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
530 m_formatSupported
= FALSE
;
532 /* perform query. this will set m_formatSupported to
533 TRUE if m_targetRequested is supported.
534 also, we have to wait for the "answer" from the
535 clipboard owner which is an asynchronous process.
536 therefore we set m_waiting = TRUE here and wait
537 until the callback "targets_selection_received"
542 gtk_selection_convert( m_targetsWidget
,
543 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
546 (guint32
) GDK_CURRENT_TIME
);
548 while (m_waiting
) gtk_main_iteration();
550 if (!m_formatSupported
) continue;
552 /* store pointer to data object to be filled up by callbacks */
553 m_receivedData
= &data
;
555 /* store requested format to be asked for by callbacks */
556 m_targetRequested
= format
;
558 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
561 m_formatSupported
= FALSE
;
563 /* ask for clipboard contents. this will set
564 m_formatSupported to TRUE if m_targetRequested
566 also, we have to wait for the "answer" from the
567 clipboard owner which is an asynchronous process.
568 therefore we set m_waiting = TRUE here and wait
569 until the callback "targets_selection_received"
574 wxLogTrace( TRACE_CLIPBOARD
,
575 wxT("wxClipboard::GetData: format found, start convert") );
577 gtk_selection_convert( m_clipboardWidget
,
578 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
581 (guint32
) GDK_CURRENT_TIME
);
583 while (m_waiting
) gtk_main_iteration();
585 /* this is a true error as we checked for the presence of such data before */
586 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
593 wxLogTrace( TRACE_CLIPBOARD
,
594 wxT("wxClipboard::GetData: format not found") );