]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dnd.cpp
Initial version
[wxWidgets.git] / src / gtk / dnd.cpp
CommitLineData
c801d85f
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: dnd.cpp
3// Purpose: wxDropTarget class
4// Author: Robert Roebling
5// Copyright: Robert Roebling
6// Licence: wxWindows license
7///////////////////////////////////////////////////////////////////////////////
8
9#ifdef __GNUG__
10#pragma implementation "dnd.h"
11#endif
12
13#include "wx/dnd.h"
14#include "wx/window.h"
15#include "wx/app.h"
16#include "wx/gdicmn.h"
1a5a8367 17#include <wx/intl.h>
c801d85f
KB
18
19#include "gdk/gdkprivate.h"
20
21#include <X11/Xlib.h>
22
23// ----------------------------------------------------------------------------
24// global
25// ----------------------------------------------------------------------------
26
27extern bool g_blockEventsOnDrag;
28
29// ----------------------------------------------------------------------------
30// wxDropTarget
31// ----------------------------------------------------------------------------
32
33wxDropTarget::wxDropTarget()
34{
35};
36
37wxDropTarget::~wxDropTarget()
38{
39};
40
41void wxDropTarget::Drop( GdkEvent *event, int x, int y )
42{
43 printf( "Drop data is of type %s.\n", event->dropdataavailable.data_type );
44
45 OnDrop( x, y, (char *)event->dropdataavailable.data);
46};
47
48void wxDropTarget::UnregisterWidget( GtkWidget *widget )
49{
e3e65dac
RR
50 if (!widget) return;
51
c801d85f
KB
52 gtk_widget_dnd_drop_set( widget, FALSE, NULL, 0, FALSE );
53};
54
e3e65dac
RR
55void wxDropTarget::RegisterWidget( GtkWidget *widget )
56{
57 wxString formats;
58 int valid = 0;
59
60 for ( uint i = 0; i < GetFormatCount(); i++ )
61 {
62 wxDataFormat df = GetFormat( i );
63 switch (df)
64 {
65 case wxDF_TEXT:
66 if (i > 0) formats += ";";
67 formats += "text/plain";
68 valid++;
69 break;
70 case wxDF_FILENAME:
71 if (i > 0) formats += ";";
72 formats += "url:any";
73 valid++;
74 break;
75 default:
76 break;
77 };
78 }
79
80 char *str = WXSTRINGCAST formats;
81
82 gtk_widget_dnd_drop_set( widget, TRUE, &str, valid, FALSE );
83};
84
c801d85f
KB
85// ----------------------------------------------------------------------------
86// wxTextDropTarget
87// ----------------------------------------------------------------------------
88
89bool wxTextDropTarget::OnDrop( long x, long y, const void *pData )
90{
91 OnDropText( x, y, (const char*)pData );
92 return TRUE;
93};
94
95bool wxTextDropTarget::OnDropText( long x, long y, const char *psz )
96{
97 printf( "Got dropped text: %s.\n", psz );
98 printf( "At x: %d, y: %d.\n", (int)x, (int)y );
99 return TRUE;
100};
101
e3e65dac 102size_t wxTextDropTarget::GetFormatCount() const
c801d85f 103{
e3e65dac
RR
104 return 1;
105}
106
107wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const
108{
109 return wxDF_TEXT;
110}
111
112// ----------------------------------------------------------------------------
113// wxFileDropTarget
114// ----------------------------------------------------------------------------
115
116bool wxFileDropTarget::OnDropFiles( long x, long y, size_t nFiles, const char * const WXUNUSED(aszFiles)[] )
117{
118 printf( "Got %d dropped files.\n", (int)nFiles );
119 printf( "At x: %d, y: %d.\n", (int)x, (int)y );
120 return TRUE;
121}
122
123bool wxFileDropTarget::OnDrop(long x, long y, const void *WXUNUSED(pData) )
124{
125 char *str = "/this/is/a/path.txt";
126
127 return OnDropFiles(x, y, 1, &str );
128}
129
130size_t wxFileDropTarget::GetFormatCount() const
131{
132 return 1;
133}
134
135wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const
136{
137 return wxDF_FILENAME;
138}
c801d85f
KB
139
140//-------------------------------------------------------------------------
e3e65dac 141// wxDropSource
c801d85f
KB
142//-------------------------------------------------------------------------
143
144//-----------------------------------------------------------------------------
145// drag request
146
b6af8d80 147void gtk_drag_callback( GtkWidget *widget, GdkEvent *event, wxDropSource *source )
c801d85f 148{
e3e65dac
RR
149 printf( "Data requested for dropping.\n" );
150
b6af8d80
RR
151 wxDataObject *data = source->m_data;
152
e3e65dac
RR
153 uint size = data->GetDataSize();
154 char *ptr = new char[size];
155 data->GetDataHere( ptr );
156
157 gtk_widget_dnd_data_set( widget, event, ptr, size );
c801d85f 158
e3e65dac 159 delete ptr;
b6af8d80
RR
160
161 source->m_retValue = wxDropSource::Copy;
c801d85f
KB
162};
163
e3e65dac 164wxDropSource::wxDropSource( wxWindow *win )
c801d85f
KB
165{
166 g_blockEventsOnDrag = TRUE;
e3e65dac 167
c801d85f
KB
168 m_window = win;
169 m_widget = win->m_widget;
170 if (win->m_wxwindow) m_widget = win->m_wxwindow;
c801d85f 171
b6af8d80
RR
172 m_data = NULL;
173 m_retValue = Cancel;
e3e65dac 174
c801d85f
KB
175 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
176 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
177};
178
e3e65dac 179wxDropSource::wxDropSource( wxDataObject &data, wxWindow *win )
c801d85f 180{
e3e65dac
RR
181 g_blockEventsOnDrag = TRUE;
182
183 m_window = win;
184 m_widget = win->m_widget;
185 if (win->m_wxwindow) m_widget = win->m_wxwindow;
b6af8d80 186 m_retValue = Cancel;
e3e65dac
RR
187
188 m_data = &data;
189
190 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
191 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
c801d85f 192};
e3e65dac
RR
193
194void wxDropSource::SetData( wxDataObject &data )
c801d85f 195{
e3e65dac 196 m_data = &data;
c801d85f
KB
197};
198
e3e65dac 199wxDropSource::~wxDropSource(void)
c801d85f 200{
e3e65dac
RR
201// if (m_data) delete m_data;
202
203 g_blockEventsOnDrag = FALSE;
204};
205
206wxDropSource::DragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) )
207{
208 if (gdk_dnd.dnd_grabbed) return None;
209 if (gdk_dnd.drag_really) return None;
210
b6af8d80
RR
211 wxASSERT_MSG( data, "wxDragSource: no data" );
212
e3e65dac
RR
213 if (!m_data) return None;
214 if (m_data->GetDataSize() == 0) return None;
c801d85f
KB
215
216 GdkWindowPrivate *wp = (GdkWindowPrivate*) m_widget->window;
217
218 RegisterWindow();
c801d85f
KB
219
220 gdk_dnd.drag_perhaps = TRUE;
221
222 gdk_dnd.dnd_drag_start.x = 5;
223 gdk_dnd.dnd_drag_start.y = 5;
224 gdk_dnd.real_sw = wp;
225
226 if (gdk_dnd.drag_startwindows)
227 {
228 g_free( gdk_dnd.drag_startwindows );
229 gdk_dnd.drag_startwindows = NULL;
230 };
231 gdk_dnd.drag_numwindows = gdk_dnd.drag_really = 0;
232
233 XWindowAttributes dnd_winattr;
234 XGetWindowAttributes( gdk_display, wp->xwindow, &dnd_winattr );
235 wp->dnd_drag_savedeventmask = dnd_winattr.your_event_mask;
236
237 gdk_dnd_drag_addwindow( m_widget->window );
238
239 GdkEventDragBegin ev;
240 ev.type = GDK_DRAG_BEGIN;
241 ev.window = m_widget->window;
242 ev.u.allflags = 0;
243 ev.u.flags.protocol_version = DND_PROTOCOL_VERSION;
244
245 gdk_event_put( (GdkEvent*)&ev );
246
247 XGrabPointer( gdk_display, wp->xwindow, False,
248 ButtonMotionMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
249 GrabModeAsync, GrabModeAsync, gdk_root_window, None, CurrentTime );
250
251 gdk_dnd_set_drag_cursors( m_defaultCursor.GetCursor(), m_goaheadCursor.GetCursor() );
252
253 gdk_dnd.dnd_grabbed = TRUE;
254 gdk_dnd.drag_really = 1;
e3e65dac
RR
255
256 int x = 0;
257 int y = 0;
258 gdk_window_get_pointer( m_widget->window, &x, &y, NULL );
259
c801d85f
KB
260 gdk_dnd_display_drag_cursor( x, y, FALSE, TRUE );
261
262 while (gdk_dnd.drag_really || gdk_dnd.drag_perhaps) wxYield();
263
c801d85f 264 UnregisterWindow();
e3e65dac 265
b6af8d80 266 return m_retValue;
c801d85f
KB
267};
268
e3e65dac 269void wxDropSource::RegisterWindow(void)
c801d85f 270{
e3e65dac 271 if (!m_data) return;
c801d85f 272
e3e65dac
RR
273 wxString formats;
274
275 wxDataFormat df = m_data->GetPreferredFormat();
c801d85f 276
e3e65dac
RR
277 switch (df)
278 {
279 case wxDF_TEXT:
280 formats += "text/plain";
281 break;
282 case wxDF_FILENAME:
283 formats += "url:any";
284 break;
285 default:
286 break;
287 }
c801d85f 288
e3e65dac 289 char *str = WXSTRINGCAST formats;
c801d85f 290
e3e65dac 291 gtk_widget_dnd_drag_set( m_widget, TRUE, &str, 1 );
c801d85f 292
e3e65dac 293 gtk_signal_connect( GTK_OBJECT(m_widget), "drag_request_event",
b6af8d80 294 GTK_SIGNAL_FUNC(gtk_drag_callback), (gpointer)this );
c801d85f
KB
295};
296
e3e65dac 297void wxDropSource::UnregisterWindow(void)
c801d85f
KB
298{
299 if (!m_widget) return;
300
e3e65dac
RR
301 gtk_widget_dnd_drag_set( m_widget, FALSE, NULL, 0 );
302
b6af8d80 303 gtk_signal_disconnect_by_data( GTK_OBJECT(m_widget), (gpointer)this );
c801d85f 304};