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 // the trace mask we use with wxLogTrace() - call
41 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
42 // (there will be a *lot* of them!)
43 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 /* The contents of a selection are returned in a GtkSelectionData
50 structure. selection/target identify the request.
51 type specifies the type of the return; if length < 0, and
52 the data should be ignored. This structure has object semantics -
53 no fields should be modified directly, they should not be created
54 directly, and pointers to them should not be stored beyond the duration of
55 a callback. (If the last is changed, we'll need to add reference
58 struct _GtkSelectionData
70 //-----------------------------------------------------------------------------
71 // "selection_received" for targets
72 //-----------------------------------------------------------------------------
76 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
77 GtkSelectionData
*selection_data
,
78 guint32
WXUNUSED(time
),
79 wxClipboard
*clipboard
)
81 if ( wxTheClipboard
&& selection_data
->length
> 0 )
83 // make sure we got the data in the correct form
84 GdkAtom type
= selection_data
->type
;
85 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
87 gchar
* atom_name
= gdk_atom_name(type
);
88 if ( strcmp(atom_name
, "TARGETS") )
90 wxLogTrace( TRACE_CLIPBOARD
,
91 _T("got unsupported clipboard target") );
93 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 signal_data
)
248 if (!wxTheClipboard
) return;
250 if (!wxTheClipboard
->m_data
) return;
252 wxDataObject
*data
= wxTheClipboard
->m_data
;
254 // ICCCM says that TIMESTAMP is a required atom.
255 // In particular, it satisfies Klipper, which polls
256 // TIMESTAMP to see if the clipboards content has changed.
257 // It shall return the time which was used to set the data.
258 if (selection_data
->target
== g_timestampAtom
)
260 guint timestamp
= GPOINTER_TO_UINT (signal_data
);
261 gtk_selection_data_set(selection_data
,
262 GDK_SELECTION_TYPE_INTEGER
,
264 (guchar
*)&(timestamp
),
266 wxLogTrace(TRACE_CLIPBOARD
,
267 _T("Clipboard TIMESTAMP requested, returning timestamp=%u"),
272 wxDataFormat
format( selection_data
->target
);
275 wxLogTrace(TRACE_CLIPBOARD
,
276 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s timestamp=%u"),
277 format
.GetId().c_str(),
278 wxString::FromAscii(gdk_atom_name(selection_data
->target
)).c_str(),
279 wxString::FromAscii(gdk_atom_name(selection_data
->type
)).c_str(),
280 wxString::FromAscii(gdk_atom_name(selection_data
->selection
)).c_str(),
281 GPOINTER_TO_UINT( signal_data
)
285 if (!data
->IsSupportedFormat( format
)) return;
287 int size
= data
->GetDataSize( format
);
289 if (size
== 0) return;
291 void *d
= malloc(size
);
293 // Text data will be in UTF8 in Unicode mode.
294 data
->GetDataHere( selection_data
->target
, d
);
296 gtk_selection_data_set(
298 GDK_SELECTION_TYPE_STRING
,
307 //-----------------------------------------------------------------------------
309 //-----------------------------------------------------------------------------
311 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
313 wxClipboard::wxClipboard()
318 m_ownsClipboard
= FALSE
;
319 m_ownsPrimarySelection
= FALSE
;
321 m_data
= (wxDataObject
*) NULL
;
322 m_receivedData
= (wxDataObject
*) NULL
;
324 /* we use m_targetsWidget to query what formats are available */
326 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
327 gtk_widget_realize( m_targetsWidget
);
329 gtk_signal_connect( GTK_OBJECT(m_targetsWidget
),
330 "selection_received",
331 GTK_SIGNAL_FUNC( targets_selection_received
),
334 /* we use m_clipboardWidget to get and to offer data */
336 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
337 gtk_widget_realize( m_clipboardWidget
);
339 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
340 "selection_received",
341 GTK_SIGNAL_FUNC( selection_received
),
344 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
345 "selection_clear_event",
346 GTK_SIGNAL_FUNC( selection_clear_clip
),
349 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
350 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
351 if (!g_timestampAtom
) g_timestampAtom
= gdk_atom_intern ("TIMESTAMP", FALSE
);
353 m_formatSupported
= FALSE
;
354 m_targetRequested
= 0;
356 m_usePrimary
= FALSE
;
359 wxClipboard::~wxClipboard()
363 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
364 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
367 void wxClipboard::Clear()
372 /* disable GUI threads */
375 // As we have data we also own the clipboard. Once we no longer own
376 // it, clear_selection is called which will set m_data to zero
377 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
381 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
382 (guint32
) GDK_CURRENT_TIME
);
384 while (m_waiting
) gtk_main_iteration();
387 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
391 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
392 (guint32
) GDK_CURRENT_TIME
);
394 while (m_waiting
) gtk_main_iteration();
400 m_data
= (wxDataObject
*) NULL
;
404 /* re-enable GUI threads */
408 m_targetRequested
= 0;
409 m_formatSupported
= FALSE
;
412 bool wxClipboard::Open()
414 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
421 bool wxClipboard::SetData( wxDataObject
*data
)
423 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
425 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
429 return AddData( data
);
432 bool wxClipboard::AddData( wxDataObject
*data
)
434 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
436 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
438 // we can only store one wxDataObject
443 // get formats from wxDataObjects
444 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
445 m_data
->GetAllFormats( array
);
447 // primary selection or clipboard
448 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
451 // by default provide TIMESTAMP as a target
452 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
457 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
459 wxLogTrace( TRACE_CLIPBOARD
,
460 wxT("wxClipboard now supports atom %s"),
461 array
[i
].GetId().c_str() );
463 // printf( "added %s\n",
464 // gdk_atom_name( array[i].GetFormatId() ) );
466 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
469 0 ); /* what is info ? */
474 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
476 GTK_SIGNAL_FUNC(selection_handler
),
477 GUINT_TO_POINTER( gdk_event_get_time(gtk_get_current_event()) ) );
480 /* disable GUI threads */
483 /* Tell the world we offer clipboard data */
484 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
486 (guint32
) GDK_CURRENT_TIME
));
489 m_ownsPrimarySelection
= res
;
491 m_ownsClipboard
= res
;
494 /* re-enable GUI threads */
500 void wxClipboard::Close()
502 wxCHECK_RET( m_open
, wxT("clipboard not open") );
507 bool wxClipboard::IsOpened() const
512 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
514 /* reentrance problems */
515 if (m_waiting
) return FALSE
;
517 /* store requested format to be asked for by callbacks */
518 m_targetRequested
= format
;
520 wxLogTrace( TRACE_CLIPBOARD
,
521 wxT("wxClipboard:IsSupported: requested format: %s"),
522 format
.GetId().c_str() );
524 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
526 m_formatSupported
= FALSE
;
528 /* perform query. this will set m_formatSupported to
529 TRUE if m_targetRequested is supported.
530 also, we have to wait for the "answer" from the
531 clipboard owner which is an asynchronous process.
532 therefore we set m_waiting = TRUE here and wait
533 until the callback "targets_selection_received"
538 gtk_selection_convert( m_targetsWidget
,
539 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
542 (guint32
) GDK_CURRENT_TIME
);
544 while (m_waiting
) gtk_main_iteration();
546 return m_formatSupported
;
549 bool wxClipboard::GetData( wxDataObject
& data
)
551 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
553 /* get formats from wxDataObjects */
554 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
555 data
.GetAllFormats( array
);
557 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
559 wxDataFormat
format( array
[i
] );
561 wxLogTrace( TRACE_CLIPBOARD
,
562 wxT("wxClipboard::GetData: requested format: %s"),
563 format
.GetId().c_str() );
565 /* is data supported by clipboard ? */
567 /* store requested format to be asked for by callbacks */
568 m_targetRequested
= format
;
570 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
572 m_formatSupported
= FALSE
;
574 /* perform query. this will set m_formatSupported to
575 TRUE if m_targetRequested is supported.
576 also, we have to wait for the "answer" from the
577 clipboard owner which is an asynchronous process.
578 therefore we set m_waiting = TRUE here and wait
579 until the callback "targets_selection_received"
584 gtk_selection_convert( m_targetsWidget
,
585 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
588 (guint32
) GDK_CURRENT_TIME
);
590 while (m_waiting
) gtk_main_iteration();
592 if (!m_formatSupported
) continue;
594 /* store pointer to data object to be filled up by callbacks */
595 m_receivedData
= &data
;
597 /* store requested format to be asked for by callbacks */
598 m_targetRequested
= format
;
600 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
603 m_formatSupported
= FALSE
;
605 /* ask for clipboard contents. this will set
606 m_formatSupported to TRUE if m_targetRequested
608 also, we have to wait for the "answer" from the
609 clipboard owner which is an asynchronous process.
610 therefore we set m_waiting = TRUE here and wait
611 until the callback "targets_selection_received"
616 wxLogTrace( TRACE_CLIPBOARD
,
617 wxT("wxClipboard::GetData: format found, start convert") );
619 gtk_selection_convert( m_clipboardWidget
,
620 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
623 (guint32
) GDK_CURRENT_TIME
);
625 while (m_waiting
) gtk_main_iteration();
627 /* this is a true error as we checked for the presence of such data before */
628 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
635 wxLogTrace( TRACE_CLIPBOARD
,
636 wxT("wxClipboard::GetData: format not found") );