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