]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/clipbrd.cpp
DnD updates
[wxWidgets.git] / src / gtk / clipbrd.cpp
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
20 wxClipboard *wxTheClipboard = (wxClipboard*) NULL;
21
22 GdkAtom g_textAtom = 0;
23 GdkAtom g_clipboardAtom = 0;
24 GdkAtom g_targetsAtom = 0;
25
26 //-----------------------------------------------------------------------------
27 // reminder
28 //-----------------------------------------------------------------------------
29
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
39 struct _GtkSelectionData
40 {
41 GdkAtom selection;
42 GdkAtom target;
43 GdkAtom type;
44 gint format;
45 guchar *data;
46 gint length;
47 };
48
49 */
50
51 //-----------------------------------------------------------------------------
52 // "selection_received" for targets
53 //-----------------------------------------------------------------------------
54
55 static void
56 targets_selection_received( GtkWidget *WXUNUSED(widget),
57 GtkSelectionData *selection_data,
58 wxClipboard *clipboard )
59 {
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;
80 }
81
82 //-----------------------------------------------------------------------------
83 // "selection_received" for the actual data
84 //-----------------------------------------------------------------------------
85
86 static void
87 selection_received( GtkWidget *WXUNUSED(widget),
88 GtkSelectionData *selection_data,
89 wxClipboard *clipboard )
90 {
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);
105 }
106
107 //-----------------------------------------------------------------------------
108 // "selection_clear"
109 //-----------------------------------------------------------------------------
110
111 static gint
112 selection_clear( GtkWidget *WXUNUSED(widget), GdkEventSelection *WXUNUSED(event) )
113 {
114 if (!wxTheClipboard) return TRUE;
115
116 /* the clipboard is no longer in our hands. we can delete the
117 * clipboard data. I hope I got that one right... */
118
119 wxTheClipboard->SetData( (wxDataObject*) NULL );
120
121 return TRUE;
122 }
123
124 //-----------------------------------------------------------------------------
125 // selection handler for supplying data
126 //-----------------------------------------------------------------------------
127
128 static void
129 selection_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 }
155
156 //-----------------------------------------------------------------------------
157 // wxClipboard
158 //-----------------------------------------------------------------------------
159
160 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
161
162 wxClipboard::wxClipboard()
163 {
164 m_data = (wxDataObject*) NULL;
165 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
166 gtk_widget_realize( m_clipboardWidget );
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 );
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;
181 }
182
183 wxClipboard::~wxClipboard()
184 {
185 Clear();
186
187 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
188 }
189
190 void wxClipboard::Clear()
191 {
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 }
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;
212 }
213
214 void wxClipboard::SetData( wxDataObject *data )
215 {
216 if (m_data) delete m_data;
217 m_data = data;
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 {
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 }
241 }
242
243 bool 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
276 bool wxClipboard::ObtainData( wxDataFormat format )
277 {
278 m_receivedSize = 0;
279
280 if (m_receivedData)
281 {
282 delete[] m_receivedData;
283 m_receivedData = (char*) NULL;
284 }
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
315 size_t wxClipboard::GetDataSize() const
316 {
317 return m_receivedSize;
318 }
319
320 void wxClipboard::GetDataHere( void *data ) const
321 {
322 memcpy(data, m_receivedData, m_receivedSize );
323 }
324
325 //-----------------------------------------------------------------------------
326 // wxClipboardModule
327 //-----------------------------------------------------------------------------
328
329 IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule)
330
331 bool wxClipboardModule::OnInit()
332 {
333 wxTheClipboard = new wxClipboard();
334
335 return TRUE;
336 }
337
338 void wxClipboardModule::OnExit()
339 {
340 if (wxTheClipboard) delete wxTheClipboard;
341 wxTheClipboard = (wxClipboard*) NULL;
342 }