From 1dd989e1bf4c3667d27b3c27a9cf64a66484ee90 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Mon, 18 Oct 1999 22:22:56 +0000 Subject: [PATCH] New wxDataObject etc. Almost works. A few more compatibility funcs for (long*) vs (int*). Makefile.in regenerated from filelist... git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4058 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- Makefile.in | 14 +- include/wx/dataobj.h | 4 +- include/wx/dc.h | 10 + include/wx/event.h | 17 +- include/wx/gtk/clipbrd.h | 9 +- include/wx/gtk/dataobj.h | 270 +++++++++++++-------------- include/wx/gtk/dnd.h | 26 +-- include/wx/gtk1/clipbrd.h | 9 +- include/wx/gtk1/dataobj.h | 270 +++++++++++++-------------- include/wx/gtk1/dnd.h | 26 +-- samples/dnd/dnd.cpp | 10 +- src/gtk/clipbrd.cpp | 164 +++++++--------- src/gtk/dataobj.cpp | 382 +++++++++++++------------------------- src/gtk/dnd.cpp | 120 +++++------- src/gtk1/clipbrd.cpp | 164 +++++++--------- src/gtk1/dataobj.cpp | 382 +++++++++++++------------------------- src/gtk1/dnd.cpp | 120 +++++------- 17 files changed, 806 insertions(+), 1191 deletions(-) diff --git a/Makefile.in b/Makefile.in index f1d1dfbf67..187172f811 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,5 +1,5 @@ # -# This file was automatically generated by tmake at 15:31, 1999/10/17 +# This file was automatically generated by tmake at 22:21, 1999/10/18 # DO NOT CHANGE THIS FILE, YOUR CHANGES WILL BE LOST! CHANGE UNX.T! # @@ -679,8 +679,11 @@ GTK_GENERICDEPS = \ GTK_COMMONOBJS = \ parser.o \ + appcmn.o \ + choiccmn.o \ cmndata.o \ config.o \ + ctrlcmn.o \ date.o \ datstrm.o \ db.o \ @@ -766,8 +769,11 @@ GTK_COMMONOBJS = \ GTK_COMMONDEPS = \ parser.d \ + appcmn.d \ + choiccmn.d \ cmndata.d \ config.d \ + ctrlcmn.d \ date.d \ datstrm.d \ db.d \ @@ -1047,6 +1053,7 @@ MOTIF_GENERICDEPS = \ MOTIF_COMMONOBJS = \ parser.o \ + appcmn.o \ choiccmn.o \ cmndata.o \ config.o \ @@ -1137,6 +1144,7 @@ MOTIF_COMMONOBJS = \ MOTIF_COMMONDEPS = \ parser.d \ + appcmn.d \ choiccmn.d \ cmndata.d \ config.d \ @@ -1383,6 +1391,7 @@ MSW_GENERICDEPS = \ MSW_COMMONOBJS = \ parser.o \ + appcmn.o \ choiccmn.o \ cmndata.o \ config.o \ @@ -1473,6 +1482,7 @@ MSW_COMMONOBJS = \ MSW_COMMONDEPS = \ parser.d \ + appcmn.d \ choiccmn.d \ cmndata.d \ config.d \ @@ -1737,6 +1747,7 @@ MSW_GUIDEPS = \ BASE_OBJS = \ init.o \ + appcmn.o \ config.o \ date.o \ dynarray.o \ @@ -1771,6 +1782,7 @@ BASE_OBJS = \ BASE_DEPS = \ init.d \ + appcmn.d \ config.d \ date.d \ dynarray.d \ diff --git a/include/wx/dataobj.h b/include/wx/dataobj.h index 48f84bca8d..c17f2f8341 100644 --- a/include/wx/dataobj.h +++ b/include/wx/dataobj.h @@ -37,7 +37,7 @@ class WXDLLEXPORT wxPrivateDataObject : public wxDataObject { #if defined(__WXGTK__) || defined(__WXMOTIF__) - DECLARE_DYNAMIC_CLASS( wxPrivateDataObject ) + DECLARE_CLASS( wxPrivateDataObject ) #endif public: @@ -87,7 +87,7 @@ private: size_t m_size; void *m_data; -#if !defined(__WXGTK__) && !defined(__WXMOTIF__) +#if !defined(__WXMOTIF__) // the data format wxDataFormat m_format; #endif diff --git a/include/wx/dc.h b/include/wx/dc.h index 4fe6bda1a1..2c66232f81 100644 --- a/include/wx/dc.h +++ b/include/wx/dc.h @@ -251,6 +251,7 @@ public: virtual wxCoord GetCharHeight() const = 0; virtual wxCoord GetCharWidth() const = 0; + void GetTextExtent(const wxString& string, wxCoord *x, wxCoord *y, wxCoord *descent = NULL, @@ -441,6 +442,15 @@ public: if ( y ) *y = y2; } + void GetClippingBox(long *x, long *y, long *w, long *h) const + { + wxCoord xx,yy,ww,hh; + DoGetClippingBox(&xx, &yy, &ww, &hh); + if (x) *x = xx; + if (y) *y = yy; + if (w) *w = ww; + if (h) *h = hh; + } #endif // !Win16 #if WXWIN_COMPATIBILITY diff --git a/include/wx/event.h b/include/wx/event.h index e8d25fd425..1e9f2df629 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -605,8 +605,15 @@ public: bool Leaving() const { return (m_eventType == wxEVT_LEAVE_WINDOW); } // Find the position of the event - void GetPosition(long *xpos, long *ypos) const { *xpos = m_x; *ypos = m_y; } - void Position(long *xpos, long *ypos) const { *xpos = m_x; *ypos = m_y; } + void GetPosition(long *xpos, long *ypos) const + { if (xpos) *xpos = m_x; + if (ypos) *ypos = m_y; } + void GetPosition(int *xpos, int *ypos) const + { if (xpos) *xpos = m_x; + if (ypos) *ypos = m_y; } + void Position(long *xpos, long *ypos) const + { if (xpos) *xpos = m_x; + if (ypos) *ypos = m_y; } // Find the position of the event wxPoint GetPosition() const { return wxPoint(m_x, m_y); } @@ -668,7 +675,11 @@ public: // Find the position of the event void GetPosition(long *xpos, long *ypos) const - { *xpos = m_x; *ypos = m_y; } + { if (xpos) *xpos = m_x; + if (ypos) *ypos = m_y; } + void GetPosition(int *xpos, int *ypos) const + { if (xpos) *xpos = m_x; + if (ypos) *ypos = m_y; } wxPoint GetPosition() const { return wxPoint(m_x, m_y); } diff --git a/include/wx/gtk/clipbrd.h b/include/wx/gtk/clipbrd.h index ebd46448d6..188ea58fcf 100644 --- a/include/wx/gtk/clipbrd.h +++ b/include/wx/gtk/clipbrd.h @@ -69,18 +69,19 @@ public: // clears wxTheClipboard and the system's clipboard if possible virtual void Clear(); + // flushes the clipboard: not available under GTK + virtual bool Flush() { return FALSE; } + /// If primary == TRUE, use primary selection in all further ops, /// primary=FALSE resets it. inline void UsePrimarySelection(bool primary = TRUE) { m_usePrimary = primary; } - // implementation - + // implementation bool m_open; - bool m_ownsClipboard; bool m_ownsPrimarySelection; + wxDataObject *m_data; - wxDataBroker *m_dataBroker; GtkWidget *m_clipboardWidget; /* for getting and offering data */ GtkWidget *m_targetsWidget; /* for getting list of supported formats */ bool m_waiting; /* querying data or formats is asynchronous */ diff --git a/include/wx/gtk/dataobj.h b/include/wx/gtk/dataobj.h index d79e1b63c8..7a0d22c781 100644 --- a/include/wx/gtk/dataobj.h +++ b/include/wx/gtk/dataobj.h @@ -32,123 +32,111 @@ class wxPrivateDataObject; class wxFileDataObject; //------------------------------------------------------------------------- -// wxDataFormat (internal) +// wxDataFormat //------------------------------------------------------------------------- -class wxDataFormat : public wxObject +class wxDataFormat { public: + // the clipboard formats under GDK are GdkAtoms + typedef GdkAtom NativeFormat; + wxDataFormat(); wxDataFormat( wxDataFormatId type ); wxDataFormat( const wxString &id ); wxDataFormat( const wxChar *id ); - wxDataFormat( const wxDataFormat &format ); - wxDataFormat( const GdkAtom atom ); - - void SetType( wxDataFormatId type ); - wxDataFormatId GetType() const; + wxDataFormat( NativeFormat format ); + + wxDataFormat& operator=(NativeFormat format) + { SetId(format); return *this; } + wxDataFormat& operator=(const wxDataFormat& format) + { SetId(format); return *this; } + + // comparison (must have both versions) + bool operator==(wxDataFormatId type) const + { return m_type == (wxDataFormatId)type; } + bool operator!=(wxDataFormatId type) const + { return m_type != (wxDataFormatId)type; } + bool operator==(NativeFormat format) const + { return m_format == (NativeFormat)format; } + bool operator!=(NativeFormat format) const + { return m_format != (NativeFormat)format; } + bool operator==(const wxDataFormat& format) const + { return m_format == format.m_format; } + bool operator!=(const wxDataFormat& format) const + { return m_format != format.m_format; } + + // explicit and implicit conversions to NativeFormat which is one of + // standard data types (implicit conversion is useful for preserving the + // compatibility with old code) + NativeFormat GetFormatId() const { return m_format; } + operator NativeFormat() const { return m_format; } + + // this only works with standard ids + void SetId( wxDataFormatId type ); - /* 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", an - * image manipulation program would put a wxBitmapDataObject and a - * wxPrivateDataObject to the clipboard - the latter with "image/png". */ + // this only works with standard ids + void SetId( NativeFormat format ); + // string ids are used for custom types - this SetId() must be used for + // application-specific formats wxString GetId() const; void SetId( const wxChar *id ); - GdkAtom GetAtom(); - void SetAtom(GdkAtom atom) { m_hasAtom = TRUE; m_atom = atom; } - - // implicit conversion to wxDataFormatId - operator wxDataFormatId() const { return m_type; } - - bool operator==(wxDataFormatId type) const { return m_type == type; } - bool operator!=(wxDataFormatId type) const { return m_type != type; } + // implementation + wxDataFormatId GetType() const; private: - wxDataFormatId m_type; - wxString m_id; - bool m_hasAtom; - GdkAtom m_atom; + wxDataFormatId m_type; + NativeFormat m_format; void PrepareFormats(); - -private: - DECLARE_CLASS( wxDataFormat ) -}; - -//------------------------------------------------------------------------- -// wxDataBroker (internal) -//------------------------------------------------------------------------- - -class wxDataBroker : public wxObject -{ -public: - /* constructor */ - wxDataBroker(); - - /* add data object */ - void Add( wxDataObject *dataObject, bool preferred = FALSE ); - -private: - /* OLE implementation, the methods don't need to be overridden */ - - /* get number of supported formats */ - virtual size_t GetFormatCount() const; - - /* return nth supported format */ - virtual wxDataFormat &GetNthFormat( size_t nth ) const; - - /* return preferrd/best supported format */ - virtual wxDataFormatId GetPreferredFormat() const; - - /* search through m_dataObjects, return TRUE if found */ - virtual bool IsSupportedFormat( wxDataFormat &format ) const; - - /* search through m_dataObjects and call child's GetSize() */ - virtual size_t GetSize( wxDataFormat& format ) const; - - /* search through m_dataObjects and call child's WriteData(dest) */ - virtual void WriteData( wxDataFormat& format, void *dest ) const; - -public: - /* implementation */ - wxList m_dataObjects; - size_t m_preferred; - -private: - DECLARE_CLASS( wxDataBroker ) + void SetType( wxDataFormatId type ); }; //---------------------------------------------------------------------------- -// wxDataObject to be placed in wxDataBroker +// wxDataObject //---------------------------------------------------------------------------- class wxDataObject : public wxObject { public: - /* constructor */ wxDataObject(); - - /* destructor */ ~wxDataObject(); - - /* write data to dest */ - virtual void WriteData( void *dest ) const = 0; - - /* get size of data */ - virtual size_t GetSize() const = 0; - -public: - /* implementation */ - wxDataFormat m_format; - wxDataFormat &GetFormat(); + virtual wxDataFormat GetPreferredFormat() const = 0; + + // get the number of formats we support: it is understood that if we + // can accept data in some format, then we can render data in this + // format as well, but the contrary is not necessarily true. For the + // default value of the argument, all formats we support should be + // returned, but if outputOnlyToo == FALSE, then we should only return + // the formats which our SetData() understands + virtual size_t GetFormatCount(bool outputOnlyToo = TRUE) const + { return 1; } + + // return all formats in the provided array (of size GetFormatCount()) + virtual void GetAllFormats(wxDataFormat *formats, + bool outputOnlyToo = TRUE) const + { formats[0] = GetPreferredFormat(); } + + // get the (total) size of data for the given format + virtual size_t GetDataSize(const wxDataFormat& format) const = 0; + + // copy raw data (in the specified format) to provided pointer + virtual bool GetDataHere(const wxDataFormat& format, void *buf) const = 0; + + // get data from the buffer (in the given format) + virtual bool SetData(const wxDataFormat& format, const void *buf) = 0; + + // a simpler name which makes more sense for data objects supporting + // only one format + wxDataFormat GetFormat() const { return GetPreferredFormat(); } - wxDataFormatId GetFormatType() const; - wxString GetFormatId() const; - GdkAtom GetFormatAtom() const; + // old interface + // decide if we support this format (can be either standard or custom + // format) -- now uses GetAllFormats() + virtual bool IsSupportedFormat(const wxDataFormat& format) const; private: DECLARE_DYNAMIC_CLASS( wxDataObject ) @@ -161,34 +149,27 @@ private: class wxTextDataObject : public wxDataObject { public: - /* default constructor. call SetText() later or override - WriteData() and GetSize() for working on-demand */ + // ctors wxTextDataObject(); - - /* constructor */ - wxTextDataObject( const wxString& data ); - - /* set current text data */ - void SetText( const wxString& data ); - - /* get current text data */ - wxString GetText() const; - - /* by default calls WriteString() with string set by constructor or - by SetText(). can be overridden for working on-demand */ - virtual void WriteData( void *dest ) const; - - /* by default, returns length of string as set by constructor or - by SetText(). can be overridden for working on-demand */ - virtual size_t GetSize() const; - - /* write string to dest */ - void WriteString( const wxString &str, void *dest ) const; - -public: - /* implementation */ - wxString m_data; + wxTextDataObject(const wxString& strText); + void Init(const wxString& strText) { m_strText = strText; } + + virtual wxDataFormat GetPreferredFormat() const + { return wxDF_TEXT; } + virtual bool IsSupportedFormat(const wxDataFormat& format) const + { return format == wxDF_TEXT; } + + virtual size_t GetDataSize(const wxDataFormat& format) const; + virtual bool GetDataHere(const wxDataFormat& format, void *buf) const; + virtual bool SetData(const wxDataFormat& format, const void *buf); + + // additional helpers + void SetText(const wxString& strText) { m_strText = strText; } + wxString GetText() const { return m_strText; } +private: + wxString m_strText; + private: DECLARE_DYNAMIC_CLASS( wxTextDataObject ) }; @@ -200,24 +181,21 @@ private: class wxFileDataObject : public wxDataObject { public: - /* default constructor */ wxFileDataObject(); - - /* add file name to list */ + void AddFile( const wxString &file ); - - /* get all filename as one string. each file name is 0 terminated, - the list is double zero terminated */ wxString GetFiles() const; - - /* write list of filenames */ - virtual void WriteData( void *dest ) const; - - /* return length of list of filenames */ - virtual size_t GetSize() const; + + virtual wxDataFormat GetPreferredFormat() const + { return wxDF_FILENAME; } + virtual bool IsSupportedFormat(const wxDataFormat& format) const + { return format == wxDF_FILENAME; } + + virtual size_t GetDataSize(const wxDataFormat& format) const; + virtual bool GetDataHere(const wxDataFormat& format, void *buf) const; + virtual bool SetData(const wxDataFormat& format, const void *buf); public: - /* implementation */ wxString m_files; private: @@ -231,26 +209,38 @@ private: class wxBitmapDataObject : public wxDataObject { public: - /* see wxTextDataObject for explanation */ + // ctors wxBitmapDataObject(); - wxBitmapDataObject( const wxBitmap& bitmap ); - ~wxBitmapDataObject(); - - void SetBitmap( const wxBitmap &bitmap ); - wxBitmap GetBitmap() const; + wxBitmapDataObject(const wxBitmap& bitmap); - virtual void WriteData( void *dest ) const; - virtual size_t GetSize() const; - void *GetData() const { return (void*)m_pngData; } - - void WriteBitmap( const wxBitmap &bitmap, void *dest ) const; + // destr + ~wxBitmapDataObject(); - void SetPngData( const char *pngData, size_t pngSize ); + // set/get our bitmap + void SetBitmap(const wxBitmap& bitmap); + const wxBitmap GetBitmap() const { return m_bitmap; } + + virtual wxDataFormat GetPreferredFormat() const + { return wxDF_BITMAP; } + virtual bool IsSupportedFormat(const wxDataFormat& format) const + { return format == wxDF_BITMAP; } + + virtual size_t GetDataSize(const wxDataFormat& format) const; + virtual bool GetDataHere(const wxDataFormat& format, void *buf) const; + + // sets PNG data + virtual bool SetData(const wxDataFormat& format, const void *buf); + // sets PNG data + virtual void SetPngData(const void *buf, size_t size); + + void *GetData() + { return m_pngData; } + private: wxBitmap m_bitmap; size_t m_pngSize; - char *m_pngData; + void *m_pngData; void DoConvertToPng(); diff --git a/include/wx/gtk/dnd.h b/include/wx/gtk/dnd.h index 0804af75c8..225c809209 100644 --- a/include/wx/gtk/dnd.h +++ b/include/wx/gtk/dnd.h @@ -119,27 +119,20 @@ public: // wxPrivateDropTarget //------------------------------------------------------------------------- +/* 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( long x, long y ); virtual bool OnDrop( long x, long y ); virtual bool OnData( long x, long y ); - /* you have to override OnDropData to get at the data */ virtual bool OnDropData( long x, long 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; } @@ -147,6 +140,7 @@ private: wxString m_id; }; +*/ //---------------------------------------------------------------------------- // A drop target which accepts files (dragged from File Manager or Explorer) @@ -162,9 +156,7 @@ public: virtual bool OnDrop( long x, long y ); virtual bool OnData( long x, long y ); - /* you have to override OnDropFiles to get at the file names */ virtual bool OnDropFiles( long x, long y, size_t nFiles, const wxChar * const aszFiles[] ) = 0; - }; //------------------------------------------------------------------------- @@ -187,23 +179,11 @@ public: /* 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 ); - // this one isn't portable to wxMSW -- deprecated - void SetData( wxDataObject *data ); - /* start drag action */ wxDragResult DoDragDrop( bool bAllowMove = FALSE ); @@ -218,7 +198,7 @@ public: GtkWidget *m_widget; wxWindow *m_window; wxDragResult m_retValue; - wxDataBroker *m_data; + wxDataObject *m_data; wxCursor m_defaultCursor; wxCursor m_goaheadCursor; diff --git a/include/wx/gtk1/clipbrd.h b/include/wx/gtk1/clipbrd.h index ebd46448d6..188ea58fcf 100644 --- a/include/wx/gtk1/clipbrd.h +++ b/include/wx/gtk1/clipbrd.h @@ -69,18 +69,19 @@ public: // clears wxTheClipboard and the system's clipboard if possible virtual void Clear(); + // flushes the clipboard: not available under GTK + virtual bool Flush() { return FALSE; } + /// If primary == TRUE, use primary selection in all further ops, /// primary=FALSE resets it. inline void UsePrimarySelection(bool primary = TRUE) { m_usePrimary = primary; } - // implementation - + // implementation bool m_open; - bool m_ownsClipboard; bool m_ownsPrimarySelection; + wxDataObject *m_data; - wxDataBroker *m_dataBroker; GtkWidget *m_clipboardWidget; /* for getting and offering data */ GtkWidget *m_targetsWidget; /* for getting list of supported formats */ bool m_waiting; /* querying data or formats is asynchronous */ diff --git a/include/wx/gtk1/dataobj.h b/include/wx/gtk1/dataobj.h index d79e1b63c8..7a0d22c781 100644 --- a/include/wx/gtk1/dataobj.h +++ b/include/wx/gtk1/dataobj.h @@ -32,123 +32,111 @@ class wxPrivateDataObject; class wxFileDataObject; //------------------------------------------------------------------------- -// wxDataFormat (internal) +// wxDataFormat //------------------------------------------------------------------------- -class wxDataFormat : public wxObject +class wxDataFormat { public: + // the clipboard formats under GDK are GdkAtoms + typedef GdkAtom NativeFormat; + wxDataFormat(); wxDataFormat( wxDataFormatId type ); wxDataFormat( const wxString &id ); wxDataFormat( const wxChar *id ); - wxDataFormat( const wxDataFormat &format ); - wxDataFormat( const GdkAtom atom ); - - void SetType( wxDataFormatId type ); - wxDataFormatId GetType() const; + wxDataFormat( NativeFormat format ); + + wxDataFormat& operator=(NativeFormat format) + { SetId(format); return *this; } + wxDataFormat& operator=(const wxDataFormat& format) + { SetId(format); return *this; } + + // comparison (must have both versions) + bool operator==(wxDataFormatId type) const + { return m_type == (wxDataFormatId)type; } + bool operator!=(wxDataFormatId type) const + { return m_type != (wxDataFormatId)type; } + bool operator==(NativeFormat format) const + { return m_format == (NativeFormat)format; } + bool operator!=(NativeFormat format) const + { return m_format != (NativeFormat)format; } + bool operator==(const wxDataFormat& format) const + { return m_format == format.m_format; } + bool operator!=(const wxDataFormat& format) const + { return m_format != format.m_format; } + + // explicit and implicit conversions to NativeFormat which is one of + // standard data types (implicit conversion is useful for preserving the + // compatibility with old code) + NativeFormat GetFormatId() const { return m_format; } + operator NativeFormat() const { return m_format; } + + // this only works with standard ids + void SetId( wxDataFormatId type ); - /* 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", an - * image manipulation program would put a wxBitmapDataObject and a - * wxPrivateDataObject to the clipboard - the latter with "image/png". */ + // this only works with standard ids + void SetId( NativeFormat format ); + // string ids are used for custom types - this SetId() must be used for + // application-specific formats wxString GetId() const; void SetId( const wxChar *id ); - GdkAtom GetAtom(); - void SetAtom(GdkAtom atom) { m_hasAtom = TRUE; m_atom = atom; } - - // implicit conversion to wxDataFormatId - operator wxDataFormatId() const { return m_type; } - - bool operator==(wxDataFormatId type) const { return m_type == type; } - bool operator!=(wxDataFormatId type) const { return m_type != type; } + // implementation + wxDataFormatId GetType() const; private: - wxDataFormatId m_type; - wxString m_id; - bool m_hasAtom; - GdkAtom m_atom; + wxDataFormatId m_type; + NativeFormat m_format; void PrepareFormats(); - -private: - DECLARE_CLASS( wxDataFormat ) -}; - -//------------------------------------------------------------------------- -// wxDataBroker (internal) -//------------------------------------------------------------------------- - -class wxDataBroker : public wxObject -{ -public: - /* constructor */ - wxDataBroker(); - - /* add data object */ - void Add( wxDataObject *dataObject, bool preferred = FALSE ); - -private: - /* OLE implementation, the methods don't need to be overridden */ - - /* get number of supported formats */ - virtual size_t GetFormatCount() const; - - /* return nth supported format */ - virtual wxDataFormat &GetNthFormat( size_t nth ) const; - - /* return preferrd/best supported format */ - virtual wxDataFormatId GetPreferredFormat() const; - - /* search through m_dataObjects, return TRUE if found */ - virtual bool IsSupportedFormat( wxDataFormat &format ) const; - - /* search through m_dataObjects and call child's GetSize() */ - virtual size_t GetSize( wxDataFormat& format ) const; - - /* search through m_dataObjects and call child's WriteData(dest) */ - virtual void WriteData( wxDataFormat& format, void *dest ) const; - -public: - /* implementation */ - wxList m_dataObjects; - size_t m_preferred; - -private: - DECLARE_CLASS( wxDataBroker ) + void SetType( wxDataFormatId type ); }; //---------------------------------------------------------------------------- -// wxDataObject to be placed in wxDataBroker +// wxDataObject //---------------------------------------------------------------------------- class wxDataObject : public wxObject { public: - /* constructor */ wxDataObject(); - - /* destructor */ ~wxDataObject(); - - /* write data to dest */ - virtual void WriteData( void *dest ) const = 0; - - /* get size of data */ - virtual size_t GetSize() const = 0; - -public: - /* implementation */ - wxDataFormat m_format; - wxDataFormat &GetFormat(); + virtual wxDataFormat GetPreferredFormat() const = 0; + + // get the number of formats we support: it is understood that if we + // can accept data in some format, then we can render data in this + // format as well, but the contrary is not necessarily true. For the + // default value of the argument, all formats we support should be + // returned, but if outputOnlyToo == FALSE, then we should only return + // the formats which our SetData() understands + virtual size_t GetFormatCount(bool outputOnlyToo = TRUE) const + { return 1; } + + // return all formats in the provided array (of size GetFormatCount()) + virtual void GetAllFormats(wxDataFormat *formats, + bool outputOnlyToo = TRUE) const + { formats[0] = GetPreferredFormat(); } + + // get the (total) size of data for the given format + virtual size_t GetDataSize(const wxDataFormat& format) const = 0; + + // copy raw data (in the specified format) to provided pointer + virtual bool GetDataHere(const wxDataFormat& format, void *buf) const = 0; + + // get data from the buffer (in the given format) + virtual bool SetData(const wxDataFormat& format, const void *buf) = 0; + + // a simpler name which makes more sense for data objects supporting + // only one format + wxDataFormat GetFormat() const { return GetPreferredFormat(); } - wxDataFormatId GetFormatType() const; - wxString GetFormatId() const; - GdkAtom GetFormatAtom() const; + // old interface + // decide if we support this format (can be either standard or custom + // format) -- now uses GetAllFormats() + virtual bool IsSupportedFormat(const wxDataFormat& format) const; private: DECLARE_DYNAMIC_CLASS( wxDataObject ) @@ -161,34 +149,27 @@ private: class wxTextDataObject : public wxDataObject { public: - /* default constructor. call SetText() later or override - WriteData() and GetSize() for working on-demand */ + // ctors wxTextDataObject(); - - /* constructor */ - wxTextDataObject( const wxString& data ); - - /* set current text data */ - void SetText( const wxString& data ); - - /* get current text data */ - wxString GetText() const; - - /* by default calls WriteString() with string set by constructor or - by SetText(). can be overridden for working on-demand */ - virtual void WriteData( void *dest ) const; - - /* by default, returns length of string as set by constructor or - by SetText(). can be overridden for working on-demand */ - virtual size_t GetSize() const; - - /* write string to dest */ - void WriteString( const wxString &str, void *dest ) const; - -public: - /* implementation */ - wxString m_data; + wxTextDataObject(const wxString& strText); + void Init(const wxString& strText) { m_strText = strText; } + + virtual wxDataFormat GetPreferredFormat() const + { return wxDF_TEXT; } + virtual bool IsSupportedFormat(const wxDataFormat& format) const + { return format == wxDF_TEXT; } + + virtual size_t GetDataSize(const wxDataFormat& format) const; + virtual bool GetDataHere(const wxDataFormat& format, void *buf) const; + virtual bool SetData(const wxDataFormat& format, const void *buf); + + // additional helpers + void SetText(const wxString& strText) { m_strText = strText; } + wxString GetText() const { return m_strText; } +private: + wxString m_strText; + private: DECLARE_DYNAMIC_CLASS( wxTextDataObject ) }; @@ -200,24 +181,21 @@ private: class wxFileDataObject : public wxDataObject { public: - /* default constructor */ wxFileDataObject(); - - /* add file name to list */ + void AddFile( const wxString &file ); - - /* get all filename as one string. each file name is 0 terminated, - the list is double zero terminated */ wxString GetFiles() const; - - /* write list of filenames */ - virtual void WriteData( void *dest ) const; - - /* return length of list of filenames */ - virtual size_t GetSize() const; + + virtual wxDataFormat GetPreferredFormat() const + { return wxDF_FILENAME; } + virtual bool IsSupportedFormat(const wxDataFormat& format) const + { return format == wxDF_FILENAME; } + + virtual size_t GetDataSize(const wxDataFormat& format) const; + virtual bool GetDataHere(const wxDataFormat& format, void *buf) const; + virtual bool SetData(const wxDataFormat& format, const void *buf); public: - /* implementation */ wxString m_files; private: @@ -231,26 +209,38 @@ private: class wxBitmapDataObject : public wxDataObject { public: - /* see wxTextDataObject for explanation */ + // ctors wxBitmapDataObject(); - wxBitmapDataObject( const wxBitmap& bitmap ); - ~wxBitmapDataObject(); - - void SetBitmap( const wxBitmap &bitmap ); - wxBitmap GetBitmap() const; + wxBitmapDataObject(const wxBitmap& bitmap); - virtual void WriteData( void *dest ) const; - virtual size_t GetSize() const; - void *GetData() const { return (void*)m_pngData; } - - void WriteBitmap( const wxBitmap &bitmap, void *dest ) const; + // destr + ~wxBitmapDataObject(); - void SetPngData( const char *pngData, size_t pngSize ); + // set/get our bitmap + void SetBitmap(const wxBitmap& bitmap); + const wxBitmap GetBitmap() const { return m_bitmap; } + + virtual wxDataFormat GetPreferredFormat() const + { return wxDF_BITMAP; } + virtual bool IsSupportedFormat(const wxDataFormat& format) const + { return format == wxDF_BITMAP; } + + virtual size_t GetDataSize(const wxDataFormat& format) const; + virtual bool GetDataHere(const wxDataFormat& format, void *buf) const; + + // sets PNG data + virtual bool SetData(const wxDataFormat& format, const void *buf); + // sets PNG data + virtual void SetPngData(const void *buf, size_t size); + + void *GetData() + { return m_pngData; } + private: wxBitmap m_bitmap; size_t m_pngSize; - char *m_pngData; + void *m_pngData; void DoConvertToPng(); diff --git a/include/wx/gtk1/dnd.h b/include/wx/gtk1/dnd.h index 0804af75c8..225c809209 100644 --- a/include/wx/gtk1/dnd.h +++ b/include/wx/gtk1/dnd.h @@ -119,27 +119,20 @@ public: // wxPrivateDropTarget //------------------------------------------------------------------------- +/* 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( long x, long y ); virtual bool OnDrop( long x, long y ); virtual bool OnData( long x, long y ); - /* you have to override OnDropData to get at the data */ virtual bool OnDropData( long x, long 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; } @@ -147,6 +140,7 @@ private: wxString m_id; }; +*/ //---------------------------------------------------------------------------- // A drop target which accepts files (dragged from File Manager or Explorer) @@ -162,9 +156,7 @@ public: virtual bool OnDrop( long x, long y ); virtual bool OnData( long x, long y ); - /* you have to override OnDropFiles to get at the file names */ virtual bool OnDropFiles( long x, long y, size_t nFiles, const wxChar * const aszFiles[] ) = 0; - }; //------------------------------------------------------------------------- @@ -187,23 +179,11 @@ public: /* 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 ); - // this one isn't portable to wxMSW -- deprecated - void SetData( wxDataObject *data ); - /* start drag action */ wxDragResult DoDragDrop( bool bAllowMove = FALSE ); @@ -218,7 +198,7 @@ public: GtkWidget *m_widget; wxWindow *m_window; wxDragResult m_retValue; - wxDataBroker *m_data; + wxDataObject *m_data; wxCursor m_defaultCursor; wxCursor m_goaheadCursor; diff --git a/samples/dnd/dnd.cpp b/samples/dnd/dnd.cpp index 6bd041c924..99267a4a72 100644 --- a/samples/dnd/dnd.cpp +++ b/samples/dnd/dnd.cpp @@ -181,7 +181,7 @@ public: // to implement in derived classes virtual Kind GetKind() const = 0; - virtual void Draw(wxDC& dc) = 0 + virtual void Draw(wxDC& dc) { dc.SetPen(wxPen(m_col, 1, wxSOLID)); } @@ -228,7 +228,9 @@ public: dc.DrawLine(p2, p3); dc.DrawLine(p3, p1); +#ifdef __WXMSW__ dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER); +#endif } }; @@ -257,7 +259,9 @@ public: dc.DrawLine(p3, p4); dc.DrawLine(p4, p1); +#ifdef __WXMSW__ dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER); +#endif } }; @@ -278,7 +282,9 @@ public: dc.DrawEllipse(m_pos, m_size); +#ifdef __WXMSW__ dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER); +#endif } }; @@ -654,9 +660,11 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) wxTE_MULTILINE | wxTE_READONLY | wxSUNKEN_BORDER ); +#ifdef __WXMSW__ // redirect log messages to the text window and switch on OLE messages // logging wxLog::AddTraceMask(wxTRACE_OleCalls); +#endif m_pLog = new wxLogTextCtrl(m_ctrlLog); m_pLogPrev = wxLog::SetActiveTarget(m_pLog); diff --git a/src/gtk/clipbrd.cpp b/src/gtk/clipbrd.cpp index d981a17e76..9a9e38ee22 100644 --- a/src/gtk/clipbrd.cpp +++ b/src/gtk/clipbrd.cpp @@ -100,7 +100,8 @@ targets_selection_received( GtkWidget *WXUNUSED(widget), for (unsigned int i=0; ilength/sizeof(GdkAtom); i++) { -/* char *name = gdk_atom_name (atoms[i]); +/* + char *name = gdk_atom_name (atoms[i]); if (name) printf( "Format available: %s.\n", name ); */ if (atoms[i] == clipboard->m_targetRequested) @@ -134,7 +135,7 @@ selection_received( GtkWidget *WXUNUSED(widget), } wxDataObject *data_object = clipboard->m_receivedData; - + if (!data_object) { clipboard->m_waiting = FALSE; @@ -148,7 +149,7 @@ selection_received( GtkWidget *WXUNUSED(widget), } /* make sure we got the data in the correct format */ - if (data_object->GetFormat().GetAtom() != selection_data->target) + if (data_object->GetFormat() != selection_data->target) { clipboard->m_waiting = FALSE; return; @@ -186,7 +187,7 @@ selection_received( GtkWidget *WXUNUSED(widget), wxBitmapDataObject *bitmap_object = (wxBitmapDataObject *) data_object; - bitmap_object->SetPngData( (const char*) selection_data->data, (size_t) selection_data->length ); + bitmap_object->SetPngData( (const void*) selection_data->data, (size_t) selection_data->length ); break; } @@ -245,10 +246,10 @@ selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event ) (!wxTheClipboard->m_ownsClipboard)) { /* the clipboard is no longer in our hands. we can the delete clipboard data. */ - if (wxTheClipboard->m_dataBroker) + if (wxTheClipboard->m_data) { - delete wxTheClipboard->m_dataBroker; - wxTheClipboard->m_dataBroker = (wxDataBroker*) NULL; + delete wxTheClipboard->m_data; + wxTheClipboard->m_data = (wxDataObject*) NULL; } } @@ -265,82 +266,64 @@ selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data { if (!wxTheClipboard) return; - if (!wxTheClipboard->m_dataBroker) return; + if (!wxTheClipboard->m_data) return; - wxNode *node = wxTheClipboard->m_dataBroker->m_dataObjects.First(); + wxDataObject *data = wxTheClipboard->m_data; - while (node) + if (!data->IsSupportedFormat( selection_data->target )) return; + + if (data->GetFormat().GetType() == wxDF_TEXT) { - wxDataObject *data_object = (wxDataObject *)node->Data(); - - if (data_object->GetFormat().GetAtom() != selection_data->target) - { - node = node->Next(); - continue; - } - - switch (data_object->GetFormat().GetType()) - { - case wxDF_TEXT: - { - wxTextDataObject *text_object = (wxTextDataObject*) data_object; - - wxString text = text_object->GetText(); - + wxTextDataObject *text_object = (wxTextDataObject*) data; + wxString text( text_object->GetText() ); + #if wxUSE_UNICODE - const wxWX2MBbuf s = text.mbc_str(); - int len = strlen(s); + const wxWX2MBbuf s = text.mbc_str(); + int len = strlen(s); #else // more efficient in non-Unicode - const char *s = text.c_str(); - int len = (int) text.Length(); + const char *s = text.c_str(); + int len = (int) text.Length(); #endif - - gtk_selection_data_set( - selection_data, - GDK_SELECTION_TYPE_STRING, - 8*sizeof(gchar), - (unsigned char*) (const char*) s, - len ); + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) (const char*) s, + len ); - break; - } - - case wxDF_BITMAP: - { - wxBitmapDataObject *bitmap_object = (wxBitmapDataObject*) data_object; - - if (bitmap_object->GetSize() == 0) return; - - gtk_selection_data_set( - selection_data, - GDK_SELECTION_TYPE_STRING, - 8*sizeof(gchar), - (unsigned char*) bitmap_object->GetData(), - (int) bitmap_object->GetSize() ); - - break; - } - - case wxDF_PRIVATE: - { - wxPrivateDataObject *private_object = (wxPrivateDataObject*) data_object; + return; + } + + if (data->GetFormat().GetType() == wxDF_BITMAP) + { + wxBitmapDataObject *bitmap_object = (wxBitmapDataObject*) data; - if (private_object->GetSize() == 0) return; + if (bitmap_object->GetDataSize(wxDF_BITMAP) == 0) return; - gtk_selection_data_set( - selection_data, - GDK_SELECTION_TYPE_STRING, - 8*sizeof(gchar), - (unsigned char*) private_object->GetData(), - (int) private_object->GetSize() ); - } + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) bitmap_object->GetData(), + (int) bitmap_object->GetDataSize(wxDF_BITMAP) ); - default: - break; - } - - node = node->Next(); + return; } + + int size = data->GetDataSize( selection_data->target ); + + if (size == 0) return; + + char *d = new char[size]; + + data->GetDataHere( selection_data->target, (void*) d ); + + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) d, + size ); } //----------------------------------------------------------------------------- @@ -356,8 +339,7 @@ wxClipboard::wxClipboard() m_ownsClipboard = FALSE; m_ownsPrimarySelection = FALSE; - m_dataBroker = (wxDataBroker*) NULL; - + m_data = (wxDataObject*) NULL; m_receivedData = (wxDataObject*) NULL; /* we use m_targetsWidget to query what formats are available */ @@ -404,8 +386,8 @@ wxClipboard::~wxClipboard() void wxClipboard::Clear() { - if (m_dataBroker) - { + if (m_data) + { #if wxUSE_THREADS /* disable GUI threads */ wxapp_uninstall_thread_wakeup(); @@ -431,10 +413,10 @@ void wxClipboard::Clear() while (m_waiting) gtk_main_iteration(); } - if (m_dataBroker) - { - delete m_dataBroker; - m_dataBroker = (wxDataBroker*) NULL; + if (m_data) + { + delete m_data; + m_data = (wxDataObject*) NULL; } #if wxUSE_THREADS @@ -444,7 +426,6 @@ void wxClipboard::Clear() } m_targetRequested = 0; - m_formatSupported = FALSE; } @@ -474,14 +455,13 @@ bool wxClipboard::AddData( wxDataObject *data ) wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); - /* if clipboard has been cleared before, create new data broker */ - if (!m_dataBroker) m_dataBroker = new wxDataBroker(); + // we can only store one wxDataObject + Clear(); - /* add new data to list of offered data objects */ - m_dataBroker->Add( data ); - + m_data = data; + /* get native format id of new data object */ - GdkAtom format = data->GetFormat().GetAtom(); + GdkAtom format = data->GetFormat(); wxCHECK_MSG( format, FALSE, wxT("data has invalid format") ); @@ -570,11 +550,9 @@ void wxClipboard::Close() bool wxClipboard::IsSupported( wxDataFormat format ) { - wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); - /* store requested format to be asked for by callbacks */ - m_targetRequested = format.GetAtom(); + m_targetRequested = format; wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); @@ -591,7 +569,7 @@ bool wxClipboard::IsSupported( wxDataFormat format ) m_waiting = TRUE; gtk_selection_convert( m_targetsWidget, - m_usePrimary?GDK_SELECTION_PRIMARY:g_clipboardAtom, + m_usePrimary ? GDK_SELECTION_PRIMARY : g_clipboardAtom, g_targetsAtom, GDK_CURRENT_TIME ); @@ -616,7 +594,7 @@ bool wxClipboard::GetData( wxDataObject *data ) /* store requested format to be asked for by callbacks */ - m_targetRequested = data->GetFormat().GetAtom(); + m_targetRequested = data->GetFormat(); wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); @@ -636,7 +614,7 @@ bool wxClipboard::GetData( wxDataObject *data ) m_waiting = TRUE; gtk_selection_convert( m_clipboardWidget, - m_usePrimary?GDK_SELECTION_PRIMARY:g_clipboardAtom, + m_usePrimary ? GDK_SELECTION_PRIMARY : g_clipboardAtom, m_targetRequested, GDK_CURRENT_TIME ); diff --git a/src/gtk/dataobj.cpp b/src/gtk/dataobj.cpp index d1668eb212..401dca0db0 100644 --- a/src/gtk/dataobj.cpp +++ b/src/gtk/dataobj.cpp @@ -26,19 +26,17 @@ GdkAtom g_textAtom = 0; GdkAtom g_pngAtom = 0; +GdkAtom g_fileAtom = 0; //------------------------------------------------------------------------- // wxDataFormat //------------------------------------------------------------------------- -IMPLEMENT_CLASS(wxDataFormat, wxObject) - wxDataFormat::wxDataFormat() { PrepareFormats(); m_type = wxDF_INVALID; - m_hasAtom = FALSE; - m_atom = (GdkAtom) 0; + m_format = (GdkAtom) 0; } wxDataFormat::wxDataFormat( wxDataFormatId type ) @@ -59,39 +57,10 @@ wxDataFormat::wxDataFormat( const wxString &id ) SetId( id ); } -wxDataFormat::wxDataFormat( const wxDataFormat &format ) -{ - PrepareFormats(); - m_type = format.GetType(); - m_id = format.GetId(); - m_hasAtom = TRUE; - m_atom = ((wxDataFormat &)format).GetAtom(); // const_cast -} - -wxDataFormat::wxDataFormat( const GdkAtom atom ) +wxDataFormat::wxDataFormat( NativeFormat format ) { PrepareFormats(); - m_hasAtom = TRUE; - - m_atom = atom; - - if (m_atom == g_textAtom) - { - m_type = wxDF_TEXT; - } else - if (m_atom == GDK_TARGET_BITMAP) - { - m_type = wxDF_BITMAP; - } else - { - m_type = wxDF_PRIVATE; - m_id = gdk_atom_name( m_atom ); - - if (m_id == wxT("file:ALL")) - { - m_type = wxDF_FILENAME; - } - } + SetId( format ); } void wxDataFormat::SetType( wxDataFormatId type ) @@ -99,25 +68,17 @@ void wxDataFormat::SetType( wxDataFormatId type ) m_type = type; if (m_type == wxDF_TEXT) - { - m_id = wxT("STRING"); - } + m_format = g_textAtom; else if (m_type == wxDF_BITMAP) - { - m_id = wxT("image/png"); - } + m_format = g_pngAtom; else if (m_type == wxDF_FILENAME) - { - m_id = wxT("file:ALL"); - } + m_format = g_fileAtom; else { wxFAIL_MSG( wxT("invalid dataformat") ); } - - m_hasAtom = FALSE; } wxDataFormatId wxDataFormat::GetType() const @@ -127,152 +88,41 @@ wxDataFormatId wxDataFormat::GetType() const wxString wxDataFormat::GetId() const { - return m_id; -} - -void wxDataFormat::SetId( const wxChar *id ) -{ - m_type = wxDF_PRIVATE; - m_id = id; - m_hasAtom = FALSE; -} - -GdkAtom wxDataFormat::GetAtom() -{ - if (!m_hasAtom) - { - m_hasAtom = TRUE; - - if (m_type == wxDF_TEXT) - { - m_atom = g_textAtom; - } - else - if (m_type == wxDF_BITMAP) - { - m_atom = GDK_TARGET_BITMAP; - } - else - if (m_type == wxDF_PRIVATE) - { - m_atom = gdk_atom_intern( wxMBSTRINGCAST m_id.mbc_str(), FALSE ); - } - else - if (m_type == wxDF_FILENAME) - { - m_atom = gdk_atom_intern( "file:ALL", FALSE ); - } - else - { - m_hasAtom = FALSE; - m_atom = (GdkAtom) 0; - } - } - - return m_atom; -} - -void wxDataFormat::PrepareFormats() -{ - if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE ); - if (!g_pngAtom) g_pngAtom = gdk_atom_intern( "image/png", FALSE ); -} - -//------------------------------------------------------------------------- -// wxDataBroker -//------------------------------------------------------------------------- - -IMPLEMENT_CLASS(wxDataBroker,wxObject) - -wxDataBroker::wxDataBroker() -{ - m_dataObjects.DeleteContents(TRUE); - m_preferred = 0; -} - -void wxDataBroker::Add( wxDataObject *dataObject, bool preferred ) -{ - if (preferred) m_preferred = m_dataObjects.GetCount(); - m_dataObjects.Append( dataObject ); -} - -size_t wxDataBroker::GetFormatCount() const -{ - return m_dataObjects.GetCount(); -} - -wxDataFormatId wxDataBroker::GetPreferredFormat() const -{ - wxNode *node = m_dataObjects.Nth( m_preferred ); - - wxASSERT( node ); - - wxDataObject* data_obj = (wxDataObject*)node->Data(); - - return data_obj->GetFormat().GetType(); -} - -wxDataFormat &wxDataBroker::GetNthFormat( size_t nth ) const -{ - wxNode *node = m_dataObjects.Nth( nth ); - - wxASSERT( node ); - - wxDataObject* data_obj = (wxDataObject*)node->Data(); - - return data_obj->GetFormat(); + wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode + return ret; } -bool wxDataBroker::IsSupportedFormat( wxDataFormat &format ) const +void wxDataFormat::SetId( NativeFormat format ) { - wxNode *node = m_dataObjects.First(); - while (node) - { - wxDataObject *dobj = (wxDataObject*)node->Data(); - - if (dobj->GetFormat().GetAtom() == format.GetAtom()) - { - return TRUE; - } - - node = node->Next(); - } + m_format = format; - return FALSE; + if (m_format == g_textAtom) + m_type = wxDF_TEXT; + else + if (m_format == g_pngAtom) + m_type = wxDF_BITMAP; + else + if (m_format == g_fileAtom) + m_type = wxDF_FILENAME; + else + m_type = wxDF_PRIVATE; } -size_t wxDataBroker::GetSize( wxDataFormat& format ) const +void wxDataFormat::SetId( const wxChar *id ) { - wxNode *node = m_dataObjects.First(); - while (node) - { - wxDataObject *dobj = (wxDataObject*)node->Data(); - - if (dobj->GetFormat().GetAtom() == format.GetAtom()) - { - return dobj->GetSize(); - } - - node = node->Next(); - } - - return 0; + m_type = wxDF_PRIVATE; + wxString tmp( id ); + m_format = gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); // what is the string cast for? } -void wxDataBroker::WriteData( wxDataFormat& format, void *dest ) const +void wxDataFormat::PrepareFormats() { - wxNode *node = m_dataObjects.First(); - while (node) - { - wxDataObject *dobj = (wxDataObject*)node->Data(); - - if (dobj->GetFormat().GetAtom() == format.GetAtom()) - { - dobj->WriteData( dest ); - } - - node = node->Next(); - } + if (!g_textAtom) + g_textAtom = gdk_atom_intern( "STRING", FALSE ); + if (!g_pngAtom) + g_pngAtom = gdk_atom_intern( "image/png", FALSE ); + if (!g_fileAtom) + g_fileAtom = gdk_atom_intern( "file:ALL", FALSE ); } //------------------------------------------------------------------------- @@ -289,26 +139,29 @@ wxDataObject::~wxDataObject() { } -wxDataFormat &wxDataObject::GetFormat() +bool wxDataObject::IsSupportedFormat(const wxDataFormat& format) const { - return m_format; -} + size_t nFormatCount = GetFormatCount(); + if ( nFormatCount == 1 ) { + return format == GetPreferredFormat(); + } + else { + wxDataFormat *formats = new wxDataFormat[nFormatCount]; + GetAllFormats(formats); + + size_t n; + for ( n = 0; n < nFormatCount; n++ ) { + if ( formats[n] == format ) + break; + } -wxDataFormatId wxDataObject::GetFormatType() const -{ - return m_format.GetType(); -} + delete [] formats; -wxString wxDataObject::GetFormatId() const -{ - return m_format.GetId(); + // found? + return n < nFormatCount; + } } -GdkAtom wxDataObject::GetFormatAtom() const -{ - GdkAtom ret = ((wxDataObject*) this)->m_format.GetAtom(); - return ret; -} // ---------------------------------------------------------------------------- // wxTextDataObject @@ -316,41 +169,30 @@ GdkAtom wxDataObject::GetFormatAtom() const IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject, wxDataObject ) -wxTextDataObject::wxTextDataObject() -{ - m_format.SetType( wxDF_TEXT ); -} - -wxTextDataObject::wxTextDataObject( const wxString& data ) -{ - m_format.SetType( wxDF_TEXT ); - - m_data = data; +wxTextDataObject::wxTextDataObject() +{ } -void wxTextDataObject::SetText( const wxString& data ) -{ - m_data = data; +wxTextDataObject::wxTextDataObject(const wxString& strText) + : m_strText(strText) +{ } -wxString wxTextDataObject::GetText() const -{ - return m_data; +size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const +{ + return m_strText.Len() + 1; // +1 for trailing '\0'of course } -void wxTextDataObject::WriteData( void *dest ) const -{ - WriteString( m_data, dest ); +bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const +{ + memcpy(buf, m_strText.c_str(), GetDataSize(format)); + return TRUE; } -size_t wxTextDataObject::GetSize() const -{ - return m_data.Len() + 1; -} - -void wxTextDataObject::WriteString( const wxString &str, void *dest ) const -{ - memcpy( dest, str.mb_str(), str.Len()+1 ); +bool wxTextDataObject::SetData(const wxDataFormat& format, const void *buf) +{ + m_strText = (const wxChar *)buf; + return TRUE; } // ---------------------------------------------------------------------------- @@ -361,7 +203,6 @@ IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject, wxDataObject ) wxFileDataObject::wxFileDataObject() { - m_format.SetType( wxDF_FILENAME ); } void wxFileDataObject::AddFile( const wxString &file ) @@ -375,16 +216,34 @@ wxString wxFileDataObject::GetFiles() const return m_files; } -void wxFileDataObject::WriteData( void *dest ) const +bool wxFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const { - memcpy( dest, m_files.mbc_str(), GetSize() ); + if (format == wxDF_FILENAME) + { + memcpy( buf, m_files.mbc_str(), m_files.Len() + 1 ); + return TRUE; + } + + return FALSE; } -size_t wxFileDataObject::GetSize() const +size_t wxFileDataObject::GetDataSize(const wxDataFormat& format) const { + if (format != wxDF_FILENAME) return 0; + return m_files.Len() + 1; } +bool wxFileDataObject::SetData(const wxDataFormat& format, const void *buf) +{ + if (format != wxDF_FILENAME) + return FALSE; + + m_files = (char*)(buf); // this is so ugly, I cannot look at it + + return TRUE; +} + // ---------------------------------------------------------------------------- // wxBitmapDataObject // ---------------------------------------------------------------------------- @@ -393,15 +252,13 @@ IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject, wxDataObject ) wxBitmapDataObject::wxBitmapDataObject() { - m_format.SetType( wxDF_BITMAP ); - m_pngData = (char*)NULL; + m_pngData = (void*)NULL; m_pngSize = 0; } wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) { - m_format.SetType( wxDF_BITMAP ); - m_pngData = (char*)NULL; + m_pngData = (void*)NULL; m_pngSize = 0; m_bitmap = bitmap; DoConvertToPng(); @@ -409,45 +266,60 @@ wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) wxBitmapDataObject::~wxBitmapDataObject() { - if (m_pngData) delete[] m_pngData; + if (m_pngData) + delete[] m_pngData; } void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap ) { + if (m_pngData) + delete[] m_pngData; + m_pngData = (void*)NULL; + m_pngSize = 0; + m_bitmap = bitmap; DoConvertToPng(); } -wxBitmap wxBitmapDataObject::GetBitmap() const +size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const { - return m_bitmap; -} - -void wxBitmapDataObject::WriteData( void *dest ) const -{ - WriteBitmap( m_bitmap, dest ); + if (format != wxDF_BITMAP) return 0; + + return m_pngSize; } -size_t wxBitmapDataObject::GetSize() const +bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, void *buf) const { - return m_pngSize; + if (format != wxDF_BITMAP) return FALSE; + + if (m_pngSize > 0) + { + memcpy(buf, m_pngData, m_pngSize); + return TRUE; + } + + return FALSE; } -void wxBitmapDataObject::WriteBitmap( const wxBitmap &WXUNUSED(bitmap), void *dest ) const +bool wxBitmapDataObject::SetData(const wxDataFormat& format, const void *buf) { -// if (m_bitmap == bitmap) - memcpy( dest, m_pngData, m_pngSize ); + if (m_pngData) delete[] m_pngData; + m_pngData = (void*) NULL; + m_pngSize = 0; + m_bitmap = wxNullBitmap; + + return FALSE; } -void wxBitmapDataObject::SetPngData( const char *pngData, size_t pngSize ) +void wxBitmapDataObject::SetPngData(const void *buf, size_t size) { if (m_pngData) delete[] m_pngData; - m_pngData = (char*) NULL; - m_pngSize = pngSize; - m_pngData = new char[m_pngSize]; - memcpy( m_pngData, pngData, m_pngSize ); + m_pngSize = size; + m_pngData = (void*) new char[m_pngSize]; - wxMemoryInputStream mstream( pngData, pngSize ); + memcpy( m_pngData, buf, m_pngSize ); + + wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize ); wxImage image; wxPNGHandler handler; handler.LoadFile( &image, mstream ); @@ -456,7 +328,7 @@ void wxBitmapDataObject::SetPngData( const char *pngData, size_t pngSize ) void wxBitmapDataObject::DoConvertToPng() { - if (m_pngData) delete[] m_pngData; + if (!m_bitmap.Ok()) return; wxImage image( m_bitmap ); wxPNGHandler handler; @@ -464,9 +336,9 @@ void wxBitmapDataObject::DoConvertToPng() wxCountingOutputStream count; handler.SaveFile( &image, count ); m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? - m_pngData = new char[m_pngSize]; + m_pngData = (void*) new char[m_pngSize]; - wxMemoryOutputStream mstream( m_pngData, m_pngSize ); + wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize ); handler.SaveFile( &image, mstream ); } @@ -474,7 +346,7 @@ void wxBitmapDataObject::DoConvertToPng() // wxPrivateDataObject // ---------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject, wxDataObject ) +IMPLEMENT_CLASS( wxPrivateDataObject, wxDataObject ) void wxPrivateDataObject::Free() { diff --git a/src/gtk/dnd.cpp b/src/gtk/dnd.cpp index 11fcc9c080..dbf3456d02 100644 --- a/src/gtk/dnd.cpp +++ b/src/gtk/dnd.cpp @@ -380,7 +380,7 @@ bool wxDropTarget::RequestData( wxDataFormat format ) /* this should trigger an "drag_data_received" event */ gtk_drag_get_data( m_dragWidget, m_dragContext, - format.GetAtom(), + format, m_dragTime ); #if wxUSE_THREADS @@ -403,7 +403,7 @@ bool wxDropTarget::IsSupported( wxDataFormat format ) // char *name = gdk_atom_name( formatAtom ); // if (name) printf( "Format available: %s.\n", name ); - if (formatAtom == format.GetAtom()) return TRUE; + if (formatAtom == format) return TRUE; child = child->next; } @@ -414,7 +414,7 @@ bool wxDropTarget::GetData( wxDataObject *data_object ) { if (!m_dragData) return FALSE; - if (m_dragData->target != data_object->GetFormat().GetAtom()) return FALSE; + if (m_dragData->target != data_object->GetFormat()) return FALSE; if (data_object->GetFormat().GetType() == wxDF_TEXT) { @@ -521,6 +521,7 @@ bool wxTextDropTarget::OnData( long x, long y ) // wxPrivateDropTarget //------------------------------------------------------------------------- +/* wxPrivateDropTarget::wxPrivateDropTarget() { m_id = wxTheApp->GetAppName(); @@ -558,6 +559,7 @@ bool wxPrivateDropTarget::OnData( long x, long y ) return TRUE; } +*/ //---------------------------------------------------------------------------- // A drop target which accepts files (dragged from File Manager or Explorer) @@ -584,7 +586,7 @@ bool wxFileDropTarget::OnData( long x, long y ) wxFileDataObject data; if (!GetData( &data )) return FALSE; - /* get number of substrings /root/mytext.txt/0/root/myothertext.txt/0/0 */ + // get number of substrings /root/mytext.txt/0/root/myothertext.txt/0/0 size_t number = 0; size_t i; size_t size = data.GetFiles().Length(); @@ -630,51 +632,51 @@ source_drag_data_get (GtkWidget *WXUNUSED(widget), // char *name = gdk_atom_name( selection_data->target ); // if (name) printf( "Format requested: %s.\n", name ); - wxNode *node = drop_source->m_data->m_dataObjects.First(); - while (node) - { - wxDataObject *data_object = (wxDataObject*) node->Data(); - if (data_object->GetFormat().GetAtom() == selection_data->target) - { -// printf( "format found.\n" ); + drop_source->m_retValue = wxDragCancel; + + wxDataObject *data = drop_source->m_data; + + if (!data) + return; + + if (!data->IsSupportedFormat(selection_data->target)) + return; - size_t data_size = data_object->GetSize(); + if (data->GetDataSize(selection_data->target) == 0) + return; + + size_t size = data->GetDataSize(selection_data->target); - if (data_size > 0) - { -// printf( "data size: %d.\n", (int)data_size ); +// printf( "data size: %d.\n", (int)data_size ); - guchar *buffer = new guchar[data_size]; - data_object->WriteData( buffer ); + guchar *d = new guchar[size]; + + if (!data->GetDataHere( selection_data->target, (void*)d )) + { + free( d ); + return; + } #if wxUSE_THREADS - /* disable GUI threads */ - wxapp_uninstall_thread_wakeup(); + /* disable GUI threads */ + wxapp_uninstall_thread_wakeup(); #endif gtk_selection_data_set( selection_data, selection_data->target, 8, // 8-bit - buffer, - data_size ); + d, + size ); #if wxUSE_THREADS - /* enable GUI threads */ - wxapp_install_thread_wakeup(); + /* enable GUI threads */ + wxapp_install_thread_wakeup(); #endif - free( buffer ); - /* so far only copy, no moves. TODO. */ - drop_source->m_retValue = wxDragCopy; - - return; - } - } - - node = node->Next(); - } - - drop_source->m_retValue = wxDragCancel; + free( d ); + + /* so far only copy, no moves. TODO. */ + drop_source->m_retValue = wxDragCopy; } //---------------------------------------------------------------------------- @@ -733,7 +735,7 @@ wxDropSource::wxDropSource( wxWindow *win, const wxIcon &go, const wxIcon &stop m_widget = win->m_widget; if (win->m_wxwindow) m_widget = win->m_wxwindow; - m_data = (wxDataBroker*) NULL; + m_data = (wxDataObject*) NULL; m_retValue = wxDragCancel; m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); @@ -755,8 +757,7 @@ wxDropSource::wxDropSource( wxDataObject& data, wxWindow *win, if (win->m_wxwindow) m_widget = win->m_wxwindow; m_retValue = wxDragCancel; - m_data = new wxDataBroker; - m_data->Add(&data); + m_data = &data; m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); m_goaheadCursor = wxCursor( wxCURSOR_HAND ); @@ -767,53 +768,18 @@ wxDropSource::wxDropSource( wxDataObject& data, wxWindow *win, if (wxNullIcon == stop) m_stopIcon = wxIcon( gv_xpm ); } -wxDropSource::wxDropSource( wxDataBroker *data, wxWindow *win ) -{ - m_window = win; - m_widget = win->m_widget; - if (win->m_wxwindow) m_widget = win->m_wxwindow; - m_retValue = wxDragCancel; - - m_data = data; - - m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); - m_goaheadCursor = wxCursor( wxCURSOR_HAND ); -} - void wxDropSource::SetData( wxDataObject& data ) { - if ( m_data ) + if (m_data) delete m_data; - m_data = new wxDataBroker; - m_data->Add(&data); -} - -void wxDropSource::SetData( wxDataObject *data ) -{ - if (m_data) delete m_data; - - if (data) - { - m_data = new wxDataBroker(); - m_data->Add( data ); - } - else - { - m_data = (wxDataBroker*) NULL; - } -} - -void wxDropSource::SetData( wxDataBroker *data ) -{ - if (m_data) delete m_data; - - m_data = data; + m_data = &data; } wxDropSource::~wxDropSource() { - if (m_data) delete m_data; + if (m_data) +// delete m_data; g_blockEventsOnDrag = FALSE; } diff --git a/src/gtk1/clipbrd.cpp b/src/gtk1/clipbrd.cpp index d981a17e76..9a9e38ee22 100644 --- a/src/gtk1/clipbrd.cpp +++ b/src/gtk1/clipbrd.cpp @@ -100,7 +100,8 @@ targets_selection_received( GtkWidget *WXUNUSED(widget), for (unsigned int i=0; ilength/sizeof(GdkAtom); i++) { -/* char *name = gdk_atom_name (atoms[i]); +/* + char *name = gdk_atom_name (atoms[i]); if (name) printf( "Format available: %s.\n", name ); */ if (atoms[i] == clipboard->m_targetRequested) @@ -134,7 +135,7 @@ selection_received( GtkWidget *WXUNUSED(widget), } wxDataObject *data_object = clipboard->m_receivedData; - + if (!data_object) { clipboard->m_waiting = FALSE; @@ -148,7 +149,7 @@ selection_received( GtkWidget *WXUNUSED(widget), } /* make sure we got the data in the correct format */ - if (data_object->GetFormat().GetAtom() != selection_data->target) + if (data_object->GetFormat() != selection_data->target) { clipboard->m_waiting = FALSE; return; @@ -186,7 +187,7 @@ selection_received( GtkWidget *WXUNUSED(widget), wxBitmapDataObject *bitmap_object = (wxBitmapDataObject *) data_object; - bitmap_object->SetPngData( (const char*) selection_data->data, (size_t) selection_data->length ); + bitmap_object->SetPngData( (const void*) selection_data->data, (size_t) selection_data->length ); break; } @@ -245,10 +246,10 @@ selection_clear_clip( GtkWidget *WXUNUSED(widget), GdkEventSelection *event ) (!wxTheClipboard->m_ownsClipboard)) { /* the clipboard is no longer in our hands. we can the delete clipboard data. */ - if (wxTheClipboard->m_dataBroker) + if (wxTheClipboard->m_data) { - delete wxTheClipboard->m_dataBroker; - wxTheClipboard->m_dataBroker = (wxDataBroker*) NULL; + delete wxTheClipboard->m_data; + wxTheClipboard->m_data = (wxDataObject*) NULL; } } @@ -265,82 +266,64 @@ selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data { if (!wxTheClipboard) return; - if (!wxTheClipboard->m_dataBroker) return; + if (!wxTheClipboard->m_data) return; - wxNode *node = wxTheClipboard->m_dataBroker->m_dataObjects.First(); + wxDataObject *data = wxTheClipboard->m_data; - while (node) + if (!data->IsSupportedFormat( selection_data->target )) return; + + if (data->GetFormat().GetType() == wxDF_TEXT) { - wxDataObject *data_object = (wxDataObject *)node->Data(); - - if (data_object->GetFormat().GetAtom() != selection_data->target) - { - node = node->Next(); - continue; - } - - switch (data_object->GetFormat().GetType()) - { - case wxDF_TEXT: - { - wxTextDataObject *text_object = (wxTextDataObject*) data_object; - - wxString text = text_object->GetText(); - + wxTextDataObject *text_object = (wxTextDataObject*) data; + wxString text( text_object->GetText() ); + #if wxUSE_UNICODE - const wxWX2MBbuf s = text.mbc_str(); - int len = strlen(s); + const wxWX2MBbuf s = text.mbc_str(); + int len = strlen(s); #else // more efficient in non-Unicode - const char *s = text.c_str(); - int len = (int) text.Length(); + const char *s = text.c_str(); + int len = (int) text.Length(); #endif - - gtk_selection_data_set( - selection_data, - GDK_SELECTION_TYPE_STRING, - 8*sizeof(gchar), - (unsigned char*) (const char*) s, - len ); + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) (const char*) s, + len ); - break; - } - - case wxDF_BITMAP: - { - wxBitmapDataObject *bitmap_object = (wxBitmapDataObject*) data_object; - - if (bitmap_object->GetSize() == 0) return; - - gtk_selection_data_set( - selection_data, - GDK_SELECTION_TYPE_STRING, - 8*sizeof(gchar), - (unsigned char*) bitmap_object->GetData(), - (int) bitmap_object->GetSize() ); - - break; - } - - case wxDF_PRIVATE: - { - wxPrivateDataObject *private_object = (wxPrivateDataObject*) data_object; + return; + } + + if (data->GetFormat().GetType() == wxDF_BITMAP) + { + wxBitmapDataObject *bitmap_object = (wxBitmapDataObject*) data; - if (private_object->GetSize() == 0) return; + if (bitmap_object->GetDataSize(wxDF_BITMAP) == 0) return; - gtk_selection_data_set( - selection_data, - GDK_SELECTION_TYPE_STRING, - 8*sizeof(gchar), - (unsigned char*) private_object->GetData(), - (int) private_object->GetSize() ); - } + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) bitmap_object->GetData(), + (int) bitmap_object->GetDataSize(wxDF_BITMAP) ); - default: - break; - } - - node = node->Next(); + return; } + + int size = data->GetDataSize( selection_data->target ); + + if (size == 0) return; + + char *d = new char[size]; + + data->GetDataHere( selection_data->target, (void*) d ); + + gtk_selection_data_set( + selection_data, + GDK_SELECTION_TYPE_STRING, + 8*sizeof(gchar), + (unsigned char*) d, + size ); } //----------------------------------------------------------------------------- @@ -356,8 +339,7 @@ wxClipboard::wxClipboard() m_ownsClipboard = FALSE; m_ownsPrimarySelection = FALSE; - m_dataBroker = (wxDataBroker*) NULL; - + m_data = (wxDataObject*) NULL; m_receivedData = (wxDataObject*) NULL; /* we use m_targetsWidget to query what formats are available */ @@ -404,8 +386,8 @@ wxClipboard::~wxClipboard() void wxClipboard::Clear() { - if (m_dataBroker) - { + if (m_data) + { #if wxUSE_THREADS /* disable GUI threads */ wxapp_uninstall_thread_wakeup(); @@ -431,10 +413,10 @@ void wxClipboard::Clear() while (m_waiting) gtk_main_iteration(); } - if (m_dataBroker) - { - delete m_dataBroker; - m_dataBroker = (wxDataBroker*) NULL; + if (m_data) + { + delete m_data; + m_data = (wxDataObject*) NULL; } #if wxUSE_THREADS @@ -444,7 +426,6 @@ void wxClipboard::Clear() } m_targetRequested = 0; - m_formatSupported = FALSE; } @@ -474,14 +455,13 @@ bool wxClipboard::AddData( wxDataObject *data ) wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); - /* if clipboard has been cleared before, create new data broker */ - if (!m_dataBroker) m_dataBroker = new wxDataBroker(); + // we can only store one wxDataObject + Clear(); - /* add new data to list of offered data objects */ - m_dataBroker->Add( data ); - + m_data = data; + /* get native format id of new data object */ - GdkAtom format = data->GetFormat().GetAtom(); + GdkAtom format = data->GetFormat(); wxCHECK_MSG( format, FALSE, wxT("data has invalid format") ); @@ -570,11 +550,9 @@ void wxClipboard::Close() bool wxClipboard::IsSupported( wxDataFormat format ) { - wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); - /* store requested format to be asked for by callbacks */ - m_targetRequested = format.GetAtom(); + m_targetRequested = format; wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); @@ -591,7 +569,7 @@ bool wxClipboard::IsSupported( wxDataFormat format ) m_waiting = TRUE; gtk_selection_convert( m_targetsWidget, - m_usePrimary?GDK_SELECTION_PRIMARY:g_clipboardAtom, + m_usePrimary ? GDK_SELECTION_PRIMARY : g_clipboardAtom, g_targetsAtom, GDK_CURRENT_TIME ); @@ -616,7 +594,7 @@ bool wxClipboard::GetData( wxDataObject *data ) /* store requested format to be asked for by callbacks */ - m_targetRequested = data->GetFormat().GetAtom(); + m_targetRequested = data->GetFormat(); wxCHECK_MSG( m_targetRequested, FALSE, wxT("invalid clipboard format") ); @@ -636,7 +614,7 @@ bool wxClipboard::GetData( wxDataObject *data ) m_waiting = TRUE; gtk_selection_convert( m_clipboardWidget, - m_usePrimary?GDK_SELECTION_PRIMARY:g_clipboardAtom, + m_usePrimary ? GDK_SELECTION_PRIMARY : g_clipboardAtom, m_targetRequested, GDK_CURRENT_TIME ); diff --git a/src/gtk1/dataobj.cpp b/src/gtk1/dataobj.cpp index d1668eb212..401dca0db0 100644 --- a/src/gtk1/dataobj.cpp +++ b/src/gtk1/dataobj.cpp @@ -26,19 +26,17 @@ GdkAtom g_textAtom = 0; GdkAtom g_pngAtom = 0; +GdkAtom g_fileAtom = 0; //------------------------------------------------------------------------- // wxDataFormat //------------------------------------------------------------------------- -IMPLEMENT_CLASS(wxDataFormat, wxObject) - wxDataFormat::wxDataFormat() { PrepareFormats(); m_type = wxDF_INVALID; - m_hasAtom = FALSE; - m_atom = (GdkAtom) 0; + m_format = (GdkAtom) 0; } wxDataFormat::wxDataFormat( wxDataFormatId type ) @@ -59,39 +57,10 @@ wxDataFormat::wxDataFormat( const wxString &id ) SetId( id ); } -wxDataFormat::wxDataFormat( const wxDataFormat &format ) -{ - PrepareFormats(); - m_type = format.GetType(); - m_id = format.GetId(); - m_hasAtom = TRUE; - m_atom = ((wxDataFormat &)format).GetAtom(); // const_cast -} - -wxDataFormat::wxDataFormat( const GdkAtom atom ) +wxDataFormat::wxDataFormat( NativeFormat format ) { PrepareFormats(); - m_hasAtom = TRUE; - - m_atom = atom; - - if (m_atom == g_textAtom) - { - m_type = wxDF_TEXT; - } else - if (m_atom == GDK_TARGET_BITMAP) - { - m_type = wxDF_BITMAP; - } else - { - m_type = wxDF_PRIVATE; - m_id = gdk_atom_name( m_atom ); - - if (m_id == wxT("file:ALL")) - { - m_type = wxDF_FILENAME; - } - } + SetId( format ); } void wxDataFormat::SetType( wxDataFormatId type ) @@ -99,25 +68,17 @@ void wxDataFormat::SetType( wxDataFormatId type ) m_type = type; if (m_type == wxDF_TEXT) - { - m_id = wxT("STRING"); - } + m_format = g_textAtom; else if (m_type == wxDF_BITMAP) - { - m_id = wxT("image/png"); - } + m_format = g_pngAtom; else if (m_type == wxDF_FILENAME) - { - m_id = wxT("file:ALL"); - } + m_format = g_fileAtom; else { wxFAIL_MSG( wxT("invalid dataformat") ); } - - m_hasAtom = FALSE; } wxDataFormatId wxDataFormat::GetType() const @@ -127,152 +88,41 @@ wxDataFormatId wxDataFormat::GetType() const wxString wxDataFormat::GetId() const { - return m_id; -} - -void wxDataFormat::SetId( const wxChar *id ) -{ - m_type = wxDF_PRIVATE; - m_id = id; - m_hasAtom = FALSE; -} - -GdkAtom wxDataFormat::GetAtom() -{ - if (!m_hasAtom) - { - m_hasAtom = TRUE; - - if (m_type == wxDF_TEXT) - { - m_atom = g_textAtom; - } - else - if (m_type == wxDF_BITMAP) - { - m_atom = GDK_TARGET_BITMAP; - } - else - if (m_type == wxDF_PRIVATE) - { - m_atom = gdk_atom_intern( wxMBSTRINGCAST m_id.mbc_str(), FALSE ); - } - else - if (m_type == wxDF_FILENAME) - { - m_atom = gdk_atom_intern( "file:ALL", FALSE ); - } - else - { - m_hasAtom = FALSE; - m_atom = (GdkAtom) 0; - } - } - - return m_atom; -} - -void wxDataFormat::PrepareFormats() -{ - if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE ); - if (!g_pngAtom) g_pngAtom = gdk_atom_intern( "image/png", FALSE ); -} - -//------------------------------------------------------------------------- -// wxDataBroker -//------------------------------------------------------------------------- - -IMPLEMENT_CLASS(wxDataBroker,wxObject) - -wxDataBroker::wxDataBroker() -{ - m_dataObjects.DeleteContents(TRUE); - m_preferred = 0; -} - -void wxDataBroker::Add( wxDataObject *dataObject, bool preferred ) -{ - if (preferred) m_preferred = m_dataObjects.GetCount(); - m_dataObjects.Append( dataObject ); -} - -size_t wxDataBroker::GetFormatCount() const -{ - return m_dataObjects.GetCount(); -} - -wxDataFormatId wxDataBroker::GetPreferredFormat() const -{ - wxNode *node = m_dataObjects.Nth( m_preferred ); - - wxASSERT( node ); - - wxDataObject* data_obj = (wxDataObject*)node->Data(); - - return data_obj->GetFormat().GetType(); -} - -wxDataFormat &wxDataBroker::GetNthFormat( size_t nth ) const -{ - wxNode *node = m_dataObjects.Nth( nth ); - - wxASSERT( node ); - - wxDataObject* data_obj = (wxDataObject*)node->Data(); - - return data_obj->GetFormat(); + wxString ret( gdk_atom_name( m_format ) ); // this will convert from ascii to Unicode + return ret; } -bool wxDataBroker::IsSupportedFormat( wxDataFormat &format ) const +void wxDataFormat::SetId( NativeFormat format ) { - wxNode *node = m_dataObjects.First(); - while (node) - { - wxDataObject *dobj = (wxDataObject*)node->Data(); - - if (dobj->GetFormat().GetAtom() == format.GetAtom()) - { - return TRUE; - } - - node = node->Next(); - } + m_format = format; - return FALSE; + if (m_format == g_textAtom) + m_type = wxDF_TEXT; + else + if (m_format == g_pngAtom) + m_type = wxDF_BITMAP; + else + if (m_format == g_fileAtom) + m_type = wxDF_FILENAME; + else + m_type = wxDF_PRIVATE; } -size_t wxDataBroker::GetSize( wxDataFormat& format ) const +void wxDataFormat::SetId( const wxChar *id ) { - wxNode *node = m_dataObjects.First(); - while (node) - { - wxDataObject *dobj = (wxDataObject*)node->Data(); - - if (dobj->GetFormat().GetAtom() == format.GetAtom()) - { - return dobj->GetSize(); - } - - node = node->Next(); - } - - return 0; + m_type = wxDF_PRIVATE; + wxString tmp( id ); + m_format = gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE ); // what is the string cast for? } -void wxDataBroker::WriteData( wxDataFormat& format, void *dest ) const +void wxDataFormat::PrepareFormats() { - wxNode *node = m_dataObjects.First(); - while (node) - { - wxDataObject *dobj = (wxDataObject*)node->Data(); - - if (dobj->GetFormat().GetAtom() == format.GetAtom()) - { - dobj->WriteData( dest ); - } - - node = node->Next(); - } + if (!g_textAtom) + g_textAtom = gdk_atom_intern( "STRING", FALSE ); + if (!g_pngAtom) + g_pngAtom = gdk_atom_intern( "image/png", FALSE ); + if (!g_fileAtom) + g_fileAtom = gdk_atom_intern( "file:ALL", FALSE ); } //------------------------------------------------------------------------- @@ -289,26 +139,29 @@ wxDataObject::~wxDataObject() { } -wxDataFormat &wxDataObject::GetFormat() +bool wxDataObject::IsSupportedFormat(const wxDataFormat& format) const { - return m_format; -} + size_t nFormatCount = GetFormatCount(); + if ( nFormatCount == 1 ) { + return format == GetPreferredFormat(); + } + else { + wxDataFormat *formats = new wxDataFormat[nFormatCount]; + GetAllFormats(formats); + + size_t n; + for ( n = 0; n < nFormatCount; n++ ) { + if ( formats[n] == format ) + break; + } -wxDataFormatId wxDataObject::GetFormatType() const -{ - return m_format.GetType(); -} + delete [] formats; -wxString wxDataObject::GetFormatId() const -{ - return m_format.GetId(); + // found? + return n < nFormatCount; + } } -GdkAtom wxDataObject::GetFormatAtom() const -{ - GdkAtom ret = ((wxDataObject*) this)->m_format.GetAtom(); - return ret; -} // ---------------------------------------------------------------------------- // wxTextDataObject @@ -316,41 +169,30 @@ GdkAtom wxDataObject::GetFormatAtom() const IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject, wxDataObject ) -wxTextDataObject::wxTextDataObject() -{ - m_format.SetType( wxDF_TEXT ); -} - -wxTextDataObject::wxTextDataObject( const wxString& data ) -{ - m_format.SetType( wxDF_TEXT ); - - m_data = data; +wxTextDataObject::wxTextDataObject() +{ } -void wxTextDataObject::SetText( const wxString& data ) -{ - m_data = data; +wxTextDataObject::wxTextDataObject(const wxString& strText) + : m_strText(strText) +{ } -wxString wxTextDataObject::GetText() const -{ - return m_data; +size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const +{ + return m_strText.Len() + 1; // +1 for trailing '\0'of course } -void wxTextDataObject::WriteData( void *dest ) const -{ - WriteString( m_data, dest ); +bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const +{ + memcpy(buf, m_strText.c_str(), GetDataSize(format)); + return TRUE; } -size_t wxTextDataObject::GetSize() const -{ - return m_data.Len() + 1; -} - -void wxTextDataObject::WriteString( const wxString &str, void *dest ) const -{ - memcpy( dest, str.mb_str(), str.Len()+1 ); +bool wxTextDataObject::SetData(const wxDataFormat& format, const void *buf) +{ + m_strText = (const wxChar *)buf; + return TRUE; } // ---------------------------------------------------------------------------- @@ -361,7 +203,6 @@ IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject, wxDataObject ) wxFileDataObject::wxFileDataObject() { - m_format.SetType( wxDF_FILENAME ); } void wxFileDataObject::AddFile( const wxString &file ) @@ -375,16 +216,34 @@ wxString wxFileDataObject::GetFiles() const return m_files; } -void wxFileDataObject::WriteData( void *dest ) const +bool wxFileDataObject::GetDataHere(const wxDataFormat& format, void *buf) const { - memcpy( dest, m_files.mbc_str(), GetSize() ); + if (format == wxDF_FILENAME) + { + memcpy( buf, m_files.mbc_str(), m_files.Len() + 1 ); + return TRUE; + } + + return FALSE; } -size_t wxFileDataObject::GetSize() const +size_t wxFileDataObject::GetDataSize(const wxDataFormat& format) const { + if (format != wxDF_FILENAME) return 0; + return m_files.Len() + 1; } +bool wxFileDataObject::SetData(const wxDataFormat& format, const void *buf) +{ + if (format != wxDF_FILENAME) + return FALSE; + + m_files = (char*)(buf); // this is so ugly, I cannot look at it + + return TRUE; +} + // ---------------------------------------------------------------------------- // wxBitmapDataObject // ---------------------------------------------------------------------------- @@ -393,15 +252,13 @@ IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject, wxDataObject ) wxBitmapDataObject::wxBitmapDataObject() { - m_format.SetType( wxDF_BITMAP ); - m_pngData = (char*)NULL; + m_pngData = (void*)NULL; m_pngSize = 0; } wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) { - m_format.SetType( wxDF_BITMAP ); - m_pngData = (char*)NULL; + m_pngData = (void*)NULL; m_pngSize = 0; m_bitmap = bitmap; DoConvertToPng(); @@ -409,45 +266,60 @@ wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap ) wxBitmapDataObject::~wxBitmapDataObject() { - if (m_pngData) delete[] m_pngData; + if (m_pngData) + delete[] m_pngData; } void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap ) { + if (m_pngData) + delete[] m_pngData; + m_pngData = (void*)NULL; + m_pngSize = 0; + m_bitmap = bitmap; DoConvertToPng(); } -wxBitmap wxBitmapDataObject::GetBitmap() const +size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const { - return m_bitmap; -} - -void wxBitmapDataObject::WriteData( void *dest ) const -{ - WriteBitmap( m_bitmap, dest ); + if (format != wxDF_BITMAP) return 0; + + return m_pngSize; } -size_t wxBitmapDataObject::GetSize() const +bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, void *buf) const { - return m_pngSize; + if (format != wxDF_BITMAP) return FALSE; + + if (m_pngSize > 0) + { + memcpy(buf, m_pngData, m_pngSize); + return TRUE; + } + + return FALSE; } -void wxBitmapDataObject::WriteBitmap( const wxBitmap &WXUNUSED(bitmap), void *dest ) const +bool wxBitmapDataObject::SetData(const wxDataFormat& format, const void *buf) { -// if (m_bitmap == bitmap) - memcpy( dest, m_pngData, m_pngSize ); + if (m_pngData) delete[] m_pngData; + m_pngData = (void*) NULL; + m_pngSize = 0; + m_bitmap = wxNullBitmap; + + return FALSE; } -void wxBitmapDataObject::SetPngData( const char *pngData, size_t pngSize ) +void wxBitmapDataObject::SetPngData(const void *buf, size_t size) { if (m_pngData) delete[] m_pngData; - m_pngData = (char*) NULL; - m_pngSize = pngSize; - m_pngData = new char[m_pngSize]; - memcpy( m_pngData, pngData, m_pngSize ); + m_pngSize = size; + m_pngData = (void*) new char[m_pngSize]; - wxMemoryInputStream mstream( pngData, pngSize ); + memcpy( m_pngData, buf, m_pngSize ); + + wxMemoryInputStream mstream( (char*) m_pngData, m_pngSize ); wxImage image; wxPNGHandler handler; handler.LoadFile( &image, mstream ); @@ -456,7 +328,7 @@ void wxBitmapDataObject::SetPngData( const char *pngData, size_t pngSize ) void wxBitmapDataObject::DoConvertToPng() { - if (m_pngData) delete[] m_pngData; + if (!m_bitmap.Ok()) return; wxImage image( m_bitmap ); wxPNGHandler handler; @@ -464,9 +336,9 @@ void wxBitmapDataObject::DoConvertToPng() wxCountingOutputStream count; handler.SaveFile( &image, count ); m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? - m_pngData = new char[m_pngSize]; + m_pngData = (void*) new char[m_pngSize]; - wxMemoryOutputStream mstream( m_pngData, m_pngSize ); + wxMemoryOutputStream mstream( (char*) m_pngData, m_pngSize ); handler.SaveFile( &image, mstream ); } @@ -474,7 +346,7 @@ void wxBitmapDataObject::DoConvertToPng() // wxPrivateDataObject // ---------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject, wxDataObject ) +IMPLEMENT_CLASS( wxPrivateDataObject, wxDataObject ) void wxPrivateDataObject::Free() { diff --git a/src/gtk1/dnd.cpp b/src/gtk1/dnd.cpp index 11fcc9c080..dbf3456d02 100644 --- a/src/gtk1/dnd.cpp +++ b/src/gtk1/dnd.cpp @@ -380,7 +380,7 @@ bool wxDropTarget::RequestData( wxDataFormat format ) /* this should trigger an "drag_data_received" event */ gtk_drag_get_data( m_dragWidget, m_dragContext, - format.GetAtom(), + format, m_dragTime ); #if wxUSE_THREADS @@ -403,7 +403,7 @@ bool wxDropTarget::IsSupported( wxDataFormat format ) // char *name = gdk_atom_name( formatAtom ); // if (name) printf( "Format available: %s.\n", name ); - if (formatAtom == format.GetAtom()) return TRUE; + if (formatAtom == format) return TRUE; child = child->next; } @@ -414,7 +414,7 @@ bool wxDropTarget::GetData( wxDataObject *data_object ) { if (!m_dragData) return FALSE; - if (m_dragData->target != data_object->GetFormat().GetAtom()) return FALSE; + if (m_dragData->target != data_object->GetFormat()) return FALSE; if (data_object->GetFormat().GetType() == wxDF_TEXT) { @@ -521,6 +521,7 @@ bool wxTextDropTarget::OnData( long x, long y ) // wxPrivateDropTarget //------------------------------------------------------------------------- +/* wxPrivateDropTarget::wxPrivateDropTarget() { m_id = wxTheApp->GetAppName(); @@ -558,6 +559,7 @@ bool wxPrivateDropTarget::OnData( long x, long y ) return TRUE; } +*/ //---------------------------------------------------------------------------- // A drop target which accepts files (dragged from File Manager or Explorer) @@ -584,7 +586,7 @@ bool wxFileDropTarget::OnData( long x, long y ) wxFileDataObject data; if (!GetData( &data )) return FALSE; - /* get number of substrings /root/mytext.txt/0/root/myothertext.txt/0/0 */ + // get number of substrings /root/mytext.txt/0/root/myothertext.txt/0/0 size_t number = 0; size_t i; size_t size = data.GetFiles().Length(); @@ -630,51 +632,51 @@ source_drag_data_get (GtkWidget *WXUNUSED(widget), // char *name = gdk_atom_name( selection_data->target ); // if (name) printf( "Format requested: %s.\n", name ); - wxNode *node = drop_source->m_data->m_dataObjects.First(); - while (node) - { - wxDataObject *data_object = (wxDataObject*) node->Data(); - if (data_object->GetFormat().GetAtom() == selection_data->target) - { -// printf( "format found.\n" ); + drop_source->m_retValue = wxDragCancel; + + wxDataObject *data = drop_source->m_data; + + if (!data) + return; + + if (!data->IsSupportedFormat(selection_data->target)) + return; - size_t data_size = data_object->GetSize(); + if (data->GetDataSize(selection_data->target) == 0) + return; + + size_t size = data->GetDataSize(selection_data->target); - if (data_size > 0) - { -// printf( "data size: %d.\n", (int)data_size ); +// printf( "data size: %d.\n", (int)data_size ); - guchar *buffer = new guchar[data_size]; - data_object->WriteData( buffer ); + guchar *d = new guchar[size]; + + if (!data->GetDataHere( selection_data->target, (void*)d )) + { + free( d ); + return; + } #if wxUSE_THREADS - /* disable GUI threads */ - wxapp_uninstall_thread_wakeup(); + /* disable GUI threads */ + wxapp_uninstall_thread_wakeup(); #endif gtk_selection_data_set( selection_data, selection_data->target, 8, // 8-bit - buffer, - data_size ); + d, + size ); #if wxUSE_THREADS - /* enable GUI threads */ - wxapp_install_thread_wakeup(); + /* enable GUI threads */ + wxapp_install_thread_wakeup(); #endif - free( buffer ); - /* so far only copy, no moves. TODO. */ - drop_source->m_retValue = wxDragCopy; - - return; - } - } - - node = node->Next(); - } - - drop_source->m_retValue = wxDragCancel; + free( d ); + + /* so far only copy, no moves. TODO. */ + drop_source->m_retValue = wxDragCopy; } //---------------------------------------------------------------------------- @@ -733,7 +735,7 @@ wxDropSource::wxDropSource( wxWindow *win, const wxIcon &go, const wxIcon &stop m_widget = win->m_widget; if (win->m_wxwindow) m_widget = win->m_wxwindow; - m_data = (wxDataBroker*) NULL; + m_data = (wxDataObject*) NULL; m_retValue = wxDragCancel; m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); @@ -755,8 +757,7 @@ wxDropSource::wxDropSource( wxDataObject& data, wxWindow *win, if (win->m_wxwindow) m_widget = win->m_wxwindow; m_retValue = wxDragCancel; - m_data = new wxDataBroker; - m_data->Add(&data); + m_data = &data; m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); m_goaheadCursor = wxCursor( wxCURSOR_HAND ); @@ -767,53 +768,18 @@ wxDropSource::wxDropSource( wxDataObject& data, wxWindow *win, if (wxNullIcon == stop) m_stopIcon = wxIcon( gv_xpm ); } -wxDropSource::wxDropSource( wxDataBroker *data, wxWindow *win ) -{ - m_window = win; - m_widget = win->m_widget; - if (win->m_wxwindow) m_widget = win->m_wxwindow; - m_retValue = wxDragCancel; - - m_data = data; - - m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); - m_goaheadCursor = wxCursor( wxCURSOR_HAND ); -} - void wxDropSource::SetData( wxDataObject& data ) { - if ( m_data ) + if (m_data) delete m_data; - m_data = new wxDataBroker; - m_data->Add(&data); -} - -void wxDropSource::SetData( wxDataObject *data ) -{ - if (m_data) delete m_data; - - if (data) - { - m_data = new wxDataBroker(); - m_data->Add( data ); - } - else - { - m_data = (wxDataBroker*) NULL; - } -} - -void wxDropSource::SetData( wxDataBroker *data ) -{ - if (m_data) delete m_data; - - m_data = data; + m_data = &data; } wxDropSource::~wxDropSource() { - if (m_data) delete m_data; + if (m_data) +// delete m_data; g_blockEventsOnDrag = FALSE; } -- 2.45.2