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