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