From 3da0faaf6ecdf76af3953fb7e77ca7ca90fa26ea Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 25 Oct 2015 22:03:12 +0100 Subject: [PATCH] Further work towards new menu system. --- src/config.cc | 13 +---- src/juci.cc | 1 - src/menu.cc | 135 ++++++++++++-------------------------------------- src/menu.h | 6 ++- src/window.cc | 113 ++++++++++++++++++------------------------ 5 files changed, 86 insertions(+), 182 deletions(-) diff --git a/src/config.cc b/src/config.cc index b673814..c28594f 100644 --- a/src/config.cc +++ b/src/config.cc @@ -54,18 +54,7 @@ void MainConfig::find_or_create_config_files() { void MainConfig::retrieve_config() { auto keybindings_pt = cfg.get_child("keybindings"); for (auto &i : keybindings_pt) { - auto key = i.second.get_value(); - size_t pos=0; - while((pos=key.find('<', pos))!=std::string::npos) { - key.replace(pos, 1, "<"); - pos+=4; - } - pos=0; - while((pos=key.find('>', pos))!=std::string::npos) { - key.replace(pos, 1, ">"); - pos+=4; - } - Singleton::Config::menu()->keys[i.first] = key; + Singleton::Config::menu()->keys[i.first] = i.second.get_value(); } GenerateSource(); GenerateDirectoryFilter(); diff --git a/src/juci.cc b/src/juci.cc index 12d39c4..30f7332 100644 --- a/src/juci.cc +++ b/src/juci.cc @@ -89,7 +89,6 @@ void app::on_startup() { app::app() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) { Glib::set_application_name("juCi++"); - window = std::unique_ptr(new Window()); } diff --git a/src/menu.cc b/src/menu.cc index 0768bc8..ed3118b 100644 --- a/src/menu.cc +++ b/src/menu.cc @@ -6,28 +6,25 @@ using namespace std; //TODO: remove Menu::Menu() { - auto &keys=Singleton::Config::menu()->keys; - ui_xml = "" " " "
" " " " _About" - " win.about" + " app.about" " " "
" "
" " " " _Preferences" - " win.preferences" + " app.preferences" " " "
" "
" " " " _Quit" - " win.quit" - " "+keys["quit"]+"" + " app.quit" " " "
" "
" @@ -38,119 +35,53 @@ Menu::Menu() { "
" " " " _New _File" - " win.new_file" + " app.new_file" " " " " " _New _Directory" - " win.new_directory" + " app.new_folder" " " + " " + " _New _Project" + " " + " C++" + " app.new_project_cpp" + " " + " " "
" "
" " " " _Open _File" - " win.open_file" + " app.open_file" + " " + " " + " _Open _Folder" + " app.open_folder" + " " + "
" + "
" + " " + " _Save" + " app.save" " " " " - " _Open _Directory" - " win.open_directory" + " _Save _As" + " app.save_as" " " "
" " " " " "
"; - - /*action_group = Gtk::ActionGroup::create(); - ui_manager = Gtk::UIManager::create(); - ui_manager->insert_action_group(action_group); - - action_group->add(Gtk::Action::create("FileMenu", "File")); - action_group->add(Gtk::Action::create("EditMenu", "Edit")); - action_group->add(Gtk::Action::create("WindowMenu", "_Window")); - action_group->add(Gtk::Action::create("ProjectMenu", "P_roject")); - action_group->add(Gtk::Action::create("SourceMenu", "_Source")); - action_group->add(Gtk::Action::create("PluginMenu", "_Plugins")); - action_group->add(Gtk::Action::create("HelpMenu", "Help")); - - ui_xml = - "\n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - "\n";*/ } -Gtk::Widget& Menu::get_widget() { - //return *ui_manager->get_widget("/MenuBar"); +void Menu::add_action(const std::string &name, std::function action) { + auto application = builder->get_application(); + + actions[name]=application->add_action(name, action); + + auto key=Singleton::Config::menu()->keys.find(name); + if(key!=Singleton::Config::menu()->keys.end()) + application->set_accel_for_action("app."+name, key->second); } void Menu::build() { diff --git a/src/menu.h b/src/menu.h index 00df97e..d5cec5a 100644 --- a/src/menu.h +++ b/src/menu.h @@ -11,9 +11,11 @@ public: public: std::unordered_map keys; }; - + Menu(); - Gtk::Widget& get_widget(); + void add_action(const std::string &name, std::function action); + std::unordered_map > actions; + void build(); std::string ui_xml; diff --git a/src/window.cc b/src/window.cc index 24a26a3..bf52ebb 100644 --- a/src/window.cc +++ b/src/window.cc @@ -31,6 +31,7 @@ Window::Window() : compiling(false) { set_default_size(Singleton::Config::window()->default_size.first, Singleton::Config::window()->default_size.second); //PluginApi(&this->notebook, &this->menu); + add(vpaned); directory_and_notebook_panes.pack1(*Singleton::directories(), Gtk::SHRINK); notebook_vbox.pack_start(notebook); @@ -48,7 +49,6 @@ Window::Window() : compiling(false) { terminal_vbox.pack_end(info_and_status_hbox, Gtk::PACK_SHRINK); vpaned.pack2(terminal_vbox, true, true); - add(vpaned); show_all_children(); Singleton::directories()->on_row_activated=[this](const std::string &file) { @@ -152,13 +152,22 @@ void Window::configure() { style_context->add_provider_for_screen(screen, css_provider, GTK_STYLE_PROVIDER_PRIORITY_SETTINGS); } - void Window::set_menu_actions() { +void Window::set_menu_actions() { + auto menu = Singleton::menu(); - add_action("quit", [this]() { + menu->add_action("about", [this]() { + about.show(); + about.present(); + }); + menu->add_action("preferences", [this]() { + notebook.open(Singleton::config_dir()+"config.json"); + }); + menu->add_action("quit", [this]() { hide(); }); - auto action=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)) { @@ -176,33 +185,7 @@ void Window::configure() { } } }); - auto accel_group=get_accel_group(); - auto accel_grp=Gtk::AccelGroup::create(); - - - - /*menu.action_group->add(Gtk::Action::create("FileQuit", "Quit juCi++"), Gtk::AccelKey(key_map["quit"]), [this]() { - hide(); - }); - menu.action_group->add(Gtk::Action::create("FileNewFile", "New File"), Gtk::AccelKey(key_map["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"); - } - else { - if(juci::filesystem::write(path)) { - if(Singleton::directories()->current_path!="") - Singleton::directories()->update(); - notebook.open(path.string()); - Singleton::terminal()->print("New file "+path.string()+" created.\n"); - } - else - Singleton::terminal()->print("Error: could not create new file "+path.string()+".\n"); - } - } - }); - menu.action_group->add(Gtk::Action::create("FileNewFolder", "New Folder"), Gtk::AccelKey(key_map["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)) { @@ -216,8 +199,7 @@ void Window::configure() { Singleton::directories()->select(path); } }); - menu.action_group->add(Gtk::Action::create("FileNewProject", "New Project")); - menu.action_group->add(Gtk::Action::create("FileNewProjectCpp", "C++"), [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(); @@ -248,49 +230,53 @@ void Window::configure() { Singleton::terminal()->print("Error: Could not create project "+project_path.string()+"\n"); } }); - menu.action_group->add(Gtk::Action::create("FileOpenFile", "Open File"), Gtk::AccelKey(key_map["open_file"]), [this]() { + + menu->add_action("open_file", [this]() { auto path=Dialog::select_file(); if(path!="") notebook.open(path); }); - menu.action_group->add(Gtk::Action::create("FileOpenFolder", "Open Folder"), Gtk::AccelKey(key_map["open_folder"]), [this]() { + menu->add_action("open_folder", [this]() { auto path = Dialog::select_folder(); if (path!="" && boost::filesystem::exists(path)) Singleton::directories()->open(path); }); - menu.action_group->add(Gtk::Action::create("FileSaveAs", "Save As"), Gtk::AccelKey(key_map["save_as"]), [this]() { - auto path = Dialog::save_file(); - if(path!="") { - std::ofstream file(path); - if(file) { - file << notebook.get_current_view()->get_buffer()->get_text(); - file.close(); - if(Singleton::directories()->current_path!="") - Singleton::directories()->update(); - notebook.open(path); - Singleton::terminal()->print("File saved to: " + notebook.get_current_view()->file_path.string()+"\n"); + + 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_dir()+"config.json") { + configure(); + for(int c=0;cconfigure(); + notebook.configure(c); + } + } + } } - else - Singleton::terminal()->print("Error saving file\n"); } -}); - menu.action_group->add(Gtk::Action::create("Preferences", "Preferences..."), Gtk::AccelKey(key_map["preferences"]), [this]() { - notebook.open(Singleton::config_dir()+"config.json"); }); - - menu.action_group->add(Gtk::Action::create("FileSave", "Save"), Gtk::AccelKey(key_map["save"]), [this]() { - if(notebook.save_current()) { - if(notebook.get_current_page()!=-1) { - if(notebook.get_current_view()->file_path==Singleton::config_dir()+"config.json") { - configure(); - for(int c=0;cconfigure(); - notebook.configure(c); - } + menu->add_action("save_as", [this]() { + if(notebook.get_current_page()!=-1) { + auto path = Dialog::save_file(); + if(path!="") { + std::ofstream file(path); + if(file) { + file << notebook.get_current_view()->get_buffer()->get_text(); + file.close(); + if(Singleton::directories()->current_path!="") + Singleton::directories()->update(); + notebook.open(path); + Singleton::terminal()->print("File saved to: " + notebook.get_current_view()->file_path.string()+"\n"); } + else + Singleton::terminal()->print("Error saving file\n"); } } }); + + /* menu.action_group->add(Gtk::Action::create("EditCopy", "Copy"), Gtk::AccelKey(key_map["edit_copy"]), [this]() { auto widget=get_focus(); @@ -576,10 +562,7 @@ void Window::configure() { notebook.open(notebook.get_view(previous_page)->file_path); } }); - menu.action_group->add(Gtk::Action::create("HelpAbout", "About"), [this] () { - about.show(); - about.present(); - });*/ + */ } bool Window::on_key_press_event(GdkEventKey *event) {