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