Browse Source

Cleanup: removed notebook and window dependencies in dialogs

merge-requests/365/head
eidheim 10 years ago
parent
commit
08657ad37f
  1. 38
      src/dialogs.cc
  2. 15
      src/dialogs.h
  3. 22
      src/dialogs_unix.cc
  4. 10
      src/window.cc

38
src/dialogs.cc

@ -1,6 +1,4 @@
#include "dialogs.h" #include "dialogs.h"
#include "window.h"
#include "notebook.h"
#include <cmath> #include <cmath>
namespace sigc { 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) { 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<Gtk::Application>::cast_static(gio_application);
set_transient_for(*application->get_active_window());
set_position(Gtk::WindowPosition::WIN_POS_CENTER_ON_PARENT); set_position(Gtk::WindowPosition::WIN_POS_CENTER_ON_PARENT);
show_now(); show_now();
@ -27,23 +28,28 @@ Dialog::Message::Message(const std::string &text): Gtk::MessageDialog(text, fals
g_main_context_iteration(NULL, false); 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<std::pair<std::string, Gtk::ResponseType>> &buttons, const std::vector<std::pair<std::string, Gtk::ResponseType>> &buttons,
Gtk::FileChooserAction gtk_options, Gtk::FileChooserAction gtk_options) {
const std::string &file_name) {
Gtk::FileChooserDialog dialog(title, 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<Gtk::Application>::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); dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ON_PARENT);
for (auto &button : buttons) for (auto &button : buttons)

15
src/dialogs.h

@ -7,11 +7,11 @@
class Dialog { class Dialog {
public: public:
static std::string open_folder(); static std::string open_folder(const boost::filesystem::path &path);
static std::string open_file(); static std::string open_file(const boost::filesystem::path &path);
static std::string new_file(); static std::string new_file(const boost::filesystem::path &path);
static std::string new_folder(); static std::string new_folder(const boost::filesystem::path &path);
static std::string save_file_as(const boost::filesystem::path &file_path); static std::string save_file_as(const boost::filesystem::path &path);
class Message : public Gtk::MessageDialog { class Message : public Gtk::MessageDialog {
public: public:
@ -19,10 +19,9 @@ public:
}; };
private: 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<std::pair<std::string, Gtk::ResponseType>> &buttons, const std::vector<std::pair<std::string, Gtk::ResponseType>> &buttons,
Gtk::FileChooserAction gtk_options, Gtk::FileChooserAction gtk_options);
const std::string &file_name = "");
}; };
#endif //JUCI_DIALOG_H_ #endif //JUCI_DIALOG_H_

22
src/dialogs_unix.cc

@ -1,32 +1,32 @@
#include "dialogs.h" #include "dialogs.h"
std::string Dialog::open_folder() { std::string Dialog::open_folder(const boost::filesystem::path &path) {
return gtk_dialog("Open Folder", return gtk_dialog(path, "Open Folder",
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Open", Gtk::RESPONSE_OK)}, {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Open", Gtk::RESPONSE_OK)},
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
} }
std::string Dialog::new_file() { std::string Dialog::new_file(const boost::filesystem::path &path) {
return gtk_dialog("New File", return gtk_dialog(path, "New File",
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)}, {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)},
Gtk::FILE_CHOOSER_ACTION_SAVE); Gtk::FILE_CHOOSER_ACTION_SAVE);
} }
std::string Dialog::new_folder() { std::string Dialog::new_folder(const boost::filesystem::path &path) {
return gtk_dialog("New Folder", return gtk_dialog(path, "New Folder",
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Create", Gtk::RESPONSE_OK)}, {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Create", Gtk::RESPONSE_OK)},
Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER); Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER);
} }
std::string Dialog::open_file() { std::string Dialog::open_file(const boost::filesystem::path &path) {
return gtk_dialog("Open File", return gtk_dialog(path, "Open File",
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Select", Gtk::RESPONSE_OK)}, {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Select", Gtk::RESPONSE_OK)},
Gtk::FILE_CHOOSER_ACTION_OPEN); Gtk::FILE_CHOOSER_ACTION_OPEN);
} }
std::string Dialog::save_file_as(const boost::filesystem::path &file_path) { std::string Dialog::save_file_as(const boost::filesystem::path &path) {
return gtk_dialog("Save File As", return gtk_dialog(path, "Save File As",
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Save", Gtk::RESPONSE_OK)}, {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);
} }

10
src/window.cc

@ -162,7 +162,7 @@ void Window::set_menu_actions() {
}); });
menu.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(notebook.get_current_folder());
if(path!="") { if(path!="") {
if(boost::filesystem::exists(path)) { if(boost::filesystem::exists(path)) {
Terminal::get().print("Error: "+path.string()+" already exists.\n", true); 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]() { menu.add_action("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(notebook.get_current_folder());
if(path!="" && boost::filesystem::exists(path)) { if(path!="" && boost::filesystem::exists(path)) {
boost::system::error_code ec; boost::system::error_code ec;
auto last_write_time=boost::filesystem::last_write_time(path, 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]() { 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!="") { if(project_path!="") {
auto project_name=project_path.filename().string(); auto project_name=project_path.filename().string();
for(size_t c=0;c<project_name.size();c++) { for(size_t c=0;c<project_name.size();c++) {
@ -228,12 +228,12 @@ void Window::set_menu_actions() {
}); });
menu.add_action("open_file", [this]() { menu.add_action("open_file", [this]() {
auto path=Dialog::open_file(); auto path=Dialog::open_file(notebook.get_current_folder());
if(path!="") if(path!="")
notebook.open(path); notebook.open(path);
}); });
menu.add_action("open_folder", [this]() { menu.add_action("open_folder", [this]() {
auto path = Dialog::open_folder(); auto path = Dialog::open_folder(notebook.get_current_folder());
if (path!="" && boost::filesystem::exists(path)) if (path!="" && boost::filesystem::exists(path))
Directories::get().open(path); Directories::get().open(path);
}); });

Loading…
Cancel
Save