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