]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dnd.cpp
Another typo fixed.
[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();
b72aa48c
VZ
304
305 // this does happen somehow, see bug 555111
306 wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") )
8ee9d618 307
bd77da97
RR
308/*
309 GdkDragAction action = GDK_ACTION_MOVE;
8ee9d618
VZ
310 if (result == wxDragCopy) action == GDK_ACTION_COPY;
311 context->action = action;
bd77da97 312*/
8e00741d
RR
313 /* this should trigger an "drag_data_received" event */
314 gtk_drag_get_data( widget,
315 context,
316 format,
317 time );
318
319#if wxUSE_THREADS
320 /* re-enable GUI threads */
8e00741d
RR
321#endif
322 }
f03fc89f 323
829e3e8d
RR
324 /* after this, invalidate the drop_target's GdkDragContext */
325 drop_target->SetDragContext( (GdkDragContext*) NULL );
f03fc89f 326
829e3e8d
RR
327 /* after this, invalidate the drop_target's drag widget */
328 drop_target->SetDragWidget( (GtkWidget*) NULL );
f03fc89f 329
829e3e8d
RR
330 /* this has to be done because GDK has no "drag_enter" event */
331 drop_target->m_firstMotion = TRUE;
f03fc89f 332
829e3e8d 333 return ret;
33a5bc52
RR
334}
335
336// ----------------------------------------------------------------------------
337// "drag_data_received"
338// ----------------------------------------------------------------------------
339
340static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
341 GdkDragContext *context,
342 gint x,
343 gint y,
344 GtkSelectionData *data,
345 guint WXUNUSED(info),
346 guint time,
347 wxDropTarget *drop_target )
33a5bc52 348{
5549fa65
RR
349 if (g_isIdle) wxapp_install_idle_handler();
350
829e3e8d
RR
351 /* Owen Taylor: "call gtk_drag_finish() with
352 success == TRUE" */
353
829e3e8d 354 if ((data->length <= 0) || (data->format != 8))
33a5bc52 355 {
829e3e8d
RR
356 /* negative data length and non 8-bit data format
357 qualifies for junk */
358 gtk_drag_finish (context, FALSE, FALSE, time);
f03fc89f 359
f03fc89f 360 return;
829e3e8d 361 }
f03fc89f 362
b2dea2ab 363 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
8ee9d618 364
5af019af
RR
365 /* inform the wxDropTarget about the current GtkSelectionData.
366 this is only valid for the duration of this call */
367 drop_target->SetDragData( data );
f03fc89f 368
2245b2b2 369 wxDragResult result = ConvertFromGTK(context->suggested_action);
8ee9d618
VZ
370
371 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
829e3e8d 372 {
b2dea2ab 373 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned TRUE") );
8ee9d618 374
f03fc89f 375 /* tell GTK that data transfer was successfull */
ab8884ac 376 gtk_drag_finish( context, TRUE, FALSE, time );
33a5bc52 377 }
5af019af
RR
378 else
379 {
b2dea2ab 380 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
8ee9d618 381
f03fc89f 382 /* tell GTK that data transfer was not successfull */
5af019af
RR
383 gtk_drag_finish( context, FALSE, FALSE, time );
384 }
f03fc89f 385
5af019af
RR
386 /* after this, invalidate the drop_target's drag data */
387 drop_target->SetDragData( (GtkSelectionData*) NULL );
33a5bc52
RR
388}
389
4ba47b40 390//----------------------------------------------------------------------------
33a5bc52 391// wxDropTarget
4ba47b40 392//----------------------------------------------------------------------------
33a5bc52 393
8ee9d618
VZ
394wxDropTarget::wxDropTarget( wxDataObject *data )
395 : wxDropTargetBase( data )
f5368809 396{
829e3e8d
RR
397 m_firstMotion = TRUE;
398 m_dragContext = (GdkDragContext*) NULL;
399 m_dragWidget = (GtkWidget*) NULL;
5af019af 400 m_dragData = (GtkSelectionData*) NULL;
829e3e8d 401 m_dragTime = 0;
f5368809
RR
402}
403
c9057ae1
VZ
404wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
405 wxCoord WXUNUSED(y),
406 wxDragResult def )
d6086ea6 407{
8ee9d618 408 // GetMatchingPair() checks for m_dataObject too, no need to do it here
2edc8f5b
VZ
409
410 // disable the debug message from GetMatchingPair() - there are too many
411 // of them otherwise
c4fda16b 412#ifdef __WXDEBUG__
2edc8f5b
VZ
413 wxLogNull noLog;
414#endif // Debug
415
c9057ae1 416 return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
d6086ea6
RR
417}
418
c9057ae1 419bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
d6086ea6 420{
b068c4e8 421 if (!m_dataObject)
a3e7d24d 422 return FALSE;
8ee9d618 423
8e00741d 424 return (GetMatchingPair() != (GdkAtom) 0);
d6086ea6
RR
425}
426
8ee9d618
VZ
427wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
428 wxDragResult def )
5af019af 429{
b068c4e8 430 if (!m_dataObject)
11e1c70d 431 return wxDragNone;
8ee9d618 432
8e00741d 433 if (GetMatchingPair() == (GdkAtom) 0)
11e1c70d 434 return wxDragNone;
8ee9d618
VZ
435
436 return GetData() ? def : wxDragNone;
5af019af
RR
437}
438
8e00741d 439GdkAtom wxDropTarget::GetMatchingPair()
5af019af 440{
8ee9d618 441 if (!m_dataObject)
8e00741d 442 return (GdkAtom) 0;
5af019af 443
8ee9d618 444 if (!m_dragContext)
8e00741d 445 return (GdkAtom) 0;
f03fc89f 446
22d5903e
RR
447 GList *child = m_dragContext->targets;
448 while (child)
449 {
450 GdkAtom formatAtom = (GdkAtom) GPOINTER_TO_INT(child->data);
2edc8f5b 451 wxDataFormat format( formatAtom );
f03fc89f 452
a3e7d24d 453#ifdef __WXDEBUG__
b2dea2ab
VZ
454 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
455 format.GetId().c_str());
2edc8f5b
VZ
456#endif // Debug
457
b068c4e8 458 if (m_dataObject->IsSupportedFormat( format ))
2edc8f5b 459 return formatAtom;
f03fc89f 460
22d5903e
RR
461 child = child->next;
462 }
829e3e8d 463
8e00741d 464 return (GdkAtom) 0;
d6086ea6 465}
f03fc89f 466
8e00741d 467bool wxDropTarget::GetData()
d6086ea6 468{
8ee9d618 469 if (!m_dragData)
8e00741d 470 return FALSE;
f03fc89f 471
8ee9d618 472 if (!m_dataObject)
8e00741d 473 return FALSE;
f03fc89f 474
8e00741d 475 wxDataFormat dragFormat( m_dragData->target );
8ee9d618 476
b068c4e8 477 if (!m_dataObject->IsSupportedFormat( dragFormat ))
8e00741d 478 return FALSE;
f03fc89f 479
97c79de2
RR
480 m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
481
482 return TRUE;
d6086ea6 483}
f03fc89f 484
f5368809
RR
485void wxDropTarget::UnregisterWidget( GtkWidget *widget )
486{
223d09f6 487 wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
f03fc89f 488
829e3e8d 489 gtk_drag_dest_unset( widget );
f03fc89f 490
d6086ea6 491 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
f03fc89f 492 GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this );
33a5bc52 493
d6086ea6 494 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
f03fc89f 495 GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this );
33a5bc52 496
d6086ea6 497 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
f03fc89f 498 GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this );
33a5bc52 499
d6086ea6 500 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
f03fc89f 501 GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this );
f5368809
RR
502}
503
504void wxDropTarget::RegisterWidget( GtkWidget *widget )
505{
223d09f6 506 wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
f03fc89f 507
829e3e8d
RR
508 /* gtk_drag_dest_set() determines what default behaviour we'd like
509 GTK to supply. we don't want to specify out targets (=formats)
510 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
511 not GTK_DEST_DEFAULT_DROP). instead we react individually to
512 "drag_motion" and "drag_drop" events. this makes it possible
f03fc89f 513 to allow dropping on only a small area. we should set
829e3e8d
RR
514 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
515 highlighting if dragging over standard controls, but this
516 seems to be broken without the other two. */
f03fc89f 517
d6086ea6 518 gtk_drag_dest_set( widget,
f03fc89f
VZ
519 (GtkDestDefaults) 0, /* no default behaviour */
520 (GtkTargetEntry*) NULL, /* we don't supply any formats here */
521 0, /* number of targets = 0 */
522 (GdkDragAction) 0 ); /* we don't supply any actions here */
523
d6086ea6 524 gtk_signal_connect( GTK_OBJECT(widget), "drag_leave",
f03fc89f 525 GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this );
33a5bc52 526
d6086ea6 527 gtk_signal_connect( GTK_OBJECT(widget), "drag_motion",
f03fc89f 528 GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this );
33a5bc52 529
d6086ea6 530 gtk_signal_connect( GTK_OBJECT(widget), "drag_drop",
f03fc89f 531 GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this );
33a5bc52 532
d6086ea6 533 gtk_signal_connect( GTK_OBJECT(widget), "drag_data_received",
f03fc89f 534 GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this );
f5368809
RR
535}
536
4ba47b40
RR
537//----------------------------------------------------------------------------
538// "drag_data_get"
539//----------------------------------------------------------------------------
540
f03fc89f 541static void
4ba47b40 542source_drag_data_get (GtkWidget *WXUNUSED(widget),
b02da6b1 543 GdkDragContext *WXUNUSED(context),
f03fc89f
VZ
544 GtkSelectionData *selection_data,
545 guint WXUNUSED(info),
546 guint WXUNUSED(time),
547 wxDropSource *drop_source )
4ba47b40 548{
5549fa65
RR
549 if (g_isIdle) wxapp_install_idle_handler();
550
97c79de2 551 wxDataFormat format( selection_data->target );
8ee9d618 552
b2dea2ab
VZ
553 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
554 format.GetId().c_str());
f6bcfd97 555
1dd989e1 556 drop_source->m_retValue = wxDragCancel;
8ee9d618 557
97c79de2 558 wxDataObject *data = drop_source->GetDataObject();
f6bcfd97 559
1dd989e1 560 if (!data)
97c79de2 561 {
b2dea2ab 562 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
2edc8f5b 563 return;
97c79de2 564 }
1dd989e1 565
97c79de2
RR
566 if (!data->IsSupportedFormat(format))
567 {
b2dea2ab 568 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
2edc8f5b 569 return;
97c79de2 570 }
f03fc89f 571
97c79de2
RR
572 if (data->GetDataSize(format) == 0)
573 {
b2dea2ab 574 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
2edc8f5b 575 return;
97c79de2 576 }
8ee9d618 577
97c79de2 578 size_t size = data->GetDataSize(format);
f03fc89f 579
1dd989e1 580// printf( "data size: %d.\n", (int)data_size );
f03fc89f 581
1dd989e1 582 guchar *d = new guchar[size];
8ee9d618 583
97c79de2 584 if (!data->GetDataHere( format, (void*)d ))
1dd989e1 585 {
97c79de2 586 delete[] d;
2edc8f5b 587 return;
1dd989e1 588 }
f03fc89f 589
b453e1b2 590#if wxUSE_THREADS
1dd989e1 591 /* disable GUI threads */
b453e1b2 592#endif
8e193f38 593
4ba47b40 594 gtk_selection_data_set( selection_data,
f03fc89f
VZ
595 selection_data->target,
596 8, // 8-bit
1dd989e1
RR
597 d,
598 size );
f03fc89f 599
b453e1b2 600#if wxUSE_THREADS
1dd989e1 601 /* enable GUI threads */
b453e1b2 602#endif
f03fc89f 603
97c79de2 604 delete[] d;
4ba47b40 605}
f03fc89f 606
4ba47b40
RR
607//----------------------------------------------------------------------------
608// "drag_data_delete"
609//----------------------------------------------------------------------------
610
9f39393d
VZ
611static void source_drag_data_delete( GtkWidget *WXUNUSED(widget),
612 GdkDragContext *context,
613 wxDropSource *WXUNUSED(drop_source) )
4ba47b40 614{
f6bcfd97 615 if (g_isIdle)
13fb7b7a 616 wxapp_install_idle_handler();
33a5bc52 617
13fb7b7a 618 // printf( "Drag source: drag_data_delete\n" );
4ba47b40 619}
f03fc89f 620
4ba47b40
RR
621//----------------------------------------------------------------------------
622// "drag_begin"
623//----------------------------------------------------------------------------
624
625static void source_drag_begin( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
626 GdkDragContext *WXUNUSED(context),
627 wxDropSource *WXUNUSED(drop_source) )
4ba47b40 628{
f6bcfd97 629 if (g_isIdle)
13fb7b7a 630 wxapp_install_idle_handler();
5549fa65 631
13fb7b7a 632 // printf( "Drag source: drag_begin.\n" );
4ba47b40 633}
f03fc89f 634
4ba47b40
RR
635//----------------------------------------------------------------------------
636// "drag_end"
637//----------------------------------------------------------------------------
638
639static void source_drag_end( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
640 GdkDragContext *WXUNUSED(context),
641 wxDropSource *drop_source )
4ba47b40 642{
5549fa65
RR
643 if (g_isIdle) wxapp_install_idle_handler();
644
13fb7b7a 645 // printf( "Drag source: drag_end.\n" );
4ba47b40
RR
646
647 drop_source->m_waiting = FALSE;
648}
f03fc89f 649
7b5d5699
RR
650//-----------------------------------------------------------------------------
651// "configure_event" from m_iconWindow
652//-----------------------------------------------------------------------------
653
8ee9d618 654static gint
7b5d5699
RR
655gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
656{
8ee9d618 657 if (g_isIdle)
7b5d5699
RR
658 wxapp_install_idle_handler();
659
2245b2b2 660 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
8ee9d618 661
7b5d5699
RR
662 return 0;
663}
664
4ba47b40
RR
665//---------------------------------------------------------------------------
666// wxDropSource
667//---------------------------------------------------------------------------
22d5903e 668
f6bcfd97
BP
669wxDropSource::wxDropSource(wxWindow *win,
670 const wxIcon &iconCopy,
671 const wxIcon &iconMove,
672 const wxIcon &iconNone)
22d5903e 673{
4ba47b40 674 m_waiting = TRUE;
f03fc89f 675
7b5d5699 676 m_iconWindow = (GtkWidget*) NULL;
8ee9d618 677
22d5903e 678 m_window = win;
a2053b27
RR
679 m_widget = win->m_widget;
680 if (win->m_wxwindow) m_widget = win->m_wxwindow;
f03fc89f 681
22d5903e
RR
682 m_retValue = wxDragCancel;
683
f6bcfd97 684 SetIcons(iconCopy, iconMove, iconNone);
22d5903e
RR
685}
686
f6bcfd97
BP
687wxDropSource::wxDropSource(wxDataObject& data,
688 wxWindow *win,
689 const wxIcon &iconCopy,
690 const wxIcon &iconMove,
691 const wxIcon &iconNone)
22d5903e 692{
4ba47b40 693 m_waiting = TRUE;
8ee9d618 694
b068c4e8 695 SetData( data );
f03fc89f 696
7b5d5699 697 m_iconWindow = (GtkWidget*) NULL;
8ee9d618 698
22d5903e 699 m_window = win;
a2053b27
RR
700 m_widget = win->m_widget;
701 if (win->m_wxwindow) m_widget = win->m_wxwindow;
8ee9d618 702
22d5903e 703 m_retValue = wxDragCancel;
f03fc89f 704
f6bcfd97
BP
705 SetIcons(iconCopy, iconMove, iconNone);
706}
707
708void wxDropSource::SetIcons(const wxIcon &iconCopy,
709 const wxIcon &iconMove,
710 const wxIcon &iconNone)
711{
712 m_iconCopy = iconCopy;
713 m_iconMove = iconMove;
714 m_iconNone = iconNone;
715
716 if ( !m_iconCopy.Ok() )
717 m_iconCopy = wxIcon(page_xpm);
718 if ( !m_iconMove.Ok() )
719 m_iconMove = m_iconCopy;
720 if ( !m_iconNone.Ok() )
721 m_iconNone = m_iconCopy;
22d5903e
RR
722}
723
8e193f38 724wxDropSource::~wxDropSource()
22d5903e 725{
22d5903e 726}
f03fc89f 727
f6bcfd97 728void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
7b5d5699 729{
f6bcfd97
BP
730 // get the right icon to display
731 wxIcon *icon = NULL;
732 if ( action & GDK_ACTION_MOVE )
733 icon = &m_iconMove;
734 else if ( action & GDK_ACTION_COPY )
735 icon = &m_iconCopy;
736 else
737 icon = &m_iconNone;
738
739 GdkBitmap *mask;
740 if ( icon->GetMask() )
741 mask = icon->GetMask()->GetBitmap();
742 else
743 mask = (GdkBitmap *)NULL;
744
745 GdkPixmap *pixmap = icon->GetPixmap();
7b5d5699
RR
746
747 gint width,height;
748 gdk_window_get_size (pixmap, &width, &height);
749
750 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
9d084560 751#ifndef __WXGTK20__
7b5d5699 752 gtk_widget_push_visual (gdk_colormap_get_visual (colormap));
9d084560 753#endif
7b5d5699
RR
754 gtk_widget_push_colormap (colormap);
755
756 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
757 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
758 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE);
759
9d084560 760#ifndef __WXGTK20__
7b5d5699 761 gtk_widget_pop_visual ();
9d084560 762#endif
7b5d5699
RR
763 gtk_widget_pop_colormap ();
764
765 gtk_widget_set_usize (m_iconWindow, width, height);
766 gtk_widget_realize (m_iconWindow);
767
768 gtk_signal_connect( GTK_OBJECT(m_iconWindow), "configure_event",
769 GTK_SIGNAL_FUNC(gtk_dnd_window_configure_callback), (gpointer)this );
8ee9d618 770
7b5d5699 771 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
8ee9d618 772
7b5d5699
RR
773 if (mask)
774 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
775
f6bcfd97 776 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
7b5d5699
RR
777}
778
2245b2b2 779wxDragResult wxDropSource::DoDragDrop(int flags)
22d5903e 780{
2245b2b2
VZ
781 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
782 wxT("Drop source: no data") );
8ee9d618 783
513abb88
RR
784 // still in drag
785 if (g_blockEventsOnDrag)
2245b2b2 786 return wxDragNone;
b2dea2ab 787
f6bcfd97 788 // disabled for now
513abb88 789 g_blockEventsOnDrag = TRUE;
f03fc89f 790
4ba47b40 791 RegisterWindow();
f03fc89f 792
4ba47b40 793 m_waiting = TRUE;
22d5903e 794
4ba47b40 795 GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
8ee9d618 796
a3e7d24d 797 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
b068c4e8 798 m_data->GetAllFormats( array );
2245b2b2
VZ
799 size_t count = m_data->GetFormatCount();
800 for (size_t i = 0; i < count; i++)
a3e7d24d
RR
801 {
802 GdkAtom atom = array[i];
b2dea2ab 803 wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ));
a3e7d24d
RR
804 gtk_target_list_add( target_list, atom, 0, 0 );
805 }
806 delete[] array;
f03fc89f 807
4ba47b40
RR
808 GdkEventMotion event;
809 event.window = m_widget->window;
810 int x = 0;
811 int y = 0;
812 GdkModifierType state;
813 gdk_window_get_pointer( event.window, &x, &y, &state );
814 event.x = x;
815 event.y = y;
816 event.state = state;
b02da6b1 817 event.time = (guint32)GDK_CURRENT_TIME;
f03fc89f 818
4ba47b40
RR
819 /* GTK wants to know which button was pressed which caused the dragging */
820 int button_number = 0;
821 if (event.state & GDK_BUTTON1_MASK) button_number = 1;
822 else if (event.state & GDK_BUTTON2_MASK) button_number = 2;
823 else if (event.state & GDK_BUTTON3_MASK) button_number = 3;
824
b453e1b2
RR
825#if wxUSE_THREADS
826 /* disable GUI threads */
b453e1b2 827#endif
8e193f38 828
4ba47b40
RR
829 /* don't start dragging if no button is down */
830 if (button_number)
22d5903e 831 {
9f39393d 832 int action = GDK_ACTION_COPY;
2245b2b2 833 if ( flags & wxDrag_AllowMove )
9f39393d 834 action |= GDK_ACTION_MOVE;
2245b2b2
VZ
835
836 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
837 // to use a global to pass the flags to the drop target but I'd
838 // surely prefer a better way to do it
839 gs_flagsForDrag = flags;
840
4ba47b40 841 GdkDragContext *context = gtk_drag_begin( m_widget,
f6bcfd97 842 target_list,
46c87a9a 843 (GdkDragAction)action,
f6bcfd97
BP
844 button_number, /* number of mouse button which started drag */
845 (GdkEvent*) &event );
8ee9d618
VZ
846
847 m_dragContext = context;
848
f6bcfd97 849 PrepareIcon( action, context );
f03fc89f 850
2245b2b2
VZ
851 while (m_waiting)
852 gtk_main_iteration();
f6bcfd97 853
2245b2b2
VZ
854 m_retValue = ConvertFromGTK(context->action);
855 if ( m_retValue == wxDragNone )
856 m_retValue = wxDragCancel;
4ba47b40 857 }
22d5903e 858
b453e1b2
RR
859#if wxUSE_THREADS
860 /* re-enable GUI threads */
b453e1b2 861#endif
8e193f38 862
4ba47b40 863 g_blockEventsOnDrag = FALSE;
f03fc89f 864
4ba47b40
RR
865 UnregisterWindow();
866
867 return m_retValue;
22d5903e
RR
868}
869
4ba47b40 870void wxDropSource::RegisterWindow()
22d5903e 871{
4ba47b40 872 if (!m_widget) return;
f6bcfd97 873
4ba47b40 874 gtk_signal_connect( GTK_OBJECT(m_widget), "drag_data_get",
f03fc89f 875 GTK_SIGNAL_FUNC (source_drag_data_get), (gpointer) this);
4ba47b40 876 gtk_signal_connect (GTK_OBJECT(m_widget), "drag_data_delete",
f03fc89f 877 GTK_SIGNAL_FUNC (source_drag_data_delete), (gpointer) this );
4ba47b40 878 gtk_signal_connect (GTK_OBJECT(m_widget), "drag_begin",
f03fc89f 879 GTK_SIGNAL_FUNC (source_drag_begin), (gpointer) this );
4ba47b40 880 gtk_signal_connect (GTK_OBJECT(m_widget), "drag_end",
f03fc89f 881 GTK_SIGNAL_FUNC (source_drag_end), (gpointer) this );
4ba47b40 882
22d5903e
RR
883}
884
4ba47b40 885void wxDropSource::UnregisterWindow()
22d5903e
RR
886{
887 if (!m_widget) return;
f03fc89f 888
4ba47b40 889 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
f03fc89f 890 GTK_SIGNAL_FUNC(source_drag_data_get), (gpointer) this );
4ba47b40 891 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
f03fc89f 892 GTK_SIGNAL_FUNC(source_drag_data_delete), (gpointer) this );
4ba47b40 893 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
f03fc89f 894 GTK_SIGNAL_FUNC(source_drag_begin), (gpointer) this );
4ba47b40 895 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
f03fc89f 896 GTK_SIGNAL_FUNC(source_drag_end), (gpointer) this );
22d5903e
RR
897}
898
ac57418f 899#endif
93c5dd39 900 // wxUSE_DRAG_AND_DROP