enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / export_format_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 "export_format_selector.h"
22
23 #include "export_format_dialog.h"
24
25 #include "ardour/export_format_specification.h"
26 #include "ardour/export_profile_manager.h"
27
28 #include "pbd/i18n.h"
29
30 ExportFormatSelector::ExportFormatSelector () :
31   edit_button (Gtk::Stock::EDIT),
32   remove_button (Gtk::Stock::REMOVE),
33   new_button (Gtk::Stock::NEW)
34 {
35         pack_start (format_combo, true, true, 0);
36         pack_start (edit_button, false, false, 3);
37         pack_start (remove_button, false, false, 3);
38         pack_start (new_button, false, false, 3);
39
40         edit_button.signal_clicked().connect (sigc::hide_return (sigc::bind (sigc::mem_fun (*this, &ExportFormatSelector::open_edit_dialog), false)));
41         remove_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatSelector::remove_format), true));
42         new_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFormatSelector::add_new_format));
43
44         /* Format combo */
45
46         format_list = Gtk::ListStore::create (format_cols);
47         format_list->set_sort_column (format_cols.label, Gtk::SORT_ASCENDING);
48         format_combo.set_model (format_list);
49         format_combo.pack_start (format_cols.label);
50         format_combo.set_active (0);
51
52         format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatSelector::update_format_combo));
53 }
54
55 ExportFormatSelector::~ExportFormatSelector ()
56 {
57
58 }
59
60 void
61 ExportFormatSelector::set_state (ARDOUR::ExportProfileManager::FormatStatePtr const state_, ARDOUR::Session * session_)
62 {
63         SessionHandlePtr::set_session (session_);
64
65         state = state_;
66
67         update_format_list ();
68 }
69
70 void
71 ExportFormatSelector::update_format_list ()
72 {
73         FormatPtr format_to_select = state->format;
74         format_list->clear();
75
76         if (state->list->empty()) {
77                 edit_button.set_sensitive (false);
78                 remove_button.set_sensitive (false);
79                 return;
80         } else {
81                 edit_button.set_sensitive (true);
82                 remove_button.set_sensitive (true);
83         }
84
85         Gtk::ListStore::iterator tree_it;
86
87         for (FormatList::const_iterator it = state->list->begin(); it != state->list->end(); ++it) {
88                 tree_it = format_list->append();
89                 tree_it->set_value (format_cols.format, *it);
90                 tree_it->set_value (format_cols.label, (*it)->description());
91         }
92
93         if (format_combo.get_active_row_number() == -1 && format_combo.get_model()->children().size() > 0) {
94                 format_combo.set_active (0);
95         }
96
97         select_format (format_to_select);
98 }
99
100 void
101 ExportFormatSelector::select_format (FormatPtr f)
102 {
103         Gtk::TreeModel::Children::iterator it;
104         for (it = format_list->children().begin(); it != format_list->children().end(); ++it) {
105                 if (it->get_value (format_cols.format) == f) {
106                         format_combo.set_active (it);
107                         break;
108                 }
109         }
110
111         CriticalSelectionChanged();
112 }
113
114 void
115 ExportFormatSelector::add_new_format ()
116 {
117         FormatPtr new_format = state->format = NewFormat (state->format);
118
119         if (open_edit_dialog (true) != Gtk::RESPONSE_APPLY) {
120                 remove_format(false);
121                 if (state->list->empty()) {
122                         state->format.reset ();
123                 }
124         }
125 }
126
127 void
128 ExportFormatSelector::remove_format (bool called_from_button)
129 {
130         if (called_from_button) {
131                 Gtk::MessageDialog dialog (_("Do you really want to remove the format?"),
132                                 false,
133                                 Gtk::MESSAGE_QUESTION,
134                                 Gtk::BUTTONS_YES_NO);
135
136                 if (Gtk::RESPONSE_YES != dialog.run ()) {
137                         /* User has selected "no" or closed the dialog, better
138                          * abort
139                          */
140                         return;
141                 }
142         }
143
144         FormatPtr remove;
145         Gtk::TreeModel::iterator it = format_combo.get_active();
146         remove = it->get_value (format_cols.format);
147
148         FormatRemoved (remove);
149 }
150
151 int
152 ExportFormatSelector::open_edit_dialog (bool new_dialog)
153 {
154         ExportFormatDialog dialog (state->format, new_dialog);
155         dialog.set_session (_session);
156         Gtk::ResponseType response = (Gtk::ResponseType) dialog.run();
157         if (response == Gtk::RESPONSE_APPLY) {
158                 update_format_description ();
159                 FormatEdited (state->format);
160                 CriticalSelectionChanged();
161         }
162         return response;
163 }
164
165 void
166 ExportFormatSelector::update_format_combo ()
167 {
168         Gtk::TreeModel::iterator it = format_combo.get_active();
169         if (format_list->iter_is_valid (it)) {
170                 state->format = it->get_value(format_cols.format);
171         } else if (!format_list->children().empty()) {
172                 format_combo.set_active (0);
173         } else {
174                 edit_button.set_sensitive (false);
175                 remove_button.set_sensitive (false);
176         }
177
178         CriticalSelectionChanged();
179 }
180
181 void
182 ExportFormatSelector::update_format_description ()
183 {
184         format_combo.get_active()->set_value(format_cols.label, state->format->description());
185 }