1 #include "export_file_chooser.hpp"
2 #include "util/util.hpp"
3
4 namespace horizon {
ExportFileChooser()5 ExportFileChooser::ExportFileChooser()
6 {
7 }
~ExportFileChooser()8 ExportFileChooser::~ExportFileChooser()
9 {
10 }
11
attach(Gtk::Entry * en,Gtk::Button * bu,Gtk::Window * w)12 void ExportFileChooser::attach(Gtk::Entry *en, Gtk::Button *bu, Gtk::Window *w)
13 {
14 if (entry)
15 return;
16 entry = en;
17 button = bu;
18 window = w;
19
20 button->signal_clicked().connect([this] {
21 const char *title = "Select output file name";
22 if (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) {
23 title = "Select output folder";
24 }
25 GtkFileChooserNative *native =
26 gtk_file_chooser_native_new(title, GTK_WINDOW(window->gobj()), action, "Select", "_Cancel");
27 auto chooser = Glib::wrap(GTK_FILE_CHOOSER(native));
28 prepare_chooser(chooser);
29 chooser->add_shortcut_folder(project_dir);
30 if (entry->get_text().size()) {
31 if (Glib::path_is_absolute(entry->get_text()))
32 chooser->set_filename(entry->get_text());
33 else
34 chooser->set_filename(Glib::build_filename(project_dir, entry->get_text()));
35 }
36 else {
37 chooser->set_current_folder(project_dir);
38 }
39 if (gtk_native_dialog_run(GTK_NATIVE_DIALOG(native)) == GTK_RESPONSE_ACCEPT) {
40 std::string filename = chooser->get_filename();
41 prepare_filename(filename);
42 auto fi = Gio::File::create_for_path(filename);
43 auto pf = Gio::File::create_for_path(project_dir);
44 if (fi->has_prefix(pf)) {
45 std::string p = pf->get_relative_path(fi);
46 replace_backslash(p);
47 entry->set_text(p);
48 }
49 else {
50 entry->set_text(filename);
51 }
52
53 s_signal_changed.emit();
54 }
55 });
56
57 entry->signal_changed().connect([this] {
58 if (filename_bound) {
59 *filename_bound = entry->get_text();
60 s_signal_changed.emit();
61 }
62 });
63 }
64
prepare_chooser(Glib::RefPtr<Gtk::FileChooser> chooser)65 void ExportFileChooser::prepare_chooser(Glib::RefPtr<Gtk::FileChooser> chooser)
66 {
67 }
68
prepare_filename(std::string & filename)69 void ExportFileChooser::prepare_filename(std::string &filename)
70 {
71 }
72
set_project_dir(const std::string & d)73 void ExportFileChooser::set_project_dir(const std::string &d)
74 {
75 project_dir = d;
76 }
77
get_project_dir() const78 const std::string &ExportFileChooser::get_project_dir() const
79 {
80 return project_dir;
81 }
82
get_filename_abs(const std::string & filename)83 std::string ExportFileChooser::get_filename_abs(const std::string &filename)
84 {
85 if (Glib::path_is_absolute(filename)) {
86 return filename;
87 }
88 else {
89 return Glib::build_filename(project_dir, filename);
90 }
91 }
92
get_filename_abs()93 std::string ExportFileChooser::get_filename_abs()
94 {
95 return get_filename_abs(get_filename());
96 }
97
get_filename()98 std::string ExportFileChooser::get_filename()
99 {
100 return entry->get_text();
101 }
102
bind_filename(std::string & filename)103 void ExportFileChooser::bind_filename(std::string &filename)
104 {
105 filename_bound = nullptr;
106 entry->set_text(filename);
107 filename_bound = &filename;
108 }
109
set_action(GtkFileChooserAction a)110 void ExportFileChooser::set_action(GtkFileChooserAction a)
111 {
112 action = a;
113 }
114
115 } // namespace horizon
116