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