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