// Name: dnd.h
// Purpose: declaration of the wxDropTarget class
// Author: Robert Roebling
-// RCS-ID:
+// RCS-ID: $Id$
// Copyright: (c) 1998 Vadim Zeitlin, Robert Roebling
// Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////////
#endif
#include "wx/defs.h"
+
+#if wxUSE_DRAG_AND_DROP
+
#include "wx/object.h"
#include "wx/string.h"
+#include "wx/dataobj.h"
#include "wx/cursor.h"
+#include "wx/icon.h"
+#include "wx/gdicmn.h"
//-------------------------------------------------------------------------
// classes
class wxWindow;
-class wxDataObject;
-class wxTextDataObject;
-class wxFileDataObject;
-
class wxDropTarget;
class wxTextDropTarget;
class wxFileDropTarget;
+class wxPrivateDropTarget;
class wxDropSource;
//-------------------------------------------------------------------------
-// wxDataObject
+// wxDropSource
//-------------------------------------------------------------------------
-class wxDataObject: public wxObject
+enum wxDragResult
+{
+ wxDragError, // error prevented the d&d operation from completing
+ wxDragNone, // drag target didn't accept the data
+ wxDragCopy, // the data was successfully copied
+ wxDragMove, // the data was successfully moved (MSW only)
+ wxDragCancel // the operation was cancelled by user (not an error)
+};
+
+class wxDropSource: public wxObject
{
public:
- // all data formats (values are the same as in windows.h, do not change!)
- enum StdFormat
- {
- Invalid,
- Text,
- Bitmap,
- MetafilePict,
- Sylk,
- Dif,
- Tiff,
- OemText,
- Dib,
- Palette,
- Pendata,
- Riff,
- Wave,
- UnicodeText,
- EnhMetafile,
- Hdrop,
- Locale,
- Max
- };
-
- // function to return symbolic name of clipboard format (debug messages)
- static const char *GetFormatName(wxDataFormat format);
-
- // ctor & dtor
- wxDataObject() {};
- ~wxDataObject() {};
-
- // pure virtuals to override
- // get the best suited format for our data
- virtual wxDataFormat GetPreferredFormat() const = 0;
- // decide if we support this format (should be one of values of
- // StdFormat enumerations or a user-defined format)
- virtual bool IsSupportedFormat(wxDataFormat format) const = 0;
- // get the (total) size of data
- virtual size_t GetDataSize() const = 0;
- // copy raw data to provided pointer
- virtual void GetDataHere(void *pBuf) const = 0;
+ /* constructor. set data later with SetData() */
+ wxDropSource( wxWindow *win, const wxIcon &go = wxNullIcon, const wxIcon &stop = wxNullIcon );
+
+ /* constructor for setting one data object */
+ wxDropSource( wxDataObject *data, wxWindow *win, const wxIcon &go = wxNullIcon, const wxIcon &stop = wxNullIcon );
+
+ /* constructor for setting several data objects via wxDataBroker */
+ wxDropSource( wxDataBroker *data, wxWindow *win );
+
+ ~wxDropSource();
+
+ /* set several dataobjects via wxDataBroker */
+ void SetData( wxDataBroker *data );
+
+ /* set one dataobject */
+ void SetData( wxDataObject *data );
+
+ /* start drag action */
+ wxDragResult DoDragDrop( bool bAllowMove = FALSE );
+
+ /* override to give feedback */
+ virtual bool GiveFeedback( wxDragResult WXUNUSED(effect), bool WXUNUSED(bScrolling) ) { return TRUE; }
+
+ /* GTK implementation */
+
+ void RegisterWindow();
+ void UnregisterWindow();
+
+ GtkWidget *m_widget;
+ wxWindow *m_window;
+ wxDragResult m_retValue;
+ wxDataBroker *m_data;
+
+ wxCursor m_defaultCursor;
+ wxCursor m_goaheadCursor;
+
+ wxIcon m_goIcon;
+ wxIcon m_stopIcon;
+
+ bool m_waiting;
};
-// ----------------------------------------------------------------------------
-// wxTextDataObject is a specialization of wxDataObject for text data
-// ----------------------------------------------------------------------------
+#include "gtk/gtk.h"
+#if (GTK_MINOR_VERSION > 0)
+
+//-------------------------------------------------------------------------
+// wxDropTarget
+//-------------------------------------------------------------------------
-class wxTextDataObject : public wxDataObject
+class wxDropTarget: public wxObject
{
public:
- // ctors
- wxTextDataObject() { }
- wxTextDataObject(const wxString& strText) : m_strText(strText) { }
- void Init(const wxString& strText) { m_strText = strText; }
-
- // implement base class pure virtuals
- virtual wxDataFormat GetPreferredFormat() const
- { return wxDF_TEXT; }
- virtual bool IsSupportedFormat(wxDataFormat format) const
- { return format == wxDF_TEXT; }
- virtual size_t GetDataSize() const
- { return m_strText.Len() + 1; } // +1 for trailing '\0'of course
- virtual void GetDataHere(void *pBuf) const
- { memcpy(pBuf, m_strText.c_str(), GetDataSize()); }
-private:
- wxString m_strText;
+ wxDropTarget();
+ ~wxDropTarget();
+
+ /* may be overridden to react to events */
+ virtual void OnEnter();
+ virtual void OnLeave();
+
+ /* may be overridden to reject certain formats or drops
+ on certain areas. always returns TRUE by default
+ indicating that you'd accept the data from the drag. */
+ virtual bool OnMove( int x, int y );
+
+ /* has to be overridden to accept a drop event. call
+ IsSupported() to ask which formats are available
+ and then call RequestData() to indicate the format
+ you request. */
+ virtual bool OnDrop( int x, int y );
+
+ /* this gets called once the data has actually arrived. get
+ it with GetData(). this has to be overridden. */
+ virtual bool OnData( int x, int y );
+
+ /* called from within OnDrop() to request a certain format
+ from the drop event. */
+ bool RequestData( wxDataFormat format );
+
+ /* called to query what formats are available */
+ bool IsSupported( wxDataFormat format );
+
+ /* fill data with data from the dragging source */
+ bool GetData( wxDataObject *data );
+
+// implementation
+ void RegisterWidget( GtkWidget *widget );
+ void UnregisterWidget( GtkWidget *widget );
+
+ GdkDragContext *m_dragContext;
+ GtkWidget *m_dragWidget;
+ GtkSelectionData *m_dragData;
+ guint m_dragTime;
+ bool m_firstMotion; /* gdk has no "gdk_drag_enter" event */
+
+ void SetDragContext( GdkDragContext *dc ) { m_dragContext = dc; }
+ void SetDragWidget( GtkWidget *w ) { m_dragWidget = w; }
+ void SetDragData( GtkSelectionData *sd ) { m_dragData = sd; }
+ void SetDragTime( guint time ) { m_dragTime = time; }
};
-// ----------------------------------------------------------------------------
-// wxFileDataObject is a specialization of wxDataObject for file names
-// ----------------------------------------------------------------------------
+//-------------------------------------------------------------------------
+// wxTextDropTarget
+//-------------------------------------------------------------------------
-class wxFileDataObject : public wxDataObject
+class wxTextDropTarget: public wxDropTarget
{
public:
- wxFileDataObject(void) { }
- void AddFile( const wxString &file )
- { m_files += file; m_files += ";"; }
+ wxTextDropTarget() {}
+
+ virtual bool OnMove( int x, int y );
+ virtual bool OnDrop( int x, int y );
+ virtual bool OnData( int x, int y );
+
+ /* you have to override OnDropData to get at the text */
+ virtual bool OnDropText( int x, int y, const char *text ) = 0;
+
+};
+
+//-------------------------------------------------------------------------
+// wxPrivateDropTarget
+//-------------------------------------------------------------------------
- // implement base class pure virtuals
- virtual wxDataFormat GetPreferredFormat() const
- { return wxDF_FILENAME; }
- virtual bool IsSupportedFormat(wxDataFormat format) const
- { return format == wxDF_FILENAME; }
- virtual size_t GetDataSize() const
- { return m_files.Len() + 1; } // +1 for trailing '\0'of course
- virtual void GetDataHere(void *pBuf) const
- { memcpy(pBuf, m_files.c_str(), GetDataSize()); }
+class wxPrivateDropTarget: public wxDropTarget
+{
+public:
+ /* sets id to "application/myprogram" where "myprogram" is the
+ same as wxApp->GetAppName() */
+ wxPrivateDropTarget();
+ /* see SetId() below for explanation */
+ wxPrivateDropTarget( const wxString &id );
+
+ virtual bool OnMove( int x, int y );
+ virtual bool OnDrop( int x, int y );
+ virtual bool OnData( int x, int y );
+
+ /* you have to override OnDropData to get at the data */
+ virtual bool OnDropData( int x, int y, void *data, size_t size ) = 0;
+
+ /* the string ID identifies the format of clipboard or DnD data. a word
+ processor would e.g. add a wxTextDataObject and a wxPrivateDataObject
+ to the clipboard - the latter with the Id "application/wxword" or
+ "image/png". */
+ void SetId( const wxString& id ) { m_id = id; }
+ wxString GetId() { return m_id; }
+
private:
- wxString m_files;
+
+ wxString m_id;
+};
+
+//----------------------------------------------------------------------------
+// A drop target which accepts files (dragged from File Manager or Explorer)
+//----------------------------------------------------------------------------
+
+class wxFileDropTarget: public wxDropTarget
+{
+public:
+
+ wxFileDropTarget() {}
+
+ virtual bool OnMove( int x, int y );
+ virtual bool OnDrop( int x, int y );
+ virtual bool OnData( int x, int y );
+ /* you have to override OnDropFiles to get at the file names */
+ virtual bool OnDropFiles( int x, int y, size_t nFiles, const wxChar * const aszFiles[] ) = 0;
+
};
+
+#else
+
//-------------------------------------------------------------------------
// wxDropTarget
//-------------------------------------------------------------------------
virtual void OnEnter() { }
virtual void OnLeave() { }
- virtual bool OnDrop( long x, long y, const void *pData ) = 0;
+ virtual void OnMouseMove( long WXUNUSED(x), long WXUNUSED(y) ) { }
+ virtual bool OnDrop( long x, long y, const void *data, size_t size ) = 0;
-// protected:
-
- friend wxWindow;
-
// Override these to indicate what kind of data you support:
virtual size_t GetFormatCount() const = 0;
- virtual wxDataFormat GetFormat(size_t n) const = 0;
+ virtual wxDataFormat &GetFormat(size_t n) const;
+
+ // implementation
- void Drop( GdkEvent *event, int x, int y );
void RegisterWidget( GtkWidget *widget );
void UnregisterWidget( GtkWidget *widget );
+
+ wxDataFormat *m_format;
};
//-------------------------------------------------------------------------
{
public:
- wxTextDropTarget() {};
- virtual bool OnDrop( long x, long y, const void *pData );
+ wxTextDropTarget();
+ virtual bool OnDrop( long x, long y, const void *data, size_t size );
virtual bool OnDropText( long x, long y, const char *psz );
protected:
virtual size_t GetFormatCount() const;
- virtual wxDataFormat GetFormat(size_t n) const;
};
-// ----------------------------------------------------------------------------
+//-------------------------------------------------------------------------
+// wxPrivateDropTarget
+//-------------------------------------------------------------------------
+
+class wxPrivateDropTarget: public wxDropTarget
+{
+public:
+
+ wxPrivateDropTarget();
+
+ // you have to override OnDrop to get at the data
+
+ // the string ID identifies the format of clipboard or DnD data. a word
+ // processor would e.g. add a wxTextDataObject and a wxPrivateDataObject
+ // to the clipboard - the latter with the Id "application/wxword" or
+ // "image/png".
+
+ void SetId( const wxString& id );
+
+ wxString GetId()
+ { return m_id; }
+
+private:
+
+ virtual size_t GetFormatCount() const;
+
+ wxString m_id;
+};
+
+//----------------------------------------------------------------------------
// A drop target which accepts files (dragged from File Manager or Explorer)
-// ----------------------------------------------------------------------------
+//----------------------------------------------------------------------------
class wxFileDropTarget: public wxDropTarget
{
public:
- wxFileDropTarget() {};
+ wxFileDropTarget();
- virtual bool OnDrop(long x, long y, const void *pData);
+ virtual bool OnDrop( long x, long y, const void *data, size_t size );
virtual bool OnDropFiles( long x, long y,
- size_t nFiles, const char * const aszFiles[]);
+ size_t nFiles, const char * const aszFiles[] );
protected:
virtual size_t GetFormatCount() const;
- virtual wxDataFormat GetFormat(size_t n) const;
};
-//-------------------------------------------------------------------------
-// wxDropSource
-//-------------------------------------------------------------------------
-
-class wxDropSource: public wxObject
-{
- public:
+#endif
- enum DragResult
- {
- Error, // error prevented the d&d operation from completing
- None, // drag target didn't accept the data
- Copy, // the data was successfully copied
- Move, // the data was successfully moved
- Cancel // the operation was cancelled by user (not an error)
- };
- wxDropSource( wxWindow *win );
- wxDropSource( wxDataObject &data, wxWindow *win );
-
- ~wxDropSource(void);
-
- void SetData( wxDataObject &data );
- DragResult DoDragDrop( bool bAllowMove = FALSE );
-
- virtual bool GiveFeedback( DragResult WXUNUSED(effect), bool WXUNUSED(bScrolling) ) { return TRUE; };
+#endif
- protected:
-
- friend void gtk_drag_callback( GtkWidget *widget, GdkEvent *event, wxDropSource *source );
-
- void RegisterWindow(void);
- void UnregisterWindow(void);
-
- GtkWidget *m_widget;
- wxWindow *m_window;
- DragResult m_retValue;
- wxDataObject *m_data;
-
- wxCursor m_defaultCursor;
- wxCursor m_goaheadCursor;
-};
+ // wxUSE_DRAG_AND_DROP
#endif
//__GTKDNDH__