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