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