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