From 8c180ee7b5d00e1e083cdc874ccf8c2437d561eb Mon Sep 17 00:00:00 2001 From: New Pagodi Date: Sun, 17 Mar 2019 01:47:36 -0500 Subject: [PATCH] Change the implementation of ListBoxImpl::RegisterImage The ListBoxImpl::RegisterImage in PlatWX.cpp is supposed to accept an XPM and convert it into a useable form. For wxWidgets, the useable form is obviously a wxBitmap. According to the Scintilla specification, the function should accept both a copy of an XPM file and a set of XPM data. Currently RegisterImage uses the the wxWidgets XPM image handler. This has 2 drawbacks. First it requires that the XPM handler is loaded before the function can be called. Second, the function only accepts a copy of an XPM file and does not work with XPM data. Instead use wxXPMDecoder. This class can be decode both types of input and can be used to build a wxBitmap. --- src/stc/PlatWX.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/stc/PlatWX.cpp b/src/stc/PlatWX.cpp index 44dfca3664..58a448deec 100644 --- a/src/stc/PlatWX.cpp +++ b/src/stc/PlatWX.cpp @@ -27,6 +27,7 @@ #include "wx/encconv.h" #include "wx/listctrl.h" #include "wx/mstream.h" +#include "wx/xpmdecod.h" #include "wx/image.h" #include "wx/imaglist.h" #include "wx/tokenzr.h" @@ -2561,10 +2562,23 @@ void ListBoxImpl::RegisterImageHelper(int type, wxBitmap& bmp) } void ListBoxImpl::RegisterImage(int type, const char *xpm_data) { - wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1); - wxImage img(stream, wxBITMAP_TYPE_XPM); + wxXPMDecoder dec; + wxImage img; + + // This check is borrowed from src/stc/scintilla/src/XPM.cpp. + // Test done is two parts to avoid possibility of overstepping the memory + // if memcmp implemented strangely. Must be 4 bytes at least at destination. + if ( (0 == memcmp(xpm_data, "/* X", 4)) && + (0 == memcmp(xpm_data, "/* XPM */", 9)) ) + { + wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1); + img = dec.ReadFile(stream); + } + else + img = dec.ReadData(reinterpret_cast(xpm_data)); + wxBitmap bmp(img); - RegisterImageHelper(type, bmp); + RegisterImageHelper(type,bmp); }