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