]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dnd.cpp
gtk_pizza_set_external is redundant
[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
9e691f46
VZ
26#include "wx/gtk/private.h"
27
071a2d78 28#include <gdk/gdkprivate.h>
c801d85f 29
071a2d78
RR
30#include <gtk/gtkdnd.h>
31#include <gtk/gtkselection.h>
c801d85f 32
22d5903e
RR
33//----------------------------------------------------------------------------
34// global data
35//----------------------------------------------------------------------------
c801d85f
KB
36
37extern bool g_blockEventsOnDrag;
38
2245b2b2
VZ
39// the flags used for the last DoDragDrop()
40static long gs_flagsForDrag = 0;
41
b2dea2ab
VZ
42// the trace mask we use with wxLogTrace() - call
43// wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here
44// (there are quite a few of them, so don't enable this by default)
45static const wxChar *TRACE_DND = _T("dnd");
46
22d5903e
RR
47//----------------------------------------------------------------------------
48// standard icons
49//----------------------------------------------------------------------------
50
13b22a67 51/* Copyright (c) Julian Smart */
90350682 52static const char * page_xpm[] = {
13b22a67
JS
53/* columns rows colors chars-per-pixel */
54"32 32 37 1",
55"5 c #7198D9",
56", c #769CDA",
57"2 c #DCE6F6",
58"i c #FFFFFF",
59"e c #779DDB",
60": c #9AB6E4",
61"9 c #EAF0FA",
62"- c #B1C7EB",
63"$ c #6992D7",
64"y c #F7F9FD",
65"= c #BED0EE",
66"q c #F0F5FC",
67"; c #A8C0E8",
68"@ c #366BC2",
69" c None",
70"u c #FDFEFF",
71"8 c #5987D3",
72"* c #C4D5F0",
73"7 c #7CA0DC",
74"O c #487BCE",
75"< c #6B94D7",
76"& c #CCDAF2",
77"> c #89A9DF",
78"3 c #5584D1",
79"w c #82A5DE",
80"1 c #3F74CB",
81"+ c #3A70CA",
82". c #3569BF",
83"% c #D2DFF4",
84"# c #3366BB",
85"r c #F5F8FD",
86"0 c #FAFCFE",
87"4 c #DFE8F7",
88"X c #5E8AD4",
89"o c #5282D0",
90"t c #B8CCEC",
91"6 c #E5EDF9",
22d5903e 92/* pixels */
13b22a67
JS
93" ",
94" ",
95" ",
96" ",
97" ",
98" .XXXooOO++@# ",
99" $%&*=-;::>,<1 ",
100" $2%&*=-;::><:3 ",
101" $42%&*=-;::<&:3 ",
102" 56477<<<<8<<9&:X ",
103" 59642%&*=-;<09&:5 ",
104" 5q9642%&*=-<<<<<# ",
105" 5qqw777<<<<<88:>+ ",
106" erqq9642%&*=t;::+ ",
107" eyrqq9642%&*=t;:O ",
108" eyywwww777<<<<t;O ",
109" e0yyrqq9642%&*=to ",
110" e00yyrqq9642%&*=o ",
111" eu0wwwwwww777<&*X ",
112" euu00yyrqq9642%&X ",
113" eiuu00yyrqq9642%X ",
114" eiiwwwwwwwwww742$ ",
115" eiiiuu00yyrqq964$ ",
116" eiiiiuu00yyrqq96$ ",
117" eiiiiiuu00yyrqq95 ",
118" eiiiiiiuu00yyrqq5 ",
119" eeeeeeeeeeeeee55e ",
120" ",
121" ",
122" ",
123" ",
124" "
125};
f03fc89f
VZ
126
127
2245b2b2
VZ
128// ============================================================================
129// private functions
130// ============================================================================
131
132// ----------------------------------------------------------------------------
77ffb593 133// convert between GTK+ and wxWidgets DND constants
2245b2b2
VZ
134// ----------------------------------------------------------------------------
135
136static wxDragResult ConvertFromGTK(long action)
137{
138 switch ( action )
139 {
140 case GDK_ACTION_COPY:
141 return wxDragCopy;
142
143 case GDK_ACTION_LINK:
144 return wxDragLink;
145
146 case GDK_ACTION_MOVE:
147 return wxDragMove;
148 }
149
150 return wxDragNone;
151}
4ba47b40 152
33a5bc52
RR
153// ----------------------------------------------------------------------------
154// "drag_leave"
155// ----------------------------------------------------------------------------
156
865bb325 157extern "C" {
33a5bc52 158static void target_drag_leave( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
159 GdkDragContext *context,
160 guint WXUNUSED(time),
161 wxDropTarget *drop_target )
33a5bc52 162{
5549fa65
RR
163 if (g_isIdle) wxapp_install_idle_handler();
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{
5549fa65
RR
193 if (g_isIdle) wxapp_install_idle_handler();
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{
5549fa65
RR
291 if (g_isIdle) wxapp_install_idle_handler();
292
829e3e8d
RR
293 /* Owen Taylor: "if the drop is not in a drop zone,
294 return FALSE, otherwise, if you aren't accepting
295 the drop, call gtk_drag_finish() with success == FALSE
296 otherwise call gtk_drag_data_get()" */
4ba47b40
RR
297
298// printf( "drop.\n" );
f03fc89f 299
829e3e8d
RR
300 /* this seems to make a difference between not accepting
301 due to wrong target area and due to wrong format. let
302 us hope that this is not required.. */
f03fc89f 303
829e3e8d
RR
304 /* inform the wxDropTarget about the current GdkDragContext.
305 this is only valid for the duration of this call */
306 drop_target->SetDragContext( context );
f03fc89f 307
829e3e8d
RR
308 /* inform the wxDropTarget about the current drag widget.
309 this is only valid for the duration of this call */
310 drop_target->SetDragWidget( widget );
f03fc89f 311
829e3e8d
RR
312 /* inform the wxDropTarget about the current drag time.
313 this is only valid for the duration of this call */
314 drop_target->SetDragTime( time );
f03fc89f 315
bd77da97
RR
316/*
317 wxDragResult result = wxDragMove;
318 if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy;
319*/
320
513abb88
RR
321 /* reset the block here as someone might very well
322 show a dialog as a reaction to a drop and this
323 wouldn't work without events */
0a164d4c 324 g_blockEventsOnDrag = false;
b2dea2ab 325
829e3e8d 326 bool ret = drop_target->OnDrop( x, y );
f03fc89f 327
5af019af 328 if (!ret)
829e3e8d 329 {
b2dea2ab 330 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") );
8ee9d618 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 {
670f9935 340 wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") );
8ee9d618 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{
5549fa65
RR
394 if (g_isIdle) wxapp_install_idle_handler();
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
b2dea2ab 408 wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") );
8ee9d618 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 {
670f9935 418 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") );
8ee9d618 419
3103e8a9 420 /* tell GTK that data transfer was successful */
ab8884ac 421 gtk_drag_finish( context, TRUE, FALSE, time );
33a5bc52 422 }
5af019af
RR
423 else
424 {
b2dea2ab 425 wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") );
8ee9d618 426
3103e8a9 427 /* tell GTK that data transfer was not successful */
5af019af
RR
428 gtk_drag_finish( context, FALSE, FALSE, time );
429 }
f03fc89f 430
5af019af
RR
431 /* after this, invalidate the drop_target's drag data */
432 drop_target->SetDragData( (GtkSelectionData*) NULL );
33a5bc52 433}
865bb325 434}
33a5bc52 435
4ba47b40 436//----------------------------------------------------------------------------
33a5bc52 437// wxDropTarget
4ba47b40 438//----------------------------------------------------------------------------
33a5bc52 439
8ee9d618
VZ
440wxDropTarget::wxDropTarget( wxDataObject *data )
441 : wxDropTargetBase( data )
f5368809 442{
0a164d4c 443 m_firstMotion = true;
829e3e8d
RR
444 m_dragContext = (GdkDragContext*) NULL;
445 m_dragWidget = (GtkWidget*) NULL;
5af019af 446 m_dragData = (GtkSelectionData*) NULL;
829e3e8d 447 m_dragTime = 0;
f5368809
RR
448}
449
c9057ae1
VZ
450wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
451 wxCoord WXUNUSED(y),
452 wxDragResult def )
d6086ea6 453{
8ee9d618 454 // GetMatchingPair() checks for m_dataObject too, no need to do it here
2edc8f5b
VZ
455
456 // disable the debug message from GetMatchingPair() - there are too many
457 // of them otherwise
c4fda16b 458#ifdef __WXDEBUG__
2edc8f5b
VZ
459 wxLogNull noLog;
460#endif // Debug
461
c9057ae1 462 return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone;
d6086ea6
RR
463}
464
c9057ae1 465bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
d6086ea6 466{
b068c4e8 467 if (!m_dataObject)
0a164d4c 468 return false;
8ee9d618 469
8e00741d 470 return (GetMatchingPair() != (GdkAtom) 0);
d6086ea6
RR
471}
472
8ee9d618
VZ
473wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
474 wxDragResult def )
5af019af 475{
b068c4e8 476 if (!m_dataObject)
11e1c70d 477 return wxDragNone;
8ee9d618 478
8e00741d 479 if (GetMatchingPair() == (GdkAtom) 0)
11e1c70d 480 return wxDragNone;
8ee9d618
VZ
481
482 return GetData() ? def : wxDragNone;
5af019af
RR
483}
484
8e00741d 485GdkAtom wxDropTarget::GetMatchingPair()
5af019af 486{
8ee9d618 487 if (!m_dataObject)
8e00741d 488 return (GdkAtom) 0;
5af019af 489
8ee9d618 490 if (!m_dragContext)
8e00741d 491 return (GdkAtom) 0;
f03fc89f 492
22d5903e
RR
493 GList *child = m_dragContext->targets;
494 while (child)
495 {
68567a96 496 GdkAtom formatAtom = (GdkAtom)(child->data);
2edc8f5b 497 wxDataFormat format( formatAtom );
f03fc89f 498
a3e7d24d 499#ifdef __WXDEBUG__
b2dea2ab
VZ
500 wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"),
501 format.GetId().c_str());
2edc8f5b
VZ
502#endif // Debug
503
b068c4e8 504 if (m_dataObject->IsSupportedFormat( format ))
2edc8f5b 505 return formatAtom;
f03fc89f 506
22d5903e
RR
507 child = child->next;
508 }
829e3e8d 509
8e00741d 510 return (GdkAtom) 0;
d6086ea6 511}
f03fc89f 512
8e00741d 513bool wxDropTarget::GetData()
d6086ea6 514{
8ee9d618 515 if (!m_dragData)
0a164d4c 516 return false;
f03fc89f 517
8ee9d618 518 if (!m_dataObject)
0a164d4c 519 return false;
f03fc89f 520
8e00741d 521 wxDataFormat dragFormat( m_dragData->target );
8ee9d618 522
b068c4e8 523 if (!m_dataObject->IsSupportedFormat( dragFormat ))
0a164d4c 524 return false;
f03fc89f 525
97c79de2
RR
526 m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data );
527
0a164d4c 528 return true;
d6086ea6 529}
f03fc89f 530
f5368809
RR
531void wxDropTarget::UnregisterWidget( GtkWidget *widget )
532{
223d09f6 533 wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") );
f03fc89f 534
829e3e8d 535 gtk_drag_dest_unset( widget );
f03fc89f 536
9fa72bd2
MR
537 g_signal_handlers_disconnect_by_func (widget,
538 (gpointer) target_drag_leave, this);
539 g_signal_handlers_disconnect_by_func (widget,
540 (gpointer) target_drag_motion, this);
541 g_signal_handlers_disconnect_by_func (widget,
542 (gpointer) target_drag_drop, this);
543 g_signal_handlers_disconnect_by_func (widget,
544 (gpointer) target_drag_data_received, this);
f5368809
RR
545}
546
547void wxDropTarget::RegisterWidget( GtkWidget *widget )
548{
223d09f6 549 wxCHECK_RET( widget != NULL, wxT("register widget is NULL") );
f03fc89f 550
829e3e8d
RR
551 /* gtk_drag_dest_set() determines what default behaviour we'd like
552 GTK to supply. we don't want to specify out targets (=formats)
553 or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and
554 not GTK_DEST_DEFAULT_DROP). instead we react individually to
555 "drag_motion" and "drag_drop" events. this makes it possible
f03fc89f 556 to allow dropping on only a small area. we should set
829e3e8d
RR
557 GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice
558 highlighting if dragging over standard controls, but this
559 seems to be broken without the other two. */
f03fc89f 560
d6086ea6 561 gtk_drag_dest_set( widget,
f03fc89f
VZ
562 (GtkDestDefaults) 0, /* no default behaviour */
563 (GtkTargetEntry*) NULL, /* we don't supply any formats here */
564 0, /* number of targets = 0 */
565 (GdkDragAction) 0 ); /* we don't supply any actions here */
566
9fa72bd2
MR
567 g_signal_connect (widget, "drag_leave",
568 G_CALLBACK (target_drag_leave), this);
33a5bc52 569
9fa72bd2
MR
570 g_signal_connect (widget, "drag_motion",
571 G_CALLBACK (target_drag_motion), this);
33a5bc52 572
9fa72bd2
MR
573 g_signal_connect (widget, "drag_drop",
574 G_CALLBACK (target_drag_drop), this);
33a5bc52 575
9fa72bd2
MR
576 g_signal_connect (widget, "drag_data_received",
577 G_CALLBACK (target_drag_data_received), this);
f5368809
RR
578}
579
4ba47b40
RR
580//----------------------------------------------------------------------------
581// "drag_data_get"
582//----------------------------------------------------------------------------
583
865bb325 584extern "C" {
f03fc89f 585static void
4ba47b40 586source_drag_data_get (GtkWidget *WXUNUSED(widget),
b02da6b1 587 GdkDragContext *WXUNUSED(context),
f03fc89f
VZ
588 GtkSelectionData *selection_data,
589 guint WXUNUSED(info),
590 guint WXUNUSED(time),
591 wxDropSource *drop_source )
4ba47b40 592{
5549fa65
RR
593 if (g_isIdle) wxapp_install_idle_handler();
594
97c79de2 595 wxDataFormat format( selection_data->target );
8ee9d618 596
b2dea2ab
VZ
597 wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"),
598 format.GetId().c_str());
f6bcfd97 599
1dd989e1 600 drop_source->m_retValue = wxDragCancel;
8ee9d618 601
97c79de2 602 wxDataObject *data = drop_source->GetDataObject();
f6bcfd97 603
1dd989e1 604 if (!data)
97c79de2 605 {
b2dea2ab 606 wxLogTrace(TRACE_DND, wxT("Drop source: no data object") );
2edc8f5b 607 return;
97c79de2 608 }
1dd989e1 609
97c79de2
RR
610 if (!data->IsSupportedFormat(format))
611 {
b2dea2ab 612 wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") );
2edc8f5b 613 return;
97c79de2 614 }
f03fc89f 615
97c79de2
RR
616 if (data->GetDataSize(format) == 0)
617 {
b2dea2ab 618 wxLogTrace(TRACE_DND, wxT("Drop source: empty data") );
2edc8f5b 619 return;
97c79de2 620 }
8ee9d618 621
97c79de2 622 size_t size = data->GetDataSize(format);
f03fc89f 623
1dd989e1 624// printf( "data size: %d.\n", (int)data_size );
f03fc89f 625
1dd989e1 626 guchar *d = new guchar[size];
8ee9d618 627
97c79de2 628 if (!data->GetDataHere( format, (void*)d ))
1dd989e1 629 {
97c79de2 630 delete[] d;
2edc8f5b 631 return;
1dd989e1 632 }
f03fc89f 633
b453e1b2 634#if wxUSE_THREADS
1dd989e1 635 /* disable GUI threads */
b453e1b2 636#endif
8e193f38 637
0a164d4c
WS
638 gtk_selection_data_set( selection_data,
639 selection_data->target,
640 8, // 8-bit
641 d,
642 size );
f03fc89f 643
b453e1b2 644#if wxUSE_THREADS
1dd989e1 645 /* enable GUI threads */
b453e1b2 646#endif
f03fc89f 647
97c79de2 648 delete[] d;
4ba47b40 649}
865bb325 650}
f03fc89f 651
4ba47b40
RR
652//----------------------------------------------------------------------------
653// "drag_data_delete"
654//----------------------------------------------------------------------------
655
865bb325 656extern "C" {
9f39393d
VZ
657static void source_drag_data_delete( GtkWidget *WXUNUSED(widget),
658 GdkDragContext *context,
659 wxDropSource *WXUNUSED(drop_source) )
4ba47b40 660{
f6bcfd97 661 if (g_isIdle)
13fb7b7a 662 wxapp_install_idle_handler();
33a5bc52 663
13fb7b7a 664 // printf( "Drag source: drag_data_delete\n" );
4ba47b40 665}
865bb325 666}
f03fc89f 667
4ba47b40
RR
668//----------------------------------------------------------------------------
669// "drag_begin"
670//----------------------------------------------------------------------------
671
865bb325 672extern "C" {
4ba47b40 673static void source_drag_begin( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
674 GdkDragContext *WXUNUSED(context),
675 wxDropSource *WXUNUSED(drop_source) )
4ba47b40 676{
f6bcfd97 677 if (g_isIdle)
13fb7b7a 678 wxapp_install_idle_handler();
5549fa65 679
13fb7b7a 680 // printf( "Drag source: drag_begin.\n" );
4ba47b40 681}
865bb325 682}
f03fc89f 683
4ba47b40
RR
684//----------------------------------------------------------------------------
685// "drag_end"
686//----------------------------------------------------------------------------
687
865bb325 688extern "C" {
4ba47b40 689static void source_drag_end( GtkWidget *WXUNUSED(widget),
f03fc89f
VZ
690 GdkDragContext *WXUNUSED(context),
691 wxDropSource *drop_source )
4ba47b40 692{
5549fa65
RR
693 if (g_isIdle) wxapp_install_idle_handler();
694
13fb7b7a 695 // printf( "Drag source: drag_end.\n" );
4ba47b40 696
0a164d4c 697 drop_source->m_waiting = false;
4ba47b40 698}
865bb325 699}
f03fc89f 700
7b5d5699
RR
701//-----------------------------------------------------------------------------
702// "configure_event" from m_iconWindow
703//-----------------------------------------------------------------------------
704
865bb325 705extern "C" {
8ee9d618 706static gint
7b5d5699
RR
707gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
708{
14819684 709 // don't need to install idle handler, its done from "event" signal
7b5d5699 710
2245b2b2 711 source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) );
8ee9d618 712
7b5d5699
RR
713 return 0;
714}
865bb325 715}
7b5d5699 716
4ba47b40
RR
717//---------------------------------------------------------------------------
718// wxDropSource
719//---------------------------------------------------------------------------
22d5903e 720
f6bcfd97
BP
721wxDropSource::wxDropSource(wxWindow *win,
722 const wxIcon &iconCopy,
723 const wxIcon &iconMove,
724 const wxIcon &iconNone)
22d5903e 725{
0a164d4c 726 m_waiting = true;
f03fc89f 727
7b5d5699 728 m_iconWindow = (GtkWidget*) NULL;
8ee9d618 729
22d5903e 730 m_window = win;
a2053b27
RR
731 m_widget = win->m_widget;
732 if (win->m_wxwindow) m_widget = win->m_wxwindow;
f03fc89f 733
22d5903e
RR
734 m_retValue = wxDragCancel;
735
f6bcfd97 736 SetIcons(iconCopy, iconMove, iconNone);
22d5903e
RR
737}
738
f6bcfd97
BP
739wxDropSource::wxDropSource(wxDataObject& data,
740 wxWindow *win,
741 const wxIcon &iconCopy,
742 const wxIcon &iconMove,
743 const wxIcon &iconNone)
22d5903e 744{
0a164d4c 745 m_waiting = true;
8ee9d618 746
b068c4e8 747 SetData( data );
f03fc89f 748
7b5d5699 749 m_iconWindow = (GtkWidget*) NULL;
8ee9d618 750
22d5903e 751 m_window = win;
a2053b27
RR
752 m_widget = win->m_widget;
753 if (win->m_wxwindow) m_widget = win->m_wxwindow;
8ee9d618 754
22d5903e 755 m_retValue = wxDragCancel;
f03fc89f 756
f6bcfd97
BP
757 SetIcons(iconCopy, iconMove, iconNone);
758}
759
760void wxDropSource::SetIcons(const wxIcon &iconCopy,
761 const wxIcon &iconMove,
762 const wxIcon &iconNone)
763{
764 m_iconCopy = iconCopy;
765 m_iconMove = iconMove;
766 m_iconNone = iconNone;
767
768 if ( !m_iconCopy.Ok() )
769 m_iconCopy = wxIcon(page_xpm);
770 if ( !m_iconMove.Ok() )
771 m_iconMove = m_iconCopy;
772 if ( !m_iconNone.Ok() )
773 m_iconNone = m_iconCopy;
22d5903e
RR
774}
775
8e193f38 776wxDropSource::~wxDropSource()
22d5903e 777{
22d5903e 778}
f03fc89f 779
f6bcfd97 780void wxDropSource::PrepareIcon( int action, GdkDragContext *context )
7b5d5699 781{
f6bcfd97
BP
782 // get the right icon to display
783 wxIcon *icon = NULL;
784 if ( action & GDK_ACTION_MOVE )
785 icon = &m_iconMove;
786 else if ( action & GDK_ACTION_COPY )
787 icon = &m_iconCopy;
788 else
789 icon = &m_iconNone;
790
791 GdkBitmap *mask;
792 if ( icon->GetMask() )
793 mask = icon->GetMask()->GetBitmap();
794 else
795 mask = (GdkBitmap *)NULL;
796
797 GdkPixmap *pixmap = icon->GetPixmap();
7b5d5699
RR
798
799 gint width,height;
791d7ea2 800 gdk_drawable_get_size (pixmap, &width, &height);
7b5d5699
RR
801
802 GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
7b5d5699
RR
803 gtk_widget_push_colormap (colormap);
804
805 m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
806 gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
807 gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE);
808
7b5d5699
RR
809 gtk_widget_pop_colormap ();
810
370dc79c 811 gtk_widget_set_size_request (m_iconWindow, width, height);
7b5d5699
RR
812 gtk_widget_realize (m_iconWindow);
813
9fa72bd2
MR
814 g_signal_connect (m_iconWindow, "configure_event",
815 G_CALLBACK (gtk_dnd_window_configure_callback), this);
8ee9d618 816
7b5d5699 817 gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
8ee9d618 818
7b5d5699
RR
819 if (mask)
820 gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
821
f6bcfd97 822 gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 );
7b5d5699
RR
823}
824
2245b2b2 825wxDragResult wxDropSource::DoDragDrop(int flags)
22d5903e 826{
2245b2b2
VZ
827 wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone,
828 wxT("Drop source: no data") );
8ee9d618 829
513abb88
RR
830 // still in drag
831 if (g_blockEventsOnDrag)
2245b2b2 832 return wxDragNone;
b2dea2ab 833
f6bcfd97 834 // disabled for now
0a164d4c 835 g_blockEventsOnDrag = true;
f03fc89f 836
4ba47b40 837 RegisterWindow();
f03fc89f 838
0a164d4c 839 m_waiting = true;
22d5903e 840
4ba47b40 841 GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
8ee9d618 842
a3e7d24d 843 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
b068c4e8 844 m_data->GetAllFormats( array );
2245b2b2
VZ
845 size_t count = m_data->GetFormatCount();
846 for (size_t i = 0; i < count; i++)
a3e7d24d
RR
847 {
848 GdkAtom atom = array[i];
b2dea2ab 849 wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ));
a3e7d24d
RR
850 gtk_target_list_add( target_list, atom, 0, 0 );
851 }
852 delete[] array;
f03fc89f 853
4ba47b40
RR
854 GdkEventMotion event;
855 event.window = m_widget->window;
856 int x = 0;
857 int y = 0;
858 GdkModifierType state;
859 gdk_window_get_pointer( event.window, &x, &y, &state );
860 event.x = x;
861 event.y = y;
862 event.state = state;
b02da6b1 863 event.time = (guint32)GDK_CURRENT_TIME;
f03fc89f 864
4ba47b40
RR
865 /* GTK wants to know which button was pressed which caused the dragging */
866 int button_number = 0;
867 if (event.state & GDK_BUTTON1_MASK) button_number = 1;
868 else if (event.state & GDK_BUTTON2_MASK) button_number = 2;
869 else if (event.state & GDK_BUTTON3_MASK) button_number = 3;
870
b453e1b2
RR
871#if wxUSE_THREADS
872 /* disable GUI threads */
b453e1b2 873#endif
8e193f38 874
4ba47b40
RR
875 /* don't start dragging if no button is down */
876 if (button_number)
22d5903e 877 {
9f39393d 878 int action = GDK_ACTION_COPY;
2245b2b2 879 if ( flags & wxDrag_AllowMove )
9f39393d 880 action |= GDK_ACTION_MOVE;
2245b2b2
VZ
881
882 // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad
883 // to use a global to pass the flags to the drop target but I'd
884 // surely prefer a better way to do it
885 gs_flagsForDrag = flags;
886
4ba47b40 887 GdkDragContext *context = gtk_drag_begin( m_widget,
f6bcfd97 888 target_list,
46c87a9a 889 (GdkDragAction)action,
f6bcfd97
BP
890 button_number, /* number of mouse button which started drag */
891 (GdkEvent*) &event );
8ee9d618
VZ
892
893 m_dragContext = context;
894
f6bcfd97 895 PrepareIcon( action, context );
f03fc89f 896
2245b2b2
VZ
897 while (m_waiting)
898 gtk_main_iteration();
f6bcfd97 899
2245b2b2
VZ
900 m_retValue = ConvertFromGTK(context->action);
901 if ( m_retValue == wxDragNone )
902 m_retValue = wxDragCancel;
4ba47b40 903 }
22d5903e 904
b453e1b2
RR
905#if wxUSE_THREADS
906 /* re-enable GUI threads */
b453e1b2 907#endif
8e193f38 908
0a164d4c 909 g_blockEventsOnDrag = false;
f03fc89f 910
4ba47b40
RR
911 UnregisterWindow();
912
90527a50
VZ
913 // this shouldn't be needed but somehow, sometimes, without this the cursor
914 // stays grabbed even when the DND operation ends and the application
915 // becomes unresponsive and has to be killed resulting in loss of all
916 // unsaved data, so while this fix is ugly it's still better than
917 // alternative
918 if ( gdk_pointer_is_grabbed() )
919 gdk_pointer_ungrab(GDK_CURRENT_TIME);
920
4ba47b40 921 return m_retValue;
22d5903e
RR
922}
923
4ba47b40 924void wxDropSource::RegisterWindow()
22d5903e 925{
4ba47b40 926 if (!m_widget) return;
f6bcfd97 927
9fa72bd2
MR
928 g_signal_connect (m_widget, "drag_data_get",
929 G_CALLBACK (source_drag_data_get), this);
930 g_signal_connect (m_widget, "drag_data_delete",
931 G_CALLBACK (source_drag_data_delete), this);
932 g_signal_connect (m_widget, "drag_begin",
933 G_CALLBACK (source_drag_begin), this);
934 g_signal_connect (m_widget, "drag_end",
935 G_CALLBACK (source_drag_end), this);
4ba47b40 936
22d5903e
RR
937}
938
4ba47b40 939void wxDropSource::UnregisterWindow()
22d5903e 940{
9fa72bd2
MR
941 if (!m_widget)
942 return;
f03fc89f 943
9fa72bd2
MR
944 g_signal_handlers_disconnect_by_func (m_widget,
945 (gpointer) source_drag_data_get,
946 this);
947 g_signal_handlers_disconnect_by_func (m_widget,
948 (gpointer) source_drag_data_delete,
949 this);
950 g_signal_handlers_disconnect_by_func (m_widget,
951 (gpointer) source_drag_begin,
952 this);
953 g_signal_handlers_disconnect_by_func (m_widget,
954 (gpointer) source_drag_end,
955 this);
22d5903e
RR
956}
957
ac57418f 958#endif
93c5dd39 959 // wxUSE_DRAG_AND_DROP