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