]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dnd.cpp
corrected painting implementation for wxDFB
[wxWidgets.git] / src / gtk / dnd.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dnd.cpp
3 // Purpose: wxDropTarget class
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_DRAG_AND_DROP
14
15 #include "wx/dnd.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/log.h"
20 #include "wx/app.h"
21 #include "wx/utils.h"
22 #include "wx/window.h"
23 #include "wx/gdicmn.h"
24 #endif
25
26 #include "wx/gtk/private.h"
27
28 #include <gdk/gdkprivate.h>
29
30 #include <gtk/gtkdnd.h>
31 #include <gtk/gtkselection.h>
32
33 //----------------------------------------------------------------------------
34 // global data
35 //----------------------------------------------------------------------------
36
37 extern bool g_blockEventsOnDrag;
38
39 // the flags used for the last DoDragDrop()
40 static long gs_flagsForDrag = 0;
41
42 // the trace mask we use with wxLogTrace() - call
43 // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
44 // (there are quite a few of them, so don't enable this by default)
45 static const wxChar *TRACE_DND = _T("dnd");
46
47 //----------------------------------------------------------------------------
48 // standard icons
49 //----------------------------------------------------------------------------
50
51 /* Copyright (c) Julian Smart */
52 static const char * page_xpm[] = {
53 /* columns rows colors chars-per-pixel */
54 "32 32 37 1",
55 "5 c #7198D9",
56 ", c #769CDA",
57 "2 c #DCE6F6",
58 "i c #FFFFFF",
59 "e c #779DDB",
60 ": c #9AB6E4",
61 "9 c #EAF0FA",
62 "- c #B1C7EB",
63 "$ c #6992D7",
64 "y c #F7F9FD",
65 "= c #BED0EE",
66 "q c #F0F5FC",
67 "; c #A8C0E8",
68 "@ c #366BC2",
69 " c None",
70 "u c #FDFEFF",
71 "8 c #5987D3",
72 "* c #C4D5F0",
73 "7 c #7CA0DC",
74 "O c #487BCE",
75 "< c #6B94D7",
76 "& c #CCDAF2",
77 "> c #89A9DF",
78 "3 c #5584D1",
79 "w c #82A5DE",
80 "1 c #3F74CB",
81 "+ c #3A70CA",
82 ". c #3569BF",
83 "% c #D2DFF4",
84 "# c #3366BB",
85 "r c #F5F8FD",
86 "0 c #FAFCFE",
87 "4 c #DFE8F7",
88 "X c #5E8AD4",
89 "o c #5282D0",
90 "t c #B8CCEC",
91 "6 c #E5EDF9",
92 /* pixels */
93 " ",
94 " ",
95 " ",
96 " ",
97 " ",
98 " .XXXooOO++@# ",
99 " $%&*=-;::>,<1 ",
100 " $2%&*=-;::><:3 ",
101 " $42%&*=-;::<&:3 ",
102 " 56477<<<<8<<9&:X ",
103 " 59642%&*=-;<09&:5 ",
104 " 5q9642%&*=-<<<<<# ",
105 " 5qqw777<<<<<88:>+ ",
106 " erqq9642%&*=t;::+ ",
107 " eyrqq9642%&*=t;:O ",
108 " eyywwww777<<<<t;O ",
109 " e0yyrqq9642%&*=to ",
110 " e00yyrqq9642%&*=o ",
111 " eu0wwwwwww777<&*X ",
112 " euu00yyrqq9642%&X ",
113 " eiuu00yyrqq9642%X ",
114 " eiiwwwwwwwwww742$ ",
115 " eiiiuu00yyrqq964$ ",
116 " eiiiiuu00yyrqq96$ ",
117 " eiiiiiuu00yyrqq95 ",
118 " eiiiiiiuu00yyrqq5 ",
119 " eeeeeeeeeeeeee55e ",
120 " ",
121 " ",
122 " ",
123 " ",
124 " "
125 };
126
127
128 // ============================================================================
129 // private functions
130 // ============================================================================
131
132 // ----------------------------------------------------------------------------
133 // convert between GTK+ and wxWidgets DND constants
134 // ----------------------------------------------------------------------------
135
136 static wxDragResult ConvertFromGTK(long action)
137 {
138 switch ( action )
139 {
140 case GDK_ACTION_COPY:
141 return wxDragCopy;
142
143 case GDK_ACTION_LINK:
144 return wxDragLink;
145
146 case GDK_ACTION_MOVE:
147 return wxDragMove;
148 }
149
150 return wxDragNone;
151 }
152
153 // ----------------------------------------------------------------------------
154 // "drag_leave"
155 // ----------------------------------------------------------------------------
156
157 extern "C" {
158 static void target_drag_leave( GtkWidget *WXUNUSED(widget),
159 GdkDragContext *context,
160 guint WXUNUSED(time),
161 wxDropTarget *drop_target )
162 {
163 if (g_isIdle) wxapp_install_idle_handler();
164
165 /* inform the wxDropTarget about the current GdkDragContext.
166 this is only valid for the duration of this call */
167 drop_target->SetDragContext( context );
168
169 /* we don't need return values. this event is just for
170 information */
171 drop_target->OnLeave();
172
173 /* this has to be done because GDK has no "drag_enter" event */
174 drop_target->m_firstMotion = true;
175
176 /* after this, invalidate the drop_target's GdkDragContext */
177 drop_target->SetDragContext( (GdkDragContext*) NULL );
178 }
179 }
180
181 // ----------------------------------------------------------------------------
182 // "drag_motion"
183 // ----------------------------------------------------------------------------
184
185 extern "C" {
186 static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget),
187 GdkDragContext *context,
188 gint x,
189 gint y,
190 guint time,
191 wxDropTarget *drop_target )
192 {
193 if (g_isIdle) wxapp_install_idle_handler();
194
195 /* Owen Taylor: "if the coordinates not in a drop zone,
196 return FALSE, otherwise call gtk_drag_status() and
197 return TRUE" */
198
199 /* inform the wxDropTarget about the current GdkDragContext.
200 this is only valid for the duration of this call */
201 drop_target->SetDragContext( context );
202
203 // GTK+ always supposes that we want to copy the data by default while we
204 // might want to move it, so examine not only suggested_action - which is
205 // only good if we don't have our own preferences - but also the actions
206 // field
207 wxDragResult result;
208 if (drop_target->GetDefaultAction() == wxDragNone)
209 {
210 // use default action set by wxDropSource::DoDragDrop()
211 if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove &&
212 (context->actions & GDK_ACTION_MOVE ) )
213 {
214 // move is requested by the program and allowed by GTK+ - do it, even
215 // though suggested_action may be currently wxDragCopy
216 result = wxDragMove;
217 }
218 else // use whatever GTK+ says we should
219 {
220 result = ConvertFromGTK(context->suggested_action);
221
222 if ( (result == wxDragMove) && !(gs_flagsForDrag & wxDrag_AllowMove) )
223 {
224 // we're requested to move but we can't
225 result = wxDragCopy;
226 }
227 }
228 }
229 else if (drop_target->GetDefaultAction() == wxDragMove &&
230 (context->actions & GDK_ACTION_MOVE))
231 {
232 result = wxDragMove;
233 }
234 else
235 {
236 if (context->actions & GDK_ACTION_COPY)
237 result = wxDragCopy;
238 else if (context->actions & GDK_ACTION_MOVE)
239 result = wxDragMove;
240 else
241 result = wxDragNone;
242 }
243
244 if (drop_target->m_firstMotion)
245 {
246 /* the first "drag_motion" event substitutes a "drag_enter" event */
247 result = drop_target->OnEnter( x, y, result );
248 }
249 else
250 {
251 /* give program a chance to react (i.e. to say no by returning FALSE) */
252 result = drop_target->OnDragOver( x, y, result );
253 }
254
255 bool ret = wxIsDragResultOk( result );
256 if (ret)
257 {
258 GdkDragAction action;
259 if (result == wxDragCopy)
260 action = GDK_ACTION_COPY;
261 else if (result == wxDragLink)
262 action = GDK_ACTION_LINK;
263 else
264 action = GDK_ACTION_MOVE;
265
266 gdk_drag_status( context, action, time );
267 }
268
269 /* after this, invalidate the drop_target's GdkDragContext */
270 drop_target->SetDragContext( (GdkDragContext*) NULL );
271
272 /* this has to be done because GDK has no "drag_enter" event */
273 drop_target->m_firstMotion = false;
274
275 return ret;
276 }
277 }
278
279 // ----------------------------------------------------------------------------
280 // "drag_drop"
281 // ----------------------------------------------------------------------------
282
283 extern "C" {
284 static gboolean target_drag_drop( GtkWidget *widget,
285 GdkDragContext *context,
286 gint x,
287 gint y,
288 guint time,
289 wxDropTarget *drop_target )
290 {
291 if (g_isIdle) wxapp_install_idle_handler();
292
293 /* Owen Taylor: "if the drop is not in a drop zone,
294 return FALSE, otherwise, if you aren't accepting
295 the drop, call gtk_drag_finish() with success == FALSE
296 otherwise call gtk_drag_data_get()" */
297
298 // printf( "drop.\n" );
299
300 /* this seems to make a difference between not accepting
301 due to wrong target area and due to wrong format. let
302 us hope that this is not required.. */
303
304 /* inform the wxDropTarget about the current GdkDragContext.
305 this is only valid for the duration of this call */
306 drop_target->SetDragContext( context );
307
308 /* inform the wxDropTarget about the current drag widget.
309 this is only valid for the duration of this call */
310 drop_target->SetDragWidget( widget );
311
312 /* inform the wxDropTarget about the current drag time.
313 this is only valid for the duration of this call */
314 drop_target->SetDragTime( time );
315
316 /*
317 wxDragResult result = wxDragMove;
318 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
319 */
320
321 /* reset the block here as someone might very well
322 show a dialog as a reaction to a drop and this
323 wouldn't work without events */
324 g_blockEventsOnDrag = false;
325
326 bool ret = drop_target->OnDrop( x, y );
327
328 if (!ret)
329 {
330 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
331
332 /* cancel the whole thing */
333 gtk_drag_finish( context,
334 FALSE, /* no success */
335 FALSE, /* don't delete data on dropping side */
336 time );
337 }
338 else
339 {
340 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") );
341
342 #if wxUSE_THREADS
343 /* disable GUI threads */
344 #endif
345
346 GdkAtom format = drop_target->GetMatchingPair();
347
348 // this does happen somehow, see bug 555111
349 wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") );
350
351 /*
352 GdkDragAction action = GDK_ACTION_MOVE;
353 if (result == wxDragCopy) action == GDK_ACTION_COPY;
354 context->action = action;
355 */
356 /* this should trigger an "drag_data_received" event */
357 gtk_drag_get_data( widget,
358 context,
359 format,
360 time );
361
362 #if wxUSE_THREADS
363 /* re-enable GUI threads */
364 #endif
365 }
366
367 /* after this, invalidate the drop_target's GdkDragContext */
368 drop_target->SetDragContext( (GdkDragContext*) NULL );
369
370 /* after this, invalidate the drop_target's drag widget */
371 drop_target->SetDragWidget( (GtkWidget*) NULL );
372
373 /* this has to be done because GDK has no "drag_enter" event */
374 drop_target->m_firstMotion = true;
375
376 return ret;
377 }
378 }
379
380 // ----------------------------------------------------------------------------
381 // "drag_data_received"
382 // ----------------------------------------------------------------------------
383
384 extern "C" {
385 static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
386 GdkDragContext *context,
387 gint x,
388 gint y,
389 GtkSelectionData *data,
390 guint WXUNUSED(info),
391 guint time,
392 wxDropTarget *drop_target )
393 {
394 if (g_isIdle) wxapp_install_idle_handler();
395
396 /* Owen Taylor: "call gtk_drag_finish() with
397 success == TRUE" */
398
399 if ((data->length <= 0) || (data->format != 8))
400 {
401 /* negative data length and non 8-bit data format
402 qualifies for junk */
403 gtk_drag_finish (context, FALSE, FALSE, time);
404
405 return;
406 }
407
408 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
409
410 /* inform the wxDropTarget about the current GtkSelectionData.
411 this is only valid for the duration of this call */
412 drop_target->SetDragData( data );
413
414 wxDragResult result = ConvertFromGTK(context->action);
415
416 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
417 {
418 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") );
419
420 /* tell GTK that data transfer was successful */
421 gtk_drag_finish( context, TRUE, FALSE, time );
422 }
423 else
424 {
425 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
426
427 /* tell GTK that data transfer was not successful */
428 gtk_drag_finish( context, FALSE, FALSE, time );
429 }
430
431 /* after this, invalidate the drop_target's drag data */
432 drop_target->SetDragData( (GtkSelectionData*) NULL );
433 }
434 }
435
436 //----------------------------------------------------------------------------
437 // wxDropTarget
438 //----------------------------------------------------------------------------
439
440 wxDropTarget::wxDropTarget( wxDataObject *data )
441 : wxDropTargetBase( data )
442 {
443 m_firstMotion = true;
444 m_dragContext = (GdkDragContext*) NULL;
445 m_dragWidget = (GtkWidget*) NULL;
446 m_dragData = (GtkSelectionData*) NULL;
447 m_dragTime = 0;
448 }
449
450 wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
451 wxCoord WXUNUSED(y),
452 wxDragResult def )
453 {
454 // GetMatchingPair() checks for m_dataObject too, no need to do it here
455
456 // disable the debug message from GetMatchingPair() - there are too many
457 // of them otherwise
458 #ifdef __WXDEBUG__
459 wxLogNull noLog;
460 #endif // Debug
461
462 return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
463 }
464
465 bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
466 {
467 if (!m_dataObject)
468 return false;
469
470 return (GetMatchingPair() != (GdkAtom) 0);
471 }
472
473 wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
474 wxDragResult def )
475 {
476 if (!m_dataObject)
477 return wxDragNone;
478
479 if (GetMatchingPair() == (GdkAtom) 0)
480 return wxDragNone;
481
482 return GetData() ? def : wxDragNone;
483 }
484
485 GdkAtom wxDropTarget::GetMatchingPair()
486 {
487 if (!m_dataObject)
488 return (GdkAtom) 0;
489
490 if (!m_dragContext)
491 return (GdkAtom) 0;
492
493 GList *child = m_dragContext->targets;
494 while (child)
495 {
496 GdkAtom formatAtom = (GdkAtom)(child->data);
497 wxDataFormat format( formatAtom );
498
499 #ifdef __WXDEBUG__
500 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
501 format.GetId().c_str());
502 #endif // Debug
503
504 if (m_dataObject->IsSupportedFormat( format ))
505 return formatAtom;
506
507 child = child->next;
508 }
509
510 return (GdkAtom) 0;
511 }
512
513 bool wxDropTarget::GetData()
514 {
515 if (!m_dragData)
516 return false;
517
518 if (!m_dataObject)
519 return false;
520
521 wxDataFormat dragFormat( m_dragData->target );
522
523 if (!m_dataObject->IsSupportedFormat( dragFormat ))
524 return false;
525
526 m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
527
528 return true;
529 }
530
531 void wxDropTarget::UnregisterWidget( GtkWidget *widget )
532 {
533 wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
534
535 gtk_drag_dest_unset( widget );
536
537 g_signal_handlers_disconnect_by_func (widget,
538 (gpointer) target_drag_leave, this);
539 g_signal_handlers_disconnect_by_func (widget,
540 (gpointer) target_drag_motion, this);
541 g_signal_handlers_disconnect_by_func (widget,
542 (gpointer) target_drag_drop, this);
543 g_signal_handlers_disconnect_by_func (widget,
544 (gpointer) target_drag_data_received, this);
545 }
546
547 void wxDropTarget::RegisterWidget( GtkWidget *widget )
548 {
549 wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
550
551 /* gtk_drag_dest_set() determines what default behaviour we'd like
552 GTK to supply. we don't want to specify out targets (=formats)
553 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
554 not GTK_DEST_DEFAULT_DROP). instead we react individually to
555 "drag_motion" and "drag_drop" events. this makes it possible
556 to allow dropping on only a small area. we should set
557 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
558 highlighting if dragging over standard controls, but this
559 seems to be broken without the other two. */
560
561 gtk_drag_dest_set( widget,
562 (GtkDestDefaults) 0, /* no default behaviour */
563 (GtkTargetEntry*) NULL, /* we don't supply any formats here */
564 0, /* number of targets = 0 */
565 (GdkDragAction) 0 ); /* we don't supply any actions here */
566
567 g_signal_connect (widget, "drag_leave",
568 G_CALLBACK (target_drag_leave), this);
569
570 g_signal_connect (widget, "drag_motion",
571 G_CALLBACK (target_drag_motion), this);
572
573 g_signal_connect (widget, "drag_drop",
574 G_CALLBACK (target_drag_drop), this);
575
576 g_signal_connect (widget, "drag_data_received",
577 G_CALLBACK (target_drag_data_received), this);
578 }
579
580 //----------------------------------------------------------------------------
581 // "drag_data_get"
582 //----------------------------------------------------------------------------
583
584 extern "C" {
585 static void
586 source_drag_data_get (GtkWidget *WXUNUSED(widget),
587 GdkDragContext *WXUNUSED(context),
588 GtkSelectionData *selection_data,
589 guint WXUNUSED(info),
590 guint WXUNUSED(time),
591 wxDropSource *drop_source )
592 {
593 if (g_isIdle) wxapp_install_idle_handler();
594
595 wxDataFormat format( selection_data->target );
596
597 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
598 format.GetId().c_str());
599
600 drop_source->m_retValue = wxDragCancel;
601
602 wxDataObject *data = drop_source->GetDataObject();
603
604 if (!data)
605 {
606 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
607 return;
608 }
609
610 if (!data->IsSupportedFormat(format))
611 {
612 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
613 return;
614 }
615
616 if (data->GetDataSize(format) == 0)
617 {
618 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
619 return;
620 }
621
622 size_t size = data->GetDataSize(format);
623
624 // printf( "data size: %d.\n", (int)data_size );
625
626 guchar *d = new guchar[size];
627
628 if (!data->GetDataHere( format, (void*)d ))
629 {
630 delete[] d;
631 return;
632 }
633
634 #if wxUSE_THREADS
635 /* disable GUI threads */
636 #endif
637
638 gtk_selection_data_set( selection_data,
639 selection_data->target,
640 8, // 8-bit
641 d,
642 size );
643
644 #if wxUSE_THREADS
645 /* enable GUI threads */
646 #endif
647
648 delete[] d;
649 }
650 }
651
652 //----------------------------------------------------------------------------
653 // "drag_data_delete"
654 //----------------------------------------------------------------------------
655
656 extern "C" {
657 static void source_drag_data_delete( GtkWidget *WXUNUSED(widget),
658 GdkDragContext *context,
659 wxDropSource *WXUNUSED(drop_source) )
660 {
661 if (g_isIdle)
662 wxapp_install_idle_handler();
663
664 // printf( "Drag source: drag_data_delete\n" );
665 }
666 }
667
668 //----------------------------------------------------------------------------
669 // "drag_begin"
670 //----------------------------------------------------------------------------
671
672 extern "C" {
673 static void source_drag_begin( GtkWidget *WXUNUSED(widget),
674 GdkDragContext *WXUNUSED(context),
675 wxDropSource *WXUNUSED(drop_source) )
676 {
677 if (g_isIdle)
678 wxapp_install_idle_handler();
679
680 // printf( "Drag source: drag_begin.\n" );
681 }
682 }
683
684 //----------------------------------------------------------------------------
685 // "drag_end"
686 //----------------------------------------------------------------------------
687
688 extern "C" {
689 static void source_drag_end( GtkWidget *WXUNUSED(widget),
690 GdkDragContext *WXUNUSED(context),
691 wxDropSource *drop_source )
692 {
693 if (g_isIdle) wxapp_install_idle_handler();
694
695 // printf( "Drag source: drag_end.\n" );
696
697 drop_source->m_waiting = false;
698 }
699 }
700
701 //-----------------------------------------------------------------------------
702 // "configure_event" from m_iconWindow
703 //-----------------------------------------------------------------------------
704
705 extern "C" {
706 static gint
707 gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
708 {
709 // don't need to install idle handler, its done from "event" signal
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_drawable_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 // this shouldn't be needed but somehow, sometimes, without this the cursor
914 // stays grabbed even when the DND operation ends and the application
915 // becomes unresponsive and has to be killed resulting in loss of all
916 // unsaved data, so while this fix is ugly it's still better than
917 // alternative
918 if ( gdk_pointer_is_grabbed() )
919 gdk_pointer_ungrab(GDK_CURRENT_TIME);
920
921 return m_retValue;
922 }
923
924 void wxDropSource::RegisterWindow()
925 {
926 if (!m_widget) return;
927
928 g_signal_connect (m_widget, "drag_data_get",
929 G_CALLBACK (source_drag_data_get), this);
930 g_signal_connect (m_widget, "drag_data_delete",
931 G_CALLBACK (source_drag_data_delete), this);
932 g_signal_connect (m_widget, "drag_begin",
933 G_CALLBACK (source_drag_begin), this);
934 g_signal_connect (m_widget, "drag_end",
935 G_CALLBACK (source_drag_end), this);
936
937 }
938
939 void wxDropSource::UnregisterWindow()
940 {
941 if (!m_widget)
942 return;
943
944 g_signal_handlers_disconnect_by_func (m_widget,
945 (gpointer) source_drag_data_get,
946 this);
947 g_signal_handlers_disconnect_by_func (m_widget,
948 (gpointer) source_drag_data_delete,
949 this);
950 g_signal_handlers_disconnect_by_func (m_widget,
951 (gpointer) source_drag_begin,
952 this);
953 g_signal_handlers_disconnect_by_func (m_widget,
954 (gpointer) source_drag_end,
955 this);
956 }
957
958 #endif
959 // wxUSE_DRAG_AND_DROP