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