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