Tyro/src/widgets/EditorPane.cpp

470 lines
12 KiB
C++
Raw Normal View History

/**
* The editor widget
*/
#include "src/widgets/EditorPane.h"
#include "src/widgets/TabContainer.h"
extern StringConstMap Glob_lexer_map;
extern LangConfig *Glob_lang_config;
extern ThemeConfig *Glob_theme_config;
extern wxConfigBase *Glob_config;
/**
* Constructor
*
* @param wxWindow* parent
* @param wxWindowID id
* @param const wxPoint& pos
* @param const wxSize& size
*/
EditorPane::EditorPane(
wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size
) : wxStyledTextCtrl (parent, id, pos, size, wxBORDER_NONE)
2015-04-21 14:07:04 -04:00
{
this->BindEvents();
// Some basic properties to set
this->SetStyleBits(8);
this->SetScrollWidthTracking(true);// Set scroll width by longest line
this->SetProperty("technology", "2");
this->SetProperty("error.inline", "0");
this->SetProperty("styling.within.preprocessor", "1");
this->SetProperty("lexer.cpp.track.preprocessor", "1");
this->SetProperty("font.quality", "3"); // LCD Optimized
// this->SetLayoutCache (wxSTC_CACHE_DOCUMENT);
// set spaces and indention
this->SetTabWidth(4);
// Blank highlight
this->Highlight("");
}
/**
* Destructor
*/
EditorPane::~EditorPane()
2015-04-14 15:58:43 -04:00
{
wxLogDebug("EditorPane Destructor Called.");
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
*/
void EditorPane::Highlight(const 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
// Get the configuration name for the selected language
string lang = Glob_lang_config->GetLangByFile(this->fileName);
2015-04-17 16:55:48 -04:00
2015-04-21 14:07:04 -04:00
// Apply the theme
this->ApplyTheme(lang);
bool use_tabs = (lang != "yaml");
2015-04-23 11:03:50 -04:00
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
*/
void EditorPane::ApplyTheme(const string &lang, const string &theme)
{
this->StyleClearAll();
if (Glob_lexer_map.count(lang) > 0)
{
this->SetLexer(Glob_lexer_map[lang]);
}
else
{
this->SetLexer(wxSTC_LEX_NULL);
}
2019-03-20 16:49:25 -04:00
if ( ! theme.empty())
{
Glob_theme_config->SetTheme(theme);
}
2015-04-21 14:07:04 -04:00
2015-04-29 17:03:15 -04:00
// Get the keywords and mapping for the selected language
JsonValue lexer_map = Glob_lang_config->GetLexerMap(lang);
JsonValue keywords_array = Glob_lang_config->GetKeywordList(lang);
2015-04-29 17:03:15 -04:00
if (keywords_array.isArray())
{
2015-04-22 21:06:35 -04:00
for(unsigned int i = 0; i < keywords_array.size(); i++)
2015-04-15 12:17:25 -04:00
{
this->SetKeyWords(i, keywords_array[i].asString());
}
}
else
{
2019-03-20 16:49:25 -04:00
if ( ! lang.empty())
2015-05-08 16:01:36 -04:00
{
string typeMap[] = {"null", "int", "unsigned int", "double", "string", "boolean", "array", "object"};
stringstream output;
2015-04-21 14:07:04 -04:00
2015-05-08 16:01:36 -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-05-08 16:01:36 -04:00
wxLogDebug(output.str().c_str());
}
}
2015-04-21 14:07:04 -04:00
// Do the appropriate mappings to load the selected theme
2015-04-29 17:03:15 -04:00
this->_ApplyTheme(lexer_map);
2015-04-17 16:55:48 -04:00
}
2015-04-10 15:11:15 -04:00
/**
* Re-style the control based on changed preferences
*
* @param string [theme]
* @return void
*/
void EditorPane::ReApplyTheme(const string &theme)
{
this->ApplyTheme(Glob_lang_config->GetLangByName(this->GetCurrentLang()), theme);
}
2015-04-17 16:55:48 -04:00
/**
* Check file path and open the selected file
*
* @param wxString filePath
* @return bool
*/
bool EditorPane::Load(const wxString &filePath)
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
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
}
/**
* Save the current file
*
* @return bool
*/
bool EditorPane::SaveFile()
{
wxString fname = this->fileName.GetFullPath();
2015-04-21 14:07:04 -04:00
2019-05-16 15:21:32 -04:00
return this->SaveFile(fname);
}
/**
* Save the current file with the specified filename
*
* @param const wxString filename
* @return bool
*/
bool EditorPane::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();
bool saved = wxStyledTextCtrl::SaveFile(filename);
if (saved)
{
auto parent = (TabContainer*) this->GetParent();
auto currentPage = parent->GetCurrentPage();
auto idx = parent->GetPageIndex(currentPage);
wxString currentTitle = parent->GetPageText(idx);
currentTitle.Replace("*", "");
parent->SetPageText(idx, currentTitle);
}
return saved;
2015-04-17 16:55:48 -04:00
}
2015-04-21 14:07:04 -04:00
2015-04-17 16:55:48 -04:00
return false;
}
2015-04-17 16:55:48 -04:00
/**
* 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 EditorPane::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 EditorPane::FileWritable()
2015-04-17 16:55:48 -04:00
{
// 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;
}
/**
* Wire Event handlers
*
* @return void
*/
void EditorPane::BindEvents()
{
// Code folding event handler
this->Bind(wxEVT_STC_MARGINCLICK, [=](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-23 11:03:50 -04:00
}
}, wxID_ANY);
// On modification, update parent tab to show "dirtyness"
this->Bind(wxEVT_STC_MODIFIED, [=](wxStyledTextEvent& event) {
auto parent = (TabContainer*) this->GetParent();
auto currentPage = parent->GetCurrentPage();
auto idx = parent->GetPageIndex(currentPage);
wxString currentTitle = parent->GetPageText(idx);
if (this->IsModified() && ! currentTitle.Contains("*"))
{
parent->SetPageText(idx, currentTitle + "*");
}
if (currentTitle.Contains("*") && ! this->IsModified())
{
currentTitle.Replace("*", "");
parent->SetPageText(idx, currentTitle);
}
}, wxID_ANY);
2019-06-03 11:14:38 -04:00
this->Bind(wxEVT_STC_SAVEPOINTREACHED, [=](wxStyledTextEvent& event) {
auto parent = (TabContainer*) this->GetParent();
2019-06-03 11:14:38 -04:00
auto currentPage = parent->GetCurrentPage();
auto idx = parent->GetPageIndex(currentPage);
wxString currentTitle = parent->GetPageText(idx);
if (currentTitle.Contains("*"))
{
currentTitle.Replace("*", "");
parent->SetPageText(idx, currentTitle);
}
}, wxID_ANY);
// this->Bind(wxEVT_STC_CHARADDED, &EditorPane::OnCharAdded, this, wxID_ANY);
}
2015-04-21 09:55:28 -04:00
2015-04-21 14:07:04 -04:00
/**
* Iterate through the theme settings and apply them
*
* @param JsonValue lexer_map - Maps token types to theme colors
* @return void
*/
void EditorPane::_ApplyTheme(JsonValue &lexer_map)
2015-04-21 14:07:04 -04:00
{
// Make sure to have a default font, especially for Linux
wxFont globalFont = wxSystemSettings::GetFont(wxSYS_ANSI_FIXED_FONT);
2015-04-21 14:07:04 -04:00
static const wxColor default_background = Glob_theme_config->GetThemeColor("background", "default");
static const wxColor default_foreground = Glob_theme_config->GetThemeColor("foreground", "default");
wxColor line_number_background = ( ! Glob_theme_config->GetThemeValue("line_numbers", "background").isNull())
? (Glob_theme_config->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 = ( ! Glob_theme_config->GetThemeValue("line_numbers", "foreground").isNull())
? (Glob_theme_config->GetThemeColor("line_numbers", "foreground"))
2015-04-21 17:06:21 -04:00
: default_foreground;
2015-04-21 14:07:04 -04:00
// Attempt to set the font according to config
Glob_config->Read("global_font", &globalFont);
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);
this->StyleSetFont(i, globalFont);
2015-04-21 14:07:04 -04:00
}
2019-05-16 15:21:32 -04:00
// Set up Code folding
if (Glob_config->ReadBool("show_code_folding", false))
{
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);
this->MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_BOXPLUSCONNECTED, "WHITE", "BLACK");
this->MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_BOXMINUSCONNECTED, "WHITE", "BLACK");
2019-05-17 16:19:42 -04:00
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");
2019-05-17 16:19:42 -04:00
this->MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNER, "BLACK", "BLACK");
this->MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNER, "BLACK", "BLACK");
}
else
{
this->SetProperty("fold", "0");
this->SetProperty("fold.comment", "0");
this->SetProperty("fold.compact", "0");
this->SetProperty("fold.html", "0");
this->SetMarginWidth(MARGIN_FOLD, 0);
}
// Setup indent guides
if (Glob_config->ReadBool("show_indent_guides", false))
{
this->StyleSetForeground(wxSTC_STYLE_DEFAULT, default_foreground);
this->StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, default_foreground);
this->SetIndentationGuides(1);
}
else
{
this->SetIndentationGuides(0);
}
2015-04-21 14:07:04 -04:00
// Setup line numbers
if (Glob_config->ReadBool("show_line_numbers", true))
{
this->SetMarginWidth (MARGIN_LINE_NUMBERS, TextWidth(wxSTC_STYLE_LINENUMBER, "_999"));
this->StyleSetForeground (wxSTC_STYLE_LINENUMBER, line_number_foreground);
this->StyleSetBackground (wxSTC_STYLE_LINENUMBER, line_number_background);
this->SetMarginType (MARGIN_LINE_NUMBERS, wxSTC_MARGIN_NUMBER);
}
else
{
this->SetMarginWidth (MARGIN_LINE_NUMBERS, 0);
}
2015-04-22 16:57:10 -04:00
2015-05-28 16:25:16 -04:00
int max = lexer_map.size();
2015-04-29 17:03:15 -04:00
for (int i = 0; i < max; i++)
2015-04-21 14:07:04 -04:00
{
2015-04-29 17:03:15 -04:00
string key = lexer_map[i].asString();
2015-04-21 14:07:04 -04:00
// Set the foreground color, if it exists
if ( ! Glob_theme_config->GetThemeValue("foreground", key).isNull())
2015-04-21 14:07:04 -04:00
{
this->StyleSetForeground(i, Glob_theme_config->GetThemeColor("foreground", key));
2015-04-21 14:07:04 -04:00
}
// Set the background color, if it exists
if ( ! Glob_theme_config->GetThemeValue("background", key).isNull())
2015-04-21 14:07:04 -04:00
{
this->StyleSetBackground(i, Glob_theme_config->GetThemeColor("background", key));
2015-04-21 14:07:04 -04:00
}
// Set bold, if it applies
if (Glob_theme_config->GetThemeValue("bold", key).isBool())
2015-04-21 14:07:04 -04:00
{
this->StyleSetBold(i, Glob_theme_config->GetThemeValue("bold", key).asBool());
2015-04-21 14:07:04 -04:00
}
2015-04-21 17:06:21 -04:00
// Italic
if (Glob_theme_config->GetThemeValue("italic", key).isBool())
2015-04-21 17:06:21 -04:00
{
this->StyleSetItalic(i, Glob_theme_config->GetThemeValue("italic", key).asBool());
2015-04-21 17:06:21 -04:00
}
// Underline
if (Glob_theme_config->GetThemeValue("underline", key).isBool())
{
this->StyleSetUnderline(i, Glob_theme_config->GetThemeValue("underline", key).asBool());
}
2015-04-21 14:07:04 -04:00
}
}
/**
* Get the display name of the currently selected
* language used for highlighting
*
* @return string
*/
string EditorPane::GetCurrentLang()
{
return Glob_lang_config->GetCurrentLangName();
}
/**
* Set the highlighting language
*
* @param string name
* @return void
*/
void EditorPane::SetCurrentLang(const string &name)
{
// Update the current lang in the config
string langKey = Glob_lang_config->GetLangByName(name);
Glob_lang_config->SetLang(langKey);
// Re-highlight the page with the new langauge
this->ApplyTheme(langKey);
}