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