1 |
/* |
2 |
* Copyright (C) 2006, 2007 Andreas Persson |
3 |
* |
4 |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License as |
6 |
* published by the Free Software Foundation; either version 2, or (at |
7 |
* your option) any later version. |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, but |
10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
* General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with program; see the file COPYING. If not, write to the Free |
16 |
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
17 |
* 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include <libintl.h> |
21 |
#include <iostream> |
22 |
|
23 |
#include <gtkmm/filechooserdialog.h> |
24 |
#include <gtkmm/messagedialog.h> |
25 |
#include <gtkmm/stock.h> |
26 |
#include <gtkmm/targetentry.h> |
27 |
#include <gtkmm/main.h> |
28 |
|
29 |
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 6) || GTKMM_MAJOR_VERSION > 2 |
30 |
#define ABOUT_DIALOG |
31 |
#include <gtkmm/aboutdialog.h> |
32 |
#endif |
33 |
|
34 |
#include <stdio.h> |
35 |
#include <sndfile.h> |
36 |
|
37 |
#include "mainwindow.h" |
38 |
|
39 |
#define _(String) gettext(String) |
40 |
|
41 |
template<class T> inline std::string ToString(T o) { |
42 |
std::stringstream ss; |
43 |
ss << o; |
44 |
return ss.str(); |
45 |
} |
46 |
|
47 |
MainWindow::MainWindow() |
48 |
{ |
49 |
// set_border_width(5); |
50 |
// set_default_size(400, 200); |
51 |
|
52 |
|
53 |
add(m_VBox); |
54 |
|
55 |
// Handle selection |
56 |
Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection(); |
57 |
tree_sel_ref->signal_changed().connect( |
58 |
sigc::mem_fun(*this, &MainWindow::on_sel_change)); |
59 |
|
60 |
// m_TreeView.set_reorderable(); |
61 |
|
62 |
m_TreeView.signal_button_press_event().connect_notify( |
63 |
sigc::mem_fun(*this, &MainWindow::on_button_release)); |
64 |
|
65 |
// Add the TreeView tab, inside a ScrolledWindow, with the button underneath: |
66 |
m_ScrolledWindow.add(m_TreeView); |
67 |
// m_ScrolledWindow.set_size_request(200, 600); |
68 |
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
69 |
|
70 |
m_ScrolledWindowSamples.add(m_TreeViewSamples); |
71 |
m_ScrolledWindowSamples.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
72 |
|
73 |
|
74 |
m_TreeViewNotebook.set_size_request(300); |
75 |
|
76 |
m_HPaned.add1(m_TreeViewNotebook); |
77 |
m_HPaned.add2(dimreg_edit); |
78 |
|
79 |
|
80 |
m_TreeViewNotebook.append_page(m_ScrolledWindowSamples, "Samples"); |
81 |
m_TreeViewNotebook.append_page(m_ScrolledWindow, "Instruments"); |
82 |
|
83 |
|
84 |
actionGroup = Gtk::ActionGroup::create(); |
85 |
|
86 |
actionGroup->add(Gtk::Action::create("MenuFile", _("_File"))); |
87 |
actionGroup->add(Gtk::Action::create("New", Gtk::Stock::NEW), |
88 |
sigc::mem_fun( |
89 |
*this, &MainWindow::on_action_file_new)); |
90 |
Glib::RefPtr<Gtk::Action> action = |
91 |
Gtk::Action::create("Open", Gtk::Stock::OPEN); |
92 |
action->property_label() = action->property_label() + "..."; |
93 |
actionGroup->add(action, |
94 |
sigc::mem_fun( |
95 |
*this, &MainWindow::on_action_file_open)); |
96 |
actionGroup->add(Gtk::Action::create("Save", Gtk::Stock::SAVE), |
97 |
sigc::mem_fun( |
98 |
*this, &MainWindow::on_action_file_save)); |
99 |
action = Gtk::Action::create("SaveAs", Gtk::Stock::SAVE_AS); |
100 |
action->property_label() = action->property_label() + "..."; |
101 |
actionGroup->add(action, |
102 |
Gtk::AccelKey("<shift><control>s"), |
103 |
sigc::mem_fun( |
104 |
*this, &MainWindow::on_action_file_save_as)); |
105 |
actionGroup->add(Gtk::Action::create("Properties", |
106 |
Gtk::Stock::PROPERTIES), |
107 |
sigc::mem_fun( |
108 |
*this, &MainWindow::on_action_file_properties)); |
109 |
actionGroup->add(Gtk::Action::create("InstrProperties", |
110 |
Gtk::Stock::PROPERTIES), |
111 |
sigc::mem_fun( |
112 |
*this, &MainWindow::show_instr_props)); |
113 |
actionGroup->add(Gtk::Action::create("Quit", Gtk::Stock::QUIT), |
114 |
sigc::mem_fun( |
115 |
*this, &MainWindow::on_action_quit)); |
116 |
actionGroup->add(Gtk::Action::create("MenuInstrument", _("_Instrument"))); |
117 |
|
118 |
action = Gtk::Action::create("MenuHelp", Gtk::Stock::HELP); |
119 |
actionGroup->add(Gtk::Action::create("MenuHelp", |
120 |
action->property_label())); |
121 |
#ifdef ABOUT_DIALOG |
122 |
actionGroup->add(Gtk::Action::create("About", Gtk::Stock::ABOUT), |
123 |
sigc::mem_fun( |
124 |
*this, &MainWindow::on_action_help_about)); |
125 |
#endif |
126 |
actionGroup->add( |
127 |
Gtk::Action::create("AddInstrument", _("Add _Instrument")), |
128 |
sigc::mem_fun(*this, &MainWindow::on_action_add_instrument) |
129 |
); |
130 |
actionGroup->add( |
131 |
Gtk::Action::create("RemoveInstrument", Gtk::Stock::REMOVE), |
132 |
sigc::mem_fun(*this, &MainWindow::on_action_remove_instrument) |
133 |
); |
134 |
|
135 |
// sample right-click popup actions |
136 |
actionGroup->add( |
137 |
Gtk::Action::create("SampleProperties", Gtk::Stock::PROPERTIES), |
138 |
sigc::mem_fun(*this, &MainWindow::on_action_sample_properties) |
139 |
); |
140 |
actionGroup->add( |
141 |
Gtk::Action::create("AddGroup", _("Add _Group")), |
142 |
sigc::mem_fun(*this, &MainWindow::on_action_add_group) |
143 |
); |
144 |
actionGroup->add( |
145 |
Gtk::Action::create("AddSample", _("Add _Sample(s)")), |
146 |
sigc::mem_fun(*this, &MainWindow::on_action_add_sample) |
147 |
); |
148 |
actionGroup->add( |
149 |
Gtk::Action::create("RemoveSample", Gtk::Stock::REMOVE), |
150 |
sigc::mem_fun(*this, &MainWindow::on_action_remove_sample) |
151 |
); |
152 |
|
153 |
uiManager = Gtk::UIManager::create(); |
154 |
uiManager->insert_action_group(actionGroup); |
155 |
add_accel_group(uiManager->get_accel_group()); |
156 |
|
157 |
Glib::ustring ui_info = |
158 |
"<ui>" |
159 |
" <menubar name='MenuBar'>" |
160 |
" <menu action='MenuFile'>" |
161 |
" <menuitem action='New'/>" |
162 |
" <menuitem action='Open'/>" |
163 |
" <separator/>" |
164 |
" <menuitem action='Save'/>" |
165 |
" <menuitem action='SaveAs'/>" |
166 |
" <separator/>" |
167 |
" <menuitem action='Properties'/>" |
168 |
" <separator/>" |
169 |
" <menuitem action='Quit'/>" |
170 |
" </menu>" |
171 |
" <menu action='MenuInstrument'>" |
172 |
" </menu>" |
173 |
#ifdef ABOUT_DIALOG |
174 |
" <menu action='MenuHelp'>" |
175 |
" <menuitem action='About'/>" |
176 |
" </menu>" |
177 |
#endif |
178 |
" </menubar>" |
179 |
" <popup name='PopupMenu'>" |
180 |
" <menuitem action='InstrProperties'/>" |
181 |
" <menuitem action='AddInstrument'/>" |
182 |
" <separator/>" |
183 |
" <menuitem action='RemoveInstrument'/>" |
184 |
" </popup>" |
185 |
" <popup name='SamplePopupMenu'>" |
186 |
" <menuitem action='SampleProperties'/>" |
187 |
" <menuitem action='AddGroup'/>" |
188 |
" <menuitem action='AddSample'/>" |
189 |
" <separator/>" |
190 |
" <menuitem action='RemoveSample'/>" |
191 |
" </popup>" |
192 |
"</ui>"; |
193 |
uiManager->add_ui_from_string(ui_info); |
194 |
|
195 |
popup_menu = dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/PopupMenu")); |
196 |
|
197 |
Gtk::Widget* menuBar = uiManager->get_widget("/MenuBar"); |
198 |
m_VBox.pack_start(*menuBar, Gtk::PACK_SHRINK); |
199 |
m_VBox.pack_start(m_HPaned); |
200 |
m_VBox.pack_start(m_RegionChooser, Gtk::PACK_SHRINK); |
201 |
m_VBox.pack_start(m_DimRegionChooser, Gtk::PACK_SHRINK); |
202 |
|
203 |
m_RegionChooser.signal_region_selected().connect( |
204 |
sigc::mem_fun(*this, &MainWindow::region_changed) ); |
205 |
m_DimRegionChooser.signal_dimregion_selected().connect( |
206 |
sigc::mem_fun(*this, &MainWindow::dimreg_changed) ); |
207 |
|
208 |
|
209 |
// Create the Tree model: |
210 |
m_refTreeModel = Gtk::ListStore::create(m_Columns); |
211 |
m_TreeView.set_model(m_refTreeModel); |
212 |
m_refTreeModel->signal_row_changed().connect( |
213 |
sigc::mem_fun(*this, &MainWindow::instrument_name_changed) |
214 |
); |
215 |
|
216 |
// Add the TreeView's view columns: |
217 |
m_TreeView.append_column_editable("Instrument", m_Columns.m_col_name); |
218 |
m_TreeView.set_headers_visible(false); |
219 |
|
220 |
// create samples treeview (including its data model) |
221 |
m_refSamplesTreeModel = SamplesTreeStore::create(m_SamplesModel); |
222 |
m_TreeViewSamples.set_model(m_refSamplesTreeModel); |
223 |
// m_TreeViewSamples.set_reorderable(); |
224 |
m_TreeViewSamples.append_column_editable("Samples", m_SamplesModel.m_col_name); |
225 |
m_TreeViewSamples.set_headers_visible(false); |
226 |
m_TreeViewSamples.signal_button_press_event().connect_notify( |
227 |
sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release) |
228 |
); |
229 |
m_refSamplesTreeModel->signal_row_changed().connect( |
230 |
sigc::mem_fun(*this, &MainWindow::sample_name_changed) |
231 |
); |
232 |
|
233 |
// establish drag&drop between samples tree view and dimension region 'Sample' text entry |
234 |
std::list<Gtk::TargetEntry> drag_target_gig_sample; |
235 |
drag_target_gig_sample.push_back( Gtk::TargetEntry("gig::Sample") ); |
236 |
m_TreeViewSamples.drag_source_set(drag_target_gig_sample); |
237 |
m_TreeViewSamples.signal_drag_data_get().connect( |
238 |
sigc::mem_fun(*this, &MainWindow::on_sample_treeview_drag_data_get) |
239 |
); |
240 |
dimreg_edit.wSample->drag_dest_set(drag_target_gig_sample); |
241 |
dimreg_edit.wSample->signal_drag_data_received().connect( |
242 |
sigc::mem_fun(*this, &MainWindow::on_sample_label_drop_drag_data_received) |
243 |
); |
244 |
dimreg_edit.signal_dimreg_changed().connect( |
245 |
sigc::mem_fun(*this, &MainWindow::file_changed)); |
246 |
m_RegionChooser.signal_instrument_changed().connect( |
247 |
sigc::mem_fun(*this, &MainWindow::file_changed)); |
248 |
m_DimRegionChooser.signal_region_changed().connect( |
249 |
sigc::mem_fun(*this, &MainWindow::file_changed)); |
250 |
instrumentProps.signal_instrument_changed().connect( |
251 |
sigc::mem_fun(*this, &MainWindow::file_changed)); |
252 |
file = 0; |
253 |
file_is_changed = false; |
254 |
|
255 |
show_all_children(); |
256 |
} |
257 |
|
258 |
MainWindow::~MainWindow() |
259 |
{ |
260 |
} |
261 |
|
262 |
bool MainWindow::on_delete_event(GdkEventAny* event) |
263 |
{ |
264 |
return file_is_changed && !close_confirmation_dialog(); |
265 |
} |
266 |
|
267 |
void MainWindow::on_action_quit() |
268 |
{ |
269 |
if (file_is_changed && !close_confirmation_dialog()) return; |
270 |
hide(); |
271 |
} |
272 |
|
273 |
void MainWindow::region_changed() |
274 |
{ |
275 |
m_DimRegionChooser.set_region(m_RegionChooser.get_region()); |
276 |
} |
277 |
|
278 |
void MainWindow::dimreg_changed() |
279 |
{ |
280 |
dimreg_edit.set_dim_region(m_DimRegionChooser.get_dimregion()); |
281 |
} |
282 |
|
283 |
void MainWindow::on_sel_change() |
284 |
{ |
285 |
Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection(); |
286 |
|
287 |
Gtk::TreeModel::iterator it = tree_sel_ref->get_selected(); |
288 |
if (it) { |
289 |
Gtk::TreeModel::Row row = *it; |
290 |
std::cout << row[m_Columns.m_col_name] << std::endl; |
291 |
|
292 |
m_RegionChooser.set_instrument(row[m_Columns.m_col_instr]); |
293 |
} else { |
294 |
m_RegionChooser.set_instrument(0); |
295 |
} |
296 |
} |
297 |
|
298 |
void loader_progress_callback(gig::progress_t* progress) |
299 |
{ |
300 |
Loader* loader = static_cast<Loader*>(progress->custom); |
301 |
loader->progress_callback(progress->factor); |
302 |
} |
303 |
|
304 |
void Loader::progress_callback(float fraction) |
305 |
{ |
306 |
{ |
307 |
Glib::Mutex::Lock lock(progressMutex); |
308 |
progress = fraction; |
309 |
} |
310 |
progress_dispatcher(); |
311 |
} |
312 |
|
313 |
void Loader::thread_function() |
314 |
{ |
315 |
printf("thread_function self=%x\n", Glib::Thread::self()); |
316 |
printf("Start %s\n", filename); |
317 |
RIFF::File* riff = new RIFF::File(filename); |
318 |
gig = new gig::File(riff); |
319 |
gig::progress_t progress; |
320 |
progress.callback = loader_progress_callback; |
321 |
progress.custom = this; |
322 |
|
323 |
gig->GetInstrument(0, &progress); |
324 |
printf("End\n"); |
325 |
finished_dispatcher(); |
326 |
} |
327 |
|
328 |
Loader::Loader(const char* filename) |
329 |
: thread(0), filename(filename) |
330 |
{ |
331 |
} |
332 |
|
333 |
void Loader::launch() |
334 |
{ |
335 |
thread = Glib::Thread::create(sigc::mem_fun(*this, &Loader::thread_function), true); |
336 |
printf("launch thread=%x\n", thread); |
337 |
} |
338 |
|
339 |
float Loader::get_progress() |
340 |
{ |
341 |
float res; |
342 |
{ |
343 |
Glib::Mutex::Lock lock(progressMutex); |
344 |
res = progress; |
345 |
} |
346 |
return res; |
347 |
} |
348 |
|
349 |
Glib::Dispatcher& Loader::signal_progress() |
350 |
{ |
351 |
return progress_dispatcher; |
352 |
} |
353 |
|
354 |
Glib::Dispatcher& Loader::signal_finished() |
355 |
{ |
356 |
return finished_dispatcher; |
357 |
} |
358 |
|
359 |
LoadDialog::LoadDialog(const Glib::ustring& title, Gtk::Window& parent) |
360 |
: Gtk::Dialog(title, parent, true) |
361 |
{ |
362 |
get_vbox()->pack_start(progressBar); |
363 |
show_all_children(); |
364 |
} |
365 |
|
366 |
// Clear all GUI elements / controls. This method is typically called |
367 |
// before a new .gig file is to be created or to be loaded. |
368 |
void MainWindow::__clear() { |
369 |
// remove all entries from "Instrument" menu |
370 |
Gtk::MenuItem* instrument_menu = |
371 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument")); |
372 |
instrument_menu->hide(); |
373 |
for (int i = 0; i < instrument_menu->get_submenu()->items().size(); i++) { |
374 |
delete &instrument_menu->get_submenu()->items()[i]; |
375 |
} |
376 |
instrument_menu->get_submenu()->items().clear(); |
377 |
// forget all samples that ought to be imported |
378 |
m_SampleImportQueue.clear(); |
379 |
// clear the samples and instruments tree views |
380 |
m_refTreeModel->clear(); |
381 |
m_refSamplesTreeModel->clear(); |
382 |
// free libgig's gig::File instance |
383 |
if (file) { |
384 |
delete file; |
385 |
file = NULL; |
386 |
} |
387 |
} |
388 |
|
389 |
void MainWindow::on_action_file_new() |
390 |
{ |
391 |
if (file_is_changed && !close_confirmation_dialog()) return; |
392 |
|
393 |
// clear all GUI elements |
394 |
__clear(); |
395 |
// create a new .gig file (virtually yet) |
396 |
gig::File* pFile = new gig::File; |
397 |
// already add one new instrument by default |
398 |
gig::Instrument* pInstrument = pFile->AddInstrument(); |
399 |
pInstrument->pInfo->Name = "Unnamed Instrument"; |
400 |
// update GUI with that new gig::File |
401 |
load_gig(pFile, 0 /*no file name yet*/); |
402 |
} |
403 |
|
404 |
bool MainWindow::close_confirmation_dialog() |
405 |
{ |
406 |
gchar* msg = g_strdup_printf(_("Save changes to \"%s\" before closing?"), |
407 |
Glib::filename_display_basename(filename).c_str()); |
408 |
Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); |
409 |
g_free(msg); |
410 |
dialog.set_secondary_text(_("If you close without saving, your changes will be lost.")); |
411 |
dialog.add_button(_("Close _Without Saving"), Gtk::RESPONSE_NO); |
412 |
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
413 |
dialog.add_button(file_has_name ? Gtk::Stock::SAVE : Gtk::Stock::SAVE_AS, Gtk::RESPONSE_YES); |
414 |
dialog.set_default_response(Gtk::RESPONSE_YES); |
415 |
int response = dialog.run(); |
416 |
if (response == Gtk::RESPONSE_YES) return file_save(); |
417 |
return response != Gtk::RESPONSE_CANCEL; |
418 |
} |
419 |
|
420 |
void MainWindow::on_action_file_open() |
421 |
{ |
422 |
if (file_is_changed && !close_confirmation_dialog()) return; |
423 |
|
424 |
Gtk::FileChooserDialog dialog(*this, _("Open file")); |
425 |
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
426 |
dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); |
427 |
dialog.set_default_response(Gtk::RESPONSE_OK); |
428 |
Gtk::FileFilter filter; |
429 |
filter.add_pattern("*.gig"); |
430 |
dialog.set_filter(filter); |
431 |
if (current_dir != "") { |
432 |
dialog.set_current_folder(current_dir); |
433 |
} |
434 |
if (dialog.run() == Gtk::RESPONSE_OK) { |
435 |
std::string filename = dialog.get_filename(); |
436 |
printf("filename=%s\n", filename.c_str()); |
437 |
__clear(); |
438 |
printf("on_action_file_open self=%x\n", Glib::Thread::self()); |
439 |
load_file(filename.c_str()); |
440 |
current_dir = Glib::path_get_dirname(filename); |
441 |
} |
442 |
} |
443 |
|
444 |
void MainWindow::load_file(const char* name) |
445 |
{ |
446 |
load_dialog = new LoadDialog("Loading...", *this); |
447 |
load_dialog->show_all(); |
448 |
loader = new Loader(strdup(name)); |
449 |
loader->signal_progress().connect( |
450 |
sigc::mem_fun(*this, &MainWindow::on_loader_progress)); |
451 |
loader->signal_finished().connect( |
452 |
sigc::mem_fun(*this, &MainWindow::on_loader_finished)); |
453 |
loader->launch(); |
454 |
} |
455 |
|
456 |
void MainWindow::load_instrument(gig::Instrument* instr) { |
457 |
if (!instr) { |
458 |
Glib::ustring txt = "Provided instrument is NULL!\n"; |
459 |
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); |
460 |
msg.run(); |
461 |
Gtk::Main::quit(); |
462 |
} |
463 |
gig::File* pFile = (gig::File*) instr->GetParent(); |
464 |
load_gig(pFile, 0 /*file name*/); |
465 |
//TODO: automatically select the given instrument |
466 |
} |
467 |
|
468 |
void MainWindow::on_loader_progress() |
469 |
{ |
470 |
load_dialog->set_fraction(loader->get_progress()); |
471 |
} |
472 |
|
473 |
void MainWindow::on_loader_finished() |
474 |
{ |
475 |
printf("Loader finished!\n"); |
476 |
printf("on_loader_finished self=%x\n", Glib::Thread::self()); |
477 |
load_gig(loader->gig, loader->filename); |
478 |
load_dialog->hide(); |
479 |
} |
480 |
|
481 |
void MainWindow::on_action_file_save() |
482 |
{ |
483 |
file_save(); |
484 |
} |
485 |
|
486 |
bool MainWindow::file_save() |
487 |
{ |
488 |
if (!file) return false; |
489 |
if (!file_has_name) return file_save_as(); |
490 |
|
491 |
std::cout << "Saving file\n" << std::flush; |
492 |
try { |
493 |
file->Save(); |
494 |
if (file_is_changed) { |
495 |
set_title(get_title().substr(1)); |
496 |
file_is_changed = false; |
497 |
} |
498 |
} catch (RIFF::Exception e) { |
499 |
Glib::ustring txt = "Could not save file: " + e.Message; |
500 |
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); |
501 |
msg.run(); |
502 |
return false; |
503 |
} |
504 |
std::cout << "Saving file done\n" << std::flush; |
505 |
__import_queued_samples(); |
506 |
return true; |
507 |
} |
508 |
|
509 |
void MainWindow::on_action_file_save_as() |
510 |
{ |
511 |
file_save_as(); |
512 |
} |
513 |
|
514 |
bool MainWindow::file_save_as() |
515 |
{ |
516 |
if (!file) return false; |
517 |
Gtk::FileChooserDialog dialog(*this, _("Save as"), Gtk::FILE_CHOOSER_ACTION_SAVE); |
518 |
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
519 |
dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); |
520 |
dialog.set_default_response(Gtk::RESPONSE_OK); |
521 |
|
522 |
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 8) || GTKMM_MAJOR_VERSION > 2 |
523 |
dialog.set_do_overwrite_confirmation(); |
524 |
// TODO: an overwrite dialog for gtkmm < 2.8 |
525 |
#endif |
526 |
Gtk::FileFilter filter; |
527 |
filter.add_pattern("*.gig"); |
528 |
dialog.set_filter(filter); |
529 |
|
530 |
if (Glib::path_is_absolute(filename)) { |
531 |
dialog.set_filename(filename); |
532 |
} else if (current_dir != "") { |
533 |
dialog.set_current_folder(current_dir); |
534 |
} |
535 |
dialog.set_current_name(Glib::filename_display_basename(filename)); |
536 |
|
537 |
if (dialog.run() == Gtk::RESPONSE_OK) { |
538 |
try { |
539 |
std::string filename = dialog.get_filename(); |
540 |
if (!Glib::str_has_suffix(filename, ".gig")) { |
541 |
filename += ".gig"; |
542 |
} |
543 |
printf("filename=%s\n", filename.c_str()); |
544 |
file->Save(filename); |
545 |
this->filename = filename; |
546 |
current_dir = Glib::path_get_dirname(filename); |
547 |
set_title(Glib::filename_display_basename(filename)); |
548 |
file_has_name = true; |
549 |
file_is_changed = false; |
550 |
} catch (RIFF::Exception e) { |
551 |
Glib::ustring txt = "Could not save file: " + e.Message; |
552 |
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); |
553 |
msg.run(); |
554 |
return false; |
555 |
} |
556 |
__import_queued_samples(); |
557 |
return true; |
558 |
} |
559 |
return false; |
560 |
} |
561 |
|
562 |
// actually write the sample(s)' data to the gig file |
563 |
void MainWindow::__import_queued_samples() { |
564 |
std::cout << "Starting sample import\n" << std::flush; |
565 |
Glib::ustring error_files; |
566 |
printf("Samples to import: %d\n", m_SampleImportQueue.size()); |
567 |
for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin(); |
568 |
iter != m_SampleImportQueue.end(); ) { |
569 |
printf("Importing sample %s\n",(*iter).sample_path.c_str()); |
570 |
SF_INFO info; |
571 |
info.format = 0; |
572 |
SNDFILE* hFile = sf_open((*iter).sample_path.c_str(), SFM_READ, &info); |
573 |
try { |
574 |
if (!hFile) throw std::string("could not open file"); |
575 |
// determine sample's bit depth |
576 |
int bitdepth; |
577 |
switch (info.format & 0xff) { |
578 |
case SF_FORMAT_PCM_S8: |
579 |
bitdepth = 16; // we simply convert to 16 bit for now |
580 |
break; |
581 |
case SF_FORMAT_PCM_16: |
582 |
bitdepth = 16; |
583 |
break; |
584 |
case SF_FORMAT_PCM_24: |
585 |
bitdepth = 32; // we simply convert to 32 bit for now |
586 |
break; |
587 |
case SF_FORMAT_PCM_32: |
588 |
bitdepth = 32; |
589 |
break; |
590 |
case SF_FORMAT_PCM_U8: |
591 |
bitdepth = 16; // we simply convert to 16 bit for now |
592 |
break; |
593 |
case SF_FORMAT_FLOAT: |
594 |
bitdepth = 32; |
595 |
break; |
596 |
case SF_FORMAT_DOUBLE: |
597 |
bitdepth = 32; // I guess we will always truncate this to 32 bit |
598 |
break; |
599 |
default: |
600 |
sf_close(hFile); // close sound file |
601 |
throw std::string("format not supported"); // unsupported subformat (yet?) |
602 |
} |
603 |
// allocate appropriate copy buffer (TODO: for now we copy |
604 |
// it in one piece, might be tough for very long samples) |
605 |
// and copy sample data into buffer |
606 |
int8_t* buffer = NULL; |
607 |
switch (bitdepth) { |
608 |
case 16: |
609 |
buffer = new int8_t[2 * info.channels * info.frames]; |
610 |
// libsndfile does the conversion for us (if needed) |
611 |
sf_readf_short(hFile, (short*) buffer, info.frames); |
612 |
break; |
613 |
case 32: |
614 |
buffer = new int8_t[4 * info.channels * info.frames]; |
615 |
// libsndfile does the conversion for us (if needed) |
616 |
sf_readf_int(hFile, (int*) buffer, info.frames); |
617 |
break; |
618 |
} |
619 |
// write from buffer directly (physically) into .gig file |
620 |
(*iter).gig_sample->Write(buffer, info.frames); |
621 |
// cleanup |
622 |
sf_close(hFile); |
623 |
delete[] buffer; |
624 |
// on success we remove the sample from the import queue, |
625 |
// otherwise keep it, maybe it works the next time ? |
626 |
std::list<SampleImportItem>::iterator cur = iter; |
627 |
++iter; |
628 |
m_SampleImportQueue.erase(cur); |
629 |
} catch (std::string what) { |
630 |
// remember the files that made trouble (and their cause) |
631 |
if (error_files.size()) error_files += "\n"; |
632 |
error_files += (*iter).sample_path += " (" + what + ")"; |
633 |
++iter; |
634 |
} |
635 |
} |
636 |
// show error message box when some sample(s) could not be imported |
637 |
if (error_files.size()) { |
638 |
Glib::ustring txt = "Could not import the following sample(s):\n" + error_files; |
639 |
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); |
640 |
msg.run(); |
641 |
} |
642 |
} |
643 |
|
644 |
void MainWindow::on_action_file_properties() |
645 |
{ |
646 |
propDialog.show(); |
647 |
propDialog.deiconify(); |
648 |
} |
649 |
|
650 |
void MainWindow::on_action_help_about() |
651 |
{ |
652 |
#ifdef ABOUT_DIALOG |
653 |
Gtk::AboutDialog dialog; |
654 |
dialog.set_version(VERSION); |
655 |
dialog.run(); |
656 |
#endif |
657 |
} |
658 |
|
659 |
PropDialog::PropDialog() |
660 |
: table(2,1) |
661 |
{ |
662 |
table.set_col_spacings(5); |
663 |
const char* propLabels[] = { |
664 |
"Name:", |
665 |
"CreationDate:", |
666 |
"Comments:", // TODO: multiline |
667 |
"Product:", |
668 |
"Copyright:", |
669 |
"Artists:", |
670 |
"Genre:", |
671 |
"Keywords:", |
672 |
"Engineer:", |
673 |
"Technician:", |
674 |
"Software:", // TODO: readonly |
675 |
"Medium:", |
676 |
"Source:", |
677 |
"SourceForm:", |
678 |
"Commissioned:", |
679 |
"Subject:" |
680 |
}; |
681 |
for (int i = 0 ; i < sizeof(propLabels) / sizeof(char*) ; i++) { |
682 |
label[i].set_text(propLabels[i]); |
683 |
label[i].set_alignment(Gtk::ALIGN_LEFT); |
684 |
table.attach(label[i], 0, 1, i, i + 1, Gtk::FILL, Gtk::SHRINK); |
685 |
table.attach(entry[i], 1, 2, i, i + 1, Gtk::FILL | Gtk::EXPAND, |
686 |
Gtk::SHRINK); |
687 |
} |
688 |
|
689 |
add(table); |
690 |
// add_button(Gtk::Stock::CANCEL, 0); |
691 |
// add_button(Gtk::Stock::OK, 1); |
692 |
show_all_children(); |
693 |
} |
694 |
|
695 |
void PropDialog::set_info(DLS::Info* info) |
696 |
{ |
697 |
entry[0].set_text(info->Name); |
698 |
entry[1].set_text(info->CreationDate); |
699 |
entry[2].set_text(Glib::convert(info->Comments, "UTF-8", "ISO-8859-1")); |
700 |
entry[3].set_text(info->Product); |
701 |
entry[4].set_text(info->Copyright); |
702 |
entry[5].set_text(info->Artists); |
703 |
entry[6].set_text(info->Genre); |
704 |
entry[7].set_text(info->Keywords); |
705 |
entry[8].set_text(info->Engineer); |
706 |
entry[9].set_text(info->Technician); |
707 |
entry[10].set_text(info->Software); |
708 |
entry[11].set_text(info->Medium); |
709 |
entry[12].set_text(info->Source); |
710 |
entry[13].set_text(info->SourceForm); |
711 |
entry[14].set_text(info->Commissioned); |
712 |
entry[15].set_text(info->Subject); |
713 |
} |
714 |
|
715 |
void InstrumentProps::add_prop(BoolEntry& boolentry) |
716 |
{ |
717 |
table.attach(boolentry.widget, 0, 2, rowno, rowno + 1, |
718 |
Gtk::FILL, Gtk::SHRINK); |
719 |
rowno++; |
720 |
boolentry.signal_changed_by_user().connect(instrument_changed.make_slot()); |
721 |
} |
722 |
|
723 |
void InstrumentProps::add_prop(BoolEntryPlus6& boolentry) |
724 |
{ |
725 |
table.attach(boolentry.widget, 0, 2, rowno, rowno + 1, |
726 |
Gtk::FILL, Gtk::SHRINK); |
727 |
rowno++; |
728 |
boolentry.signal_changed_by_user().connect(instrument_changed.make_slot()); |
729 |
} |
730 |
|
731 |
void InstrumentProps::add_prop(LabelWidget& prop) |
732 |
{ |
733 |
table.attach(prop.label, 0, 1, rowno, rowno + 1, |
734 |
Gtk::FILL, Gtk::SHRINK); |
735 |
table.attach(prop.widget, 1, 2, rowno, rowno + 1, |
736 |
Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK); |
737 |
rowno++; |
738 |
prop.signal_changed_by_user().connect(instrument_changed.make_slot()); |
739 |
} |
740 |
|
741 |
InstrumentProps::InstrumentProps() |
742 |
: table(2,1), |
743 |
quitButton(Gtk::Stock::CLOSE), |
744 |
eName("Name"), |
745 |
eIsDrum("Is drum"), |
746 |
eMIDIBank("MIDI bank", 0, 16383), |
747 |
eMIDIProgram("MIDI program"), |
748 |
eAttenuation("Attenuation", 0, 96, 0, 1), |
749 |
eGainPlus6("Gain +6dB", eAttenuation, -6), |
750 |
eEffectSend("Effect send", 0, 65535), |
751 |
eFineTune("Fine tune", -8400, 8400), |
752 |
ePitchbendRange("Pitchbend range", 0, 12), |
753 |
ePianoReleaseMode("Piano release mode"), |
754 |
eDimensionKeyRangeLow("Dimension key range low"), |
755 |
eDimensionKeyRangeHigh("Dimension key range high") |
756 |
{ |
757 |
set_title("Instrument properties"); |
758 |
|
759 |
rowno = 0; |
760 |
table.set_col_spacings(5); |
761 |
|
762 |
add_prop(eName); |
763 |
add_prop(eIsDrum); |
764 |
add_prop(eMIDIBank); |
765 |
add_prop(eMIDIProgram); |
766 |
add_prop(eAttenuation); |
767 |
add_prop(eGainPlus6); |
768 |
add_prop(eEffectSend); |
769 |
add_prop(eFineTune); |
770 |
add_prop(ePitchbendRange); |
771 |
add_prop(ePianoReleaseMode); |
772 |
add_prop(eDimensionKeyRangeLow); |
773 |
add_prop(eDimensionKeyRangeHigh); |
774 |
|
775 |
eDimensionKeyRangeLow.signal_changed_by_user().connect( |
776 |
sigc::mem_fun(*this, &InstrumentProps::key_range_low_changed)); |
777 |
eDimensionKeyRangeHigh.signal_changed_by_user().connect( |
778 |
sigc::mem_fun(*this, &InstrumentProps::key_range_high_changed)); |
779 |
|
780 |
add(vbox); |
781 |
table.set_border_width(5); |
782 |
vbox.pack_start(table); |
783 |
table.show(); |
784 |
vbox.pack_start(buttonBox, Gtk::PACK_SHRINK); |
785 |
buttonBox.set_layout(Gtk::BUTTONBOX_END); |
786 |
buttonBox.set_border_width(5); |
787 |
buttonBox.show(); |
788 |
buttonBox.pack_start(quitButton); |
789 |
quitButton.set_flags(Gtk::CAN_DEFAULT); |
790 |
quitButton.grab_focus(); |
791 |
|
792 |
quitButton.signal_clicked().connect( |
793 |
sigc::mem_fun(*this, &InstrumentProps::hide)); |
794 |
|
795 |
quitButton.show(); |
796 |
vbox.show(); |
797 |
show_all_children(); |
798 |
} |
799 |
|
800 |
void InstrumentProps::set_instrument(gig::Instrument* instrument) |
801 |
{ |
802 |
eName.set_ptr(&instrument->pInfo->Name); |
803 |
eIsDrum.set_ptr(&instrument->IsDrum); |
804 |
eMIDIBank.set_ptr(&instrument->MIDIBank); |
805 |
eMIDIProgram.set_ptr(&instrument->MIDIProgram); |
806 |
eAttenuation.set_ptr(&instrument->Attenuation); |
807 |
eGainPlus6.set_ptr(&instrument->Attenuation); |
808 |
eEffectSend.set_ptr(&instrument->EffectSend); |
809 |
eFineTune.set_ptr(&instrument->FineTune); |
810 |
ePitchbendRange.set_ptr(&instrument->PitchbendRange); |
811 |
ePianoReleaseMode.set_ptr(&instrument->PianoReleaseMode); |
812 |
eDimensionKeyRangeLow.set_ptr(0); |
813 |
eDimensionKeyRangeHigh.set_ptr(0); |
814 |
eDimensionKeyRangeLow.set_ptr(&instrument->DimensionKeyRange.low); |
815 |
eDimensionKeyRangeHigh.set_ptr(&instrument->DimensionKeyRange.high); |
816 |
} |
817 |
|
818 |
void InstrumentProps::key_range_low_changed() |
819 |
{ |
820 |
double l = eDimensionKeyRangeLow.get_value(); |
821 |
double h = eDimensionKeyRangeHigh.get_value(); |
822 |
if (h < l) eDimensionKeyRangeHigh.set_value(l); |
823 |
} |
824 |
|
825 |
void InstrumentProps::key_range_high_changed() |
826 |
{ |
827 |
double l = eDimensionKeyRangeLow.get_value(); |
828 |
double h = eDimensionKeyRangeHigh.get_value(); |
829 |
if (h < l) eDimensionKeyRangeLow.set_value(h); |
830 |
} |
831 |
|
832 |
sigc::signal<void> InstrumentProps::signal_instrument_changed() |
833 |
{ |
834 |
return instrument_changed; |
835 |
} |
836 |
|
837 |
void MainWindow::file_changed() |
838 |
{ |
839 |
if (file && !file_is_changed) { |
840 |
set_title("*" + get_title()); |
841 |
file_is_changed = true; |
842 |
} |
843 |
} |
844 |
|
845 |
void MainWindow::load_gig(gig::File* gig, const char* filename) |
846 |
{ |
847 |
file = 0; |
848 |
|
849 |
this->filename = filename ? filename : _("Unsaved Gig File"); |
850 |
set_title(Glib::filename_display_basename(this->filename)); |
851 |
file_has_name = filename; |
852 |
file_is_changed = false; |
853 |
|
854 |
propDialog.set_info(gig->pInfo); |
855 |
|
856 |
Gtk::MenuItem* instrument_menu = |
857 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument")); |
858 |
|
859 |
int instrument_index = 0; |
860 |
Gtk::RadioMenuItem::Group instrument_group; |
861 |
for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ; |
862 |
instrument = gig->GetNextInstrument()) { |
863 |
Gtk::TreeModel::iterator iter = m_refTreeModel->append(); |
864 |
Gtk::TreeModel::Row row = *iter; |
865 |
row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str(); |
866 |
row[m_Columns.m_col_instr] = instrument; |
867 |
// create a menu item for this instrument |
868 |
Gtk::RadioMenuItem* item = |
869 |
new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str()); |
870 |
instrument_menu->get_submenu()->append(*item); |
871 |
item->signal_activate().connect( |
872 |
sigc::bind( |
873 |
sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change), |
874 |
instrument_index |
875 |
) |
876 |
); |
877 |
instrument_index++; |
878 |
} |
879 |
instrument_menu->show(); |
880 |
instrument_menu->get_submenu()->show_all_children(); |
881 |
|
882 |
for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) { |
883 |
if (group->Name != "") { |
884 |
Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append(); |
885 |
Gtk::TreeModel::Row rowGroup = *iterGroup; |
886 |
rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str(); |
887 |
rowGroup[m_SamplesModel.m_col_group] = group; |
888 |
rowGroup[m_SamplesModel.m_col_sample] = NULL; |
889 |
for (gig::Sample* sample = group->GetFirstSample(); |
890 |
sample; sample = group->GetNextSample()) { |
891 |
Gtk::TreeModel::iterator iterSample = |
892 |
m_refSamplesTreeModel->append(rowGroup.children()); |
893 |
Gtk::TreeModel::Row rowSample = *iterSample; |
894 |
rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str(); |
895 |
rowSample[m_SamplesModel.m_col_sample] = sample; |
896 |
rowSample[m_SamplesModel.m_col_group] = NULL; |
897 |
} |
898 |
} |
899 |
} |
900 |
|
901 |
file = gig; |
902 |
|
903 |
// select the first instrument |
904 |
Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection(); |
905 |
tree_sel_ref->select(Gtk::TreePath("0")); |
906 |
} |
907 |
|
908 |
void MainWindow::show_instr_props() |
909 |
{ |
910 |
Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection(); |
911 |
Gtk::TreeModel::iterator it = tree_sel_ref->get_selected(); |
912 |
if (it) |
913 |
{ |
914 |
Gtk::TreeModel::Row row = *it; |
915 |
if (row[m_Columns.m_col_instr]) |
916 |
{ |
917 |
instrumentProps.set_instrument(row[m_Columns.m_col_instr]); |
918 |
instrumentProps.show(); |
919 |
instrumentProps.deiconify(); |
920 |
} |
921 |
} |
922 |
} |
923 |
|
924 |
void MainWindow::on_button_release(GdkEventButton* button) |
925 |
{ |
926 |
if (button->type == GDK_2BUTTON_PRESS) { |
927 |
show_instr_props(); |
928 |
} else if (button->type == GDK_BUTTON_PRESS && button->button == 3) { |
929 |
popup_menu->popup(button->button, button->time); |
930 |
} |
931 |
} |
932 |
|
933 |
void MainWindow::on_instrument_selection_change(int index) { |
934 |
m_RegionChooser.set_instrument(file->GetInstrument(index)); |
935 |
} |
936 |
|
937 |
void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) { |
938 |
if (button->type == GDK_BUTTON_PRESS && button->button == 3) { |
939 |
Gtk::Menu* sample_popup = |
940 |
dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu")); |
941 |
// update enabled/disabled state of sample popup items |
942 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection(); |
943 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
944 |
bool group_selected = false; |
945 |
bool sample_selected = false; |
946 |
if (it) { |
947 |
Gtk::TreeModel::Row row = *it; |
948 |
group_selected = row[m_SamplesModel.m_col_group]; |
949 |
sample_selected = row[m_SamplesModel.m_col_sample]; |
950 |
} |
951 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))-> |
952 |
set_sensitive(group_selected || sample_selected); |
953 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))-> |
954 |
set_sensitive(group_selected || sample_selected); |
955 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))-> |
956 |
set_sensitive(file); |
957 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))-> |
958 |
set_sensitive(group_selected || sample_selected); |
959 |
// show sample popup |
960 |
sample_popup->popup(button->button, button->time); |
961 |
} |
962 |
} |
963 |
|
964 |
void MainWindow::on_action_add_instrument() { |
965 |
static int __instrument_indexer = 0; |
966 |
if (!file) return; |
967 |
gig::Instrument* instrument = file->AddInstrument(); |
968 |
__instrument_indexer++; |
969 |
instrument->pInfo->Name = |
970 |
"Unnamed Instrument " + ToString(__instrument_indexer); |
971 |
// update instrument tree view |
972 |
Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append(); |
973 |
Gtk::TreeModel::Row rowInstr = *iterInstr; |
974 |
rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str(); |
975 |
rowInstr[m_Columns.m_col_instr] = instrument; |
976 |
file_changed(); |
977 |
} |
978 |
|
979 |
void MainWindow::on_action_remove_instrument() { |
980 |
if (!file) return; |
981 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection(); |
982 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
983 |
if (it) { |
984 |
Gtk::TreeModel::Row row = *it; |
985 |
gig::Instrument* instr = row[m_Columns.m_col_instr]; |
986 |
try { |
987 |
// remove instrument from the gig file |
988 |
if (instr) file->DeleteInstrument(instr); |
989 |
// remove respective row from instruments tree view |
990 |
m_refTreeModel->erase(it); |
991 |
file_changed(); |
992 |
} catch (RIFF::Exception e) { |
993 |
Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR); |
994 |
msg.run(); |
995 |
} |
996 |
} |
997 |
} |
998 |
|
999 |
void MainWindow::on_action_sample_properties() { |
1000 |
//TODO: show a dialog where the selected sample's properties can be edited |
1001 |
Gtk::MessageDialog msg( |
1002 |
*this, "Sorry, yet to be implemented!", false, Gtk::MESSAGE_INFO |
1003 |
); |
1004 |
msg.run(); |
1005 |
} |
1006 |
|
1007 |
void MainWindow::on_action_add_group() { |
1008 |
static int __sample_indexer = 0; |
1009 |
if (!file) return; |
1010 |
gig::Group* group = file->AddGroup(); |
1011 |
group->Name = "Unnamed Group"; |
1012 |
if (__sample_indexer) group->Name += " " + ToString(__sample_indexer); |
1013 |
__sample_indexer++; |
1014 |
// update sample tree view |
1015 |
Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append(); |
1016 |
Gtk::TreeModel::Row rowGroup = *iterGroup; |
1017 |
rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str(); |
1018 |
rowGroup[m_SamplesModel.m_col_sample] = NULL; |
1019 |
rowGroup[m_SamplesModel.m_col_group] = group; |
1020 |
file_changed(); |
1021 |
} |
1022 |
|
1023 |
void MainWindow::on_action_add_sample() { |
1024 |
if (!file) return; |
1025 |
// get selected group |
1026 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection(); |
1027 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
1028 |
if (!it) return; |
1029 |
Gtk::TreeModel::Row row = *it; |
1030 |
gig::Group* group = row[m_SamplesModel.m_col_group]; |
1031 |
if (!group) { // not a group, but a sample is selected (probably) |
1032 |
gig::Sample* sample = row[m_SamplesModel.m_col_sample]; |
1033 |
if (!sample) return; |
1034 |
it = row.parent(); // resolve parent (that is the sample's group) |
1035 |
if (!it) return; |
1036 |
row = *it; |
1037 |
group = row[m_SamplesModel.m_col_group]; |
1038 |
if (!group) return; |
1039 |
} |
1040 |
// show 'browse for file' dialog |
1041 |
Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)")); |
1042 |
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
1043 |
dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); |
1044 |
dialog.set_select_multiple(true); |
1045 |
Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile |
1046 |
const char* const supportedFileTypes[] = { |
1047 |
"*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd", |
1048 |
"*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF", |
1049 |
"*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64", |
1050 |
"*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK", |
1051 |
"*.caf", "*.CAF", NULL |
1052 |
}; |
1053 |
for (int i = 0; supportedFileTypes[i]; i++) |
1054 |
soundfilter.add_pattern(supportedFileTypes[i]); |
1055 |
soundfilter.set_name("Sound Files"); |
1056 |
Gtk::FileFilter allpassfilter; // matches every file |
1057 |
allpassfilter.add_pattern("*.*"); |
1058 |
allpassfilter.set_name("All Files"); |
1059 |
dialog.add_filter(soundfilter); |
1060 |
dialog.add_filter(allpassfilter); |
1061 |
if (dialog.run() == Gtk::RESPONSE_OK) { |
1062 |
Glib::ustring error_files; |
1063 |
Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames(); |
1064 |
for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin(); |
1065 |
iter != filenames.end(); ++iter) { |
1066 |
printf("Adding sample %s\n",(*iter).c_str()); |
1067 |
// use libsndfile to retrieve file informations |
1068 |
SF_INFO info; |
1069 |
info.format = 0; |
1070 |
SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info); |
1071 |
try { |
1072 |
if (!hFile) throw std::string("could not open file"); |
1073 |
int bitdepth; |
1074 |
switch (info.format & 0xff) { |
1075 |
case SF_FORMAT_PCM_S8: |
1076 |
bitdepth = 16; // we simply convert to 16 bit for now |
1077 |
break; |
1078 |
case SF_FORMAT_PCM_16: |
1079 |
bitdepth = 16; |
1080 |
break; |
1081 |
case SF_FORMAT_PCM_24: |
1082 |
bitdepth = 32; // we simply convert to 32 bit for now |
1083 |
break; |
1084 |
case SF_FORMAT_PCM_32: |
1085 |
bitdepth = 32; |
1086 |
break; |
1087 |
case SF_FORMAT_PCM_U8: |
1088 |
bitdepth = 16; // we simply convert to 16 bit for now |
1089 |
break; |
1090 |
case SF_FORMAT_FLOAT: |
1091 |
bitdepth = 32; |
1092 |
break; |
1093 |
case SF_FORMAT_DOUBLE: |
1094 |
bitdepth = 32; // I guess we will always truncate this to 32 bit |
1095 |
break; |
1096 |
default: |
1097 |
sf_close(hFile); // close sound file |
1098 |
throw std::string("format not supported"); // unsupported subformat (yet?) |
1099 |
} |
1100 |
// add a new sample to the .gig file |
1101 |
gig::Sample* sample = file->AddSample(); |
1102 |
// file name without path |
1103 |
Glib::ustring filename = Glib::filename_display_basename(*iter); |
1104 |
// remove file extension if there is one |
1105 |
for (int i = 0; supportedFileTypes[i]; i++) { |
1106 |
if (Glib::str_has_suffix(filename, supportedFileTypes[i] + 1)) { |
1107 |
filename.erase(filename.length() - strlen(supportedFileTypes[i] + 1)); |
1108 |
break; |
1109 |
} |
1110 |
} |
1111 |
sample->pInfo->Name = filename; |
1112 |
sample->Channels = info.channels; |
1113 |
sample->BitDepth = bitdepth; |
1114 |
sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels; |
1115 |
sample->SamplesPerSecond = info.samplerate; |
1116 |
// schedule resizing the sample (which will be done |
1117 |
// physically when File::Save() is called) |
1118 |
sample->Resize(info.frames); |
1119 |
// make sure sample is part of the selected group |
1120 |
group->AddSample(sample); |
1121 |
// schedule that physical resize and sample import |
1122 |
// (data copying), performed when "Save" is requested |
1123 |
SampleImportItem sched_item; |
1124 |
sched_item.gig_sample = sample; |
1125 |
sched_item.sample_path = *iter; |
1126 |
m_SampleImportQueue.push_back(sched_item); |
1127 |
// add sample to the tree view |
1128 |
Gtk::TreeModel::iterator iterSample = |
1129 |
m_refSamplesTreeModel->append(row.children()); |
1130 |
Gtk::TreeModel::Row rowSample = *iterSample; |
1131 |
rowSample[m_SamplesModel.m_col_name] = filename; |
1132 |
rowSample[m_SamplesModel.m_col_sample] = sample; |
1133 |
rowSample[m_SamplesModel.m_col_group] = NULL; |
1134 |
// close sound file |
1135 |
sf_close(hFile); |
1136 |
file_changed(); |
1137 |
} catch (std::string what) { // remember the files that made trouble (and their cause) |
1138 |
if (error_files.size()) error_files += "\n"; |
1139 |
error_files += *iter += " (" + what + ")"; |
1140 |
} |
1141 |
} |
1142 |
// show error message box when some file(s) could not be opened / added |
1143 |
if (error_files.size()) { |
1144 |
Glib::ustring txt = "Could not add the following sample(s):\n" + error_files; |
1145 |
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); |
1146 |
msg.run(); |
1147 |
} |
1148 |
} |
1149 |
} |
1150 |
|
1151 |
void MainWindow::on_action_remove_sample() { |
1152 |
if (!file) return; |
1153 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection(); |
1154 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
1155 |
if (it) { |
1156 |
Gtk::TreeModel::Row row = *it; |
1157 |
gig::Group* group = row[m_SamplesModel.m_col_group]; |
1158 |
gig::Sample* sample = row[m_SamplesModel.m_col_sample]; |
1159 |
Glib::ustring name = row[m_SamplesModel.m_col_name]; |
1160 |
try { |
1161 |
// remove group or sample from the gig file |
1162 |
if (group) { |
1163 |
// temporarily remember the samples that bolong to |
1164 |
// that group (we need that to clean the queue) |
1165 |
std::list<gig::Sample*> members; |
1166 |
for (gig::Sample* pSample = group->GetFirstSample(); |
1167 |
pSample; pSample = group->GetNextSample()) { |
1168 |
members.push_back(pSample); |
1169 |
} |
1170 |
// delete the group in the .gig file including the |
1171 |
// samples that belong to the group |
1172 |
file->DeleteGroup(group); |
1173 |
// if sample(s) were just previously added, remove |
1174 |
// them from the import queue |
1175 |
for (std::list<gig::Sample*>::iterator member = members.begin(); |
1176 |
member != members.end(); ++member) { |
1177 |
for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin(); |
1178 |
iter != m_SampleImportQueue.end(); ++iter) { |
1179 |
if ((*iter).gig_sample == *member) { |
1180 |
printf("Removing previously added sample '%s' from group '%s'\n", |
1181 |
(*iter).sample_path.c_str(), name.c_str()); |
1182 |
m_SampleImportQueue.erase(iter); |
1183 |
break; |
1184 |
} |
1185 |
} |
1186 |
} |
1187 |
file_changed(); |
1188 |
} else if (sample) { |
1189 |
// remove sample from the .gig file |
1190 |
file->DeleteSample(sample); |
1191 |
// if sample was just previously added, remove it from |
1192 |
// the import queue |
1193 |
for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin(); |
1194 |
iter != m_SampleImportQueue.end(); ++iter) { |
1195 |
if ((*iter).gig_sample == sample) { |
1196 |
printf("Removing previously added sample '%s'\n", |
1197 |
(*iter).sample_path.c_str()); |
1198 |
m_SampleImportQueue.erase(iter); |
1199 |
break; |
1200 |
} |
1201 |
} |
1202 |
file_changed(); |
1203 |
} |
1204 |
// remove respective row(s) from samples tree view |
1205 |
m_refSamplesTreeModel->erase(it); |
1206 |
} catch (RIFF::Exception e) { |
1207 |
Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR); |
1208 |
msg.run(); |
1209 |
} |
1210 |
} |
1211 |
} |
1212 |
|
1213 |
void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&, |
1214 |
Gtk::SelectionData& selection_data, guint, guint) |
1215 |
{ |
1216 |
// get selected sample |
1217 |
gig::Sample* sample = NULL; |
1218 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection(); |
1219 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
1220 |
if (it) { |
1221 |
Gtk::TreeModel::Row row = *it; |
1222 |
sample = row[m_SamplesModel.m_col_sample]; |
1223 |
} |
1224 |
// pass the gig::Sample as pointer |
1225 |
selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample, |
1226 |
sizeof(sample)/*length of data in bytes*/); |
1227 |
} |
1228 |
|
1229 |
void MainWindow::on_sample_label_drop_drag_data_received( |
1230 |
const Glib::RefPtr<Gdk::DragContext>& context, int, int, |
1231 |
const Gtk::SelectionData& selection_data, guint, guint time) |
1232 |
{ |
1233 |
gig::DimensionRegion* dimregion = m_DimRegionChooser.get_dimregion(); |
1234 |
gig::Sample* sample = *((gig::Sample**) selection_data.get_data()); |
1235 |
|
1236 |
if (sample && dimregion && selection_data.get_length() == sizeof(gig::Sample*)) { |
1237 |
if (sample != dimregion->pSample) { |
1238 |
dimregion->pSample = sample; |
1239 |
dimreg_edit.wSample->set_text(dimregion->pSample->pInfo->Name.c_str()); |
1240 |
std::cout << "Drop received sample \"" << |
1241 |
dimregion->pSample->pInfo->Name.c_str() << "\"" << std::endl; |
1242 |
// drop success |
1243 |
context->drop_reply(true, time); |
1244 |
file_changed(); |
1245 |
return; |
1246 |
} |
1247 |
} |
1248 |
// drop failed |
1249 |
context->drop_reply(false, time); |
1250 |
} |
1251 |
|
1252 |
void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path, |
1253 |
const Gtk::TreeModel::iterator& iter) { |
1254 |
if (!iter) return; |
1255 |
Gtk::TreeModel::Row row = *iter; |
1256 |
Glib::ustring name = row[m_SamplesModel.m_col_name]; |
1257 |
gig::Group* group = row[m_SamplesModel.m_col_group]; |
1258 |
gig::Sample* sample = row[m_SamplesModel.m_col_sample]; |
1259 |
if (group) { |
1260 |
if (group->Name != name) { |
1261 |
group->Name = name; |
1262 |
printf("group name changed\n"); |
1263 |
file_changed(); |
1264 |
} |
1265 |
} else if (sample) { |
1266 |
if (sample->pInfo->Name != name.raw()) { |
1267 |
sample->pInfo->Name = name.raw(); |
1268 |
printf("sample name changed\n"); |
1269 |
file_changed(); |
1270 |
} |
1271 |
} |
1272 |
} |
1273 |
|
1274 |
void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path, |
1275 |
const Gtk::TreeModel::iterator& iter) { |
1276 |
if (!iter) return; |
1277 |
Gtk::TreeModel::Row row = *iter; |
1278 |
Glib::ustring name = row[m_Columns.m_col_name]; |
1279 |
gig::Instrument* instrument = row[m_Columns.m_col_instr]; |
1280 |
if (instrument && instrument->pInfo->Name != name.raw()) { |
1281 |
instrument->pInfo->Name = name.raw(); |
1282 |
file_changed(); |
1283 |
} |
1284 |
} |