tweak layout of export dialog to try to make it clear what to enter when setting...
[ardour.git] / gtk2_ardour / export_filename_selector.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <gtkmm/messagedialog.h>
22
23 #include "export_filename_selector.h"
24
25 #include "i18n.h"
26
27 using namespace ARDOUR;
28
29 ExportFilenameSelector::ExportFilenameSelector () :
30         include_label ("", Gtk::ALIGN_LEFT),
31
32         label_label (_("Label:"), Gtk::ALIGN_LEFT),
33         session_checkbox (_("Session Name")),
34         revision_checkbox (_("Revision:")),
35
36         path_label (_("Folder:"), Gtk::ALIGN_LEFT),
37         browse_button (_("Browse")),
38
39         example_filename_label ("", Gtk::ALIGN_LEFT)
40 {
41         include_label.set_markup (_("<i>Build filename(s) from these components:</i>"));
42
43         pack_start (path_hbox, false, false, 12);
44         pack_start (include_label, false, false, 6);
45         pack_start (include_hbox, false, false, 0);
46         pack_start (example_filename_label, false, false, 12);
47
48         include_hbox.pack_start (label_label, false, false, 3);
49         include_hbox.pack_start (label_entry, false, false, 3);
50         include_hbox.pack_start (session_checkbox, false, false, 3);
51         include_hbox.pack_start (date_format_combo, false, false, 3);
52         include_hbox.pack_start (time_format_combo, false, false, 3);
53         include_hbox.pack_start (revision_checkbox, false, false, 3);
54         include_hbox.pack_start (revision_spinbutton, false, false, 3);
55
56         label_entry.set_activates_default ();
57
58         path_hbox.pack_start (path_label, false, false, 3);
59         path_hbox.pack_start (path_entry, true, true, 3);
60         path_hbox.pack_start (browse_button, false, false, 3);
61
62         path_entry.set_activates_default ();
63
64         date_format_combo.set_name ("PaddedButton");
65         time_format_combo.set_name ("PaddedButton");
66         browse_button.set_name ("PaddedButton");
67
68         label_sizegroup = Gtk::SizeGroup::create (Gtk::SIZE_GROUP_HORIZONTAL);
69         label_sizegroup->add_widget (label_label);
70         label_sizegroup->add_widget (path_label);
71
72         /* Date */
73
74         date_format_list = Gtk::ListStore::create (date_format_cols);
75         date_format_combo.set_model (date_format_list);
76         date_format_combo.pack_start (date_format_cols.label);
77
78         date_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_date_format));
79
80         /* Time */
81
82         time_format_list = Gtk::ListStore::create (time_format_cols);
83         time_format_combo.set_model (time_format_list);
84         time_format_combo.pack_start (time_format_cols.label);
85
86         time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_time_format));
87
88         /* Revision */
89
90         revision_spinbutton.set_digits (0);
91         revision_spinbutton.set_increments (1, 10);
92         revision_spinbutton.set_range (1, 1000);
93         revision_spinbutton.set_sensitive (false);
94
95         /* Signals */
96
97         label_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::update_label));
98         path_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::update_folder));
99         path_entry.signal_activate().connect (sigc::mem_fun (*this, &ExportFilenameSelector::check_folder), false);
100
101         session_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_session_selection));
102
103         revision_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_revision_selection));
104         revision_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_revision_value));
105
106         browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFilenameSelector::open_browse_dialog));
107 }
108
109 ExportFilenameSelector::~ExportFilenameSelector ()
110 {
111
112 }
113
114 void
115 ExportFilenameSelector::load_state ()
116 {
117         if (!filename) {
118                 return;
119         }
120
121         label_entry.set_text (filename->include_label ? filename->get_label() : "");
122         session_checkbox.set_active (filename->include_session);
123         revision_checkbox.set_active (filename->include_revision);
124         revision_spinbutton.set_value (filename->get_revision());
125         path_entry.set_text (filename->get_folder());
126
127         Gtk::TreeModel::Children::iterator it;
128
129         for (it = date_format_list->children().begin(); it != date_format_list->children().end(); ++it) {
130                 if (it->get_value (date_format_cols.format) == filename->get_date_format()) {
131                         date_format_combo.set_active (it);
132                 }
133         }
134
135         for (it = time_format_list->children().begin(); it != time_format_list->children().end(); ++it) {
136                 if (it->get_value (time_format_cols.format) == filename->get_time_format()) {
137                         time_format_combo.set_active (it);
138                 }
139         }
140 }
141
142 void
143 ExportFilenameSelector::set_state (ARDOUR::ExportProfileManager::FilenameStatePtr state_, ARDOUR::Session * session_)
144 {
145         SessionHandlePtr::set_session (session_);
146
147         filename = state_->filename;
148
149         /* Fill combo boxes */
150
151         Gtk::TreeModel::iterator iter;
152         Gtk::TreeModel::Row row;
153
154         /* Dates */
155
156         date_format_list->clear();
157
158         iter = date_format_list->append();
159         row = *iter;
160         row[date_format_cols.format] = ExportFilename::D_None;
161         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_None);
162
163         iter = date_format_list->append();
164         row = *iter;
165         row[date_format_cols.format] = ExportFilename::D_ISO;
166         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_ISO);
167
168         iter = date_format_list->append();
169         row = *iter;
170         row[date_format_cols.format] = ExportFilename::D_ISOShortY;
171         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_ISOShortY);
172
173         iter = date_format_list->append();
174         row = *iter;
175         row[date_format_cols.format] = ExportFilename::D_BE;
176         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_BE);
177
178         iter = date_format_list->append();
179         row = *iter;
180         row[date_format_cols.format] = ExportFilename::D_BEShortY;
181         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_BEShortY);
182
183         /* Times */
184
185         time_format_list->clear();
186
187         iter = time_format_list->append();
188         row = *iter;
189         row[time_format_cols.format] = ExportFilename::T_None;
190         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_None);
191
192         iter = time_format_list->append();
193         row = *iter;
194         row[time_format_cols.format] = ExportFilename::T_NoDelim;
195         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_NoDelim);
196
197         iter = time_format_list->append();
198         row = *iter;
199         row[time_format_cols.format] = ExportFilename::T_Delim;
200         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_Delim);
201
202         /* Load state */
203
204         load_state();
205
206 }
207
208 void
209 ExportFilenameSelector::set_example_filename (std::string filename)
210 {
211         if (filename == "") {
212                 example_filename_label.set_markup (_("<small><i>Sorry, no example filename can be shown at the moment</i></small>"));
213         } else {
214                 example_filename_label.set_markup (string_compose(_("<small><i>Current (approximate) filename: \"%1\"</i></small>"), filename));
215         }
216 }
217
218 void
219 ExportFilenameSelector::update_label ()
220 {
221         if (!filename) {
222                 return;
223         }
224
225         filename->set_label (label_entry.get_text());
226
227         filename->include_label = !label_entry.get_text().empty();
228         CriticalSelectionChanged();
229 }
230
231 void
232 ExportFilenameSelector::update_folder ()
233 {
234         if (!filename) {
235                 return;
236         }
237
238         filename->set_folder (path_entry.get_text());
239         CriticalSelectionChanged();
240 }
241
242 void
243 ExportFilenameSelector::check_folder ()
244 {
245         if (!filename) {
246                 return;
247         }
248
249         if (!Glib::file_test (path_entry.get_text(), Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
250                 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n\
251 The filename will be chosen from the information just above the folder selector."), path_entry.get_text()));
252                 msg.run ();
253                 path_entry.set_text (Glib::path_get_dirname (path_entry.get_text()));
254                 filename->set_folder (path_entry.get_text());
255                 CriticalSelectionChanged();
256         }
257 }
258
259 void
260 ExportFilenameSelector::change_date_format ()
261 {
262         if (!filename) {
263                 return;
264         }
265
266         DateFormat format = date_format_combo.get_active()->get_value (date_format_cols.format);
267         filename->set_date_format (format);
268         CriticalSelectionChanged();
269 }
270
271 void
272 ExportFilenameSelector::change_time_format ()
273 {
274         if (!filename) {
275                 return;
276         }
277
278         TimeFormat format = time_format_combo.get_active()->get_value (time_format_cols.format);
279         filename->set_time_format (format);
280         CriticalSelectionChanged();
281 }
282
283 void
284 ExportFilenameSelector::change_session_selection ()
285 {
286         if (!filename) {
287                 return;
288         }
289
290         filename->include_session = session_checkbox.get_active();
291         CriticalSelectionChanged();
292 }
293
294 void
295 ExportFilenameSelector::change_revision_selection ()
296 {
297         if (!filename) {
298                 return;
299         }
300
301         bool selected = revision_checkbox.get_active();
302         filename->include_revision = selected;
303
304         revision_spinbutton.set_sensitive (selected);
305         CriticalSelectionChanged();
306 }
307
308 void
309 ExportFilenameSelector::change_revision_value ()
310 {
311         if (!filename) {
312                 return;
313         }
314
315         filename->set_revision ((uint32_t) revision_spinbutton.get_value_as_int());
316         CriticalSelectionChanged();
317 }
318
319 void
320 ExportFilenameSelector::open_browse_dialog ()
321 {
322         Gtk::FileChooserDialog dialog(_("Choose export folder"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
323         //dialog.set_transient_for(*this);
324         dialog.set_filename (path_entry.get_text());
325
326         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
327         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
328
329         while (true) {
330                 int result = dialog.run();
331                 
332                 if (result == Gtk::RESPONSE_OK) {
333                         std::string filename = dialog.get_filename();
334                         
335                         if (!Glib::file_test (filename, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
336                                 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n\
337 The filename will be chosen from the information just above the folder selector."), filename));
338                                 msg.run ();
339                                 continue;
340                         }
341
342                         if (filename.length()) {
343                                 path_entry.set_text (filename);
344                                 break;
345                         }
346                 }
347         }
348
349         CriticalSelectionChanged();
350 }