Config-file based theming

This commit is contained in:
Tim Warren 2015-04-21 14:07:04 -04:00
parent 586824e111
commit e42e31e4b0
4 changed files with 374 additions and 125 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,90 @@
{
"solarized" : [
"Solarized" : {
"background": {
"default": [253, 246, 227]
},
"foreground": {
"default": [101, 123, 131],
"keyword1": [220, 50, 47],
"keyword2": [203, 76, 22],
"keyword3": [181, 137, 0],
"keyword4": [181, 137, 0],
"keyword5": [181, 137, 0],
"keyword6": [181, 137, 0],
"comment": [147, 161, 161],
"comment_line": [147, 161, 161],
"comment_doc": [131, 148, 150],
"comment_special": [101, 123, 131],
"character": [108, 113, 196],
"character_eol": [108, 113, 196],
"string": [108, 113, 196],
"string_eol": [108, 113, 196],
"delimeter": [38, 139, 210],
"punctuation": [101, 123, 131],
"operator": [101, 123, 131],
"brace": [101, 123, 131],
"command": [],
"identifier": [38, 139, 210],
"label": [38, 139, 210],
"number": [211, 54, 130],
"parameter": [0, 43, 54],
"regex": [7, 54, 66],
"uuid": [0, 43, 54],
"value": [7, 54, 66],
"preprocessor": [181, 137, 0],
"script": [88, 110, 117],
"error": []
},
"bold": {
"keyword2": true,
"operator": true
},
"italic": {
]
},
"underline": {
},
"line_numbers": {
"foreground": [147, 161, 161],
"background": [238, 232, 213]
}
},
"Solarized Dark" : {
"background": {
"default": []
},
"foreground": {
"default": [],
"keyword1": [],
"keyword2": [],
"keyword3": [],
"keyword4": [],
"keyword5": [],
"keyword6": [],
"comment": [],
"line_comment": [],
"doc_comment": [],
"special_comment": [],
"character": [],
"character_eol": [],
"string": [],
"string_eol": [],
"delimeter": [],
"punctuation": [],
"operator": [],
"brace": [],
"command": [],
"identifier": [],
"label": [],
"number": [],
"parameter": [],
"regex": [],
"uuid": [],
"value": [],
"preprocessor": [],
"script": [],
"error": []
}
}
}

View File

@ -16,17 +16,26 @@ EditPane::EditPane(
lexerMap["batch"] = wxSTC_LEX_BATCH;
lexerMap["caml"] = wxSTC_LEX_CAML;
lexerMap["cmake"] = wxSTC_LEX_CMAKE;
lexerMap["cobol"] = wxSTC_LEX_COBOL;
lexerMap["coffeescript"] = wxSTC_LEX_CPP;
lexerMap["coffeescript"] = wxSTC_LEX_COFFEESCRIPT;
lexerMap["cpp"] = wxSTC_LEX_CPP;
lexerMap["css"] = wxSTC_LEX_CSS;
lexerMap["js"] = wxSTC_LEX_CPP;
lexerMap["fortran"] = wxSTC_LEX_FORTRAN;
lexerMap["haskell"] = wxSTC_LEX_HASKELL;
lexerMap["html"] = wxSTC_LEX_HTML;
lexerMap["java"] = wxSTC_LEX_CPP;
lexerMap["js"] = wxSTC_LEX_CPP;
lexerMap["lisp"] = wxSTC_LEX_LISP;
lexerMap["lua"] = wxSTC_LEX_LUA;
lexerMap["makefile"] = wxSTC_LEX_MAKEFILE;
lexerMap["markdown"] = wxSTC_LEX_MARKDOWN;
lexerMap["php"] = wxSTC_LEX_PHPSCRIPT;
lexerMap["perl"] = wxSTC_LEX_PERL;
lexerMap["python"] = wxSTC_LEX_PYTHON;
lexerMap["ruby"] = wxSTC_LEX_RUBY;
lexerMap["shell"] = wxSTC_LEX_BASH;
lexerMap["sql"] = wxSTC_LEX_SQL;
lexerMap["xml"] = wxSTC_LEX_XML;
lexerMap["yaml"] = wxSTC_LEX_YAML;
this->BindEvents();
}
@ -55,21 +64,6 @@ void EditPane::Highlight(wxString filePath)
this->StyleClearAll();
// Font setup
#ifdef __WXMAC__
wxFont *defaultFont = wxFont::New(
14,
wxFONTFAMILY_MODERN,
wxFONTFLAG_ANTIALIASED
);
#else
wxFont *defaultFont = wxFont::New(
12,
wxFONTFAMILY_MODERN,
wxFONTFLAG_ANTIALIASED
);
#endif
if (lexerMap.count(lang) > 0)
{
this->SetLexer(lexerMap[lang]);
@ -84,45 +78,8 @@ void EditPane::Highlight(wxString filePath)
this->SetProperty("error.inline", "0");
this->SetProperty("font.quality", "3"); // LCD Optimized
// Get the list of keywords for the current language
JsonValue keywords_array = this->GetKeywordList(lang);
// Make sure every background is the same color!
for(int i = 0; i <= wxSTC_STYLE_MAX; i++)
{
this->StyleSetBackground(i, wxColor(253, 246, 227));
this->StyleSetFont(i, *defaultFont);
}
this->StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColor(101, 123, 131));
this->StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColor(147, 161, 161));
this->SetMarginWidth (MARGIN_LINE_NUMBERS, TextWidth(wxSTC_STYLE_LINENUMBER, _T("_9999")));
this->StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColor(147, 161, 161));
this->StyleSetBackground (wxSTC_STYLE_LINENUMBER, wxColor(238, 232, 213));
this->SetMarginType (MARGIN_LINE_NUMBERS, wxSTC_MARGIN_NUMBER);
this->StyleSetForeground(wxSTC_C_DEFAULT, wxColor(101, 123, 131));
this->StyleSetForeground(wxSTC_C_OPERATOR, wxColor(101, 123, 131));
this->StyleSetForeground(wxSTC_C_HASHQUOTEDSTRING, wxColor(181, 137, 0));
this->StyleSetForeground(wxSTC_C_STRING, wxColor(108, 113, 196));
this->StyleSetForeground(wxSTC_C_PREPROCESSOR, wxColor(181, 137, 0));
this->StyleSetForeground(wxSTC_C_PREPROCESSORCOMMENT, wxColor(181, 137, 0));
this->StyleSetForeground(wxSTC_C_IDENTIFIER, wxColor(38, 139, 210));
this->StyleSetForeground(wxSTC_C_NUMBER, wxColor(211, 54, 130));
this->StyleSetForeground(wxSTC_C_CHARACTER, wxColor(108, 113, 196));
this->StyleSetForeground(wxSTC_C_WORD2, wxColor(203, 75, 22));
this->StyleSetForeground(wxSTC_C_WORD, wxColor(220, 50, 47));
this->StyleSetForeground(wxSTC_C_COMMENT, wxColor(147, 161, 161));
this->StyleSetForeground(wxSTC_C_COMMENTLINE, wxColor(147, 161, 161));
this->StyleSetForeground(wxSTC_C_COMMENTLINEDOC, wxColor(147, 161, 161));
this->StyleSetForeground(wxSTC_C_COMMENTDOC, wxColor(147, 161, 161));
this->StyleSetForeground(wxSTC_C_COMMENTDOCKEYWORD, wxColor(131, 148, 150));
this->StyleSetForeground(wxSTC_C_COMMENTDOCKEYWORDERROR, wxColor(101, 123, 131));
this->StyleSetBold(wxSTC_C_WORD, false);
this->StyleSetBold(wxSTC_C_WORD2, true);
this->StyleSetBold(wxSTC_C_COMMENTDOCKEYWORD, true);
this->StyleSetBold(wxSTC_C_OPERATOR, true);
// Apply the theme
this->ApplyTheme(lang);
// Set up Code folding
this->SetProperty(wxT("fold"), wxT("1"));
@ -147,10 +104,23 @@ void EditPane::Highlight(wxString filePath)
// set spaces and indention
this->SetTabWidth(4);
this->SetUseTabs(true);
bool use_tabs = (lang != "yaml");
this->SetUseTabs(use_tabs);
this->SetTabIndents(true);
this->SetBackSpaceUnIndents(true);
}
void EditPane::ApplyTheme(string lang, string theme)
{
this->SetTheme(theme);
JsonValue lang_list = lang_config->GetRoot();
JsonValue lexer_map = lang_list.get(lang, JsonValue()).get("lexer_map", JsonValue());
// Get the list of keywords for the current language
JsonValue keywords_array = this->GetKeywordList(lang);
if (keywords_array.isArray())
{
for(int i = 0; i < keywords_array.size(); i++)
@ -169,6 +139,9 @@ void EditPane::Highlight(wxString filePath)
wxLogDebug(output.str().c_str());
}
// Do the appropriate mappings to load the selected theme
this->_ApplyTheme(lexer_map);
}
/**
@ -358,3 +331,114 @@ JsonValue EditPane::GetKeywordList(string lang)
.get(lang, JsonValue())
.get("keywords", JsonValue());
}
JsonValue EditPane::GetThemeValue(string type, string key)
{
JsonValue value = this->current_theme
.get(type, JsonValue())
.get(key, JsonValue());
return value;
}
wxColor EditPane::GetThemeColor(string type, string key)
{
JsonValue color_value = this->GetThemeValue(type, key);
if (color_value.isArray())
{
return wxColor(
(unsigned char) color_value[0].asUInt(),
(unsigned char) color_value[1].asUInt(),
(unsigned char) color_value[2].asUInt()
);
}
else
{
return wxColor("black");
}
}
/**
* Iterate through the theme settings and apply them
*
* @param JsonValue lexer_map - Maps token types to theme colors
* @return void
*/
void EditPane::_ApplyTheme(JsonValue lexer_map)
{
// Font setup
#ifdef __WXMAC__
wxFont *defaultFont = wxFont::New(
14,
wxFONTFAMILY_MODERN,
wxFONTFLAG_ANTIALIASED
);
#else
wxFont *defaultFont = wxFont::New(
12,
wxFONTFAMILY_MODERN,
wxFONTFLAG_ANTIALIASED
);
#endif
wxColor default_background = this->GetThemeColor("background", "default");
wxColor default_foreground = this->GetThemeColor("foreground", "default");
wxColor line_number_background = ( ! this->GetThemeValue("line_numbers", "background").isNull())
? (this->GetThemeColor("line_numbers", "background"))
: wxColor("White");
wxColor line_number_foreground = ( ! this->GetThemeValue("line_numbers", "foreground").isNull())
? (this->GetThemeColor("line_numbers", "foreground"))
: wxColor("Black");
// Set default colors/ fonts
for(int i = 0; i <= wxSTC_STYLE_MAX; i++)
{
this->StyleSetBackground(i, default_background);
this->StyleSetFont(i, *defaultFont);
}
this->StyleSetForeground (wxSTC_STYLE_DEFAULT, default_foreground);
this->StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColor(147, 161, 161));
this->SetMarginWidth (MARGIN_LINE_NUMBERS, TextWidth(wxSTC_STYLE_LINENUMBER, _T("_9999")));
this->StyleSetForeground (wxSTC_STYLE_LINENUMBER, line_number_foreground);
this->StyleSetBackground (wxSTC_STYLE_LINENUMBER, line_number_background);
this->SetMarginType (MARGIN_LINE_NUMBERS, wxSTC_MARGIN_NUMBER);
for (int i = 0; i < lexer_map.size(); i++)
{
string key = lexer_map[i].asString();
// Set the foreground color, if it exists
if ( ! this->GetThemeValue("foreground", key).isNull())
{
this->StyleSetForeground(i, this->GetThemeColor("foreground", key));
}
// Set the background color, if it exists
if ( ! this->GetThemeValue("background", key).isNull())
{
this->StyleSetBackground(i, this->GetThemeColor("background", key));
}
// Set bold, if it applies
if (this->GetThemeValue("bold", key).isBool())
{
this->StyleSetBold(i, this->GetThemeValue("bold", key).asBool());
}
}
/*this->StyleSetBold(wxSTC_C_WORD, false);
this->StyleSetBold(wxSTC_C_WORD2, true);
this->StyleSetBold(wxSTC_C_COMMENTDOCKEYWORD, true);
this->StyleSetBold(wxSTC_C_OPERATOR, true);*/
}
void EditPane::SetTheme(string theme_name)
{
JsonValue theme_list = this->theme_config->GetRoot();
this->current_theme = theme_list.get(theme_name, JsonValue());
}

View File

@ -28,11 +28,13 @@ public:
bool SaveFile();
bool SaveFile(const wxString &filename);
bool IsModified();
void ApplyTheme(string lang, string theme="Solarized");
private:
StringConstMap lexerMap;
StringConstMap::iterator lexerMapIt;
TyroConfig *lang_config;
TyroConfig *theme_config;
JsonValue current_theme;
enum
{
MARGIN_FOLD,
@ -42,7 +44,11 @@ private:
bool FileWritable();
void BindEvents();
void OnMarginClick(wxStyledTextEvent &event);
void SetTheme(string theme_name);
JsonValue GetThemeValue(string type, string key);
wxColor GetThemeColor(string type, string key);
JsonValue GetKeywordList(string lang);
void _ApplyTheme(JsonValue lexer_map);
};
#endif // TYRODOC_FRAME_H