From b8acf11e74d4ac25f9899d8d426ad04569c99e88 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Wed, 30 May 2012 19:21:42 +0000 Subject: [PATCH] A patch adding wxHTMLDataObject which can be used for handling the standard platform formats for transfering HTML formatted text. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71610 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/dataobj.h | 39 +++++++++++++ interface/wx/dataobj.h | 31 ++++++++++- src/common/dobjcmn.cpp | 111 +++++++++++++++++++++++++++++++++++++ src/gtk/dataobj.cpp | 9 +++ src/msw/clipbrd.cpp | 17 ++++-- src/msw/ole/dataobj.cpp | 26 ++++++++- src/osx/carbon/dataobj.cpp | 8 +++ 7 files changed, 231 insertions(+), 10 deletions(-) diff --git a/include/wx/dataobj.h b/include/wx/dataobj.h index d255a5064c..8832aad706 100644 --- a/include/wx/dataobj.h +++ b/include/wx/dataobj.h @@ -334,6 +334,45 @@ private: #endif #endif // wxUSE_UNICODE +class WXDLLIMPEXP_CORE wxHTMLDataObject : public wxDataObjectSimple +{ +public: + // ctor: you can specify the text here or in SetText(), or override + // GetText() + wxHTMLDataObject(const wxString& html = wxEmptyString) + : wxDataObjectSimple(wxDF_HTML), + m_html(html) + { + } + + // virtual functions which you may override if you want to provide text on + // demand only - otherwise, the trivial default versions will be used + virtual size_t GetLength() const { return m_html.Len() + 1; } + virtual wxString GetHTML() const { return m_html; } + virtual void SetHTML(const wxString& html) { m_html = html; } + + virtual size_t GetDataSize() const; + virtual bool GetDataHere(void *buf) const; + virtual bool SetData(size_t len, const void *buf); + + // Must provide overloads to avoid hiding them (and warnings about it) + virtual size_t GetDataSize(const wxDataFormat&) const + { + return GetDataSize(); + } + virtual bool GetDataHere(const wxDataFormat&, void *buf) const + { + return GetDataHere(buf); + } + virtual bool SetData(const wxDataFormat&, size_t len, const void *buf) + { + return SetData(len, buf); + } + +private: + wxString m_html; +}; + class WXDLLIMPEXP_CORE wxTextDataObject : public wxDataObjectSimple { public: diff --git a/interface/wx/dataobj.h b/interface/wx/dataobj.h index b5614303d0..4e2e9c9bd7 100644 --- a/interface/wx/dataobj.h +++ b/interface/wx/dataobj.h @@ -34,9 +34,7 @@ @itemdef{wxDF_FILENAME, A list of filenames.} @itemdef{wxDF_HTML, - An HTML string. This is only valid when passed to - wxSetClipboardData when compiled with Visual C++ in non-Unicode - mode.} + An HTML string. This is currently only valid on Mac and MSW.} @endDefList As mentioned above, these standard formats may be passed to any function @@ -789,4 +787,31 @@ public: const wxArrayString& GetFilenames() const; }; +/** + @class wxHTMLDataObject + + wxHTMLDataObject is used for working with HTML-formatted text. + + @library{wxcore} + @category{dnd} + + @see wxDataObject, wxDataObjectSimple +*/ +class wxHTMLDataObject : public wxDataObjectSimple +{ +public: + /** + Constructor. + */ + wxHTMLDataObject(const wxString& html = wxEmptyString); + /** + Returns the HTML string. + */ + virtual wxString GetHTML() const; + + /** + Sets the HTML string. + */ + virtual void SetHTML(const wxString& html); +}; diff --git a/src/common/dobjcmn.cpp b/src/common/dobjcmn.cpp index ebe87267b7..2fd5984afe 100644 --- a/src/common/dobjcmn.cpp +++ b/src/common/dobjcmn.cpp @@ -427,6 +427,117 @@ bool wxTextDataObject::SetData(size_t len, const void *buf) #endif // different wxTextDataObject implementations +size_t wxHTMLDataObject::GetDataSize() const +{ + size_t size = 0; + // Windows and Mac always use UTF-8, and docs suggest GTK does as well. + wxCharBuffer buffer = wxConvUTF8.cWX2MB( GetHTML().c_str() ); + + if (buffer) + { + size = strlen( buffer ); +#if __WXMSW__ + // On Windows we need to add some stuff to the string to satisfy + // its clipboard format requirements. + size += 400; +#endif + } + + return size; +} + +bool wxHTMLDataObject::GetDataHere(void *buf) const +{ + if ( !buf ) + return false; + + // Windows and Mac always use UTF-8, and docs suggest GTK does as well. + wxCharBuffer html = wxConvUTF8.cWX2MB( GetHTML().c_str() ); + if ( !html ) + return false; + + size_t bytes = GetDataSize(); +#if __WXMSW__ + // add the extra info that the MSW clipboard format requires. + char* buffer = new char[bytes]; + + // Create a template string for the HTML header... + strcpy(buffer, + "Version:0.9\r\n" + "StartHTML:00000000\r\n" + "EndHTML:00000000\r\n" + "StartFragment:00000000\r\n" + "EndFragment:00000000\r\n" + "\r\n" + "\r\n"); + + // Append the HTML... + strcat(buffer, html); + strcat(buffer, "\r\n"); + // Finish up the HTML format... + strcat(buffer, + "\r\n" + "\r\n" + ""); + + // Now go back, calculate all the lengths, and write out the + // necessary header information. Note, wsprintf() truncates the + // string when you overwrite it so you follow up with code to replace + // the 0 appended at the end with a '\r'... + char *ptr = strstr(buffer, "StartHTML"); + sprintf(ptr+10, "%08u", (unsigned)(strstr(buffer, "") - buffer)); + *(ptr+10+8) = '\r'; + + ptr = strstr(buffer, "EndHTML"); + sprintf(ptr+8, "%08u", (unsigned)strlen(buffer)); + *(ptr+8+8) = '\r'; + + ptr = strstr(buffer, "StartFragment"); + sprintf(ptr+14, "%08u", (unsigned)(strstr(buffer, "", fragmentStart) + 3; + int endCommentStart = html.rfind("