diff --git a/src/api.cc b/src/api.cc index fa93820..0cf4d8c 100644 --- a/src/api.cc +++ b/src/api.cc @@ -2,15 +2,15 @@ #include "logging.h" #include "singletons.h" -Menu* PluginApi::menu_=nullptr; +Menu* PluginApi::menu=nullptr; Notebook* PluginApi::notebook=nullptr; ///////////////////////////// //// API ServiceProvider //// ///////////////////////////// -PluginApi::PluginApi(Notebook* notebook) { +PluginApi::PluginApi(Notebook* notebook, Menu* menu) { DEBUG("Adding pointers for the API"); this->notebook = notebook; - menu_ = Singleton::menu(); + this->menu = menu; DEBUG("Initiating plugins(from plugins.py).."); #ifndef __APPLE__ InitPlugins(); //TODO: fix this @@ -58,7 +58,7 @@ void PluginApi::AddMenuElement(std::string plugin_name) { DEBUG("Adding menu element for "+plugin_name); AddMenuXml(plugin_name, "PluginMenu"); std::string plugin_action_name = plugin_name+"Menu"; - Singleton::menu()->action_group->add(Gtk::Action::create(plugin_action_name, plugin_name)); + menu->action_group->add(Gtk::Action::create(plugin_action_name, plugin_name)); } void PluginApi::AddSubMenuElement(std::string parent_menu, @@ -67,7 +67,7 @@ void PluginApi::AddSubMenuElement(std::string parent_menu, std::string plugin_path, std::string menu_keybinding) { AddSubMenuXml(menu_func_name, parent_menu); - Singleton::menu()->action_group->add(Gtk::Action::create(menu_func_name, + menu->action_group->add(Gtk::Action::create(menu_func_name, menu_name), Gtk::AccelKey(menu_keybinding), [=]() { @@ -76,7 +76,7 @@ void PluginApi::AddSubMenuElement(std::string parent_menu, } void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) { - std::string temp_menu = Singleton::menu()->ui; + std::string temp_menu = menu->ui; std::size_t plugin_menu_pos = temp_menu.find(parent_menu); // +2 gets you outside of the tag:<'menu_name'> plugin_menu_pos+=parent_menu.size() +2; @@ -86,12 +86,12 @@ void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) { " " " "; - Singleton::menu()->ui = menu_prefix + menu_input + menu_suffix; + menu->ui = menu_prefix + menu_input + menu_suffix; } void PluginApi::AddSubMenuXml(std::string plugin_name, std::string parent_menu) { - std::string temp_menu = Singleton::menu()->ui; + std::string temp_menu = menu->ui; std::size_t parent_menu_pos = temp_menu.find(parent_menu); // +2 gets you outside of the tag:<'menu_name'> @@ -100,7 +100,7 @@ void PluginApi::AddSubMenuXml(std::string plugin_name, std::string menu_suffix = temp_menu.substr(parent_menu_pos); std::string menu_input =""; - Singleton::menu()->ui = menu_prefix + menu_input + menu_suffix; + menu->ui = menu_prefix + menu_input + menu_suffix; } /////////////////////// diff --git a/src/api.h b/src/api.h index 4468631..a3aafca 100644 --- a/src/api.h +++ b/src/api.h @@ -12,8 +12,8 @@ //////////////////// class PluginApi { public: - PluginApi(Notebook* notebook); - static Menu* menu_; + PluginApi(Notebook* notebook, Menu* menu); + static Menu* menu; static Notebook* notebook; static void InitPlugins(); // for Python module: diff --git a/src/config.cc b/src/config.cc index a75b078..6957af4 100644 --- a/src/config.cc +++ b/src/config.cc @@ -5,7 +5,7 @@ #include "files.h" #include "sourcefile.h" -MainConfig::MainConfig() { +MainConfig::MainConfig(Menu &menu) : menu(menu) { find_or_create_config_files(); boost::property_tree::json_parser::read_json(Singleton::config_dir() + "config.json", cfg); GenerateSource(); @@ -71,17 +71,16 @@ void MainConfig::GenerateTerminalCommands() { } void MainConfig::GenerateKeybindings() { - auto menu = Singleton::menu(); boost::filesystem::path path(Singleton::config_dir() + "menu.xml"); if (!boost::filesystem::is_regular_file(path)) { std::cerr << "menu.xml not found" << std::endl; throw; } - menu->ui = juci::filesystem::open(path); + menu.ui = juci::filesystem::open(path); boost::property_tree::ptree keys_json = cfg.get_child("keybindings"); for (auto &i : keys_json) { auto key=i.second.get_value(); - menu->key_map[i.first] = key; + menu.key_map[i.first] = key; } DEBUG("Keybindings fetched"); } diff --git a/src/config.h b/src/config.h index 9f97a90..d1ea90b 100644 --- a/src/config.h +++ b/src/config.h @@ -2,10 +2,11 @@ #define JUCI_CONFIG_H_ #include #include +#include "menu.h" class MainConfig { public: - MainConfig(); + MainConfig(Menu &menu); void find_or_create_config_files(); void PrintMenu(); void GenerateSource(); @@ -15,5 +16,6 @@ public: private: boost::property_tree::ptree cfg; boost::property_tree::ptree key_tree; + Menu &menu; }; #endif diff --git a/src/menu.cc b/src/menu.cc index 7a040d3..ca59401 100644 --- a/src/menu.cc +++ b/src/menu.cc @@ -1,6 +1,5 @@ #include "menu.h" #include "logging.h" -#include Menu::Menu() : box(Gtk::ORIENTATION_VERTICAL) { INFO("Creating menu"); @@ -34,3 +33,4 @@ void Menu::build() { } ui_manager->insert_action_group(action_group); } + diff --git a/src/singletons.cc b/src/singletons.cc index 9af56e2..17f4b37 100644 --- a/src/singletons.cc +++ b/src/singletons.cc @@ -5,14 +5,8 @@ std::unique_ptr Singleton::Config::terminal_=std::unique_ptr Singleton::Config::directories_=std::unique_ptr(new Directories::Config()); std::unique_ptr Singleton::terminal_=std::unique_ptr(); -std::unique_ptr Singleton::menu_=std::unique_ptr(); Terminal *Singleton::terminal() { if(!terminal_) terminal_=std::unique_ptr(new Terminal()); return terminal_.get(); } -Menu *Singleton::menu() { - if(!menu_) - menu_=std::unique_ptr(new Menu()); - return menu_.get(); -} diff --git a/src/singletons.h b/src/singletons.h index 973eca8..2652d99 100644 --- a/src/singletons.h +++ b/src/singletons.h @@ -23,10 +23,8 @@ public: static std::string config_dir() { return std::string(getenv("HOME")) + "/.juci/config/"; } static std::string log_dir() { return std::string(getenv("HOME")) + "/.juci/log/"; } static Terminal *terminal(); - static Menu *menu(); private: static std::unique_ptr terminal_; - static std::unique_ptr menu_; }; #endif // JUCI_SINGLETONS_H_ diff --git a/src/window.cc b/src/window.cc index 4a8e583..180ac64 100644 --- a/src/window.cc +++ b/src/window.cc @@ -16,9 +16,10 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL) { set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK); add(box); - MainConfig(); //Read the configs here - PluginApi(&this->notebook); //Initialise plugins - add_menu(); + MainConfig(this->menu); //Read the configs here + PluginApi(&this->notebook, &this->menu); //Initialise plugins + create_menu(); + box.pack_start(menu.get_widget(), Gtk::PACK_SHRINK); box.pack_start(entry_box, Gtk::PACK_SHRINK); @@ -59,67 +60,66 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL) { } if(notebook.get_current_page()!=-1) { - if(auto menu_item=dynamic_cast(Singleton::menu()->ui_manager->get_widget("/MenuBar/SourceMenu/SourceGotoDeclaration"))) + if(auto menu_item=dynamic_cast(menu.ui_manager->get_widget("/MenuBar/SourceMenu/SourceGotoDeclaration"))) menu_item->set_sensitive((bool)notebook.get_current_view()->get_declaration_location); - if(auto menu_item=dynamic_cast(Singleton::menu()->ui_manager->get_widget("/MenuBar/SourceMenu/SourceGotoMethod"))) + if(auto menu_item=dynamic_cast(menu.ui_manager->get_widget("/MenuBar/SourceMenu/SourceGotoMethod"))) menu_item->set_sensitive((bool)notebook.get_current_view()->goto_method); - if(auto menu_item=dynamic_cast(Singleton::menu()->ui_manager->get_widget("/MenuBar/SourceMenu/SourceRename"))) + if(auto menu_item=dynamic_cast(menu.ui_manager->get_widget("/MenuBar/SourceMenu/SourceRename"))) menu_item->set_sensitive((bool)notebook.get_current_view()->rename_similar_tokens); } }); INFO("Window created"); } // Window constructor -void Window::add_menu() { - auto menu=Singleton::menu(); +void Window::create_menu() { INFO("Adding actions to menu"); - menu->action_group->add(Gtk::Action::create("FileQuit", "Quit juCi++"), Gtk::AccelKey(menu->key_map["quit"]), [this]() { + menu.action_group->add(Gtk::Action::create("FileQuit", "Quit juCi++"), Gtk::AccelKey(menu.key_map["quit"]), [this]() { hide(); }); - menu->action_group->add(Gtk::Action::create("FileNewFile", "New file"), Gtk::AccelKey(menu->key_map["new_file"]), [this]() { + menu.action_group->add(Gtk::Action::create("FileNewFile", "New file"), Gtk::AccelKey(menu.key_map["new_file"]), [this]() { new_file_entry(); }); - menu->action_group->add(Gtk::Action::create("FileOpenFile", "Open file"), Gtk::AccelKey(menu->key_map["open_file"]), [this]() { + menu.action_group->add(Gtk::Action::create("FileOpenFile", "Open file"), Gtk::AccelKey(menu.key_map["open_file"]), [this]() { open_file_dialog(); }); - menu->action_group->add(Gtk::Action::create("FileOpenFolder", "Open folder"), Gtk::AccelKey(menu->key_map["open_folder"]), [this]() { + menu.action_group->add(Gtk::Action::create("FileOpenFolder", "Open folder"), Gtk::AccelKey(menu.key_map["open_folder"]), [this]() { open_folder_dialog(); }); - menu->action_group->add(Gtk::Action::create("FileSaveAs", "Save as"), Gtk::AccelKey(menu->key_map["save_as"]), [this]() { + menu.action_group->add(Gtk::Action::create("FileSaveAs", "Save as"), Gtk::AccelKey(menu.key_map["save_as"]), [this]() { save_file_dialog(); }); - menu->action_group->add(Gtk::Action::create("FileSave", "Save"), Gtk::AccelKey(menu->key_map["save"]), [this]() { + menu.action_group->add(Gtk::Action::create("FileSave", "Save"), Gtk::AccelKey(menu.key_map["save"]), [this]() { notebook.save_current(); }); - menu->action_group->add(Gtk::Action::create("EditCopy", "Copy"), Gtk::AccelKey(menu->key_map["edit_copy"]), [this]() { + menu.action_group->add(Gtk::Action::create("EditCopy", "Copy"), Gtk::AccelKey(menu.key_map["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->action_group->add(Gtk::Action::create("EditCut", "Cut"), Gtk::AccelKey(menu->key_map["edit_cut"]), [this]() { + menu.action_group->add(Gtk::Action::create("EditCut", "Cut"), Gtk::AccelKey(menu.key_map["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->action_group->add(Gtk::Action::create("EditPaste", "Paste"), Gtk::AccelKey(menu->key_map["edit_paste"]), [this]() { + menu.action_group->add(Gtk::Action::create("EditPaste", "Paste"), Gtk::AccelKey(menu.key_map["edit_paste"]), [this]() { auto widget=get_focus(); if(auto entry=dynamic_cast(widget)) entry->paste_clipboard(); else if(notebook.get_current_page()!=-1) notebook.get_current_view()->get_buffer()->paste_clipboard(Gtk::Clipboard::get()); }); - menu->action_group->add(Gtk::Action::create("EditFind", "Find"), Gtk::AccelKey(menu->key_map["edit_find"]), [this]() { + menu.action_group->add(Gtk::Action::create("EditFind", "Find"), Gtk::AccelKey(menu.key_map["edit_find"]), [this]() { search_and_replace_entry(); }); - menu->action_group->add(Gtk::Action::create("EditUndo", "Undo"), Gtk::AccelKey(menu->key_map["edit_undo"]), [this]() { + menu.action_group->add(Gtk::Action::create("EditUndo", "Undo"), Gtk::AccelKey(menu.key_map["edit_undo"]), [this]() { INFO("On undo"); if(notebook.get_current_page()!=-1) { auto undo_manager = notebook.get_current_view()->get_source_buffer()->get_undo_manager(); @@ -129,7 +129,7 @@ void Window::add_menu() { } INFO("Done undo"); }); - menu->action_group->add(Gtk::Action::create("EditRedo", "Redo"), Gtk::AccelKey(menu->key_map["edit_redo"]), [this]() { + menu.action_group->add(Gtk::Action::create("EditRedo", "Redo"), Gtk::AccelKey(menu.key_map["edit_redo"]), [this]() { INFO("On Redo"); if(notebook.get_current_page()!=-1) { auto undo_manager = notebook.get_current_view()->get_source_buffer()->get_undo_manager(); @@ -140,7 +140,7 @@ void Window::add_menu() { INFO("Done Redo"); }); - menu->action_group->add(Gtk::Action::create("SourceGotoDeclaration", "Go to declaration"), Gtk::AccelKey(menu->key_map["source_goto_declaration"]), [this]() { + menu.action_group->add(Gtk::Action::create("SourceGotoDeclaration", "Go to declaration"), Gtk::AccelKey(menu.key_map["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(); @@ -154,18 +154,18 @@ void Window::add_menu() { } } }); - menu->action_group->add(Gtk::Action::create("SourceGotoMethod", "Go to method"), Gtk::AccelKey(menu->key_map["source_goto_method"]), [this]() { + menu.action_group->add(Gtk::Action::create("SourceGotoMethod", "Go to method"), Gtk::AccelKey(menu.key_map["source_goto_method"]), [this]() { if(notebook.get_current_page()!=-1) { if(notebook.get_current_view()->goto_method) { notebook.get_current_view()->goto_method(); } } }); - menu->action_group->add(Gtk::Action::create("SourceRename", "Rename"), Gtk::AccelKey(menu->key_map["source_rename"]), [this]() { + menu.action_group->add(Gtk::Action::create("SourceRename", "Rename"), Gtk::AccelKey(menu.key_map["source_rename"]), [this]() { rename_token_entry(); }); - menu->action_group->add(Gtk::Action::create("ProjectCompileAndRun", "Compile And Run"), Gtk::AccelKey(menu->key_map["compile_and_run"]), [this]() { + menu.action_group->add(Gtk::Action::create("ProjectCompileAndRun", "Compile And Run"), Gtk::AccelKey(menu.key_map["compile_and_run"]), [this]() { if(notebook.get_current_page()==-1) return; notebook.save_current(); @@ -185,7 +185,7 @@ void Window::add_menu() { execute.detach(); } }); - menu->action_group->add(Gtk::Action::create("ProjectCompile", "Compile"), Gtk::AccelKey(menu->key_map["compile"]), [this]() { + menu.action_group->add(Gtk::Action::create("ProjectCompile", "Compile"), Gtk::AccelKey(menu.key_map["compile"]), [this]() { if(notebook.get_current_page()==-1) return; notebook.save_current(); @@ -204,13 +204,12 @@ void Window::add_menu() { } }); - menu->action_group->add(Gtk::Action::create("WindowCloseTab", "Close tab"), Gtk::AccelKey(menu->key_map["close_tab"]), [this]() { + menu.action_group->add(Gtk::Action::create("WindowCloseTab", "Close tab"), Gtk::AccelKey(menu.key_map["close_tab"]), [this]() { notebook.close_current_page(); }); - add_accel_group(menu->ui_manager->get_accel_group()); - menu->build(); + add_accel_group(menu.ui_manager->get_accel_group()); + menu.build(); INFO("Menu build") - box.pack_start(menu->get_widget(), Gtk::PACK_SHRINK); } bool Window::on_key_press_event(GdkEventKey *event) { diff --git a/src/window.h b/src/window.h index 8c505f9..b257fc3 100644 --- a/src/window.h +++ b/src/window.h @@ -5,6 +5,7 @@ #include "directories.h" #include "entrybox.h" #include "notebook.h" +#include "menu.h" class Window : public Gtk::Window { public: @@ -20,8 +21,9 @@ private: Gtk::Paned directory_and_notebook_panes; EntryBox entry_box; std::mutex running; + Menu menu; - void add_menu(); + void create_menu(); void hide(); void new_file_entry(); void open_folder_dialog();