]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dnd.cpp
Added USE_CONFIG
[wxWidgets.git] / src / gtk1 / 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{
ff7b1510 35}
c801d85f
KB
36
37wxDropTarget::~wxDropTarget()
38{
ff7b1510 39}
c801d85f
KB
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);
ff7b1510 46}
c801d85f
KB
47
48void wxDropTarget::UnregisterWidget( GtkWidget *widget )
49{
e3e65dac
RR
50 if (!widget) return;
51
c67daf87 52 gtk_widget_dnd_drop_set( widget, FALSE, (gchar **) NULL, 0, FALSE );
ff7b1510 53}
c801d85f 54
e3e65dac
RR
55void wxDropTarget::RegisterWidget( GtkWidget *widget )
56{
57 wxString formats;
58 int valid = 0;
59
c86f1403 60 for ( size_t i = 0; i < GetFormatCount(); i++ )
e3e65dac
RR
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;
ff7b1510 77 }
e3e65dac
RR
78 }
79
80 char *str = WXSTRINGCAST formats;
81
82 gtk_widget_dnd_drop_set( widget, TRUE, &str, valid, FALSE );
ff7b1510 83}
e3e65dac 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;
ff7b1510 93}
c801d85f
KB
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;
ff7b1510 100}
c801d85f 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
c86f1403 153 size_t size = data->GetDataSize();
e3e65dac
RR
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;
ff7b1510 162}
c801d85f 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
c67daf87 172 m_data = (wxDataObject *) NULL;
b6af8d80 173 m_retValue = Cancel;
e3e65dac 174
c801d85f
KB
175 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
176 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
ff7b1510 177}
c801d85f 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 );
ff7b1510 192}
e3e65dac
RR
193
194void wxDropSource::SetData( wxDataObject &data )
c801d85f 195{
e3e65dac 196 m_data = &data;
ff7b1510 197}
c801d85f 198
e3e65dac 199wxDropSource::~wxDropSource(void)
c801d85f 200{
e3e65dac
RR
201// if (m_data) delete m_data;
202
203 g_blockEventsOnDrag = FALSE;
ff7b1510 204}
e3e65dac
RR
205
206wxDropSource::DragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) )
207{
c67daf87
UR
208 if (gdk_dnd.dnd_grabbed) return (wxDropSource::DragResult) None;
209 if (gdk_dnd.drag_really) return (wxDropSource::DragResult) None;
e3e65dac 210
f6d53978 211 wxASSERT_MSG( m_data, "wxDragSource: no data" );
b6af8d80 212
c67daf87
UR
213 if (!m_data) return (wxDropSource::DragResult) None;
214 if (m_data->GetDataSize() == 0) return (wxDropSource::DragResult) 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 );
c67daf87 229 gdk_dnd.drag_startwindows = (GdkWindow **) NULL;
ff7b1510 230 }
c801d85f
KB
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;
c67daf87 258 gdk_window_get_pointer( m_widget->window, &x, &y, (GdkModifierType *) NULL );
e3e65dac 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
30dea054
RR
266 g_blockEventsOnDrag = FALSE;
267
b6af8d80 268 return m_retValue;
ff7b1510 269}
c801d85f 270
e3e65dac 271void wxDropSource::RegisterWindow(void)
c801d85f 272{
e3e65dac 273 if (!m_data) return;
c801d85f 274
e3e65dac
RR
275 wxString formats;
276
277 wxDataFormat df = m_data->GetPreferredFormat();
c801d85f 278
e3e65dac
RR
279 switch (df)
280 {
281 case wxDF_TEXT:
282 formats += "text/plain";
283 break;
284 case wxDF_FILENAME:
285 formats += "url:any";
286 break;
287 default:
288 break;
289 }
c801d85f 290
e3e65dac 291 char *str = WXSTRINGCAST formats;
c801d85f 292
e3e65dac 293 gtk_widget_dnd_drag_set( m_widget, TRUE, &str, 1 );
c801d85f 294
e3e65dac 295 gtk_signal_connect( GTK_OBJECT(m_widget), "drag_request_event",
b6af8d80 296 GTK_SIGNAL_FUNC(gtk_drag_callback), (gpointer)this );
ff7b1510 297}
c801d85f 298
e3e65dac 299void wxDropSource::UnregisterWindow(void)
c801d85f
KB
300{
301 if (!m_widget) return;
302
c67daf87 303 gtk_widget_dnd_drag_set( m_widget, FALSE, (gchar **) NULL, 0 );
e3e65dac 304
b6af8d80 305 gtk_signal_disconnect_by_data( GTK_OBJECT(m_widget), (gpointer)this );
ff7b1510 306}