1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/clipbrd.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "clipbrd.h"
14 #include "wx/clipbrd.h"
18 #include "wx/dataobj.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
31 extern void wxapp_install_thread_wakeup();
32 extern void wxapp_uninstall_thread_wakeup();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 GdkAtom g_clipboardAtom
= 0;
40 GdkAtom g_targetsAtom
= 0;
42 // the trace mask we use with wxLogTrace() - call
43 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
44 // (there will be a *lot* of them!)
45 static const char *TRACE_CLIPBOARD
= _T("clipboard");
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 /* The contents of a selection are returned in a GtkSelectionData
52 structure. selection/target identify the request.
53 type specifies the type of the return; if length < 0, and
54 the data should be ignored. This structure has object semantics -
55 no fields should be modified directly, they should not be created
56 directly, and pointers to them should not be stored beyond the duration of
57 a callback. (If the last is changed, we'll need to add reference
60 struct _GtkSelectionData
72 //-----------------------------------------------------------------------------
73 // "selection_received" for targets
74 //-----------------------------------------------------------------------------
77 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
78 GtkSelectionData
*selection_data
,
79 #if (GTK_MINOR_VERSION > 0)
80 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 if (format
== clipboard
->m_targetRequested
)
120 clipboard
->m_waiting
= FALSE
;
121 clipboard
->m_formatSupported
= TRUE
;
127 clipboard
->m_waiting
= FALSE
;
130 //-----------------------------------------------------------------------------
131 // "selection_received" for the actual data
132 //-----------------------------------------------------------------------------
135 selection_received( GtkWidget
*WXUNUSED(widget
),
136 GtkSelectionData
*selection_data
,
137 #if (GTK_MINOR_VERSION > 0)
138 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
;
171 /* make sure we got the data in the correct form (selection type).
172 if so, copy data to target object */
173 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
175 clipboard
->m_waiting
= FALSE
;
179 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
181 wxTheClipboard
->m_formatSupported
= TRUE
;
182 clipboard
->m_waiting
= FALSE
;
185 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
190 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
192 if (!wxTheClipboard
) return TRUE
;
194 if (event
->selection
== GDK_SELECTION_PRIMARY
)
196 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
199 if (event
->selection
== g_clipboardAtom
)
201 wxTheClipboard
->m_ownsClipboard
= FALSE
;
205 wxTheClipboard
->m_waiting
= FALSE
;
209 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
210 (!wxTheClipboard
->m_ownsClipboard
))
212 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
213 if (wxTheClipboard
->m_data
)
215 wxLogDebug( wxT("wxClipboard will get cleared" ) );
217 delete wxTheClipboard
->m_data
;
218 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
222 wxTheClipboard
->m_waiting
= FALSE
;
226 //-----------------------------------------------------------------------------
227 // selection handler for supplying data
228 //-----------------------------------------------------------------------------
231 selection_handler( GtkWidget
*WXUNUSED(widget
),
232 GtkSelectionData
*selection_data
,
233 guint
WXUNUSED(info
),
234 guint
WXUNUSED(time
),
235 gpointer
WXUNUSED(data
) )
237 if (!wxTheClipboard
) return;
239 if (!wxTheClipboard
->m_data
) return;
241 wxDataObject
*data
= wxTheClipboard
->m_data
;
243 wxDataFormat
format( selection_data
->target
);
245 if (!data
->IsSupportedFormat( format
)) return;
247 /* this will fail for composite formats */
248 if (format
.GetType() == wxDF_TEXT
)
250 wxTextDataObject
*text_object
= (wxTextDataObject
*) data
;
251 wxString
text( text_object
->GetText() );
254 const wxWX2MBbuf s
= text
.mbc_str();
256 #else // more efficient in non-Unicode
257 const char *s
= text
.c_str();
258 int len
= (int) text
.Length();
260 gtk_selection_data_set(
262 GDK_SELECTION_TYPE_STRING
,
264 (unsigned char*) (const char*) s
,
270 int size
= data
->GetDataSize( format
);
272 if (size
== 0) return;
274 char *d
= new char[size
];
276 data
->GetDataHere( selection_data
->target
, (void*) d
);
278 gtk_selection_data_set(
280 GDK_SELECTION_TYPE_STRING
,
286 //-----------------------------------------------------------------------------
288 //-----------------------------------------------------------------------------
290 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
292 wxClipboard::wxClipboard()
296 m_ownsClipboard
= FALSE
;
297 m_ownsPrimarySelection
= FALSE
;
299 m_data
= (wxDataObject
*) NULL
;
300 m_receivedData
= (wxDataObject
*) NULL
;
302 /* we use m_targetsWidget to query what formats are available */
304 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
305 gtk_widget_realize( m_targetsWidget
);
307 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
308 "selection_received",
309 GTK_SIGNAL_FUNC( targets_selection_received
),
312 /* we use m_clipboardWidget to get and to offer data */
314 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
315 gtk_widget_realize( m_clipboardWidget
);
317 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
318 "selection_received",
319 GTK_SIGNAL_FUNC( selection_received
),
322 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
323 "selection_clear_event",
324 GTK_SIGNAL_FUNC( selection_clear_clip
),
327 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
328 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
330 m_formatSupported
= FALSE
;
331 m_targetRequested
= 0;
333 m_usePrimary
= FALSE
;
336 wxClipboard::~wxClipboard()
340 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
341 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
344 void wxClipboard::Clear()
349 /* disable GUI threads */
350 wxapp_uninstall_thread_wakeup();
353 /* As we have data we also own the clipboard. Once we no longer own
354 it, clear_selection is called which will set m_data to zero */
355 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
359 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
360 (guint32
) GDK_CURRENT_TIME
);
362 while (m_waiting
) gtk_main_iteration();
365 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
369 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
370 (guint32
) GDK_CURRENT_TIME
);
372 while (m_waiting
) gtk_main_iteration();
378 m_data
= (wxDataObject
*) NULL
;
382 /* re-enable GUI threads */
383 wxapp_install_thread_wakeup();
387 m_targetRequested
= 0;
388 m_formatSupported
= FALSE
;
391 bool wxClipboard::Open()
393 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
400 bool wxClipboard::SetData( wxDataObject
*data
)
402 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
404 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
408 return AddData( data
);
411 bool wxClipboard::AddData( wxDataObject
*data
)
413 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
415 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
417 /* we can only store one wxDataObject */
422 /* get formats from wxDataObjects */
423 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
424 m_data
->GetAllFormats( array
);
426 /* primary selection or clipboard */
427 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
431 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
433 wxLogTrace( TRACE_CLIPBOARD
,
434 wxT("wxClipboard now supports atom %s"),
435 array
[i
].GetId().c_str() );
437 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
440 0 ); /* what is info ? */
445 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
447 GTK_SIGNAL_FUNC(selection_handler
),
451 /* disable GUI threads */
452 wxapp_uninstall_thread_wakeup();
455 /* Tell the world we offer clipboard data */
456 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
458 (guint32
) GDK_CURRENT_TIME
));
461 m_ownsPrimarySelection
= res
;
463 m_ownsClipboard
= res
;
466 /* re-enable GUI threads */
467 wxapp_install_thread_wakeup();
473 void wxClipboard::Close()
475 wxCHECK_RET( m_open
, wxT("clipboard not open") );
480 bool wxClipboard::IsOpened() const
485 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
487 /* reentrance problems */
488 if (m_waiting
) return FALSE
;
490 /* store requested format to be asked for by callbacks */
491 m_targetRequested
= format
;
493 wxLogTrace( TRACE_CLIPBOARD
,
494 wxT("wxClipboard:IsSupported: requested format: %s"),
495 format
.GetId().c_str() );
497 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
499 m_formatSupported
= FALSE
;
501 /* perform query. this will set m_formatSupported to
502 TRUE if m_targetRequested is supported.
503 also, we have to wait for the "answer" from the
504 clipboard owner which is an asynchronous process.
505 therefore we set m_waiting = TRUE here and wait
506 until the callback "targets_selection_received"
511 gtk_selection_convert( m_targetsWidget
,
512 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
515 (guint32
) GDK_CURRENT_TIME
);
517 while (m_waiting
) gtk_main_iteration();
519 if (!m_formatSupported
) return FALSE
;
524 bool wxClipboard::GetData( wxDataObject
& data
)
526 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
528 /* get formats from wxDataObjects */
529 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
530 data
.GetAllFormats( array
);
532 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
534 wxDataFormat
format( array
[i
] );
536 wxLogTrace( TRACE_CLIPBOARD
,
537 wxT("wxClipboard::GetData: requested format: %s"),
538 format
.GetId().c_str() );
540 /* is data supported by clipboard ? */
542 /* store requested format to be asked for by callbacks */
543 m_targetRequested
= format
;
545 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
547 m_formatSupported
= FALSE
;
549 /* perform query. this will set m_formatSupported to
550 TRUE if m_targetRequested is supported.
551 also, we have to wait for the "answer" from the
552 clipboard owner which is an asynchronous process.
553 therefore we set m_waiting = TRUE here and wait
554 until the callback "targets_selection_received"
559 gtk_selection_convert( m_targetsWidget
,
560 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
563 (guint32
) GDK_CURRENT_TIME
);
565 while (m_waiting
) gtk_main_iteration();
567 if (!m_formatSupported
) continue;
569 /* store pointer to data object to be filled up by callbacks */
570 m_receivedData
= &data
;
572 /* store requested format to be asked for by callbacks */
573 m_targetRequested
= format
;
575 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
578 m_formatSupported
= FALSE
;
580 /* ask for clipboard contents. this will set
581 m_formatSupported to TRUE if m_targetRequested
583 also, we have to wait for the "answer" from the
584 clipboard owner which is an asynchronous process.
585 therefore we set m_waiting = TRUE here and wait
586 until the callback "targets_selection_received"
591 wxLogDebug( wxT("wxClipboard::GetData: format found, start convert") );
593 gtk_selection_convert( m_clipboardWidget
,
594 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
597 (guint32
) GDK_CURRENT_TIME
);
599 while (m_waiting
) gtk_main_iteration();
601 /* this is a true error as we checked for the presence of such data before */
602 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
609 wxLogDebug( wxT("wxClipboard::GetData: format not found") );