Fix X server hang in DND.
[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/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
37 extern bool g_blockEventsOnDrag;
38
39 // the flags used for the last DoDragDrop()
40 static long gs_flagsForDrag = 0;
41
42 // the trace mask we use with wxLogTrace() - call
43 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
44 // (there are quite a few of them, so don't enable this by default)
45 static const wxChar *TRACE_DND = _T("dnd");
46
47 extern GdkEvent *g_lastMouseEvent;
48
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 if (g_isIdle) wxapp_install_idle_handler();
168
169 /* inform the wxDropTarget about the current GdkDragContext.
170 this is only valid for the duration of this call */
171 drop_target->SetDragContext( context );
172
173 /* we don't need return values. this event is just for
174 information */
175 drop_target->OnLeave();
176
177 /* this has to be done because GDK has no "drag_enter" event */
178 drop_target->m_firstMotion = true;
179
180 /* after this, invalidate the drop_target's GdkDragContext */
181 drop_target->SetDragContext( (GdkDragContext*) NULL );
182 }
183 }
184
185 // ----------------------------------------------------------------------------
186 // "drag_motion"
187 // ----------------------------------------------------------------------------
188
189 extern "C" {
190 static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget),
191 GdkDragContext *context,
192 gint x,
193 gint y,
194 guint time,
195 wxDropTarget *drop_target )
196 {
197 if (g_isIdle) wxapp_install_idle_handler();
198
199 /* Owen Taylor: "if the coordinates not in a drop zone,
200 return FALSE, otherwise call gtk_drag_status() and
201 return TRUE" */
202
203 /* inform the wxDropTarget about the current GdkDragContext.
204 this is only valid for the duration of this call */
205 drop_target->SetDragContext( context );
206
207 // GTK+ always supposes that we want to copy the data by default while we
208 // might want to move it, so examine not only suggested_action - which is
209 // only good if we don't have our own preferences - but also the actions
210 // field
211 wxDragResult result;
212 if (drop_target->GetDefaultAction() == wxDragNone)
213 {
214 // use default action set by wxDropSource::DoDragDrop()
215 if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove &&
216 (context->actions & GDK_ACTION_MOVE ) )
217 {
218 // move is requested by the program and allowed by GTK+ - do it, even
219 // though suggested_action may be currently wxDragCopy
220 result = wxDragMove;
221 }
222 else // use whatever GTK+ says we should
223 {
224 result = ConvertFromGTK(context->suggested_action);
225
226 if ( (result == wxDragMove) && !(gs_flagsForDrag & wxDrag_AllowMove) )
227 {
228 // we're requested to move but we can't
229 result = wxDragCopy;
230 }
231 }
232 }
233 else if (drop_target->GetDefaultAction() == wxDragMove &&
234 (context->actions & GDK_ACTION_MOVE))
235 {
236 result = wxDragMove;
237 }
238 else
239 {
240 if (context->actions & GDK_ACTION_COPY)
241 result = wxDragCopy;
242 else if (context->actions & GDK_ACTION_MOVE)
243 result = wxDragMove;
244 else
245 result = wxDragNone;
246 }
247
248 if (drop_target->m_firstMotion)
249 {
250 /* the first "drag_motion" event substitutes a "drag_enter" event */
251 result = drop_target->OnEnter( x, y, result );
252 }
253 else
254 {
255 /* give program a chance to react (i.e. to say no by returning FALSE) */
256 result = drop_target->OnDragOver( x, y, result );
257 }
258
259 bool ret = wxIsDragResultOk( result );
260 if (ret)
261 {
262 GdkDragAction action;
263 if (result == wxDragCopy)
264 action = GDK_ACTION_COPY;
265 else if (result == wxDragLink)
266 action = GDK_ACTION_LINK;
267 else
268 action = GDK_ACTION_MOVE;
269
270 gdk_drag_status( context, action, time );
271 }
272
273 /* after this, invalidate the drop_target's GdkDragContext */
274 drop_target->SetDragContext( (GdkDragContext*) NULL );
275
276 /* this has to be done because GDK has no "drag_enter" event */
277 drop_target->m_firstMotion = false;
278
279 return ret;
280 }
281 }
282
283 // ----------------------------------------------------------------------------
284 // "drag_drop"
285 // ----------------------------------------------------------------------------
286
287 extern "C" {
288 static gboolean target_drag_drop( GtkWidget *widget,
289 GdkDragContext *context,
290 gint x,
291 gint y,
292 guint time,
293 wxDropTarget *drop_target )
294 {
295 if (g_isIdle) wxapp_install_idle_handler();
296
297 /* Owen Taylor: "if the drop is not in a drop zone,
298 return FALSE, otherwise, if you aren't accepting
299 the drop, call gtk_drag_finish() with success == FALSE
300 otherwise call gtk_drag_data_get()" */
301
302 // printf( "drop.\n" );
303
304 /* this seems to make a difference between not accepting
305 due to wrong target area and due to wrong format. let
306 us hope that this is not required.. */
307
308 /* inform the wxDropTarget about the current GdkDragContext.
309 this is only valid for the duration of this call */
310 drop_target->SetDragContext( context );
311
312 /* inform the wxDropTarget about the current drag widget.
313 this is only valid for the duration of this call */
314 drop_target->SetDragWidget( widget );
315
316 /* inform the wxDropTarget about the current drag time.
317 this is only valid for the duration of this call */
318 drop_target->SetDragTime( time );
319
320 /*
321 wxDragResult result = wxDragMove;
322 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
323 */
324
325 /* reset the block here as someone might very well
326 show a dialog as a reaction to a drop and this
327 wouldn't work without events */
328 g_blockEventsOnDrag = false;
329
330 bool ret = drop_target->OnDrop( x, y );
331
332 if (!ret)
333 {
334 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
335
336 /* cancel the whole thing */
337 gtk_drag_finish( context,
338 FALSE, /* no success */
339 FALSE, /* don't delete data on dropping side */
340 time );
341 }
342 else
343 {
344 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") );
345
346 #if wxUSE_THREADS
347 /* disable GUI threads */
348 #endif
349
350 GdkAtom format = drop_target->GetMatchingPair();
351
352 // this does happen somehow, see bug 555111
353 wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") );
354
355 /*
356 GdkDragAction action = GDK_ACTION_MOVE;
357 if (result == wxDragCopy) action == GDK_ACTION_COPY;
358 context->action = action;
359 */
360 /* this should trigger an "drag_data_received" event */
361 gtk_drag_get_data( widget,
362 context,
363 format,
364 time );
365
366 #if wxUSE_THREADS
367 /* re-enable GUI threads */
368 #endif
369 }
370
371 /* after this, invalidate the drop_target's GdkDragContext */
372 drop_target->SetDragContext( (GdkDragContext*) NULL );
373
374 /* after this, invalidate the drop_target's drag widget */
375 drop_target->SetDragWidget( (GtkWidget*) NULL );
376
377 /* this has to be done because GDK has no "drag_enter" event */
378 drop_target->m_firstMotion = true;
379
380 return ret;
381 }
382 }
383
384 // ----------------------------------------------------------------------------
385 // "drag_data_received"
386 // ----------------------------------------------------------------------------
387
388 extern "C" {
389 static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
390 GdkDragContext *context,
391 gint x,
392 gint y,
393 GtkSelectionData *data,
394 guint WXUNUSED(info),
395 guint time,
396 wxDropTarget *drop_target )
397 {
398 if (g_isIdle) wxapp_install_idle_handler();
399
400 /* Owen Taylor: "call gtk_drag_finish() with
401 success == TRUE" */
402
403 if ((data->length <= 0) || (data->format != 8))
404 {
405 /* negative data length and non 8-bit data format
406 qualifies for junk */
407 gtk_drag_finish (context, FALSE, FALSE, time);
408
409 return;
410 }
411
412 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
413
414 /* inform the wxDropTarget about the current GtkSelectionData.
415 this is only valid for the duration of this call */
416 drop_target->SetDragData( data );
417
418 wxDragResult result = ConvertFromGTK(context->action);
419
420 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
421 {
422 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") );
423
424 /* tell GTK that data transfer was successful */
425 gtk_drag_finish( context, TRUE, FALSE, time );
426 }
427 else
428 {
429 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
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 if (g_isIdle) wxapp_install_idle_handler();
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_data_delete"
658 //----------------------------------------------------------------------------
659
660 extern "C" {
661 static void source_drag_data_delete( GtkWidget *WXUNUSED(widget),
662 GdkDragContext *WXUNUSED(context),
663 wxDropSource *WXUNUSED(drop_source) )
664 {
665 if (g_isIdle)
666 wxapp_install_idle_handler();
667
668 // printf( "Drag source: drag_data_delete\n" );
669 }
670 }
671
672 //----------------------------------------------------------------------------
673 // "drag_begin"
674 //----------------------------------------------------------------------------
675
676 extern "C" {
677 static void source_drag_begin( GtkWidget *WXUNUSED(widget),
678 GdkDragContext *WXUNUSED(context),
679 wxDropSource *WXUNUSED(drop_source) )
680 {
681 if (g_isIdle)
682 wxapp_install_idle_handler();
683
684 // printf( "Drag source: drag_begin.\n" );
685 }
686 }
687
688 //----------------------------------------------------------------------------
689 // "drag_end"
690 //----------------------------------------------------------------------------
691
692 extern "C" {
693 static void source_drag_end( GtkWidget *WXUNUSED(widget),
694 GdkDragContext *WXUNUSED(context),
695 wxDropSource *drop_source )
696 {
697 if (g_isIdle) wxapp_install_idle_handler();
698
699 // printf( "Drag source: drag_end.\n" );
700
701 drop_source->m_waiting = false;
702 }
703 }
704
705 //-----------------------------------------------------------------------------
706 // "configure_event" from m_iconWindow
707 //-----------------------------------------------------------------------------
708
709 extern "C" {
710 static gint
711 gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
712 {
713 // don't need to install idle handler, its done from "event" signal
714
715 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
716
717 return 0;
718 }
719 }
720
721 //---------------------------------------------------------------------------
722 // wxDropSource
723 //---------------------------------------------------------------------------
724
725 wxDropSource::wxDropSource(wxWindow *win,
726 const wxIcon &iconCopy,
727 const wxIcon &iconMove,
728 const wxIcon &iconNone)
729 {
730 m_waiting = true;
731
732 m_iconWindow = (GtkWidget*) NULL;
733
734 m_window = win;
735 m_widget = win->m_widget;
736 if (win->m_wxwindow) m_widget = win->m_wxwindow;
737
738 m_retValue = wxDragCancel;
739
740 SetIcons(iconCopy, iconMove, iconNone);
741 }
742
743 wxDropSource::wxDropSource(wxDataObject& data,
744 wxWindow *win,
745 const wxIcon &iconCopy,
746 const wxIcon &iconMove,
747 const wxIcon &iconNone)
748 {
749 m_waiting = true;
750
751 SetData( data );
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
764 void wxDropSource::SetIcons(const wxIcon &iconCopy,
765 const wxIcon &iconMove,
766 const wxIcon &iconNone)
767 {
768 m_iconCopy = iconCopy;
769 m_iconMove = iconMove;
770 m_iconNone = iconNone;
771
772 if ( !m_iconCopy.Ok() )
773 m_iconCopy = wxIcon(page_xpm);
774 if ( !m_iconMove.Ok() )
775 m_iconMove = m_iconCopy;
776 if ( !m_iconNone.Ok() )
777 m_iconNone = m_iconCopy;
778 }
779
780 wxDropSource::~wxDropSource()
781 {
782 }
783
784 void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
785 {
786 // get the right icon to display
787 wxIcon *icon = NULL;
788 if ( action & GDK_ACTION_MOVE )
789 icon = &m_iconMove;
790 else if ( action & GDK_ACTION_COPY )
791 icon = &m_iconCopy;
792 else
793 icon = &m_iconNone;
794
795 GdkBitmap *mask;
796 if ( icon->GetMask() )
797 mask = icon->GetMask()->GetBitmap();
798 else
799 mask = (GdkBitmap *)NULL;
800
801 GdkPixmap *pixmap = icon->GetPixmap();
802
803 gint width,height;
804 gdk_drawable_get_size (pixmap, &width, &height);
805
806 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
807 gtk_widget_push_colormap (colormap);
808
809 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
810 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
811 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE);
812
813 gtk_widget_pop_colormap ();
814
815 gtk_widget_set_size_request (m_iconWindow, width, height);
816 gtk_widget_realize (m_iconWindow);
817
818 g_signal_connect (m_iconWindow, "configure_event",
819 G_CALLBACK (gtk_dnd_window_configure_callback), this);
820
821 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
822
823 if (mask)
824 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
825
826 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
827 }
828
829 wxDragResult wxDropSource::DoDragDrop(int flags)
830 {
831 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
832 wxT("Drop source: no data") );
833
834 // still in drag
835 if (g_blockEventsOnDrag)
836 return wxDragNone;
837
838 // disabled for now
839 g_blockEventsOnDrag = true;
840
841 RegisterWindow();
842
843 m_waiting = true;
844
845 GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
846
847 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
848 m_data->GetAllFormats( array );
849 size_t count = m_data->GetFormatCount();
850 for (size_t i = 0; i < count; i++)
851 {
852 GdkAtom atom = array[i];
853 wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ));
854 gtk_target_list_add( target_list, atom, 0, 0 );
855 }
856 delete[] array;
857
858 /* don't start dragging if no button is down */
859 if (g_lastButtonNumber)
860 {
861 int action = GDK_ACTION_COPY;
862 if ( flags & wxDrag_AllowMove )
863 action |= GDK_ACTION_MOVE;
864
865 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
866 // to use a global to pass the flags to the drop target but I'd
867 // surely prefer a better way to do it
868 gs_flagsForDrag = flags;
869
870 GdkDragContext *context = gtk_drag_begin( m_widget,
871 target_list,
872 (GdkDragAction)action,
873 g_lastButtonNumber, // number of mouse button which started drag
874 (GdkEvent*) g_lastMouseEvent );
875
876 m_dragContext = context;
877
878 PrepareIcon( action, context );
879
880 while (m_waiting)
881 gtk_main_iteration();
882
883 m_retValue = ConvertFromGTK(context->action);
884 if ( m_retValue == wxDragNone )
885 m_retValue = wxDragCancel;
886 }
887
888 g_blockEventsOnDrag = false;
889
890 UnregisterWindow();
891
892 return m_retValue;
893 }
894
895 void wxDropSource::RegisterWindow()
896 {
897 if (!m_widget) return;
898
899 g_signal_connect (m_widget, "drag_data_get",
900 G_CALLBACK (source_drag_data_get), this);
901 g_signal_connect (m_widget, "drag_data_delete",
902 G_CALLBACK (source_drag_data_delete), this);
903 g_signal_connect (m_widget, "drag_begin",
904 G_CALLBACK (source_drag_begin), this);
905 g_signal_connect (m_widget, "drag_end",
906 G_CALLBACK (source_drag_end), this);
907
908 }
909
910 void wxDropSource::UnregisterWindow()
911 {
912 if (!m_widget)
913 return;
914
915 g_signal_handlers_disconnect_by_func (m_widget,
916 (gpointer) source_drag_data_get,
917 this);
918 g_signal_handlers_disconnect_by_func (m_widget,
919 (gpointer) source_drag_data_delete,
920 this);
921 g_signal_handlers_disconnect_by_func (m_widget,
922 (gpointer) source_drag_begin,
923 this);
924 g_signal_handlers_disconnect_by_func (m_widget,
925 (gpointer) source_drag_end,
926 this);
927 }
928
929 #endif
930 // wxUSE_DRAG_AND_DROP