1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDropTarget class
4 // Author: Robert Roebling
5 // Copyright: Robert Roebling
6 // Licence: wxWindows license
7 ///////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "dnd.h"
14 #include "wx/window.h"
16 #include "wx/gdicmn.h"
18 #include "gdk/gdkprivate.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 extern bool g_blockEventsOnDrag
;
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 wxDropTarget::wxDropTarget()
36 wxDropTarget::~wxDropTarget()
40 void wxDropTarget::Drop( GdkEvent
*event
, int x
, int y
)
42 printf( "Drop data is of type %s.\n", event
->dropdataavailable
.data_type
);
44 OnDrop( x
, y
, (char *)event
->dropdataavailable
.data
);
47 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
49 gtk_widget_dnd_drop_set( widget
, FALSE
, NULL
, 0, FALSE
);
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 bool wxTextDropTarget::OnDrop( long x
, long y
, const void *pData
)
58 OnDropText( x
, y
, (const char*)pData
);
62 bool wxTextDropTarget::OnDropText( long x
, long y
, const char *psz
)
64 printf( "Got dropped text: %s.\n", psz
);
65 printf( "At x: %d, y: %d.\n", (int)x
, (int)y
);
69 void wxTextDropTarget::RegisterWidget( GtkWidget
*widget
)
71 char *accepted_drop_types
[] = { "text/plain" };
72 gtk_widget_dnd_drop_set( widget
, TRUE
, accepted_drop_types
, 1, FALSE
);
75 //-------------------------------------------------------------------------
77 //-------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
82 void gtk_drag_callback( GtkWidget
*widget
, GdkEvent
*event
, wxDragSource
*drag
)
84 printf( "OnDragRequest.\n" );
86 gtk_widget_dnd_data_set( widget
, event
, drag
->m_data
, drag
->m_size
);
89 wxDragSource::wxDragSource( wxWindow
*win
)
91 g_blockEventsOnDrag
= TRUE
;
94 m_widget
= win
->m_widget
;
95 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
100 m_defaultCursor
= wxCursor( wxCURSOR_NO_ENTRY
);
101 m_goaheadCursor
= wxCursor( wxCURSOR_HAND
);
104 wxDragSource::~wxDragSource(void)
106 g_blockEventsOnDrag
= FALSE
;
109 void wxDragSource::SetData( char *data
, const long size
)
115 void wxDragSource::Start( int x
, int y
)
117 if (gdk_dnd
.dnd_grabbed
) return;
118 if (gdk_dnd
.drag_really
) return;
119 if (m_size
== 0) return;
122 GdkWindowPrivate
*wp
= (GdkWindowPrivate
*) m_widget
->window
;
127 gdk_dnd
.drag_perhaps
= TRUE
;
129 gdk_dnd
.dnd_drag_start
.x
= 5;
130 gdk_dnd
.dnd_drag_start
.y
= 5;
131 gdk_dnd
.real_sw
= wp
;
133 if (gdk_dnd
.drag_startwindows
)
135 g_free( gdk_dnd
.drag_startwindows
);
136 gdk_dnd
.drag_startwindows
= NULL
;
138 gdk_dnd
.drag_numwindows
= gdk_dnd
.drag_really
= 0;
140 XWindowAttributes dnd_winattr
;
141 XGetWindowAttributes( gdk_display
, wp
->xwindow
, &dnd_winattr
);
142 wp
->dnd_drag_savedeventmask
= dnd_winattr
.your_event_mask
;
144 gdk_dnd_drag_addwindow( m_widget
->window
);
146 GdkEventDragBegin ev
;
147 ev
.type
= GDK_DRAG_BEGIN
;
148 ev
.window
= m_widget
->window
;
150 ev
.u
.flags
.protocol_version
= DND_PROTOCOL_VERSION
;
152 gdk_event_put( (GdkEvent
*)&ev
);
154 XGrabPointer( gdk_display
, wp
->xwindow
, False
,
155 ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
,
156 GrabModeAsync
, GrabModeAsync
, gdk_root_window
, None
, CurrentTime
);
158 gdk_dnd_set_drag_cursors( m_defaultCursor
.GetCursor(), m_goaheadCursor
.GetCursor() );
160 gdk_dnd
.dnd_grabbed
= TRUE
;
161 gdk_dnd
.drag_really
= 1;
162 gdk_dnd_display_drag_cursor( x
, y
, FALSE
, TRUE
);
164 while (gdk_dnd
.drag_really
|| gdk_dnd
.drag_perhaps
) wxYield();
170 void wxDragSource::ConnectWindow(void)
172 gtk_signal_connect( GTK_OBJECT(m_widget
), "drag_request_event",
173 GTK_SIGNAL_FUNC(gtk_drag_callback
), (gpointer
)this );
176 void wxDragSource::UnconnectWindow(void)
178 if (!m_widget
) return;
180 gtk_signal_disconnect_by_data( GTK_OBJECT(m_widget
), (gpointer
)this );
183 void wxDragSource::UnregisterWindow(void)
185 if (!m_widget
) return;
187 gtk_widget_dnd_drag_set( m_widget
, FALSE
, NULL
, 0 );
190 //-------------------------------------------------------------------------
192 //-------------------------------------------------------------------------
194 void wxTextDragSource::SetTextData( const wxString
&text
)
197 SetData( WXSTRINGCAST(m_tmp
), m_tmp
.Length()+1 );
200 void wxTextDragSource::RegisterWindow(void)
202 if (!m_widget
) return;
204 char *accepted_drop_types
[] = { "text/plain" };
205 gtk_widget_dnd_drag_set( m_widget
, TRUE
, accepted_drop_types
, 1 );