]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dnd.cpp
replaced FIXME comment left in the last commit with an explanation
[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 #ifdef __WXDEBUG__
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 static const wxChar *TRACE_DND = _T("dnd");
44 #endif
45
46 // global variables because GTK+ DnD want to have the
47 // mouse event that caused it
48 extern GdkEvent *g_lastMouseEvent;
49 extern int g_lastButtonNumber;
50
51 //----------------------------------------------------------------------------
52 // standard icons
53 //----------------------------------------------------------------------------
54
55 /* Copyright (c) Julian Smart */
56 static const char * page_xpm[] = {
57 /* columns rows colors chars-per-pixel */
58 "32 32 37 1",
59 "5 c #7198D9",
60 ", c #769CDA",
61 "2 c #DCE6F6",
62 "i c #FFFFFF",
63 "e c #779DDB",
64 ": c #9AB6E4",
65 "9 c #EAF0FA",
66 "- c #B1C7EB",
67 "$ c #6992D7",
68 "y c #F7F9FD",
69 "= c #BED0EE",
70 "q c #F0F5FC",
71 "; c #A8C0E8",
72 "@ c #366BC2",
73 " c None",
74 "u c #FDFEFF",
75 "8 c #5987D3",
76 "* c #C4D5F0",
77 "7 c #7CA0DC",
78 "O c #487BCE",
79 "< c #6B94D7",
80 "& c #CCDAF2",
81 "> c #89A9DF",
82 "3 c #5584D1",
83 "w c #82A5DE",
84 "1 c #3F74CB",
85 "+ c #3A70CA",
86 ". c #3569BF",
87 "% c #D2DFF4",
88 "# c #3366BB",
89 "r c #F5F8FD",
90 "0 c #FAFCFE",
91 "4 c #DFE8F7",
92 "X c #5E8AD4",
93 "o c #5282D0",
94 "t c #B8CCEC",
95 "6 c #E5EDF9",
96 /* pixels */
97 " ",
98 " ",
99 " ",
100 " ",
101 " ",
102 " .XXXooOO++@# ",
103 " $%&*=-;::>,<1 ",
104 " $2%&*=-;::><:3 ",
105 " $42%&*=-;::<&:3 ",
106 " 56477<<<<8<<9&:X ",
107 " 59642%&*=-;<09&:5 ",
108 " 5q9642%&*=-<<<<<# ",
109 " 5qqw777<<<<<88:>+ ",
110 " erqq9642%&*=t;::+ ",
111 " eyrqq9642%&*=t;:O ",
112 " eyywwww777<<<<t;O ",
113 " e0yyrqq9642%&*=to ",
114 " e00yyrqq9642%&*=o ",
115 " eu0wwwwwww777<&*X ",
116 " euu00yyrqq9642%&X ",
117 " eiuu00yyrqq9642%X ",
118 " eiiwwwwwwwwww742$ ",
119 " eiiiuu00yyrqq964$ ",
120 " eiiiiuu00yyrqq96$ ",
121 " eiiiiiuu00yyrqq95 ",
122 " eiiiiiiuu00yyrqq5 ",
123 " eeeeeeeeeeeeee55e ",
124 " ",
125 " ",
126 " ",
127 " ",
128 " "
129 };
130
131
132 // ============================================================================
133 // private functions
134 // ============================================================================
135
136 // ----------------------------------------------------------------------------
137 // convert between GTK+ and wxWidgets DND constants
138 // ----------------------------------------------------------------------------
139
140 static wxDragResult ConvertFromGTK(long action)
141 {
142 switch ( action )
143 {
144 case GDK_ACTION_COPY:
145 return wxDragCopy;
146
147 case GDK_ACTION_LINK:
148 return wxDragLink;
149
150 case GDK_ACTION_MOVE:
151 return wxDragMove;
152 }
153
154 return wxDragNone;
155 }
156
157 // ----------------------------------------------------------------------------
158 // "drag_leave"
159 // ----------------------------------------------------------------------------
160
161 extern "C" {
162 static void target_drag_leave( GtkWidget *WXUNUSED(widget),
163 GdkDragContext *context,
164 guint WXUNUSED(time),
165 wxDropTarget *drop_target )
166 {
167 /* inform the wxDropTarget about the current GdkDragContext.
168 this is only valid for the duration of this call */
169 drop_target->SetDragContext( context );
170
171 /* we don't need return values. this event is just for
172 information */
173 drop_target->OnLeave();
174
175 /* this has to be done because GDK has no "drag_enter" event */
176 drop_target->m_firstMotion = true;
177
178 /* after this, invalidate the drop_target's GdkDragContext */
179 drop_target->SetDragContext( (GdkDragContext*) NULL );
180 }
181 }
182
183 // ----------------------------------------------------------------------------
184 // "drag_motion"
185 // ----------------------------------------------------------------------------
186
187 extern "C" {
188 static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget),
189 GdkDragContext *context,
190 gint x,
191 gint y,
192 guint time,
193 wxDropTarget *drop_target )
194 {
195 /* Owen Taylor: "if the coordinates not in a drop zone,
196 return FALSE, otherwise call gtk_drag_status() and
197 return TRUE" */
198
199 /* inform the wxDropTarget about the current GdkDragContext.
200 this is only valid for the duration of this call */
201 drop_target->SetDragContext( context );
202
203 // GTK+ always supposes that we want to copy the data by default while we
204 // might want to move it, so examine not only suggested_action - which is
205 // only good if we don't have our own preferences - but also the actions
206 // field
207 wxDragResult result;
208 if (drop_target->GetDefaultAction() == wxDragNone)
209 {
210 // use default action set by wxDropSource::DoDragDrop()
211 if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove &&
212 (context->actions & GDK_ACTION_MOVE ) )
213 {
214 // move is requested by the program and allowed by GTK+ - do it, even
215 // though suggested_action may be currently wxDragCopy
216 result = wxDragMove;
217 }
218 else // use whatever GTK+ says we should
219 {
220 result = ConvertFromGTK(context->suggested_action);
221
222 if ( (result == wxDragMove) && !(gs_flagsForDrag & wxDrag_AllowMove) )
223 {
224 // we're requested to move but we can't
225 result = wxDragCopy;
226 }
227 }
228 }
229 else if (drop_target->GetDefaultAction() == wxDragMove &&
230 (context->actions & GDK_ACTION_MOVE))
231 {
232 result = wxDragMove;
233 }
234 else
235 {
236 if (context->actions & GDK_ACTION_COPY)
237 result = wxDragCopy;
238 else if (context->actions & GDK_ACTION_MOVE)
239 result = wxDragMove;
240 else
241 result = wxDragNone;
242 }
243
244 if (drop_target->m_firstMotion)
245 {
246 /* the first "drag_motion" event substitutes a "drag_enter" event */
247 result = drop_target->OnEnter( x, y, result );
248 }
249 else
250 {
251 /* give program a chance to react (i.e. to say no by returning FALSE) */
252 result = drop_target->OnDragOver( x, y, result );
253 }
254
255 bool ret = wxIsDragResultOk( result );
256 if (ret)
257 {
258 GdkDragAction action;
259 if (result == wxDragCopy)
260 action = GDK_ACTION_COPY;
261 else if (result == wxDragLink)
262 action = GDK_ACTION_LINK;
263 else
264 action = GDK_ACTION_MOVE;
265
266 gdk_drag_status( context, action, time );
267 }
268
269 /* after this, invalidate the drop_target's GdkDragContext */
270 drop_target->SetDragContext( (GdkDragContext*) NULL );
271
272 /* this has to be done because GDK has no "drag_enter" event */
273 drop_target->m_firstMotion = false;
274
275 return ret;
276 }
277 }
278
279 // ----------------------------------------------------------------------------
280 // "drag_drop"
281 // ----------------------------------------------------------------------------
282
283 extern "C" {
284 static gboolean target_drag_drop( GtkWidget *widget,
285 GdkDragContext *context,
286 gint x,
287 gint y,
288 guint time,
289 wxDropTarget *drop_target )
290 {
291 /* Owen Taylor: "if the drop is not in a drop zone,
292 return FALSE, otherwise, if you aren't accepting
293 the drop, call gtk_drag_finish() with success == FALSE
294 otherwise call gtk_drag_data_get()" */
295
296 // printf( "drop.\n" );
297
298 /* this seems to make a difference between not accepting
299 due to wrong target area and due to wrong format. let
300 us hope that this is not required.. */
301
302 /* inform the wxDropTarget about the current GdkDragContext.
303 this is only valid for the duration of this call */
304 drop_target->SetDragContext( context );
305
306 /* inform the wxDropTarget about the current drag widget.
307 this is only valid for the duration of this call */
308 drop_target->SetDragWidget( widget );
309
310 /* inform the wxDropTarget about the current drag time.
311 this is only valid for the duration of this call */
312 drop_target->SetDragTime( time );
313
314 /*
315 wxDragResult result = wxDragMove;
316 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
317 */
318
319 /* reset the block here as someone might very well
320 show a dialog as a reaction to a drop and this
321 wouldn't work without events */
322 g_blockEventsOnDrag = false;
323
324 bool ret = drop_target->OnDrop( x, y );
325
326 if (!ret)
327 {
328 #ifdef __WXDEBUG__
329 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
330 #endif
331
332 /* cancel the whole thing */
333 gtk_drag_finish( context,
334 FALSE, /* no success */
335 FALSE, /* don't delete data on dropping side */
336 time );
337 }
338 else
339 {
340 #ifdef __WXDEBUG__
341 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") );
342 #endif
343
344 #if wxUSE_THREADS
345 /* disable GUI threads */
346 #endif
347
348 GdkAtom format = drop_target->GetMatchingPair();
349
350 // this does happen somehow, see bug 555111
351 wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") );
352
353 /*
354 GdkDragAction action = GDK_ACTION_MOVE;
355 if (result == wxDragCopy) action == GDK_ACTION_COPY;
356 context->action = action;
357 */
358 /* this should trigger an "drag_data_received" event */
359 gtk_drag_get_data( widget,
360 context,
361 format,
362 time );
363
364 #if wxUSE_THREADS
365 /* re-enable GUI threads */
366 #endif
367 }
368
369 /* after this, invalidate the drop_target's GdkDragContext */
370 drop_target->SetDragContext( (GdkDragContext*) NULL );
371
372 /* after this, invalidate the drop_target's drag widget */
373 drop_target->SetDragWidget( (GtkWidget*) NULL );
374
375 /* this has to be done because GDK has no "drag_enter" event */
376 drop_target->m_firstMotion = true;
377
378 return ret;
379 }
380 }
381
382 // ----------------------------------------------------------------------------
383 // "drag_data_received"
384 // ----------------------------------------------------------------------------
385
386 extern "C" {
387 static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
388 GdkDragContext *context,
389 gint x,
390 gint y,
391 GtkSelectionData *data,
392 guint WXUNUSED(info),
393 guint time,
394 wxDropTarget *drop_target )
395 {
396 /* Owen Taylor: "call gtk_drag_finish() with
397 success == TRUE" */
398
399 if ((data->length <= 0) || (data->format != 8))
400 {
401 /* negative data length and non 8-bit data format
402 qualifies for junk */
403 gtk_drag_finish (context, FALSE, FALSE, time);
404
405 return;
406 }
407
408 #ifdef __WXDEBUG__
409 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
410 #endif
411
412 /* inform the wxDropTarget about the current GtkSelectionData.
413 this is only valid for the duration of this call */
414 drop_target->SetDragData( data );
415
416 wxDragResult result = ConvertFromGTK(context->action);
417
418 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
419 {
420 #ifdef __WXDEBUG__
421 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") );
422 #endif
423
424 /* tell GTK that data transfer was successful */
425 gtk_drag_finish( context, TRUE, FALSE, time );
426 }
427 else
428 {
429 #ifdef __WXDEBUG__
430 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
431 #endif
432
433 /* tell GTK that data transfer was not successful */
434 gtk_drag_finish( context, FALSE, FALSE, time );
435 }
436
437 /* after this, invalidate the drop_target's drag data */
438 drop_target->SetDragData( (GtkSelectionData*) NULL );
439 }
440 }
441
442 //----------------------------------------------------------------------------
443 // wxDropTarget
444 //----------------------------------------------------------------------------
445
446 wxDropTarget::wxDropTarget( wxDataObject *data )
447 : wxDropTargetBase( data )
448 {
449 m_firstMotion = true;
450 m_dragContext = (GdkDragContext*) NULL;
451 m_dragWidget = (GtkWidget*) NULL;
452 m_dragData = (GtkSelectionData*) NULL;
453 m_dragTime = 0;
454 }
455
456 wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
457 wxCoord WXUNUSED(y),
458 wxDragResult def )
459 {
460 // GetMatchingPair() checks for m_dataObject too, no need to do it here
461
462 // disable the debug message from GetMatchingPair() - there are too many
463 // of them otherwise
464 #ifdef __WXDEBUG__
465 wxLogNull noLog;
466 #endif // Debug
467
468 return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
469 }
470
471 bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
472 {
473 if (!m_dataObject)
474 return false;
475
476 return (GetMatchingPair() != (GdkAtom) 0);
477 }
478
479 wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
480 wxDragResult def )
481 {
482 if (!m_dataObject)
483 return wxDragNone;
484
485 if (GetMatchingPair() == (GdkAtom) 0)
486 return wxDragNone;
487
488 return GetData() ? def : wxDragNone;
489 }
490
491 GdkAtom wxDropTarget::GetMatchingPair()
492 {
493 if (!m_dataObject)
494 return (GdkAtom) 0;
495
496 if (!m_dragContext)
497 return (GdkAtom) 0;
498
499 GList *child = m_dragContext->targets;
500 while (child)
501 {
502 GdkAtom formatAtom = (GdkAtom)(child->data);
503 wxDataFormat format( formatAtom );
504
505 #ifdef __WXDEBUG__
506 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
507 format.GetId().c_str());
508 #endif // Debug
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::UnregisterWidget( 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::RegisterWidget( 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 (GtkTargetEntry*) 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 #ifdef __WXDEBUG__
602 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
603 format.GetId().c_str());
604 #endif
605
606 drop_source->m_retValue = wxDragCancel;
607
608 wxDataObject *data = drop_source->GetDataObject();
609
610 if (!data)
611 {
612 #ifdef __WXDEBUG__
613 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
614 #endif
615 return;
616 }
617
618 if (!data->IsSupportedFormat(format))
619 {
620 #ifdef __WXDEBUG__
621 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
622 #endif
623 return;
624 }
625
626 if (data->GetDataSize(format) == 0)
627 {
628 #ifdef __WXDEBUG__
629 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
630 #endif
631 return;
632 }
633
634 size_t size = data->GetDataSize(format);
635
636 // printf( "data size: %d.\n", (int)data_size );
637
638 guchar *d = new guchar[size];
639
640 if (!data->GetDataHere( format, (void*)d ))
641 {
642 delete[] d;
643 return;
644 }
645
646 #if wxUSE_THREADS
647 /* disable GUI threads */
648 #endif
649
650 gtk_selection_data_set( selection_data,
651 selection_data->target,
652 8, // 8-bit
653 d,
654 size );
655
656 #if wxUSE_THREADS
657 /* enable GUI threads */
658 #endif
659
660 delete[] d;
661 }
662 }
663
664 //----------------------------------------------------------------------------
665 // "drag_end"
666 //----------------------------------------------------------------------------
667
668 extern "C" {
669 static void source_drag_end( GtkWidget *WXUNUSED(widget),
670 GdkDragContext *WXUNUSED(context),
671 wxDropSource *drop_source )
672 {
673 // printf( "Drag source: drag_end.\n" );
674
675 drop_source->m_waiting = false;
676 }
677 }
678
679 //-----------------------------------------------------------------------------
680 // "configure_event" from m_iconWindow
681 //-----------------------------------------------------------------------------
682
683 extern "C" {
684 static gint
685 gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
686 {
687 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
688
689 return 0;
690 }
691 }
692
693 //---------------------------------------------------------------------------
694 // wxDropSource
695 //---------------------------------------------------------------------------
696
697 wxDropSource::wxDropSource(wxWindow *win,
698 const wxIcon &iconCopy,
699 const wxIcon &iconMove,
700 const wxIcon &iconNone)
701 {
702 m_waiting = true;
703
704 m_iconWindow = (GtkWidget*) NULL;
705
706 m_window = win;
707 m_widget = win->m_widget;
708 if (win->m_wxwindow) m_widget = win->m_wxwindow;
709
710 m_retValue = wxDragCancel;
711
712 SetIcons(iconCopy, iconMove, iconNone);
713 }
714
715 wxDropSource::wxDropSource(wxDataObject& data,
716 wxWindow *win,
717 const wxIcon &iconCopy,
718 const wxIcon &iconMove,
719 const wxIcon &iconNone)
720 {
721 m_waiting = true;
722
723 SetData( data );
724
725 m_iconWindow = (GtkWidget*) NULL;
726
727 m_window = win;
728 m_widget = win->m_widget;
729 if (win->m_wxwindow) m_widget = win->m_wxwindow;
730
731 m_retValue = wxDragCancel;
732
733 SetIcons(iconCopy, iconMove, iconNone);
734 }
735
736 void wxDropSource::SetIcons(const wxIcon &iconCopy,
737 const wxIcon &iconMove,
738 const wxIcon &iconNone)
739 {
740 m_iconCopy = iconCopy;
741 m_iconMove = iconMove;
742 m_iconNone = iconNone;
743
744 if ( !m_iconCopy.Ok() )
745 m_iconCopy = wxIcon(page_xpm);
746 if ( !m_iconMove.Ok() )
747 m_iconMove = m_iconCopy;
748 if ( !m_iconNone.Ok() )
749 m_iconNone = m_iconCopy;
750 }
751
752 wxDropSource::~wxDropSource()
753 {
754 }
755
756 void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
757 {
758 // get the right icon to display
759 wxIcon *icon = NULL;
760 if ( action & GDK_ACTION_MOVE )
761 icon = &m_iconMove;
762 else if ( action & GDK_ACTION_COPY )
763 icon = &m_iconCopy;
764 else
765 icon = &m_iconNone;
766
767 GdkBitmap *mask;
768 if ( icon->GetMask() )
769 mask = icon->GetMask()->GetBitmap();
770 else
771 mask = (GdkBitmap *)NULL;
772
773 GdkPixmap *pixmap = icon->GetPixmap();
774
775 gint width,height;
776 gdk_drawable_get_size (pixmap, &width, &height);
777
778 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
779 gtk_widget_push_colormap (colormap);
780
781 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
782 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
783 gtk_widget_set_app_paintable (m_iconWindow, TRUE);
784
785 gtk_widget_pop_colormap ();
786
787 gtk_widget_set_size_request (m_iconWindow, width, height);
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 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
794
795 if (mask)
796 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
797
798 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
799 }
800
801 wxDragResult wxDropSource::DoDragDrop(int flags)
802 {
803 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
804 wxT("Drop source: no data") );
805
806 // still in drag
807 if (g_blockEventsOnDrag)
808 return wxDragNone;
809
810 // don't start dragging if no button is down
811 if (g_lastButtonNumber == 0)
812 return wxDragNone;
813
814 // we can only start a drag after a mouse event
815 if (g_lastMouseEvent == NULL)
816 return wxDragNone;
817
818 GTKConnectDragSignals();
819 wxON_BLOCK_EXIT_OBJ0(*this, wxDropSource::GTKDisconnectDragSignals);
820
821 m_waiting = true;
822
823 GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
824
825 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
826 m_data->GetAllFormats( array );
827 size_t count = m_data->GetFormatCount();
828 for (size_t i = 0; i < count; i++)
829 {
830 GdkAtom atom = array[i];
831 #ifdef __WXDEBUG__
832 wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ));
833 #endif
834 gtk_target_list_add( target_list, atom, 0, 0 );
835 }
836 delete[] array;
837
838 int action = GDK_ACTION_COPY;
839 if ( flags & wxDrag_AllowMove )
840 action |= GDK_ACTION_MOVE;
841
842 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
843 // to use a global to pass the flags to the drop target but I'd
844 // surely prefer a better way to do it
845 gs_flagsForDrag = flags;
846
847 GdkDragContext *context = gtk_drag_begin( m_widget,
848 target_list,
849 (GdkDragAction)action,
850 g_lastButtonNumber, // number of mouse button which started drag
851 (GdkEvent*) g_lastMouseEvent );
852
853 if ( !context )
854 {
855 // this can happen e.g. if gdk_pointer_grab() failed
856 return wxDragError;
857 }
858
859 m_dragContext = context;
860
861 PrepareIcon( action, context );
862
863 while (m_waiting)
864 gtk_main_iteration();
865
866 m_retValue = ConvertFromGTK(context->action);
867 if ( m_retValue == wxDragNone )
868 m_retValue = wxDragCancel;
869
870 return m_retValue;
871 }
872
873 void wxDropSource::GTKConnectDragSignals()
874 {
875 if (!m_widget)
876 return;
877
878 g_blockEventsOnDrag = true;
879
880 g_signal_connect (m_widget, "drag_data_get",
881 G_CALLBACK (source_drag_data_get), this);
882 g_signal_connect (m_widget, "drag_end",
883 G_CALLBACK (source_drag_end), this);
884
885 }
886
887 void wxDropSource::GTKDisconnectDragSignals()
888 {
889 if (!m_widget)
890 return;
891
892 g_blockEventsOnDrag = false;
893
894 g_signal_handlers_disconnect_by_func (m_widget,
895 (gpointer) source_drag_data_get,
896 this);
897 g_signal_handlers_disconnect_by_func (m_widget,
898 (gpointer) source_drag_end,
899 this);
900 }
901
902 #endif
903 // wxUSE_DRAG_AND_DROP