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