1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDropTarget class
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dnd.h"
16 #if wxUSE_DRAG_AND_DROP
18 #include "wx/window.h"
20 #include "wx/gdicmn.h"
26 #include "gdk/gdkprivate.h"
28 #include "gtk/gtkdnd.h"
29 #include "gtk/gtkselection.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern void wxapp_install_idle_handler();
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
43 extern void wxapp_install_thread_wakeup();
44 extern void wxapp_uninstall_thread_wakeup();
47 //----------------------------------------------------------------------------
49 //----------------------------------------------------------------------------
51 extern bool g_blockEventsOnDrag
;
53 //----------------------------------------------------------------------------
55 //----------------------------------------------------------------------------
58 static char * gv_xpm
[] = {
72 " ....XX....XX....XX. ",
73 " .XXX.XXX..XXXX..XXX.... ",
74 " .XXXXXXXXXXXXXXXXXXX.XXX. ",
75 " .XXXXXXXXXXXXXXXXXXXXXXXX. ",
76 " .XXXXXXXXXXXXXXXXXXXXXXXX. ",
77 " ..XXXXXXXXXXXXXXXXXXXXXX. ",
78 " .XXXXXXXXXXXXXXXXXX... ",
79 " ..XXXXXXXXXXXXXXXX. ",
80 " .XXXXXXXXXXXXXXXX. ",
81 " .XXXXXXXXXXXXXXXX. ",
82 " .XXXXXXXXXXXXXXXXX. ",
83 " .XXXXXXXXXXXXXXXXX. ",
84 " .XXXXXXXXXXXXXXXXXX. ",
85 " .XXXXXXXXXXXXXXXXXXX. ",
86 " .XXXXXXXXXXXXXXXXXXXXX. ",
87 " .XXXXXXXXXXXXXX.XXXXXXX. ",
88 " .XXXXXXX.XXXXXXX.XXXXXXX. ",
89 " .XXXXXXXX.XXXXXXX.XXXXXXX. ",
90 " .XXXXXXX...XXXXX...XXXXX. ",
91 " .XXXXXXX. ..... ..... ",
99 static char * page_xpm
[] = {
100 /* width height ncolors chars_per_pixel */
109 " ................... ",
110 " .XXXXXXXXXXXXXXXXX.. ",
111 " .XXXXXXXXXXXXXXXXX.o. ",
112 " .XXXXXXXXXXXXXXXXX.oo. ",
113 " .XXXXXXXXXXXXXXXXX.ooo. ",
114 " .XXXXXXXXXXXXXXXXX.oooo. ",
115 " .XXXXXXXXXXXXXXXXX....... ",
116 " .XXXXXOOOOOOOOOOXXXooooo. ",
117 " .XXXXXXXXXXXXXXXXXXooooo. ",
118 " .XXXXXOOOOOOOOOOXXXXXXXX. ",
119 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
120 " .XXXXXXXOOOOOOOOOXXXXXXX. ",
121 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
122 " .XXXXXXOOOOOOOOOOXXXXXXX. ",
123 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
124 " .XXXXXOOOOOOOOOOXXXXXXXX. ",
125 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
126 " .XXXXXXXOOOOOOOOOXXXXXXX. ",
127 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
128 " .XXXXXXOOOOOOOOOOXXXXXXX. ",
129 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
130 " .XXXXXOOOOOOOOOOXXXXXXXX. ",
131 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
132 " .XXXXXXOOOOOOOOOOXXXXXXX. ",
133 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
134 " .XXXXXOOOOOOOXXXXXXXXXXX. ",
135 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
136 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
137 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
138 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
139 " .XXXXXXXXXXXXXXXXXXXXXXX. ",
140 " ......................... "};
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 static void target_drag_leave( GtkWidget
*WXUNUSED(widget
),
149 GdkDragContext
*context
,
150 guint
WXUNUSED(time
),
151 wxDropTarget
*drop_target
)
153 if (g_isIdle
) wxapp_install_idle_handler();
155 /* inform the wxDropTarget about the current GdkDragContext.
156 this is only valid for the duration of this call */
157 drop_target
->SetDragContext( context
);
159 /* we don't need return values. this event is just for
161 drop_target
->OnLeave();
163 /* this has to be done because GDK has no "drag_enter" event */
164 drop_target
->m_firstMotion
= TRUE
;
166 /* after this, invalidate the drop_target's GdkDragContext */
167 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 static gboolean
target_drag_motion( GtkWidget
*WXUNUSED(widget
),
175 GdkDragContext
*context
,
179 wxDropTarget
*drop_target
)
181 if (g_isIdle
) wxapp_install_idle_handler();
183 /* Owen Taylor: "if the coordinates not in a drop zone,
184 return FALSE, otherwise call gtk_drag_status() and
187 /* inform the wxDropTarget about the current GdkDragContext.
188 this is only valid for the duration of this call */
189 drop_target
->SetDragContext( context
);
193 if (drop_target
->m_firstMotion
)
195 /* the first "drag_motion" event substitutes a "drag_enter" event */
196 ret
= drop_target
->OnEnter( x
, y
);
200 /* give program a chance to react (i.e. to say no by returning FALSE) */
201 ret
= drop_target
->OnMove( x
, y
);
204 /* we don't yet handle which "actions" (i.e. copy or move)
205 the target accepts. so far we simply accept the
206 suggested action. TODO. */
208 gdk_drag_status( context
, context
->suggested_action
, time
);
210 /* after this, invalidate the drop_target's GdkDragContext */
211 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
213 /* this has to be done because GDK has no "drag_enter" event */
214 drop_target
->m_firstMotion
= FALSE
;
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 static gboolean
target_drag_drop( GtkWidget
*widget
,
224 GdkDragContext
*context
,
228 wxDropTarget
*drop_target
)
230 if (g_isIdle
) wxapp_install_idle_handler();
232 /* Owen Taylor: "if the drop is not in a drop zone,
233 return FALSE, otherwise, if you aren't accepting
234 the drop, call gtk_drag_finish() with success == FALSE
235 otherwise call gtk_drag_data_get()" */
237 // printf( "drop.\n" );
239 /* this seems to make a difference between not accepting
240 due to wrong target area and due to wrong format. let
241 us hope that this is not required.. */
243 /* inform the wxDropTarget about the current GdkDragContext.
244 this is only valid for the duration of this call */
245 drop_target
->SetDragContext( context
);
247 /* inform the wxDropTarget about the current drag widget.
248 this is only valid for the duration of this call */
249 drop_target
->SetDragWidget( widget
);
251 /* inform the wxDropTarget about the current drag time.
252 this is only valid for the duration of this call */
253 drop_target
->SetDragTime( time
);
255 bool ret
= drop_target
->OnDrop( x
, y
);
259 /* cancel the whole thing */
260 gtk_drag_finish( context
,
261 FALSE
, /* no success */
262 FALSE
, /* don't delete data on dropping side */
268 /* disable GUI threads */
269 wxapp_uninstall_thread_wakeup();
272 GdkAtom format
= drop_target
->GetMatchingPair();
275 /* this should trigger an "drag_data_received" event */
276 gtk_drag_get_data( widget
,
282 /* re-enable GUI threads */
283 wxapp_install_thread_wakeup();
287 /* after this, invalidate the drop_target's GdkDragContext */
288 drop_target
->SetDragContext( (GdkDragContext
*) NULL
);
290 /* after this, invalidate the drop_target's drag widget */
291 drop_target
->SetDragWidget( (GtkWidget
*) NULL
);
293 /* this has to be done because GDK has no "drag_enter" event */
294 drop_target
->m_firstMotion
= TRUE
;
299 // ----------------------------------------------------------------------------
300 // "drag_data_received"
301 // ----------------------------------------------------------------------------
303 static void target_drag_data_received( GtkWidget
*WXUNUSED(widget
),
304 GdkDragContext
*context
,
307 GtkSelectionData
*data
,
308 guint
WXUNUSED(info
),
310 wxDropTarget
*drop_target
)
312 if (g_isIdle
) wxapp_install_idle_handler();
314 /* Owen Taylor: "call gtk_drag_finish() with
318 if ((data
->length
<= 0) || (data
->format
!= 8))
320 /* negative data length and non 8-bit data format
321 qualifies for junk */
322 gtk_drag_finish (context
, FALSE
, FALSE
, time
);
327 wxLogDebug( wxT( "Drop target: data received") );
329 /* inform the wxDropTarget about the current GtkSelectionData.
330 this is only valid for the duration of this call */
331 drop_target
->SetDragData( data
);
333 if (drop_target
->OnData( x
, y
))
335 /* tell GTK that data transfer was successfull */
336 gtk_drag_finish( context
, TRUE
, FALSE
, time
);
340 /* tell GTK that data transfer was not successfull */
341 gtk_drag_finish( context
, FALSE
, FALSE
, time
);
344 /* after this, invalidate the drop_target's drag data */
345 drop_target
->SetDragData( (GtkSelectionData
*) NULL
);
348 //----------------------------------------------------------------------------
350 //----------------------------------------------------------------------------
352 wxDropTarget::wxDropTarget( wxDataObject
*data
)
353 : wxDropTargetBase( data
)
355 m_firstMotion
= TRUE
;
356 m_dragContext
= (GdkDragContext
*) NULL
;
357 m_dragWidget
= (GtkWidget
*) NULL
;
358 m_dragData
= (GtkSelectionData
*) NULL
;
362 bool wxDropTarget::OnEnter( int WXUNUSED(x
), int WXUNUSED(y
) )
367 return (GetMatchingPair() != (GdkAtom
) 0);
370 bool wxDropTarget::OnMove( int WXUNUSED(x
), int WXUNUSED(y
) )
375 return (GetMatchingPair() != (GdkAtom
) 0);
378 bool wxDropTarget::OnDrop( int WXUNUSED(x
), int WXUNUSED(y
) )
383 return (GetMatchingPair() != (GdkAtom
) 0);
386 bool wxDropTarget::OnData( int WXUNUSED(x
), int WXUNUSED(y
) )
391 if (GetMatchingPair() == (GdkAtom
) 0)
397 GdkAtom
wxDropTarget::GetMatchingPair()
405 GList
*child
= m_dragContext
->targets
;
408 GdkAtom formatAtom
= (GdkAtom
) GPOINTER_TO_INT(child
->data
);
409 wxDataFormat
format( formatAtom
);
412 char *name
= gdk_atom_name( formatAtom
);
413 if (name
) wxLogDebug( "Drop target: drag has format: %s", name
);
415 if (m_dataObject
->IsSupportedFormat( format
))
424 bool wxDropTarget::GetData()
432 wxDataFormat
dragFormat( m_dragData
->target
);
434 if (!m_dataObject
->IsSupportedFormat( dragFormat
))
437 if (dragFormat
.GetType() == wxDF_TEXT
)
439 wxTextDataObject
*text_object
= (wxTextDataObject
*)m_dataObject
;
440 text_object
->SetText( (const char*)m_dragData
->data
);
444 if (dragFormat
.GetType() == wxDF_FILENAME
)
446 wxFileDataObject
*file_object
= (wxFileDataObject
*)m_dataObject
;
447 file_object
->SetData( 0, (const char*)m_dragData
->data
);
454 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
456 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
458 gtk_drag_dest_unset( widget
);
460 gtk_signal_disconnect_by_func( GTK_OBJECT(widget
),
461 GTK_SIGNAL_FUNC(target_drag_leave
), (gpointer
) this );
463 gtk_signal_disconnect_by_func( GTK_OBJECT(widget
),
464 GTK_SIGNAL_FUNC(target_drag_motion
), (gpointer
) this );
466 gtk_signal_disconnect_by_func( GTK_OBJECT(widget
),
467 GTK_SIGNAL_FUNC(target_drag_drop
), (gpointer
) this );
469 gtk_signal_disconnect_by_func( GTK_OBJECT(widget
),
470 GTK_SIGNAL_FUNC(target_drag_data_received
), (gpointer
) this );
473 void wxDropTarget::RegisterWidget( GtkWidget
*widget
)
475 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
477 /* gtk_drag_dest_set() determines what default behaviour we'd like
478 GTK to supply. we don't want to specify out targets (=formats)
479 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
480 not GTK_DEST_DEFAULT_DROP). instead we react individually to
481 "drag_motion" and "drag_drop" events. this makes it possible
482 to allow dropping on only a small area. we should set
483 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
484 highlighting if dragging over standard controls, but this
485 seems to be broken without the other two. */
487 gtk_drag_dest_set( widget
,
488 (GtkDestDefaults
) 0, /* no default behaviour */
489 (GtkTargetEntry
*) NULL
, /* we don't supply any formats here */
490 0, /* number of targets = 0 */
491 (GdkDragAction
) 0 ); /* we don't supply any actions here */
493 gtk_signal_connect( GTK_OBJECT(widget
), "drag_leave",
494 GTK_SIGNAL_FUNC(target_drag_leave
), (gpointer
) this );
496 gtk_signal_connect( GTK_OBJECT(widget
), "drag_motion",
497 GTK_SIGNAL_FUNC(target_drag_motion
), (gpointer
) this );
499 gtk_signal_connect( GTK_OBJECT(widget
), "drag_drop",
500 GTK_SIGNAL_FUNC(target_drag_drop
), (gpointer
) this );
502 gtk_signal_connect( GTK_OBJECT(widget
), "drag_data_received",
503 GTK_SIGNAL_FUNC(target_drag_data_received
), (gpointer
) this );
506 //----------------------------------------------------------------------------
508 //----------------------------------------------------------------------------
511 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
512 GdkDragContext
*context
,
513 GtkSelectionData
*selection_data
,
514 guint
WXUNUSED(info
),
515 guint
WXUNUSED(time
),
516 wxDropSource
*drop_source
)
518 if (g_isIdle
) wxapp_install_idle_handler();
521 char *name
= gdk_atom_name( selection_data
->target
);
522 if (name
) wxLogDebug( wxT("Drop source: format requested: %s"), name
);
525 drop_source
->m_retValue
= wxDragCancel
;
527 wxDataObject
*data
= drop_source
->m_data
;
532 if (!data
->IsSupportedFormat(selection_data
->target
))
535 if (data
->GetDataSize(selection_data
->target
) == 0)
538 size_t size
= data
->GetDataSize(selection_data
->target
);
540 // printf( "data size: %d.\n", (int)data_size );
542 guchar
*d
= new guchar
[size
];
544 if (!data
->GetDataHere( selection_data
->target
, (void*)d
))
551 /* disable GUI threads */
552 wxapp_uninstall_thread_wakeup();
555 gtk_selection_data_set( selection_data
,
556 selection_data
->target
,
562 /* enable GUI threads */
563 wxapp_install_thread_wakeup();
568 /* so far only copy, no moves. TODO. */
569 drop_source
->m_retValue
= wxDragCopy
;
572 //----------------------------------------------------------------------------
573 // "drag_data_delete"
574 //----------------------------------------------------------------------------
576 static void source_drag_data_delete( GtkWidget
*WXUNUSED(widget
),
577 GdkDragContext
*WXUNUSED(context
),
578 wxDropSource
*drop_source
)
580 if (g_isIdle
) wxapp_install_idle_handler();
582 // printf( "Delete the data!\n" );
584 drop_source
->m_retValue
= wxDragMove
;
587 //----------------------------------------------------------------------------
589 //----------------------------------------------------------------------------
591 static void source_drag_begin( GtkWidget
*WXUNUSED(widget
),
592 GdkDragContext
*WXUNUSED(context
),
593 wxDropSource
*WXUNUSED(drop_source
) )
595 if (g_isIdle
) wxapp_install_idle_handler();
597 // printf( "drag_begin.\n" );
600 //----------------------------------------------------------------------------
602 //----------------------------------------------------------------------------
604 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
605 GdkDragContext
*WXUNUSED(context
),
606 wxDropSource
*drop_source
)
608 if (g_isIdle
) wxapp_install_idle_handler();
610 // printf( "drag_end.\n" );
612 drop_source
->m_waiting
= FALSE
;
615 //---------------------------------------------------------------------------
617 //---------------------------------------------------------------------------
619 wxDropSource::wxDropSource( wxWindow
*win
, const wxIcon
&go
, const wxIcon
&stop
)
621 g_blockEventsOnDrag
= TRUE
;
625 m_widget
= win
->m_widget
;
626 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
628 m_retValue
= wxDragCancel
;
630 m_defaultCursor
= wxCursor( wxCURSOR_NO_ENTRY
);
631 m_goaheadCursor
= wxCursor( wxCURSOR_HAND
);
634 if (wxNullIcon
== go
) m_goIcon
= wxIcon( page_xpm
);
636 if (wxNullIcon
== stop
) m_stopIcon
= wxIcon( gv_xpm
);
639 wxDropSource::wxDropSource( wxDataObject
& data
, wxWindow
*win
,
640 const wxIcon
&go
, const wxIcon
&stop
)
647 m_widget
= win
->m_widget
;
648 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
649 m_retValue
= wxDragCancel
;
651 m_defaultCursor
= wxCursor( wxCURSOR_NO_ENTRY
);
652 m_goaheadCursor
= wxCursor( wxCURSOR_HAND
);
655 if (wxNullIcon
== go
) m_goIcon
= wxIcon( page_xpm
);
657 if (wxNullIcon
== stop
) m_stopIcon
= wxIcon( gv_xpm
);
660 wxDropSource::~wxDropSource()
662 g_blockEventsOnDrag
= FALSE
;
665 wxDragResult
wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove
) )
667 wxASSERT_MSG( m_data
, wxT("wxDragSource: no data") );
670 return (wxDragResult
) wxDragNone
;
672 if (m_data
->GetFormatCount() == 0)
673 return (wxDragResult
) wxDragNone
;
675 g_blockEventsOnDrag
= TRUE
;
681 GtkTargetList
*target_list
= gtk_target_list_new( (GtkTargetEntry
*) NULL
, 0 );
683 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
684 m_data
->GetAllFormats( array
);
685 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
687 GdkAtom atom
= array
[i
];
688 wxLogDebug( wxT("Supported atom %s"), gdk_atom_name( atom
) );
689 gtk_target_list_add( target_list
, atom
, 0, 0 );
693 GdkEventMotion event
;
694 event
.window
= m_widget
->window
;
697 GdkModifierType state
;
698 gdk_window_get_pointer( event
.window
, &x
, &y
, &state
);
702 event
.time
= GDK_CURRENT_TIME
;
704 /* GTK wants to know which button was pressed which caused the dragging */
705 int button_number
= 0;
706 if (event
.state
& GDK_BUTTON1_MASK
) button_number
= 1;
707 else if (event
.state
& GDK_BUTTON2_MASK
) button_number
= 2;
708 else if (event
.state
& GDK_BUTTON3_MASK
) button_number
= 3;
711 /* disable GUI threads */
712 wxapp_uninstall_thread_wakeup();
715 /* don't start dragging if no button is down */
718 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
721 button_number
, /* number of mouse button which started drag */
722 (GdkEvent
*) &event
);
724 wxMask
*mask
= m_goIcon
.GetMask();
725 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
726 if (mask
) bm
= mask
->GetBitmap();
727 GdkPixmap
*pm
= m_goIcon
.GetPixmap();
729 gtk_drag_set_icon_pixmap( context
,
730 gtk_widget_get_colormap( m_widget
),
736 while (m_waiting
) gtk_main_iteration();;
740 /* re-enable GUI threads */
741 wxapp_install_thread_wakeup();
744 g_blockEventsOnDrag
= FALSE
;
751 void wxDropSource::RegisterWindow()
753 if (!m_widget
) return;
755 gtk_signal_connect( GTK_OBJECT(m_widget
), "drag_data_get",
756 GTK_SIGNAL_FUNC (source_drag_data_get
), (gpointer
) this);
757 gtk_signal_connect (GTK_OBJECT(m_widget
), "drag_data_delete",
758 GTK_SIGNAL_FUNC (source_drag_data_delete
), (gpointer
) this );
759 gtk_signal_connect (GTK_OBJECT(m_widget
), "drag_begin",
760 GTK_SIGNAL_FUNC (source_drag_begin
), (gpointer
) this );
761 gtk_signal_connect (GTK_OBJECT(m_widget
), "drag_end",
762 GTK_SIGNAL_FUNC (source_drag_end
), (gpointer
) this );
766 void wxDropSource::UnregisterWindow()
768 if (!m_widget
) return;
770 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
771 GTK_SIGNAL_FUNC(source_drag_data_get
), (gpointer
) this );
772 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
773 GTK_SIGNAL_FUNC(source_drag_data_delete
), (gpointer
) this );
774 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
775 GTK_SIGNAL_FUNC(source_drag_begin
), (gpointer
) this );
776 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
777 GTK_SIGNAL_FUNC(source_drag_end
), (gpointer
) this );
782 // wxUSE_DRAG_AND_DROP