Improve stc sample appearance in dark mode

This shows one possible way of adapting the application to support dark
mode, using the default for/background colours and SelectLightDark() for
the custom ones.

Note that this replaces "background" field of the styles in this sample
as it was never used (more precisely, was always specified as "WHITE")
with the new "foregroundDark" field which is set pretty arbitrarily but
differently enough from the foreground used in light mode for the sample
to look better in dark mode.

Also remove some duplicated code for setting styles and remove the code
for setting the defaults completely, as this is unnecessary, and even
harmful, after the changes of the previous commit.
This commit is contained in:
Vadim Zeitlin 2022-12-28 23:53:06 +00:00
parent 7eed1d6d14
commit 53935f6607
4 changed files with 92 additions and 86 deletions

View file

@ -30,6 +30,7 @@
//! wxWidgets headers
#include "wx/file.h" // raw file io support
#include "wx/filename.h" // filename support
#include "wx/settings.h" // system colours
//! application headers
#include "defsext.h" // additional definitions
@ -167,11 +168,6 @@ Edit::Edit (wxWindow *parent, wxWindowID id,
wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
wxFont font(wxFontInfo(10).Family(wxFONTFAMILY_MODERN));
StyleSetFont (wxSTC_STYLE_DEFAULT, font);
StyleSetForeground (wxSTC_STYLE_DEFAULT, *wxBLACK);
StyleSetBackground (wxSTC_STYLE_DEFAULT, *wxWHITE);
StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour ("DARK GREY"));
StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE);
StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColour ("DARK GREY"));
InitializePrefs (DEFAULT_LANGUAGE);
// set visibility
@ -179,14 +175,17 @@ Edit::Edit (wxWindow *parent, wxWindowID id,
SetXCaretPolicy (wxSTC_CARET_EVEN|wxSTC_VISIBLE_STRICT|wxSTC_CARET_SLOP, 1);
SetYCaretPolicy (wxSTC_CARET_EVEN|wxSTC_VISIBLE_STRICT|wxSTC_CARET_SLOP, 1);
// markers
MarkerDefine (wxSTC_MARKNUM_FOLDER, wxSTC_MARK_DOTDOTDOT, "BLACK", "BLACK");
MarkerDefine (wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_ARROWDOWN, "BLACK", "BLACK");
MarkerDefine (wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_EMPTY, "BLACK", "BLACK");
MarkerDefine (wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_DOTDOTDOT, "BLACK", "WHITE");
MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_ARROWDOWN, "BLACK", "WHITE");
MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, "BLACK", "BLACK");
MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, "BLACK", "BLACK");
// markers: note that some of them are made invisible by using the same
// colour for their foreground and background
const wxColour colFg = StyleGetForeground(wxSTC_STYLE_DEFAULT);
const wxColour colBg = StyleGetBackground(wxSTC_STYLE_DEFAULT);
MarkerDefine (wxSTC_MARKNUM_FOLDER, wxSTC_MARK_DOTDOTDOT, colFg, colFg);
MarkerDefine (wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_ARROWDOWN, colFg, colFg);
MarkerDefine (wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_EMPTY, colFg, colFg);
MarkerDefine (wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_DOTDOTDOT, colFg, colBg);
MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_ARROWDOWN, colFg, colBg);
MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, colFg, colFg);
MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, colFg, colFg);
// annotations
AnnotationSetVisible(wxSTC_ANNOTATION_BOXED);
@ -606,6 +605,25 @@ bool Edit::InitializePrefs (const wxString &name) {
// initialize styles
StyleClearAll();
const wxColour
colAux = wxSystemSettings::SelectLightDark("DARK GREY", "LIGHT GREY");
// set common styles
StyleSetForeground (wxSTC_STYLE_INDENTGUIDE, colAux);
// set margin for line numbers
SetMarginType (m_LineNrID, wxSTC_MARGIN_NUMBER);
StyleSetForeground (wxSTC_STYLE_LINENUMBER, colAux);
SetMarginWidth (m_LineNrID, 0); // start out not visible
// annotations style
StyleSetBackground(ANNOTATION_STYLE,
wxSystemSettings::SelectLightDark(wxColour(244, 220, 220),
wxColour(100, 100, 100)));
StyleSetSizeFractional(ANNOTATION_STYLE,
(StyleGetSizeFractional(wxSTC_STYLE_DEFAULT)*4)/5);
LanguageInfo const* curInfo = nullptr;
// determine language
@ -624,18 +642,6 @@ bool Edit::InitializePrefs (const wxString &name) {
SetLexer (curInfo->lexer);
m_language = curInfo;
// set margin for line numbers
SetMarginType (m_LineNrID, wxSTC_MARGIN_NUMBER);
StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour ("DARK GREY"));
StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE);
SetMarginWidth (m_LineNrID, 0); // start out not visible
// annotations style
StyleSetBackground(ANNOTATION_STYLE, wxColour(244, 220, 220));
StyleSetForeground(ANNOTATION_STYLE, *wxBLACK);
StyleSetSizeFractional(ANNOTATION_STYLE,
(StyleGetSizeFractional(wxSTC_STYLE_DEFAULT)*4)/5);
// default fonts for all styles!
int Nr;
for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) {
@ -643,10 +649,6 @@ bool Edit::InitializePrefs (const wxString &name) {
StyleSetFont (Nr, font);
}
// set common styles
StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColour ("DARK GREY"));
StyleSetForeground (wxSTC_STYLE_INDENTGUIDE, wxColour ("DARK GREY"));
// initialize settings
if (g_CommonPrefs.syntaxEnable) {
int keywordnr = 0;
@ -657,12 +659,10 @@ bool Edit::InitializePrefs (const wxString &name) {
.Family(wxFONTFAMILY_MODERN)
.FaceName(curType.fontname));
StyleSetFont (Nr, font);
if (curType.foreground.length()) {
StyleSetForeground (Nr, wxColour (curType.foreground));
}
if (curType.background.length()) {
StyleSetBackground (Nr, wxColour (curType.background));
}
StyleSetForeground (Nr, wxSystemSettings::SelectLightDark(
curType.foreground,
curType.foregroundDark
));
StyleSetBold (Nr, (curType.fontstyle & mySTC_STYLE_BOLD) > 0);
StyleSetItalic (Nr, (curType.fontstyle & mySTC_STYLE_ITALIC) > 0);
StyleSetUnderline (Nr, (curType.fontstyle & mySTC_STYLE_UNDERL) > 0);
@ -684,7 +684,7 @@ bool Edit::InitializePrefs (const wxString &name) {
// folding
SetMarginType (m_FoldingID, wxSTC_MARGIN_SYMBOL);
SetMarginMask (m_FoldingID, wxSTC_MASK_FOLDERS);
StyleSetBackground (m_FoldingID, *wxWHITE);
StyleSetBackground (m_FoldingID, StyleGetBackground(wxSTC_STYLE_DEFAULT));
SetMarginWidth (m_FoldingID, 0);
SetMarginSensitive (m_FoldingID, false);
if (g_CommonPrefs.foldEnable) {

View file

@ -221,82 +221,82 @@ const StyleInfo g_StylePrefs [] = {
// mySTC_TYPE_WORD1
{"Keyword1",
"BLUE", "WHITE",
"BLUE", "CYAN",
"", 10, mySTC_STYLE_BOLD, 0},
// mySTC_TYPE_WORD2
{"Keyword2",
"MIDNIGHT BLUE", "WHITE",
"MIDNIGHT BLUE", "LIGHT BLUE",
"", 10, 0, 0},
// mySTC_TYPE_WORD3
{"Keyword3",
"CORNFLOWER BLUE", "WHITE",
"CORNFLOWER BLUE", "LIGHT STEEL BLUE",
"", 10, 0, 0},
// mySTC_TYPE_WORD4
{"Keyword4",
"CYAN", "WHITE",
"CYAN", "MAGENTA",
"", 10, 0, 0},
// mySTC_TYPE_WORD5
{"Keyword5",
"DARK GREY", "WHITE",
"DARK GREY", "LIGHT GREY",
"", 10, 0, 0},
// mySTC_TYPE_WORD6
{"Keyword6",
"GREY", "WHITE",
"GREY", "KHAKI",
"", 10, 0, 0},
// mySTC_TYPE_COMMENT
{"Comment",
"FOREST GREEN", "WHITE",
"FOREST GREEN", "LIME GREEN",
"", 10, 0, 0},
// mySTC_TYPE_COMMENT_DOC
{"Comment (Doc)",
"FOREST GREEN", "WHITE",
"FOREST GREEN", "LIME GREEN",
"", 10, 0, 0},
// mySTC_TYPE_COMMENT_LINE
{"Comment line",
"FOREST GREEN", "WHITE",
"FOREST GREEN", "LIME GREEN",
"", 10, 0, 0},
// mySTC_TYPE_COMMENT_SPECIAL
{"Special comment",
"FOREST GREEN", "WHITE",
"FOREST GREEN", "LIME GREEN",
"", 10, mySTC_STYLE_ITALIC, 0},
// mySTC_TYPE_CHARACTER
{"Character",
"KHAKI", "WHITE",
"KHAKI", "LIGHT MAGENTA",
"", 10, 0, 0},
// mySTC_TYPE_CHARACTER_EOL
{"Character (EOL)",
"KHAKI", "WHITE",
"KHAKI", "LIGHT MAGENTA",
"", 10, 0, 0},
// mySTC_TYPE_STRING
{"String",
"BROWN", "WHITE",
"BROWN", "YELLOW",
"", 10, 0, 0},
// mySTC_TYPE_STRING_EOL
{"String (EOL)",
"BROWN", "WHITE",
"BROWN", "YELLOW",
"", 10, 0, 0},
// mySTC_TYPE_DELIMITER
{"Delimiter",
"ORANGE", "WHITE",
"ORANGE", "YELLOW GREEN",
"", 10, 0, 0},
// mySTC_TYPE_PUNCTUATION
{"Punctuation",
"ORANGE", "WHITE",
"ORANGE", "YELLOW GREEN",
"", 10, 0, 0},
// mySTC_TYPE_OPERATOR
@ -306,12 +306,12 @@ const StyleInfo g_StylePrefs [] = {
// mySTC_TYPE_BRACE
{"Label",
"VIOLET", "WHITE",
"VIOLET", "PINK",
"", 10, 0, 0},
// mySTC_TYPE_COMMAND
{"Command",
"BLUE", "WHITE",
"BLUE", "CYAN",
"", 10, 0, 0},
// mySTC_TYPE_IDENTIFIER
@ -321,52 +321,52 @@ const StyleInfo g_StylePrefs [] = {
// mySTC_TYPE_LABEL
{"Label",
"VIOLET", "WHITE",
"VIOLET", "PINK",
"", 10, 0, 0},
// mySTC_TYPE_NUMBER
{"Number",
"SIENNA", "WHITE",
"SIENNA", "SALMON",
"", 10, 0, 0},
// mySTC_TYPE_PARAMETER
{"Parameter",
"VIOLET", "WHITE",
"VIOLET", "THISTLE",
"", 10, mySTC_STYLE_ITALIC, 0},
// mySTC_TYPE_REGEX
{"Regular expression",
"ORCHID", "WHITE",
"ORCHID", "ORANGE",
"", 10, 0, 0},
// mySTC_TYPE_UUID
{"UUID",
"ORCHID", "WHITE",
"ORCHID", "ORANGE",
"", 10, 0, 0},
// mySTC_TYPE_VALUE
{"Value",
"ORCHID", "WHITE",
"ORCHID", "ORANGE",
"", 10, mySTC_STYLE_ITALIC, 0},
// mySTC_TYPE_PREPROCESSOR
{"Preprocessor",
"GREY", "WHITE",
"GREY", "LIGHT GREY",
"", 10, 0, 0},
// mySTC_TYPE_SCRIPT
{"Script",
"DARK GREY", "WHITE",
"DARK GREY", "LIGHT GREY",
"", 10, 0, 0},
// mySTC_TYPE_ERROR
{"Error",
"RED", "WHITE",
"RED", "PURPLE",
"", 10, 0, 0},
// mySTC_TYPE_UNDEFINED
{"Undefined",
"ORANGE", "WHITE",
"ORANGE", "ORCHID",
"", 10, 0, 0}
};

View file

@ -138,7 +138,7 @@ extern const int g_LanguagePrefsSize;
struct StyleInfo {
const wxString name;
const wxString foreground;
const wxString background;
const wxString foregroundDark;
const wxString fontname;
int fontsize;
int fontstyle;

View file

@ -762,13 +762,16 @@ public:
SetMarginType(margin_id_lineno, wxSTC_MARGIN_NUMBER);
SetMarginWidth(margin_id_lineno, 32);
MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_BOXPLUS, "WHITE", "BLACK");
MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_BOXMINUS, "WHITE", "BLACK");
MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_VLINE, "WHITE", "BLACK");
MarkerDefine(wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_BOXPLUSCONNECTED, "WHITE", "BLACK");
MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_BOXMINUSCONNECTED, "WHITE", "BLACK");
MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNER, "WHITE", "BLACK");
MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNER, "WHITE", "BLACK");
// We intentionally invert foreground and background colours here.
const wxColour colFg = StyleGetForeground(wxSTC_STYLE_DEFAULT);
const wxColour colBg = StyleGetBackground(wxSTC_STYLE_DEFAULT);
MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_BOXPLUS, colBg, colFg);
MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_BOXMINUS, colBg, colFg);
MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_VLINE, colBg, colFg);
MarkerDefine(wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_BOXPLUSCONNECTED, colBg, colFg);
MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_BOXMINUSCONNECTED, colBg, colFg);
MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNER, colBg, colFg);
MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNER, colBg, colFg);
SetMarginMask(margin_id_fold, wxSTC_MASK_FOLDERS);
SetMarginWidth(margin_id_fold, 32);
@ -788,24 +791,27 @@ public:
}
void SetLexerXml()
{
const wxColour colTag = wxSystemSettings::SelectLightDark(*wxBLUE, *wxCYAN);
const wxColour colAttr = wxSystemSettings::SelectLightDark(*wxRED, "PINK");
SetLexer(wxSTC_LEX_XML);
StyleSetForeground(wxSTC_H_DEFAULT, *wxBLACK);
StyleSetForeground(wxSTC_H_TAG, *wxBLUE);
StyleSetForeground(wxSTC_H_TAGUNKNOWN, *wxBLUE);
StyleSetForeground(wxSTC_H_ATTRIBUTE, *wxRED);
StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, *wxRED);
// Ensure the correct default background is used for all styles.
StyleClearAll();
StyleSetForeground(wxSTC_H_TAG, colTag);
StyleSetForeground(wxSTC_H_TAGUNKNOWN, colTag);
StyleSetForeground(wxSTC_H_ATTRIBUTE, colAttr);
StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, colAttr);
StyleSetBold(wxSTC_H_ATTRIBUTEUNKNOWN, true);
StyleSetForeground(wxSTC_H_NUMBER, *wxBLACK);
StyleSetForeground(wxSTC_H_DOUBLESTRING, *wxBLACK);
StyleSetForeground(wxSTC_H_SINGLESTRING, *wxBLACK);
StyleSetForeground(wxSTC_H_OTHER, *wxBLUE);
StyleSetForeground(wxSTC_H_OTHER, colTag);
StyleSetForeground(wxSTC_H_COMMENT, wxColour("GREY"));
StyleSetForeground(wxSTC_H_ENTITY, *wxRED);
StyleSetForeground(wxSTC_H_ENTITY, colAttr);
StyleSetBold(wxSTC_H_ENTITY, true);
StyleSetForeground(wxSTC_H_TAGEND, *wxBLUE);
StyleSetForeground(wxSTC_H_XMLSTART, *wxBLUE);
StyleSetForeground(wxSTC_H_XMLEND, *wxBLUE);
StyleSetForeground(wxSTC_H_CDATA, *wxRED);
StyleSetForeground(wxSTC_H_TAGEND, colTag);
StyleSetForeground(wxSTC_H_XMLSTART, colTag);
StyleSetForeground(wxSTC_H_XMLEND, colTag);
StyleSetForeground(wxSTC_H_CDATA, colAttr);
}
protected:
void OnMarginClick(wxStyledTextEvent&);