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