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