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