From 1cf83980a2295b2c35bdd455dab54f57fe5597f9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 21 Jun 2023 18:38:27 +0100 Subject: [PATCH] Refactor wxHTMLDataObject to keep wxMSW-specific parts together Also add a link to the official CF_HTML format documentation. No real changes, just prepare for further ones. --- src/common/dobjcmn.cpp | 116 ++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 42 deletions(-) diff --git a/src/common/dobjcmn.cpp b/src/common/dobjcmn.cpp index df53042af8..22cb01ddf5 100644 --- a/src/common/dobjcmn.cpp +++ b/src/common/dobjcmn.cpp @@ -429,38 +429,26 @@ bool wxTextDataObject::SetData(size_t len, const void *buf) // wxHTMLDataObject // ---------------------------------------------------------------------------- -size_t wxHTMLDataObject::GetDataSize() const -{ - // Ensure that the temporary string returned by GetHTML() is kept alive for - // as long as we need it here. - const wxString& htmlStr = GetHTML(); - const wxScopedCharBuffer buffer(htmlStr.utf8_str()); - - size_t size = buffer.length(); - #ifdef __WXMSW__ - // On Windows we need to add some stuff to the string to satisfy - // its clipboard format requirements. - size += 400; -#endif - return size; +// Helper functions for MSW CF_HTML format, see MSDN for more information: +// +// https://learn.microsoft.com/en-us/windows/win32/dataxchg/html-clipboard-format +namespace wxMSWClip +{ + +// Return the extra size needed by HTML data in addition to the length of the +// HTML fragment itself. +int GetExtraDataSize() +{ + // This more than covers the extra contents added by FillFromHTML() below. + return 400; } -bool wxHTMLDataObject::GetDataHere(void *buf) const +// Wrap HTML data with the extra information needed by CF_HTML and copy +// everything into the provided buffer assumed to be of sufficient size. +void FillFromHTML(char* buffer, const char* html) { - if ( !buf ) - return false; - - // Windows and Mac always use UTF-8, and docs suggest GTK does as well. - const wxString& htmlStr = GetHTML(); - const wxScopedCharBuffer html(htmlStr.utf8_str()); - if ( !html ) - return false; - - char* const buffer = static_cast(buf); - -#ifdef __WXMSW__ // add the extra info that the MSW clipboard format requires. // Create a template string for the HTML header... @@ -501,24 +489,12 @@ bool wxHTMLDataObject::GetDataHere(void *buf) const ptr = strstr(buffer, "EndFragment"); sprintf(ptr+12, "%08u", (unsigned)(strstr(buffer, "