From 08657ad37f833c9cbf46cf48a389271e1201c098 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 17 Mar 2016 11:28:08 +0100 Subject: [PATCH] Cleanup: removed notebook and window dependencies in dialogs --- src/dialogs.cc | 38 ++++++++++++++++++++++---------------- src/dialogs.h | 15 +++++++-------- src/dialogs_unix.cc | 22 +++++++++++----------- src/window.cc | 10 +++++----- 4 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/dialogs.cc b/src/dialogs.cc index 32d234b..1c83d0a 100644 --- a/src/dialogs.cc +++ b/src/dialogs.cc @@ -1,6 +1,4 @@ #include "dialogs.h" -#include "window.h" -#include "notebook.h" #include namespace sigc { @@ -18,7 +16,10 @@ namespace sigc { } Dialog::Message::Message(const std::string &text): Gtk::MessageDialog(text, false, Gtk::MessageType::MESSAGE_INFO, Gtk::ButtonsType::BUTTONS_NONE, true) { - set_transient_for(::Window::get()); + auto g_application=g_application_get_default(); + auto gio_application=Glib::wrap(g_application, true); + auto application=Glib::RefPtr::cast_static(gio_application); + set_transient_for(*application->get_active_window()); set_position(Gtk::WindowPosition::WIN_POS_CENTER_ON_PARENT); show_now(); @@ -27,23 +28,28 @@ Dialog::Message::Message(const std::string &text): Gtk::MessageDialog(text, fals g_main_context_iteration(NULL, false); } -std::string Dialog::gtk_dialog(const std::string &title, +std::string Dialog::gtk_dialog(const boost::filesystem::path &path, const std::string &title, const std::vector> &buttons, - Gtk::FileChooserAction gtk_options, - const std::string &file_name) { + Gtk::FileChooserAction gtk_options) { Gtk::FileChooserDialog dialog(title, gtk_options); - dialog.set_transient_for(Window::get()); + auto g_application=g_application_get_default(); + auto gio_application=Glib::wrap(g_application, true); + auto application=Glib::RefPtr::cast_static(gio_application); + dialog.set_transient_for(*application->get_active_window()); + dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ON_PARENT); + + if(title=="Save File As") + gtk_file_chooser_set_filename((GtkFileChooser*)dialog.gobj(), path.c_str()); + else if(!path.empty()) + gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), path.c_str()); + else { + boost::system::error_code ec; + auto current_path=boost::filesystem::current_path(ec); + if(!ec) + gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), current_path.c_str()); + } - auto current_path=Notebook::get().get_current_folder(); - boost::system::error_code ec; - if(current_path.empty()) - current_path=boost::filesystem::current_path(ec); - if(!ec) - gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), current_path.string().c_str()); - - if (!file_name.empty()) - gtk_file_chooser_set_filename((GtkFileChooser*)dialog.gobj(), file_name.c_str()); dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ON_PARENT); for (auto &button : buttons) diff --git a/src/dialogs.h b/src/dialogs.h index 68e5d19..9565dd3 100644 --- a/src/dialogs.h +++ b/src/dialogs.h @@ -7,11 +7,11 @@ class Dialog { public: - static std::string open_folder(); - static std::string open_file(); - static std::string new_file(); - static std::string new_folder(); - static std::string save_file_as(const boost::filesystem::path &file_path); + static std::string open_folder(const boost::filesystem::path &path); + static std::string open_file(const boost::filesystem::path &path); + static std::string new_file(const boost::filesystem::path &path); + static std::string new_folder(const boost::filesystem::path &path); + static std::string save_file_as(const boost::filesystem::path &path); class Message : public Gtk::MessageDialog { public: @@ -19,10 +19,9 @@ public: }; private: - static std::string gtk_dialog(const std::string &title, + static std::string gtk_dialog(const boost::filesystem::path &path, const std::string &title, const std::vector> &buttons, - Gtk::FileChooserAction gtk_options, - const std::string &file_name = ""); + Gtk::FileChooserAction gtk_options); }; #endif //JUCI_DIALOG_H_ diff --git a/src/dialogs_unix.cc b/src/dialogs_unix.cc index 04aff01..2512ce8 100644 --- a/src/dialogs_unix.cc +++ b/src/dialogs_unix.cc @@ -1,32 +1,32 @@ #include "dialogs.h" -std::string Dialog::open_folder() { - return gtk_dialog("Open Folder", +std::string Dialog::open_folder(const boost::filesystem::path &path) { + return gtk_dialog(path, "Open Folder", {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Open", Gtk::RESPONSE_OK)}, Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); } -std::string Dialog::new_file() { - return gtk_dialog("New File", +std::string Dialog::new_file(const boost::filesystem::path &path) { + return gtk_dialog(path, "New File", {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)}, Gtk::FILE_CHOOSER_ACTION_SAVE); } -std::string Dialog::new_folder() { - return gtk_dialog("New Folder", +std::string Dialog::new_folder(const boost::filesystem::path &path) { + return gtk_dialog(path, "New Folder", {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Create", Gtk::RESPONSE_OK)}, Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER); } -std::string Dialog::open_file() { - return gtk_dialog("Open File", +std::string Dialog::open_file(const boost::filesystem::path &path) { + return gtk_dialog(path, "Open File", {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Select", Gtk::RESPONSE_OK)}, Gtk::FILE_CHOOSER_ACTION_OPEN); } -std::string Dialog::save_file_as(const boost::filesystem::path &file_path) { - return gtk_dialog("Save File As", +std::string Dialog::save_file_as(const boost::filesystem::path &path) { + return gtk_dialog(path, "Save File As", {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Save", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_SAVE, file_path.string()); + Gtk::FILE_CHOOSER_ACTION_SAVE); } diff --git a/src/window.cc b/src/window.cc index 30f6e16..6a746cc 100644 --- a/src/window.cc +++ b/src/window.cc @@ -162,7 +162,7 @@ void Window::set_menu_actions() { }); menu.add_action("new_file", [this]() { - boost::filesystem::path path = Dialog::new_file(); + boost::filesystem::path path = Dialog::new_file(notebook.get_current_folder()); if(path!="") { if(boost::filesystem::exists(path)) { Terminal::get().print("Error: "+path.string()+" already exists.\n", true); @@ -181,7 +181,7 @@ void Window::set_menu_actions() { }); 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(); + boost::filesystem::path path = Dialog::new_folder(notebook.get_current_folder()); if(path!="" && boost::filesystem::exists(path)) { boost::system::error_code ec; auto last_write_time=boost::filesystem::last_write_time(path, ec); @@ -196,7 +196,7 @@ void Window::set_menu_actions() { } }); menu.add_action("new_project_cpp", [this]() { - boost::filesystem::path project_path = Dialog::new_folder(); + boost::filesystem::path project_path = Dialog::new_folder(notebook.get_current_folder()); if(project_path!="") { auto project_name=project_path.filename().string(); for(size_t c=0;c