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