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