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