]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | /////////////////////////////////////////////////////////////////////////////// |
88a7a4e1 | 2 | // Name: src/gtk/dnd.cpp |
c801d85f KB |
3 | // Purpose: wxDropTarget class |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
88a7a4e1 | 13 | #if wxUSE_DRAG_AND_DROP |
0a164d4c | 14 | |
88a7a4e1 | 15 | #include "wx/dnd.h" |
0a164d4c | 16 | |
88a7a4e1 WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/intl.h" | |
e4db172a | 19 | #include "wx/log.h" |
670f9935 | 20 | #include "wx/app.h" |
de6185e2 | 21 | #include "wx/utils.h" |
cdccdfab | 22 | #include "wx/window.h" |
dd05139a | 23 | #include "wx/gdicmn.h" |
88a7a4e1 | 24 | #endif |
ac57418f | 25 | |
9e691f46 VZ |
26 | #include "wx/gtk/private.h" |
27 | ||
071a2d78 | 28 | #include <gdk/gdkprivate.h> |
c801d85f | 29 | |
071a2d78 RR |
30 | #include <gtk/gtkdnd.h> |
31 | #include <gtk/gtkselection.h> | |
c801d85f | 32 | |
22d5903e RR |
33 | //---------------------------------------------------------------------------- |
34 | // global data | |
35 | //---------------------------------------------------------------------------- | |
c801d85f KB |
36 | |
37 | extern bool g_blockEventsOnDrag; | |
38 | ||
2245b2b2 VZ |
39 | // the flags used for the last DoDragDrop() |
40 | static long gs_flagsForDrag = 0; | |
41 | ||
b2dea2ab VZ |
42 | // the trace mask we use with wxLogTrace() - call |
43 | // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here | |
44 | // (there are quite a few of them, so don't enable this by default) | |
45 | static const wxChar *TRACE_DND = _T("dnd"); | |
46 | ||
8f9850dd RR |
47 | extern GdkEvent *g_lastMouseEvent; |
48 | ||
49 | extern int g_lastButtonNumber; | |
50 | ||
22d5903e RR |
51 | //---------------------------------------------------------------------------- |
52 | // standard icons | |
53 | //---------------------------------------------------------------------------- | |
54 | ||
13b22a67 | 55 | /* Copyright (c) Julian Smart */ |
90350682 | 56 | static const char * page_xpm[] = { |
13b22a67 JS |
57 | /* columns rows colors chars-per-pixel */ |
58 | "32 32 37 1", | |
59 | "5 c #7198D9", | |
60 | ", c #769CDA", | |
61 | "2 c #DCE6F6", | |
62 | "i c #FFFFFF", | |
63 | "e c #779DDB", | |
64 | ": c #9AB6E4", | |
65 | "9 c #EAF0FA", | |
66 | "- c #B1C7EB", | |
67 | "$ c #6992D7", | |
68 | "y c #F7F9FD", | |
69 | "= c #BED0EE", | |
70 | "q c #F0F5FC", | |
71 | "; c #A8C0E8", | |
72 | "@ c #366BC2", | |
73 | " c None", | |
74 | "u c #FDFEFF", | |
75 | "8 c #5987D3", | |
76 | "* c #C4D5F0", | |
77 | "7 c #7CA0DC", | |
78 | "O c #487BCE", | |
79 | "< c #6B94D7", | |
80 | "& c #CCDAF2", | |
81 | "> c #89A9DF", | |
82 | "3 c #5584D1", | |
83 | "w c #82A5DE", | |
84 | "1 c #3F74CB", | |
85 | "+ c #3A70CA", | |
86 | ". c #3569BF", | |
87 | "% c #D2DFF4", | |
88 | "# c #3366BB", | |
89 | "r c #F5F8FD", | |
90 | "0 c #FAFCFE", | |
91 | "4 c #DFE8F7", | |
92 | "X c #5E8AD4", | |
93 | "o c #5282D0", | |
94 | "t c #B8CCEC", | |
95 | "6 c #E5EDF9", | |
22d5903e | 96 | /* pixels */ |
13b22a67 JS |
97 | " ", |
98 | " ", | |
99 | " ", | |
100 | " ", | |
101 | " ", | |
102 | " .XXXooOO++@# ", | |
103 | " $%&*=-;::>,<1 ", | |
104 | " $2%&*=-;::><:3 ", | |
105 | " $42%&*=-;::<&:3 ", | |
106 | " 56477<<<<8<<9&:X ", | |
107 | " 59642%&*=-;<09&:5 ", | |
108 | " 5q9642%&*=-<<<<<# ", | |
109 | " 5qqw777<<<<<88:>+ ", | |
110 | " erqq9642%&*=t;::+ ", | |
111 | " eyrqq9642%&*=t;:O ", | |
112 | " eyywwww777<<<<t;O ", | |
113 | " e0yyrqq9642%&*=to ", | |
114 | " e00yyrqq9642%&*=o ", | |
115 | " eu0wwwwwww777<&*X ", | |
116 | " euu00yyrqq9642%&X ", | |
117 | " eiuu00yyrqq9642%X ", | |
118 | " eiiwwwwwwwwww742$ ", | |
119 | " eiiiuu00yyrqq964$ ", | |
120 | " eiiiiuu00yyrqq96$ ", | |
121 | " eiiiiiuu00yyrqq95 ", | |
122 | " eiiiiiiuu00yyrqq5 ", | |
123 | " eeeeeeeeeeeeee55e ", | |
124 | " ", | |
125 | " ", | |
126 | " ", | |
127 | " ", | |
128 | " " | |
129 | }; | |
f03fc89f VZ |
130 | |
131 | ||
2245b2b2 VZ |
132 | // ============================================================================ |
133 | // private functions | |
134 | // ============================================================================ | |
135 | ||
136 | // ---------------------------------------------------------------------------- | |
77ffb593 | 137 | // convert between GTK+ and wxWidgets DND constants |
2245b2b2 VZ |
138 | // ---------------------------------------------------------------------------- |
139 | ||
140 | static wxDragResult ConvertFromGTK(long action) | |
141 | { | |
142 | switch ( action ) | |
143 | { | |
144 | case GDK_ACTION_COPY: | |
145 | return wxDragCopy; | |
146 | ||
147 | case GDK_ACTION_LINK: | |
148 | return wxDragLink; | |
149 | ||
150 | case GDK_ACTION_MOVE: | |
151 | return wxDragMove; | |
152 | } | |
153 | ||
154 | return wxDragNone; | |
155 | } | |
4ba47b40 | 156 | |
33a5bc52 RR |
157 | // ---------------------------------------------------------------------------- |
158 | // "drag_leave" | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
865bb325 | 161 | extern "C" { |
33a5bc52 | 162 | static void target_drag_leave( GtkWidget *WXUNUSED(widget), |
f03fc89f VZ |
163 | GdkDragContext *context, |
164 | guint WXUNUSED(time), | |
165 | wxDropTarget *drop_target ) | |
33a5bc52 | 166 | { |
5549fa65 RR |
167 | if (g_isIdle) wxapp_install_idle_handler(); |
168 | ||
829e3e8d RR |
169 | /* inform the wxDropTarget about the current GdkDragContext. |
170 | this is only valid for the duration of this call */ | |
171 | drop_target->SetDragContext( context ); | |
f03fc89f | 172 | |
829e3e8d RR |
173 | /* we don't need return values. this event is just for |
174 | information */ | |
175 | drop_target->OnLeave(); | |
f03fc89f | 176 | |
829e3e8d | 177 | /* this has to be done because GDK has no "drag_enter" event */ |
0a164d4c | 178 | drop_target->m_firstMotion = true; |
f03fc89f | 179 | |
829e3e8d RR |
180 | /* after this, invalidate the drop_target's GdkDragContext */ |
181 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
33a5bc52 | 182 | } |
865bb325 | 183 | } |
33a5bc52 RR |
184 | |
185 | // ---------------------------------------------------------------------------- | |
186 | // "drag_motion" | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
865bb325 | 189 | extern "C" { |
33a5bc52 | 190 | static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget), |
f03fc89f VZ |
191 | GdkDragContext *context, |
192 | gint x, | |
193 | gint y, | |
194 | guint time, | |
195 | wxDropTarget *drop_target ) | |
33a5bc52 | 196 | { |
5549fa65 RR |
197 | if (g_isIdle) wxapp_install_idle_handler(); |
198 | ||
829e3e8d RR |
199 | /* Owen Taylor: "if the coordinates not in a drop zone, |
200 | return FALSE, otherwise call gtk_drag_status() and | |
201 | return TRUE" */ | |
f03fc89f | 202 | |
829e3e8d RR |
203 | /* inform the wxDropTarget about the current GdkDragContext. |
204 | this is only valid for the duration of this call */ | |
205 | drop_target->SetDragContext( context ); | |
f03fc89f | 206 | |
2245b2b2 VZ |
207 | // GTK+ always supposes that we want to copy the data by default while we |
208 | // might want to move it, so examine not only suggested_action - which is | |
209 | // only good if we don't have our own preferences - but also the actions | |
210 | // field | |
8ee9d618 | 211 | wxDragResult result; |
917ae499 RR |
212 | if (drop_target->GetDefaultAction() == wxDragNone) |
213 | { | |
214 | // use default action set by wxDropSource::DoDragDrop() | |
2245b2b2 VZ |
215 | if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove && |
216 | (context->actions & GDK_ACTION_MOVE ) ) | |
217 | { | |
218 | // move is requested by the program and allowed by GTK+ - do it, even | |
219 | // though suggested_action may be currently wxDragCopy | |
8ee9d618 | 220 | result = wxDragMove; |
2245b2b2 VZ |
221 | } |
222 | else // use whatever GTK+ says we should | |
223 | { | |
224 | result = ConvertFromGTK(context->suggested_action); | |
225 | ||
226 | if ( (result == wxDragMove) && !(gs_flagsForDrag & wxDrag_AllowMove) ) | |
227 | { | |
228 | // we're requested to move but we can't | |
229 | result = wxDragCopy; | |
230 | } | |
231 | } | |
917ae499 RR |
232 | } |
233 | else if (drop_target->GetDefaultAction() == wxDragMove && | |
234 | (context->actions & GDK_ACTION_MOVE)) | |
235 | { | |
236 | result = wxDragMove; | |
237 | } | |
238 | else | |
239 | { | |
240 | if (context->actions & GDK_ACTION_COPY) | |
241 | result = wxDragCopy; | |
242 | else if (context->actions & GDK_ACTION_MOVE) | |
243 | result = wxDragMove; | |
244 | else | |
245 | result = wxDragNone; | |
246 | } | |
8e00741d | 247 | |
829e3e8d | 248 | if (drop_target->m_firstMotion) |
d6086ea6 | 249 | { |
829e3e8d | 250 | /* the first "drag_motion" event substitutes a "drag_enter" event */ |
c9057ae1 | 251 | result = drop_target->OnEnter( x, y, result ); |
8e00741d RR |
252 | } |
253 | else | |
254 | { | |
255 | /* give program a chance to react (i.e. to say no by returning FALSE) */ | |
c9057ae1 | 256 | result = drop_target->OnDragOver( x, y, result ); |
d6086ea6 | 257 | } |
f03fc89f | 258 | |
8ee9d618 | 259 | bool ret = wxIsDragResultOk( result ); |
829e3e8d | 260 | if (ret) |
bd77da97 | 261 | { |
8ee9d618 | 262 | GdkDragAction action; |
917ae499 | 263 | if (result == wxDragCopy) |
8ee9d618 | 264 | action = GDK_ACTION_COPY; |
b184227d RD |
265 | else if (result == wxDragLink) |
266 | action = GDK_ACTION_LINK; | |
8ee9d618 VZ |
267 | else |
268 | action = GDK_ACTION_MOVE; | |
269 | ||
7b5d5699 | 270 | gdk_drag_status( context, action, time ); |
bd77da97 | 271 | } |
f03fc89f | 272 | |
829e3e8d RR |
273 | /* after this, invalidate the drop_target's GdkDragContext */ |
274 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
f03fc89f | 275 | |
829e3e8d | 276 | /* this has to be done because GDK has no "drag_enter" event */ |
0a164d4c | 277 | drop_target->m_firstMotion = false; |
f03fc89f | 278 | |
829e3e8d | 279 | return ret; |
33a5bc52 | 280 | } |
865bb325 | 281 | } |
33a5bc52 RR |
282 | |
283 | // ---------------------------------------------------------------------------- | |
284 | // "drag_drop" | |
285 | // ---------------------------------------------------------------------------- | |
286 | ||
865bb325 | 287 | extern "C" { |
33a5bc52 | 288 | static gboolean target_drag_drop( GtkWidget *widget, |
f03fc89f VZ |
289 | GdkDragContext *context, |
290 | gint x, | |
291 | gint y, | |
292 | guint time, | |
293 | wxDropTarget *drop_target ) | |
33a5bc52 | 294 | { |
5549fa65 RR |
295 | if (g_isIdle) wxapp_install_idle_handler(); |
296 | ||
829e3e8d RR |
297 | /* Owen Taylor: "if the drop is not in a drop zone, |
298 | return FALSE, otherwise, if you aren't accepting | |
299 | the drop, call gtk_drag_finish() with success == FALSE | |
300 | otherwise call gtk_drag_data_get()" */ | |
4ba47b40 RR |
301 | |
302 | // printf( "drop.\n" ); | |
f03fc89f | 303 | |
829e3e8d RR |
304 | /* this seems to make a difference between not accepting |
305 | due to wrong target area and due to wrong format. let | |
306 | us hope that this is not required.. */ | |
f03fc89f | 307 | |
829e3e8d RR |
308 | /* inform the wxDropTarget about the current GdkDragContext. |
309 | this is only valid for the duration of this call */ | |
310 | drop_target->SetDragContext( context ); | |
f03fc89f | 311 | |
829e3e8d RR |
312 | /* inform the wxDropTarget about the current drag widget. |
313 | this is only valid for the duration of this call */ | |
314 | drop_target->SetDragWidget( widget ); | |
f03fc89f | 315 | |
829e3e8d RR |
316 | /* inform the wxDropTarget about the current drag time. |
317 | this is only valid for the duration of this call */ | |
318 | drop_target->SetDragTime( time ); | |
f03fc89f | 319 | |
bd77da97 RR |
320 | /* |
321 | wxDragResult result = wxDragMove; | |
322 | if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy; | |
323 | */ | |
324 | ||
513abb88 RR |
325 | /* reset the block here as someone might very well |
326 | show a dialog as a reaction to a drop and this | |
327 | wouldn't work without events */ | |
0a164d4c | 328 | g_blockEventsOnDrag = false; |
b2dea2ab | 329 | |
829e3e8d | 330 | bool ret = drop_target->OnDrop( x, y ); |
f03fc89f | 331 | |
5af019af | 332 | if (!ret) |
829e3e8d | 333 | { |
b2dea2ab | 334 | wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") ); |
8ee9d618 | 335 | |
829e3e8d RR |
336 | /* cancel the whole thing */ |
337 | gtk_drag_finish( context, | |
f03fc89f VZ |
338 | FALSE, /* no success */ |
339 | FALSE, /* don't delete data on dropping side */ | |
340 | time ); | |
829e3e8d | 341 | } |
8e00741d RR |
342 | else |
343 | { | |
670f9935 | 344 | wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") ); |
8ee9d618 | 345 | |
8e00741d RR |
346 | #if wxUSE_THREADS |
347 | /* disable GUI threads */ | |
8e00741d RR |
348 | #endif |
349 | ||
350 | GdkAtom format = drop_target->GetMatchingPair(); | |
b72aa48c VZ |
351 | |
352 | // this does happen somehow, see bug 555111 | |
637b7e4f | 353 | wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") ); |
8ee9d618 | 354 | |
bd77da97 RR |
355 | /* |
356 | GdkDragAction action = GDK_ACTION_MOVE; | |
8ee9d618 VZ |
357 | if (result == wxDragCopy) action == GDK_ACTION_COPY; |
358 | context->action = action; | |
bd77da97 | 359 | */ |
8e00741d RR |
360 | /* this should trigger an "drag_data_received" event */ |
361 | gtk_drag_get_data( widget, | |
362 | context, | |
363 | format, | |
364 | time ); | |
365 | ||
366 | #if wxUSE_THREADS | |
367 | /* re-enable GUI threads */ | |
8e00741d RR |
368 | #endif |
369 | } | |
f03fc89f | 370 | |
829e3e8d RR |
371 | /* after this, invalidate the drop_target's GdkDragContext */ |
372 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
f03fc89f | 373 | |
829e3e8d RR |
374 | /* after this, invalidate the drop_target's drag widget */ |
375 | drop_target->SetDragWidget( (GtkWidget*) NULL ); | |
f03fc89f | 376 | |
829e3e8d | 377 | /* this has to be done because GDK has no "drag_enter" event */ |
0a164d4c | 378 | drop_target->m_firstMotion = true; |
f03fc89f | 379 | |
829e3e8d | 380 | return ret; |
33a5bc52 | 381 | } |
865bb325 | 382 | } |
33a5bc52 RR |
383 | |
384 | // ---------------------------------------------------------------------------- | |
385 | // "drag_data_received" | |
386 | // ---------------------------------------------------------------------------- | |
387 | ||
865bb325 | 388 | extern "C" { |
33a5bc52 | 389 | static void target_drag_data_received( GtkWidget *WXUNUSED(widget), |
f03fc89f VZ |
390 | GdkDragContext *context, |
391 | gint x, | |
392 | gint y, | |
393 | GtkSelectionData *data, | |
394 | guint WXUNUSED(info), | |
395 | guint time, | |
396 | wxDropTarget *drop_target ) | |
33a5bc52 | 397 | { |
5549fa65 RR |
398 | if (g_isIdle) wxapp_install_idle_handler(); |
399 | ||
829e3e8d RR |
400 | /* Owen Taylor: "call gtk_drag_finish() with |
401 | success == TRUE" */ | |
402 | ||
829e3e8d | 403 | if ((data->length <= 0) || (data->format != 8)) |
33a5bc52 | 404 | { |
829e3e8d RR |
405 | /* negative data length and non 8-bit data format |
406 | qualifies for junk */ | |
407 | gtk_drag_finish (context, FALSE, FALSE, time); | |
f03fc89f | 408 | |
f03fc89f | 409 | return; |
829e3e8d | 410 | } |
f03fc89f | 411 | |
b2dea2ab | 412 | wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") ); |
8ee9d618 | 413 | |
5af019af RR |
414 | /* inform the wxDropTarget about the current GtkSelectionData. |
415 | this is only valid for the duration of this call */ | |
416 | drop_target->SetDragData( data ); | |
f03fc89f | 417 | |
c7d94ca4 | 418 | wxDragResult result = ConvertFromGTK(context->action); |
8ee9d618 VZ |
419 | |
420 | if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) ) | |
829e3e8d | 421 | { |
670f9935 | 422 | wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") ); |
8ee9d618 | 423 | |
3103e8a9 | 424 | /* tell GTK that data transfer was successful */ |
ab8884ac | 425 | gtk_drag_finish( context, TRUE, FALSE, time ); |
33a5bc52 | 426 | } |
5af019af RR |
427 | else |
428 | { | |
b2dea2ab | 429 | wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") ); |
8ee9d618 | 430 | |
3103e8a9 | 431 | /* tell GTK that data transfer was not successful */ |
5af019af RR |
432 | gtk_drag_finish( context, FALSE, FALSE, time ); |
433 | } | |
f03fc89f | 434 | |
5af019af RR |
435 | /* after this, invalidate the drop_target's drag data */ |
436 | drop_target->SetDragData( (GtkSelectionData*) NULL ); | |
33a5bc52 | 437 | } |
865bb325 | 438 | } |
33a5bc52 | 439 | |
4ba47b40 | 440 | //---------------------------------------------------------------------------- |
33a5bc52 | 441 | // wxDropTarget |
4ba47b40 | 442 | //---------------------------------------------------------------------------- |
33a5bc52 | 443 | |
8ee9d618 VZ |
444 | wxDropTarget::wxDropTarget( wxDataObject *data ) |
445 | : wxDropTargetBase( data ) | |
f5368809 | 446 | { |
0a164d4c | 447 | m_firstMotion = true; |
829e3e8d RR |
448 | m_dragContext = (GdkDragContext*) NULL; |
449 | m_dragWidget = (GtkWidget*) NULL; | |
5af019af | 450 | m_dragData = (GtkSelectionData*) NULL; |
829e3e8d | 451 | m_dragTime = 0; |
f5368809 RR |
452 | } |
453 | ||
c9057ae1 VZ |
454 | wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x), |
455 | wxCoord WXUNUSED(y), | |
456 | wxDragResult def ) | |
d6086ea6 | 457 | { |
8ee9d618 | 458 | // GetMatchingPair() checks for m_dataObject too, no need to do it here |
2edc8f5b VZ |
459 | |
460 | // disable the debug message from GetMatchingPair() - there are too many | |
461 | // of them otherwise | |
c4fda16b | 462 | #ifdef __WXDEBUG__ |
2edc8f5b VZ |
463 | wxLogNull noLog; |
464 | #endif // Debug | |
465 | ||
c9057ae1 | 466 | return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone; |
d6086ea6 RR |
467 | } |
468 | ||
c9057ae1 | 469 | bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) ) |
d6086ea6 | 470 | { |
b068c4e8 | 471 | if (!m_dataObject) |
0a164d4c | 472 | return false; |
8ee9d618 | 473 | |
8e00741d | 474 | return (GetMatchingPair() != (GdkAtom) 0); |
d6086ea6 RR |
475 | } |
476 | ||
8ee9d618 VZ |
477 | wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), |
478 | wxDragResult def ) | |
5af019af | 479 | { |
b068c4e8 | 480 | if (!m_dataObject) |
11e1c70d | 481 | return wxDragNone; |
8ee9d618 | 482 | |
8e00741d | 483 | if (GetMatchingPair() == (GdkAtom) 0) |
11e1c70d | 484 | return wxDragNone; |
8ee9d618 VZ |
485 | |
486 | return GetData() ? def : wxDragNone; | |
5af019af RR |
487 | } |
488 | ||
8e00741d | 489 | GdkAtom wxDropTarget::GetMatchingPair() |
5af019af | 490 | { |
8ee9d618 | 491 | if (!m_dataObject) |
8e00741d | 492 | return (GdkAtom) 0; |
5af019af | 493 | |
8ee9d618 | 494 | if (!m_dragContext) |
8e00741d | 495 | return (GdkAtom) 0; |
f03fc89f | 496 | |
22d5903e RR |
497 | GList *child = m_dragContext->targets; |
498 | while (child) | |
499 | { | |
68567a96 | 500 | GdkAtom formatAtom = (GdkAtom)(child->data); |
2edc8f5b | 501 | wxDataFormat format( formatAtom ); |
f03fc89f | 502 | |
a3e7d24d | 503 | #ifdef __WXDEBUG__ |
b2dea2ab VZ |
504 | wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"), |
505 | format.GetId().c_str()); | |
2edc8f5b VZ |
506 | #endif // Debug |
507 | ||
b068c4e8 | 508 | if (m_dataObject->IsSupportedFormat( format )) |
2edc8f5b | 509 | return formatAtom; |
f03fc89f | 510 | |
22d5903e RR |
511 | child = child->next; |
512 | } | |
829e3e8d | 513 | |
8e00741d | 514 | return (GdkAtom) 0; |
d6086ea6 | 515 | } |
f03fc89f | 516 | |
8e00741d | 517 | bool wxDropTarget::GetData() |
d6086ea6 | 518 | { |
8ee9d618 | 519 | if (!m_dragData) |
0a164d4c | 520 | return false; |
f03fc89f | 521 | |
8ee9d618 | 522 | if (!m_dataObject) |
0a164d4c | 523 | return false; |
f03fc89f | 524 | |
8e00741d | 525 | wxDataFormat dragFormat( m_dragData->target ); |
8ee9d618 | 526 | |
b068c4e8 | 527 | if (!m_dataObject->IsSupportedFormat( dragFormat )) |
0a164d4c | 528 | return false; |
f03fc89f | 529 | |
97c79de2 RR |
530 | m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data ); |
531 | ||
0a164d4c | 532 | return true; |
d6086ea6 | 533 | } |
f03fc89f | 534 | |
f5368809 RR |
535 | void wxDropTarget::UnregisterWidget( GtkWidget *widget ) |
536 | { | |
223d09f6 | 537 | wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") ); |
f03fc89f | 538 | |
829e3e8d | 539 | gtk_drag_dest_unset( widget ); |
f03fc89f | 540 | |
9fa72bd2 MR |
541 | g_signal_handlers_disconnect_by_func (widget, |
542 | (gpointer) target_drag_leave, this); | |
543 | g_signal_handlers_disconnect_by_func (widget, | |
544 | (gpointer) target_drag_motion, this); | |
545 | g_signal_handlers_disconnect_by_func (widget, | |
546 | (gpointer) target_drag_drop, this); | |
547 | g_signal_handlers_disconnect_by_func (widget, | |
548 | (gpointer) target_drag_data_received, this); | |
f5368809 RR |
549 | } |
550 | ||
551 | void wxDropTarget::RegisterWidget( GtkWidget *widget ) | |
552 | { | |
223d09f6 | 553 | wxCHECK_RET( widget != NULL, wxT("register widget is NULL") ); |
f03fc89f | 554 | |
829e3e8d RR |
555 | /* gtk_drag_dest_set() determines what default behaviour we'd like |
556 | GTK to supply. we don't want to specify out targets (=formats) | |
557 | or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and | |
558 | not GTK_DEST_DEFAULT_DROP). instead we react individually to | |
559 | "drag_motion" and "drag_drop" events. this makes it possible | |
f03fc89f | 560 | to allow dropping on only a small area. we should set |
829e3e8d RR |
561 | GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice |
562 | highlighting if dragging over standard controls, but this | |
563 | seems to be broken without the other two. */ | |
f03fc89f | 564 | |
d6086ea6 | 565 | gtk_drag_dest_set( widget, |
f03fc89f VZ |
566 | (GtkDestDefaults) 0, /* no default behaviour */ |
567 | (GtkTargetEntry*) NULL, /* we don't supply any formats here */ | |
568 | 0, /* number of targets = 0 */ | |
569 | (GdkDragAction) 0 ); /* we don't supply any actions here */ | |
570 | ||
9fa72bd2 MR |
571 | g_signal_connect (widget, "drag_leave", |
572 | G_CALLBACK (target_drag_leave), this); | |
33a5bc52 | 573 | |
9fa72bd2 MR |
574 | g_signal_connect (widget, "drag_motion", |
575 | G_CALLBACK (target_drag_motion), this); | |
33a5bc52 | 576 | |
9fa72bd2 MR |
577 | g_signal_connect (widget, "drag_drop", |
578 | G_CALLBACK (target_drag_drop), this); | |
33a5bc52 | 579 | |
9fa72bd2 MR |
580 | g_signal_connect (widget, "drag_data_received", |
581 | G_CALLBACK (target_drag_data_received), this); | |
f5368809 RR |
582 | } |
583 | ||
4ba47b40 RR |
584 | //---------------------------------------------------------------------------- |
585 | // "drag_data_get" | |
586 | //---------------------------------------------------------------------------- | |
587 | ||
865bb325 | 588 | extern "C" { |
f03fc89f | 589 | static void |
4ba47b40 | 590 | source_drag_data_get (GtkWidget *WXUNUSED(widget), |
b02da6b1 | 591 | GdkDragContext *WXUNUSED(context), |
f03fc89f VZ |
592 | GtkSelectionData *selection_data, |
593 | guint WXUNUSED(info), | |
594 | guint WXUNUSED(time), | |
595 | wxDropSource *drop_source ) | |
4ba47b40 | 596 | { |
5549fa65 RR |
597 | if (g_isIdle) wxapp_install_idle_handler(); |
598 | ||
97c79de2 | 599 | wxDataFormat format( selection_data->target ); |
8ee9d618 | 600 | |
b2dea2ab VZ |
601 | wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"), |
602 | format.GetId().c_str()); | |
f6bcfd97 | 603 | |
1dd989e1 | 604 | drop_source->m_retValue = wxDragCancel; |
8ee9d618 | 605 | |
97c79de2 | 606 | wxDataObject *data = drop_source->GetDataObject(); |
f6bcfd97 | 607 | |
1dd989e1 | 608 | if (!data) |
97c79de2 | 609 | { |
b2dea2ab | 610 | wxLogTrace(TRACE_DND, wxT("Drop source: no data object") ); |
2edc8f5b | 611 | return; |
97c79de2 | 612 | } |
1dd989e1 | 613 | |
97c79de2 RR |
614 | if (!data->IsSupportedFormat(format)) |
615 | { | |
b2dea2ab | 616 | wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") ); |
2edc8f5b | 617 | return; |
97c79de2 | 618 | } |
f03fc89f | 619 | |
97c79de2 RR |
620 | if (data->GetDataSize(format) == 0) |
621 | { | |
b2dea2ab | 622 | wxLogTrace(TRACE_DND, wxT("Drop source: empty data") ); |
2edc8f5b | 623 | return; |
97c79de2 | 624 | } |
8ee9d618 | 625 | |
97c79de2 | 626 | size_t size = data->GetDataSize(format); |
f03fc89f | 627 | |
1dd989e1 | 628 | // printf( "data size: %d.\n", (int)data_size ); |
f03fc89f | 629 | |
1dd989e1 | 630 | guchar *d = new guchar[size]; |
8ee9d618 | 631 | |
97c79de2 | 632 | if (!data->GetDataHere( format, (void*)d )) |
1dd989e1 | 633 | { |
97c79de2 | 634 | delete[] d; |
2edc8f5b | 635 | return; |
1dd989e1 | 636 | } |
f03fc89f | 637 | |
b453e1b2 | 638 | #if wxUSE_THREADS |
1dd989e1 | 639 | /* disable GUI threads */ |
b453e1b2 | 640 | #endif |
8e193f38 | 641 | |
0a164d4c WS |
642 | gtk_selection_data_set( selection_data, |
643 | selection_data->target, | |
644 | 8, // 8-bit | |
645 | d, | |
646 | size ); | |
f03fc89f | 647 | |
b453e1b2 | 648 | #if wxUSE_THREADS |
1dd989e1 | 649 | /* enable GUI threads */ |
b453e1b2 | 650 | #endif |
f03fc89f | 651 | |
97c79de2 | 652 | delete[] d; |
4ba47b40 | 653 | } |
865bb325 | 654 | } |
f03fc89f | 655 | |
4ba47b40 RR |
656 | //---------------------------------------------------------------------------- |
657 | // "drag_data_delete" | |
658 | //---------------------------------------------------------------------------- | |
659 | ||
865bb325 | 660 | extern "C" { |
9f39393d | 661 | static void source_drag_data_delete( GtkWidget *WXUNUSED(widget), |
4eb8ef2d | 662 | GdkDragContext *WXUNUSED(context), |
9f39393d | 663 | wxDropSource *WXUNUSED(drop_source) ) |
4ba47b40 | 664 | { |
f6bcfd97 | 665 | if (g_isIdle) |
13fb7b7a | 666 | wxapp_install_idle_handler(); |
33a5bc52 | 667 | |
13fb7b7a | 668 | // printf( "Drag source: drag_data_delete\n" ); |
4ba47b40 | 669 | } |
865bb325 | 670 | } |
f03fc89f | 671 | |
4ba47b40 RR |
672 | //---------------------------------------------------------------------------- |
673 | // "drag_begin" | |
674 | //---------------------------------------------------------------------------- | |
675 | ||
865bb325 | 676 | extern "C" { |
4ba47b40 | 677 | static void source_drag_begin( GtkWidget *WXUNUSED(widget), |
f03fc89f VZ |
678 | GdkDragContext *WXUNUSED(context), |
679 | wxDropSource *WXUNUSED(drop_source) ) | |
4ba47b40 | 680 | { |
f6bcfd97 | 681 | if (g_isIdle) |
13fb7b7a | 682 | wxapp_install_idle_handler(); |
5549fa65 | 683 | |
13fb7b7a | 684 | // printf( "Drag source: drag_begin.\n" ); |
4ba47b40 | 685 | } |
865bb325 | 686 | } |
f03fc89f | 687 | |
4ba47b40 RR |
688 | //---------------------------------------------------------------------------- |
689 | // "drag_end" | |
690 | //---------------------------------------------------------------------------- | |
691 | ||
865bb325 | 692 | extern "C" { |
4ba47b40 | 693 | static void source_drag_end( GtkWidget *WXUNUSED(widget), |
f03fc89f VZ |
694 | GdkDragContext *WXUNUSED(context), |
695 | wxDropSource *drop_source ) | |
4ba47b40 | 696 | { |
5549fa65 RR |
697 | if (g_isIdle) wxapp_install_idle_handler(); |
698 | ||
13fb7b7a | 699 | // printf( "Drag source: drag_end.\n" ); |
4ba47b40 | 700 | |
0a164d4c | 701 | drop_source->m_waiting = false; |
4ba47b40 | 702 | } |
865bb325 | 703 | } |
f03fc89f | 704 | |
7b5d5699 RR |
705 | //----------------------------------------------------------------------------- |
706 | // "configure_event" from m_iconWindow | |
707 | //----------------------------------------------------------------------------- | |
708 | ||
865bb325 | 709 | extern "C" { |
8ee9d618 | 710 | static gint |
7b5d5699 RR |
711 | gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source ) |
712 | { | |
14819684 | 713 | // don't need to install idle handler, its done from "event" signal |
7b5d5699 | 714 | |
2245b2b2 | 715 | source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) ); |
8ee9d618 | 716 | |
7b5d5699 RR |
717 | return 0; |
718 | } | |
865bb325 | 719 | } |
7b5d5699 | 720 | |
4ba47b40 RR |
721 | //--------------------------------------------------------------------------- |
722 | // wxDropSource | |
723 | //--------------------------------------------------------------------------- | |
22d5903e | 724 | |
f6bcfd97 BP |
725 | wxDropSource::wxDropSource(wxWindow *win, |
726 | const wxIcon &iconCopy, | |
727 | const wxIcon &iconMove, | |
728 | const wxIcon &iconNone) | |
22d5903e | 729 | { |
0a164d4c | 730 | m_waiting = true; |
f03fc89f | 731 | |
7b5d5699 | 732 | m_iconWindow = (GtkWidget*) NULL; |
8ee9d618 | 733 | |
22d5903e | 734 | m_window = win; |
a2053b27 RR |
735 | m_widget = win->m_widget; |
736 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
f03fc89f | 737 | |
22d5903e RR |
738 | m_retValue = wxDragCancel; |
739 | ||
f6bcfd97 | 740 | SetIcons(iconCopy, iconMove, iconNone); |
22d5903e RR |
741 | } |
742 | ||
f6bcfd97 BP |
743 | wxDropSource::wxDropSource(wxDataObject& data, |
744 | wxWindow *win, | |
745 | const wxIcon &iconCopy, | |
746 | const wxIcon &iconMove, | |
747 | const wxIcon &iconNone) | |
22d5903e | 748 | { |
0a164d4c | 749 | m_waiting = true; |
8ee9d618 | 750 | |
b068c4e8 | 751 | SetData( data ); |
f03fc89f | 752 | |
7b5d5699 | 753 | m_iconWindow = (GtkWidget*) NULL; |
8ee9d618 | 754 | |
22d5903e | 755 | m_window = win; |
a2053b27 RR |
756 | m_widget = win->m_widget; |
757 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
8ee9d618 | 758 | |
22d5903e | 759 | m_retValue = wxDragCancel; |
f03fc89f | 760 | |
f6bcfd97 BP |
761 | SetIcons(iconCopy, iconMove, iconNone); |
762 | } | |
763 | ||
764 | void wxDropSource::SetIcons(const wxIcon &iconCopy, | |
765 | const wxIcon &iconMove, | |
766 | const wxIcon &iconNone) | |
767 | { | |
768 | m_iconCopy = iconCopy; | |
769 | m_iconMove = iconMove; | |
770 | m_iconNone = iconNone; | |
771 | ||
772 | if ( !m_iconCopy.Ok() ) | |
773 | m_iconCopy = wxIcon(page_xpm); | |
774 | if ( !m_iconMove.Ok() ) | |
775 | m_iconMove = m_iconCopy; | |
776 | if ( !m_iconNone.Ok() ) | |
777 | m_iconNone = m_iconCopy; | |
22d5903e RR |
778 | } |
779 | ||
8e193f38 | 780 | wxDropSource::~wxDropSource() |
22d5903e | 781 | { |
22d5903e | 782 | } |
f03fc89f | 783 | |
f6bcfd97 | 784 | void wxDropSource::PrepareIcon( int action, GdkDragContext *context ) |
7b5d5699 | 785 | { |
f6bcfd97 BP |
786 | // get the right icon to display |
787 | wxIcon *icon = NULL; | |
788 | if ( action & GDK_ACTION_MOVE ) | |
789 | icon = &m_iconMove; | |
790 | else if ( action & GDK_ACTION_COPY ) | |
791 | icon = &m_iconCopy; | |
792 | else | |
793 | icon = &m_iconNone; | |
794 | ||
795 | GdkBitmap *mask; | |
796 | if ( icon->GetMask() ) | |
797 | mask = icon->GetMask()->GetBitmap(); | |
798 | else | |
799 | mask = (GdkBitmap *)NULL; | |
800 | ||
801 | GdkPixmap *pixmap = icon->GetPixmap(); | |
7b5d5699 RR |
802 | |
803 | gint width,height; | |
791d7ea2 | 804 | gdk_drawable_get_size (pixmap, &width, &height); |
7b5d5699 RR |
805 | |
806 | GdkColormap *colormap = gtk_widget_get_colormap( m_widget ); | |
7b5d5699 RR |
807 | gtk_widget_push_colormap (colormap); |
808 | ||
809 | m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP); | |
810 | gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); | |
811 | gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE); | |
812 | ||
7b5d5699 RR |
813 | gtk_widget_pop_colormap (); |
814 | ||
370dc79c | 815 | gtk_widget_set_size_request (m_iconWindow, width, height); |
7b5d5699 RR |
816 | gtk_widget_realize (m_iconWindow); |
817 | ||
9fa72bd2 MR |
818 | g_signal_connect (m_iconWindow, "configure_event", |
819 | G_CALLBACK (gtk_dnd_window_configure_callback), this); | |
8ee9d618 | 820 | |
7b5d5699 | 821 | gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE); |
8ee9d618 | 822 | |
7b5d5699 RR |
823 | if (mask) |
824 | gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0); | |
825 | ||
f6bcfd97 | 826 | gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 ); |
7b5d5699 RR |
827 | } |
828 | ||
2245b2b2 | 829 | wxDragResult wxDropSource::DoDragDrop(int flags) |
22d5903e | 830 | { |
2245b2b2 VZ |
831 | wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone, |
832 | wxT("Drop source: no data") ); | |
8ee9d618 | 833 | |
513abb88 RR |
834 | // still in drag |
835 | if (g_blockEventsOnDrag) | |
2245b2b2 | 836 | return wxDragNone; |
b2dea2ab | 837 | |
f6bcfd97 | 838 | // disabled for now |
0a164d4c | 839 | g_blockEventsOnDrag = true; |
f03fc89f | 840 | |
4ba47b40 | 841 | RegisterWindow(); |
f03fc89f | 842 | |
0a164d4c | 843 | m_waiting = true; |
22d5903e | 844 | |
4ba47b40 | 845 | GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 ); |
8ee9d618 | 846 | |
a3e7d24d | 847 | wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; |
b068c4e8 | 848 | m_data->GetAllFormats( array ); |
2245b2b2 VZ |
849 | size_t count = m_data->GetFormatCount(); |
850 | for (size_t i = 0; i < count; i++) | |
a3e7d24d RR |
851 | { |
852 | GdkAtom atom = array[i]; | |
b2dea2ab | 853 | wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom )); |
a3e7d24d RR |
854 | gtk_target_list_add( target_list, atom, 0, 0 ); |
855 | } | |
856 | delete[] array; | |
f03fc89f | 857 | |
4ba47b40 | 858 | /* don't start dragging if no button is down */ |
8f9850dd | 859 | if (g_lastButtonNumber) |
22d5903e | 860 | { |
9f39393d | 861 | int action = GDK_ACTION_COPY; |
2245b2b2 | 862 | if ( flags & wxDrag_AllowMove ) |
9f39393d | 863 | action |= GDK_ACTION_MOVE; |
2245b2b2 VZ |
864 | |
865 | // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad | |
866 | // to use a global to pass the flags to the drop target but I'd | |
867 | // surely prefer a better way to do it | |
868 | gs_flagsForDrag = flags; | |
869 | ||
4ba47b40 | 870 | GdkDragContext *context = gtk_drag_begin( m_widget, |
f6bcfd97 | 871 | target_list, |
46c87a9a | 872 | (GdkDragAction)action, |
8f9850dd RR |
873 | g_lastButtonNumber, // number of mouse button which started drag |
874 | (GdkEvent*) g_lastMouseEvent ); | |
8ee9d618 VZ |
875 | |
876 | m_dragContext = context; | |
877 | ||
f6bcfd97 | 878 | PrepareIcon( action, context ); |
f03fc89f | 879 | |
2245b2b2 VZ |
880 | while (m_waiting) |
881 | gtk_main_iteration(); | |
f6bcfd97 | 882 | |
2245b2b2 VZ |
883 | m_retValue = ConvertFromGTK(context->action); |
884 | if ( m_retValue == wxDragNone ) | |
885 | m_retValue = wxDragCancel; | |
4ba47b40 | 886 | } |
22d5903e | 887 | |
0a164d4c | 888 | g_blockEventsOnDrag = false; |
f03fc89f | 889 | |
4ba47b40 RR |
890 | UnregisterWindow(); |
891 | ||
892 | return m_retValue; | |
22d5903e RR |
893 | } |
894 | ||
4ba47b40 | 895 | void wxDropSource::RegisterWindow() |
22d5903e | 896 | { |
4ba47b40 | 897 | if (!m_widget) return; |
f6bcfd97 | 898 | |
9fa72bd2 MR |
899 | g_signal_connect (m_widget, "drag_data_get", |
900 | G_CALLBACK (source_drag_data_get), this); | |
901 | g_signal_connect (m_widget, "drag_data_delete", | |
902 | G_CALLBACK (source_drag_data_delete), this); | |
903 | g_signal_connect (m_widget, "drag_begin", | |
904 | G_CALLBACK (source_drag_begin), this); | |
905 | g_signal_connect (m_widget, "drag_end", | |
906 | G_CALLBACK (source_drag_end), this); | |
4ba47b40 | 907 | |
22d5903e RR |
908 | } |
909 | ||
4ba47b40 | 910 | void wxDropSource::UnregisterWindow() |
22d5903e | 911 | { |
9fa72bd2 MR |
912 | if (!m_widget) |
913 | return; | |
f03fc89f | 914 | |
9fa72bd2 MR |
915 | g_signal_handlers_disconnect_by_func (m_widget, |
916 | (gpointer) source_drag_data_get, | |
917 | this); | |
918 | g_signal_handlers_disconnect_by_func (m_widget, | |
919 | (gpointer) source_drag_data_delete, | |
920 | this); | |
921 | g_signal_handlers_disconnect_by_func (m_widget, | |
922 | (gpointer) source_drag_begin, | |
923 | this); | |
924 | g_signal_handlers_disconnect_by_func (m_widget, | |
925 | (gpointer) source_drag_end, | |
926 | this); | |
22d5903e RR |
927 | } |
928 | ||
ac57418f | 929 | #endif |
93c5dd39 | 930 | // wxUSE_DRAG_AND_DROP |