diff --git a/juci/menu.cc b/juci/menu.cc index 71703ba..3c3113a 100644 --- a/juci/menu.cc +++ b/juci/menu.cc @@ -5,7 +5,7 @@ Menu::Menu() : box(Gtk::ORIENTATION_VERTICAL) { action_group = Gtk::ActionGroup::create(); ui_manager = Gtk::UIManager::create(); - action_group->add(Gtk::Action::create("FileNew", "New File")); + 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("WindowSplitWindow", "Split window"), Gtk::AccelKey(key_map["split_window"]), [this]() { @@ -22,10 +22,6 @@ Gtk::Widget& Menu::get_widget() { return *ui_manager->get_widget("/MenuBar"); } -Gtk::Menu& Menu::get_source_menu() { - return *(Gtk::Menu*)ui_manager->get_widget("/MenuBar/SourceMenu"); -} - void Menu::build() { try { ui_manager->add_ui_from_string(ui); diff --git a/juci/menu.h b/juci/menu.h index 40e3cb3..9dcc6eb 100644 --- a/juci/menu.h +++ b/juci/menu.h @@ -9,7 +9,6 @@ class Menu { public: Menu(); Gtk::Widget& get_widget(); - Gtk::Menu& get_source_menu(); void build(); Gtk::Box box; diff --git a/juci/notebook.cc b/juci/notebook.cc index f3cd736..e5ed07e 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -10,58 +10,7 @@ namespace sigc { } Notebook::Notebook() : Gtk::Notebook() { - INFO("Create notebook"); Gsv::init(); - - auto menu=Singleton::menu(); - INFO("Notebook create signal handlers"); - menu->action_group->add(Gtk::Action::create("FileMenu", "File")); - - menu->action_group->add(Gtk::Action::create("WindowCloseTab", "Close tab"), Gtk::AccelKey(menu->key_map["close_tab"]), [this]() { - close_current_page(); - }); - - menu->action_group->add(Gtk::Action::create("EditUndo", "Undo"), Gtk::AccelKey(menu->key_map["edit_undo"]), [this]() { - INFO("On undo"); - Glib::RefPtr undo_manager = get_current_view()->get_source_buffer()->get_undo_manager(); - if (size() != 0 && undo_manager->can_undo()) { - undo_manager->undo(); - } - INFO("Done undo"); - }); - - menu->action_group->add(Gtk::Action::create("EditRedo", "Redo"), Gtk::AccelKey(menu->key_map["edit_redo"]), [this]() { - INFO("On Redo"); - Glib::RefPtr undo_manager = - get_current_view()->get_source_buffer()->get_undo_manager(); - if (size() != 0 && undo_manager->can_redo()) { - undo_manager->redo(); - } - INFO("Done Redo"); - }); - - menu->action_group->add(Gtk::Action::create("SourceGotoDeclaration", "Go to declaration"), Gtk::AccelKey(menu->key_map["source_goto_declaration"]), [this]() { - if(get_current_page()!=-1) { - if(get_current_view()->get_declaration_location) { - auto location=get_current_view()->get_declaration_location(); - if(location.first.size()>0) { - open(location.first); - get_current_view()->get_buffer()->place_cursor(get_current_view()->get_buffer()->get_iter_at_offset(location.second)); - while(gtk_events_pending()) - gtk_main_iteration(); - get_current_view()->scroll_to(get_current_view()->get_buffer()->get_insert(), 0.0, 1.0, 0.5); - } - } - } - }); - - menu->action_group->add(Gtk::Action::create("SourceGotoMethod", "Go to method"), Gtk::AccelKey(menu->key_map["source_goto_method"]), [this]() { - if(get_current_page()!=-1) { - if(get_current_view()->goto_method) { - get_current_view()->goto_method(); - } - } - }); } int Notebook::size() { @@ -149,7 +98,7 @@ bool Notebook::save(int page) { } bool Notebook::save_current() { - INFO("Source save file"); + INFO("Notebook save current file"); if(get_current_page()==-1) return false; return save(get_current_page()); @@ -172,7 +121,7 @@ bool Notebook::close_current_page() { } bool Notebook::save_modified_dialog() { - INFO("Notebook::save_dialog"); + INFO("Notebook::save_modified_dialog"); Gtk::MessageDialog dialog((Gtk::Window&)(*get_toplevel()), "Save file!", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); dialog.set_secondary_text("Do you want to save: " + get_current_view()->file_path+" ?"); int result = dialog.run(); diff --git a/juci/window.cc b/juci/window.cc index 2fb9795..acafd26 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -14,6 +14,58 @@ Window::Window() : notebook(), plugin_api(¬ebook), box(Gtk::ORIENTATION_VERTI add(box); //TODO: see TODO Window::on_directory_navigation directories.m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this, &Window::on_directory_navigation)); + + add_menu(); + + box.pack_start(entry_box, Gtk::PACK_SHRINK); + + directory_and_notebook_panes.pack1(directories.widget(), true, true); //TODO: should be pack1(directories, ...) Clean up directories.* + directory_and_notebook_panes.pack2(notebook); + directory_and_notebook_panes.set_position(120); + + vpaned.set_position(300); + vpaned.pack1(directory_and_notebook_panes, true, false); + vpaned.pack2(Singleton::terminal()->view, true, true); + box.pack_end(vpaned); + show_all_children(); + + entry_box.signal_show().connect([this](){ + std::vector focus_chain; + focus_chain.emplace_back(&entry_box); + box.set_focus_chain(focus_chain); + }); + entry_box.signal_hide().connect([this](){ + box.unset_focus_chain(); + }); + entry_box.signal_hide().connect([this]() { + if(notebook.get_current_page()!=-1) { + notebook.get_current_view()->grab_focus(); + } + }); + notebook.signal_switch_page().connect([this](Gtk::Widget* page, guint page_num) { + if(search_entry_shown && entry_box.labels.size()>0 && notebook.get_current_page()!=-1) { + notebook.get_current_view()->update_search_occurrences=[this](int number){ + entry_box.labels.begin()->update(0, std::to_string(number)); + }; + notebook.get_current_view()->search_highlight(last_search, case_sensitive_search, regex_search); + } + + if(notebook.get_current_page()!=-1) { + if(auto menu_item=dynamic_cast(Singleton::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"))) + 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"))) + 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(); menu->action_group->add(Gtk::Action::create("FileQuit", "Quit juCi++"), Gtk::AccelKey(menu->key_map["quit"]), [this]() { hide(); @@ -46,65 +98,64 @@ Window::Window() : notebook(), plugin_api(¬ebook), box(Gtk::ORIENTATION_VERTI auto widget=get_focus(); if(auto entry=dynamic_cast(widget)) entry->cut_clipboard(); - else { - if (notebook.size() != 0) - notebook.get_current_view()->get_buffer()->cut_clipboard(Gtk::Clipboard::get()); - } + 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]() { auto widget=get_focus(); if(auto entry=dynamic_cast(widget)) entry->paste_clipboard(); - else { - if (notebook.size() != 0) - notebook.get_current_view()->get_buffer()->paste_clipboard(Gtk::Clipboard::get()); - } + 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]() { search_and_replace_entry(); }); + 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(); + if (undo_manager->can_undo()) { + undo_manager->undo(); + } + } + INFO("Done undo"); + }); + 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(); + if(undo_manager->can_redo()) { + undo_manager->redo(); + } + } + INFO("Done Redo"); + }); - menu->action_group->add(Gtk::Action::create("SourceRename", "Rename function/variable"), Gtk::AccelKey(menu->key_map["source_rename"]), [this]() { - entry_box.clear(); + 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_token && notebook.get_current_view()->get_token_name) { - auto token=std::make_shared(notebook.get_current_view()->get_token()); - if(token->size()>0 && notebook.get_current_view()->get_token_name) { - auto token_name=std::make_shared(notebook.get_current_view()->get_token_name()); - for(int c=0;ctag_similar_tokens) { - notebook.get_view(c)->tag_similar_tokens(*token); - } - } - entry_box.labels.emplace_back(); - auto label_it=entry_box.labels.begin(); - label_it->update=[label_it](int state, const std::string& message){ - label_it->set_text("Warning: only opened and parsed tabs will have its content renamed, and modified files will be saved."); - }; - label_it->update(0, ""); - entry_box.entries.emplace_back(*token_name, [this, token_name, token](const std::string& content){ - if(notebook.get_current_page()!=-1 && content!=*token_name) { - for(int c=0;crename_similar_tokens) { - auto number=notebook.get_view(c)->rename_similar_tokens(*token, content); - if(number>0) { - Singleton::terminal()->print("Replaced "+std::to_string(number)+" occurrences in file "+notebook.get_view(c)->file_path+"\n"); - notebook.save(c); - } - } - } - entry_box.hide(); - } - }); - auto entry_it=entry_box.entries.begin(); - entry_box.buttons.emplace_back("Rename", [this, entry_it](){ - entry_it->activate(); - }); - entry_box.show(); + if(notebook.get_current_view()->get_declaration_location) { + auto location=notebook.get_current_view()->get_declaration_location(); + if(location.first.size()>0) { + notebook.open(location.first); + notebook.get_current_view()->get_buffer()->place_cursor(notebook.get_current_view()->get_buffer()->get_iter_at_offset(location.second)); + while(gtk_events_pending()) + gtk_main_iteration(); + notebook.get_current_view()->scroll_to(notebook.get_current_view()->get_buffer()->get_insert(), 0.0, 1.0, 0.5); } } } }); + 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]() { + rename_token_entry(); + }); 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) @@ -126,7 +177,6 @@ Window::Window() : notebook(), plugin_api(¬ebook), box(Gtk::ORIENTATION_VERTI execute.detach(); } }); - menu->action_group->add(Gtk::Action::create("ProjectCompile", "Compile"), Gtk::AccelKey(menu->key_map["compile"]), [this]() { if(notebook.get_current_page()==-1) return; @@ -146,56 +196,14 @@ Window::Window() : notebook(), plugin_api(¬ebook), box(Gtk::ORIENTATION_VERTI } }); + 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(); box.pack_start(menu->get_widget(), Gtk::PACK_SHRINK); - box.pack_start(entry_box, Gtk::PACK_SHRINK); - - directory_and_notebook_panes.pack1(directories.widget(), true, true); //TODO: should be pack1(directories, ...) Clean up directories.* - directory_and_notebook_panes.pack2(notebook); - directory_and_notebook_panes.set_position(120); - - vpaned.set_position(300); - vpaned.pack1(directory_and_notebook_panes, true, false); - vpaned.pack2(Singleton::terminal()->view, true, true); - box.pack_end(vpaned); - show_all_children(); - - entry_box.signal_show().connect([this](){ - std::vector focus_chain; - focus_chain.emplace_back(&entry_box); - box.set_focus_chain(focus_chain); - }); - entry_box.signal_hide().connect([this](){ - box.unset_focus_chain(); - }); - entry_box.signal_hide().connect([this]() { - if(notebook.get_current_page()!=-1) { - notebook.get_current_view()->grab_focus(); - } - }); - notebook.signal_switch_page().connect([this](Gtk::Widget* page, guint page_num) { - if(search_entry_shown && entry_box.labels.size()>0 && notebook.get_current_page()!=-1) { - notebook.get_current_view()->update_search_occurrences=[this](int number){ - entry_box.labels.begin()->update(0, std::to_string(number)); - }; - notebook.get_current_view()->search_highlight(last_search, case_sensitive_search, regex_search); - } - - if(notebook.get_current_page()!=-1) { - if(auto menu_item=dynamic_cast(Singleton::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"))) - 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"))) - menu_item->set_sensitive((bool)notebook.get_current_view()->rename_similar_tokens); - } - }); - - INFO("Window created"); -} // Window constructor +} bool Window::on_key_press_event(GdkEventKey *event) { if(event->keyval==GDK_KEY_Escape) @@ -485,3 +493,45 @@ void Window::search_and_replace_entry() { search_entry_shown=true; entry_box.show(); } + +void Window::rename_token_entry() { + entry_box.clear(); + if(notebook.get_current_page()!=-1) { + if(notebook.get_current_view()->get_token && notebook.get_current_view()->get_token_name) { + auto token=std::make_shared(notebook.get_current_view()->get_token()); + if(token->size()>0 && notebook.get_current_view()->get_token_name) { + auto token_name=std::make_shared(notebook.get_current_view()->get_token_name()); + for(int c=0;ctag_similar_tokens) { + notebook.get_view(c)->tag_similar_tokens(*token); + } + } + entry_box.labels.emplace_back(); + auto label_it=entry_box.labels.begin(); + label_it->update=[label_it](int state, const std::string& message){ + label_it->set_text("Warning: only opened and parsed tabs will have its content renamed, and modified files will be saved."); + }; + label_it->update(0, ""); + entry_box.entries.emplace_back(*token_name, [this, token_name, token](const std::string& content){ + if(notebook.get_current_page()!=-1 && content!=*token_name) { + for(int c=0;crename_similar_tokens) { + auto number=notebook.get_view(c)->rename_similar_tokens(*token, content); + if(number>0) { + Singleton::terminal()->print("Replaced "+std::to_string(number)+" occurrences in file "+notebook.get_view(c)->file_path+"\n"); + notebook.save(c); + } + } + } + entry_box.hide(); + } + }); + auto entry_it=entry_box.entries.begin(); + entry_box.buttons.emplace_back("Rename", [this, entry_it](){ + entry_it->activate(); + }); + entry_box.show(); + } + } + } +} diff --git a/juci/window.h b/juci/window.h index d40f9b6..2ebadc6 100644 --- a/juci/window.h +++ b/juci/window.h @@ -22,6 +22,7 @@ private: PluginApi plugin_api; std::mutex running; + void add_menu(); void hide(); void new_file_entry(); void open_folder_dialog(); @@ -30,6 +31,7 @@ private: void on_directory_navigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); void search_and_replace_entry(); + void rename_token_entry(); std::string last_search; std::string last_replace; bool case_sensitive_search=true;