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