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