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