nana_test/codeEditor.cpp

144 lines
3.7 KiB
C++

#include "codeEditor.h"
/**
* @brief Construct a new Code Editor:: Code Editor object
*
*/
CodeEditor::CodeEditor()
{
caption("Simple Notepad - Nana C++ Library");
textbox_.borderless(true);
API::effects_edge_nimbus(textbox_, effects::edge_nimbus::none);
textbox_.enable_dropfiles(true);
textbox_.events().mouse_dropfiles([this](const arg_dropfiles &arg) {
if (arg.files.size() && _m_ask_save())
textbox_.load(arg.files.front());
});
// textbox tb;
textbox_.set_highlight("keyWord", colors::dark_blue, colors::white);
textbox_.set_keywords("keyWord", false, true,
{"while", "for", "if", "else", "switch", "case",
"break", "struct", "typedef", "uinon", "enum",
"static", "const", "#include", "#define", "using",
"namespace", "auto"});
textbox_.set_highlight("Type", colors::red, {});
textbox_.set_keywords("Type", false, true,
{"void", "unsigned", "char", "int", "uint8_t", "int8_t",
"uint16_t", "int16_t", "uint32_t", "int32_t", "float",
"double", "long"});
textbox_.set_highlight("field", colors::deep_sky_blue, {});
textbox_.set_keywords("field", false, false, {"//*"});
_m_make_menus();
place_.div("vert<menubar weight=28><textbox>");
place_["menubar"] << menubar_;
place_["textbox"] << textbox_;
place_.collocate();
events().unload([this](const arg_unload &arg) {
if (!_m_ask_save())
arg.cancel = true;
});
}
/**
* @brief Destroy the Code Editor:: Code Editor object
*
*/
CodeEditor::~CodeEditor()
{
}
/**
* @brief
*
* @param is_open
* @return std::filesystem::path
*/
std::filesystem::path CodeEditor::_m_pick_file(bool is_open) const {
filebox fbox(*this, is_open);
fbox.add_filter("Text", "*.txt");
fbox.add_filter("c/c++", "*.c");
fbox.add_filter("All Files", "*.*");
auto files = fbox.show();
return (files.empty() ? std::filesystem::path{} : files.front());
}
/**
* @brief
*
* @return true
* @return false
*/
bool CodeEditor::_m_ask_save() {
if (textbox_.edited()) {
auto fs = textbox_.filename();
msgbox box(*this, "Simple Notepad", msgbox::button_t::yes_no_cancel);
box << "Do you want to save these changes?";
switch (box.show()) {
case msgbox::pick_yes:
if (fs.empty()) {
fs = _m_pick_file(false);
if (fs.empty())
break;
if (fs.extension().string() != ".txt")
fs = fs.extension().string() + ".txt";
}
textbox_.store(fs);
break;
case msgbox::pick_no:
break;
case msgbox::pick_cancel:
return false;
}
}
return true;
}
/**
* @brief
*
*/
void CodeEditor::_m_make_menus()
{
menubar_.push_back("&FILE");
menubar_.at(0).append("New", [this](menu::item_proxy &ip) {
if (_m_ask_save())
textbox_.reset();
});
menubar_.at(0).append("Open", [this](menu::item_proxy &ip) {
if (_m_ask_save()) {
static int a = 32;
auto fs = _m_pick_file(true);
if (!fs.empty())
textbox_.load(fs);
a++;
}
});
menubar_.at(0).append("Save", [this](menu::item_proxy &) {
auto fs = textbox_.filename();
if (fs.empty()) {
fs = _m_pick_file(false);
if (fs.empty())
return;
}
textbox_.store(fs);
});
menubar_.push_back("F&ORMAT");
menubar_.at(1).append("Line Wrap", [this](menu::item_proxy &ip) {
textbox_.line_wrapped(ip.checked());
});
menubar_.at(1).check_style(0, menu::checks::highlight);
}
/**
* @brief
*
* @param wait
*/
void Wait(unsigned wait=0)
{
if (wait)
std::this_thread::sleep_for(std::chrono::seconds{ wait } );
}