]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/clipbrd.cpp
Added OLE automation sample, changed BC++ makefiles
[wxWidgets.git] / src / gtk1 / clipbrd.cpp
CommitLineData
dc86cb34
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: clipbrd.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "clipbrd.h"
12#endif
13
14#include "wx/clipbrd.h"
15
16//-----------------------------------------------------------------------------
17// data
18//-----------------------------------------------------------------------------
19
20wxClipboard *wxTheClipboard = (wxClipboard*) NULL;
21
fd0eed64
RR
22GdkAtom g_textAtom = 0;
23GdkAtom g_clipboardAtom = 0;
b527aac5 24GdkAtom g_targetsAtom = 0;
fd0eed64 25
dc86cb34 26//-----------------------------------------------------------------------------
b527aac5 27// reminder
dc86cb34
RR
28//-----------------------------------------------------------------------------
29
b527aac5
RR
30/* The contents of a selection are returned in a GtkSelectionData
31 structure. selection/target identify the request.
32 type specifies the type of the return; if length < 0, and
33 the data should be ignored. This structure has object semantics -
34 no fields should be modified directly, they should not be created
35 directly, and pointers to them should not be stored beyond the duration of
36 a callback. (If the last is changed, we'll need to add reference
37 counting)
38
39struct _GtkSelectionData
dc86cb34 40{
b527aac5
RR
41 GdkAtom selection;
42 GdkAtom target;
43 GdkAtom type;
44 gint format;
45 guchar *data;
46 gint length;
47};
48
49*/
dc86cb34 50
b527aac5
RR
51//-----------------------------------------------------------------------------
52// "selection_received" for targets
53//-----------------------------------------------------------------------------
54
55static void
56targets_selection_received( GtkWidget *WXUNUSED(widget),
57 GtkSelectionData *selection_data,
58 wxClipboard *clipboard )
dc86cb34 59{
b527aac5
RR
60 if (!wxTheClipboard) return;
61
62 if (selection_data->length <= 0) return;
63
64 // make sure we got the data in the correct form
65 if (selection_data->type != GDK_SELECTION_TYPE_ATOM) return;
66
67 // the atoms we received, holding a list of targets (= formats)
68 GdkAtom *atoms = (GdkAtom *)selection_data->data;
69
70 for (unsigned int i=0; i<selection_data->length/sizeof(GdkAtom); i++)
71 {
72 if (atoms[i] == clipboard->m_targetRequested)
73 {
74 clipboard->m_formatSupported = TRUE;
75 return;
76 }
77 }
78
79 return;
dc86cb34
RR
80}
81
82//-----------------------------------------------------------------------------
b527aac5 83// "selection_received" for the actual data
dc86cb34
RR
84//-----------------------------------------------------------------------------
85
fd0eed64 86static void
b527aac5
RR
87selection_received( GtkWidget *WXUNUSED(widget),
88 GtkSelectionData *selection_data,
89 wxClipboard *clipboard )
dc86cb34 90{
b527aac5
RR
91 if (!wxTheClipboard) return;
92
93 if (selection_data->length <= 0) return;
94
95 size_t size = (size_t) selection_data->length;
96
97 // make sure we got the data in the correct form
98 if (selection_data->type != GDK_SELECTION_TYPE_STRING) return;
99
100 clipboard->m_receivedSize = size;
101
102 clipboard->m_receivedData = new char[size+1];
103
104 memcpy( clipboard->m_receivedData, selection_data->data, size);
dc86cb34 105}
fd0eed64
RR
106
107//-----------------------------------------------------------------------------
108// "selection_clear"
109//-----------------------------------------------------------------------------
110
111static gint
b527aac5 112selection_clear( GtkWidget *WXUNUSED(widget), GdkEventSelection *WXUNUSED(event) )
fd0eed64 113{
fd0eed64
RR
114 if (!wxTheClipboard) return TRUE;
115
b527aac5
RR
116 /* the clipboard is no longer in our hands. we can delete the
117 * clipboard data. I hope I got that one right... */
118
fd0eed64
RR
119 wxTheClipboard->SetData( (wxDataObject*) NULL );
120
121 return TRUE;
122}
123
124//-----------------------------------------------------------------------------
125// selection handler for supplying data
126//-----------------------------------------------------------------------------
127
128static void
129selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, gpointer WXUNUSED(data) )
130{
131 if (!wxTheClipboard) return;
132
133 wxDataObject *data_object = wxTheClipboard->m_data;
134
135 if (!data_object) return;
136
137 if (data_object->GetDataSize() == 0) return;
138
139 gint len = data_object->GetDataSize();
140 guchar *bin_data = (guchar*) malloc( len );
141 data_object->GetDataHere( (void*)bin_data );
142
143 if (selection_data->target == GDK_SELECTION_TYPE_STRING)
144 {
145 gtk_selection_data_set(
146 selection_data, GDK_SELECTION_TYPE_STRING, 8*sizeof(gchar), bin_data, len );
147 }
148 else if (selection_data->target == g_textAtom)
149 {
150 gtk_selection_data_set(
151 selection_data, g_textAtom, 8*sizeof(gchar), bin_data, len );
152 }
153 free( bin_data );
154}
dc86cb34
RR
155
156//-----------------------------------------------------------------------------
157// wxClipboard
158//-----------------------------------------------------------------------------
159
160IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
161
162wxClipboard::wxClipboard()
163{
fd0eed64 164 m_data = (wxDataObject*) NULL;
dc86cb34
RR
165 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
166 gtk_widget_realize( m_clipboardWidget );
fd0eed64
RR
167
168 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
169 "selection_clear_event",
170 GTK_SIGNAL_FUNC( selection_clear ),
171 (gpointer) NULL );
172
173 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
174 if (!g_textAtom) g_textAtom = gdk_atom_intern( "TEXT", FALSE );
b527aac5
RR
175 if (!g_targetsAtom) g_targetsAtom = gdk_atom_intern ("TARGETS", FALSE);
176
177 m_receivedData = (char*)NULL;
178 m_receivedSize = 0;
179 m_formatSupported = FALSE;
180 m_targetRequested = 0;
dc86cb34
RR
181}
182
183wxClipboard::~wxClipboard()
b527aac5
RR
184{
185 Clear();
186
187 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
188}
189
190void wxClipboard::Clear()
dc86cb34 191{
fd0eed64
RR
192 /* As we have data we also own the clipboard. Once we no longer own
193 it, clear_selection is called which will set m_data to zero */
194
195 if (m_data)
196 {
197 delete m_data;
198 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME );
199 }
b527aac5
RR
200
201 m_receivedSize = 0;
202
203 if (m_receivedData)
204 {
205 delete[] m_receivedData;
206 m_receivedData = (char*) NULL;
207 }
208
209 m_targetRequested = 0;
210
211 m_formatSupported = FALSE;
dc86cb34
RR
212}
213
214void wxClipboard::SetData( wxDataObject *data )
215{
216 if (m_data) delete m_data;
217 m_data = data;
fd0eed64
RR
218 if (!m_data) return;
219
220 if (!gtk_selection_owner_set( m_clipboardWidget,
221 g_clipboardAtom,
222 GDK_CURRENT_TIME))
223 {
224 delete m_data;
225 m_data = (wxDataObject*) NULL;
226 return;
227 }
228
229 switch (m_data->GetPreferredFormat())
230 {
fd0eed64
RR
231 case wxDF_TEXT:
232 gtk_selection_add_handler( m_clipboardWidget,
233 g_clipboardAtom,
234 g_textAtom,
235 selection_handler,
236 NULL );
237 break;
238 default:
239 break;
240 }
dc86cb34
RR
241}
242
b527aac5
RR
243bool wxClipboard::IsSupportedFormat( wxDataFormat format )
244{
245 m_targetRequested = 0;
246
247 if (format == wxDF_TEXT)
248 {
249// m_targetRequested = g_textAtom;
250 m_targetRequested = GDK_TARGET_STRING;
251 }
252
253 if (m_targetRequested == 0) return FALSE;
254
255 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
256 "selection_received",
257 GTK_SIGNAL_FUNC( targets_selection_received ),
258 (gpointer) this );
259
260 m_formatSupported = FALSE;
261
262 gtk_selection_convert( m_clipboardWidget,
263 g_clipboardAtom,
264 g_targetsAtom,
265 GDK_CURRENT_TIME );
266
267 gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget),
268 GTK_SIGNAL_FUNC( targets_selection_received ),
269 (gpointer) this );
270
271 if (!m_formatSupported) return FALSE;
272
273 return TRUE;
274}
275
276bool wxClipboard::ObtainData( wxDataFormat format )
dc86cb34 277{
b527aac5
RR
278 m_receivedSize = 0;
279
280 if (m_receivedData)
dc86cb34 281 {
b527aac5
RR
282 delete[] m_receivedData;
283 m_receivedData = (char*) NULL;
dc86cb34 284 }
b527aac5
RR
285
286 m_targetRequested = 0;
287
288 if (format == wxDF_TEXT)
289 {
290// m_targetRequested = g_textAtom;
291 m_targetRequested = GDK_TARGET_STRING;
292 }
293
294 if (m_targetRequested == 0) return FALSE;
295
296 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
297 "selection_received",
298 GTK_SIGNAL_FUNC( selection_received ),
299 (gpointer) this );
300
301 gtk_selection_convert( m_clipboardWidget,
302 g_clipboardAtom,
303 m_targetRequested,
304 GDK_CURRENT_TIME );
305
306 gtk_signal_disconnect_by_func( GTK_OBJECT(m_clipboardWidget),
307 GTK_SIGNAL_FUNC( selection_received ),
308 (gpointer) this );
309
310 if (m_receivedSize == 0) return FALSE;
311
312 return TRUE;
313}
314
315size_t wxClipboard::GetDataSize() const
316{
317 return m_receivedSize;
318}
319
320void wxClipboard::GetDataHere( void *data ) const
321{
322 memcpy(data, m_receivedData, m_receivedSize );
dc86cb34
RR
323}
324
b527aac5
RR
325//-----------------------------------------------------------------------------
326// wxClipboardModule
327//-----------------------------------------------------------------------------
328
329IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
330
331bool wxClipboardModule::OnInit()
332{
333 wxTheClipboard = new wxClipboard();
334
335 return TRUE;
336}
337
338void wxClipboardModule::OnExit()
dc86cb34 339{
b527aac5
RR
340 if (wxTheClipboard) delete wxTheClipboard;
341 wxTheClipboard = (wxClipboard*) NULL;
dc86cb34 342}