From 1675305884e1bef0b1da7c6e2d2e07985e3f3a59 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 10 Dec 2015 12:40:28 +0100 Subject: [PATCH] New singleton system without a singleton container class. --- src/CMakeLists.txt | 2 - src/cmake.cc | 5 +- src/config.cc | 4 +- src/config.h | 9 +- src/dialogs.cc | 15 +-- src/directories.cc | 2 +- src/directories.h | 6 ++ src/juci.cc | 43 ++++----- src/juci.h | 5 +- src/menu.cc | 11 +-- src/menu.h | 8 +- src/notebook.cc | 39 ++++---- src/notebook.h | 4 +- src/singletons.cc | 20 ---- src/singletons.h | 22 ----- src/source.cc | 41 ++++---- src/source.h | 4 +- src/source_clang.cc | 27 +++--- src/terminal.cc | 14 +-- src/terminal.h | 7 ++ src/window.cc | 224 ++++++++++++++++++++++---------------------- src/window.h | 8 +- 22 files changed, 247 insertions(+), 273 deletions(-) delete mode 100644 src/singletons.cc delete mode 100644 src/singletons.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f47e2a1..86bb96a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -69,8 +69,6 @@ set(source_files juci.h terminal.cc tooltips.h tooltips.cc - singletons.h - singletons.cc cmake.h cmake.cc dialogs.cc diff --git a/src/cmake.cc b/src/cmake.cc index 6826126..e7df7ee 100644 --- a/src/cmake.cc +++ b/src/cmake.cc @@ -1,7 +1,8 @@ #include "cmake.h" -#include "singletons.h" #include "filesystem.h" #include "dialogs.h" +#include "config.h" +#include "terminal.h" #include #include //TODO: remove @@ -47,7 +48,7 @@ CMake::CMake(const boost::filesystem::path &path) { bool CMake::create_compile_commands(const boost::filesystem::path &path) { Dialog::Message message("Creating "+path.string()+"/compile_commands.json"); - auto exit_status=Singleton::terminal->process(Singleton::config->terminal.cmake_command+" . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", path); + auto exit_status=Terminal::get().process(Config::get().terminal.cmake_command+" . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", path); message.hide(); if(exit_status==EXIT_SUCCESS) { #ifdef _WIN32 //Temporary fix to MSYS2's libclang diff --git a/src/config.cc b/src/config.cc index 2f7ea47..2283f1c 100644 --- a/src/config.cc +++ b/src/config.cc @@ -1,10 +1,10 @@ -#include "singletons.h" #include "config.h" #include "logging.h" #include #include "files.h" #include #include "filesystem.h" +#include "terminal.h" using namespace std; //TODO: remove @@ -38,7 +38,7 @@ void Config::load() { retrieve_config(); } catch(const std::exception &e) { - Singleton::terminal->print("Error reading "+config_json+": "+e.what()+"\n"); + ::Terminal::get().print("Error reading "+config_json+": "+e.what()+"\n"); std::stringstream ss; ss << configjson; boost::property_tree::read_json(ss, cfg); diff --git a/src/config.h b/src/config.h index 99e398e..d86f1d2 100644 --- a/src/config.h +++ b/src/config.h @@ -2,7 +2,6 @@ #define JUCI_CONFIG_H_ #include #include -#include "menu.h" #include #include #include @@ -59,8 +58,14 @@ public: std::unordered_map documentation_searches; }; - +private: Config(); +public: + static Config &get() { + static Config singleton; + return singleton; + } + void load(); Menu menu; diff --git a/src/dialogs.cc b/src/dialogs.cc index afcd1e0..a62c9ac 100644 --- a/src/dialogs.cc +++ b/src/dialogs.cc @@ -1,6 +1,5 @@ #include "dialogs.h" -#include "juci.h" -#include "singletons.h" +#include "window.h" #include namespace sigc { @@ -18,10 +17,7 @@ namespace sigc { } Dialog::Message::Message(const std::string &text): Gtk::MessageDialog(text, false, Gtk::MessageType::MESSAGE_INFO, Gtk::ButtonsType::BUTTONS_NONE, true) { - auto g_application=g_application_get_default(); - auto gio_application=Glib::wrap(g_application, true); - auto application=Glib::RefPtr::cast_static(gio_application); - set_transient_for(*application->window); + set_transient_for(::Window::get()); set_position(Gtk::WindowPosition::WIN_POS_CENTER_ON_PARENT); show_now(); @@ -36,12 +32,9 @@ std::string Dialog::gtk_dialog(const std::string &title, const std::string &file_name) { Gtk::FileChooserDialog dialog(title, gtk_options); - auto g_application=g_application_get_default(); //TODO: Post issue that Gio::Application::get_default should return pointer and not Glib::RefPtr - auto gio_application=Glib::wrap(g_application, true); - auto application=Glib::RefPtr::cast_static(gio_application); - dialog.set_transient_for(*application->window); + dialog.set_transient_for(Window::get()); - auto current_path=application->window->notebook.get_current_folder(); + auto current_path=Window::get().notebook.get_current_folder(); boost::system::error_code ec; if(current_path.empty()) current_path=boost::filesystem::current_path(ec); diff --git a/src/directories.cc b/src/directories.cc index 21c23d8..586f88a 100644 --- a/src/directories.cc +++ b/src/directories.cc @@ -1,4 +1,4 @@ -#include "singletons.h" +#include "directories.h" #include "logging.h" #include #include diff --git a/src/directories.h b/src/directories.h index 3921012..0bca969 100644 --- a/src/directories.h +++ b/src/directories.h @@ -26,7 +26,13 @@ public: Gtk::TreeModelColumn color; }; +private: Directories(); +public: + static Directories &get() { + static Directories singleton; + return singleton; + } ~Directories(); void open(const boost::filesystem::path& dir_path=""); void update(); diff --git a/src/juci.cc b/src/juci.cc index 0e24aaa..8c6968a 100644 --- a/src/juci.cc +++ b/src/juci.cc @@ -1,16 +1,12 @@ #include "juci.h" -#include "singletons.h" +#include "window.h" +#include "directories.h" +#include "menu.h" +#include "config.h" +#include "logging.h" using namespace std; //TODO: remove -void Application::init_logging() { - boost::log::add_common_attributes(); - auto log_dir = Singleton::config->juci_home_path()/"log"/"juci.log"; - boost::log::add_file_log(boost::log::keywords::file_name = log_dir, - boost::log::keywords::auto_flush = true); - JINFO("Logging initalized"); -} - int Application::on_command_line(const Glib::RefPtr &cmd) { Glib::set_prgname("juci"); Glib::OptionContext ctx("[PATH ...]"); @@ -39,7 +35,7 @@ int Application::on_command_line(const Glib::RefPtr files.emplace_back(new_p); } else - Singleton::terminal->print("Error: folder path "+parent_p.string()+" does not exist.\n", true); + Terminal::get().print("Error: folder path "+parent_p.string()+" does not exist.\n", true); } } } @@ -48,12 +44,12 @@ int Application::on_command_line(const Glib::RefPtr } void Application::on_activate() { - add_window(*window); - window->show(); + add_window(Window::get()); + Window::get().show(); bool first_directory=true; for(auto &directory: directories) { if(first_directory) { - Singleton::directories->open(directory); + Directories::get().open(directory); first_directory=false; } else { @@ -67,25 +63,24 @@ void Application::on_activate() { it++; } std::thread another_juci_app([this, directory, files_in_directory](){ - Singleton::terminal->async_print("Executing: juci "+directory.string()+files_in_directory+"\n"); - Singleton::terminal->process("juci "+directory.string()+files_in_directory, "", false); + Terminal::get().async_print("Executing: juci "+directory.string()+files_in_directory+"\n"); + Terminal::get().process("juci "+directory.string()+files_in_directory, "", false); }); another_juci_app.detach(); } } for(auto &file: files) - window->notebook.open(file); + Window::get().notebook.open(file); } void Application::on_startup() { Gtk::Application::on_startup(); - Singleton::menu->init(); - Singleton::menu->build(); + Menu::get().build(); - auto object = Singleton::menu->builder->get_object("juci-menu"); + auto object = Menu::get().builder->get_object("juci-menu"); auto juci_menu = Glib::RefPtr::cast_dynamic(object); - object = Singleton::menu->builder->get_object("window-menu"); + object = Menu::get().builder->get_object("window-menu"); auto window_menu = Glib::RefPtr::cast_dynamic(object); if (!juci_menu || !window_menu) { std::cerr << "Menu not found." << std::endl; @@ -97,15 +92,15 @@ void Application::on_startup() { } Application::Application() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) { - Singleton::init(); - init_logging(); + boost::log::add_common_attributes(); + auto log_dir = Config::get().juci_home_path()/"log"/"juci.log"; + boost::log::add_file_log(boost::log::keywords::file_name = log_dir, boost::log::keywords::auto_flush = true); + JINFO("Logging initalized"); Glib::set_application_name("juCi++"); //Gtk::MessageDialog without buttons caused text to be selected, this prevents that Gtk::Settings::get_default()->property_gtk_label_select_on_focus()=false; - - window=std::unique_ptr(new Window()); } int main(int argc, char *argv[]) { diff --git a/src/juci.h b/src/juci.h index 5c8f6cd..a957b87 100644 --- a/src/juci.h +++ b/src/juci.h @@ -2,8 +2,7 @@ #define JUCI_JUCI_H_ #include -#include "logging.h" -#include "window.h" +#include class Application : public Gtk::Application { public: @@ -11,11 +10,9 @@ public: int on_command_line(const Glib::RefPtr &cmd) override; void on_activate() override; void on_startup() override; - std::unique_ptr window; private: std::vector directories; std::vector files; - void init_logging(); }; #endif // JUCI_JUCI_H_ diff --git a/src/menu.cc b/src/menu.cc index 60b8e9c..683ac71 100644 --- a/src/menu.cc +++ b/src/menu.cc @@ -1,16 +1,13 @@ #include "menu.h" -#include "singletons.h" +#include "config.h" #include #include using namespace std; //TODO: remove +//TODO: if Ubuntu ever gets fixed, cleanup the Ubuntu specific code Menu::Menu() { -} - -//TODO: if Ubuntu ever gets fixed, move to constructor, also cleanup the rest of the Ubuntu specific code -void Menu::init() { - auto accels=Singleton::config->menu.keys; + auto accels=Config::get().menu.keys; for(auto &accel: accels) { #ifdef JUCI_UBUNTU_BUGGED_MENU size_t pos=0; @@ -312,7 +309,7 @@ void Menu::set_keys() { auto gio_application=Glib::wrap(g_application, true); auto application=Glib::RefPtr::cast_static(gio_application); - for(auto &key: Singleton::config->menu.keys) { + for(auto &key: Config::get().menu.keys) { if(key.second.size()>0 && actions.find(key.first)!=actions.end()) { #if GTK_VERSION_GE(3, 12) application->set_accel_for_action("app."+key.first, key.second); diff --git a/src/menu.h b/src/menu.h index 3a1296e..1018eeb 100644 --- a/src/menu.h +++ b/src/menu.h @@ -6,9 +6,13 @@ #include class Menu { -public: +private: Menu(); - void init(); //For Ubuntu 14... +public: + static Menu &get() { + static Menu singleton; + return singleton; + } void add_action(const std::string &name, std::function action); std::unordered_map > actions; diff --git a/src/notebook.cc b/src/notebook.cc index 576ee79..3ce0936 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -1,5 +1,6 @@ -#include "singletons.h" #include "notebook.h" +#include "config.h" +#include "directories.h" #include "logging.h" #include #include @@ -92,7 +93,7 @@ void Notebook::open(const boost::filesystem::path &file_path) { if(boost::filesystem::exists(file_path)) { std::ifstream can_read(file_path.string()); if(!can_read) { - Singleton::terminal->print("Error: could not open "+file_path.string()+"\n", true); + Terminal::get().print("Error: could not open "+file_path.string()+"\n", true); return; } can_read.close(); @@ -100,15 +101,15 @@ void Notebook::open(const boost::filesystem::path &file_path) { auto language=Source::guess_language(file_path); boost::filesystem::path project_path; - auto &directories=Singleton::directories; - if(directories->cmake && directories->cmake->project_path!="" && file_path.generic_string().substr(0, directories->cmake->project_path.generic_string().size()+1)==directories->cmake->project_path.generic_string()+'/') - project_path=directories->cmake->project_path; + auto &directories=Directories::get(); + if(directories.cmake && directories.cmake->project_path!="" && file_path.generic_string().substr(0, directories.cmake->project_path.generic_string().size()+1)==directories.cmake->project_path.generic_string()+'/') + project_path=directories.cmake->project_path; else { project_path=file_path.parent_path(); CMake cmake(project_path); if(cmake.project_path!="") { project_path=cmake.project_path; - Singleton::terminal->print("Project path for "+file_path.string()+" set to "+project_path.string()+"\n"); + Terminal::get().print("Project path for "+file_path.string()+" set to "+project_path.string()+"\n"); } } if(language && (language->get_id()=="chdr" || language->get_id()=="cpphdr" || language->get_id()=="c" || language->get_id()=="cpp" || language->get_id()=="objc")) { @@ -119,13 +120,13 @@ void Notebook::open(const boost::filesystem::path &file_path) { else source_views.emplace_back(new Source::GenericView(file_path, project_path, language)); - source_views.back()->on_update_status=[this](Source::View* view, const std::string &status) { + source_views.back()->on_update_status=[this](Source::View* view, const std::string &status_text) { if(get_current_page()!=-1 && get_current_view()==view) - Singleton::status->set_text(status+" "); + status.set_text(status_text+" "); }; - source_views.back()->on_update_info=[this](Source::View* view, const std::string &info) { + source_views.back()->on_update_info=[this](Source::View* view, const std::string &info_text) { if(get_current_page()!=-1 && get_current_view()==view) - Singleton::info->set_text(" "+info); + info.set_text(" "+info_text); }; scrolled_windows.emplace_back(new Gtk::ScrolledWindow()); @@ -186,10 +187,10 @@ void Notebook::open(const boost::filesystem::path &file_path) { void Notebook::configure(int view_nr) { #if GTKSOURCEVIEWMM_MAJOR_VERSION > 2 & GTKSOURCEVIEWMM_MINOR_VERSION > 17 - auto source_font_description=Pango::FontDescription(Singleton::config->source.font); - auto source_map_font_desc=Pango::FontDescription(static_cast(source_font_description.get_family())+" "+Singleton::config->source.map_font_size); + auto source_font_description=Pango::FontDescription(Config::get().source.font); + auto source_map_font_desc=Pango::FontDescription(static_cast(source_font_description.get_family())+" "+Config::get().source.map_font_size); source_maps.at(view_nr)->override_font(source_map_font_desc); - if(Singleton::config->source.show_map) { + if(Config::get().source.show_map) { if(hboxes.at(view_nr)->get_children().size()==1) hboxes.at(view_nr)->pack_end(*source_maps.at(view_nr), Gtk::PACK_SHRINK); } @@ -207,7 +208,7 @@ bool Notebook::save(int page) { auto view=get_view(page); if (view->file_path != "" && view->get_buffer()->get_modified()) { //Remove trailing whitespace characters on save, and add trailing newline if missing - if(Singleton::config->source.cleanup_whitespace_characters) { + if(Config::get().source.cleanup_whitespace_characters) { auto buffer=view->get_buffer(); buffer->begin_user_action(); for(int line=0;lineget_line_count();line++) { @@ -249,9 +250,9 @@ bool Notebook::save(int page) { //If CMakeLists.txt have been modified: boost::filesystem::path project_path; if(view->file_path.filename()=="CMakeLists.txt") { - auto &directories=Singleton::directories; - if(directories->cmake && directories->cmake->project_path!="" && view->file_path.generic_string().substr(0, directories->cmake->project_path.generic_string().size()+1)==directories->cmake->project_path.generic_string()+'/' && CMake::create_compile_commands(directories->cmake->project_path)) { - project_path=directories->cmake->project_path; + auto &directories=Directories::get(); + if(directories.cmake && directories.cmake->project_path!="" && view->file_path.generic_string().substr(0, directories.cmake->project_path.generic_string().size()+1)==directories.cmake->project_path.generic_string()+'/' && CMake::create_compile_commands(directories.cmake->project_path)) { + project_path=directories.cmake->project_path; } else { CMake cmake(view->file_path.parent_path()); @@ -271,7 +272,7 @@ bool Notebook::save(int page) { JDEBUG("end true"); return true; } - Singleton::terminal->print("Error: could not save file " +view->file_path.string()+"\n", true); + Terminal::get().print("Error: could not save file " +view->file_path.string()+"\n", true); } JDEBUG("end false"); return false; @@ -335,7 +336,7 @@ boost::filesystem::path Notebook::get_current_folder() { if(get_current_page()!=-1) current_path=get_current_view()->project_path; else - current_path=Singleton::directories->current_path; + current_path=Directories::get().current_path; return current_path; } diff --git a/src/notebook.h b/src/notebook.h index 870c7cf..086d862 100644 --- a/src/notebook.h +++ b/src/notebook.h @@ -29,7 +29,9 @@ public: bool save_current(); void configure(int view_nr); boost::filesystem::path get_current_folder(); - + + Gtk::Label info; + Gtk::Label status; private: bool save_modified_dialog(int page); std::vector source_views; //Is NOT freed in destructor, this is intended for quick program exit. diff --git a/src/singletons.cc b/src/singletons.cc deleted file mode 100644 index 0abaaf2..0000000 --- a/src/singletons.cc +++ /dev/null @@ -1,20 +0,0 @@ -#include "singletons.h" - -#include //TODO: remove -using namespace std; //TODO: remove - -std::unique_ptr Singleton::config; -std::unique_ptr Singleton::menu; -std::unique_ptr Singleton::terminal; -std::unique_ptr Singleton::directories; -std::unique_ptr Singleton::info; -std::unique_ptr Singleton::status; - -void Singleton::init() { - config=std::unique_ptr(new Config()); - menu=std::unique_ptr(new Menu()); - terminal=std::unique_ptr(new Terminal()); - directories=std::unique_ptr(new Directories()); - info=std::unique_ptr(new Gtk::Label()); - status=std::unique_ptr(new Gtk::Label()); -} diff --git a/src/singletons.h b/src/singletons.h deleted file mode 100644 index 4ec1a36..0000000 --- a/src/singletons.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef JUCI_SINGLETONS_H_ -#define JUCI_SINGLETONS_H_ - -#include -#include "directories.h" -#include "terminal.h" -#include "menu.h" -#include "config.h" - -class Singleton { -public: - static std::unique_ptr config; - static std::unique_ptr menu; - static std::unique_ptr terminal; - static std::unique_ptr directories; - static std::unique_ptr info; - static std::unique_ptr status; - - static void init(); -}; - -#endif // JUCI_SINGLETONS_H_ diff --git a/src/source.cc b/src/source.cc index 983ae28..9987a15 100644 --- a/src/source.cc +++ b/src/source.cc @@ -1,11 +1,12 @@ -#include "singletons.h" #include "source.h" +#include "config.h" #include #include "logging.h" #include #include #include #include "filesystem.h" +#include "terminal.h" using namespace std; //TODO: remove @@ -87,11 +88,11 @@ Source::View::View(const boost::filesystem::path &file_path, const boost::filesy get_source_buffer()->begin_not_undoable_action(); if(language) { if(filesystem::read_non_utf8(file_path, get_buffer())==-1) - Singleton::terminal->print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n"); + Terminal::get().print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n"); } else { if(filesystem::read(file_path, get_buffer())==-1) - Singleton::terminal->print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n", true); + Terminal::get().print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n", true); } get_source_buffer()->end_not_undoable_action(); @@ -262,9 +263,9 @@ Source::View::View(const boost::filesystem::path &file_path, const boost::filesy set_tooltip_events(); - tab_char=Singleton::config->source.default_tab_char; - tab_size=Singleton::config->source.default_tab_size; - if(Singleton::config->source.auto_tab_char_and_size) { + tab_char=Config::get().source.default_tab_char; + tab_size=Config::get().source.default_tab_size; + if(Config::get().source.auto_tab_char_and_size) { auto tab_char_and_size=find_tab_char_and_size(); if(tab_char_and_size.first!=0) { if(tab_char!=tab_char_and_size.first || tab_size!=tab_char_and_size.second) { @@ -273,7 +274,7 @@ Source::View::View(const boost::filesystem::path &file_path, const boost::filesy tab_str=""; else tab_str=""; - Singleton::terminal->print("Tab char and size for file "+file_path.string()+" set to: "+tab_str+", "+std::to_string(tab_char_and_size.second)+".\n"); + Terminal::get().print("Tab char and size for file "+file_path.string()+" set to: "+tab_str+", "+std::to_string(tab_char_and_size.second)+".\n"); } tab_char=tab_char_and_size.first; @@ -295,25 +296,25 @@ void Source::View::set_tab_char_and_size(char tab_char, unsigned tab_size) { void Source::View::configure() { //TODO: Move this to notebook? Might take up too much memory doing this for every tab. auto style_scheme_manager=Gsv::StyleSchemeManager::get_default(); - style_scheme_manager->prepend_search_path((Singleton::config->juci_home_path()/"styles").string()); + style_scheme_manager->prepend_search_path((Config::get().juci_home_path()/"styles").string()); - if(Singleton::config->source.style.size()>0) { - auto scheme = style_scheme_manager->get_scheme(Singleton::config->source.style); + if(Config::get().source.style.size()>0) { + auto scheme = style_scheme_manager->get_scheme(Config::get().source.style); if(scheme) get_source_buffer()->set_style_scheme(scheme); else - Singleton::terminal->print("Error: Could not find gtksourceview style: "+Singleton::config->source.style+'\n', true); + Terminal::get().print("Error: Could not find gtksourceview style: "+Config::get().source.style+'\n', true); } - if(Singleton::config->source.wrap_lines) + if(Config::get().source.wrap_lines) set_wrap_mode(Gtk::WrapMode::WRAP_CHAR); else set_wrap_mode(Gtk::WrapMode::WRAP_NONE); - property_highlight_current_line() = Singleton::config->source.highlight_current_line; - property_show_line_numbers() = Singleton::config->source.show_line_numbers; - if(Singleton::config->source.font.size()>0) - override_font(Pango::FontDescription(Singleton::config->source.font)); + property_highlight_current_line() = Config::get().source.highlight_current_line; + property_show_line_numbers() = Config::get().source.show_line_numbers; + if(Config::get().source.font.size()>0) + override_font(Pango::FontDescription(Config::get().source.font)); #if GTKSOURCEVIEWMM_MAJOR_VERSION > 2 & GTKSOURCEVIEWMM_MINOR_VERSION > 15 gtk_source_view_set_background_pattern(this->gobj(), GTK_SOURCE_BACKGROUND_PATTERN_TYPE_GRID); #endif @@ -370,8 +371,8 @@ void Source::View::configure() { note_tag->property_foreground()=style->property_foreground(); } - if(Singleton::config->source.spellcheck_language.size()>0) { - aspell_config_replace(spellcheck_config, "lang", Singleton::config->source.spellcheck_language.c_str()); + if(Config::get().source.spellcheck_language.size()>0) { + aspell_config_replace(spellcheck_config, "lang", Config::get().source.spellcheck_language.c_str()); aspell_config_replace(spellcheck_config, "encoding", "utf-8"); } spellcheck_possible_err=new_aspell_speller(spellcheck_config); @@ -1289,7 +1290,7 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const if(language) { get_source_buffer()->set_language(language); - Singleton::terminal->print("Language for file "+file_path.string()+" set to "+language->get_name()+".\n"); + Terminal::get().print("Language for file "+file_path.string()+" set to "+language->get_name()+".\n"); } auto completion=get_completion(); @@ -1321,7 +1322,7 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, const boost::property_tree::xml_parser::read_xml(language_file.string(), pt); } catch(const std::exception &e) { - Singleton::terminal->print("Error: error parsing language file "+language_file.string()+": "+e.what()+'\n', true); + Terminal::get().print("Error: error parsing language file "+language_file.string()+": "+e.what()+'\n', true); } bool has_context_class=false; parse_language_file(completion_buffer_keywords, has_context_class, pt); diff --git a/src/source.h b/src/source.h index 4be6e15..e11a57c 100644 --- a/src/source.h +++ b/src/source.h @@ -88,8 +88,8 @@ namespace Source { std::unique_ptr selection_dialog; sigc::connection delayed_tooltips_connection; - std::function on_update_status; - std::function on_update_info; + std::function on_update_status; + std::function on_update_info; void set_status(const std::string &status); void set_info(const std::string &info); std::string status; diff --git a/src/source_clang.cc b/src/source_clang.cc index 22a8347..1303bef 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -1,5 +1,6 @@ #include "source_clang.h" -#include "singletons.h" +#include "config.h" +#include "terminal.h" #include //TODO: remove using namespace std; //TODO: remove @@ -25,14 +26,14 @@ Source::View(file_path, project_path, language) { JDEBUG("start"); auto tag_table=get_buffer()->get_tag_table(); - for (auto &item : Singleton::config->source.clang_types) { + for (auto &item : Config::get().source.clang_types) { if(!tag_table->lookup(item.second)) { get_buffer()->create_tag(item.second); } } configure(); - parsing_in_progress=Singleton::terminal->print_in_progress("Parsing "+file_path.string()); + parsing_in_progress=Terminal::get().print_in_progress("Parsing "+file_path.string()); //GTK-calls must happen in main thread, so the parse_thread //sends signals to the main thread that it is to call the following functions: parse_preprocess_connection=parse_preprocess.connect([this]{ @@ -58,7 +59,7 @@ Source::View(file_path, project_path, language) { } }); parse_error_connection=parse_error.connect([this](){ - Singleton::terminal->print("Error: failed to reparse "+this->file_path.string()+".\n", true); + Terminal::get().print("Error: failed to reparse "+this->file_path.string()+".\n", true); set_status(""); set_info(""); parsing_in_progress->cancel("failed"); @@ -79,7 +80,7 @@ void Source::ClangViewParse::configure() { auto scheme = get_source_buffer()->get_style_scheme(); auto tag_table=get_buffer()->get_tag_table(); - for (auto &item : Singleton::config->source.clang_types) { + for (auto &item : Config::get().source.clang_types) { auto tag = get_buffer()->get_tag_table()->lookup(item.second); if(tag) { auto style = scheme->get_style(item.second); @@ -237,8 +238,8 @@ void Source::ClangViewParse::update_syntax() { buffer->remove_tag_by_name(tag, buffer->begin(), buffer->end()); last_syntax_tags.clear(); for (auto &range : ranges) { - auto type_it=Singleton::config->source.clang_types.find(std::to_string(range.kind)); - if(type_it!=Singleton::config->source.clang_types.end()) { + auto type_it=Config::get().source.clang_types.find(std::to_string(range.kind)); + if(type_it!=Config::get().source.clang_types.end()) { last_syntax_tags.emplace(type_it->second); Gtk::TextIter begin_iter = buffer->get_iter_at_line_index(range.offsets.first.line-1, range.offsets.first.index-1); Gtk::TextIter end_iter = buffer->get_iter_at_line_index(range.offsets.second.line-1, range.offsets.second.index-1); @@ -711,7 +712,7 @@ Source::ClangViewParse(file_path, project_path, language), autocomplete_state(Au autocomplete_check(); }); autocomplete_error_connection=autocomplete_error.connect([this]() { - Singleton::terminal->print("Error: autocomplete failed, reparsing "+this->file_path.string()+"\n", true); + Terminal::get().print("Error: autocomplete failed, reparsing "+this->file_path.string()+"\n", true); autocomplete_state=AutocompleteState::CANCELED; full_reparse(); }); @@ -968,7 +969,7 @@ Source::ClangViewAutocomplete(file_path, project_path, language) { }); auto_indent=[this]() { - auto command=Singleton::config->terminal.clang_format_command; + auto command=Config::get().terminal.clang_format_command; unsigned indent_width; std::string tab_style; if(tab_char=='\t') { @@ -982,13 +983,13 @@ Source::ClangViewAutocomplete(file_path, project_path, language) { command+=" -style=\"{IndentWidth: "+std::to_string(indent_width); command+=", "+tab_style; command+=", "+std::string("AccessModifierOffset: -")+std::to_string(indent_width); - if(Singleton::config->source.clang_format_style!="") - command+=", "+Singleton::config->source.clang_format_style; + if(Config::get().source.clang_format_style!="") + command+=", "+Config::get().source.clang_format_style; command+="}\""; std::stringstream stdin_stream(get_buffer()->get_text()), stdout_stream; - auto exit_status=Singleton::terminal->process(stdin_stream, stdout_stream, command); + auto exit_status=Terminal::get().process(stdin_stream, stdout_stream, command); if(exit_status==0) { get_source_buffer()->begin_user_action(); auto iter=get_buffer()->get_insert()->get_iter(); @@ -1224,7 +1225,7 @@ Source::ClangViewAutocomplete(file_path, project_path, language) { auto usr=referenced.get_usr(); boost::filesystem::path referenced_path=referenced.get_source_location().get_path(); - //Singleton::terminal->print(usr+'\n', true); //TODO: remove + //Terminal::get().print(usr+'\n', true); //TODO: remove //Return empty if referenced is within project if(referenced_path.generic_string().substr(0, this->project_path.generic_string().size()+1)==this->project_path.generic_string()+'/') diff --git a/src/terminal.cc b/src/terminal.cc index 278b60e..1544bf8 100644 --- a/src/terminal.cc +++ b/src/terminal.cc @@ -1,14 +1,14 @@ #include "terminal.h" #include #include "logging.h" -#include "singletons.h" +#include "config.h" #include //TODO: remove using namespace std; //TODO: remove Terminal::InProgress::InProgress(const std::string& start_msg): stop(false) { waiting_print.connect([this](){ - Singleton::terminal->async_print(line_nr-1, "."); + Terminal::get().async_print(line_nr-1, "."); }); start(start_msg); } @@ -20,7 +20,7 @@ Terminal::InProgress::~InProgress() { } void Terminal::InProgress::start(const std::string& msg) { - line_nr=Singleton::terminal->print(msg+"...\n"); + line_nr=Terminal::get().print(msg+"...\n"); wait_thread=std::thread([this](){ size_t c=0; while(!stop) { @@ -35,14 +35,14 @@ void Terminal::InProgress::start(const std::string& msg) { void Terminal::InProgress::done(const std::string& msg) { if(!stop) { stop=true; - Singleton::terminal->async_print(line_nr-1, msg); + Terminal::get().async_print(line_nr-1, msg); } } void Terminal::InProgress::cancel(const std::string& msg) { if(!stop) { stop=true; - Singleton::terminal->async_print(line_nr-1, msg); + Terminal::get().async_print(line_nr-1, msg); } } @@ -191,8 +191,8 @@ size_t Terminal::print(const std::string &message, bool bold){ else get_buffer()->insert(get_buffer()->end(), umessage); - if(get_buffer()->get_line_count()>Singleton::config->terminal.history_size) { - int lines=get_buffer()->get_line_count()-Singleton::config->terminal.history_size; + if(get_buffer()->get_line_count()>Config::get().terminal.history_size) { + int lines=get_buffer()->get_line_count()-Config::get().terminal.history_size; get_buffer()->erase(get_buffer()->begin(), get_buffer()->get_iter_at_line(lines)); deleted_lines+=static_cast(lines); } diff --git a/src/terminal.h b/src/terminal.h index 9fe5037..5137510 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -26,7 +26,14 @@ public: std::thread wait_thread; }; +private: Terminal(); +public: + static Terminal &get() { + static Terminal singleton; + return singleton; + } + int process(const std::string &command, const boost::filesystem::path &path="", bool use_pipes=true); int process(std::istream &stdin_stream, std::ostream &stdout_stream, const std::string &command, const boost::filesystem::path &path=""); void async_process(const std::string &command, const boost::filesystem::path &path="", std::function callback=nullptr); diff --git a/src/window.cc b/src/window.cc index 3550671..2315aa7 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1,6 +1,8 @@ #include "window.h" #include "logging.h" -#include "singletons.h" +#include "config.h" +#include "menu.h" +#include "directories.h" //#include "api.h" #include "dialogs.h" #include "filesystem.h" @@ -28,40 +30,40 @@ Window::Window() : compiling(false) { set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK); set_menu_actions(); configure(); - set_default_size(Singleton::config->window.default_size.first, Singleton::config->window.default_size.second); + set_default_size(Config::get().window.default_size.first, Config::get().window.default_size.second); //PluginApi(&this->notebook, &this->menu); add(vpaned); - directories_scrolled_window.add(*Singleton::directories); + directories_scrolled_window.add(Directories::get()); directory_and_notebook_panes.pack1(directories_scrolled_window, Gtk::SHRINK); notebook_vbox.pack_start(notebook); notebook_vbox.pack_end(entry_box, Gtk::PACK_SHRINK); directory_and_notebook_panes.pack2(notebook_vbox, Gtk::SHRINK); - directory_and_notebook_panes.set_position(static_cast(0.2*Singleton::config->window.default_size.first)); - vpaned.set_position(static_cast(0.75*Singleton::config->window.default_size.second)); + directory_and_notebook_panes.set_position(static_cast(0.2*Config::get().window.default_size.first)); + vpaned.set_position(static_cast(0.75*Config::get().window.default_size.second)); vpaned.pack1(directory_and_notebook_panes, true, false); - terminal_scrolled_window.add(*Singleton::terminal); + terminal_scrolled_window.add(Terminal::get()); terminal_vbox.pack_start(terminal_scrolled_window); - info_and_status_hbox.pack_start(*Singleton::info, Gtk::PACK_SHRINK); - info_and_status_hbox.pack_end(*Singleton::status, Gtk::PACK_SHRINK); + info_and_status_hbox.pack_start(notebook.info, Gtk::PACK_SHRINK); + info_and_status_hbox.pack_end(notebook.status, Gtk::PACK_SHRINK); terminal_vbox.pack_end(info_and_status_hbox, Gtk::PACK_SHRINK); vpaned.pack2(terminal_vbox, true, true); show_all_children(); - Singleton::directories->on_row_activated=[this](const std::string &file) { + Directories::get().on_row_activated=[this](const std::string &file) { notebook.open(file); }; //Scroll to end of terminal whenever info is printed - Singleton::terminal->signal_size_allocate().connect([this](Gtk::Allocation& allocation){ + Terminal::get().signal_size_allocate().connect([this](Gtk::Allocation& allocation){ auto adjustment=terminal_scrolled_window.get_vadjustment(); adjustment->set_value(adjustment->get_upper()-adjustment->get_page_size()); - Singleton::terminal->queue_draw(); + Terminal::get().queue_draw(); }); entry_box.signal_show().connect([this](){ @@ -92,11 +94,11 @@ Window::Window() : compiling(false) { activate_menu_items(); - Singleton::directories->select(view->file_path); + Directories::get().select(view->file_path); if(view->full_reparse_needed) { if(!view->full_reparse()) - Singleton::terminal->async_print("Error: failed to reparse "+view->file_path.string()+". Please reopen the file manually.\n", true); + Terminal::get().async_print("Error: failed to reparse "+view->file_path.string()+". Please reopen the file manually.\n", true); } else if(view->soft_reparse_needed) view->soft_reparse(); @@ -113,7 +115,7 @@ Window::Window() : compiling(false) { about.hide(); }); - about.set_version(Singleton::config->window.version); + about.set_version(Config::get().window.version); about.set_authors({"(in order of appearance)", "Ted Johan Kristoffersen", "Jørgen Lien Sellæg", @@ -128,65 +130,65 @@ Window::Window() : compiling(false) { } // Window constructor void Window::configure() { - Singleton::config->load(); + Config::get().load(); auto style_context = Gtk::StyleContext::create(); auto screen = Gdk::Screen::get_default(); - auto css_provider = Gtk::CssProvider::get_named(Singleton::config->window.theme_name, Singleton::config->window.theme_variant); - //TODO: add check if theme exists, or else write error to Singleton::terminal + auto css_provider = Gtk::CssProvider::get_named(Config::get().window.theme_name, Config::get().window.theme_variant); + //TODO: add check if theme exists, or else write error to terminal style_context->add_provider_for_screen(screen, css_provider, GTK_STYLE_PROVIDER_PRIORITY_SETTINGS); - Singleton::directories->update(); - Singleton::menu->set_keys(); + Directories::get().update(); + Menu::get().set_keys(); } void Window::set_menu_actions() { - auto &menu = Singleton::menu; + auto &menu = Menu::get(); - menu->add_action("about", [this]() { + menu.add_action("about", [this]() { about.show(); about.present(); }); - menu->add_action("preferences", [this]() { - notebook.open(Singleton::config->juci_home_path()/"config"/"config.json"); + menu.add_action("preferences", [this]() { + notebook.open(Config::get().juci_home_path()/"config"/"config.json"); }); - menu->add_action("quit", [this]() { + menu.add_action("quit", [this]() { close(); }); - menu->add_action("new_file", [this]() { + menu.add_action("new_file", [this]() { boost::filesystem::path path = Dialog::new_file(); if(path!="") { if(boost::filesystem::exists(path)) { - Singleton::terminal->print("Error: "+path.string()+" already exists.\n", true); + Terminal::get().print("Error: "+path.string()+" already exists.\n", true); } else { if(filesystem::write(path)) { - if(Singleton::directories->current_path!="") - Singleton::directories->update(); + if(Directories::get().current_path!="") + Directories::get().update(); notebook.open(path.string()); - Singleton::terminal->print("New file "+path.string()+" created.\n"); + Terminal::get().print("New file "+path.string()+" created.\n"); } else - Singleton::terminal->print("Error: could not create new file "+path.string()+".\n", true); + Terminal::get().print("Error: could not create new file "+path.string()+".\n", true); } } }); - menu->add_action("new_folder", [this]() { + menu.add_action("new_folder", [this]() { auto time_now=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); boost::filesystem::path path = Dialog::new_folder(); if(path!="" && boost::filesystem::exists(path)) { boost::system::error_code ec; auto last_write_time=boost::filesystem::last_write_time(path, ec); if(!ec && last_write_time>=time_now) { - if(Singleton::directories->current_path!="") - Singleton::directories->update(); - Singleton::terminal->print("New folder "+path.string()+" created.\n"); + if(Directories::get().current_path!="") + Directories::get().update(); + Terminal::get().print("New folder "+path.string()+" created.\n"); } else - Singleton::terminal->print("Error: "+path.string()+" already exists.\n", true); - Singleton::directories->select(path); + Terminal::get().print("Error: "+path.string()+" already exists.\n", true); + Directories::get().select(path); } }); - menu->add_action("new_project_cpp", [this]() { + menu.add_action("new_project_cpp", [this]() { boost::filesystem::path project_path = Dialog::new_folder(); if(project_path!="") { auto project_name=project_path.filename().string(); @@ -199,41 +201,41 @@ void Window::set_menu_actions() { auto cpp_main_path=project_path; cpp_main_path+="/main.cpp"; if(boost::filesystem::exists(cmakelists_path)) { - Singleton::terminal->print("Error: "+cmakelists_path.string()+" already exists.\n", true); + Terminal::get().print("Error: "+cmakelists_path.string()+" already exists.\n", true); return; } if(boost::filesystem::exists(cpp_main_path)) { - Singleton::terminal->print("Error: "+cpp_main_path.string()+" already exists.\n", true); + Terminal::get().print("Error: "+cpp_main_path.string()+" already exists.\n", true); return; } std::string cmakelists="cmake_minimum_required(VERSION 2.8)\n\nproject("+project_name+")\n\nset(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -std=c++1y -Wall\")\n\nadd_executable("+project_name+" main.cpp)\n"; std::string cpp_main="#include \n\nusing namespace std;\n\nint main() {\n cout << \"Hello World!\" << endl;\n\n return 0;\n}\n"; if(filesystem::write(cmakelists_path, cmakelists) && filesystem::write(cpp_main_path, cpp_main)) { - Singleton::directories->open(project_path); + Directories::get().open(project_path); notebook.open(cpp_main_path); - Singleton::terminal->print("C++ project "+project_name+" created.\n"); + Terminal::get().print("C++ project "+project_name+" created.\n"); } else - Singleton::terminal->print("Error: Could not create project "+project_path.string()+"\n", true); + Terminal::get().print("Error: Could not create project "+project_path.string()+"\n", true); } }); - menu->add_action("open_file", [this]() { + menu.add_action("open_file", [this]() { auto path=Dialog::open_file(); if(path!="") notebook.open(path); }); - menu->add_action("open_folder", [this]() { + menu.add_action("open_folder", [this]() { auto path = Dialog::open_folder(); if (path!="" && boost::filesystem::exists(path)) - Singleton::directories->open(path); + Directories::get().open(path); }); - menu->add_action("save", [this]() { + menu.add_action("save", [this]() { if(notebook.get_current_page()!=-1) { if(notebook.save_current()) { if(notebook.get_current_page()!=-1) { - if(notebook.get_current_view()->file_path==Singleton::config->juci_home_path()/"config"/"config.json") { + if(notebook.get_current_view()->file_path==Config::get().juci_home_path()/"config"/"config.json") { configure(); for(int c=0;cconfigure(); @@ -244,7 +246,7 @@ void Window::set_menu_actions() { } } }); - menu->add_action("save_as", [this]() { + menu.add_action("save_as", [this]() { if(notebook.get_current_page()!=-1) { auto path = Dialog::save_file_as(notebook.get_current_view()->file_path); if(path!="") { @@ -252,18 +254,18 @@ void Window::set_menu_actions() { if(file) { file << notebook.get_current_view()->get_buffer()->get_text(); file.close(); - if(Singleton::directories->current_path!="") - Singleton::directories->update(); + if(Directories::get().current_path!="") + Directories::get().update(); notebook.open(path); - Singleton::terminal->print("File saved to: " + notebook.get_current_view()->file_path.string()+"\n"); + Terminal::get().print("File saved to: " + notebook.get_current_view()->file_path.string()+"\n"); } else - Singleton::terminal->print("Error saving file\n"); + Terminal::get().print("Error saving file\n"); } } }); - menu->add_action("edit_undo", [this]() { + menu.add_action("edit_undo", [this]() { if(notebook.get_current_page()!=-1) { auto undo_manager = notebook.get_current_view()->get_source_buffer()->get_undo_manager(); if (undo_manager->can_undo()) { @@ -272,7 +274,7 @@ void Window::set_menu_actions() { } } }); - menu->add_action("edit_redo", [this]() { + menu.add_action("edit_redo", [this]() { if(notebook.get_current_page()!=-1) { auto undo_manager = notebook.get_current_view()->get_source_buffer()->get_undo_manager(); if(undo_manager->can_redo()) { @@ -282,21 +284,21 @@ void Window::set_menu_actions() { } }); - menu->add_action("edit_cut", [this]() { + menu.add_action("edit_cut", [this]() { auto widget=get_focus(); if(auto entry=dynamic_cast(widget)) entry->cut_clipboard(); else if(notebook.get_current_page()!=-1) notebook.get_current_view()->get_buffer()->cut_clipboard(Gtk::Clipboard::get()); }); - menu->add_action("edit_copy", [this]() { + menu.add_action("edit_copy", [this]() { auto widget=get_focus(); if(auto entry=dynamic_cast(widget)) entry->copy_clipboard(); else if(auto text_view=dynamic_cast(widget)) text_view->get_buffer()->copy_clipboard(Gtk::Clipboard::get()); }); - menu->add_action("edit_paste", [this]() { + menu.add_action("edit_paste", [this]() { auto widget=get_focus(); if(auto entry=dynamic_cast(widget)) entry->paste_clipboard(); @@ -304,35 +306,35 @@ void Window::set_menu_actions() { notebook.get_current_view()->paste(); }); - menu->add_action("edit_find", [this]() { + menu.add_action("edit_find", [this]() { search_and_replace_entry(); }); - menu->add_action("source_spellcheck", [this]() { + menu.add_action("source_spellcheck", [this]() { if(notebook.get_current_page()!=-1) notebook.get_current_view()->spellcheck(); }); - menu->add_action("source_spellcheck_clear", [this]() { + menu.add_action("source_spellcheck_clear", [this]() { if(notebook.get_current_page()!=-1) notebook.get_current_view()->remove_spellcheck_errors(); }); - menu->add_action("source_spellcheck_next_error", [this]() { + menu.add_action("source_spellcheck_next_error", [this]() { if(notebook.get_current_page()!=-1) notebook.get_current_view()->goto_next_spellcheck_error(); }); - menu->add_action("source_indentation_set_buffer_tab", [this]() { + menu.add_action("source_indentation_set_buffer_tab", [this]() { set_tab_entry(); }); - menu->add_action("source_indentation_auto_indent_buffer", [this]() { + menu.add_action("source_indentation_auto_indent_buffer", [this]() { if(notebook.get_current_page()!=-1 && notebook.get_current_view()->auto_indent) notebook.get_current_view()->auto_indent(); }); - menu->add_action("source_goto_line", [this]() { + menu.add_action("source_goto_line", [this]() { goto_line_entry(); }); - menu->add_action("source_center_cursor", [this]() { + menu.add_action("source_center_cursor", [this]() { if(notebook.get_current_page()!=-1) { auto view=notebook.get_current_view(); @@ -343,13 +345,13 @@ void Window::set_menu_actions() { } }); - menu->add_action("source_find_documentation", [this]() { + menu.add_action("source_find_documentation", [this]() { if(notebook.get_current_page()!=-1) { if(notebook.get_current_view()->get_token_data) { auto data=notebook.get_current_view()->get_token_data(); if(data.size()>0) { - auto documentation_search=Singleton::config->source.documentation_searches.find(data[0]); - if(documentation_search!=Singleton::config->source.documentation_searches.end()) { + auto documentation_search=Config::get().source.documentation_searches.find(data[0]); + if(documentation_search!=Config::get().source.documentation_searches.end()) { std::string token_query; for(size_t c=1;c0) { @@ -370,7 +372,7 @@ void Window::set_menu_actions() { if(query!=documentation_search->second.queries.end()) { std::string uri=query->second+token_query; #ifdef __APPLE__ - Singleton::terminal->process("open \""+uri+"\""); + Terminal::get().process("open \""+uri+"\""); #else GError* error=NULL; gtk_show_uri(NULL, uri.c_str(), GDK_CURRENT_TIME, &error); @@ -384,7 +386,7 @@ void Window::set_menu_actions() { } }); - menu->add_action("source_goto_declaration", [this]() { + menu.add_action("source_goto_declaration", [this]() { if(notebook.get_current_page()!=-1) { if(notebook.get_current_view()->get_declaration_location) { auto location=notebook.get_current_view()->get_declaration_location(); @@ -417,7 +419,7 @@ void Window::set_menu_actions() { } } }); - menu->add_action("source_goto_usage", [this]() { + menu.add_action("source_goto_usage", [this]() { if(notebook.get_current_page()!=-1) { auto current_view=notebook.get_current_view(); if(current_view->get_token && current_view->get_usages) { @@ -479,25 +481,25 @@ void Window::set_menu_actions() { } } }); - menu->add_action("source_goto_method", [this]() { + menu.add_action("source_goto_method", [this]() { if(notebook.get_current_page()!=-1) { if(notebook.get_current_view()->goto_method) { notebook.get_current_view()->goto_method(); } } }); - menu->add_action("source_rename", [this]() { + menu.add_action("source_rename", [this]() { rename_token_entry(); }); - menu->add_action("source_goto_next_diagnostic", [this]() { + menu.add_action("source_goto_next_diagnostic", [this]() { if(notebook.get_current_page()!=-1) { if(notebook.get_current_view()->goto_next_diagnostic) { notebook.get_current_view()->goto_next_diagnostic(); } } }); - menu->add_action("source_apply_fix_its", [this]() { + menu.add_action("source_apply_fix_its", [this]() { if(notebook.get_current_page()!=-1) { if(notebook.get_current_view()->apply_fix_its) { notebook.get_current_view()->apply_fix_its(); @@ -505,14 +507,14 @@ void Window::set_menu_actions() { } }); - menu->add_action("compile_and_run", [this]() { + menu.add_action("compile_and_run", [this]() { if(compiling) return; boost::filesystem::path cmake_path; if(notebook.get_current_page()!=-1) cmake_path=notebook.get_current_view()->file_path.parent_path(); else - cmake_path=Singleton::directories->current_path; + cmake_path=Directories::get().current_path; if(cmake_path.empty()) return; CMake cmake(cmake_path); @@ -525,9 +527,9 @@ void Window::set_menu_actions() { if(cmake.project_path!="") { if(executable_path!="") { compiling=true; - Singleton::terminal->print("Compiling and running "+executable_path.string()+"\n"); + Terminal::get().print("Compiling and running "+executable_path.string()+"\n"); auto project_path=cmake.project_path; - Singleton::terminal->async_process(Singleton::config->terminal.make_command, cmake.project_path, [this, executable_path, project_path](int exit_status){ + Terminal::get().async_process(Config::get().terminal.make_command, cmake.project_path, [this, executable_path, project_path](int exit_status){ compiling=false; if(exit_status==EXIT_SUCCESS) { auto executable_path_spaces_fixed=executable_path.string(); @@ -539,40 +541,40 @@ void Window::set_menu_actions() { } last_char=executable_path_spaces_fixed[c]; } - Singleton::terminal->async_process(executable_path_spaces_fixed, project_path, [this, executable_path](int exit_status){ - Singleton::terminal->async_print(executable_path.string()+" returned: "+std::to_string(exit_status)+'\n'); + Terminal::get().async_process(executable_path_spaces_fixed, project_path, [this, executable_path](int exit_status){ + Terminal::get().async_print(executable_path.string()+" returned: "+std::to_string(exit_status)+'\n'); }); } }); } else { - Singleton::terminal->print("Could not find add_executable in the following paths:\n"); + Terminal::get().print("Could not find add_executable in the following paths:\n"); for(auto &path: cmake.paths) - Singleton::terminal->print(" "+path.string()+"\n"); + Terminal::get().print(" "+path.string()+"\n"); } } }); - menu->add_action("compile", [this]() { + menu.add_action("compile", [this]() { if(compiling) return; boost::filesystem::path cmake_path; if(notebook.get_current_page()!=-1) cmake_path=notebook.get_current_view()->file_path.parent_path(); else - cmake_path=Singleton::directories->current_path; + cmake_path=Directories::get().current_path; if(cmake_path.empty()) return; CMake cmake(cmake_path); if(cmake.project_path!="") { compiling=true; - Singleton::terminal->print("Compiling project "+cmake.project_path.string()+"\n"); - Singleton::terminal->async_process(Singleton::config->terminal.make_command, cmake.project_path, [this](int exit_status){ + Terminal::get().print("Compiling project "+cmake.project_path.string()+"\n"); + Terminal::get().async_process(Config::get().terminal.make_command, cmake.project_path, [this](int exit_status){ compiling=false; }); } }); - menu->add_action("run_command", [this]() { + menu.add_action("run_command", [this]() { entry_box.clear(); entry_box.labels.emplace_back(); auto label_it=entry_box.labels.begin(); @@ -584,10 +586,10 @@ void Window::set_menu_actions() { if(content!="") { last_run_command=content; auto run_path=notebook.get_current_folder(); - Singleton::terminal->async_print("Running: "+content+'\n'); + Terminal::get().async_print("Running: "+content+'\n'); - Singleton::terminal->async_process(content, run_path, [this, content](int exit_status){ - Singleton::terminal->async_print(content+" returned: "+std::to_string(exit_status)+'\n'); + Terminal::get().async_process(content, run_path, [this, content](int exit_status){ + Terminal::get().async_print(content+" returned: "+std::to_string(exit_status)+'\n'); }); } entry_box.hide(); @@ -600,19 +602,19 @@ void Window::set_menu_actions() { entry_box.show(); }); - menu->add_action("kill_last_running", [this]() { - Singleton::terminal->kill_last_async_process(); + menu.add_action("kill_last_running", [this]() { + Terminal::get().kill_last_async_process(); }); - menu->add_action("force_kill_last_running", [this]() { - Singleton::terminal->kill_last_async_process(true); + menu.add_action("force_kill_last_running", [this]() { + Terminal::get().kill_last_async_process(true); }); - menu->add_action("next_tab", [this]() { + menu.add_action("next_tab", [this]() { if(notebook.get_current_page()!=-1) { notebook.open(notebook.get_view((notebook.get_current_page()+1)%notebook.size())->file_path); } }); - menu->add_action("previous_tab", [this]() { + menu.add_action("previous_tab", [this]() { if(notebook.get_current_page()!=-1) { int previous_page=notebook.get_current_page()-1; if(previous_page<0) @@ -620,15 +622,15 @@ void Window::set_menu_actions() { notebook.open(notebook.get_view(previous_page)->file_path); } }); - menu->add_action("close_tab", [this]() { + menu.add_action("close_tab", [this]() { notebook.close_current_page(); if(notebook.get_current_page()!=-1) { notebook.get_current_view()->set_status(notebook.get_current_view()->status); notebook.get_current_view()->set_info(notebook.get_current_view()->info); } else { - Singleton::status->set_text(""); - Singleton::info->set_text(""); + notebook.status.set_text(""); + notebook.info.set_text(""); activate_menu_items(false); } @@ -637,15 +639,15 @@ void Window::set_menu_actions() { } void Window::activate_menu_items(bool activate) { - auto &menu = Singleton::menu; - menu->actions["source_indentation_auto_indent_buffer"]->set_enabled(activate ? static_cast(notebook.get_current_view()->auto_indent) : false); - menu->actions["source_find_documentation"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_token_data) : false); - menu->actions["source_goto_declaration"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_declaration_location) : false); - menu->actions["source_goto_usage"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_usages) : false); - menu->actions["source_goto_method"]->set_enabled(activate ? static_cast(notebook.get_current_view()->goto_method) : false); - menu->actions["source_rename"]->set_enabled(activate ? static_cast(notebook.get_current_view()->rename_similar_tokens) : false); - menu->actions["source_goto_next_diagnostic"]->set_enabled(activate ? static_cast(notebook.get_current_view()->goto_next_diagnostic) : false); - menu->actions["source_apply_fix_its"]->set_enabled(activate ? static_cast(notebook.get_current_view()->apply_fix_its) : false); + auto &menu = Menu::get(); + menu.actions["source_indentation_auto_indent_buffer"]->set_enabled(activate ? static_cast(notebook.get_current_view()->auto_indent) : false); + menu.actions["source_find_documentation"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_token_data) : false); + menu.actions["source_goto_declaration"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_declaration_location) : false); + menu.actions["source_goto_usage"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_usages) : false); + menu.actions["source_goto_method"]->set_enabled(activate ? static_cast(notebook.get_current_view()->goto_method) : false); + menu.actions["source_rename"]->set_enabled(activate ? static_cast(notebook.get_current_view()->rename_similar_tokens) : false); + menu.actions["source_goto_next_diagnostic"]->set_enabled(activate ? static_cast(notebook.get_current_view()->goto_next_diagnostic) : false); + menu.actions["source_apply_fix_its"]->set_enabled(activate ? static_cast(notebook.get_current_view()->apply_fix_its) : false); } bool Window::on_key_press_event(GdkEventKey *event) { @@ -688,7 +690,7 @@ bool Window::on_delete_event(GdkEventAny *event) { if(!notebook.close_current_page()) return true; } - Singleton::terminal->kill_async_processes(); + Terminal::get().kill_async_processes(); return false; } @@ -893,7 +895,7 @@ void Window::rename_token_entry() { if(view->rename_similar_tokens) { auto number=view->rename_similar_tokens(*token, content); if(number>0) { - Singleton::terminal->print("Replaced "+std::to_string(number)+" occurrences in file "+view->file_path.string()+"\n"); + Terminal::get().print("Replaced "+std::to_string(number)+" occurrences in file "+view->file_path.string()+"\n"); notebook.save(c); modified_pages.emplace_back(c); } diff --git a/src/window.h b/src/window.h index 7c301d6..2587348 100644 --- a/src/window.h +++ b/src/window.h @@ -7,8 +7,14 @@ #include class Window : public Gtk::ApplicationWindow { -public: +private: Window(); +public: + static Window &get() { + static Window singleton; + return singleton; + } + Notebook notebook; protected: