]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dnd.cpp
Fixed a crash when GetMonthControl returns NULL
[wxWidgets.git] / src / gtk / dnd.cpp
CommitLineData
c801d85f
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: dnd.cpp
3// Purpose: wxDropTarget class
4// Author: Robert Roebling
a81258be 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
f03fc89f 7// Licence: wxWindows licence
c801d85f
KB
8///////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
510fc784 11 #pragma implementation "dnd.h"
c801d85f
KB
12#endif
13
14#include "wx/dnd.h"
ac57418f 15
06cfab17 16#if wxUSE_DRAG_AND_DROP
ac57418f 17
c801d85f
KB
18#include "wx/window.h"
19#include "wx/app.h"
20#include "wx/gdicmn.h"
b527aac5
RR
21#include "wx/intl.h"
22#include "wx/utils.h"
c801d85f 23
9e691f46
VZ
24#include "wx/gtk/private.h"
25
071a2d78 26#include <gdk/gdkprivate.h>
c801d85f 27
071a2d78
RR
28#include <gtk/gtkdnd.h>
29#include <gtk/gtkselection.h>
c801d85f 30
5549fa65
RR
31//-----------------------------------------------------------------------------
32// idle system
33//-----------------------------------------------------------------------------
34
35extern void wxapp_install_idle_handler();
36extern bool g_isIdle;
37
b453e1b2
RR
38//-----------------------------------------------------------------------------
39// thread system
40//-----------------------------------------------------------------------------
41
42#if wxUSE_THREADS
b453e1b2
RR
43#endif
44
22d5903e
RR
45//----------------------------------------------------------------------------
46// global data
47//----------------------------------------------------------------------------
c801d85f
KB
48
49extern bool g_blockEventsOnDrag;
50
2245b2b2
VZ
51// the flags used for the last DoDragDrop()
52static long gs_flagsForDrag = 0;
53
b2dea2ab
VZ
54// the trace mask we use with wxLogTrace() - call
55// wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
56// (there are quite a few of them, so don't enable this by default)
57static const wxChar *TRACE_DND = _T("dnd");
58
22d5903e
RR
59//----------------------------------------------------------------------------
60// standard icons
61//----------------------------------------------------------------------------
62
22d5903e 63/* XPM */
90350682 64static const char * page_xpm[] = {
22d5903e
RR
65/* width height ncolors chars_per_pixel */
66"32 32 5 1",
67/* colors */
f03fc89f
VZ
68" s None c None",
69". c black",
70"X c wheat",
71"o c tan",
72"O c #6699FF",
22d5903e
RR
73/* pixels */
74" ................... ",
75" .XXXXXXXXXXXXXXXXX.. ",
76" .XXXXXXXXXXXXXXXXX.o. ",
77" .XXXXXXXXXXXXXXXXX.oo. ",
78" .XXXXXXXXXXXXXXXXX.ooo. ",
79" .XXXXXXXXXXXXXXXXX.oooo. ",
80" .XXXXXXXXXXXXXXXXX....... ",
81" .XXXXXOOOOOOOOOOXXXooooo. ",
82" .XXXXXXXXXXXXXXXXXXooooo. ",
83" .XXXXXOOOOOOOOOOXXXXXXXX. ",
84" .XXXXXXXXXXXXXXXXXXXXXXX. ",
85" .XXXXXXXOOOOOOOOOXXXXXXX. ",
86" .XXXXXXXXXXXXXXXXXXXXXXX. ",
87" .XXXXXXOOOOOOOOOOXXXXXXX. ",
88" .XXXXXXXXXXXXXXXXXXXXXXX. ",
89" .XXXXXOOOOOOOOOOXXXXXXXX. ",
90" .XXXXXXXXXXXXXXXXXXXXXXX. ",
91" .XXXXXXXOOOOOOOOOXXXXXXX. ",
92" .XXXXXXXXXXXXXXXXXXXXXXX. ",
93" .XXXXXXOOOOOOOOOOXXXXXXX. ",
94" .XXXXXXXXXXXXXXXXXXXXXXX. ",
95" .XXXXXOOOOOOOOOOXXXXXXXX. ",
96" .XXXXXXXXXXXXXXXXXXXXXXX. ",
97" .XXXXXXOOOOOOOOOOXXXXXXX. ",
98" .XXXXXXXXXXXXXXXXXXXXXXX. ",
99" .XXXXXOOOOOOOXXXXXXXXXXX. ",
100" .XXXXXXXXXXXXXXXXXXXXXXX. ",
101" .XXXXXXXXXXXXXXXXXXXXXXX. ",
102" .XXXXXXXXXXXXXXXXXXXXXXX. ",
103" .XXXXXXXXXXXXXXXXXXXXXXX. ",
104" .XXXXXXXXXXXXXXXXXXXXXXX. ",
105" ......................... "};
f03fc89f
VZ
106
107
2245b2b2
VZ
108// ============================================================================
109// private functions
110// ============================================================================
111
112// ----------------------------------------------------------------------------
113// convert between GTK+ and wxWindows DND constants
114// ----------------------------------------------------------------------------
115
116static wxDragResult ConvertFromGTK(long action)
117{
118 switch ( action )
119 {
120 case GDK_ACTION_COPY:
121 return wxDragCopy;
122
123 case GDK_ACTION_LINK:
124 return wxDragLink;
125
126 case GDK_ACTION_MOVE:
127 return wxDragMove;
128 }
129
130 return wxDragNone;
131}
4ba47b40 132
33a5bc52
RR
133// ----------------------------------------------------------------------------
134// "drag_leave"
135// ----------------------------------------------------------------------------
136
137static void target_drag_leave( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
138 GdkDragContext *context,
139 guint WXUNUSED(time),
140 wxDropTarget *drop_target )
33a5bc52 141{
5549fa65
RR
142 if (g_isIdle) wxapp_install_idle_handler();
143
829e3e8d
RR
144 /* inform the wxDropTarget about the current GdkDragContext.
145 this is only valid for the duration of this call */
146 drop_target->SetDragContext( context );
f03fc89f 147
829e3e8d
RR
148 /* we don't need return values. this event is just for
149 information */
150 drop_target->OnLeave();
f03fc89f 151
829e3e8d
RR
152 /* this has to be done because GDK has no "drag_enter" event */
153 drop_target->m_firstMotion = TRUE;
f03fc89f 154
829e3e8d
RR
155 /* after this, invalidate the drop_target's GdkDragContext */
156 drop_target->SetDragContext( (GdkDragContext*) NULL );
33a5bc52
RR
157}
158
159// ----------------------------------------------------------------------------
160// "drag_motion"
161// ----------------------------------------------------------------------------
162
163static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
164 GdkDragContext *context,
165 gint x,
166 gint y,
167 guint time,
168 wxDropTarget *drop_target )
33a5bc52 169{
5549fa65
RR
170 if (g_isIdle) wxapp_install_idle_handler();
171
829e3e8d
RR
172 /* Owen Taylor: "if the coordinates not in a drop zone,
173 return FALSE, otherwise call gtk_drag_status() and
174 return TRUE" */
f03fc89f 175
829e3e8d
RR
176 /* inform the wxDropTarget about the current GdkDragContext.
177 this is only valid for the duration of this call */
178 drop_target->SetDragContext( context );
f03fc89f 179
2245b2b2
VZ
180 // GTK+ always supposes that we want to copy the data by default while we
181 // might want to move it, so examine not only suggested_action - which is
182 // only good if we don't have our own preferences - but also the actions
183 // field
8ee9d618 184 wxDragResult result;
2245b2b2
VZ
185 if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove &&
186 (context->actions & GDK_ACTION_MOVE ) )
187 {
188 // move is requested by the program and allowed by GTK+ - do it, even
189 // though suggested_action may be currently wxDragCopy
8ee9d618 190 result = wxDragMove;
2245b2b2
VZ
191 }
192 else // use whatever GTK+ says we should
193 {
194 result = ConvertFromGTK(context->suggested_action);
195
196 if ( (result == wxDragMove) && !(gs_flagsForDrag & wxDrag_AllowMove) )
197 {
198 // we're requested to move but we can't
199 result = wxDragCopy;
200 }
201 }
8e00741d 202
829e3e8d 203 if (drop_target->m_firstMotion)
d6086ea6 204 {
829e3e8d 205 /* the first "drag_motion" event substitutes a "drag_enter" event */
c9057ae1 206 result = drop_target->OnEnter( x, y, result );
8e00741d
RR
207 }
208 else
209 {
210 /* give program a chance to react (i.e. to say no by returning FALSE) */
c9057ae1 211 result = drop_target->OnDragOver( x, y, result );
d6086ea6 212 }
f03fc89f 213
8ee9d618 214 bool ret = wxIsDragResultOk( result );
829e3e8d 215 if (ret)
bd77da97 216 {
8ee9d618
VZ
217 GdkDragAction action;
218 if (result == wxDragCopy)
219 action = GDK_ACTION_COPY;
b184227d
RD
220 else if (result == wxDragLink)
221 action = GDK_ACTION_LINK;
8ee9d618
VZ
222 else
223 action = GDK_ACTION_MOVE;
224
7b5d5699 225 gdk_drag_status( context, action, time );
bd77da97 226 }
f03fc89f 227
829e3e8d
RR
228 /* after this, invalidate the drop_target's GdkDragContext */
229 drop_target->SetDragContext( (GdkDragContext*) NULL );
f03fc89f 230
829e3e8d
RR
231 /* this has to be done because GDK has no "drag_enter" event */
232 drop_target->m_firstMotion = FALSE;
f03fc89f 233
829e3e8d 234 return ret;
33a5bc52
RR
235}
236
237// ----------------------------------------------------------------------------
238// "drag_drop"
239// ----------------------------------------------------------------------------
240
241static gboolean target_drag_drop( GtkWidget *widget,
f03fc89f
VZ
242 GdkDragContext *context,
243 gint x,
244 gint y,
245 guint time,
246 wxDropTarget *drop_target )
33a5bc52 247{
5549fa65
RR
248 if (g_isIdle) wxapp_install_idle_handler();
249
829e3e8d
RR
250 /* Owen Taylor: "if the drop is not in a drop zone,
251 return FALSE, otherwise, if you aren't accepting
252 the drop, call gtk_drag_finish() with success == FALSE
253 otherwise call gtk_drag_data_get()" */
4ba47b40
RR
254
255// printf( "drop.\n" );
f03fc89f 256
829e3e8d
RR
257 /* this seems to make a difference between not accepting
258 due to wrong target area and due to wrong format. let
259 us hope that this is not required.. */
f03fc89f 260
829e3e8d
RR
261 /* inform the wxDropTarget about the current GdkDragContext.
262 this is only valid for the duration of this call */
263 drop_target->SetDragContext( context );
f03fc89f 264
829e3e8d
RR
265 /* inform the wxDropTarget about the current drag widget.
266 this is only valid for the duration of this call */
267 drop_target->SetDragWidget( widget );
f03fc89f 268
829e3e8d
RR
269 /* inform the wxDropTarget about the current drag time.
270 this is only valid for the duration of this call */
271 drop_target->SetDragTime( time );
f03fc89f 272
bd77da97
RR
273/*
274 wxDragResult result = wxDragMove;
275 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
276*/
277
513abb88
RR
278 /* reset the block here as someone might very well
279 show a dialog as a reaction to a drop and this
280 wouldn't work without events */
281 g_blockEventsOnDrag = FALSE;
b2dea2ab 282
829e3e8d 283 bool ret = drop_target->OnDrop( x, y );
f03fc89f 284
5af019af 285 if (!ret)
829e3e8d 286 {
b2dea2ab 287 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
8ee9d618 288
829e3e8d
RR
289 /* cancel the whole thing */
290 gtk_drag_finish( context,
f03fc89f
VZ
291 FALSE, /* no success */
292 FALSE, /* don't delete data on dropping side */
293 time );
829e3e8d 294 }
8e00741d
RR
295 else
296 {
b2dea2ab 297 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned TRUE") );
8ee9d618 298
8e00741d
RR
299#if wxUSE_THREADS
300 /* disable GUI threads */
8e00741d
RR
301#endif
302
303 GdkAtom format = drop_target->GetMatchingPair();
2edc8f5b 304 wxASSERT( format );
8ee9d618 305
bd77da97
RR
306/*
307 GdkDragAction action = GDK_ACTION_MOVE;
8ee9d618
VZ
308 if (result == wxDragCopy) action == GDK_ACTION_COPY;
309 context->action = action;
bd77da97 310*/
8e00741d
RR
311 /* this should trigger an "drag_data_received" event */
312 gtk_drag_get_data( widget,
313 context,
314 format,
315 time );
316
317#if wxUSE_THREADS
318 /* re-enable GUI threads */
8e00741d
RR
319#endif
320 }
f03fc89f 321
829e3e8d
RR
322 /* after this, invalidate the drop_target's GdkDragContext */
323 drop_target->SetDragContext( (GdkDragContext*) NULL );
f03fc89f 324
829e3e8d
RR
325 /* after this, invalidate the drop_target's drag widget */
326 drop_target->SetDragWidget( (GtkWidget*) NULL );
f03fc89f 327
829e3e8d
RR
328 /* this has to be done because GDK has no "drag_enter" event */
329 drop_target->m_firstMotion = TRUE;
f03fc89f 330
829e3e8d 331 return ret;
33a5bc52
RR
332}
333
334// ----------------------------------------------------------------------------
335// "drag_data_received"
336// ----------------------------------------------------------------------------
337
338static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
339 GdkDragContext *context,
340 gint x,
341 gint y,
342 GtkSelectionData *data,
343 guint WXUNUSED(info),
344 guint time,
345 wxDropTarget *drop_target )
33a5bc52 346{
5549fa65
RR
347 if (g_isIdle) wxapp_install_idle_handler();
348
829e3e8d
RR
349 /* Owen Taylor: "call gtk_drag_finish() with
350 success == TRUE" */
351
829e3e8d 352 if ((data->length <= 0) || (data->format != 8))
33a5bc52 353 {
829e3e8d
RR
354 /* negative data length and non 8-bit data format
355 qualifies for junk */
356 gtk_drag_finish (context, FALSE, FALSE, time);
f03fc89f 357
f03fc89f 358 return;
829e3e8d 359 }
f03fc89f 360
b2dea2ab 361 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
8ee9d618 362
5af019af
RR
363 /* inform the wxDropTarget about the current GtkSelectionData.
364 this is only valid for the duration of this call */
365 drop_target->SetDragData( data );
f03fc89f 366
2245b2b2 367 wxDragResult result = ConvertFromGTK(context->suggested_action);
8ee9d618
VZ
368
369 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
829e3e8d 370 {
b2dea2ab 371 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned TRUE") );
8ee9d618 372
f03fc89f 373 /* tell GTK that data transfer was successfull */
ab8884ac 374 gtk_drag_finish( context, TRUE, FALSE, time );
33a5bc52 375 }
5af019af
RR
376 else
377 {
b2dea2ab 378 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
8ee9d618 379
f03fc89f 380 /* tell GTK that data transfer was not successfull */
5af019af
RR
381 gtk_drag_finish( context, FALSE, FALSE, time );
382 }
f03fc89f 383
5af019af
RR
384 /* after this, invalidate the drop_target's drag data */
385 drop_target->SetDragData( (GtkSelectionData*) NULL );
33a5bc52
RR
386}
387
4ba47b40 388//----------------------------------------------------------------------------
33a5bc52 389// wxDropTarget
4ba47b40 390//----------------------------------------------------------------------------
33a5bc52 391
8ee9d618
VZ
392wxDropTarget::wxDropTarget( wxDataObject *data )
393 : wxDropTargetBase( data )
f5368809 394{
829e3e8d
RR
395 m_firstMotion = TRUE;
396 m_dragContext = (GdkDragContext*) NULL;
397 m_dragWidget = (GtkWidget*) NULL;
5af019af 398 m_dragData = (GtkSelectionData*) NULL;
829e3e8d 399 m_dragTime = 0;
f5368809
RR
400}
401
c9057ae1
VZ
402wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
403 wxCoord WXUNUSED(y),
404 wxDragResult def )
d6086ea6 405{
8ee9d618 406 // GetMatchingPair() checks for m_dataObject too, no need to do it here
2edc8f5b
VZ
407
408 // disable the debug message from GetMatchingPair() - there are too many
409 // of them otherwise
c4fda16b 410#ifdef __WXDEBUG__
2edc8f5b
VZ
411 wxLogNull noLog;
412#endif // Debug
413
c9057ae1 414 return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
d6086ea6
RR
415}
416
c9057ae1 417bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
d6086ea6 418{
b068c4e8 419 if (!m_dataObject)
a3e7d24d 420 return FALSE;
8ee9d618 421
8e00741d 422 return (GetMatchingPair() != (GdkAtom) 0);
d6086ea6
RR
423}
424
8ee9d618
VZ
425wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
426 wxDragResult def )
5af019af 427{
b068c4e8 428 if (!m_dataObject)
11e1c70d 429 return wxDragNone;
8ee9d618 430
8e00741d 431 if (GetMatchingPair() == (GdkAtom) 0)
11e1c70d 432 return wxDragNone;
8ee9d618
VZ
433
434 return GetData() ? def : wxDragNone;
5af019af
RR
435}
436
8e00741d 437GdkAtom wxDropTarget::GetMatchingPair()
5af019af 438{
8ee9d618 439 if (!m_dataObject)
8e00741d 440 return (GdkAtom) 0;
5af019af 441
8ee9d618 442 if (!m_dragContext)
8e00741d 443 return (GdkAtom) 0;
f03fc89f 444
22d5903e
RR
445 GList *child = m_dragContext->targets;
446 while (child)
447 {
448 GdkAtom formatAtom = (GdkAtom) GPOINTER_TO_INT(child->data);
2edc8f5b 449 wxDataFormat format( formatAtom );
f03fc89f 450
a3e7d24d 451#ifdef __WXDEBUG__
b2dea2ab
VZ
452 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
453 format.GetId().c_str());
2edc8f5b
VZ
454#endif // Debug
455
b068c4e8 456 if (m_dataObject->IsSupportedFormat( format ))
2edc8f5b 457 return formatAtom;
f03fc89f 458
22d5903e
RR
459 child = child->next;
460 }
829e3e8d 461
8e00741d 462 return (GdkAtom) 0;
d6086ea6 463}
f03fc89f 464
8e00741d 465bool wxDropTarget::GetData()
d6086ea6 466{
8ee9d618 467 if (!m_dragData)
8e00741d 468 return FALSE;
f03fc89f 469
8ee9d618 470 if (!m_dataObject)
8e00741d 471 return FALSE;
f03fc89f 472
8e00741d 473 wxDataFormat dragFormat( m_dragData->target );
8ee9d618 474
b068c4e8 475 if (!m_dataObject->IsSupportedFormat( dragFormat ))
8e00741d 476 return FALSE;
f03fc89f 477
97c79de2
RR
478 m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
479
480 return TRUE;
d6086ea6 481}
f03fc89f 482
f5368809
RR
483void wxDropTarget::UnregisterWidget( GtkWidget *widget )
484{
223d09f6 485 wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
f03fc89f 486
829e3e8d 487 gtk_drag_dest_unset( widget );
f03fc89f 488
d6086ea6 489 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
f03fc89f 490 GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this );
33a5bc52 491
d6086ea6 492 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
f03fc89f 493 GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this );
33a5bc52 494
d6086ea6 495 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
f03fc89f 496 GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this );
33a5bc52 497
d6086ea6 498 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
f03fc89f 499 GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this );
f5368809
RR
500}
501
502void wxDropTarget::RegisterWidget( GtkWidget *widget )
503{
223d09f6 504 wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
f03fc89f 505
829e3e8d
RR
506 /* gtk_drag_dest_set() determines what default behaviour we'd like
507 GTK to supply. we don't want to specify out targets (=formats)
508 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
509 not GTK_DEST_DEFAULT_DROP). instead we react individually to
510 "drag_motion" and "drag_drop" events. this makes it possible
f03fc89f 511 to allow dropping on only a small area. we should set
829e3e8d
RR
512 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
513 highlighting if dragging over standard controls, but this
514 seems to be broken without the other two. */
f03fc89f 515
d6086ea6 516 gtk_drag_dest_set( widget,
f03fc89f
VZ
517 (GtkDestDefaults) 0, /* no default behaviour */
518 (GtkTargetEntry*) NULL, /* we don't supply any formats here */
519 0, /* number of targets = 0 */
520 (GdkDragAction) 0 ); /* we don't supply any actions here */
521
d6086ea6 522 gtk_signal_connect( GTK_OBJECT(widget), "drag_leave",
f03fc89f 523 GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this );
33a5bc52 524
d6086ea6 525 gtk_signal_connect( GTK_OBJECT(widget), "drag_motion",
f03fc89f 526 GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this );
33a5bc52 527
d6086ea6 528 gtk_signal_connect( GTK_OBJECT(widget), "drag_drop",
f03fc89f 529 GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this );
33a5bc52 530
d6086ea6 531 gtk_signal_connect( GTK_OBJECT(widget), "drag_data_received",
f03fc89f 532 GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this );
f5368809
RR
533}
534
4ba47b40
RR
535//----------------------------------------------------------------------------
536// "drag_data_get"
537//----------------------------------------------------------------------------
538
f03fc89f 539static void
4ba47b40 540source_drag_data_get (GtkWidget *WXUNUSED(widget),
b02da6b1 541 GdkDragContext *WXUNUSED(context),
f03fc89f
VZ
542 GtkSelectionData *selection_data,
543 guint WXUNUSED(info),
544 guint WXUNUSED(time),
545 wxDropSource *drop_source )
4ba47b40 546{
5549fa65
RR
547 if (g_isIdle) wxapp_install_idle_handler();
548
97c79de2 549 wxDataFormat format( selection_data->target );
8ee9d618 550
b2dea2ab
VZ
551 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
552 format.GetId().c_str());
f6bcfd97 553
1dd989e1 554 drop_source->m_retValue = wxDragCancel;
8ee9d618 555
97c79de2 556 wxDataObject *data = drop_source->GetDataObject();
f6bcfd97 557
1dd989e1 558 if (!data)
97c79de2 559 {
b2dea2ab 560 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
2edc8f5b 561 return;
97c79de2 562 }
1dd989e1 563
97c79de2
RR
564 if (!data->IsSupportedFormat(format))
565 {
b2dea2ab 566 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
2edc8f5b 567 return;
97c79de2 568 }
f03fc89f 569
97c79de2
RR
570 if (data->GetDataSize(format) == 0)
571 {
b2dea2ab 572 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
2edc8f5b 573 return;
97c79de2 574 }
8ee9d618 575
97c79de2 576 size_t size = data->GetDataSize(format);
f03fc89f 577
1dd989e1 578// printf( "data size: %d.\n", (int)data_size );
f03fc89f 579
1dd989e1 580 guchar *d = new guchar[size];
8ee9d618 581
97c79de2 582 if (!data->GetDataHere( format, (void*)d ))
1dd989e1 583 {
97c79de2 584 delete[] d;
2edc8f5b 585 return;
1dd989e1 586 }
f03fc89f 587
b453e1b2 588#if wxUSE_THREADS
1dd989e1 589 /* disable GUI threads */
b453e1b2 590#endif
8e193f38 591
4ba47b40 592 gtk_selection_data_set( selection_data,
f03fc89f
VZ
593 selection_data->target,
594 8, // 8-bit
1dd989e1
RR
595 d,
596 size );
f03fc89f 597
b453e1b2 598#if wxUSE_THREADS
1dd989e1 599 /* enable GUI threads */
b453e1b2 600#endif
f03fc89f 601
97c79de2 602 delete[] d;
4ba47b40 603}
f03fc89f 604
4ba47b40
RR
605//----------------------------------------------------------------------------
606// "drag_data_delete"
607//----------------------------------------------------------------------------
608
9f39393d
VZ
609static void source_drag_data_delete( GtkWidget *WXUNUSED(widget),
610 GdkDragContext *context,
611 wxDropSource *WXUNUSED(drop_source) )
4ba47b40 612{
f6bcfd97 613 if (g_isIdle)
13fb7b7a 614 wxapp_install_idle_handler();
33a5bc52 615
13fb7b7a 616 // printf( "Drag source: drag_data_delete\n" );
4ba47b40 617}
f03fc89f 618
4ba47b40
RR
619//----------------------------------------------------------------------------
620// "drag_begin"
621//----------------------------------------------------------------------------
622
623static void source_drag_begin( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
624 GdkDragContext *WXUNUSED(context),
625 wxDropSource *WXUNUSED(drop_source) )
4ba47b40 626{
f6bcfd97 627 if (g_isIdle)
13fb7b7a 628 wxapp_install_idle_handler();
5549fa65 629
13fb7b7a 630 // printf( "Drag source: drag_begin.\n" );
4ba47b40 631}
f03fc89f 632
4ba47b40
RR
633//----------------------------------------------------------------------------
634// "drag_end"
635//----------------------------------------------------------------------------
636
637static void source_drag_end( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
638 GdkDragContext *WXUNUSED(context),
639 wxDropSource *drop_source )
4ba47b40 640{
5549fa65
RR
641 if (g_isIdle) wxapp_install_idle_handler();
642
13fb7b7a 643 // printf( "Drag source: drag_end.\n" );
4ba47b40
RR
644
645 drop_source->m_waiting = FALSE;
646}
f03fc89f 647
7b5d5699
RR
648//-----------------------------------------------------------------------------
649// "configure_event" from m_iconWindow
650//-----------------------------------------------------------------------------
651
8ee9d618 652static gint
7b5d5699
RR
653gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
654{
8ee9d618 655 if (g_isIdle)
7b5d5699
RR
656 wxapp_install_idle_handler();
657
2245b2b2 658 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
8ee9d618 659
7b5d5699
RR
660 return 0;
661}
662
4ba47b40
RR
663//---------------------------------------------------------------------------
664// wxDropSource
665//---------------------------------------------------------------------------
22d5903e 666
f6bcfd97
BP
667wxDropSource::wxDropSource(wxWindow *win,
668 const wxIcon &iconCopy,
669 const wxIcon &iconMove,
670 const wxIcon &iconNone)
22d5903e 671{
4ba47b40 672 m_waiting = TRUE;
f03fc89f 673
7b5d5699 674 m_iconWindow = (GtkWidget*) NULL;
8ee9d618 675
22d5903e 676 m_window = win;
a2053b27
RR
677 m_widget = win->m_widget;
678 if (win->m_wxwindow) m_widget = win->m_wxwindow;
f03fc89f 679
22d5903e
RR
680 m_retValue = wxDragCancel;
681
f6bcfd97 682 SetIcons(iconCopy, iconMove, iconNone);
22d5903e
RR
683}
684
f6bcfd97
BP
685wxDropSource::wxDropSource(wxDataObject& data,
686 wxWindow *win,
687 const wxIcon &iconCopy,
688 const wxIcon &iconMove,
689 const wxIcon &iconNone)
22d5903e 690{
4ba47b40 691 m_waiting = TRUE;
8ee9d618 692
b068c4e8 693 SetData( data );
f03fc89f 694
7b5d5699 695 m_iconWindow = (GtkWidget*) NULL;
8ee9d618 696
22d5903e 697 m_window = win;
a2053b27
RR
698 m_widget = win->m_widget;
699 if (win->m_wxwindow) m_widget = win->m_wxwindow;
8ee9d618 700
22d5903e 701 m_retValue = wxDragCancel;
f03fc89f 702
f6bcfd97
BP
703 SetIcons(iconCopy, iconMove, iconNone);
704}
705
706void wxDropSource::SetIcons(const wxIcon &iconCopy,
707 const wxIcon &iconMove,
708 const wxIcon &iconNone)
709{
710 m_iconCopy = iconCopy;
711 m_iconMove = iconMove;
712 m_iconNone = iconNone;
713
714 if ( !m_iconCopy.Ok() )
715 m_iconCopy = wxIcon(page_xpm);
716 if ( !m_iconMove.Ok() )
717 m_iconMove = m_iconCopy;
718 if ( !m_iconNone.Ok() )
719 m_iconNone = m_iconCopy;
22d5903e
RR
720}
721
8e193f38 722wxDropSource::~wxDropSource()
22d5903e 723{
22d5903e 724}
f03fc89f 725
f6bcfd97 726void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
7b5d5699 727{
f6bcfd97
BP
728 // get the right icon to display
729 wxIcon *icon = NULL;
730 if ( action & GDK_ACTION_MOVE )
731 icon = &m_iconMove;
732 else if ( action & GDK_ACTION_COPY )
733 icon = &m_iconCopy;
734 else
735 icon = &m_iconNone;
736
737 GdkBitmap *mask;
738 if ( icon->GetMask() )
739 mask = icon->GetMask()->GetBitmap();
740 else
741 mask = (GdkBitmap *)NULL;
742
743 GdkPixmap *pixmap = icon->GetPixmap();
7b5d5699
RR
744
745 gint width,height;
746 gdk_window_get_size (pixmap, &width, &height);
747
748 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
9d084560 749#ifndef __WXGTK20__
7b5d5699 750 gtk_widget_push_visual (gdk_colormap_get_visual (colormap));
9d084560 751#endif
7b5d5699
RR
752 gtk_widget_push_colormap (colormap);
753
754 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
755 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
756 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE);
757
9d084560 758#ifndef __WXGTK20__
7b5d5699 759 gtk_widget_pop_visual ();
9d084560 760#endif
7b5d5699
RR
761 gtk_widget_pop_colormap ();
762
763 gtk_widget_set_usize (m_iconWindow, width, height);
764 gtk_widget_realize (m_iconWindow);
765
766 gtk_signal_connect( GTK_OBJECT(m_iconWindow), "configure_event",
767 GTK_SIGNAL_FUNC(gtk_dnd_window_configure_callback), (gpointer)this );
8ee9d618 768
7b5d5699 769 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
8ee9d618 770
7b5d5699
RR
771 if (mask)
772 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
773
f6bcfd97 774 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
7b5d5699
RR
775}
776
2245b2b2 777wxDragResult wxDropSource::DoDragDrop(int flags)
22d5903e 778{
2245b2b2
VZ
779 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
780 wxT("Drop source: no data") );
8ee9d618 781
513abb88
RR
782 // still in drag
783 if (g_blockEventsOnDrag)
2245b2b2 784 return wxDragNone;
b2dea2ab 785
f6bcfd97 786 // disabled for now
513abb88 787 g_blockEventsOnDrag = TRUE;
f03fc89f 788
4ba47b40 789 RegisterWindow();
f03fc89f 790
4ba47b40 791 m_waiting = TRUE;
22d5903e 792
4ba47b40 793 GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
8ee9d618 794
a3e7d24d 795 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
b068c4e8 796 m_data->GetAllFormats( array );
2245b2b2
VZ
797 size_t count = m_data->GetFormatCount();
798 for (size_t i = 0; i < count; i++)
a3e7d24d
RR
799 {
800 GdkAtom atom = array[i];
b2dea2ab 801 wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ));
a3e7d24d
RR
802 gtk_target_list_add( target_list, atom, 0, 0 );
803 }
804 delete[] array;
f03fc89f 805
4ba47b40
RR
806 GdkEventMotion event;
807 event.window = m_widget->window;
808 int x = 0;
809 int y = 0;
810 GdkModifierType state;
811 gdk_window_get_pointer( event.window, &x, &y, &state );
812 event.x = x;
813 event.y = y;
814 event.state = state;
b02da6b1 815 event.time = (guint32)GDK_CURRENT_TIME;
f03fc89f 816
4ba47b40
RR
817 /* GTK wants to know which button was pressed which caused the dragging */
818 int button_number = 0;
819 if (event.state & GDK_BUTTON1_MASK) button_number = 1;
820 else if (event.state & GDK_BUTTON2_MASK) button_number = 2;
821 else if (event.state & GDK_BUTTON3_MASK) button_number = 3;
822
b453e1b2
RR
823#if wxUSE_THREADS
824 /* disable GUI threads */
b453e1b2 825#endif
8e193f38 826
4ba47b40
RR
827 /* don't start dragging if no button is down */
828 if (button_number)
22d5903e 829 {
9f39393d 830 int action = GDK_ACTION_COPY;
2245b2b2 831 if ( flags & wxDrag_AllowMove )
9f39393d 832 action |= GDK_ACTION_MOVE;
2245b2b2
VZ
833
834 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
835 // to use a global to pass the flags to the drop target but I'd
836 // surely prefer a better way to do it
837 gs_flagsForDrag = flags;
838
4ba47b40 839 GdkDragContext *context = gtk_drag_begin( m_widget,
f6bcfd97 840 target_list,
46c87a9a 841 (GdkDragAction)action,
f6bcfd97
BP
842 button_number, /* number of mouse button which started drag */
843 (GdkEvent*) &event );
8ee9d618
VZ
844
845 m_dragContext = context;
846
f6bcfd97 847 PrepareIcon( action, context );
f03fc89f 848
2245b2b2
VZ
849 while (m_waiting)
850 gtk_main_iteration();
f6bcfd97 851
2245b2b2
VZ
852 m_retValue = ConvertFromGTK(context->action);
853 if ( m_retValue == wxDragNone )
854 m_retValue = wxDragCancel;
4ba47b40 855 }
22d5903e 856
b453e1b2
RR
857#if wxUSE_THREADS
858 /* re-enable GUI threads */
b453e1b2 859#endif
8e193f38 860
4ba47b40 861 g_blockEventsOnDrag = FALSE;
f03fc89f 862
4ba47b40
RR
863 UnregisterWindow();
864
865 return m_retValue;
22d5903e
RR
866}
867
4ba47b40 868void wxDropSource::RegisterWindow()
22d5903e 869{
4ba47b40 870 if (!m_widget) return;
f6bcfd97 871
4ba47b40 872 gtk_signal_connect( GTK_OBJECT(m_widget), "drag_data_get",
f03fc89f 873 GTK_SIGNAL_FUNC (source_drag_data_get), (gpointer) this);
4ba47b40 874 gtk_signal_connect (GTK_OBJECT(m_widget), "drag_data_delete",
f03fc89f 875 GTK_SIGNAL_FUNC (source_drag_data_delete), (gpointer) this );
4ba47b40 876 gtk_signal_connect (GTK_OBJECT(m_widget), "drag_begin",
f03fc89f 877 GTK_SIGNAL_FUNC (source_drag_begin), (gpointer) this );
4ba47b40 878 gtk_signal_connect (GTK_OBJECT(m_widget), "drag_end",
f03fc89f 879 GTK_SIGNAL_FUNC (source_drag_end), (gpointer) this );
4ba47b40 880
22d5903e
RR
881}
882
4ba47b40 883void wxDropSource::UnregisterWindow()
22d5903e
RR
884{
885 if (!m_widget) return;
f03fc89f 886
4ba47b40 887 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
f03fc89f 888 GTK_SIGNAL_FUNC(source_drag_data_get), (gpointer) this );
4ba47b40 889 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
f03fc89f 890 GTK_SIGNAL_FUNC(source_drag_data_delete), (gpointer) this );
4ba47b40 891 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
f03fc89f 892 GTK_SIGNAL_FUNC(source_drag_begin), (gpointer) this );
4ba47b40 893 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
f03fc89f 894 GTK_SIGNAL_FUNC(source_drag_end), (gpointer) this );
22d5903e
RR
895}
896
ac57418f 897#endif
93c5dd39 898 // wxUSE_DRAG_AND_DROP