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