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
)
354 m_firstMotion
= TRUE
;
355 m_dragContext
= (GdkDragContext
*) NULL
;
356 m_dragWidget
= (GtkWidget
*) NULL
;
357 m_dragData
= (GtkSelectionData
*) NULL
;
362 wxDropTarget::~wxDropTarget()
366 bool wxDropTarget::OnEnter( int WXUNUSED(x
), int WXUNUSED(y
) )
371 return (GetMatchingPair() != (GdkAtom
) 0);
374 void wxDropTarget::OnLeave()
378 bool wxDropTarget::OnMove( int WXUNUSED(x
), int WXUNUSED(y
) )
383 return (GetMatchingPair() != (GdkAtom
) 0);
386 bool wxDropTarget::OnDrop( int WXUNUSED(x
), int WXUNUSED(y
) )
391 return (GetMatchingPair() != (GdkAtom
) 0);
394 bool wxDropTarget::OnData( int WXUNUSED(x
), int WXUNUSED(y
) )
399 if (GetMatchingPair() == (GdkAtom
) 0)
405 GdkAtom
wxDropTarget::GetMatchingPair()
413 GList
*child
= m_dragContext
->targets
;
416 GdkAtom formatAtom
= (GdkAtom
) GPOINTER_TO_INT(child
->data
);
417 wxDataFormat
format( formatAtom
);
420 char *name
= gdk_atom_name( formatAtom
);
421 if (name
) wxLogDebug( "Drop target: drag has format: %s", name
);
423 if (m_data
->IsSupportedFormat( format
))
432 bool wxDropTarget::GetData()
440 wxDataFormat
dragFormat( m_dragData
->target
);
442 if (!m_data
->IsSupportedFormat( dragFormat
))
445 if (dragFormat
.GetType() == wxDF_TEXT
)
447 wxTextDataObject
*text_object
= (wxTextDataObject
*)m_data
;
448 text_object
->SetText( (const char*)m_dragData
->data
);
452 if (dragFormat
.GetType() == wxDF_FILENAME
)
454 wxFileDataObject
*file_object
= (wxFileDataObject
*)m_data
;
455 file_object
->SetFiles( (const char*)m_dragData
->data
);
462 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
464 wxCHECK_RET( widget
!= NULL
, wxT("unregister widget is NULL") );
466 gtk_drag_dest_unset( widget
);
468 gtk_signal_disconnect_by_func( GTK_OBJECT(widget
),
469 GTK_SIGNAL_FUNC(target_drag_leave
), (gpointer
) this );
471 gtk_signal_disconnect_by_func( GTK_OBJECT(widget
),
472 GTK_SIGNAL_FUNC(target_drag_motion
), (gpointer
) this );
474 gtk_signal_disconnect_by_func( GTK_OBJECT(widget
),
475 GTK_SIGNAL_FUNC(target_drag_drop
), (gpointer
) this );
477 gtk_signal_disconnect_by_func( GTK_OBJECT(widget
),
478 GTK_SIGNAL_FUNC(target_drag_data_received
), (gpointer
) this );
481 void wxDropTarget::RegisterWidget( GtkWidget
*widget
)
483 wxCHECK_RET( widget
!= NULL
, wxT("register widget is NULL") );
485 /* gtk_drag_dest_set() determines what default behaviour we'd like
486 GTK to supply. we don't want to specify out targets (=formats)
487 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
488 not GTK_DEST_DEFAULT_DROP). instead we react individually to
489 "drag_motion" and "drag_drop" events. this makes it possible
490 to allow dropping on only a small area. we should set
491 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
492 highlighting if dragging over standard controls, but this
493 seems to be broken without the other two. */
495 gtk_drag_dest_set( widget
,
496 (GtkDestDefaults
) 0, /* no default behaviour */
497 (GtkTargetEntry
*) NULL
, /* we don't supply any formats here */
498 0, /* number of targets = 0 */
499 (GdkDragAction
) 0 ); /* we don't supply any actions here */
501 gtk_signal_connect( GTK_OBJECT(widget
), "drag_leave",
502 GTK_SIGNAL_FUNC(target_drag_leave
), (gpointer
) this );
504 gtk_signal_connect( GTK_OBJECT(widget
), "drag_motion",
505 GTK_SIGNAL_FUNC(target_drag_motion
), (gpointer
) this );
507 gtk_signal_connect( GTK_OBJECT(widget
), "drag_drop",
508 GTK_SIGNAL_FUNC(target_drag_drop
), (gpointer
) this );
510 gtk_signal_connect( GTK_OBJECT(widget
), "drag_data_received",
511 GTK_SIGNAL_FUNC(target_drag_data_received
), (gpointer
) this );
514 //----------------------------------------------------------------------------
516 //----------------------------------------------------------------------------
519 source_drag_data_get (GtkWidget
*WXUNUSED(widget
),
520 GdkDragContext
*context
,
521 GtkSelectionData
*selection_data
,
522 guint
WXUNUSED(info
),
523 guint
WXUNUSED(time
),
524 wxDropSource
*drop_source
)
526 if (g_isIdle
) wxapp_install_idle_handler();
529 char *name
= gdk_atom_name( selection_data
->target
);
530 if (name
) wxLogDebug( wxT("Drop source: format requested: %s"), name
);
533 drop_source
->m_retValue
= wxDragCancel
;
535 wxDataObject
*data
= drop_source
->m_data
;
540 if (!data
->IsSupportedFormat(selection_data
->target
))
543 if (data
->GetDataSize(selection_data
->target
) == 0)
546 size_t size
= data
->GetDataSize(selection_data
->target
);
548 // printf( "data size: %d.\n", (int)data_size );
550 guchar
*d
= new guchar
[size
];
552 if (!data
->GetDataHere( selection_data
->target
, (void*)d
))
559 /* disable GUI threads */
560 wxapp_uninstall_thread_wakeup();
563 gtk_selection_data_set( selection_data
,
564 selection_data
->target
,
570 /* enable GUI threads */
571 wxapp_install_thread_wakeup();
576 /* so far only copy, no moves. TODO. */
577 drop_source
->m_retValue
= wxDragCopy
;
580 //----------------------------------------------------------------------------
581 // "drag_data_delete"
582 //----------------------------------------------------------------------------
584 static void source_drag_data_delete( GtkWidget
*WXUNUSED(widget
),
585 GdkDragContext
*WXUNUSED(context
),
586 wxDropSource
*drop_source
)
588 if (g_isIdle
) wxapp_install_idle_handler();
590 // printf( "Delete the data!\n" );
592 drop_source
->m_retValue
= wxDragMove
;
595 //----------------------------------------------------------------------------
597 //----------------------------------------------------------------------------
599 static void source_drag_begin( GtkWidget
*WXUNUSED(widget
),
600 GdkDragContext
*WXUNUSED(context
),
601 wxDropSource
*WXUNUSED(drop_source
) )
603 if (g_isIdle
) wxapp_install_idle_handler();
605 // printf( "drag_begin.\n" );
608 //----------------------------------------------------------------------------
610 //----------------------------------------------------------------------------
612 static void source_drag_end( GtkWidget
*WXUNUSED(widget
),
613 GdkDragContext
*WXUNUSED(context
),
614 wxDropSource
*drop_source
)
616 if (g_isIdle
) wxapp_install_idle_handler();
618 // printf( "drag_end.\n" );
620 drop_source
->m_waiting
= FALSE
;
623 //---------------------------------------------------------------------------
625 //---------------------------------------------------------------------------
627 wxDropSource::wxDropSource( wxWindow
*win
, const wxIcon
&go
, const wxIcon
&stop
)
629 g_blockEventsOnDrag
= TRUE
;
633 m_widget
= win
->m_widget
;
634 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
636 m_data
= (wxDataObject
*) NULL
;
637 m_retValue
= wxDragCancel
;
639 m_defaultCursor
= wxCursor( wxCURSOR_NO_ENTRY
);
640 m_goaheadCursor
= wxCursor( wxCURSOR_HAND
);
643 if (wxNullIcon
== go
) m_goIcon
= wxIcon( page_xpm
);
645 if (wxNullIcon
== stop
) m_stopIcon
= wxIcon( gv_xpm
);
648 wxDropSource::wxDropSource( wxDataObject
& data
, wxWindow
*win
,
649 const wxIcon
&go
, const wxIcon
&stop
)
654 m_widget
= win
->m_widget
;
655 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
656 m_retValue
= wxDragCancel
;
660 m_defaultCursor
= wxCursor( wxCURSOR_NO_ENTRY
);
661 m_goaheadCursor
= wxCursor( wxCURSOR_HAND
);
664 if (wxNullIcon
== go
) m_goIcon
= wxIcon( page_xpm
);
666 if (wxNullIcon
== stop
) m_stopIcon
= wxIcon( gv_xpm
);
669 void wxDropSource::SetData( wxDataObject
& data
)
677 wxDropSource::~wxDropSource()
682 g_blockEventsOnDrag
= FALSE
;
685 wxDragResult
wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove
) )
687 wxASSERT_MSG( m_data
, wxT("wxDragSource: no data") );
690 return (wxDragResult
) wxDragNone
;
692 if (m_data
->GetFormatCount() == 0)
693 return (wxDragResult
) wxDragNone
;
695 g_blockEventsOnDrag
= TRUE
;
701 GtkTargetList
*target_list
= gtk_target_list_new( (GtkTargetEntry
*) NULL
, 0 );
703 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
704 m_data
->GetAllFormats( array
, TRUE
);
705 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
707 GdkAtom atom
= array
[i
];
708 wxLogDebug( wxT("Supported atom %s"), gdk_atom_name( atom
) );
709 gtk_target_list_add( target_list
, atom
, 0, 0 );
713 GdkEventMotion event
;
714 event
.window
= m_widget
->window
;
717 GdkModifierType state
;
718 gdk_window_get_pointer( event
.window
, &x
, &y
, &state
);
722 event
.time
= GDK_CURRENT_TIME
;
724 /* GTK wants to know which button was pressed which caused the dragging */
725 int button_number
= 0;
726 if (event
.state
& GDK_BUTTON1_MASK
) button_number
= 1;
727 else if (event
.state
& GDK_BUTTON2_MASK
) button_number
= 2;
728 else if (event
.state
& GDK_BUTTON3_MASK
) button_number
= 3;
731 /* disable GUI threads */
732 wxapp_uninstall_thread_wakeup();
735 /* don't start dragging if no button is down */
738 GdkDragContext
*context
= gtk_drag_begin( m_widget
,
741 button_number
, /* number of mouse button which started drag */
742 (GdkEvent
*) &event
);
744 wxMask
*mask
= m_goIcon
.GetMask();
745 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
746 if (mask
) bm
= mask
->GetBitmap();
747 GdkPixmap
*pm
= m_goIcon
.GetPixmap();
749 gtk_drag_set_icon_pixmap( context
,
750 gtk_widget_get_colormap( m_widget
),
756 while (m_waiting
) gtk_main_iteration();;
760 /* re-enable GUI threads */
761 wxapp_install_thread_wakeup();
764 g_blockEventsOnDrag
= FALSE
;
771 void wxDropSource::RegisterWindow()
773 if (!m_widget
) return;
775 gtk_signal_connect( GTK_OBJECT(m_widget
), "drag_data_get",
776 GTK_SIGNAL_FUNC (source_drag_data_get
), (gpointer
) this);
777 gtk_signal_connect (GTK_OBJECT(m_widget
), "drag_data_delete",
778 GTK_SIGNAL_FUNC (source_drag_data_delete
), (gpointer
) this );
779 gtk_signal_connect (GTK_OBJECT(m_widget
), "drag_begin",
780 GTK_SIGNAL_FUNC (source_drag_begin
), (gpointer
) this );
781 gtk_signal_connect (GTK_OBJECT(m_widget
), "drag_end",
782 GTK_SIGNAL_FUNC (source_drag_end
), (gpointer
) this );
786 void wxDropSource::UnregisterWindow()
788 if (!m_widget
) return;
790 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
791 GTK_SIGNAL_FUNC(source_drag_data_get
), (gpointer
) this );
792 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
793 GTK_SIGNAL_FUNC(source_drag_data_delete
), (gpointer
) this );
794 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
795 GTK_SIGNAL_FUNC(source_drag_begin
), (gpointer
) this );
796 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
797 GTK_SIGNAL_FUNC(source_drag_end
), (gpointer
) this );
802 // wxUSE_DRAG_AND_DROP