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