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