From aac51b73ee9f7a9a885dca363b734e5cfd279963 Mon Sep 17 00:00:00 2001 From: oyvang Date: Mon, 20 Apr 2015 08:45:39 +0200 Subject: [PATCH] Fixed save and save as --- juci/config.json | 1 + juci/menu.xml | 1 + juci/notebook.cc | 65 ++++++++++++++++++++++++++++++++++++++++++------ juci/notebook.h | 5 ++-- juci/source.cc | 2 ++ juci/source.h | 7 ++++++ juci/window.cc | 37 ++++++++------------------- juci/window.h | 3 ++- 8 files changed, 83 insertions(+), 38 deletions(-) diff --git a/juci/config.json b/juci/config.json index 03cbad8..3c68b32 100644 --- a/juci/config.json +++ b/juci/config.json @@ -24,6 +24,7 @@ "new_cc_file": "c", "close_tab": "w", "open_folder": "o", + "save": "s", "save_as": "s", "compile_and_run": "r>", "compile": "r" diff --git a/juci/menu.xml b/juci/menu.xml index 7007147..acb0ed2 100644 --- a/juci/menu.xml +++ b/juci/menu.xml @@ -8,6 +8,7 @@ + diff --git a/juci/notebook.cc b/juci/notebook.cc index a2d0d93..766c9d8 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -13,17 +13,24 @@ Notebook::View::View() { view_.set_position(120); } -Notebook::Controller::Controller(Gtk::Window& window, Keybindings::Controller& keybindings, +Notebook::Controller::Controller(Gtk::Window* window, Keybindings::Controller& keybindings, Source::Config& source_cfg, Directories::Config& dir_cfg) : source_config_(source_cfg), directories_(dir_cfg) { - window_ = &window; + std::cout << "NOTEBOOK CONTROLLER"<< std::endl; + window_ = window; + std::cout << "1"<< std::endl; OnNewPage("juCi++"); + std::cout << "2"<< std::endl; refClipboard_ = Gtk::Clipboard::get(); + std::cout << "3"<< std::endl; ispopup = false; + std::cout << "4"<< std::endl; view().pack1(directories_.widget(), true, true); - CreateKeybindings(keybindings); + std::cout << "5"<< std::endl; + CreateKeybindings(keybindings); + std::cout << "FERDIG"<< std::endl; } // Constructor @@ -232,6 +239,7 @@ void Notebook::Controller::OnNewPage(std::string name) { void Notebook::Controller::OnOpenFile(std::string path) { OnCreatePage(); text_vec_.back()->OnOpenFile(path); + text_vec_.back()->set_is_saved(true); unsigned pos = path.find_last_of("/\\"); Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); Notebook().show_all_children(); @@ -519,9 +527,50 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, } } -void Notebook::Controller:: OnSaveFile(std::string path){ - std::ofstream file; - file.open (path); - file << CurrentTextView().get_buffer()->get_text(); - file.close(); +void Notebook::Controller:: OnSaveFile() { + if (text_vec_.at(CurrentPage())->is_saved()) { + std::ofstream file; + file.open (text_vec_.at(CurrentPage())->path()); + file << CurrentTextView().get_buffer()->get_text(); + file.close(); + } else { + std::string path = OnSaveFileAs(); + if (path != "") { + std::ofstream file; + file.open (path); + file << CurrentTextView().get_buffer()->get_text(); + file.close(); + text_vec_.at(CurrentPage())->set_path(path); + text_vec_.at(CurrentPage())->set_is_saved(true); + } + } +} + + +std::string Notebook::Controller::OnSaveFileAs(){ + Gtk::FileChooserDialog dialog("Please choose a file", + Gtk::FILE_CHOOSER_ACTION_SAVE); + dialog.set_transient_for(*window_); + dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS); + dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL); + dialog.add_button("_Save", Gtk::RESPONSE_OK); + int result = dialog.run(); + switch (result) { + case(Gtk::RESPONSE_OK): { + std::string path = dialog.get_filename(); + unsigned pos = path.find_last_of("/\\"); + std::cout << path<< std::endl; + //notebook_.OnSaveFile(path); + return path; + break; + } + case(Gtk::RESPONSE_CANCEL): { + break; + } + default: { + std::cout << "Unexpected button clicked." << std::endl; + break; + } + } + return ""; } diff --git a/juci/notebook.h b/juci/notebook.h index 5eac6b0..3022aae 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -29,7 +29,7 @@ namespace Notebook { }; class Controller { public: - Controller(Gtk::Window& window, Keybindings::Controller& keybindings, + Controller(Gtk::Window* window, Keybindings::Controller& keybindings, Source::Config& config, Directories::Config& dir_cfg); ~Controller(); @@ -49,7 +49,7 @@ namespace Notebook { void OnFileNewEmptyfile(); void OnFileNewHeaderFile(); void OnFileOpenFolder(); - void OnSaveFile(std::string path); + void OnSaveFile(); void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); void OnNewPage(std::string name); @@ -64,6 +64,7 @@ namespace Notebook { const Source::Config& source_config() { return source_config_; } bool OnMouseRelease(GdkEventButton* button); bool OnKeyRelease(GdkEventKey* key); + std::string OnSaveFileAs(); protected: void TextViewHandlers(Gtk::TextView& textview); void PopupSelectHandler(Gtk::Dialog &popup, diff --git a/juci/source.cc b/juci/source.cc index baec800..32b84f1 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -282,6 +282,7 @@ void Source::Controller::OnNewEmptyFile() { model().set_file_path(filename); model().set_project_path(filename); s.save(""); + //OnOpenFile(filename); //OYVANG ADDED; REMOVE IF WRONG } string extract_file_path(const std::string &file_path) { @@ -334,6 +335,7 @@ void Source::View::OnUpdateSyntax(const std::vector &ranges, } void Source::Controller::OnOpenFile(const string &filepath) { + path_=filepath; sourcefile s(filepath); buffer()->set_text(s.get_content()); int start_offset = buffer()->begin().get_offset(); diff --git a/juci/source.h b/juci/source.h index 8450807..6975bbd 100644 --- a/juci/source.h +++ b/juci/source.h @@ -129,13 +129,20 @@ namespace Source { int column, std::vector *suggestions); Glib::RefPtr buffer(); + bool is_saved(){return is_saved_;}; + std::string path(){return path_;}; + void set_is_saved(bool isSaved){is_saved_ = isSaved;}; + void set_path(std::string path){path_ = path;}; + private: void OnLineEdit(); void OnSaveFile(); std::mutex syntax; std::mutex parsing; + std::string path_; bool go = false; + bool is_saved_ = false; protected: View view_; diff --git a/juci/window.cc b/juci/window.cc index 61033ae..5177995 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -4,7 +4,7 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), main_config_(), keybindings_(main_config_.keybindings_cfg()), - notebook_(*this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()), + notebook_(this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()), menu_(keybindings()) { set_title("juCi++"); set_default_size(600, 400); @@ -32,7 +32,15 @@ Window::Window() : Gtk::AccelKey(keybindings_.config_ .key_map()["save_as"]), [this]() { - OnSaveFileAs(); + notebook_.OnSaveFile(); + }); + + keybindings_.action_group_menu()->add(Gtk::Action::create("FileSave", + "Save"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["save"]), + [this]() { + notebook_.OnSaveFile(); }); keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompileAndRun", @@ -165,31 +173,6 @@ void Window::OnOpenFile() { } } } -void Window::OnSaveFileAs(){ - Gtk::FileChooserDialog dialog("Please choose a file", - Gtk::FILE_CHOOSER_ACTION_SAVE); - dialog.set_transient_for(*this); - dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS); - dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL); - dialog.add_button("_Save", Gtk::RESPONSE_OK); - int result = dialog.run(); - switch (result) { - case(Gtk::RESPONSE_OK): { - std::string path = dialog.get_filename(); - unsigned pos = path.find_last_of("/\\"); - std::cout << path<< std::endl; - notebook_.OnSaveFile(path); - break; - } - case(Gtk::RESPONSE_CANCEL): { - break; - } - default: { - std::cout << "Unexpected button clicked." << std::endl; - break; - } - } -} bool Window::OnMouseRelease(GdkEventButton *button){ return notebook_.OnMouseRelease(button); } diff --git a/juci/window.h b/juci/window.h index 954072e..3e968dc 100644 --- a/juci/window.h +++ b/juci/window.h @@ -11,6 +11,7 @@ class Window : public Gtk::Window { public: Window(); MainConfig& main_config() { return main_config_; } + // std::string OnSaveFileAs(); Gtk::Box window_box_; @@ -28,7 +29,7 @@ public: void OnWindowHide(); void OnOpenFile(); void OnFileOpenFolder(); - void OnSaveFileAs(); + bool OnMouseRelease(GdkEventButton* button); };