]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dnd.cpp
Changed m_font to * GetFont() since m_font isn't in all implementations.
[wxWidgets.git] / src / gtk1 / dnd.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dnd.cpp
3 // Purpose: wxDropTarget class
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 "dnd.h"
12 #endif
13
14 #include "wx/dnd.h"
15 #include "wx/window.h"
16 #include "wx/app.h"
17 #include "wx/gdicmn.h"
18 #include <wx/intl.h>
19
20 #include "gdk/gdkprivate.h"
21
22 #include <X11/Xlib.h>
23
24 // ----------------------------------------------------------------------------
25 // global
26 // ----------------------------------------------------------------------------
27
28 extern bool g_blockEventsOnDrag;
29
30 // ----------------------------------------------------------------------------
31 // wxDropTarget
32 // ----------------------------------------------------------------------------
33
34 wxDropTarget::wxDropTarget()
35 {
36 }
37
38 wxDropTarget::~wxDropTarget()
39 {
40 }
41
42 void wxDropTarget::UnregisterWidget( GtkWidget *widget )
43 {
44 if (!widget) return;
45
46 gtk_widget_dnd_drop_set( widget, FALSE, (gchar **) NULL, 0, FALSE );
47 }
48
49 void wxDropTarget::RegisterWidget( GtkWidget *widget )
50 {
51 wxString formats;
52 int valid = 0;
53
54 for ( size_t i = 0; i < GetFormatCount(); i++ )
55 {
56 wxDataFormat df = GetFormat( i );
57 switch (df)
58 {
59 case wxDF_TEXT:
60 if (i > 0) formats += ";";
61 formats += "text/plain";
62 valid++;
63 break;
64 case wxDF_FILENAME:
65 if (i > 0) formats += ";";
66 formats += "file:ALL";
67 valid++;
68 break;
69 default:
70 break;
71 }
72 }
73
74 char *str = WXSTRINGCAST formats;
75
76 gtk_widget_dnd_drop_set( widget, TRUE, &str, valid, FALSE );
77 }
78
79 // ----------------------------------------------------------------------------
80 // wxTextDropTarget
81 // ----------------------------------------------------------------------------
82
83 bool wxTextDropTarget::OnDrop( long x, long y, const void *data, size_t WXUNUSED(size) )
84 {
85 OnDropText( x, y, (const char*)data );
86 return TRUE;
87 }
88
89 bool wxTextDropTarget::OnDropText( long x, long y, const char *psz )
90 {
91 printf( "Got dropped text: %s.\n", psz );
92 printf( "At x: %d, y: %d.\n", (int)x, (int)y );
93 return TRUE;
94 }
95
96 size_t wxTextDropTarget::GetFormatCount() const
97 {
98 return 1;
99 }
100
101 wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const
102 {
103 return wxDF_TEXT;
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxFileDropTarget
108 // ----------------------------------------------------------------------------
109
110 bool wxFileDropTarget::OnDropFiles( long x, long y, size_t nFiles, const char * const aszFiles[] )
111 {
112 printf( "Got %d dropped files.\n", (int)nFiles );
113 printf( "At x: %d, y: %d.\n", (int)x, (int)y );
114 for (size_t i = 0; i < nFiles; i++)
115 {
116 printf( aszFiles[i] );
117 printf( "\n" );
118 }
119 return TRUE;
120 }
121
122 bool wxFileDropTarget::OnDrop(long x, long y, const void *data, size_t size )
123 {
124 size_t number = 0;
125 char *text = (char*) data;
126 for (size_t i = 0; i < size; i++)
127 if (text[i] == 0) number++;
128
129 if (number == 0) return TRUE;
130
131 char **files = new char*[number];
132
133 text = (char*) data;
134 for (size_t i = 0; i < number; i++)
135 {
136 files[i] = text;
137 int len = strlen( text );
138 text += len+1;
139 }
140
141 bool ret = OnDropFiles( x, y, 1, files );
142
143 free( files );
144
145 return ret;
146 }
147
148 size_t wxFileDropTarget::GetFormatCount() const
149 {
150 return 1;
151 }
152
153 wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const
154 {
155 return wxDF_FILENAME;
156 }
157
158 //-------------------------------------------------------------------------
159 // wxDropSource
160 //-------------------------------------------------------------------------
161
162 //-----------------------------------------------------------------------------
163 // drag request
164
165 void gtk_drag_callback( GtkWidget *widget, GdkEvent *event, wxDropSource *source )
166 {
167 printf( "Data requested for dropping.\n" );
168
169 wxDataObject *data = source->m_data;
170
171 size_t size = data->GetDataSize();
172 char *ptr = new char[size];
173 data->GetDataHere( ptr );
174
175 gtk_widget_dnd_data_set( widget, event, ptr, size );
176
177 delete ptr;
178
179 source->m_retValue = wxDragCopy;
180 }
181
182 wxDropSource::wxDropSource( wxWindow *win )
183 {
184 g_blockEventsOnDrag = TRUE;
185
186 m_window = win;
187 m_widget = win->m_widget;
188 if (win->m_wxwindow) m_widget = win->m_wxwindow;
189
190 m_data = (wxDataObject *) NULL;
191 m_retValue = wxDragCancel;
192
193 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
194 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
195 }
196
197 wxDropSource::wxDropSource( wxDataObject &data, wxWindow *win )
198 {
199 g_blockEventsOnDrag = TRUE;
200
201 m_window = win;
202 m_widget = win->m_widget;
203 if (win->m_wxwindow) m_widget = win->m_wxwindow;
204 m_retValue = wxDragCancel;
205
206 m_data = &data;
207
208 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
209 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
210 }
211
212 void wxDropSource::SetData( wxDataObject &data )
213 {
214 m_data = &data;
215 }
216
217 wxDropSource::~wxDropSource(void)
218 {
219 // if (m_data) delete m_data;
220
221 g_blockEventsOnDrag = FALSE;
222 }
223
224 wxDragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) )
225 {
226 if (gdk_dnd.dnd_grabbed) return (wxDragResult) wxDragNone;
227 if (gdk_dnd.drag_really) return (wxDragResult) wxDragNone;
228
229 wxASSERT_MSG( m_data, "wxDragSource: no data" );
230
231 if (!m_data) return (wxDragResult) wxDragNone;
232 if (m_data->GetDataSize() == 0) return (wxDragResult) wxDragNone;
233
234 GdkWindowPrivate *wp = (GdkWindowPrivate*) m_widget->window;
235
236 RegisterWindow();
237
238 gdk_dnd.drag_perhaps = TRUE;
239
240 gdk_dnd.dnd_drag_start.x = 5;
241 gdk_dnd.dnd_drag_start.y = 5;
242 gdk_dnd.real_sw = wp;
243
244 if (gdk_dnd.drag_startwindows)
245 {
246 g_free( gdk_dnd.drag_startwindows );
247 gdk_dnd.drag_startwindows = (GdkWindow **) NULL;
248 }
249 gdk_dnd.drag_numwindows = gdk_dnd.drag_really = 0;
250
251 XWindowAttributes dnd_winattr;
252 XGetWindowAttributes( gdk_display, wp->xwindow, &dnd_winattr );
253 wp->dnd_drag_savedeventmask = dnd_winattr.your_event_mask;
254
255 gdk_dnd_drag_addwindow( m_widget->window );
256
257 GdkEventDragBegin ev;
258 ev.type = GDK_DRAG_BEGIN;
259 ev.window = m_widget->window;
260 ev.u.allflags = 0;
261 ev.u.flags.protocol_version = DND_PROTOCOL_VERSION;
262
263 gdk_event_put( (GdkEvent*)&ev );
264
265 XGrabPointer( gdk_display, wp->xwindow, False,
266 ButtonMotionMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
267 GrabModeAsync, GrabModeAsync, gdk_root_window, None, CurrentTime );
268
269 gdk_dnd_set_drag_cursors( m_defaultCursor.GetCursor(), m_goaheadCursor.GetCursor() );
270
271 gdk_dnd.dnd_grabbed = TRUE;
272 gdk_dnd.drag_really = 1;
273
274 int x = 0;
275 int y = 0;
276 gdk_window_get_pointer( m_widget->window, &x, &y, (GdkModifierType *) NULL );
277
278 gdk_dnd_display_drag_cursor( x, y, FALSE, TRUE );
279
280 while (gdk_dnd.drag_really || gdk_dnd.drag_perhaps) wxYield();
281
282 UnregisterWindow();
283
284 g_blockEventsOnDrag = FALSE;
285
286 return m_retValue;
287 }
288
289 void wxDropSource::RegisterWindow(void)
290 {
291 if (!m_data) return;
292
293 wxString formats;
294
295 wxDataFormat df = m_data->GetPreferredFormat();
296
297 switch (df)
298 {
299 case wxDF_TEXT:
300 formats += "text/plain";
301 break;
302 case wxDF_FILENAME:
303 formats += "file:ALL";
304 break;
305 default:
306 break;
307 }
308
309 char *str = WXSTRINGCAST formats;
310
311 gtk_widget_dnd_drag_set( m_widget, TRUE, &str, 1 );
312
313 gtk_signal_connect( GTK_OBJECT(m_widget), "drag_request_event",
314 GTK_SIGNAL_FUNC(gtk_drag_callback), (gpointer)this );
315 }
316
317 void wxDropSource::UnregisterWindow(void)
318 {
319 if (!m_widget) return;
320
321 gtk_widget_dnd_drag_set( m_widget, FALSE, (gchar **) NULL, 0 );
322
323 gtk_signal_disconnect_by_data( GTK_OBJECT(m_widget), (gpointer)this );
324 }