]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dnd.cpp
update from suzumizaki (patch 1522506)
[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 #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)
46 static 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
51 extern GdkEvent *g_lastMouseEvent;
52 extern int g_lastButtonNumber;
53
54 //----------------------------------------------------------------------------
55 // standard icons
56 //----------------------------------------------------------------------------
57
58 /* Copyright (c) Julian Smart */
59 static 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
143 static 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
164 extern "C" {
165 static 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
192 extern "C" {
193 static 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
290 extern "C" {
291 static 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 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
338
339 /* cancel the whole thing */
340 gtk_drag_finish( context,
341 FALSE, /* no success */
342 FALSE, /* don't delete data on dropping side */
343 time );
344 }
345 else
346 {
347 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") );
348
349 #if wxUSE_THREADS
350 /* disable GUI threads */
351 #endif
352
353 GdkAtom format = drop_target->GetMatchingPair();
354
355 // this does happen somehow, see bug 555111
356 wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") );
357
358 /*
359 GdkDragAction action = GDK_ACTION_MOVE;
360 if (result == wxDragCopy) action == GDK_ACTION_COPY;
361 context->action = action;
362 */
363 /* this should trigger an "drag_data_received" event */
364 gtk_drag_get_data( widget,
365 context,
366 format,
367 time );
368
369 #if wxUSE_THREADS
370 /* re-enable GUI threads */
371 #endif
372 }
373
374 /* after this, invalidate the drop_target's GdkDragContext */
375 drop_target->SetDragContext( (GdkDragContext*) NULL );
376
377 /* after this, invalidate the drop_target's drag widget */
378 drop_target->SetDragWidget( (GtkWidget*) NULL );
379
380 /* this has to be done because GDK has no "drag_enter" event */
381 drop_target->m_firstMotion = true;
382
383 return ret;
384 }
385 }
386
387 // ----------------------------------------------------------------------------
388 // "drag_data_received"
389 // ----------------------------------------------------------------------------
390
391 extern "C" {
392 static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
393 GdkDragContext *context,
394 gint x,
395 gint y,
396 GtkSelectionData *data,
397 guint WXUNUSED(info),
398 guint time,
399 wxDropTarget *drop_target )
400 {
401 if (g_isIdle) wxapp_install_idle_handler();
402
403 /* Owen Taylor: "call gtk_drag_finish() with
404 success == TRUE" */
405
406 if ((data->length <= 0) || (data->format != 8))
407 {
408 /* negative data length and non 8-bit data format
409 qualifies for junk */
410 gtk_drag_finish (context, FALSE, FALSE, time);
411
412 return;
413 }
414
415 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
416
417 /* inform the wxDropTarget about the current GtkSelectionData.
418 this is only valid for the duration of this call */
419 drop_target->SetDragData( data );
420
421 wxDragResult result = ConvertFromGTK(context->action);
422
423 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
424 {
425 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") );
426
427 /* tell GTK that data transfer was successful */
428 gtk_drag_finish( context, TRUE, FALSE, time );
429 }
430 else
431 {
432 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
433
434 /* tell GTK that data transfer was not successful */
435 gtk_drag_finish( context, FALSE, FALSE, time );
436 }
437
438 /* after this, invalidate the drop_target's drag data */
439 drop_target->SetDragData( (GtkSelectionData*) NULL );
440 }
441 }
442
443 //----------------------------------------------------------------------------
444 // wxDropTarget
445 //----------------------------------------------------------------------------
446
447 wxDropTarget::wxDropTarget( wxDataObject *data )
448 : wxDropTargetBase( data )
449 {
450 m_firstMotion = true;
451 m_dragContext = (GdkDragContext*) NULL;
452 m_dragWidget = (GtkWidget*) NULL;
453 m_dragData = (GtkSelectionData*) NULL;
454 m_dragTime = 0;
455 }
456
457 wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
458 wxCoord WXUNUSED(y),
459 wxDragResult def )
460 {
461 // GetMatchingPair() checks for m_dataObject too, no need to do it here
462
463 // disable the debug message from GetMatchingPair() - there are too many
464 // of them otherwise
465 #ifdef __WXDEBUG__
466 wxLogNull noLog;
467 #endif // Debug
468
469 return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
470 }
471
472 bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
473 {
474 if (!m_dataObject)
475 return false;
476
477 return (GetMatchingPair() != (GdkAtom) 0);
478 }
479
480 wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
481 wxDragResult def )
482 {
483 if (!m_dataObject)
484 return wxDragNone;
485
486 if (GetMatchingPair() == (GdkAtom) 0)
487 return wxDragNone;
488
489 return GetData() ? def : wxDragNone;
490 }
491
492 GdkAtom wxDropTarget::GetMatchingPair()
493 {
494 if (!m_dataObject)
495 return (GdkAtom) 0;
496
497 if (!m_dragContext)
498 return (GdkAtom) 0;
499
500 GList *child = m_dragContext->targets;
501 while (child)
502 {
503 GdkAtom formatAtom = (GdkAtom)(child->data);
504 wxDataFormat format( formatAtom );
505
506 #ifdef __WXDEBUG__
507 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
508 format.GetId().c_str());
509 #endif // Debug
510
511 if (m_dataObject->IsSupportedFormat( format ))
512 return formatAtom;
513
514 child = child->next;
515 }
516
517 return (GdkAtom) 0;
518 }
519
520 bool wxDropTarget::GetData()
521 {
522 if (!m_dragData)
523 return false;
524
525 if (!m_dataObject)
526 return false;
527
528 wxDataFormat dragFormat( m_dragData->target );
529
530 if (!m_dataObject->IsSupportedFormat( dragFormat ))
531 return false;
532
533 m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
534
535 return true;
536 }
537
538 void wxDropTarget::UnregisterWidget( GtkWidget *widget )
539 {
540 wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
541
542 gtk_drag_dest_unset( widget );
543
544 g_signal_handlers_disconnect_by_func (widget,
545 (gpointer) target_drag_leave, this);
546 g_signal_handlers_disconnect_by_func (widget,
547 (gpointer) target_drag_motion, this);
548 g_signal_handlers_disconnect_by_func (widget,
549 (gpointer) target_drag_drop, this);
550 g_signal_handlers_disconnect_by_func (widget,
551 (gpointer) target_drag_data_received, this);
552 }
553
554 void wxDropTarget::RegisterWidget( GtkWidget *widget )
555 {
556 wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
557
558 /* gtk_drag_dest_set() determines what default behaviour we'd like
559 GTK to supply. we don't want to specify out targets (=formats)
560 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
561 not GTK_DEST_DEFAULT_DROP). instead we react individually to
562 "drag_motion" and "drag_drop" events. this makes it possible
563 to allow dropping on only a small area. we should set
564 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
565 highlighting if dragging over standard controls, but this
566 seems to be broken without the other two. */
567
568 gtk_drag_dest_set( widget,
569 (GtkDestDefaults) 0, /* no default behaviour */
570 (GtkTargetEntry*) NULL, /* we don't supply any formats here */
571 0, /* number of targets = 0 */
572 (GdkDragAction) 0 ); /* we don't supply any actions here */
573
574 g_signal_connect (widget, "drag_leave",
575 G_CALLBACK (target_drag_leave), this);
576
577 g_signal_connect (widget, "drag_motion",
578 G_CALLBACK (target_drag_motion), this);
579
580 g_signal_connect (widget, "drag_drop",
581 G_CALLBACK (target_drag_drop), this);
582
583 g_signal_connect (widget, "drag_data_received",
584 G_CALLBACK (target_drag_data_received), this);
585 }
586
587 //----------------------------------------------------------------------------
588 // "drag_data_get"
589 //----------------------------------------------------------------------------
590
591 extern "C" {
592 static void
593 source_drag_data_get (GtkWidget *WXUNUSED(widget),
594 GdkDragContext *WXUNUSED(context),
595 GtkSelectionData *selection_data,
596 guint WXUNUSED(info),
597 guint WXUNUSED(time),
598 wxDropSource *drop_source )
599 {
600 if (g_isIdle) wxapp_install_idle_handler();
601
602 wxDataFormat format( selection_data->target );
603
604 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
605 format.GetId().c_str());
606
607 drop_source->m_retValue = wxDragCancel;
608
609 wxDataObject *data = drop_source->GetDataObject();
610
611 if (!data)
612 {
613 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
614 return;
615 }
616
617 if (!data->IsSupportedFormat(format))
618 {
619 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
620 return;
621 }
622
623 if (data->GetDataSize(format) == 0)
624 {
625 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
626 return;
627 }
628
629 size_t size = data->GetDataSize(format);
630
631 // printf( "data size: %d.\n", (int)data_size );
632
633 guchar *d = new guchar[size];
634
635 if (!data->GetDataHere( format, (void*)d ))
636 {
637 delete[] d;
638 return;
639 }
640
641 #if wxUSE_THREADS
642 /* disable GUI threads */
643 #endif
644
645 gtk_selection_data_set( selection_data,
646 selection_data->target,
647 8, // 8-bit
648 d,
649 size );
650
651 #if wxUSE_THREADS
652 /* enable GUI threads */
653 #endif
654
655 delete[] d;
656 }
657 }
658
659 //----------------------------------------------------------------------------
660 // "drag_data_delete"
661 //----------------------------------------------------------------------------
662
663 extern "C" {
664 static void source_drag_data_delete( GtkWidget *WXUNUSED(widget),
665 GdkDragContext *WXUNUSED(context),
666 wxDropSource *WXUNUSED(drop_source) )
667 {
668 if (g_isIdle)
669 wxapp_install_idle_handler();
670
671 // printf( "Drag source: drag_data_delete\n" );
672 }
673 }
674
675 //----------------------------------------------------------------------------
676 // "drag_begin"
677 //----------------------------------------------------------------------------
678
679 extern "C" {
680 static void source_drag_begin( GtkWidget *WXUNUSED(widget),
681 GdkDragContext *WXUNUSED(context),
682 wxDropSource *WXUNUSED(drop_source) )
683 {
684 if (g_isIdle)
685 wxapp_install_idle_handler();
686
687 // printf( "Drag source: drag_begin.\n" );
688 }
689 }
690
691 //----------------------------------------------------------------------------
692 // "drag_end"
693 //----------------------------------------------------------------------------
694
695 extern "C" {
696 static void source_drag_end( GtkWidget *WXUNUSED(widget),
697 GdkDragContext *WXUNUSED(context),
698 wxDropSource *drop_source )
699 {
700 if (g_isIdle) wxapp_install_idle_handler();
701
702 // printf( "Drag source: drag_end.\n" );
703
704 drop_source->m_waiting = false;
705 }
706 }
707
708 //-----------------------------------------------------------------------------
709 // "configure_event" from m_iconWindow
710 //-----------------------------------------------------------------------------
711
712 extern "C" {
713 static gint
714 gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
715 {
716 // don't need to install idle handler, its done from "event" signal
717
718 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
719
720 return 0;
721 }
722 }
723
724 //---------------------------------------------------------------------------
725 // wxDropSource
726 //---------------------------------------------------------------------------
727
728 wxDropSource::wxDropSource(wxWindow *win,
729 const wxIcon &iconCopy,
730 const wxIcon &iconMove,
731 const wxIcon &iconNone)
732 {
733 m_waiting = true;
734
735 m_iconWindow = (GtkWidget*) NULL;
736
737 m_window = win;
738 m_widget = win->m_widget;
739 if (win->m_wxwindow) m_widget = win->m_wxwindow;
740
741 m_retValue = wxDragCancel;
742
743 SetIcons(iconCopy, iconMove, iconNone);
744 }
745
746 wxDropSource::wxDropSource(wxDataObject& data,
747 wxWindow *win,
748 const wxIcon &iconCopy,
749 const wxIcon &iconMove,
750 const wxIcon &iconNone)
751 {
752 m_waiting = true;
753
754 SetData( data );
755
756 m_iconWindow = (GtkWidget*) NULL;
757
758 m_window = win;
759 m_widget = win->m_widget;
760 if (win->m_wxwindow) m_widget = win->m_wxwindow;
761
762 m_retValue = wxDragCancel;
763
764 SetIcons(iconCopy, iconMove, iconNone);
765 }
766
767 void wxDropSource::SetIcons(const wxIcon &iconCopy,
768 const wxIcon &iconMove,
769 const wxIcon &iconNone)
770 {
771 m_iconCopy = iconCopy;
772 m_iconMove = iconMove;
773 m_iconNone = iconNone;
774
775 if ( !m_iconCopy.Ok() )
776 m_iconCopy = wxIcon(page_xpm);
777 if ( !m_iconMove.Ok() )
778 m_iconMove = m_iconCopy;
779 if ( !m_iconNone.Ok() )
780 m_iconNone = m_iconCopy;
781 }
782
783 wxDropSource::~wxDropSource()
784 {
785 }
786
787 void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
788 {
789 // get the right icon to display
790 wxIcon *icon = NULL;
791 if ( action & GDK_ACTION_MOVE )
792 icon = &m_iconMove;
793 else if ( action & GDK_ACTION_COPY )
794 icon = &m_iconCopy;
795 else
796 icon = &m_iconNone;
797
798 GdkBitmap *mask;
799 if ( icon->GetMask() )
800 mask = icon->GetMask()->GetBitmap();
801 else
802 mask = (GdkBitmap *)NULL;
803
804 GdkPixmap *pixmap = icon->GetPixmap();
805
806 gint width,height;
807 gdk_drawable_get_size (pixmap, &width, &height);
808
809 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
810 gtk_widget_push_colormap (colormap);
811
812 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
813 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
814 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE);
815
816 gtk_widget_pop_colormap ();
817
818 gtk_widget_set_size_request (m_iconWindow, width, height);
819 gtk_widget_realize (m_iconWindow);
820
821 g_signal_connect (m_iconWindow, "configure_event",
822 G_CALLBACK (gtk_dnd_window_configure_callback), this);
823
824 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
825
826 if (mask)
827 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
828
829 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
830 }
831
832 wxDragResult wxDropSource::DoDragDrop(int flags)
833 {
834 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
835 wxT("Drop source: no data") );
836
837 // still in drag
838 if (g_blockEventsOnDrag)
839 return wxDragNone;
840
841 // don't start dragging if no button is down
842 if (g_lastButtonNumber == 0)
843 return wxDragNone;
844
845 // we can only start a drag after a mouse event
846 if (g_lastMouseEvent == NULL)
847 return wxDragNone;
848
849 // disabled for now
850 g_blockEventsOnDrag = true;
851
852 RegisterWindow();
853
854 m_waiting = true;
855
856 GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
857
858 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
859 m_data->GetAllFormats( array );
860 size_t count = m_data->GetFormatCount();
861 for (size_t i = 0; i < count; i++)
862 {
863 GdkAtom atom = array[i];
864 wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ));
865 gtk_target_list_add( target_list, atom, 0, 0 );
866 }
867 delete[] array;
868
869 int action = GDK_ACTION_COPY;
870 if ( flags & wxDrag_AllowMove )
871 action |= GDK_ACTION_MOVE;
872
873 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
874 // to use a global to pass the flags to the drop target but I'd
875 // surely prefer a better way to do it
876 gs_flagsForDrag = flags;
877
878 GdkDragContext *context = gtk_drag_begin( m_widget,
879 target_list,
880 (GdkDragAction)action,
881 g_lastButtonNumber, // number of mouse button which started drag
882 (GdkEvent*) g_lastMouseEvent );
883
884 m_dragContext = context;
885
886 PrepareIcon( action, context );
887
888 while (m_waiting)
889 gtk_main_iteration();
890
891 m_retValue = ConvertFromGTK(context->action);
892 if ( m_retValue == wxDragNone )
893 m_retValue = wxDragCancel;
894
895 g_blockEventsOnDrag = false;
896
897 UnregisterWindow();
898
899 return m_retValue;
900 }
901
902 void wxDropSource::RegisterWindow()
903 {
904 if (!m_widget) return;
905
906 g_signal_connect (m_widget, "drag_data_get",
907 G_CALLBACK (source_drag_data_get), this);
908 g_signal_connect (m_widget, "drag_data_delete",
909 G_CALLBACK (source_drag_data_delete), this);
910 g_signal_connect (m_widget, "drag_begin",
911 G_CALLBACK (source_drag_begin), this);
912 g_signal_connect (m_widget, "drag_end",
913 G_CALLBACK (source_drag_end), this);
914
915 }
916
917 void wxDropSource::UnregisterWindow()
918 {
919 if (!m_widget)
920 return;
921
922 g_signal_handlers_disconnect_by_func (m_widget,
923 (gpointer) source_drag_data_get,
924 this);
925 g_signal_handlers_disconnect_by_func (m_widget,
926 (gpointer) source_drag_data_delete,
927 this);
928 g_signal_handlers_disconnect_by_func (m_widget,
929 (gpointer) source_drag_begin,
930 this);
931 g_signal_handlers_disconnect_by_func (m_widget,
932 (gpointer) source_drag_end,
933 this);
934 }
935
936 #endif
937 // wxUSE_DRAG_AND_DROP