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