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