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