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