Use local static variable for ArtID mapping table

The instantiation of the global static mapping table may happen before
the instantiation of the wxArtIds, i.e. the copy-constructor would
access not yet valid wxArtId/wxString instances.

Delay the instantiation until the first call to wxArtIDToStock call.
This is still not 100% safe, as wxArtIDToStock may be called by a
constructor of a static instance, but hopefully nobody does that.
This commit is contained in:
Stefan Brüns
2020-08-06 03:06:31 +02:00
parent 128e3ff123
commit 3f9531fe31

View File

@@ -52,13 +52,22 @@ protected:
namespace namespace
{ {
wxString wxArtIDToStock(const wxArtID& id)
{
struct wxArtStockMapping
{
wxArtID artId;
wxString stockId;
};
#ifdef __WXGTK3__ #ifdef __WXGTK3__
#define ART(wxId, unused, themeId) wxId, themeId, #define ART(wxId, unused, themeId) wxArtStockMapping{wxId, themeId},
#else #else
#define ART(wxId, stockId, unused) wxId, stockId, #define ART(wxId, stockId, unused) wxArtStockMapping{wxId, stockId},
#endif #endif
const wxString wxId2Gtk[] = { static const wxArtStockMapping wxId2GtkMap[] =
{
ART(wxART_ERROR, GTK_STOCK_DIALOG_ERROR, "dialog-error") ART(wxART_ERROR, GTK_STOCK_DIALOG_ERROR, "dialog-error")
ART(wxART_INFORMATION, GTK_STOCK_DIALOG_INFO, "dialog-information") ART(wxART_INFORMATION, GTK_STOCK_DIALOG_INFO, "dialog-information")
ART(wxART_WARNING, GTK_STOCK_DIALOG_WARNING, "dialog-warning") ART(wxART_WARNING, GTK_STOCK_DIALOG_WARNING, "dialog-warning")
@@ -122,26 +131,21 @@ const wxString wxId2Gtk[] = {
ART(wxART_FIND_AND_REPLACE, GTK_STOCK_FIND_AND_REPLACE, "edit-find-replace") ART(wxART_FIND_AND_REPLACE, GTK_STOCK_FIND_AND_REPLACE, "edit-find-replace")
ART(wxART_FULL_SCREEN, GTK_STOCK_FULLSCREEN, "view-fullscreen") ART(wxART_FULL_SCREEN, GTK_STOCK_FULLSCREEN, "view-fullscreen")
ART(wxART_EDIT, "accessories-text-editor", "accessories-text-editor") ART(wxART_EDIT, "accessories-text-editor", "accessories-text-editor")
}; };
#undef ART #undef ART
wxString wxArtIDToStock(const wxArtID& id)
{
// allow passing GTK+ stock IDs to wxArtProvider -- if a recognized wx // allow passing GTK+ stock IDs to wxArtProvider -- if a recognized wx
// ID wasn't found, pass it to GTK+ in the hope it is a GTK+ or theme // ID wasn't found, pass it to GTK+ in the hope it is a GTK+ or theme
// icon name: // icon name:
wxString ret(id);
for (unsigned i = 0; i < WXSIZEOF(wxId2Gtk); i += 2) for (unsigned i = 0; i < WXSIZEOF(wxId2GtkMap); i++)
{ {
if (id == wxId2Gtk[i]) if (id == wxId2GtkMap[i].artId)
{ {
ret = wxId2Gtk[i + 1]; return wxId2GtkMap[i].stockId;
break;
} }
} }
return ret; return id;
} }
GtkIconSize ArtClientToIconSize(const wxArtClient& client) GtkIconSize ArtClientToIconSize(const wxArtClient& client)