]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dnd.cpp
fixing this conversion (was essentially a no-op under NON-UNICODE)
[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 if (g_isIdle)
710 wxapp_install_idle_handler();
711
712 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
713
714 return 0;
715 }
716 }
717
718 //---------------------------------------------------------------------------
719 // wxDropSource
720 //---------------------------------------------------------------------------
721
722 wxDropSource::wxDropSource(wxWindow *win,
723 const wxIcon &iconCopy,
724 const wxIcon &iconMove,
725 const wxIcon &iconNone)
726 {
727 m_waiting = true;
728
729 m_iconWindow = (GtkWidget*) NULL;
730
731 m_window = win;
732 m_widget = win->m_widget;
733 if (win->m_wxwindow) m_widget = win->m_wxwindow;
734
735 m_retValue = wxDragCancel;
736
737 SetIcons(iconCopy, iconMove, iconNone);
738 }
739
740 wxDropSource::wxDropSource(wxDataObject& data,
741 wxWindow *win,
742 const wxIcon &iconCopy,
743 const wxIcon &iconMove,
744 const wxIcon &iconNone)
745 {
746 m_waiting = true;
747
748 SetData( data );
749
750 m_iconWindow = (GtkWidget*) NULL;
751
752 m_window = win;
753 m_widget = win->m_widget;
754 if (win->m_wxwindow) m_widget = win->m_wxwindow;
755
756 m_retValue = wxDragCancel;
757
758 SetIcons(iconCopy, iconMove, iconNone);
759 }
760
761 void wxDropSource::SetIcons(const wxIcon &iconCopy,
762 const wxIcon &iconMove,
763 const wxIcon &iconNone)
764 {
765 m_iconCopy = iconCopy;
766 m_iconMove = iconMove;
767 m_iconNone = iconNone;
768
769 if ( !m_iconCopy.Ok() )
770 m_iconCopy = wxIcon(page_xpm);
771 if ( !m_iconMove.Ok() )
772 m_iconMove = m_iconCopy;
773 if ( !m_iconNone.Ok() )
774 m_iconNone = m_iconCopy;
775 }
776
777 wxDropSource::~wxDropSource()
778 {
779 }
780
781 void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
782 {
783 // get the right icon to display
784 wxIcon *icon = NULL;
785 if ( action & GDK_ACTION_MOVE )
786 icon = &m_iconMove;
787 else if ( action & GDK_ACTION_COPY )
788 icon = &m_iconCopy;
789 else
790 icon = &m_iconNone;
791
792 GdkBitmap *mask;
793 if ( icon->GetMask() )
794 mask = icon->GetMask()->GetBitmap();
795 else
796 mask = (GdkBitmap *)NULL;
797
798 GdkPixmap *pixmap = icon->GetPixmap();
799
800 gint width,height;
801 gdk_drawable_get_size (pixmap, &width, &height);
802
803 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
804 gtk_widget_push_colormap (colormap);
805
806 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
807 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
808 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE);
809
810 gtk_widget_pop_colormap ();
811
812 gtk_widget_set_size_request (m_iconWindow, width, height);
813 gtk_widget_realize (m_iconWindow);
814
815 g_signal_connect (m_iconWindow, "configure_event",
816 G_CALLBACK (gtk_dnd_window_configure_callback), this);
817
818 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
819
820 if (mask)
821 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
822
823 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
824 }
825
826 wxDragResult wxDropSource::DoDragDrop(int flags)
827 {
828 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
829 wxT("Drop source: no data") );
830
831 // still in drag
832 if (g_blockEventsOnDrag)
833 return wxDragNone;
834
835 // disabled for now
836 g_blockEventsOnDrag = true;
837
838 RegisterWindow();
839
840 m_waiting = true;
841
842 GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
843
844 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
845 m_data->GetAllFormats( array );
846 size_t count = m_data->GetFormatCount();
847 for (size_t i = 0; i < count; i++)
848 {
849 GdkAtom atom = array[i];
850 wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ));
851 gtk_target_list_add( target_list, atom, 0, 0 );
852 }
853 delete[] array;
854
855 GdkEventMotion event;
856 event.window = m_widget->window;
857 int x = 0;
858 int y = 0;
859 GdkModifierType state;
860 gdk_window_get_pointer( event.window, &x, &y, &state );
861 event.x = x;
862 event.y = y;
863 event.state = state;
864 event.time = (guint32)GDK_CURRENT_TIME;
865
866 /* GTK wants to know which button was pressed which caused the dragging */
867 int button_number = 0;
868 if (event.state & GDK_BUTTON1_MASK) button_number = 1;
869 else if (event.state & GDK_BUTTON2_MASK) button_number = 2;
870 else if (event.state & GDK_BUTTON3_MASK) button_number = 3;
871
872 #if wxUSE_THREADS
873 /* disable GUI threads */
874 #endif
875
876 /* don't start dragging if no button is down */
877 if (button_number)
878 {
879 int action = GDK_ACTION_COPY;
880 if ( flags & wxDrag_AllowMove )
881 action |= GDK_ACTION_MOVE;
882
883 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
884 // to use a global to pass the flags to the drop target but I'd
885 // surely prefer a better way to do it
886 gs_flagsForDrag = flags;
887
888 GdkDragContext *context = gtk_drag_begin( m_widget,
889 target_list,
890 (GdkDragAction)action,
891 button_number, /* number of mouse button which started drag */
892 (GdkEvent*) &event );
893
894 m_dragContext = context;
895
896 PrepareIcon( action, context );
897
898 while (m_waiting)
899 gtk_main_iteration();
900
901 m_retValue = ConvertFromGTK(context->action);
902 if ( m_retValue == wxDragNone )
903 m_retValue = wxDragCancel;
904 }
905
906 #if wxUSE_THREADS
907 /* re-enable GUI threads */
908 #endif
909
910 g_blockEventsOnDrag = false;
911
912 UnregisterWindow();
913
914 return m_retValue;
915 }
916
917 void wxDropSource::RegisterWindow()
918 {
919 if (!m_widget) return;
920
921 g_signal_connect (m_widget, "drag_data_get",
922 G_CALLBACK (source_drag_data_get), this);
923 g_signal_connect (m_widget, "drag_data_delete",
924 G_CALLBACK (source_drag_data_delete), this);
925 g_signal_connect (m_widget, "drag_begin",
926 G_CALLBACK (source_drag_begin), this);
927 g_signal_connect (m_widget, "drag_end",
928 G_CALLBACK (source_drag_end), this);
929
930 }
931
932 void wxDropSource::UnregisterWindow()
933 {
934 if (!m_widget)
935 return;
936
937 g_signal_handlers_disconnect_by_func (m_widget,
938 (gpointer) source_drag_data_get,
939 this);
940 g_signal_handlers_disconnect_by_func (m_widget,
941 (gpointer) source_drag_data_delete,
942 this);
943 g_signal_handlers_disconnect_by_func (m_widget,
944 (gpointer) source_drag_begin,
945 this);
946 g_signal_handlers_disconnect_by_func (m_widget,
947 (gpointer) source_drag_end,
948 this);
949 }
950
951 #endif
952 // wxUSE_DRAG_AND_DROP