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