Tyro/src/widgets/EditPane.cpp

497 lines
12 KiB
C++
Raw Normal View History

#include "EditPane.h"
EditPane::EditPane(
2015-04-21 14:07:04 -04:00
wxWindow *parent, wxWindowID id, const wxPoint &pos,
const wxSize &size, long style
) : wxStyledTextCtrl (parent, id, pos, size, style)
2015-04-21 14:07:04 -04:00
{
2015-04-21 09:55:28 -04:00
#include "../../config/languages_json.h"
lang_config = new TyroConfig();
lang_config->LoadJson(languages_json);
2015-04-21 14:07:04 -04:00
2015-04-21 09:55:28 -04:00
#include "../../config/themes_json.h"
theme_config = new TyroConfig();
theme_config->LoadJson(themes_json);
2015-04-21 14:07:04 -04:00
2015-04-21 17:06:21 -04:00
// Map language types to their lexers
2015-04-16 11:23:08 -04:00
lexerMap["batch"] = wxSTC_LEX_BATCH;
lexerMap["caml"] = wxSTC_LEX_CAML;
lexerMap["cmake"] = wxSTC_LEX_CMAKE;
2015-04-21 14:07:04 -04:00
lexerMap["coffeescript"] = wxSTC_LEX_COFFEESCRIPT;
2015-04-16 11:23:08 -04:00
lexerMap["cpp"] = wxSTC_LEX_CPP;
lexerMap["css"] = wxSTC_LEX_CSS;
2015-04-21 14:07:04 -04:00
lexerMap["fortran"] = wxSTC_LEX_FORTRAN;
lexerMap["haskell"] = wxSTC_LEX_HASKELL;
2015-04-16 11:23:08 -04:00
lexerMap["html"] = wxSTC_LEX_HTML;
2015-04-21 14:07:04 -04:00
lexerMap["java"] = wxSTC_LEX_CPP;
lexerMap["js"] = wxSTC_LEX_CPP;
lexerMap["lisp"] = wxSTC_LEX_LISP;
lexerMap["lua"] = wxSTC_LEX_LUA;
2015-04-16 11:23:08 -04:00
lexerMap["makefile"] = wxSTC_LEX_MAKEFILE;
2015-04-21 14:07:04 -04:00
lexerMap["markdown"] = wxSTC_LEX_MARKDOWN;
2015-04-20 16:35:51 -04:00
lexerMap["php"] = wxSTC_LEX_PHPSCRIPT;
2015-04-16 11:23:08 -04:00
lexerMap["perl"] = wxSTC_LEX_PERL;
lexerMap["python"] = wxSTC_LEX_PYTHON;
2015-04-21 14:07:04 -04:00
lexerMap["ruby"] = wxSTC_LEX_RUBY;
2015-04-22 16:57:10 -04:00
lexerMap["rust"] = wxSTC_LEX_CPP;
2015-04-16 11:23:08 -04:00
lexerMap["shell"] = wxSTC_LEX_BASH;
2015-04-21 14:07:04 -04:00
lexerMap["sql"] = wxSTC_LEX_SQL;
lexerMap["xml"] = wxSTC_LEX_XML;
lexerMap["yaml"] = wxSTC_LEX_YAML;
this->BindEvents();
}
2015-04-21 14:07:04 -04:00
EditPane::~EditPane()
2015-04-14 15:58:43 -04:00
{
2015-04-21 09:55:28 -04:00
delete lang_config;
delete theme_config;
2015-04-14 15:58:43 -04:00
}
2015-04-10 15:11:15 -04:00
/**
2015-04-17 16:55:48 -04:00
* Handle the highlighting config for the
* selected file
2015-04-21 14:07:04 -04:00
*
2015-04-10 15:11:15 -04:00
* @param wxString filePath
2015-04-17 16:55:48 -04:00
* @return void
2015-04-10 15:11:15 -04:00
*/
2015-04-17 16:55:48 -04:00
void EditPane::Highlight(wxString filePath)
2015-04-10 15:11:15 -04:00
{
2015-04-17 16:55:48 -04:00
this->fileName.Assign(filePath);
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
wxLogDebug("Highlighting method");
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
// Get the configuration name for the selected language
string lang = this->GetLangByFile();
2015-04-22 16:57:10 -04:00
wxLogDebug("Language selected: %s", lang);
2015-04-17 16:55:48 -04:00
2015-04-14 15:58:43 -04:00
this->StyleClearAll();
2015-04-21 14:07:04 -04:00
2015-04-16 11:23:08 -04:00
if (lexerMap.count(lang) > 0)
{
this->SetLexer(lexerMap[lang]);
}
else
{
this->SetLexer(wxSTC_LEX_NULL);
}
2015-04-21 14:07:04 -04:00
// Some basic properties to set
this->SetProperty("technology", "2");
this->SetProperty("error.inline", "0");
this->SetProperty("font.quality", "3"); // LCD Optimized
2015-04-21 14:07:04 -04:00
// Apply the theme
this->ApplyTheme(lang);
// Set up Code folding
2015-04-21 17:06:21 -04:00
this->SetProperty("fold", "1");
this->SetProperty("fold.comment", "1");
this->SetProperty("fold.compact", "1");
this->SetProperty("fold.html", "1");
this->SetFoldFlags(wxSTC_FOLDFLAG_LINEBEFORE_CONTRACTED | wxSTC_FOLDFLAG_LINEAFTER_CONTRACTED);
this->SetMarginType(MARGIN_FOLD, wxSTC_MARGIN_SYMBOL);
this->SetMarginWidth(MARGIN_FOLD, 16);
this->SetMarginSensitive(MARGIN_FOLD, true);
this->SetMarginMask(MARGIN_FOLD, wxSTC_MASK_FOLDERS);
2015-04-21 17:06:21 -04:00
this->MarkerDefine (wxSTC_MARKNUM_FOLDER, wxSTC_MARK_BOXPLUSCONNECTED, "WHITE", "BLACK");
this->MarkerDefine (wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_BOXMINUSCONNECTED, "WHITE", "BLACK");
this->MarkerDefine (wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_VLINE, "BLACK", "BLACK");
this->MarkerDefine (wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_CIRCLEPLUSCONNECTED, "WHITE", "BLACK");
this->MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_CIRCLEMINUSCONNECTED, "WHITE", "BLACK");
this->MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNER, "BLACK", "BLACK");
this->MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNER, "BLACK", "BLACK");
2015-04-21 14:07:04 -04:00
this->SetLayoutCache (wxSTC_CACHE_NONE);
this->SetUseHorizontalScrollBar(1);
2015-04-21 14:07:04 -04:00
// set spaces and indention
this->SetTabWidth(4);
2015-04-21 14:07:04 -04:00
bool use_tabs = (lang != "yaml");
this->SetUseTabs(use_tabs);
this->SetTabIndents(true);
this->SetBackSpaceUnIndents(true);
2015-04-21 14:07:04 -04:00
}
2015-04-21 17:06:21 -04:00
/**
* Set the current theme for the current language
*
* @param string lang
* @param string theme
* @return void
*/
2015-04-21 14:07:04 -04:00
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())
{
2015-04-15 12:17:25 -04:00
for(int i = 0; i < keywords_array.size(); i++)
{
this->SetKeyWords(i, keywords_array[i].asString());
}
}
else
{
string typeMap[] = {"null", "int", "unsigned int", "double", "string", "boolean", "array", "object"};
2015-04-17 16:55:48 -04:00
stringstream output;
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
output << "current lang is:" << lang << endl;
output << "keywords array is not an array" << endl;
output << "keyword array is a " << typeMap[keywords_array.type()] << endl;
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
wxLogDebug(output.str().c_str());
}
2015-04-22 16:57:10 -04:00
int offset_count = 0;//(lang == "php") ? 104 : 0;
2015-04-21 14:07:04 -04:00
// Do the appropriate mappings to load the selected theme
2015-04-22 16:57:10 -04:00
this->_ApplyTheme(lexer_map, offset_count);
2015-04-17 16:55:48 -04:00
}
2015-04-10 15:11:15 -04:00
2015-04-17 16:55:48 -04:00
/**
* Check file path and open the selected file
*
* @param wxString filePath
* @return bool
*/
bool EditPane::Load(wxString filePath)
{
this->fileName.Assign(filePath);
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
if (this->FileReadable())
2015-04-21 14:07:04 -04:00
{
2015-04-17 16:55:48 -04:00
this->Highlight(filePath);
bool didLoad = this->LoadFile(filePath);
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
// @TODO Toggle controls based on write permission
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
return didLoad;
}
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
return false;
2015-04-10 15:11:15 -04:00
}
2015-04-17 16:55:48 -04:00
/**
* Determine the format of the current file by
* matching its extension against the patterns
* in the configuration files
2015-04-21 14:07:04 -04:00
*
2015-04-17 16:55:48 -04:00
* @return string
*/
string EditPane::GetLangByFile()
{
2015-04-21 09:55:28 -04:00
JsonValue langList = lang_config->GetRoot();
JsonValue::iterator it;
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
wxString curr_file = this->fileName.GetFullName();
2015-04-21 14:07:04 -04:00
// Loop through each language to find a matching file pattern
for (it = langList.begin(); it != langList.end(); ++it)
{
string lang = it.key().asString();
2015-04-21 14:07:04 -04:00
// Parse the file pattern
wxString file_pattern((*it)["file_pattern"].asString());
2015-04-21 14:07:04 -04:00
file_pattern.Lower();
2015-04-21 14:07:04 -04:00
while ( ! file_pattern.empty())
{
wxString cur = file_pattern.BeforeFirst(';');
if (
(cur == curr_file) ||
2015-04-21 17:06:21 -04:00
(cur == (curr_file.BeforeLast('.') + ".*")) ||
(cur == ("*." + curr_file.AfterLast('.')))
)
{
return lang;
}
2015-04-21 17:06:21 -04:00
// Go to the next pattern for this language
file_pattern = file_pattern.AfterFirst(';');
}
}
2015-04-21 14:07:04 -04:00
return "";
}
bool EditPane::SaveFile()
{
2015-04-17 16:55:48 -04:00
wxString fname;
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
if ( ! this->fileName.IsOk())
{
wxFileDialog dlg (
2015-04-21 14:07:04 -04:00
this,
_T("Save file"),
wxEmptyString,
wxEmptyString,
TYRO_FILE_SAVE_WILDCARDS,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT
);
2015-04-21 14:07:04 -04:00
if (dlg.ShowModal() != wxID_OK) return false;
2015-04-17 16:55:48 -04:00
fname = dlg.GetPath();
}
else
{
fname = this->fileName.GetFullPath();
}
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
const wxString cfname(fname);
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
return this->SaveFile(cfname);
}
bool EditPane::SaveFile(const wxString &filename)
{
if ( ! this->IsModified()) return true;
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
this->fileName.Assign(filename);
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
// Check file permissions
if (this->FileWritable())
{
this->SetSavePoint();
return wxStyledTextCtrl::SaveFile(filename);
}
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
return false;
}
bool EditPane::IsModified()
{
2015-04-17 16:55:48 -04:00
return this->GetModify();
}
/**
* Check that the current file can be opened
2015-04-21 14:07:04 -04:00
*
* @return bool
2015-04-17 16:55:48 -04:00
*/
bool EditPane::FileReadable()
2015-04-21 14:07:04 -04:00
{
2015-04-17 16:55:48 -04:00
if (
this->fileName.IsOk() &&
this->fileName.Exists() &&
this->fileName.IsFileReadable()
) return true;
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
// Hmm...well, let's give an error
wxMessageDialog errDlg(
2015-04-21 14:07:04 -04:00
this,
TYRO_OPEN_ERROR,
2015-04-17 16:55:48 -04:00
TYRO_OPEN_ERROR_CAPTION,
wxOK | wxICON_ERROR | wxCENTER
);
errDlg.ShowModal();
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
return false;
}
/**
* Check that the current file can be saved
2015-04-21 14:07:04 -04:00
*
* @return bool
2015-04-17 16:55:48 -04:00
*/
bool EditPane::FileWritable()
{
// Lets see...can we write somewhere?
if (
this->fileName.IsOk() &&
((this->fileName.Exists() && this->fileName.IsFileWritable()) ||
(( ! this->fileName.Exists()) && this->fileName.IsDirWritable()))
) return true;
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
// Hmm...well, let's give an error
wxMessageDialog errDlg(
2015-04-21 14:07:04 -04:00
this,
TYRO_SAVE_ERROR,
2015-04-17 16:55:48 -04:00
TYRO_SAVE_ERROR_CAPTION,
wxOK | wxICON_ERROR | wxCENTER
);
errDlg.ShowModal();
return false;
}
void EditPane::BindEvents()
{
Bind(wxEVT_STC_MARGINCLICK, &EditPane::OnMarginClick, this, wxID_ANY);
}
2015-04-21 17:06:21 -04:00
/**
* Code folding event handler
*
* @param wxStyledTextEvent& event
* @return void
*/
void EditPane::OnMarginClick(wxStyledTextEvent& event)
{
if (event.GetMargin() == MARGIN_FOLD) {
int lineClick = this->LineFromPosition (event.GetPosition());
int levelClick = this->GetFoldLevel (lineClick);
if ((levelClick & wxSTC_FOLDLEVELHEADERFLAG) > 0) {
this->ToggleFold (lineClick);
}
}
}
2015-04-21 09:55:28 -04:00
2015-04-21 17:06:21 -04:00
/**
* Get the list of keywords for the selected language
*
* @param string lang
* @return JsonValue
*/
2015-04-21 09:55:28 -04:00
JsonValue EditPane::GetKeywordList(string lang)
{
return lang_config->GetRoot()
.get(lang, JsonValue())
.get("keywords", JsonValue());
}
2015-04-21 14:07:04 -04:00
2015-04-21 17:06:21 -04:00
/**
* Retrieve a setting from the current theme
*
* @param string type
* @param string key
* @return JsonValue
*/
2015-04-21 14:07:04 -04:00
JsonValue EditPane::GetThemeValue(string type, string key)
{
JsonValue value = this->current_theme
.get(type, JsonValue())
.get(key, JsonValue());
return value;
}
2015-04-22 16:57:10 -04:00
/**
* Retrieve the configured color for the specified theme
* @param type
* @param key
* @return
*/
2015-04-21 14:07:04 -04:00
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
2015-04-22 16:57:10 -04:00
* @param int addtoi - Offset for some languages
2015-04-21 14:07:04 -04:00
* @return void
*/
2015-04-22 16:57:10 -04:00
void EditPane::_ApplyTheme(JsonValue lexer_map, int addtoi)
2015-04-21 14:07:04 -04:00
{
// Font setup
#ifdef __WXMAC__
wxFont *defaultFont = wxFont::New(
14,
wxFONTFAMILY_MODERN,
wxFONTFLAG_ANTIALIASED
);
#else
wxFont *defaultFont = wxFont::New(
2015-04-21 21:09:08 -04:00
10,
2015-04-21 14:07:04 -04:00
wxFONTFAMILY_MODERN,
wxFONTFLAG_ANTIALIASED
);
#endif
2015-04-21 17:06:21 -04:00
static const wxColor default_background = this->GetThemeColor("background", "default");
static const wxColor default_foreground = this->GetThemeColor("foreground", "default");
2015-04-21 14:07:04 -04:00
wxColor line_number_background = ( ! this->GetThemeValue("line_numbers", "background").isNull())
? (this->GetThemeColor("line_numbers", "background"))
2015-04-21 17:06:21 -04:00
: default_background;
2015-04-21 14:07:04 -04:00
wxColor line_number_foreground = ( ! this->GetThemeValue("line_numbers", "foreground").isNull())
? (this->GetThemeColor("line_numbers", "foreground"))
2015-04-21 17:06:21 -04:00
: default_foreground;
2015-04-21 14:07:04 -04:00
// Set default colors/ fonts
for(int i = 0; i <= wxSTC_STYLE_MAX; i++)
{
this->StyleSetBackground(i, default_background);
2015-04-21 17:06:21 -04:00
this->StyleSetForeground(i, default_foreground);
2015-04-21 14:07:04 -04:00
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);
2015-04-22 16:57:10 -04:00
int min = 0 + addtoi;
int max = lexer_map.size() + addtoi;
for (int i = min; i < max; i++)
2015-04-21 14:07:04 -04:00
{
2015-04-22 16:57:10 -04:00
int n = i - addtoi;
string key = lexer_map[n].asString();
//wxLogDebug("Token type: %s", key);
//wxLogDebug("Lexer constant: %i", i);
2015-04-21 14:07:04 -04:00
// Set the foreground color, if it exists
if ( ! this->GetThemeValue("foreground", key).isNull())
{
this->StyleSetForeground(i, this->GetThemeColor("foreground", key));
2015-04-22 16:57:10 -04:00
wxLogDebug("Set foreground color for %s token type, on value: %i", key, i);
2015-04-21 14:07:04 -04:00
}
// Set the background color, if it exists
if ( ! this->GetThemeValue("background", key).isNull())
{
this->StyleSetBackground(i, this->GetThemeColor("background", key));
2015-04-22 16:57:10 -04:00
wxLogDebug("Set background color for %s token type, on value: %i", key, i);
2015-04-21 14:07:04 -04:00
}
// Set bold, if it applies
if (this->GetThemeValue("bold", key).isBool())
{
this->StyleSetBold(i, this->GetThemeValue("bold", key).asBool());
}
2015-04-21 17:06:21 -04:00
// Italic
if (this->GetThemeValue("italic", key).isBool())
{
this->StyleSetItalic(i, this->GetThemeValue("italic", key).asBool());
}
2015-04-21 14:07:04 -04:00
}
}
void EditPane::SetTheme(string theme_name)
{
JsonValue theme_list = this->theme_config->GetRoot();
this->current_theme = theme_list.get(theme_name, JsonValue());
}