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