1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDropTarget class
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dnd.h"
15 #include "wx/window.h"
17 #include "wx/gdicmn.h"
20 #include "gdk/gdkprivate.h"
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 extern bool g_blockEventsOnDrag
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 wxDropTarget::wxDropTarget()
38 wxDropTarget::~wxDropTarget()
42 void wxDropTarget::UnregisterWidget( GtkWidget
*widget
)
46 gtk_widget_dnd_drop_set( widget
, FALSE
, (gchar
**) NULL
, 0, FALSE
);
49 void wxDropTarget::RegisterWidget( GtkWidget
*widget
)
54 for ( size_t i
= 0; i
< GetFormatCount(); i
++ )
56 wxDataFormat df
= GetFormat( i
);
60 if (i
> 0) formats
+= ";";
61 formats
+= "text/plain";
65 if (i
> 0) formats
+= ";";
66 formats
+= "file:ALL";
74 char *str
= WXSTRINGCAST formats
;
76 gtk_widget_dnd_drop_set( widget
, TRUE
, &str
, valid
, FALSE
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 bool wxTextDropTarget::OnDrop( long x
, long y
, const void *data
, size_t WXUNUSED(size
) )
85 OnDropText( x
, y
, (const char*)data
);
89 bool wxTextDropTarget::OnDropText( long x
, long y
, const char *psz
)
91 printf( "Got dropped text: %s.\n", psz
);
92 printf( "At x: %d, y: %d.\n", (int)x
, (int)y
);
96 size_t wxTextDropTarget::GetFormatCount() const
101 wxDataFormat
wxTextDropTarget::GetFormat(size_t WXUNUSED(n
)) const
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 bool wxFileDropTarget::OnDropFiles( long x
, long y
, size_t nFiles
, const char * const aszFiles
[] )
112 printf( "Got %d dropped files.\n", (int)nFiles
);
113 printf( "At x: %d, y: %d.\n", (int)x
, (int)y
);
114 for (size_t i
= 0; i
< nFiles
; i
++)
116 printf( aszFiles
[i
] );
122 bool wxFileDropTarget::OnDrop(long x
, long y
, const void *data
, size_t size
)
125 char *text
= (char*) data
;
126 for (size_t i
= 0; i
< size
; i
++)
127 if (text
[i
] == 0) number
++;
129 if (number
== 0) return TRUE
;
131 char **files
= new char*[number
];
134 for (size_t i
= 0; i
< number
; i
++)
137 int len
= strlen( text
);
141 bool ret
= OnDropFiles( x
, y
, 1, files
);
148 size_t wxFileDropTarget::GetFormatCount() const
153 wxDataFormat
wxFileDropTarget::GetFormat(size_t WXUNUSED(n
)) const
155 return wxDF_FILENAME
;
158 //-------------------------------------------------------------------------
160 //-------------------------------------------------------------------------
162 //-----------------------------------------------------------------------------
165 void gtk_drag_callback( GtkWidget
*widget
, GdkEvent
*event
, wxDropSource
*source
)
167 printf( "Data requested for dropping.\n" );
169 wxDataObject
*data
= source
->m_data
;
171 size_t size
= data
->GetDataSize();
172 char *ptr
= new char[size
];
173 data
->GetDataHere( ptr
);
175 gtk_widget_dnd_data_set( widget
, event
, ptr
, size
);
179 source
->m_retValue
= wxDragCopy
;
182 wxDropSource::wxDropSource( wxWindow
*win
)
184 g_blockEventsOnDrag
= TRUE
;
187 m_widget
= win
->m_widget
;
188 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
190 m_data
= (wxDataObject
*) NULL
;
191 m_retValue
= wxDragCancel
;
193 m_defaultCursor
= wxCursor( wxCURSOR_NO_ENTRY
);
194 m_goaheadCursor
= wxCursor( wxCURSOR_HAND
);
197 wxDropSource::wxDropSource( wxDataObject
&data
, wxWindow
*win
)
199 g_blockEventsOnDrag
= TRUE
;
202 m_widget
= win
->m_widget
;
203 if (win
->m_wxwindow
) m_widget
= win
->m_wxwindow
;
204 m_retValue
= wxDragCancel
;
208 m_defaultCursor
= wxCursor( wxCURSOR_NO_ENTRY
);
209 m_goaheadCursor
= wxCursor( wxCURSOR_HAND
);
212 void wxDropSource::SetData( wxDataObject
&data
)
217 wxDropSource::~wxDropSource(void)
219 // if (m_data) delete m_data;
221 g_blockEventsOnDrag
= FALSE
;
224 wxDragResult
wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove
) )
226 if (gdk_dnd
.dnd_grabbed
) return (wxDragResult
) wxDragNone
;
227 if (gdk_dnd
.drag_really
) return (wxDragResult
) wxDragNone
;
229 wxASSERT_MSG( m_data
, "wxDragSource: no data" );
231 if (!m_data
) return (wxDragResult
) wxDragNone
;
232 if (m_data
->GetDataSize() == 0) return (wxDragResult
) wxDragNone
;
234 GdkWindowPrivate
*wp
= (GdkWindowPrivate
*) m_widget
->window
;
238 gdk_dnd
.drag_perhaps
= TRUE
;
240 gdk_dnd
.dnd_drag_start
.x
= 5;
241 gdk_dnd
.dnd_drag_start
.y
= 5;
242 gdk_dnd
.real_sw
= wp
;
244 if (gdk_dnd
.drag_startwindows
)
246 g_free( gdk_dnd
.drag_startwindows
);
247 gdk_dnd
.drag_startwindows
= (GdkWindow
**) NULL
;
249 gdk_dnd
.drag_numwindows
= gdk_dnd
.drag_really
= 0;
251 XWindowAttributes dnd_winattr
;
252 XGetWindowAttributes( gdk_display
, wp
->xwindow
, &dnd_winattr
);
253 wp
->dnd_drag_savedeventmask
= dnd_winattr
.your_event_mask
;
255 gdk_dnd_drag_addwindow( m_widget
->window
);
257 GdkEventDragBegin ev
;
258 ev
.type
= GDK_DRAG_BEGIN
;
259 ev
.window
= m_widget
->window
;
261 ev
.u
.flags
.protocol_version
= DND_PROTOCOL_VERSION
;
263 gdk_event_put( (GdkEvent
*)&ev
);
265 XGrabPointer( gdk_display
, wp
->xwindow
, False
,
266 ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
,
267 GrabModeAsync
, GrabModeAsync
, gdk_root_window
, None
, CurrentTime
);
269 gdk_dnd_set_drag_cursors( m_defaultCursor
.GetCursor(), m_goaheadCursor
.GetCursor() );
271 gdk_dnd
.dnd_grabbed
= TRUE
;
272 gdk_dnd
.drag_really
= 1;
276 gdk_window_get_pointer( m_widget
->window
, &x
, &y
, (GdkModifierType
*) NULL
);
278 gdk_dnd_display_drag_cursor( x
, y
, FALSE
, TRUE
);
280 while (gdk_dnd
.drag_really
|| gdk_dnd
.drag_perhaps
) wxYield();
284 g_blockEventsOnDrag
= FALSE
;
289 void wxDropSource::RegisterWindow(void)
295 wxDataFormat df
= m_data
->GetPreferredFormat();
300 formats
+= "text/plain";
303 formats
+= "file:ALL";
309 char *str
= WXSTRINGCAST formats
;
311 gtk_widget_dnd_drag_set( m_widget
, TRUE
, &str
, 1 );
313 gtk_signal_connect( GTK_OBJECT(m_widget
), "drag_request_event",
314 GTK_SIGNAL_FUNC(gtk_drag_callback
), (gpointer
)this );
317 void wxDropSource::UnregisterWindow(void)
319 if (!m_widget
) return;
321 gtk_widget_dnd_drag_set( m_widget
, FALSE
, (gchar
**) NULL
, 0 );
323 gtk_signal_disconnect_by_data( GTK_OBJECT(m_widget
), (gpointer
)this );