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