Reorganize idle system code.
[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 <gtk/gtk.h>
27
28 //----------------------------------------------------------------------------
29 // global data
30 //----------------------------------------------------------------------------
31
32 extern bool g_blockEventsOnDrag;
33
34 // the flags used for the last DoDragDrop()
35 static long gs_flagsForDrag = 0;
36
37 #ifdef __WXDEBUG__
38 // the trace mask we use with wxLogTrace() - call
39 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
40 // (there are quite a few of them, so don't enable this by default)
41 static const wxChar *TRACE_DND = _T("dnd");
42 #endif
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->SetDragContext( 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->SetDragContext( (GdkDragContext*) 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 /* inform the wxDropTarget about the current GdkDragContext.
198 this is only valid for the duration of this call */
199 drop_target->SetDragContext( context );
200
201 // GTK+ always supposes that we want to copy the data by default while we
202 // might want to move it, so examine not only suggested_action - which is
203 // only good if we don't have our own preferences - but also the actions
204 // field
205 wxDragResult result;
206 if (drop_target->GetDefaultAction() == wxDragNone)
207 {
208 // use default action set by wxDropSource::DoDragDrop()
209 if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove &&
210 (context->actions & GDK_ACTION_MOVE ) )
211 {
212 // move is requested by the program and allowed by GTK+ - do it, even
213 // though suggested_action may be currently wxDragCopy
214 result = wxDragMove;
215 }
216 else // use whatever GTK+ says we should
217 {
218 result = ConvertFromGTK(context->suggested_action);
219
220 if ( (result == wxDragMove) && !(gs_flagsForDrag & wxDrag_AllowMove) )
221 {
222 // we're requested to move but we can't
223 result = wxDragCopy;
224 }
225 }
226 }
227 else if (drop_target->GetDefaultAction() == wxDragMove &&
228 (context->actions & GDK_ACTION_MOVE))
229 {
230 result = wxDragMove;
231 }
232 else
233 {
234 if (context->actions & GDK_ACTION_COPY)
235 result = wxDragCopy;
236 else if (context->actions & GDK_ACTION_MOVE)
237 result = wxDragMove;
238 else
239 result = wxDragNone;
240 }
241
242 if (drop_target->m_firstMotion)
243 {
244 /* the first "drag_motion" event substitutes a "drag_enter" event */
245 result = drop_target->OnEnter( x, y, result );
246 }
247 else
248 {
249 /* give program a chance to react (i.e. to say no by returning FALSE) */
250 result = drop_target->OnDragOver( x, y, result );
251 }
252
253 bool ret = wxIsDragResultOk( result );
254 if (ret)
255 {
256 GdkDragAction action;
257 if (result == wxDragCopy)
258 action = GDK_ACTION_COPY;
259 else if (result == wxDragLink)
260 action = GDK_ACTION_LINK;
261 else
262 action = GDK_ACTION_MOVE;
263
264 gdk_drag_status( context, action, time );
265 }
266
267 /* after this, invalidate the drop_target's GdkDragContext */
268 drop_target->SetDragContext( (GdkDragContext*) NULL );
269
270 /* this has to be done because GDK has no "drag_enter" event */
271 drop_target->m_firstMotion = false;
272
273 return ret;
274 }
275 }
276
277 // ----------------------------------------------------------------------------
278 // "drag_drop"
279 // ----------------------------------------------------------------------------
280
281 extern "C" {
282 static gboolean target_drag_drop( GtkWidget *widget,
283 GdkDragContext *context,
284 gint x,
285 gint y,
286 guint time,
287 wxDropTarget *drop_target )
288 {
289 /* Owen Taylor: "if the drop is not in a drop zone,
290 return FALSE, otherwise, if you aren't accepting
291 the drop, call gtk_drag_finish() with success == FALSE
292 otherwise call gtk_drag_data_get()" */
293
294 // printf( "drop.\n" );
295
296 /* this seems to make a difference between not accepting
297 due to wrong target area and due to wrong format. let
298 us hope that this is not required.. */
299
300 /* inform the wxDropTarget about the current GdkDragContext.
301 this is only valid for the duration of this call */
302 drop_target->SetDragContext( context );
303
304 /* inform the wxDropTarget about the current drag widget.
305 this is only valid for the duration of this call */
306 drop_target->SetDragWidget( widget );
307
308 /* inform the wxDropTarget about the current drag time.
309 this is only valid for the duration of this call */
310 drop_target->SetDragTime( time );
311
312 /*
313 wxDragResult result = wxDragMove;
314 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
315 */
316
317 /* reset the block here as someone might very well
318 show a dialog as a reaction to a drop and this
319 wouldn't work without events */
320 g_blockEventsOnDrag = false;
321
322 bool ret = drop_target->OnDrop( x, y );
323
324 if (!ret)
325 {
326 #ifdef __WXDEBUG__
327 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
328 #endif
329
330 /* cancel the whole thing */
331 gtk_drag_finish( context,
332 FALSE, /* no success */
333 FALSE, /* don't delete data on dropping side */
334 time );
335 }
336 else
337 {
338 #ifdef __WXDEBUG__
339 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") );
340 #endif
341
342 #if wxUSE_THREADS
343 /* disable GUI threads */
344 #endif
345
346 GdkAtom format = drop_target->GetMatchingPair();
347
348 // this does happen somehow, see bug 555111
349 wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") );
350
351 /*
352 GdkDragAction action = GDK_ACTION_MOVE;
353 if (result == wxDragCopy) action == GDK_ACTION_COPY;
354 context->action = action;
355 */
356 /* this should trigger an "drag_data_received" event */
357 gtk_drag_get_data( widget,
358 context,
359 format,
360 time );
361
362 #if wxUSE_THREADS
363 /* re-enable GUI threads */
364 #endif
365 }
366
367 /* after this, invalidate the drop_target's GdkDragContext */
368 drop_target->SetDragContext( (GdkDragContext*) NULL );
369
370 /* after this, invalidate the drop_target's drag widget */
371 drop_target->SetDragWidget( (GtkWidget*) NULL );
372
373 /* this has to be done because GDK has no "drag_enter" event */
374 drop_target->m_firstMotion = true;
375
376 return ret;
377 }
378 }
379
380 // ----------------------------------------------------------------------------
381 // "drag_data_received"
382 // ----------------------------------------------------------------------------
383
384 extern "C" {
385 static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
386 GdkDragContext *context,
387 gint x,
388 gint y,
389 GtkSelectionData *data,
390 guint WXUNUSED(info),
391 guint time,
392 wxDropTarget *drop_target )
393 {
394 /* Owen Taylor: "call gtk_drag_finish() with
395 success == TRUE" */
396
397 if ((data->length <= 0) || (data->format != 8))
398 {
399 /* negative data length and non 8-bit data format
400 qualifies for junk */
401 gtk_drag_finish (context, FALSE, FALSE, time);
402
403 return;
404 }
405
406 #ifdef __WXDEBUG__
407 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
408 #endif
409
410 /* inform the wxDropTarget about the current GtkSelectionData.
411 this is only valid for the duration of this call */
412 drop_target->SetDragData( data );
413
414 wxDragResult result = ConvertFromGTK(context->action);
415
416 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
417 {
418 #ifdef __WXDEBUG__
419 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") );
420 #endif
421
422 /* tell GTK that data transfer was successful */
423 gtk_drag_finish( context, TRUE, FALSE, time );
424 }
425 else
426 {
427 #ifdef __WXDEBUG__
428 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
429 #endif
430
431 /* tell GTK that data transfer was not successful */
432 gtk_drag_finish( context, FALSE, FALSE, time );
433 }
434
435 /* after this, invalidate the drop_target's drag data */
436 drop_target->SetDragData( (GtkSelectionData*) NULL );
437 }
438 }
439
440 //----------------------------------------------------------------------------
441 // wxDropTarget
442 //----------------------------------------------------------------------------
443
444 wxDropTarget::wxDropTarget( wxDataObject *data )
445 : wxDropTargetBase( data )
446 {
447 m_firstMotion = true;
448 m_dragContext = (GdkDragContext*) NULL;
449 m_dragWidget = (GtkWidget*) NULL;
450 m_dragData = (GtkSelectionData*) NULL;
451 m_dragTime = 0;
452 }
453
454 wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
455 wxCoord WXUNUSED(y),
456 wxDragResult def )
457 {
458 // GetMatchingPair() checks for m_dataObject too, no need to do it here
459
460 // disable the debug message from GetMatchingPair() - there are too many
461 // of them otherwise
462 #ifdef __WXDEBUG__
463 wxLogNull noLog;
464 #endif // Debug
465
466 return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
467 }
468
469 bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
470 {
471 if (!m_dataObject)
472 return false;
473
474 return (GetMatchingPair() != (GdkAtom) 0);
475 }
476
477 wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
478 wxDragResult def )
479 {
480 if (!m_dataObject)
481 return wxDragNone;
482
483 if (GetMatchingPair() == (GdkAtom) 0)
484 return wxDragNone;
485
486 return GetData() ? def : wxDragNone;
487 }
488
489 GdkAtom wxDropTarget::GetMatchingPair()
490 {
491 if (!m_dataObject)
492 return (GdkAtom) 0;
493
494 if (!m_dragContext)
495 return (GdkAtom) 0;
496
497 GList *child = m_dragContext->targets;
498 while (child)
499 {
500 GdkAtom formatAtom = (GdkAtom)(child->data);
501 wxDataFormat format( formatAtom );
502
503 #ifdef __WXDEBUG__
504 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
505 format.GetId().c_str());
506 #endif // Debug
507
508 if (m_dataObject->IsSupportedFormat( format ))
509 return formatAtom;
510
511 child = child->next;
512 }
513
514 return (GdkAtom) 0;
515 }
516
517 bool wxDropTarget::GetData()
518 {
519 if (!m_dragData)
520 return false;
521
522 if (!m_dataObject)
523 return false;
524
525 wxDataFormat dragFormat( m_dragData->target );
526
527 if (!m_dataObject->IsSupportedFormat( dragFormat ))
528 return false;
529
530 m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
531
532 return true;
533 }
534
535 void wxDropTarget::UnregisterWidget( GtkWidget *widget )
536 {
537 wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
538
539 gtk_drag_dest_unset( widget );
540
541 g_signal_handlers_disconnect_by_func (widget,
542 (gpointer) target_drag_leave, this);
543 g_signal_handlers_disconnect_by_func (widget,
544 (gpointer) target_drag_motion, this);
545 g_signal_handlers_disconnect_by_func (widget,
546 (gpointer) target_drag_drop, this);
547 g_signal_handlers_disconnect_by_func (widget,
548 (gpointer) target_drag_data_received, this);
549 }
550
551 void wxDropTarget::RegisterWidget( GtkWidget *widget )
552 {
553 wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
554
555 /* gtk_drag_dest_set() determines what default behaviour we'd like
556 GTK to supply. we don't want to specify out targets (=formats)
557 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
558 not GTK_DEST_DEFAULT_DROP). instead we react individually to
559 "drag_motion" and "drag_drop" events. this makes it possible
560 to allow dropping on only a small area. we should set
561 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
562 highlighting if dragging over standard controls, but this
563 seems to be broken without the other two. */
564
565 gtk_drag_dest_set( widget,
566 (GtkDestDefaults) 0, /* no default behaviour */
567 (GtkTargetEntry*) NULL, /* we don't supply any formats here */
568 0, /* number of targets = 0 */
569 (GdkDragAction) 0 ); /* we don't supply any actions here */
570
571 g_signal_connect (widget, "drag_leave",
572 G_CALLBACK (target_drag_leave), this);
573
574 g_signal_connect (widget, "drag_motion",
575 G_CALLBACK (target_drag_motion), this);
576
577 g_signal_connect (widget, "drag_drop",
578 G_CALLBACK (target_drag_drop), this);
579
580 g_signal_connect (widget, "drag_data_received",
581 G_CALLBACK (target_drag_data_received), this);
582 }
583
584 //----------------------------------------------------------------------------
585 // "drag_data_get"
586 //----------------------------------------------------------------------------
587
588 extern "C" {
589 static void
590 source_drag_data_get (GtkWidget *WXUNUSED(widget),
591 GdkDragContext *WXUNUSED(context),
592 GtkSelectionData *selection_data,
593 guint WXUNUSED(info),
594 guint WXUNUSED(time),
595 wxDropSource *drop_source )
596 {
597 wxDataFormat format( selection_data->target );
598
599 #ifdef __WXDEBUG__
600 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
601 format.GetId().c_str());
602 #endif
603
604 drop_source->m_retValue = wxDragCancel;
605
606 wxDataObject *data = drop_source->GetDataObject();
607
608 if (!data)
609 {
610 #ifdef __WXDEBUG__
611 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
612 #endif
613 return;
614 }
615
616 if (!data->IsSupportedFormat(format))
617 {
618 #ifdef __WXDEBUG__
619 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
620 #endif
621 return;
622 }
623
624 if (data->GetDataSize(format) == 0)
625 {
626 #ifdef __WXDEBUG__
627 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
628 #endif
629 return;
630 }
631
632 size_t size = data->GetDataSize(format);
633
634 // printf( "data size: %d.\n", (int)data_size );
635
636 guchar *d = new guchar[size];
637
638 if (!data->GetDataHere( format, (void*)d ))
639 {
640 delete[] d;
641 return;
642 }
643
644 #if wxUSE_THREADS
645 /* disable GUI threads */
646 #endif
647
648 gtk_selection_data_set( selection_data,
649 selection_data->target,
650 8, // 8-bit
651 d,
652 size );
653
654 #if wxUSE_THREADS
655 /* enable GUI threads */
656 #endif
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 // printf( "Drag source: drag_end.\n" );
672
673 drop_source->m_waiting = false;
674 }
675 }
676
677 //-----------------------------------------------------------------------------
678 // "configure_event" from m_iconWindow
679 //-----------------------------------------------------------------------------
680
681 extern "C" {
682 static gint
683 gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
684 {
685 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
686
687 return 0;
688 }
689 }
690
691 //---------------------------------------------------------------------------
692 // wxDropSource
693 //---------------------------------------------------------------------------
694
695 wxDropSource::wxDropSource(wxWindow *win,
696 const wxIcon &iconCopy,
697 const wxIcon &iconMove,
698 const wxIcon &iconNone)
699 {
700 m_waiting = true;
701
702 m_iconWindow = (GtkWidget*) NULL;
703
704 m_window = win;
705 m_widget = win->m_widget;
706 if (win->m_wxwindow) m_widget = win->m_wxwindow;
707
708 m_retValue = wxDragCancel;
709
710 SetIcons(iconCopy, iconMove, iconNone);
711 }
712
713 wxDropSource::wxDropSource(wxDataObject& data,
714 wxWindow *win,
715 const wxIcon &iconCopy,
716 const wxIcon &iconMove,
717 const wxIcon &iconNone)
718 {
719 m_waiting = true;
720
721 SetData( data );
722
723 m_iconWindow = (GtkWidget*) NULL;
724
725 m_window = win;
726 m_widget = win->m_widget;
727 if (win->m_wxwindow) m_widget = win->m_wxwindow;
728
729 m_retValue = wxDragCancel;
730
731 SetIcons(iconCopy, iconMove, iconNone);
732 }
733
734 void wxDropSource::SetIcons(const wxIcon &iconCopy,
735 const wxIcon &iconMove,
736 const wxIcon &iconNone)
737 {
738 m_iconCopy = iconCopy;
739 m_iconMove = iconMove;
740 m_iconNone = iconNone;
741
742 if ( !m_iconCopy.Ok() )
743 m_iconCopy = wxIcon(page_xpm);
744 if ( !m_iconMove.Ok() )
745 m_iconMove = m_iconCopy;
746 if ( !m_iconNone.Ok() )
747 m_iconNone = m_iconCopy;
748 }
749
750 wxDropSource::~wxDropSource()
751 {
752 }
753
754 void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
755 {
756 // get the right icon to display
757 wxIcon *icon = NULL;
758 if ( action & GDK_ACTION_MOVE )
759 icon = &m_iconMove;
760 else if ( action & GDK_ACTION_COPY )
761 icon = &m_iconCopy;
762 else
763 icon = &m_iconNone;
764
765 GdkBitmap *mask;
766 if ( icon->GetMask() )
767 mask = icon->GetMask()->GetBitmap();
768 else
769 mask = (GdkBitmap *)NULL;
770
771 GdkPixmap *pixmap = icon->GetPixmap();
772
773 gint width,height;
774 gdk_drawable_get_size (pixmap, &width, &height);
775
776 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
777 gtk_widget_push_colormap (colormap);
778
779 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
780 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
781 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE);
782
783 gtk_widget_pop_colormap ();
784
785 gtk_widget_set_size_request (m_iconWindow, width, height);
786 gtk_widget_realize (m_iconWindow);
787
788 g_signal_connect (m_iconWindow, "configure_event",
789 G_CALLBACK (gtk_dnd_window_configure_callback), this);
790
791 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
792
793 if (mask)
794 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
795
796 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
797 }
798
799 wxDragResult wxDropSource::DoDragDrop(int flags)
800 {
801 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
802 wxT("Drop source: no data") );
803
804 // still in drag
805 if (g_blockEventsOnDrag)
806 return wxDragNone;
807
808 // don't start dragging if no button is down
809 if (g_lastButtonNumber == 0)
810 return wxDragNone;
811
812 // we can only start a drag after a mouse event
813 if (g_lastMouseEvent == NULL)
814 return wxDragNone;
815
816 // disabled for now
817 g_blockEventsOnDrag = true;
818
819 RegisterWindow();
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 m_dragContext = context;
854
855 PrepareIcon( action, context );
856
857 while (m_waiting)
858 gtk_main_iteration();
859
860 m_retValue = ConvertFromGTK(context->action);
861 if ( m_retValue == wxDragNone )
862 m_retValue = wxDragCancel;
863
864 g_blockEventsOnDrag = false;
865
866 UnregisterWindow();
867
868 return m_retValue;
869 }
870
871 void wxDropSource::RegisterWindow()
872 {
873 if (!m_widget) return;
874
875 g_signal_connect (m_widget, "drag_data_get",
876 G_CALLBACK (source_drag_data_get), this);
877 g_signal_connect (m_widget, "drag_end",
878 G_CALLBACK (source_drag_end), this);
879
880 }
881
882 void wxDropSource::UnregisterWindow()
883 {
884 if (!m_widget)
885 return;
886
887 g_signal_handlers_disconnect_by_func (m_widget,
888 (gpointer) source_drag_data_get,
889 this);
890 g_signal_handlers_disconnect_by_func (m_widget,
891 (gpointer) source_drag_end,
892 this);
893 }
894
895 #endif
896 // wxUSE_DRAG_AND_DROP