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