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