]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dnd.cpp
use xdg-open in wxLaunchDefaultBrowser() on Unix if available
[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 */
169 drop_target->SetDragContext( 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
RR
178 /* after this, invalidate the drop_target's GdkDragContext */
179 drop_target->SetDragContext( (GdkDragContext*) 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
829e3e8d
RR
199 /* inform the wxDropTarget about the current GdkDragContext.
200 this is only valid for the duration of this call */
201 drop_target->SetDragContext( context );
f03fc89f 202
2245b2b2
VZ
203 // GTK+ always supposes that we want to copy the data by default while we
204 // might want to move it, so examine not only suggested_action - which is
205 // only good if we don't have our own preferences - but also the actions
206 // field
8ee9d618 207 wxDragResult result;
917ae499
RR
208 if (drop_target->GetDefaultAction() == wxDragNone)
209 {
210 // use default action set by wxDropSource::DoDragDrop()
2245b2b2
VZ
211 if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove &&
212 (context->actions & GDK_ACTION_MOVE ) )
213 {
214 // move is requested by the program and allowed by GTK+ - do it, even
215 // though suggested_action may be currently wxDragCopy
8ee9d618 216 result = wxDragMove;
2245b2b2
VZ
217 }
218 else // use whatever GTK+ says we should
219 {
220 result = ConvertFromGTK(context->suggested_action);
221
222 if ( (result == wxDragMove) && !(gs_flagsForDrag & wxDrag_AllowMove) )
223 {
224 // we're requested to move but we can't
225 result = wxDragCopy;
226 }
227 }
917ae499
RR
228 }
229 else if (drop_target->GetDefaultAction() == wxDragMove &&
230 (context->actions & GDK_ACTION_MOVE))
231 {
232 result = wxDragMove;
233 }
234 else
235 {
236 if (context->actions & GDK_ACTION_COPY)
237 result = wxDragCopy;
238 else if (context->actions & GDK_ACTION_MOVE)
239 result = wxDragMove;
240 else
241 result = wxDragNone;
242 }
8e00741d 243
829e3e8d 244 if (drop_target->m_firstMotion)
d6086ea6 245 {
829e3e8d 246 /* the first "drag_motion" event substitutes a "drag_enter" event */
c9057ae1 247 result = drop_target->OnEnter( x, y, result );
8e00741d
RR
248 }
249 else
250 {
251 /* give program a chance to react (i.e. to say no by returning FALSE) */
c9057ae1 252 result = drop_target->OnDragOver( x, y, result );
d6086ea6 253 }
f03fc89f 254
8ee9d618 255 bool ret = wxIsDragResultOk( result );
829e3e8d 256 if (ret)
bd77da97 257 {
8ee9d618 258 GdkDragAction action;
917ae499 259 if (result == wxDragCopy)
8ee9d618 260 action = GDK_ACTION_COPY;
b184227d
RD
261 else if (result == wxDragLink)
262 action = GDK_ACTION_LINK;
8ee9d618
VZ
263 else
264 action = GDK_ACTION_MOVE;
265
7b5d5699 266 gdk_drag_status( context, action, time );
bd77da97 267 }
f03fc89f 268
829e3e8d
RR
269 /* after this, invalidate the drop_target's GdkDragContext */
270 drop_target->SetDragContext( (GdkDragContext*) NULL );
f03fc89f 271
829e3e8d 272 /* this has to be done because GDK has no "drag_enter" event */
0a164d4c 273 drop_target->m_firstMotion = false;
f03fc89f 274
829e3e8d 275 return ret;
33a5bc52 276}
865bb325 277}
33a5bc52
RR
278
279// ----------------------------------------------------------------------------
280// "drag_drop"
281// ----------------------------------------------------------------------------
282
865bb325 283extern "C" {
33a5bc52 284static gboolean target_drag_drop( GtkWidget *widget,
f03fc89f
VZ
285 GdkDragContext *context,
286 gint x,
287 gint y,
288 guint time,
289 wxDropTarget *drop_target )
33a5bc52 290{
829e3e8d
RR
291 /* Owen Taylor: "if the drop is not in a drop zone,
292 return FALSE, otherwise, if you aren't accepting
293 the drop, call gtk_drag_finish() with success == FALSE
294 otherwise call gtk_drag_data_get()" */
4ba47b40
RR
295
296// printf( "drop.\n" );
f03fc89f 297
829e3e8d
RR
298 /* this seems to make a difference between not accepting
299 due to wrong target area and due to wrong format. let
300 us hope that this is not required.. */
f03fc89f 301
829e3e8d
RR
302 /* inform the wxDropTarget about the current GdkDragContext.
303 this is only valid for the duration of this call */
304 drop_target->SetDragContext( context );
f03fc89f 305
829e3e8d
RR
306 /* inform the wxDropTarget about the current drag widget.
307 this is only valid for the duration of this call */
308 drop_target->SetDragWidget( widget );
f03fc89f 309
829e3e8d
RR
310 /* inform the wxDropTarget about the current drag time.
311 this is only valid for the duration of this call */
312 drop_target->SetDragTime( time );
f03fc89f 313
bd77da97
RR
314/*
315 wxDragResult result = wxDragMove;
316 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
317*/
318
513abb88
RR
319 /* reset the block here as someone might very well
320 show a dialog as a reaction to a drop and this
321 wouldn't work without events */
0a164d4c 322 g_blockEventsOnDrag = false;
b2dea2ab 323
829e3e8d 324 bool ret = drop_target->OnDrop( x, y );
f03fc89f 325
5af019af 326 if (!ret)
829e3e8d 327 {
98528e9b 328#ifdef __WXDEBUG__
b2dea2ab 329 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
98528e9b 330#endif
f4322df6 331
829e3e8d
RR
332 /* cancel the whole thing */
333 gtk_drag_finish( context,
f03fc89f
VZ
334 FALSE, /* no success */
335 FALSE, /* don't delete data on dropping side */
336 time );
829e3e8d 337 }
8e00741d
RR
338 else
339 {
98528e9b 340#ifdef __WXDEBUG__
670f9935 341 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") );
98528e9b 342#endif
f4322df6 343
8e00741d
RR
344#if wxUSE_THREADS
345 /* disable GUI threads */
8e00741d
RR
346#endif
347
348 GdkAtom format = drop_target->GetMatchingPair();
b72aa48c
VZ
349
350 // this does happen somehow, see bug 555111
637b7e4f 351 wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") );
8ee9d618 352
bd77da97
RR
353/*
354 GdkDragAction action = GDK_ACTION_MOVE;
8ee9d618
VZ
355 if (result == wxDragCopy) action == GDK_ACTION_COPY;
356 context->action = action;
bd77da97 357*/
8e00741d
RR
358 /* this should trigger an "drag_data_received" event */
359 gtk_drag_get_data( widget,
360 context,
361 format,
362 time );
363
364#if wxUSE_THREADS
365 /* re-enable GUI threads */
8e00741d
RR
366#endif
367 }
f03fc89f 368
829e3e8d
RR
369 /* after this, invalidate the drop_target's GdkDragContext */
370 drop_target->SetDragContext( (GdkDragContext*) NULL );
f03fc89f 371
829e3e8d
RR
372 /* after this, invalidate the drop_target's drag widget */
373 drop_target->SetDragWidget( (GtkWidget*) NULL );
f03fc89f 374
829e3e8d 375 /* this has to be done because GDK has no "drag_enter" event */
0a164d4c 376 drop_target->m_firstMotion = true;
f03fc89f 377
829e3e8d 378 return ret;
33a5bc52 379}
865bb325 380}
33a5bc52
RR
381
382// ----------------------------------------------------------------------------
383// "drag_data_received"
384// ----------------------------------------------------------------------------
385
865bb325 386extern "C" {
33a5bc52 387static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
388 GdkDragContext *context,
389 gint x,
390 gint y,
391 GtkSelectionData *data,
392 guint WXUNUSED(info),
393 guint time,
394 wxDropTarget *drop_target )
33a5bc52 395{
829e3e8d
RR
396 /* Owen Taylor: "call gtk_drag_finish() with
397 success == TRUE" */
398
829e3e8d 399 if ((data->length <= 0) || (data->format != 8))
33a5bc52 400 {
829e3e8d
RR
401 /* negative data length and non 8-bit data format
402 qualifies for junk */
403 gtk_drag_finish (context, FALSE, FALSE, time);
f03fc89f 404
f03fc89f 405 return;
829e3e8d 406 }
f03fc89f 407
98528e9b 408#ifdef __WXDEBUG__
b2dea2ab 409 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
98528e9b 410#endif
f4322df6 411
5af019af
RR
412 /* inform the wxDropTarget about the current GtkSelectionData.
413 this is only valid for the duration of this call */
414 drop_target->SetDragData( data );
f03fc89f 415
c7d94ca4 416 wxDragResult result = ConvertFromGTK(context->action);
8ee9d618
VZ
417
418 if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) )
829e3e8d 419 {
98528e9b 420#ifdef __WXDEBUG__
670f9935 421 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") );
98528e9b 422#endif
f4322df6 423
3103e8a9 424 /* tell GTK that data transfer was successful */
ab8884ac 425 gtk_drag_finish( context, TRUE, FALSE, time );
33a5bc52 426 }
5af019af
RR
427 else
428 {
98528e9b 429#ifdef __WXDEBUG__
b2dea2ab 430 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
98528e9b 431#endif
f4322df6 432
3103e8a9 433 /* tell GTK that data transfer was not successful */
5af019af
RR
434 gtk_drag_finish( context, FALSE, FALSE, time );
435 }
f03fc89f 436
5af019af
RR
437 /* after this, invalidate the drop_target's drag data */
438 drop_target->SetDragData( (GtkSelectionData*) NULL );
33a5bc52 439}
865bb325 440}
33a5bc52 441
4ba47b40 442//----------------------------------------------------------------------------
33a5bc52 443// wxDropTarget
4ba47b40 444//----------------------------------------------------------------------------
33a5bc52 445
8ee9d618
VZ
446wxDropTarget::wxDropTarget( wxDataObject *data )
447 : wxDropTargetBase( data )
f5368809 448{
0a164d4c 449 m_firstMotion = true;
829e3e8d
RR
450 m_dragContext = (GdkDragContext*) NULL;
451 m_dragWidget = (GtkWidget*) NULL;
5af019af 452 m_dragData = (GtkSelectionData*) NULL;
829e3e8d 453 m_dragTime = 0;
f5368809
RR
454}
455
c9057ae1
VZ
456wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
457 wxCoord WXUNUSED(y),
458 wxDragResult def )
d6086ea6 459{
8ee9d618 460 // GetMatchingPair() checks for m_dataObject too, no need to do it here
2edc8f5b
VZ
461
462 // disable the debug message from GetMatchingPair() - there are too many
463 // of them otherwise
c4fda16b 464#ifdef __WXDEBUG__
2edc8f5b
VZ
465 wxLogNull noLog;
466#endif // Debug
467
c9057ae1 468 return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
d6086ea6
RR
469}
470
c9057ae1 471bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
d6086ea6 472{
b068c4e8 473 if (!m_dataObject)
0a164d4c 474 return false;
8ee9d618 475
8e00741d 476 return (GetMatchingPair() != (GdkAtom) 0);
d6086ea6
RR
477}
478
8ee9d618
VZ
479wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
480 wxDragResult def )
5af019af 481{
b068c4e8 482 if (!m_dataObject)
11e1c70d 483 return wxDragNone;
8ee9d618 484
8e00741d 485 if (GetMatchingPair() == (GdkAtom) 0)
11e1c70d 486 return wxDragNone;
8ee9d618
VZ
487
488 return GetData() ? def : wxDragNone;
5af019af
RR
489}
490
8e00741d 491GdkAtom wxDropTarget::GetMatchingPair()
5af019af 492{
8ee9d618 493 if (!m_dataObject)
8e00741d 494 return (GdkAtom) 0;
5af019af 495
8ee9d618 496 if (!m_dragContext)
8e00741d 497 return (GdkAtom) 0;
f03fc89f 498
22d5903e
RR
499 GList *child = m_dragContext->targets;
500 while (child)
501 {
68567a96 502 GdkAtom formatAtom = (GdkAtom)(child->data);
2edc8f5b 503 wxDataFormat format( formatAtom );
f03fc89f 504
a3e7d24d 505#ifdef __WXDEBUG__
b2dea2ab
VZ
506 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
507 format.GetId().c_str());
2edc8f5b
VZ
508#endif // Debug
509
b068c4e8 510 if (m_dataObject->IsSupportedFormat( format ))
2edc8f5b 511 return formatAtom;
f03fc89f 512
22d5903e
RR
513 child = child->next;
514 }
829e3e8d 515
8e00741d 516 return (GdkAtom) 0;
d6086ea6 517}
f03fc89f 518
8e00741d 519bool wxDropTarget::GetData()
d6086ea6 520{
8ee9d618 521 if (!m_dragData)
0a164d4c 522 return false;
f03fc89f 523
8ee9d618 524 if (!m_dataObject)
0a164d4c 525 return false;
f03fc89f 526
8e00741d 527 wxDataFormat dragFormat( m_dragData->target );
8ee9d618 528
b068c4e8 529 if (!m_dataObject->IsSupportedFormat( dragFormat ))
0a164d4c 530 return false;
f03fc89f 531
97c79de2
RR
532 m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
533
0a164d4c 534 return true;
d6086ea6 535}
f03fc89f 536
f5368809
RR
537void wxDropTarget::UnregisterWidget( GtkWidget *widget )
538{
223d09f6 539 wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
f03fc89f 540
829e3e8d 541 gtk_drag_dest_unset( widget );
f03fc89f 542
9fa72bd2
MR
543 g_signal_handlers_disconnect_by_func (widget,
544 (gpointer) target_drag_leave, this);
545 g_signal_handlers_disconnect_by_func (widget,
546 (gpointer) target_drag_motion, this);
547 g_signal_handlers_disconnect_by_func (widget,
548 (gpointer) target_drag_drop, this);
549 g_signal_handlers_disconnect_by_func (widget,
550 (gpointer) target_drag_data_received, this);
f5368809
RR
551}
552
553void wxDropTarget::RegisterWidget( GtkWidget *widget )
554{
223d09f6 555 wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
f03fc89f 556
829e3e8d
RR
557 /* gtk_drag_dest_set() determines what default behaviour we'd like
558 GTK to supply. we don't want to specify out targets (=formats)
559 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
560 not GTK_DEST_DEFAULT_DROP). instead we react individually to
561 "drag_motion" and "drag_drop" events. this makes it possible
f03fc89f 562 to allow dropping on only a small area. we should set
829e3e8d
RR
563 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
564 highlighting if dragging over standard controls, but this
565 seems to be broken without the other two. */
f03fc89f 566
d6086ea6 567 gtk_drag_dest_set( widget,
f03fc89f
VZ
568 (GtkDestDefaults) 0, /* no default behaviour */
569 (GtkTargetEntry*) NULL, /* we don't supply any formats here */
570 0, /* number of targets = 0 */
571 (GdkDragAction) 0 ); /* we don't supply any actions here */
572
9fa72bd2
MR
573 g_signal_connect (widget, "drag_leave",
574 G_CALLBACK (target_drag_leave), this);
33a5bc52 575
9fa72bd2
MR
576 g_signal_connect (widget, "drag_motion",
577 G_CALLBACK (target_drag_motion), this);
33a5bc52 578
9fa72bd2
MR
579 g_signal_connect (widget, "drag_drop",
580 G_CALLBACK (target_drag_drop), this);
33a5bc52 581
9fa72bd2
MR
582 g_signal_connect (widget, "drag_data_received",
583 G_CALLBACK (target_drag_data_received), this);
f5368809
RR
584}
585
4ba47b40
RR
586//----------------------------------------------------------------------------
587// "drag_data_get"
588//----------------------------------------------------------------------------
589
865bb325 590extern "C" {
f03fc89f 591static void
4ba47b40 592source_drag_data_get (GtkWidget *WXUNUSED(widget),
b02da6b1 593 GdkDragContext *WXUNUSED(context),
f03fc89f
VZ
594 GtkSelectionData *selection_data,
595 guint WXUNUSED(info),
596 guint WXUNUSED(time),
597 wxDropSource *drop_source )
4ba47b40 598{
97c79de2 599 wxDataFormat format( selection_data->target );
8ee9d618 600
98528e9b 601#ifdef __WXDEBUG__
b2dea2ab
VZ
602 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
603 format.GetId().c_str());
98528e9b 604#endif
f4322df6 605
1dd989e1 606 drop_source->m_retValue = wxDragCancel;
8ee9d618 607
97c79de2 608 wxDataObject *data = drop_source->GetDataObject();
f6bcfd97 609
1dd989e1 610 if (!data)
97c79de2 611 {
98528e9b 612#ifdef __WXDEBUG__
b2dea2ab 613 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
98528e9b
JJ
614#endif
615 return;
97c79de2 616 }
1dd989e1 617
97c79de2
RR
618 if (!data->IsSupportedFormat(format))
619 {
98528e9b 620#ifdef __WXDEBUG__
b2dea2ab 621 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
98528e9b
JJ
622#endif
623 return;
97c79de2 624 }
f03fc89f 625
97c79de2
RR
626 if (data->GetDataSize(format) == 0)
627 {
98528e9b 628#ifdef __WXDEBUG__
b2dea2ab 629 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
98528e9b
JJ
630#endif
631 return;
97c79de2 632 }
8ee9d618 633
97c79de2 634 size_t size = data->GetDataSize(format);
f03fc89f 635
1dd989e1 636// printf( "data size: %d.\n", (int)data_size );
f03fc89f 637
1dd989e1 638 guchar *d = new guchar[size];
8ee9d618 639
97c79de2 640 if (!data->GetDataHere( format, (void*)d ))
1dd989e1 641 {
97c79de2 642 delete[] d;
2edc8f5b 643 return;
1dd989e1 644 }
f03fc89f 645
b453e1b2 646#if wxUSE_THREADS
1dd989e1 647 /* disable GUI threads */
b453e1b2 648#endif
8e193f38 649
0a164d4c
WS
650 gtk_selection_data_set( selection_data,
651 selection_data->target,
652 8, // 8-bit
653 d,
654 size );
f03fc89f 655
b453e1b2 656#if wxUSE_THREADS
1dd989e1 657 /* enable GUI threads */
b453e1b2 658#endif
f03fc89f 659
97c79de2 660 delete[] d;
4ba47b40 661}
865bb325 662}
f03fc89f 663
4ba47b40
RR
664//----------------------------------------------------------------------------
665// "drag_end"
666//----------------------------------------------------------------------------
667
865bb325 668extern "C" {
4ba47b40 669static void source_drag_end( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
670 GdkDragContext *WXUNUSED(context),
671 wxDropSource *drop_source )
4ba47b40 672{
13fb7b7a 673 // printf( "Drag source: drag_end.\n" );
4ba47b40 674
0a164d4c 675 drop_source->m_waiting = false;
4ba47b40 676}
865bb325 677}
f03fc89f 678
7b5d5699
RR
679//-----------------------------------------------------------------------------
680// "configure_event" from m_iconWindow
681//-----------------------------------------------------------------------------
682
865bb325 683extern "C" {
8ee9d618 684static gint
7b5d5699
RR
685gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
686{
2245b2b2 687 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
8ee9d618 688
7b5d5699
RR
689 return 0;
690}
865bb325 691}
7b5d5699 692
4ba47b40
RR
693//---------------------------------------------------------------------------
694// wxDropSource
695//---------------------------------------------------------------------------
22d5903e 696
f6bcfd97
BP
697wxDropSource::wxDropSource(wxWindow *win,
698 const wxIcon &iconCopy,
699 const wxIcon &iconMove,
700 const wxIcon &iconNone)
22d5903e 701{
0a164d4c 702 m_waiting = true;
f03fc89f 703
7b5d5699 704 m_iconWindow = (GtkWidget*) NULL;
8ee9d618 705
22d5903e 706 m_window = win;
a2053b27
RR
707 m_widget = win->m_widget;
708 if (win->m_wxwindow) m_widget = win->m_wxwindow;
f03fc89f 709
22d5903e
RR
710 m_retValue = wxDragCancel;
711
f6bcfd97 712 SetIcons(iconCopy, iconMove, iconNone);
22d5903e
RR
713}
714
f6bcfd97
BP
715wxDropSource::wxDropSource(wxDataObject& data,
716 wxWindow *win,
717 const wxIcon &iconCopy,
718 const wxIcon &iconMove,
719 const wxIcon &iconNone)
22d5903e 720{
0a164d4c 721 m_waiting = true;
8ee9d618 722
b068c4e8 723 SetData( data );
f03fc89f 724
7b5d5699 725 m_iconWindow = (GtkWidget*) NULL;
8ee9d618 726
22d5903e 727 m_window = win;
a2053b27
RR
728 m_widget = win->m_widget;
729 if (win->m_wxwindow) m_widget = win->m_wxwindow;
8ee9d618 730
22d5903e 731 m_retValue = wxDragCancel;
f03fc89f 732
f6bcfd97
BP
733 SetIcons(iconCopy, iconMove, iconNone);
734}
735
736void wxDropSource::SetIcons(const wxIcon &iconCopy,
737 const wxIcon &iconMove,
738 const wxIcon &iconNone)
739{
740 m_iconCopy = iconCopy;
741 m_iconMove = iconMove;
742 m_iconNone = iconNone;
743
744 if ( !m_iconCopy.Ok() )
745 m_iconCopy = wxIcon(page_xpm);
746 if ( !m_iconMove.Ok() )
747 m_iconMove = m_iconCopy;
748 if ( !m_iconNone.Ok() )
749 m_iconNone = m_iconCopy;
22d5903e
RR
750}
751
8e193f38 752wxDropSource::~wxDropSource()
22d5903e 753{
22d5903e 754}
f03fc89f 755
f6bcfd97 756void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
7b5d5699 757{
f6bcfd97
BP
758 // get the right icon to display
759 wxIcon *icon = NULL;
760 if ( action & GDK_ACTION_MOVE )
761 icon = &m_iconMove;
762 else if ( action & GDK_ACTION_COPY )
763 icon = &m_iconCopy;
764 else
765 icon = &m_iconNone;
766
767 GdkBitmap *mask;
768 if ( icon->GetMask() )
769 mask = icon->GetMask()->GetBitmap();
770 else
771 mask = (GdkBitmap *)NULL;
772
773 GdkPixmap *pixmap = icon->GetPixmap();
7b5d5699
RR
774
775 gint width,height;
791d7ea2 776 gdk_drawable_get_size (pixmap, &width, &height);
7b5d5699
RR
777
778 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
7b5d5699
RR
779 gtk_widget_push_colormap (colormap);
780
781 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
782 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
10bd1f7d 783 gtk_widget_set_app_paintable (m_iconWindow, TRUE);
7b5d5699 784
7b5d5699
RR
785 gtk_widget_pop_colormap ();
786
370dc79c 787 gtk_widget_set_size_request (m_iconWindow, width, height);
7b5d5699
RR
788 gtk_widget_realize (m_iconWindow);
789
9fa72bd2
MR
790 g_signal_connect (m_iconWindow, "configure_event",
791 G_CALLBACK (gtk_dnd_window_configure_callback), this);
8ee9d618 792
7b5d5699 793 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
8ee9d618 794
7b5d5699
RR
795 if (mask)
796 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
797
f6bcfd97 798 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
7b5d5699
RR
799}
800
2245b2b2 801wxDragResult wxDropSource::DoDragDrop(int flags)
22d5903e 802{
2245b2b2
VZ
803 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
804 wxT("Drop source: no data") );
8ee9d618 805
513abb88
RR
806 // still in drag
807 if (g_blockEventsOnDrag)
2245b2b2 808 return wxDragNone;
b2dea2ab 809
5e513780
RR
810 // don't start dragging if no button is down
811 if (g_lastButtonNumber == 0)
812 return wxDragNone;
f4322df6 813
5e513780
RR
814 // we can only start a drag after a mouse event
815 if (g_lastMouseEvent == NULL)
816 return wxDragNone;
817
86f19f7c
VZ
818 GTKConnectDragSignals();
819 wxON_BLOCK_EXIT_OBJ0(*this, wxDropSource::GTKDisconnectDragSignals);
f03fc89f 820
0a164d4c 821 m_waiting = true;
22d5903e 822
4ba47b40 823 GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
8ee9d618 824
a3e7d24d 825 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
b068c4e8 826 m_data->GetAllFormats( array );
2245b2b2
VZ
827 size_t count = m_data->GetFormatCount();
828 for (size_t i = 0; i < count; i++)
a3e7d24d
RR
829 {
830 GdkAtom atom = array[i];
98528e9b 831#ifdef __WXDEBUG__
b2dea2ab 832 wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ));
98528e9b
JJ
833#endif
834 gtk_target_list_add( target_list, atom, 0, 0 );
a3e7d24d
RR
835 }
836 delete[] array;
f03fc89f 837
5e513780
RR
838 int action = GDK_ACTION_COPY;
839 if ( flags & wxDrag_AllowMove )
840 action |= GDK_ACTION_MOVE;
2245b2b2 841
5e513780
RR
842 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
843 // to use a global to pass the flags to the drop target but I'd
844 // surely prefer a better way to do it
845 gs_flagsForDrag = flags;
2245b2b2 846
5e513780 847 GdkDragContext *context = gtk_drag_begin( m_widget,
f6bcfd97 848 target_list,
46c87a9a 849 (GdkDragAction)action,
8f9850dd
RR
850 g_lastButtonNumber, // number of mouse button which started drag
851 (GdkEvent*) g_lastMouseEvent );
8ee9d618 852
86f19f7c
VZ
853 if ( !context )
854 {
855 // this can happen e.g. if gdk_pointer_grab() failed
856 return wxDragError;
857 }
858
5e513780 859 m_dragContext = context;
8ee9d618 860
5e513780 861 PrepareIcon( action, context );
f03fc89f 862
5e513780
RR
863 while (m_waiting)
864 gtk_main_iteration();
f6bcfd97 865
5e513780
RR
866 m_retValue = ConvertFromGTK(context->action);
867 if ( m_retValue == wxDragNone )
868 m_retValue = wxDragCancel;
22d5903e 869
4ba47b40 870 return m_retValue;
22d5903e
RR
871}
872
86f19f7c 873void wxDropSource::GTKConnectDragSignals()
22d5903e 874{
86f19f7c
VZ
875 if (!m_widget)
876 return;
877
878 g_blockEventsOnDrag = true;
f6bcfd97 879
9fa72bd2
MR
880 g_signal_connect (m_widget, "drag_data_get",
881 G_CALLBACK (source_drag_data_get), this);
9fa72bd2
MR
882 g_signal_connect (m_widget, "drag_end",
883 G_CALLBACK (source_drag_end), this);
4ba47b40 884
22d5903e
RR
885}
886
86f19f7c 887void wxDropSource::GTKDisconnectDragSignals()
22d5903e 888{
9fa72bd2
MR
889 if (!m_widget)
890 return;
f03fc89f 891
86f19f7c
VZ
892 g_blockEventsOnDrag = false;
893
9fa72bd2
MR
894 g_signal_handlers_disconnect_by_func (m_widget,
895 (gpointer) source_drag_data_get,
896 this);
9fa72bd2
MR
897 g_signal_handlers_disconnect_by_func (m_widget,
898 (gpointer) source_drag_end,
899 this);
22d5903e
RR
900}
901
ac57418f 902#endif
93c5dd39 903 // wxUSE_DRAG_AND_DROP