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