Fix MIDI disk-writer flush
[ardour.git] / gtk2_ardour / export_format_selector.cc
1 /*
2  * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2008 Sakari Bergen <sakari.bergen@beatwaves.net>
4  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
5  * Copyright (C) 2017-2018 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #include <gtkmm/messagedialog.h>
22 #include <gtkmm/stock.h>
23
24 #include "ardour/export_format_specification.h"
25 #include "ardour/export_profile_manager.h"
26
27 #include "export_format_selector.h"
28 #include "export_format_dialog.h"
29
30 #include "pbd/i18n.h"
31
32 ExportFormatSelector::ExportFormatSelector () :
33   edit_button (Gtk::Stock::EDIT),
34   remove_button (Gtk::Stock::REMOVE),
35   new_button (Gtk::Stock::NEW)
36 {
37         pack_start (format_combo, true, true, 0);
38         pack_start (edit_button, false, false, 3);
39         pack_start (remove_button, false, false, 3);
40         pack_start (new_button, false, false, 3);
41
42         edit_button.signal_clicked().connect (sigc::hide_return (sigc::bind (sigc::mem_fun (*this, &ExportFormatSelector::open_edit_dialog), false)));
43         remove_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatSelector::remove_format), true));
44         new_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFormatSelector::add_new_format));
45
46         /* Format combo */
47
48         format_list = Gtk::ListStore::create (format_cols);
49         format_list->set_sort_column (format_cols.label, Gtk::SORT_ASCENDING);
50         format_combo.set_model (format_list);
51         format_combo.pack_start (format_cols.label);
52         format_combo.set_active (0);
53
54         format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatSelector::update_format_combo));
55 }
56
57 ExportFormatSelector::~ExportFormatSelector ()
58 {
59
60 }
61
62 void
63 ExportFormatSelector::set_state (ARDOUR::ExportProfileManager::FormatStatePtr const state_, ARDOUR::Session * session_)
64 {
65         SessionHandlePtr::set_session (session_);
66
67         state = state_;
68
69         update_format_list ();
70 }
71
72 void
73 ExportFormatSelector::update_format_list ()
74 {
75         FormatPtr format_to_select = state->format;
76         format_list->clear();
77
78         if (state->list->empty()) {
79                 edit_button.set_sensitive (false);
80                 remove_button.set_sensitive (false);
81                 return;
82         } else {
83                 edit_button.set_sensitive (true);
84                 remove_button.set_sensitive (true);
85         }
86
87         Gtk::ListStore::iterator tree_it;
88
89         for (FormatList::const_iterator it = state->list->begin(); it != state->list->end(); ++it) {
90                 tree_it = format_list->append();
91                 tree_it->set_value (format_cols.format, *it);
92                 tree_it->set_value (format_cols.label, (*it)->description());
93         }
94
95         if (format_combo.get_active_row_number() == -1 && format_combo.get_model()->children().size() > 0) {
96                 format_combo.set_active (0);
97         }
98
99         select_format (format_to_select);
100 }
101
102 void
103 ExportFormatSelector::select_format (FormatPtr f)
104 {
105         Gtk::TreeModel::Children::iterator it;
106         for (it = format_list->children().begin(); it != format_list->children().end(); ++it) {
107                 if (it->get_value (format_cols.format) == f) {
108                         format_combo.set_active (it);
109                         break;
110                 }
111         }
112
113         CriticalSelectionChanged();
114 }
115
116 void
117 ExportFormatSelector::add_new_format ()
118 {
119         FormatPtr new_format = state->format = NewFormat (state->format);
120
121         if (open_edit_dialog (true) != Gtk::RESPONSE_APPLY) {
122                 remove_format(false);
123                 if (state->list->empty()) {
124                         state->format.reset ();
125                 }
126         }
127 }
128
129 void
130 ExportFormatSelector::remove_format (bool called_from_button)
131 {
132         if (called_from_button) {
133                 Gtk::MessageDialog dialog (_("Do you really want to remove the format?"),
134                                 false,
135                                 Gtk::MESSAGE_QUESTION,
136                                 Gtk::BUTTONS_YES_NO);
137
138                 if (Gtk::RESPONSE_YES != dialog.run ()) {
139                         /* User has selected "no" or closed the dialog, better
140                          * abort
141                          */
142                         return;
143                 }
144         }
145
146         FormatPtr remove;
147         Gtk::TreeModel::iterator it = format_combo.get_active();
148         remove = it->get_value (format_cols.format);
149
150         FormatRemoved (remove);
151 }
152
153 int
154 ExportFormatSelector::open_edit_dialog (bool new_dialog)
155 {
156         ExportFormatDialog dialog (state->format, new_dialog);
157         dialog.set_session (_session);
158         Gtk::ResponseType response = (Gtk::ResponseType) dialog.run();
159         if (response == Gtk::RESPONSE_APPLY) {
160                 update_format_description ();
161                 FormatEdited (state->format);
162                 CriticalSelectionChanged();
163         } else {
164                 FormatReverted (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 }