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