made definition of wxUSE_LOG_DEBUG dependent on wxDEBUG_LEVEL and added wxUSE_LOG_TRA...
[wxWidgets.git] / src / gtk / dnd.cpp
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
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 // 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
215 wxDragResult result;
216 if (drop_target->GetDefaultAction() == wxDragNone)
217 {
218 // use default action set by wxDropSource::DoDragDrop()
219 if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove &&
220 (context->actions & GDK_ACTION_MOVE ) )
221 {
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 }
235 }
236 }
237 else if (drop_target->GetDefaultAction() == wxDragMove &&
238 (context->actions & GDK_ACTION_MOVE))
239 {
240
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 }
252
253 if (drop_target->m_firstMotion)
254 {
255 /* the first "drag_motion" event substitutes a "drag_enter" event */
256 result = drop_target->OnEnter( x, y, result );
257 }
258 else
259 {
260 /* give program a chance to react (i.e. to say no by returning FALSE) */
261 result = drop_target->OnDragOver( x, y, result );
262 }
263
264 bool ret = wxIsDragResultOk( result );
265 if (ret)
266 {
267 GdkDragAction action;
268 if (result == wxDragCopy)
269 action = GDK_ACTION_COPY;
270 else if (result == wxDragLink)
271 action = GDK_ACTION_LINK;
272 else
273 action = GDK_ACTION_MOVE;
274
275 gdk_drag_status( context, action, time );
276 }
277
278 /* after this, invalidate the drop_target's GdkDragContext */
279 drop_target->GtkSetDragContext( NULL );
280
281 /* this has to be done because GDK has no "drag_enter" event */
282 drop_target->m_firstMotion = false;
283
284 return ret;
285 }
286 }
287
288 // ----------------------------------------------------------------------------
289 // "drag_drop"
290 // ----------------------------------------------------------------------------
291
292 extern "C" {
293 static gboolean target_drag_drop( GtkWidget *widget,
294 GdkDragContext *context,
295 gint x,
296 gint y,
297 guint time,
298 wxDropTarget *drop_target )
299 {
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()" */
304
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.. */
308
309 /* inform the wxDropTarget about the current GdkDragContext.
310 this is only valid for the duration of this call */
311 drop_target->GtkSetDragContext( context );
312
313 /* inform the wxDropTarget about the current drag widget.
314 this is only valid for the duration of this call */
315 drop_target->GtkSetDragWidget( widget );
316
317 /* inform the wxDropTarget about the current drag time.
318 this is only valid for the duration of this call */
319 drop_target->GtkSetDragTime( time );
320
321 /*
322 wxDragResult result = wxDragMove;
323 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
324 */
325
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 */
329 g_blockEventsOnDrag = false;
330
331 bool ret = drop_target->OnDrop( x, y );
332
333 if (!ret)
334 {
335 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
336
337 /* cancel the whole thing */
338 gtk_drag_finish( context,
339 FALSE, /* no success */
340 FALSE, /* don't delete data on dropping side */
341 time );
342 }
343 else
344 {
345 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") );
346
347 #if wxUSE_THREADS
348 /* disable GUI threads */
349 #endif
350
351 GdkAtom format = drop_target->GtkGetMatchingPair();
352
353 // this does happen somehow, see bug 555111
354 wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") );
355
356 /*
357 GdkDragAction action = GDK_ACTION_MOVE;
358 if (result == wxDragCopy) action == GDK_ACTION_COPY;
359 context->action = action;
360 */
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 */
369 #endif
370 }
371
372 /* after this, invalidate the drop_target's GdkDragContext */
373 drop_target->GtkSetDragContext( NULL );
374
375 /* after this, invalidate the drop_target's drag widget */
376 drop_target->GtkSetDragWidget( NULL );
377
378 /* this has to be done because GDK has no "drag_enter" event */
379 drop_target->m_firstMotion = true;
380
381 return ret;
382 }
383 }
384
385 // ----------------------------------------------------------------------------
386 // "drag_data_received"
387 // ----------------------------------------------------------------------------
388
389 extern "C" {
390 static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
391 GdkDragContext *context,
392 gint x,
393 gint y,
394 GtkSelectionData *data,
395 guint WXUNUSED(info),
396 guint time,
397 wxDropTarget *drop_target )
398 {
399 /* Owen Taylor: "call gtk_drag_finish() with
400 success == TRUE" */
401
402 if ((data->length <= 0) || (data->format != 8))
403 {
404 /* negative data length and non 8-bit data format
405 qualifies for junk */
406 gtk_drag_finish (context, FALSE, FALSE, time);
407
408 return;
409 }
410
411 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
412
413 /* inform the wxDropTarget about the current GtkSelectionData.
414 this is only valid for the duration of this call */
415 drop_target->GtkSetDragData( data );
416
417 wxDragResult result = ConvertFromGTK(context->action);
418
419 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
420 {
421 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") );
422
423 /* tell GTK that data transfer was successful */
424 gtk_drag_finish( context, TRUE, FALSE, time );
425 }
426 else
427 {
428 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
429
430 /* tell GTK that data transfer was not successful */
431 gtk_drag_finish( context, FALSE, FALSE, time );
432 }
433
434 /* after this, invalidate the drop_target's drag data */
435 drop_target->GtkSetDragData( NULL );
436 }
437 }
438
439 //----------------------------------------------------------------------------
440 // wxDropTarget
441 //----------------------------------------------------------------------------
442
443 wxDropTarget::wxDropTarget( wxDataObject *data )
444 : wxDropTargetBase( data )
445 {
446 m_firstMotion = true;
447 m_dragContext = NULL;
448 m_dragWidget = NULL;
449 m_dragData = NULL;
450 m_dragTime = 0;
451 }
452
453 wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
454 wxCoord WXUNUSED(y),
455 wxDragResult def )
456 {
457 // GetMatchingPair() checks for m_dataObject too, no need to do it here
458
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;
462 }
463
464 bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
465 {
466 if (!m_dataObject)
467 return false;
468
469 return (GtkGetMatchingPair() != (GdkAtom) 0);
470 }
471
472 wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
473 wxDragResult def )
474 {
475 if (!m_dataObject)
476 return wxDragNone;
477
478 if (GtkGetMatchingPair() == (GdkAtom) 0)
479 return wxDragNone;
480
481 return GetData() ? def : wxDragNone;
482 }
483
484
485 wxDataFormat wxDropTarget::GetMatchingPair()
486 {
487 return wxDataFormat( GtkGetMatchingPair() );
488 }
489
490 GdkAtom wxDropTarget::GtkGetMatchingPair(bool quiet)
491 {
492 if (!m_dataObject)
493 return (GdkAtom) 0;
494
495 if (!m_dragContext)
496 return (GdkAtom) 0;
497
498 GList *child = m_dragContext->targets;
499 while (child)
500 {
501 GdkAtom formatAtom = (GdkAtom)(child->data);
502 wxDataFormat format( formatAtom );
503
504 if ( !quiet )
505 {
506 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
507 format.GetId().c_str());
508 }
509
510 if (m_dataObject->IsSupportedFormat( format ))
511 return formatAtom;
512
513 child = child->next;
514 }
515
516 return (GdkAtom) 0;
517 }
518
519 bool wxDropTarget::GetData()
520 {
521 if (!m_dragData)
522 return false;
523
524 if (!m_dataObject)
525 return false;
526
527 wxDataFormat dragFormat( m_dragData->target );
528
529 if (!m_dataObject->IsSupportedFormat( dragFormat ))
530 return false;
531
532 m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
533
534 return true;
535 }
536
537 void wxDropTarget::GtkUnregisterWidget( GtkWidget *widget )
538 {
539 wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
540
541 gtk_drag_dest_unset( widget );
542
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);
551 }
552
553 void wxDropTarget::GtkRegisterWidget( GtkWidget *widget )
554 {
555 wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
556
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
562 to allow dropping on only a small area. we should set
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. */
566
567 gtk_drag_dest_set( widget,
568 (GtkDestDefaults) 0, /* no default behaviour */
569 NULL, /* we don't supply any formats here */
570 0, /* number of targets = 0 */
571 (GdkDragAction) 0 ); /* we don't supply any actions here */
572
573 g_signal_connect (widget, "drag_leave",
574 G_CALLBACK (target_drag_leave), this);
575
576 g_signal_connect (widget, "drag_motion",
577 G_CALLBACK (target_drag_motion), this);
578
579 g_signal_connect (widget, "drag_drop",
580 G_CALLBACK (target_drag_drop), this);
581
582 g_signal_connect (widget, "drag_data_received",
583 G_CALLBACK (target_drag_data_received), this);
584 }
585
586 //----------------------------------------------------------------------------
587 // "drag_data_get"
588 //----------------------------------------------------------------------------
589
590 extern "C" {
591 static void
592 source_drag_data_get (GtkWidget *WXUNUSED(widget),
593 GdkDragContext *WXUNUSED(context),
594 GtkSelectionData *selection_data,
595 guint WXUNUSED(info),
596 guint WXUNUSED(time),
597 wxDropSource *drop_source )
598 {
599 wxDataFormat format( selection_data->target );
600
601 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
602 format.GetId().c_str());
603
604 drop_source->m_retValue = wxDragCancel;
605
606 wxDataObject *data = drop_source->GetDataObject();
607
608 if (!data)
609 {
610 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
611 return;
612 }
613
614 if (!data->IsSupportedFormat(format))
615 {
616 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
617 return;
618 }
619
620 if (data->GetDataSize(format) == 0)
621 {
622 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
623 return;
624 }
625
626 size_t size = data->GetDataSize(format);
627
628 // printf( "data size: %d.\n", (int)data_size );
629
630 guchar *d = new guchar[size];
631
632 if (!data->GetDataHere( format, (void*)d ))
633 {
634 delete[] d;
635 return;
636 }
637
638 #if wxUSE_THREADS
639 /* disable GUI threads */
640 #endif
641
642 gtk_selection_data_set( selection_data,
643 selection_data->target,
644 8, // 8-bit
645 d,
646 size );
647
648 #if wxUSE_THREADS
649 /* enable GUI threads */
650 #endif
651
652 delete[] d;
653 }
654 }
655
656 //----------------------------------------------------------------------------
657 // "drag_end"
658 //----------------------------------------------------------------------------
659
660 extern "C" {
661 static void source_drag_end( GtkWidget *WXUNUSED(widget),
662 GdkDragContext *WXUNUSED(context),
663 wxDropSource *drop_source )
664 {
665 // printf( "Drag source: drag_end.\n" );
666
667 drop_source->m_waiting = false;
668 }
669 }
670
671 //-----------------------------------------------------------------------------
672 // "configure_event" from m_iconWindow
673 //-----------------------------------------------------------------------------
674
675 extern "C" {
676 static gint
677 gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
678 {
679 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
680
681 return 0;
682 }
683 }
684
685 //---------------------------------------------------------------------------
686 // wxDropSource
687 //---------------------------------------------------------------------------
688
689 wxDropSource::wxDropSource(wxWindow *win,
690 const wxIcon &iconCopy,
691 const wxIcon &iconMove,
692 const wxIcon &iconNone)
693 {
694 m_waiting = true;
695
696 m_iconWindow = NULL;
697
698 m_window = win;
699 m_widget = win->m_widget;
700 if (win->m_wxwindow) m_widget = win->m_wxwindow;
701
702 m_retValue = wxDragCancel;
703
704 SetIcons(iconCopy, iconMove, iconNone);
705 }
706
707 wxDropSource::wxDropSource(wxDataObject& data,
708 wxWindow *win,
709 const wxIcon &iconCopy,
710 const wxIcon &iconMove,
711 const wxIcon &iconNone)
712 {
713 m_waiting = true;
714
715 SetData( data );
716
717 m_iconWindow = NULL;
718
719 m_window = win;
720 m_widget = win->m_widget;
721 if (win->m_wxwindow) m_widget = win->m_wxwindow;
722
723 m_retValue = wxDragCancel;
724
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;
742 }
743
744 wxDropSource::~wxDropSource()
745 {
746 }
747
748 void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
749 {
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
763 mask = NULL;
764
765 GdkPixmap *pixmap = icon->GetPixmap();
766
767 gint width,height;
768 gdk_drawable_get_size (pixmap, &width, &height);
769
770 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
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);
775 gtk_widget_set_app_paintable (m_iconWindow, TRUE);
776
777 gtk_widget_pop_colormap ();
778
779 gtk_widget_set_size_request (m_iconWindow, width, height);
780 gtk_widget_realize (m_iconWindow);
781
782 g_signal_connect (m_iconWindow, "configure_event",
783 G_CALLBACK (gtk_dnd_window_configure_callback), this);
784
785 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
786
787 if (mask)
788 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
789
790 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
791 }
792
793 wxDragResult wxDropSource::DoDragDrop(int flags)
794 {
795 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
796 wxT("Drop source: no data") );
797
798 // still in drag
799 if (g_blockEventsOnDrag)
800 return wxDragNone;
801
802 // don't start dragging if no button is down
803 if (g_lastButtonNumber == 0)
804 return wxDragNone;
805
806 // we can only start a drag after a mouse event
807 if (g_lastMouseEvent == NULL)
808 return wxDragNone;
809
810 GTKConnectDragSignals();
811 wxON_BLOCK_EXIT_OBJ0(*this, wxDropSource::GTKDisconnectDragSignals);
812
813 m_waiting = true;
814
815 GtkTargetList *target_list = gtk_target_list_new( NULL, 0 );
816
817 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
818 m_data->GetAllFormats( array );
819 size_t count = m_data->GetFormatCount();
820 for (size_t i = 0; i < count; i++)
821 {
822 GdkAtom atom = array[i];
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 );
826 }
827 delete[] array;
828
829 int action = GDK_ACTION_COPY;
830 if ( flags & wxDrag_AllowMove )
831 action |= GDK_ACTION_MOVE;
832
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;
837
838 GdkDragContext *context = gtk_drag_begin( m_widget,
839 target_list,
840 (GdkDragAction)action,
841 g_lastButtonNumber, // number of mouse button which started drag
842 (GdkEvent*) g_lastMouseEvent );
843
844 if ( !context )
845 {
846 // this can happen e.g. if gdk_pointer_grab() failed
847 return wxDragError;
848 }
849
850 m_dragContext = context;
851
852 PrepareIcon( action, context );
853
854 while (m_waiting)
855 gtk_main_iteration();
856
857 m_retValue = ConvertFromGTK(context->action);
858 if ( m_retValue == wxDragNone )
859 m_retValue = wxDragCancel;
860
861 return m_retValue;
862 }
863
864 void wxDropSource::GTKConnectDragSignals()
865 {
866 if (!m_widget)
867 return;
868
869 g_blockEventsOnDrag = true;
870
871 g_signal_connect (m_widget, "drag_data_get",
872 G_CALLBACK (source_drag_data_get), this);
873 g_signal_connect (m_widget, "drag_end",
874 G_CALLBACK (source_drag_end), this);
875
876 }
877
878 void wxDropSource::GTKDisconnectDragSignals()
879 {
880 if (!m_widget)
881 return;
882
883 g_blockEventsOnDrag = false;
884
885 g_signal_handlers_disconnect_by_func (m_widget,
886 (gpointer) source_drag_data_get,
887 this);
888 g_signal_handlers_disconnect_by_func (m_widget,
889 (gpointer) source_drag_end,
890 this);
891 }
892
893 #endif
894 // wxUSE_DRAG_AND_DROP