Merge branch 'stc-dark-mode'

Improve support for dark mode in wxSTC.

See #23097.

Closes #19107.
This commit is contained in:
Vadim Zeitlin 2023-01-13 16:00:16 +00:00
commit 4e116ced45
7 changed files with 125 additions and 87 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&);

View file

@ -319,6 +319,7 @@ wxColour GetColour(wxSystemColour index)
case wxSYS_COLOUR_MENUHILIGHT:
return wxColour(0x353535);
case wxSYS_COLOUR_BTNHIGHLIGHT:
case wxSYS_COLOUR_HIGHLIGHT:
return wxColour(0x777777);
@ -328,7 +329,6 @@ wxColour GetColour(wxSystemColour index)
case wxSYS_COLOUR_3DDKSHADOW:
case wxSYS_COLOUR_3DLIGHT:
case wxSYS_COLOUR_ACTIVEBORDER:
case wxSYS_COLOUR_BTNHIGHLIGHT:
case wxSYS_COLOUR_DESKTOP:
case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:

View file

@ -232,6 +232,22 @@ bool wxStyledTextCtrl::Create(wxWindow *parent,
SetFontQuality(wxSTC_EFF_QUALITY_DEFAULT);
#endif
// Use colours appropriate for the current system colour theme.
auto attr = wxTextCtrl::GetClassDefaultAttributes();
StyleSetForeground(wxSTC_STYLE_DEFAULT, attr.colFg);
StyleSetBackground(wxSTC_STYLE_DEFAULT, attr.colBg);
SetCaretForeground(attr.colFg);
// We also need to set this one because its foreground is hardcoded as
// black in Scintilla sources.
StyleSetForeground(wxSTC_STYLE_LINENUMBER, attr.colFg);
// And foreground for this one is hardcoded as white.
StyleSetForeground(wxSTC_STYLE_CALLTIP, attr.colFg);
SetSelForeground(true, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
SetSelBackground(true, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
return true;
}

View file

@ -232,6 +232,22 @@ bool wxStyledTextCtrl::Create(wxWindow *parent,
SetFontQuality(wxSTC_EFF_QUALITY_DEFAULT);
#endif
// Use colours appropriate for the current system colour theme.
auto attr = wxTextCtrl::GetClassDefaultAttributes();
StyleSetForeground(wxSTC_STYLE_DEFAULT, attr.colFg);
StyleSetBackground(wxSTC_STYLE_DEFAULT, attr.colBg);
SetCaretForeground(attr.colFg);
// We also need to set this one because its foreground is hardcoded as
// black in Scintilla sources.
StyleSetForeground(wxSTC_STYLE_LINENUMBER, attr.colFg);
// And foreground for this one is hardcoded as white.
StyleSetForeground(wxSTC_STYLE_CALLTIP, attr.colFg);
SetSelForeground(true, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
SetSelBackground(true, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
return true;
}