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