789b7eb435f948e11c5a4cb470e5010f0a52280c
[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 "ardour/export_handler.h"
26 #include "ardour/session.h"
27 #include "ardour/session_directory.h"
28
29 #include "i18n.h"
30
31 using namespace ARDOUR;
32
33 ExportFilenameSelector::ExportFilenameSelector () :
34         include_label (_("Include in Filename(s):"), Gtk::ALIGN_LEFT),
35
36         label_label (_("Label:"), Gtk::ALIGN_LEFT),
37         session_checkbox (_("Session Name")),
38         revision_checkbox (_("Revision:")),
39
40         path_label (_("Folder:"), Gtk::ALIGN_LEFT),
41         browse_button (_("Browse"))
42 {
43         pack_start (include_label, false, false, 6);
44         pack_start (include_hbox, false, false, 0);
45         pack_start (path_hbox, false, false, 12);
46
47         include_hbox.pack_start (label_label, false, false, 3);
48         include_hbox.pack_start (label_entry, false, false, 3);
49         include_hbox.pack_start (session_checkbox, false, false, 3);
50         include_hbox.pack_start (date_format_combo, false, false, 3);
51         include_hbox.pack_start (time_format_combo, false, false, 3);
52         include_hbox.pack_start (revision_checkbox, false, false, 3);
53         include_hbox.pack_start (revision_spinbutton, false, false, 3);
54
55         label_entry.set_activates_default ();
56
57         path_hbox.pack_start (path_label, false, false, 3);
58         path_hbox.pack_start (path_entry, true, true, 3);
59         path_hbox.pack_start (browse_button, false, false, 3);
60
61         path_entry.set_activates_default ();
62
63         date_format_combo.set_name ("PaddedButton");
64         time_format_combo.set_name ("PaddedButton");
65         browse_button.set_name ("PaddedButton");
66
67         label_sizegroup = Gtk::SizeGroup::create (Gtk::SIZE_GROUP_HORIZONTAL);
68         label_sizegroup->add_widget (label_label);
69         label_sizegroup->add_widget (path_label);
70
71         /* Date */
72
73         date_format_list = Gtk::ListStore::create (date_format_cols);
74         date_format_combo.set_model (date_format_list);
75         date_format_combo.pack_start (date_format_cols.label);
76
77         date_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_date_format));
78
79         /* Time */
80
81         time_format_list = Gtk::ListStore::create (time_format_cols);
82         time_format_combo.set_model (time_format_list);
83         time_format_combo.pack_start (time_format_cols.label);
84
85         time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_time_format));
86
87         /* Revision */
88
89         revision_spinbutton.set_digits (0);
90         revision_spinbutton.set_increments (1, 10);
91         revision_spinbutton.set_range (1, 1000);
92         revision_spinbutton.set_sensitive (false);
93
94         /* Signals */
95
96         label_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::update_label));
97         path_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::update_folder));
98         path_entry.signal_activate().connect (sigc::mem_fun (*this, &ExportFilenameSelector::check_folder), false);
99
100         session_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_session_selection));
101
102         revision_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_revision_selection));
103         revision_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_revision_value));
104
105         browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFilenameSelector::open_browse_dialog));
106 }
107
108 ExportFilenameSelector::~ExportFilenameSelector ()
109 {
110
111 }
112
113 void
114 ExportFilenameSelector::load_state ()
115 {
116         if (!filename) {
117                 return;
118         }
119
120         label_entry.set_text (filename->include_label ? filename->get_label() : "");
121         session_checkbox.set_active (filename->include_session);
122         revision_checkbox.set_active (filename->include_revision);
123         revision_spinbutton.set_value (filename->get_revision());
124         path_entry.set_text (filename->get_folder());
125
126         Gtk::TreeModel::Children::iterator it;
127
128         for (it = date_format_list->children().begin(); it != date_format_list->children().end(); ++it) {
129                 if (it->get_value (date_format_cols.format) == filename->get_date_format()) {
130                         date_format_combo.set_active (it);
131                 }
132         }
133
134         for (it = time_format_list->children().begin(); it != time_format_list->children().end(); ++it) {
135                 if (it->get_value (time_format_cols.format) == filename->get_time_format()) {
136                         time_format_combo.set_active (it);
137                 }
138         }
139 }
140
141 void
142 ExportFilenameSelector::set_state (ARDOUR::ExportProfileManager::FilenameStatePtr state_, ARDOUR::Session * session_)
143 {
144         SessionHandlePtr::set_session (session_);
145
146         filename = state_->filename;
147
148         /* Fill combo boxes */
149
150         Gtk::TreeModel::iterator iter;
151         Gtk::TreeModel::Row row;
152
153         /* Dates */
154
155         date_format_list->clear();
156
157         iter = date_format_list->append();
158         row = *iter;
159         row[date_format_cols.format] = ExportFilename::D_None;
160         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_None);
161
162         iter = date_format_list->append();
163         row = *iter;
164         row[date_format_cols.format] = ExportFilename::D_ISO;
165         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_ISO);
166
167         iter = date_format_list->append();
168         row = *iter;
169         row[date_format_cols.format] = ExportFilename::D_ISOShortY;
170         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_ISOShortY);
171
172         iter = date_format_list->append();
173         row = *iter;
174         row[date_format_cols.format] = ExportFilename::D_BE;
175         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_BE);
176
177         iter = date_format_list->append();
178         row = *iter;
179         row[date_format_cols.format] = ExportFilename::D_BEShortY;
180         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_BEShortY);
181
182         /* Times */
183
184         time_format_list->clear();
185
186         iter = time_format_list->append();
187         row = *iter;
188         row[time_format_cols.format] = ExportFilename::T_None;
189         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_None);
190
191         iter = time_format_list->append();
192         row = *iter;
193         row[time_format_cols.format] = ExportFilename::T_NoDelim;
194         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_NoDelim);
195
196         iter = time_format_list->append();
197         row = *iter;
198         row[time_format_cols.format] = ExportFilename::T_Delim;
199         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_Delim);
200
201         /* Load state */
202
203         load_state();
204
205 }
206
207 void
208 ExportFilenameSelector::update_label ()
209 {
210         if (!filename) {
211                 return;
212         }
213
214         filename->set_label (label_entry.get_text());
215
216         filename->include_label = !label_entry.get_text().empty();
217         CriticalSelectionChanged();
218 }
219
220 void
221 ExportFilenameSelector::update_folder ()
222 {
223         if (!filename) {
224                 return;
225         }
226
227         filename->set_folder (path_entry.get_text());
228         CriticalSelectionChanged();
229 }
230
231 void
232 ExportFilenameSelector::check_folder ()
233 {
234         if (!filename) {
235                 return;
236         }
237
238         if (!Glib::file_test (path_entry.get_text(), Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
239                 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n\
240 The filename will be chosen from the information just above the folder selector."), path_entry.get_text()));
241                 msg.run ();
242                 path_entry.set_text (Glib::path_get_dirname (path_entry.get_text()));
243                 filename->set_folder (path_entry.get_text());
244                 CriticalSelectionChanged();
245         }
246 }
247
248 void
249 ExportFilenameSelector::change_date_format ()
250 {
251         if (!filename) {
252                 return;
253         }
254
255         DateFormat format = date_format_combo.get_active()->get_value (date_format_cols.format);
256         filename->set_date_format (format);
257         CriticalSelectionChanged();
258 }
259
260 void
261 ExportFilenameSelector::change_time_format ()
262 {
263         if (!filename) {
264                 return;
265         }
266
267         TimeFormat format = time_format_combo.get_active()->get_value (time_format_cols.format);
268         filename->set_time_format (format);
269         CriticalSelectionChanged();
270 }
271
272 void
273 ExportFilenameSelector::change_session_selection ()
274 {
275         if (!filename) {
276                 return;
277         }
278
279         filename->include_session = session_checkbox.get_active();
280         CriticalSelectionChanged();
281 }
282
283 void
284 ExportFilenameSelector::change_revision_selection ()
285 {
286         if (!filename) {
287                 return;
288         }
289
290         bool selected = revision_checkbox.get_active();
291         filename->include_revision = selected;
292
293         revision_spinbutton.set_sensitive (selected);
294         CriticalSelectionChanged();
295 }
296
297 void
298 ExportFilenameSelector::change_revision_value ()
299 {
300         if (!filename) {
301                 return;
302         }
303
304         filename->set_revision ((uint32_t) revision_spinbutton.get_value_as_int());
305         CriticalSelectionChanged();
306 }
307
308 void
309 ExportFilenameSelector::open_browse_dialog ()
310 {
311         Gtk::FileChooserDialog dialog(_("Choose export folder"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
312         //dialog.set_transient_for(*this);
313         dialog.set_filename (path_entry.get_text());
314
315         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
316         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
317
318         while (true) {
319                 int result = dialog.run();
320                 
321                 if (result == Gtk::RESPONSE_OK) {
322                         std::string filename = dialog.get_filename();
323                         
324                         if (!Glib::file_test (filename, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
325                                 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n\
326 The filename will be chosen from the information just above the folder selector."), filename));
327                                 msg.run ();
328                                 continue;
329                         }
330
331                         if (filename.length()) {
332                                 path_entry.set_text (filename);
333                                 break;
334                         }
335                 }
336         }
337
338         CriticalSelectionChanged();
339 }