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