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