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