From ae46741794c9ae259d6c3a17c67403422db2c40e Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 29 Jul 2015 10:20:53 +0200 Subject: [PATCH] Quit application fixed, also possible to cancel quit by closing dialog asking if a modified tab should be saved. --- juci/notebook.cc | 52 +++++++++++++++++------------------------------- juci/notebook.h | 4 ++-- juci/window.cc | 26 +++++++++++++++--------- juci/window.h | 6 ++---- 4 files changed, 39 insertions(+), 49 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 9286bc8..a309877 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -60,7 +60,7 @@ void Notebook::Controller::CreateKeybindings() { OnFileNewFile(); }); menu->action_group->add(Gtk::Action::create("WindowCloseTab", "Close tab"), Gtk::AccelKey(menu->key_map["close_tab"]), [this]() { - OnCloseCurrentPage(); + close_current_page(); }); menu->action_group->add(Gtk::Action::create("EditFind", "Find"), Gtk::AccelKey(menu->key_map["edit_find"]), [this]() { show_search_and_replace(); @@ -317,11 +317,12 @@ void Notebook::Controller::open_file(std::string path) { }); } -void Notebook::Controller::OnCloseCurrentPage() { +bool Notebook::Controller::close_current_page() { INFO("Notebook close page"); if (Pages() != 0) { if(CurrentSourceView()->get_buffer()->get_modified()){ - AskToSaveDialog(); + if(!save_dialog()) + return false; } int page = CurrentPage(); view.notebook.remove_page(page); @@ -329,6 +330,7 @@ void Notebook::Controller::OnCloseCurrentPage() { scrolled_windows.erase(scrolled_windows.begin()+page); hboxes.erase(hboxes.begin()+page); } + return true; } void Notebook::Controller::OnFileNewFile() { entry_box.clear(); @@ -444,38 +446,20 @@ std::string Notebook::Controller::OnSaveFileAs(){ return ""; } -void Notebook::Controller::AskToSaveDialog() { - INFO("AskToSaveDialog"); - DEBUG("AskToSaveDialog: Finding file path"); - Gtk::MessageDialog dialog((Gtk::Window&)(*view.get_toplevel()), "Save file!", - false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); - dialog.set_secondary_text( - "Do you want to save: " + - CurrentSourceView()->file_path+" ?"); - DEBUG("AskToSaveDialog: run dialog"); +bool Notebook::Controller::save_dialog() { + INFO("Notebook::Controller::save_dialog"); + Gtk::MessageDialog dialog((Gtk::Window&)(*view.get_toplevel()), "Save file!", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); + dialog.set_secondary_text("Do you want to save: " + CurrentSourceView()->file_path+" ?"); int result = dialog.run(); - - //Handle the response: - DEBUG("AskToSaveDialog: switch response"); - switch(result) - { - case(Gtk::RESPONSE_YES): - { - DEBUG("AskToSaveDialog: save file: yes, trying to save file"); - CurrentSourceView()->save(); - DEBUG("AskToSaveDialog: save file: yes, saved sucess"); - break; - } - case(Gtk::RESPONSE_NO): - { - DEBUG("AskToSaveDialog: save file: no"); - break; - } - default: - { - DEBUG("AskToSaveDialog: unexpected action: Default switch"); - break; - } + if(result==Gtk::RESPONSE_YES) { + CurrentSourceView()->save(); + return true; + } + else if(result==Gtk::RESPONSE_NO) { + return true; + } + else { + return false; } } diff --git a/juci/notebook.h b/juci/notebook.h index b84d983..46ce462 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -23,7 +23,7 @@ namespace Notebook { Controller(); Source::View* CurrentSourceView(); int CurrentPage(); - void OnCloseCurrentPage(); + bool close_current_page(); void OnFileNewFile(); bool OnSaveFile(std::string path); void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, @@ -47,7 +47,7 @@ namespace Notebook { std::vector > source_views; private: void CreateKeybindings(); - void AskToSaveDialog(); + bool save_dialog(); std::vector > scrolled_windows; std::vector > hboxes; diff --git a/juci/window.cc b/juci/window.cc index 19edff8..b223d8d 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -15,7 +15,7 @@ Window::Window() : add(window_box_); auto menu=Singleton::menu(); menu->action_group->add(Gtk::Action::create("FileQuit", "Quit juCi++"), Gtk::AccelKey(menu->key_map["quit"]), [this]() { - OnWindowHide(); + hide(); }); menu->action_group->add(Gtk::Action::create("FileOpenFile", "Open file"), Gtk::AccelKey(menu->key_map["open_file"]), [this]() { OnOpenFile(); @@ -24,12 +24,12 @@ Window::Window() : OnFileOpenFolder(); }); menu->action_group->add(Gtk::Action::create("FileSaveAs", "Save as"), Gtk::AccelKey(menu->key_map["save_as"]), [this]() { - SaveFileAs(); - }); + SaveFileAs(); + }); menu->action_group->add(Gtk::Action::create("FileSave", "Save"), Gtk::AccelKey(menu->key_map["save"]), [this]() { - SaveFile(); - }); + SaveFile(); + }); menu->action_group->add(Gtk::Action::create("ProjectCompileAndRun", "Compile And Run"), Gtk::AccelKey(menu->key_map["compile_and_run"]), [this]() { SaveFile(); @@ -133,12 +133,20 @@ bool Window::on_key_press_event(GdkEventKey *event) { return Gtk::Window::on_key_press_event(event); } -void Window::OnWindowHide() { - auto size=Singleton::notebook()->source_views.size(); - for(size_t c=0;cOnCloseCurrentPage(); +bool Window::on_delete_event (GdkEventAny *event) { hide(); + return true; +} + +void Window::hide() { + auto size=Singleton::notebook()->source_views.size(); + for(size_t c=0;cclose_current_page()) + return; + } + Gtk::Window::hide(); } + void Window::OnFileOpenFolder() { Gtk::FileChooserDialog dialog("Please choose a folder", Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); diff --git a/juci/window.h b/juci/window.h index a18e980..1341cca 100644 --- a/juci/window.h +++ b/juci/window.h @@ -9,19 +9,17 @@ class Window : public Gtk::Window { public: Window(); - // std::string OnSaveFileAs(); Gtk::Box window_box_; - virtual ~Window() { } - MainConfig main_config; PluginApi api; protected: bool on_key_press_event(GdkEventKey *event); + bool on_delete_event (GdkEventAny *event); private: std::mutex running; Gtk::VPaned paned_; //signal handlers - void OnWindowHide(); + void hide(); void OnOpenFile(); void OnFileOpenFolder(); bool SaveFile();