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