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