]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dnd.cpp
OnExit() is called for modules which were initialized even if the init of
[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
RR
6// Copyright: (c) 1998 Robert Roebling
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
28#include <X11/Xlib.h>
29
30// ----------------------------------------------------------------------------
31// global
32// ----------------------------------------------------------------------------
33
34extern bool g_blockEventsOnDrag;
35
f5368809
RR
36#ifdef NEW_GTK_DND_CODE
37
33a5bc52
RR
38#include "gtk/gtkdnd.h"
39#include "gtk/gtkselection.h"
40
41// ----------------------------------------------------------------------------
42// "drag_leave"
43// ----------------------------------------------------------------------------
44
45static void target_drag_leave( GtkWidget *WXUNUSED(widget),
46 GdkDragContext *WXUNUSED(context),
47 guint WXUNUSED(time) )
48{
49 printf( "leave.\n" );
50}
51
52// ----------------------------------------------------------------------------
53// "drag_motion"
54// ----------------------------------------------------------------------------
55
56static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget),
57 GdkDragContext *context,
58 gint WXUNUSED(x),
59 gint WXUNUSED(y),
60 guint time )
61{
62 printf( "motion.\n" );
63 gdk_drag_status( context, context->suggested_action, time );
64 return TRUE;
65}
66
67// ----------------------------------------------------------------------------
68// "drag_drop"
69// ----------------------------------------------------------------------------
70
71static gboolean target_drag_drop( GtkWidget *widget,
72 GdkDragContext *context,
73 gint x,
74 gint y,
75 guint time )
76{
77 printf( "drop at: %d,%d.\n", x, y );
78
79 if (context->targets)
80 {
81 gtk_drag_get_data( widget,
82 context,
83 GPOINTER_TO_INT (context->targets->data),
84 time );
85 }
86 return FALSE;
87}
88
89// ----------------------------------------------------------------------------
90// "drag_data_received"
91// ----------------------------------------------------------------------------
92
93static void target_drag_data_received( GtkWidget *WXUNUSED(widget),
94 GdkDragContext *context,
95 gint x,
96 gint y,
97 GtkSelectionData *data,
98 guint WXUNUSED(info),
99 guint time )
100{
101 printf( "data receive at: %d,%d.\n", x, y );
102
103 if ((data->length >= 0) && (data->format == 8))
104 {
ab8884ac
RR
105 wxString str = (const char*)data->data;
106 printf( "Received %s\n.", WXSTRINGCAST str );
107 gtk_drag_finish( context, TRUE, FALSE, time );
108 return;
33a5bc52
RR
109 }
110
111 gtk_drag_finish (context, FALSE, FALSE, time);
112}
113
114// ----------------------------------------------------------------------------
115// wxDropTarget
116// ----------------------------------------------------------------------------
117
f5368809
RR
118wxDropTarget::wxDropTarget()
119{
120}
121
122wxDropTarget::~wxDropTarget()
123{
124}
125
126void wxDropTarget::UnregisterWidget( GtkWidget *widget )
127{
33a5bc52 128 wxCHECK_RET( widget != NULL, "unregister widget is NULL" );
f5368809 129
33a5bc52
RR
130 gtk_drag_dest_set( widget,
131 (GtkDestDefaults) 0,
132 (GtkTargetEntry*) NULL,
133 0,
134 (GdkDragAction) 0 );
135
136 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
137 GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this );
138
139 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
140 GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this );
141
142 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
143 GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this );
144
145 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
146 GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this );
f5368809
RR
147}
148
149void wxDropTarget::RegisterWidget( GtkWidget *widget )
150{
33a5bc52 151 wxCHECK_RET( widget != NULL, "register widget is NULL" );
f5368809 152
33a5bc52
RR
153 GtkTargetEntry format;
154 format.info = 0;
155 format.flags = 0;
ab8884ac 156 char buf[100];
f5368809 157
33a5bc52 158 int valid = 0;
f5368809
RR
159 for ( size_t i = 0; i < GetFormatCount(); i++ )
160 {
161 wxDataFormat df = GetFormat( i );
162 switch (df)
163 {
164 case wxDF_TEXT:
33a5bc52 165 format.target = "text/plain";
f5368809
RR
166 valid++;
167 break;
168 case wxDF_FILENAME:
33a5bc52 169 format.target = "file:ALL";
f5368809
RR
170 valid++;
171 break;
ab8884ac
RR
172 case wxDF_PRIVATE:
173 wxPrivateDropTarget *pdt = (wxPrivateDropTarget *)this;
174 strcpy( buf, "applications/" );
175 strcat( buf, WXSTRINGCAST pdt->GetID() );
176 format.target = buf;
177 valid++;
f5368809
RR
178 default:
179 break;
180 }
181 }
182
33a5bc52 183 wxASSERT_MSG( valid != 0, "No valid DnD format supported." );
f5368809 184
33a5bc52
RR
185 gtk_drag_dest_set( widget,
186 GTK_DEST_DEFAULT_ALL,
187 &format,
188 1,
189 (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE) );
190
191 gtk_signal_connect( GTK_OBJECT(widget), "drag_leave",
192 GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this );
193
194 gtk_signal_connect( GTK_OBJECT(widget), "drag_motion",
195 GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this );
196
197 gtk_signal_connect( GTK_OBJECT(widget), "drag_drop",
198 GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this );
199
200 gtk_signal_connect( GTK_OBJECT(widget), "drag_data_received",
201 GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this );
f5368809
RR
202}
203
204// ----------------------------------------------------------------------------
205// wxTextDropTarget
206// ----------------------------------------------------------------------------
207
208bool wxTextDropTarget::OnDrop( long x, long y, const void *data, size_t WXUNUSED(size) )
209{
210 OnDropText( x, y, (const char*)data );
211 return TRUE;
212}
213
214bool wxTextDropTarget::OnDropText( long x, long y, const char *psz )
215{
216 printf( "Got dropped text: %s.\n", psz );
217 printf( "At x: %d, y: %d.\n", (int)x, (int)y );
218 return TRUE;
219}
220
221size_t wxTextDropTarget::GetFormatCount() const
222{
223 return 1;
224}
225
226wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const
227{
228 return wxDF_TEXT;
229}
230
231// ----------------------------------------------------------------------------
232// wxFileDropTarget
233// ----------------------------------------------------------------------------
234
235bool wxFileDropTarget::OnDropFiles( long x, long y, size_t nFiles, const char * const aszFiles[] )
236{
237 printf( "Got %d dropped files.\n", (int)nFiles );
238 printf( "At x: %d, y: %d.\n", (int)x, (int)y );
239 for (size_t i = 0; i < nFiles; i++)
240 {
241 printf( aszFiles[i] );
242 printf( "\n" );
243 }
244 return TRUE;
245}
246
247bool wxFileDropTarget::OnDrop(long x, long y, const void *data, size_t size )
248{
249 size_t number = 0;
250 char *text = (char*) data;
251 for (size_t i = 0; i < size; i++)
252 if (text[i] == 0) number++;
253
254 if (number == 0) return TRUE;
255
256 char **files = new char*[number];
257
258 text = (char*) data;
259 for (size_t i = 0; i < number; i++)
260 {
261 files[i] = text;
262 int len = strlen( text );
263 text += len+1;
264 }
265
266 bool ret = OnDropFiles( x, y, 1, files );
267
268 free( files );
269
270 return ret;
271}
272
273size_t wxFileDropTarget::GetFormatCount() const
274{
275 return 1;
276}
277
278wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const
279{
280 return wxDF_FILENAME;
281}
282
283//-------------------------------------------------------------------------
284// wxDropSource
285//-------------------------------------------------------------------------
286
287wxDropSource::wxDropSource( wxWindow *win )
288{
289 g_blockEventsOnDrag = TRUE;
290
291 m_window = win;
292 m_widget = win->m_widget;
293 if (win->m_wxwindow) m_widget = win->m_wxwindow;
294
295 m_data = (wxDataObject *) NULL;
296 m_retValue = wxDragCancel;
297
298 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
299 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
300}
301
302wxDropSource::wxDropSource( wxDataObject &data, wxWindow *win )
303{
304 g_blockEventsOnDrag = TRUE;
305
306 m_window = win;
307 m_widget = win->m_widget;
308 if (win->m_wxwindow) m_widget = win->m_wxwindow;
309 m_retValue = wxDragCancel;
310
311 m_data = &data;
312
313 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
314 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
315}
316
317void wxDropSource::SetData( wxDataObject &data )
318{
319 m_data = &data;
320}
321
322wxDropSource::~wxDropSource(void)
323{
324// if (m_data) delete m_data;
325
326 g_blockEventsOnDrag = FALSE;
327}
328
329wxDragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) )
330{
331 wxASSERT_MSG( m_data, "wxDragSource: no data" );
332
333 if (!m_data) return (wxDragResult) wxDragNone;
334 if (m_data->GetDataSize() == 0) return (wxDragResult) wxDragNone;
335
336 RegisterWindow();
337
338 // TODO
339
340 UnregisterWindow();
341
342 g_blockEventsOnDrag = FALSE;
343
344 return m_retValue;
345}
346
347void wxDropSource::RegisterWindow(void)
348{
349 if (!m_data) return;
350
351 wxString formats;
352
353 wxDataFormat df = m_data->GetPreferredFormat();
354
355 switch (df)
356 {
357 case wxDF_TEXT:
358 formats += "text/plain";
359 break;
360 case wxDF_FILENAME:
361 formats += "file:ALL";
362 break;
363 default:
364 break;
365 }
366
367 char *str = WXSTRINGCAST formats;
368
369 // TODO
370}
371
372void wxDropSource::UnregisterWindow(void)
373{
374 if (!m_widget) return;
375
376 // TODO
377}
378
33a5bc52
RR
379
380#else // NEW_CODE
381
a802c3a1
RR
382GtkWidget *shape_create_icon (char **data,
383 gint x,
384 gint y,
385 gint px,
386 gint py,
387 gint window_type);
388
389/* XPM */
390static char * gv_xpm[] = {
391"40 34 3 1",
392" s None c None",
393". c black",
394"X c white",
395" ",
396" ",
397" ...... ",
398" ..XXXXXX.. ",
399" .XXXXXXXXXX. ",
400" .XXXXXXXXXXXX. ",
401" .XXXXXXXXXXXX. ",
402" .XXXXXXXXXXXXXX. ",
403" .XXX..XXXX..XXX. ",
404" ....XX....XX....XX. ",
405" .XXX.XXX..XXXX..XXX.... ",
406" .XXXXXXXXXXXXXXXXXXX.XXX. ",
407" .XXXXXXXXXXXXXXXXXXXXXXXX. ",
408" .XXXXXXXXXXXXXXXXXXXXXXXX. ",
409" ..XXXXXXXXXXXXXXXXXXXXXX. ",
410" .XXXXXXXXXXXXXXXXXX... ",
411" ..XXXXXXXXXXXXXXXX. ",
412" .XXXXXXXXXXXXXXXX. ",
413" .XXXXXXXXXXXXXXXX. ",
414" .XXXXXXXXXXXXXXXXX. ",
415" .XXXXXXXXXXXXXXXXX. ",
416" .XXXXXXXXXXXXXXXXXX. ",
417" .XXXXXXXXXXXXXXXXXXX. ",
418" .XXXXXXXXXXXXXXXXXXXXX. ",
419" .XXXXXXXXXXXXXX.XXXXXXX. ",
420" .XXXXXXX.XXXXXXX.XXXXXXX. ",
421" .XXXXXXXX.XXXXXXX.XXXXXXX. ",
422" .XXXXXXX...XXXXX...XXXXX. ",
423" .XXXXXXX. ..... ..... ",
424" ..XXXX.. ",
425" .... ",
426" ",
427" ",
428" "};
429
430/* XPM */
431static char * page_xpm[] = {
432/* width height ncolors chars_per_pixel */
433"32 32 5 1",
434/* colors */
435" s None c None",
436". c black",
437"X c wheat",
438"o c tan",
439"O c #6699FF",
440/* pixels */
441" ................... ",
442" .XXXXXXXXXXXXXXXXX.. ",
443" .XXXXXXXXXXXXXXXXX.o. ",
444" .XXXXXXXXXXXXXXXXX.oo. ",
445" .XXXXXXXXXXXXXXXXX.ooo. ",
446" .XXXXXXXXXXXXXXXXX.oooo. ",
447" .XXXXXXXXXXXXXXXXX....... ",
448" .XXXXXOOOOOOOOOOXXXooooo. ",
449" .XXXXXXXXXXXXXXXXXXooooo. ",
450" .XXXXXOOOOOOOOOOXXXXXXXX. ",
451" .XXXXXXXXXXXXXXXXXXXXXXX. ",
452" .XXXXXXXOOOOOOOOOXXXXXXX. ",
453" .XXXXXXXXXXXXXXXXXXXXXXX. ",
454" .XXXXXXOOOOOOOOOOXXXXXXX. ",
455" .XXXXXXXXXXXXXXXXXXXXXXX. ",
456" .XXXXXOOOOOOOOOOXXXXXXXX. ",
457" .XXXXXXXXXXXXXXXXXXXXXXX. ",
458" .XXXXXXXOOOOOOOOOXXXXXXX. ",
459" .XXXXXXXXXXXXXXXXXXXXXXX. ",
460" .XXXXXXOOOOOOOOOOXXXXXXX. ",
461" .XXXXXXXXXXXXXXXXXXXXXXX. ",
462" .XXXXXOOOOOOOOOOXXXXXXXX. ",
463" .XXXXXXXXXXXXXXXXXXXXXXX. ",
464" .XXXXXXOOOOOOOOOOXXXXXXX. ",
465" .XXXXXXXXXXXXXXXXXXXXXXX. ",
466" .XXXXXOOOOOOOXXXXXXXXXXX. ",
467" .XXXXXXXXXXXXXXXXXXXXXXX. ",
468" .XXXXXXXXXXXXXXXXXXXXXXX. ",
469" .XXXXXXXXXXXXXXXXXXXXXXX. ",
470" .XXXXXXXXXXXXXXXXXXXXXXX. ",
471" .XXXXXXXXXXXXXXXXXXXXXXX. ",
472" ......................... "};
473
474
b666df2c
RR
475//-----------------------------------------------------------------------------
476// globals
477//-----------------------------------------------------------------------------
478
479wxDropSource *gs_currentDropSource = (wxDropSource*) NULL;
480
481//-----------------------------------------------------------------------------
482// "drop_enter_event"
483//-----------------------------------------------------------------------------
484
485static void gtk_target_enter_callback( GtkWidget *WXUNUSED(widget),
486 GdkEventDropEnter *WXUNUSED(event),
487 wxDropTarget *target )
488{
489 if (target)
490 target->OnEnter();
491}
492
493//-----------------------------------------------------------------------------
494// "drop_leave_event"
495//-----------------------------------------------------------------------------
496
497static void gtk_target_leave_callback( GtkWidget *WXUNUSED(widget),
498 GdkEventDropLeave *WXUNUSED(event),
499 wxDropTarget *target )
500{
501 if (target)
502 target->OnLeave();
503}
504
33a5bc52
RR
505//-----------------------------------------------------------------------------
506// "drop_data_available_event"
507//-----------------------------------------------------------------------------
508
509static void gtk_target_callback( GtkWidget *widget,
510 GdkEventDropDataAvailable *event,
511 wxDropTarget *target )
512{
513 if (target)
514 {
515 int x = 0;
516 int y = 0;
517 gdk_window_get_pointer( widget->window, &x, &y, (GdkModifierType *) NULL );
ab8884ac
RR
518/*
519 printf( "Drop data is of type %s.\n", event->data_type );
520*/
33a5bc52
RR
521 target->OnDrop( x, y, (const void*)event->data, (size_t)event->data_numbytes );
522 }
523
524/*
525 g_free (event->data);
526 g_free (event->data_type);
527*/
528}
f5368809 529
c801d85f
KB
530// ----------------------------------------------------------------------------
531// wxDropTarget
532// ----------------------------------------------------------------------------
533
534wxDropTarget::wxDropTarget()
535{
ff7b1510 536}
c801d85f
KB
537
538wxDropTarget::~wxDropTarget()
539{
ff7b1510 540}
c801d85f 541
c801d85f
KB
542void wxDropTarget::UnregisterWidget( GtkWidget *widget )
543{
33a5bc52 544 if (!widget) return;
e3e65dac 545
33a5bc52
RR
546 gtk_signal_disconnect_by_func( GTK_OBJECT(widget),
547 GTK_SIGNAL_FUNC(gtk_target_callback), (gpointer) this );
548
549 gtk_widget_dnd_drop_set( widget, FALSE, (gchar **) NULL, 0, FALSE );
ff7b1510 550}
c801d85f 551
e3e65dac
RR
552void wxDropTarget::RegisterWidget( GtkWidget *widget )
553{
33a5bc52
RR
554 wxString formats;
555 int valid = 0;
e3e65dac 556
33a5bc52 557 for ( size_t i = 0; i < GetFormatCount(); i++ )
e3e65dac 558 {
33a5bc52
RR
559 wxDataFormat df = GetFormat( i );
560 switch (df)
561 {
562 case wxDF_TEXT:
ab8884ac
RR
563 {
564 if (i > 0) formats += ";";
565 formats += "text/plain";
566 valid++;
567 break;
568 }
569 case wxDF_FILENAME:
570 {
571 if (i > 0) formats += ";";
572 formats += "file:ALL";
573 valid++;
574 break;
575 }
576 case wxDF_PRIVATE:
577 {
578 if (i > 0) formats += ";";
579 wxPrivateDropTarget *pdt = (wxPrivateDropTarget *)this;
580 formats += "applications/";
581 formats += pdt->GetId();
582 valid++;
583 break;
584 }
585 default:
33a5bc52
RR
586 break;
587 }
ff7b1510 588 }
e3e65dac 589
33a5bc52 590 char *str = WXSTRINGCAST formats;
ab8884ac 591
33a5bc52
RR
592 gtk_widget_dnd_drop_set( widget, TRUE, &str, valid, FALSE );
593
594 gtk_signal_connect( GTK_OBJECT(widget), "drop_data_available_event",
595 GTK_SIGNAL_FUNC(gtk_target_callback), (gpointer) this );
b666df2c
RR
596
597 gtk_signal_connect( GTK_OBJECT(widget), "drop_enter_event",
598 GTK_SIGNAL_FUNC(gtk_target_enter_callback), (gpointer) this );
599
600 gtk_signal_connect( GTK_OBJECT(widget), "drop_leave_event",
601 GTK_SIGNAL_FUNC(gtk_target_leave_callback), (gpointer) this );
ff7b1510 602}
e3e65dac 603
c801d85f
KB
604// ----------------------------------------------------------------------------
605// wxTextDropTarget
606// ----------------------------------------------------------------------------
607
dc86cb34 608bool wxTextDropTarget::OnDrop( long x, long y, const void *data, size_t WXUNUSED(size) )
c801d85f 609{
33a5bc52
RR
610 OnDropText( x, y, (const char*)data );
611 return TRUE;
ff7b1510 612}
c801d85f
KB
613
614bool wxTextDropTarget::OnDropText( long x, long y, const char *psz )
615{
ab8884ac 616/*
33a5bc52
RR
617 printf( "Got dropped text: %s.\n", psz );
618 printf( "At x: %d, y: %d.\n", (int)x, (int)y );
ab8884ac 619*/
33a5bc52 620 return TRUE;
ff7b1510 621}
c801d85f 622
e3e65dac 623size_t wxTextDropTarget::GetFormatCount() const
c801d85f 624{
33a5bc52 625 return 1;
e3e65dac
RR
626}
627
628wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const
629{
33a5bc52 630 return wxDF_TEXT;
e3e65dac
RR
631}
632
ab8884ac
RR
633// ----------------------------------------------------------------------------
634// wxPrivateDropTarget
635// ----------------------------------------------------------------------------
636
637wxPrivateDropTarget::wxPrivateDropTarget()
638{
639 m_id = wxTheApp->GetAppName();
640}
641
642size_t wxPrivateDropTarget::GetFormatCount() const
643{
644 return 1;
645}
646
647wxDataFormat wxPrivateDropTarget::GetFormat(size_t WXUNUSED(n)) const
648{
649 return wxDF_PRIVATE;
650}
651
e3e65dac
RR
652// ----------------------------------------------------------------------------
653// wxFileDropTarget
654// ----------------------------------------------------------------------------
655
e5403d7c 656bool wxFileDropTarget::OnDropFiles( long x, long y, size_t nFiles, const char * const aszFiles[] )
e3e65dac 657{
33a5bc52
RR
658 printf( "Got %d dropped files.\n", (int)nFiles );
659 printf( "At x: %d, y: %d.\n", (int)x, (int)y );
ab8884ac 660
33a5bc52
RR
661 for (size_t i = 0; i < nFiles; i++)
662 {
663 printf( aszFiles[i] );
664 printf( "\n" );
665 }
ab8884ac 666
33a5bc52 667 return TRUE;
e3e65dac
RR
668}
669
dc86cb34 670bool wxFileDropTarget::OnDrop(long x, long y, const void *data, size_t size )
e3e65dac 671{
33a5bc52
RR
672 size_t number = 0;
673 char *text = (char*) data;
674 for (size_t i = 0; i < size; i++)
675 if (text[i] == 0) number++;
e5403d7c 676
33a5bc52 677 if (number == 0) return TRUE;
e5403d7c 678
33a5bc52 679 char **files = new char*[number];
e5403d7c 680
33a5bc52
RR
681 text = (char*) data;
682 for (size_t i = 0; i < number; i++)
683 {
684 files[i] = text;
685 int len = strlen( text );
686 text += len+1;
687 }
e3e65dac 688
33a5bc52 689 bool ret = OnDropFiles( x, y, 1, files );
e5403d7c 690
33a5bc52 691 free( files );
e5403d7c 692
33a5bc52 693 return ret;
e3e65dac
RR
694}
695
696size_t wxFileDropTarget::GetFormatCount() const
697{
33a5bc52 698 return 1;
e3e65dac
RR
699}
700
701wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const
702{
33a5bc52 703 return wxDF_FILENAME;
e3e65dac 704}
c801d85f
KB
705
706//-------------------------------------------------------------------------
e3e65dac 707// wxDropSource
c801d85f
KB
708//-------------------------------------------------------------------------
709
b527aac5
RR
710static void
711shape_motion (GtkWidget *widget,
b666df2c 712 GdkEventMotion * /*event*/);
b527aac5 713
c801d85f
KB
714//-----------------------------------------------------------------------------
715// drag request
716
b6af8d80 717void gtk_drag_callback( GtkWidget *widget, GdkEvent *event, wxDropSource *source )
c801d85f 718{
8b53e5a2
RR
719 wxDataObject *data = source->m_data;
720
721 switch (data->GetFormat())
722 {
ab8884ac
RR
723 case wxDF_PRIVATE:
724 {
725 wxPrivateDataObject *pdo = (wxPrivateDataObject*) data;
726
727 gtk_widget_dnd_data_set( widget,
728 event,
729 (unsigned char*) pdo->GetData(),
730 (int) pdo->GetDataSize() );
731
732 break;
733 }
8b53e5a2
RR
734 case wxDF_TEXT:
735 {
736 wxTextDataObject *text_object = (wxTextDataObject*) data;
737
738 wxString text = text_object->GetText();
739
2830bf19
RR
740 char *s = WXSTRINGCAST text;
741
8b53e5a2
RR
742 gtk_widget_dnd_data_set( widget,
743 event,
2830bf19
RR
744 (unsigned char*) s,
745 (int) text.Length()+1 );
8b53e5a2
RR
746
747 break;
748 }
749
750 case wxDF_FILENAME:
751 {
752 wxFileDataObject *file_object = (wxFileDataObject*) data;
753
754 wxString text = file_object->GetFiles();
755
2830bf19
RR
756 char *s = WXSTRINGCAST text;
757
8b53e5a2
RR
758 gtk_widget_dnd_data_set( widget,
759 event,
2830bf19
RR
760 (unsigned char*) s,
761 (int) text.Length()+1 );
8b53e5a2
RR
762
763 break;
764 }
765
766 default:
767 {
768 return;
769 }
770 }
b6af8d80 771
46ccb510 772 source->m_retValue = wxDragCopy;
ff7b1510 773}
c801d85f 774
e3e65dac 775wxDropSource::wxDropSource( wxWindow *win )
c801d85f 776{
ab8884ac 777 g_blockEventsOnDrag = TRUE;
e3e65dac 778
ab8884ac
RR
779 m_window = win;
780 m_widget = win->m_widget;
781 if (win->m_wxwindow) m_widget = win->m_wxwindow;
c801d85f 782
ab8884ac
RR
783 m_data = (wxDataObject *) NULL;
784 m_retValue = wxDragCancel;
e3e65dac 785
ab8884ac
RR
786 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
787 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
ff7b1510 788}
c801d85f 789
e3e65dac 790wxDropSource::wxDropSource( wxDataObject &data, wxWindow *win )
c801d85f 791{
ab8884ac 792 g_blockEventsOnDrag = TRUE;
e3e65dac 793
ab8884ac
RR
794 m_window = win;
795 m_widget = win->m_widget;
796 if (win->m_wxwindow) m_widget = win->m_wxwindow;
797 m_retValue = wxDragCancel;
e3e65dac 798
ab8884ac 799 m_data = &data;
e3e65dac 800
ab8884ac
RR
801 m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
802 m_goaheadCursor = wxCursor( wxCURSOR_HAND );
ff7b1510 803}
e3e65dac
RR
804
805void wxDropSource::SetData( wxDataObject &data )
c801d85f 806{
ab8884ac 807 m_data = &data;
ff7b1510 808}
c801d85f 809
e3e65dac 810wxDropSource::~wxDropSource(void)
c801d85f 811{
b666df2c 812// if (m_data) delete m_data;
e3e65dac 813
ab8884ac 814 g_blockEventsOnDrag = FALSE;
ff7b1510 815}
e3e65dac 816
46ccb510 817wxDragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) )
e3e65dac 818{
ab8884ac
RR
819 if (gdk_dnd.dnd_grabbed) return (wxDragResult) wxDragNone;
820 if (gdk_dnd.drag_really) return (wxDragResult) wxDragNone;
e3e65dac 821
ab8884ac 822 wxASSERT_MSG( m_data, "wxDragSource: no data" );
b6af8d80 823
ab8884ac 824 if (!m_data) return (wxDragResult) wxDragNone;
c801d85f 825
ab8884ac
RR
826 static GtkWidget *drag_icon = (GtkWidget*) NULL;
827 static GtkWidget *drop_icon = (GtkWidget*) NULL;
a802c3a1 828
ab8884ac 829 GdkPoint hotspot_1 = {0,-5 };
a802c3a1 830
ab8884ac
RR
831 if (!drag_icon)
832 {
a802c3a1
RR
833 drag_icon = shape_create_icon ( gv_xpm,
834 440, 140, 0,0, GTK_WINDOW_POPUP);
835
836 gtk_signal_connect (GTK_OBJECT (drag_icon), "destroy",
837 GTK_SIGNAL_FUNC(gtk_widget_destroyed),
838 &drag_icon);
839
840 gtk_widget_hide (drag_icon);
ab8884ac 841 }
a802c3a1 842
ab8884ac 843 GdkPoint hotspot_2 = {-5,-5};
a802c3a1 844
ab8884ac
RR
845 if (!drop_icon)
846 {
a802c3a1
RR
847 drop_icon = shape_create_icon ( page_xpm,
848 440, 140, 0,0, GTK_WINDOW_POPUP);
849
850 gtk_signal_connect (GTK_OBJECT (drop_icon), "destroy",
851 GTK_SIGNAL_FUNC(gtk_widget_destroyed),
852 &drop_icon);
853
854 gtk_widget_hide (drop_icon);
ab8884ac 855 }
b527aac5 856
a802c3a1 857
b527aac5 858 gdk_dnd_set_drag_shape( drag_icon->window,
a802c3a1
RR
859 &hotspot_1,
860 drop_icon->window,
861 &hotspot_2);
862
863
ab8884ac 864 GdkWindowPrivate *wp = (GdkWindowPrivate*) m_widget->window;
c801d85f 865
ab8884ac 866 RegisterWindow();
c801d85f 867
ab8884ac 868 gdk_dnd.drag_perhaps = TRUE;
c801d85f 869
ab8884ac
RR
870 gdk_dnd.dnd_drag_start.x = 5;
871 gdk_dnd.dnd_drag_start.y = 5;
872 gdk_dnd.real_sw = wp;
c801d85f 873
ab8884ac
RR
874 if (gdk_dnd.drag_startwindows)
875 {
876 g_free( gdk_dnd.drag_startwindows );
877 gdk_dnd.drag_startwindows = (GdkWindow **) NULL;
878 }
879 gdk_dnd.drag_numwindows = gdk_dnd.drag_really = 0;
c801d85f 880
ab8884ac
RR
881 XWindowAttributes dnd_winattr;
882 XGetWindowAttributes( gdk_display, wp->xwindow, &dnd_winattr );
883 wp->dnd_drag_savedeventmask = dnd_winattr.your_event_mask;
c801d85f 884
ab8884ac 885 gdk_dnd_drag_addwindow( m_widget->window );
c801d85f 886
ab8884ac
RR
887 GdkEventDragBegin ev;
888 ev.type = GDK_DRAG_BEGIN;
889 ev.window = m_widget->window;
890 ev.u.allflags = 0;
891 ev.u.flags.protocol_version = DND_PROTOCOL_VERSION;
c801d85f 892
ab8884ac 893 gdk_event_put( (GdkEvent*)&ev );
c801d85f 894
ab8884ac
RR
895 XGrabPointer( gdk_display, wp->xwindow, False,
896 ButtonMotionMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
897 GrabModeAsync, GrabModeAsync, gdk_root_window, None, CurrentTime );
c801d85f 898
ab8884ac 899 gdk_dnd_set_drag_cursors( m_defaultCursor.GetCursor(), m_goaheadCursor.GetCursor() );
c801d85f 900
ab8884ac
RR
901 gdk_dnd.dnd_grabbed = TRUE;
902 gdk_dnd.drag_really = 1;
e3e65dac 903
ab8884ac
RR
904 int x = 0;
905 int y = 0;
906 wxGetMousePosition( &x, &y );
e3e65dac 907
ab8884ac 908 gdk_dnd_display_drag_cursor( x, y, FALSE, TRUE );
b666df2c
RR
909
910 gs_currentDropSource = this;
911
ab8884ac 912 while (gdk_dnd.drag_really || gdk_dnd.drag_perhaps) wxYield();
b666df2c
RR
913
914 gs_currentDropSource = (wxDropSource*) NULL;
c801d85f 915
ab8884ac 916 UnregisterWindow();
e3e65dac 917
ab8884ac 918 g_blockEventsOnDrag = FALSE;
30dea054 919
ab8884ac 920 return m_retValue;
ff7b1510 921}
c801d85f 922
e3e65dac 923void wxDropSource::RegisterWindow(void)
c801d85f 924{
ab8884ac 925 if (!m_data) return;
c801d85f 926
ab8884ac 927 wxString formats;
e3e65dac 928
ab8884ac 929 wxDataFormat df = m_data->GetFormat();
c801d85f 930
e3e65dac
RR
931 switch (df)
932 {
ab8884ac
RR
933 case wxDF_TEXT:
934 {
935 formats += "text/plain";
936 break;
937 }
938 case wxDF_FILENAME:
939 {
940 formats += "file:ALL";
941 break;
942 }
943 case wxDF_PRIVATE:
944 {
945 wxPrivateDataObject* pdo = (wxPrivateDataObject*) m_data;
946 formats += "applications/";
947 formats += pdo->GetId();
948 break;
949 }
950 default:
951 break;
e3e65dac 952 }
c801d85f 953
ab8884ac 954 char *str = WXSTRINGCAST formats;
c801d85f 955
ab8884ac 956 gtk_widget_dnd_drag_set( m_widget, TRUE, &str, 1 );
c801d85f 957
ab8884ac
RR
958 gtk_signal_connect( GTK_OBJECT(m_widget), "drag_request_event",
959 GTK_SIGNAL_FUNC(gtk_drag_callback), (gpointer)this );
ff7b1510 960}
c801d85f 961
e3e65dac 962void wxDropSource::UnregisterWindow(void)
c801d85f 963{
ab8884ac 964 if (!m_widget) return;
c801d85f 965
ab8884ac 966 gtk_widget_dnd_drag_set( m_widget, FALSE, (gchar **) NULL, 0 );
e3e65dac 967
ab8884ac 968 gtk_signal_disconnect_by_data( GTK_OBJECT(m_widget), (gpointer)this );
ff7b1510 969}
f5368809 970
a802c3a1
RR
971
972/*
973 * Shaped Windows
974 */
bbe0af5b 975static GdkWindow *root_win = (GdkWindow*) NULL;
a802c3a1
RR
976
977typedef struct _cursoroffset {gint x,y;} CursorOffset;
978
979static void
980shape_pressed (GtkWidget *widget, GdkEventButton *event)
981{
982 CursorOffset *p;
983
984 /* ignore double and triple click */
985 if (event->type != GDK_BUTTON_PRESS)
986 return;
987
b527aac5 988 p = (CursorOffset *)gtk_object_get_user_data (GTK_OBJECT(widget));
a802c3a1
RR
989 p->x = (int) event->x;
990 p->y = (int) event->y;
991
992 gtk_grab_add (widget);
993 gdk_pointer_grab (widget->window, TRUE,
b527aac5
RR
994 (GdkEventMask)
995 (GDK_BUTTON_RELEASE_MASK |
996 GDK_BUTTON_MOTION_MASK |
997 GDK_POINTER_MOTION_HINT_MASK),
bbe0af5b
RR
998 (GdkWindow*)NULL,
999 (GdkCursor*) NULL, 0);
a802c3a1
RR
1000}
1001
1002
1003static void
1004shape_released (GtkWidget *widget)
1005{
1006 gtk_grab_remove (widget);
1007 gdk_pointer_ungrab (0);
1008}
1009
1010static void
1011shape_motion (GtkWidget *widget,
b666df2c 1012 GdkEventMotion * /*event*/ )
a802c3a1
RR
1013{
1014 gint xp, yp;
1015 CursorOffset * p;
1016 GdkModifierType mask;
1017
b527aac5 1018 p = (CursorOffset *)gtk_object_get_user_data (GTK_OBJECT (widget));
a802c3a1
RR
1019
1020 /*
1021 * Can't use event->x / event->y here
1022 * because I need absolute coordinates.
1023 */
b666df2c 1024
a802c3a1
RR
1025 gdk_window_get_pointer (root_win, &xp, &yp, &mask);
1026 gtk_widget_set_uposition (widget, xp - p->x, yp - p->y);
b666df2c
RR
1027
1028 if (gs_currentDropSource) gs_currentDropSource->GiveFeedback( wxDragCopy, FALSE );
a802c3a1
RR
1029}
1030
1031GtkWidget *
1032shape_create_icon (char **data,
1033 gint x,
1034 gint y,
1035 gint px,
1036 gint py,
1037 gint window_type)
1038{
1039 GtkWidget *window;
1040 GtkWidget *pixmap;
1041 GtkWidget *fixed;
1042 CursorOffset* icon_pos;
1043 GdkGC* gc;
1044 GdkBitmap *gdk_pixmap_mask;
1045 GdkPixmap *gdk_pixmap;
1046 GtkStyle *style;
1047
1048 style = gtk_widget_get_default_style ();
1049 gc = style->black_gc;
1050
1051 /*
1052 * GDK_WINDOW_TOPLEVEL works also, giving you a title border
1053 */
b527aac5 1054 window = gtk_window_new ((GtkWindowType)window_type);
a802c3a1
RR
1055
1056 fixed = gtk_fixed_new ();
1057 gtk_widget_set_usize (fixed, 100,100);
1058 gtk_container_add (GTK_CONTAINER (window), fixed);
1059 gtk_widget_show (fixed);
1060
1061 gtk_widget_set_events (window,
1062 gtk_widget_get_events (window) |
1063 GDK_BUTTON_MOTION_MASK |
1064 GDK_POINTER_MOTION_HINT_MASK |
1065 GDK_BUTTON_PRESS_MASK);
1066
1067 gtk_widget_realize (window);
1068
1069 gdk_pixmap = gdk_pixmap_create_from_xpm_d (window->window, &gdk_pixmap_mask,
1070 &style->bg[GTK_STATE_NORMAL],
1071 (gchar**) data );
1072
1073 pixmap = gtk_pixmap_new (gdk_pixmap, gdk_pixmap_mask);
1074 gtk_fixed_put (GTK_FIXED (fixed), pixmap, px,py);
1075 gtk_widget_show (pixmap);
1076
1077 gtk_widget_shape_combine_mask (window, gdk_pixmap_mask, px,py);
1078
1079
1080 gtk_signal_connect (GTK_OBJECT (window), "button_press_event",
1081 GTK_SIGNAL_FUNC (shape_pressed),NULL);
1082 gtk_signal_connect (GTK_OBJECT (window), "button_release_event",
1083 GTK_SIGNAL_FUNC (shape_released),NULL);
1084 gtk_signal_connect (GTK_OBJECT (window), "motion_notify_event",
1085 GTK_SIGNAL_FUNC (shape_motion),NULL);
1086
1087 icon_pos = g_new (CursorOffset, 1);
1088 gtk_object_set_user_data(GTK_OBJECT(window), icon_pos);
1089
1090 gtk_widget_set_uposition (window, x, y);
1091 gtk_widget_show (window);
1092
1093 return window;
1094}
1095
f5368809
RR
1096#endif
1097 // NEW_GTK_DND_CODE
1098
ac57418f
RR
1099#endif
1100
1101 // wxUSE_DRAG_AND_DROP