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