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