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

@ -1,37 +1,46 @@
#include "EditPane.h"
EditPane::EditPane(
wxWindow *parent, wxWindowID id, const wxPoint &pos,
wxWindow *parent, wxWindowID id, const wxPoint &pos,
const wxSize &size, long style
) : wxStyledTextCtrl (parent, id, pos, size, style)
{
{
#include "../../config/languages_json.h"
lang_config = new TyroConfig();
lang_config->LoadJson(languages_json);
#include "../../config/themes_json.h"
theme_config = new TyroConfig();
theme_config->LoadJson(themes_json);
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();
}
EditPane::~EditPane()
EditPane::~EditPane()
{
delete lang_config;
delete theme_config;
@ -40,36 +49,21 @@ EditPane::~EditPane()
/**
* Handle the highlighting config for the
* selected file
*
*
* @param wxString filePath
* @return void
*/
void EditPane::Highlight(wxString filePath)
{
this->fileName.Assign(filePath);
wxLogDebug("Highlighting method");
// Get the configuration name for the selected language
string lang = this->GetLangByFile();
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]);
@ -78,52 +72,15 @@ void EditPane::Highlight(wxString filePath)
{
this->SetLexer(wxSTC_LEX_NULL);
}
// Some basic properties to set
this->SetProperty("technology", "2");
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"));
this->SetProperty(wxT("fold.comment"), wxT("1"));
@ -141,16 +98,29 @@ void EditPane::Highlight(wxString filePath)
this->MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_CIRCLEMINUSCONNECTED, _T("WHITE"), _T("BLACK"));
this->MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNER, _T("BLACK"), _T("BLACK"));
this->MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNER, _T("BLACK"), _T("BLACK"));
this->SetLayoutCache (wxSTC_CACHE_NONE);
this->SetUseHorizontalScrollBar(1);
// 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++)
@ -162,13 +132,16 @@ void EditPane::Highlight(wxString filePath)
{
string typeMap[] = {"null", "int", "unsigned int", "double", "string", "boolean", "array", "object"};
stringstream output;
output << "current lang is:" << lang << endl;
output << "keywords array is not an array" << endl;
output << "keyword array is a " << typeMap[keywords_array.type()] << endl;
wxLogDebug(output.str().c_str());
}
// Do the appropriate mappings to load the selected theme
this->_ApplyTheme(lexer_map);
}
/**
@ -180,17 +153,17 @@ void EditPane::Highlight(wxString filePath)
bool EditPane::Load(wxString filePath)
{
this->fileName.Assign(filePath);
if (this->FileReadable())
{
{
this->Highlight(filePath);
bool didLoad = this->LoadFile(filePath);
// @TODO Toggle controls based on write permission
return didLoad;
}
return false;
}
@ -198,26 +171,26 @@ bool EditPane::Load(wxString filePath)
* Determine the format of the current file by
* matching its extension against the patterns
* in the configuration files
*
*
* @return string
*/
string EditPane::GetLangByFile()
{
JsonValue langList = lang_config->GetRoot();
JsonValue::iterator it;
wxString curr_file = this->fileName.GetFullName();
// Loop through each language to find a matching file pattern
for (it = langList.begin(); it != langList.end(); ++it)
{
string lang = it.key().asString();
// Parse the file pattern
wxString file_pattern((*it)["file_pattern"].asString());
file_pattern.Lower();
while ( ! file_pattern.empty())
{
wxString cur = file_pattern.BeforeFirst(';');
@ -232,25 +205,25 @@ string EditPane::GetLangByFile()
file_pattern = file_pattern.AfterFirst(';');
}
}
return "";
}
bool EditPane::SaveFile()
{
wxString fname;
if ( ! this->fileName.IsOk())
{
wxFileDialog dlg (
this,
_T("Save file"),
wxEmptyString,
this,
_T("Save file"),
wxEmptyString,
wxEmptyString,
TYRO_FILE_SAVE_WILDCARDS,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT
);
if (dlg.ShowModal() != wxID_OK) return false;
fname = dlg.GetPath();
}
@ -258,25 +231,25 @@ bool EditPane::SaveFile()
{
fname = this->fileName.GetFullPath();
}
const wxString cfname(fname);
return this->SaveFile(cfname);
}
bool EditPane::SaveFile(const wxString &filename)
{
if ( ! this->IsModified()) return true;
this->fileName.Assign(filename);
// Check file permissions
if (this->FileWritable())
{
this->SetSavePoint();
return wxStyledTextCtrl::SaveFile(filename);
}
return false;
}
@ -287,33 +260,33 @@ bool EditPane::IsModified()
/**
* Check that the current file can be opened
*
* @return bool
*
* @return bool
*/
bool EditPane::FileReadable()
{
{
if (
this->fileName.IsOk() &&
this->fileName.Exists() &&
this->fileName.IsFileReadable()
) return true;
// Hmm...well, let's give an error
wxMessageDialog errDlg(
this,
TYRO_OPEN_ERROR,
this,
TYRO_OPEN_ERROR,
TYRO_OPEN_ERROR_CAPTION,
wxOK | wxICON_ERROR | wxCENTER
);
errDlg.ShowModal();
return false;
}
/**
* Check that the current file can be saved
*
* @return bool
*
* @return bool
*/
bool EditPane::FileWritable()
{
@ -323,11 +296,11 @@ bool EditPane::FileWritable()
((this->fileName.Exists() && this->fileName.IsFileWritable()) ||
(( ! this->fileName.Exists()) && this->fileName.IsDirWritable()))
) return true;
// Hmm...well, let's give an error
wxMessageDialog errDlg(
this,
TYRO_SAVE_ERROR,
this,
TYRO_SAVE_ERROR,
TYRO_SAVE_ERROR_CAPTION,
wxOK | wxICON_ERROR | wxCENTER
);
@ -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

@ -10,7 +10,7 @@ class EditPane: public wxStyledTextCtrl
{
public:
EditPane(
wxWindow *parent,
wxWindow *parent,
wxWindowID id = wxID_ANY,
const wxPoint &post = wxDefaultPosition,
const wxSize &size = wxDefaultSize,
@ -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