Browse Source

Further work towards new menu system.

merge-requests/365/head
eidheim 10 years ago
parent
commit
3da0faaf6e
  1. 13
      src/config.cc
  2. 1
      src/juci.cc
  3. 133
      src/menu.cc
  4. 4
      src/menu.h
  5. 113
      src/window.cc

13
src/config.cc

@ -54,18 +54,7 @@ void MainConfig::find_or_create_config_files() {
void MainConfig::retrieve_config() { void MainConfig::retrieve_config() {
auto keybindings_pt = cfg.get_child("keybindings"); auto keybindings_pt = cfg.get_child("keybindings");
for (auto &i : keybindings_pt) { for (auto &i : keybindings_pt) {
auto key = i.second.get_value<std::string>(); Singleton::Config::menu()->keys[i.first] = i.second.get_value<std::string>();
size_t pos=0;
while((pos=key.find('<', pos))!=std::string::npos) {
key.replace(pos, 1, "&lt;");
pos+=4;
}
pos=0;
while((pos=key.find('>', pos))!=std::string::npos) {
key.replace(pos, 1, "&gt;");
pos+=4;
}
Singleton::Config::menu()->keys[i.first] = key;
} }
GenerateSource(); GenerateSource();
GenerateDirectoryFilter(); GenerateDirectoryFilter();

1
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) { app::app() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) {
Glib::set_application_name("juCi++"); Glib::set_application_name("juCi++");
window = std::unique_ptr<Window>(new Window()); window = std::unique_ptr<Window>(new Window());
} }

133
src/menu.cc

@ -6,28 +6,25 @@
using namespace std; //TODO: remove using namespace std; //TODO: remove
Menu::Menu() { Menu::Menu() {
auto &keys=Singleton::Config::menu()->keys;
ui_xml = ui_xml =
"<interface>" "<interface>"
" <menu id='juci-menu'>" " <menu id='juci-menu'>"
" <section>" " <section>"
" <item>" " <item>"
" <attribute name='label' translatable='yes'>_About</attribute>" " <attribute name='label' translatable='yes'>_About</attribute>"
" <attribute name='action'>win.about</attribute>" " <attribute name='action'>app.about</attribute>"
" </item>" " </item>"
" </section>" " </section>"
" <section>" " <section>"
" <item>" " <item>"
" <attribute name='label' translatable='yes'>_Preferences</attribute>" " <attribute name='label' translatable='yes'>_Preferences</attribute>"
" <attribute name='action'>win.preferences</attribute>" " <attribute name='action'>app.preferences</attribute>"
" </item>" " </item>"
" </section>" " </section>"
" <section>" " <section>"
" <item>" " <item>"
" <attribute name='label' translatable='yes'>_Quit</attribute>" " <attribute name='label' translatable='yes'>_Quit</attribute>"
" <attribute name='action'>win.quit</attribute>" " <attribute name='action'>app.quit</attribute>"
" <attribute name='accel'>"+keys["quit"]+"</attribute>"
" </item>" " </item>"
" </section>" " </section>"
" </menu>" " </menu>"
@ -38,119 +35,53 @@ Menu::Menu() {
" <section>" " <section>"
" <item>" " <item>"
" <attribute name='label' translatable='yes'>_New _File</attribute>" " <attribute name='label' translatable='yes'>_New _File</attribute>"
" <attribute name='action'>win.new_file</attribute>" " <attribute name='action'>app.new_file</attribute>"
" </item>" " </item>"
" <item>" " <item>"
" <attribute name='label' translatable='yes'>_New _Directory</attribute>" " <attribute name='label' translatable='yes'>_New _Directory</attribute>"
" <attribute name='action'>win.new_directory</attribute>" " <attribute name='action'>app.new_folder</attribute>"
" </item>" " </item>"
" <submenu>"
" <attribute name='label' translatable='yes'>_New _Project</attribute>"
" <item>"
" <attribute name='label' translatable='yes'>C++</attribute>"
" <attribute name='action'>app.new_project_cpp</attribute>"
" </item>"
" </submenu>"
" </section>" " </section>"
" <section>" " <section>"
" <item>" " <item>"
" <attribute name='label' translatable='yes'>_Open _File</attribute>" " <attribute name='label' translatable='yes'>_Open _File</attribute>"
" <attribute name='action'>win.open_file</attribute>" " <attribute name='action'>app.open_file</attribute>"
" </item>" " </item>"
" <item>" " <item>"
" <attribute name='label' translatable='yes'>_Open _Directory</attribute>" " <attribute name='label' translatable='yes'>_Open _Folder</attribute>"
" <attribute name='action'>win.open_directory</attribute>" " <attribute name='action'>app.open_folder</attribute>"
" </item>"
" </section>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_Save</attribute>"
" <attribute name='action'>app.save</attribute>"
" </item>"
" <item>"
" <attribute name='label' translatable='yes'>_Save _As</attribute>"
" <attribute name='action'>app.save_as</attribute>"
" </item>" " </item>"
" </section>" " </section>"
" </submenu>" " </submenu>"
" </menu>" " </menu>"
"</interface>"; "</interface>";
}
/*action_group = Gtk::ActionGroup::create(); void Menu::add_action(const std::string &name, std::function<void()> action) {
ui_manager = Gtk::UIManager::create(); auto application = builder->get_application();
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 = actions[name]=application->add_action(name, action);
"<ui>\n"
" <menubar name=\"MenuBar\">\n"
" <menu action=\"FileMenu\">\n"
" <menuitem action=\"FileNewFile\"/>\n"
" <menuitem action=\"FileNewFolder\"/>\n"
" <menu action=\"FileNewProject\">\n"
" <menuitem action=\"FileNewProjectCpp\"/>\n"
" </menu>\n"
" <separator/>\n"
" <menuitem action=\"FileOpenFile\"/>\n"
" <menuitem action=\"FileOpenFolder\"/>\n"
" <separator/>\n"
" <menuitem action=\"FileSave\"/>\n"
" <menuitem action=\"FileSaveAs\"/>\n"
" <separator/>\n"
" <menuitem action=\"Preferences\"/>\n"
" <separator/>\n"
" <menuitem action=\"FileQuit\"/>\n"
" </menu>\n"
" <menu action=\"EditMenu\">\n"
" <menuitem action=\"EditUndo\"/>\n"
" <menuitem action=\"EditRedo\"/>\n"
" <separator/>\n"
" <menuitem action=\"EditCopy\"/>\n"
" <menuitem action=\"EditCut\"/>\n"
" <menuitem action=\"EditPaste\"/>\n"
" <separator/>\n"
" <menuitem action=\"EditFind\"/>\n"
" </menu>\n"
" <menu action=\"SourceMenu\">\n"
" <menu action=\"SourceSpellCheck\">\n"
" <menuitem action=\"SourceSpellCheckBuffer\"/>\n"
" <menuitem action=\"SourceSpellCheckClear\"/>\n"
" <menuitem action=\"SourceSpellCheckNextError\"/>\n"
" </menu>\n"
" <separator/>\n"
" <menu action=\"SourceIndentation\">\n"
" <menuitem action=\"SourceIndentationSetBufferTab\"/>\n"
" <menuitem action=\"SourceIndentationAutoIndentBuffer\"/>\n"
" </menu>\n"
" <separator/>\n"
" <menuitem action=\"SourceGotoLine\"/>\n"
" <menuitem action=\"SourceCenterCursor\"/>\n"
" <separator/>\n"
" <menuitem action=\"SourceFindDocumentation\"/>\n"
" <separator/>\n"
" <menuitem action=\"SourceGotoDeclaration\"/>\n"
" <menuitem action=\"SourceGotoMethod\"/>\n"
" <menuitem action=\"SourceRename\"/>\n"
" <separator/>\n"
" <menuitem action=\"SourceGotoNextDiagnostic\"/>\n"
" <menuitem action=\"SourceApplyFixIts\"/>\n"
" <separator/>\n"
" </menu>\n"
" <menu action=\"ProjectMenu\">\n"
" <menuitem action=\"ProjectCompileAndRun\"/>\n"
" <menuitem action=\"ProjectCompile\"/>\n"
" <separator/>\n"
" <menuitem action=\"ProjectRunCommand\"/>\n"
" <menuitem action=\"ProjectKillLastRunning\"/>\n"
" <menuitem action=\"ProjectForceKillLastRunning\"/>\n"
" </menu>\n"
" <menu action=\"WindowMenu\">\n"
" <menuitem action=\"WindowNextTab\"/>\n"
" <menuitem action=\"WindowPreviousTab\"/>\n"
" <separator/>\n"
" <menuitem action=\"WindowCloseTab\"/>\n"
" </menu>\n"
" <menu action=\"PluginMenu\">\n"
" </menu>\n"
" <menu action=\"HelpMenu\">\n"
" <menuitem action=\"HelpAbout\"/>\n"
" </menu>\n"
" </menubar>\n"
"</ui>\n";*/
}
Gtk::Widget& Menu::get_widget() { auto key=Singleton::Config::menu()->keys.find(name);
//return *ui_manager->get_widget("/MenuBar"); if(key!=Singleton::Config::menu()->keys.end())
application->set_accel_for_action("app."+name, key->second);
} }
void Menu::build() { void Menu::build() {

4
src/menu.h

@ -13,7 +13,9 @@ public:
}; };
Menu(); Menu();
Gtk::Widget& get_widget(); void add_action(const std::string &name, std::function<void()> action);
std::unordered_map<std::string, Glib::RefPtr<Gio::SimpleAction> > actions;
void build(); void build();
std::string ui_xml; std::string ui_xml;

113
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); set_default_size(Singleton::Config::window()->default_size.first, Singleton::Config::window()->default_size.second);
//PluginApi(&this->notebook, &this->menu); //PluginApi(&this->notebook, &this->menu);
add(vpaned);
directory_and_notebook_panes.pack1(*Singleton::directories(), Gtk::SHRINK); directory_and_notebook_panes.pack1(*Singleton::directories(), Gtk::SHRINK);
notebook_vbox.pack_start(notebook); notebook_vbox.pack_start(notebook);
@ -48,7 +49,6 @@ Window::Window() : compiling(false) {
terminal_vbox.pack_end(info_and_status_hbox, Gtk::PACK_SHRINK); terminal_vbox.pack_end(info_and_status_hbox, Gtk::PACK_SHRINK);
vpaned.pack2(terminal_vbox, true, true); vpaned.pack2(terminal_vbox, true, true);
add(vpaned);
show_all_children(); show_all_children();
Singleton::directories()->on_row_activated=[this](const std::string &file) { 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); 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(); hide();
}); });
auto action=add_action("new_file", [this]() {
menu->add_action("new_file", [this]() {
boost::filesystem::path path = Dialog::new_file(); boost::filesystem::path path = Dialog::new_file();
if(path!="") { if(path!="") {
if(boost::filesystem::exists(path)) { if(boost::filesystem::exists(path)) {
@ -176,33 +185,7 @@ void Window::configure() {
} }
} }
}); });
auto accel_group=get_accel_group(); menu->add_action("new_folder", [this]() {
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]() {
auto time_now=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); auto time_now=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
boost::filesystem::path path = Dialog::new_folder(); boost::filesystem::path path = Dialog::new_folder();
if(path!="" && boost::filesystem::exists(path)) { if(path!="" && boost::filesystem::exists(path)) {
@ -216,8 +199,7 @@ void Window::configure() {
Singleton::directories()->select(path); Singleton::directories()->select(path);
} }
}); });
menu.action_group->add(Gtk::Action::create("FileNewProject", "New Project")); menu->add_action("new_project_cpp", [this]() {
menu.action_group->add(Gtk::Action::create("FileNewProjectCpp", "C++"), [this]() {
boost::filesystem::path project_path = Dialog::new_folder(); boost::filesystem::path project_path = Dialog::new_folder();
if(project_path!="") { if(project_path!="") {
auto project_name=project_path.filename().string(); auto project_name=project_path.filename().string();
@ -248,50 +230,54 @@ void Window::configure() {
Singleton::terminal()->print("Error: Could not create project "+project_path.string()+"\n"); 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(); auto path=Dialog::select_file();
if(path!="") if(path!="")
notebook.open(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(); auto path = Dialog::select_folder();
if (path!="" && boost::filesystem::exists(path)) if (path!="" && boost::filesystem::exists(path))
Singleton::directories()->open(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(); menu->add_action("save", [this]() {
if(path!="") { if(notebook.get_current_page()!=-1) {
std::ofstream file(path); if(notebook.save_current()) {
if(file) { if(notebook.get_current_page()!=-1) {
file << notebook.get_current_view()->get_buffer()->get_text(); if(notebook.get_current_view()->file_path==Singleton::config_dir()+"config.json") {
file.close(); configure();
if(Singleton::directories()->current_path!="") for(int c=0;c<notebook.size();c++) {
Singleton::directories()->update(); notebook.get_view(c)->configure();
notebook.open(path); notebook.configure(c);
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("Preferences", "Preferences..."), Gtk::AccelKey(key_map["preferences"]), [this]() {
notebook.open(Singleton::config_dir()+"config.json");
}); });
menu->add_action("save_as", [this]() {
menu.action_group->add(Gtk::Action::create("FileSave", "Save"), Gtk::AccelKey(key_map["save"]), [this]() { if(notebook.get_current_page()!=-1) {
if(notebook.save_current()) { auto path = Dialog::save_file();
if(notebook.get_current_page()!=-1) { if(path!="") {
if(notebook.get_current_view()->file_path==Singleton::config_dir()+"config.json") { std::ofstream file(path);
configure(); if(file) {
for(int c=0;c<notebook.size();c++) { file << notebook.get_current_view()->get_buffer()->get_text();
notebook.get_view(c)->configure(); file.close();
notebook.configure(c); 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]() { menu.action_group->add(Gtk::Action::create("EditCopy", "Copy"), Gtk::AccelKey(key_map["edit_copy"]), [this]() {
auto widget=get_focus(); auto widget=get_focus();
if(auto entry=dynamic_cast<Gtk::Entry*>(widget)) if(auto entry=dynamic_cast<Gtk::Entry*>(widget))
@ -576,10 +562,7 @@ void Window::configure() {
notebook.open(notebook.get_view(previous_page)->file_path); 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) { bool Window::on_key_press_event(GdkEventKey *event) {

Loading…
Cancel
Save