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