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