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