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(LabelWidget& prop) |
716 |
{ |
717 |
table.attach(prop.label, 0, 1, rowno, rowno + 1, |
718 |
Gtk::FILL, Gtk::SHRINK); |
719 |
table.attach(prop.widget, 1, 2, rowno, rowno + 1, |
720 |
Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK); |
721 |
rowno++; |
722 |
prop.signal_changed_by_user().connect(instrument_changed.make_slot()); |
723 |
} |
724 |
|
725 |
InstrumentProps::InstrumentProps() |
726 |
: table(2,1), |
727 |
quitButton(Gtk::Stock::CLOSE), |
728 |
eName("Name"), |
729 |
eIsDrum("IsDrum"), |
730 |
eMIDIBank("MIDIBank", 0, 16383), |
731 |
eMIDIProgram("MIDIProgram"), |
732 |
eAttenuation("Attenuation", 0, 96, 0, 1), |
733 |
eGainPlus6("Gain +6dB", eAttenuation, -6), |
734 |
eEffectSend("EffectSend", 0, 65535), |
735 |
eFineTune("FineTune", -8400, 8400), |
736 |
ePitchbendRange("PitchbendRange", 0, 12), |
737 |
ePianoReleaseMode("PianoReleaseMode"), |
738 |
eDimensionKeyRangeLow("DimensionKeyRangeLow"), |
739 |
eDimensionKeyRangeHigh("DimensionKeyRangeHigh") |
740 |
{ |
741 |
set_title("Instrument properties"); |
742 |
|
743 |
rowno = 0; |
744 |
table.set_col_spacings(5); |
745 |
|
746 |
add_prop(eName); |
747 |
add_prop(eIsDrum); |
748 |
add_prop(eMIDIBank); |
749 |
add_prop(eMIDIProgram); |
750 |
add_prop(eAttenuation); |
751 |
add_prop(eGainPlus6); |
752 |
add_prop(eEffectSend); |
753 |
add_prop(eFineTune); |
754 |
add_prop(ePitchbendRange); |
755 |
add_prop(ePianoReleaseMode); |
756 |
add_prop(eDimensionKeyRangeLow); |
757 |
add_prop(eDimensionKeyRangeHigh); |
758 |
|
759 |
eDimensionKeyRangeLow.signal_changed_by_user().connect( |
760 |
sigc::mem_fun(*this, &InstrumentProps::key_range_low_changed)); |
761 |
eDimensionKeyRangeHigh.signal_changed_by_user().connect( |
762 |
sigc::mem_fun(*this, &InstrumentProps::key_range_high_changed)); |
763 |
|
764 |
add(vbox); |
765 |
table.set_border_width(5); |
766 |
vbox.pack_start(table); |
767 |
table.show(); |
768 |
vbox.pack_start(buttonBox, Gtk::PACK_SHRINK); |
769 |
buttonBox.set_layout(Gtk::BUTTONBOX_END); |
770 |
buttonBox.set_border_width(5); |
771 |
buttonBox.show(); |
772 |
buttonBox.pack_start(quitButton); |
773 |
quitButton.set_flags(Gtk::CAN_DEFAULT); |
774 |
quitButton.grab_focus(); |
775 |
|
776 |
quitButton.signal_clicked().connect( |
777 |
sigc::mem_fun(*this, &InstrumentProps::hide)); |
778 |
|
779 |
quitButton.show(); |
780 |
vbox.show(); |
781 |
show_all_children(); |
782 |
} |
783 |
|
784 |
void InstrumentProps::set_instrument(gig::Instrument* instrument) |
785 |
{ |
786 |
eName.set_ptr(&instrument->pInfo->Name); |
787 |
eIsDrum.set_ptr(&instrument->IsDrum); |
788 |
eMIDIBank.set_ptr(&instrument->MIDIBank); |
789 |
eMIDIProgram.set_ptr(&instrument->MIDIProgram); |
790 |
eAttenuation.set_ptr(&instrument->Attenuation); |
791 |
eGainPlus6.set_ptr(&instrument->Attenuation); |
792 |
eEffectSend.set_ptr(&instrument->EffectSend); |
793 |
eFineTune.set_ptr(&instrument->FineTune); |
794 |
ePitchbendRange.set_ptr(&instrument->PitchbendRange); |
795 |
ePianoReleaseMode.set_ptr(&instrument->PianoReleaseMode); |
796 |
eDimensionKeyRangeLow.set_ptr(0); |
797 |
eDimensionKeyRangeHigh.set_ptr(0); |
798 |
eDimensionKeyRangeLow.set_ptr(&instrument->DimensionKeyRange.low); |
799 |
eDimensionKeyRangeHigh.set_ptr(&instrument->DimensionKeyRange.high); |
800 |
} |
801 |
|
802 |
void InstrumentProps::key_range_low_changed() |
803 |
{ |
804 |
double l = eDimensionKeyRangeLow.get_value(); |
805 |
double h = eDimensionKeyRangeHigh.get_value(); |
806 |
if (h < l) eDimensionKeyRangeHigh.set_value(l); |
807 |
} |
808 |
|
809 |
void InstrumentProps::key_range_high_changed() |
810 |
{ |
811 |
double l = eDimensionKeyRangeLow.get_value(); |
812 |
double h = eDimensionKeyRangeHigh.get_value(); |
813 |
if (h < l) eDimensionKeyRangeLow.set_value(h); |
814 |
} |
815 |
|
816 |
sigc::signal<void> InstrumentProps::signal_instrument_changed() |
817 |
{ |
818 |
return instrument_changed; |
819 |
} |
820 |
|
821 |
void MainWindow::file_changed() |
822 |
{ |
823 |
if (file && !file_is_changed) { |
824 |
set_title("*" + get_title()); |
825 |
file_is_changed = true; |
826 |
} |
827 |
} |
828 |
|
829 |
void MainWindow::load_gig(gig::File* gig, const char* filename) |
830 |
{ |
831 |
file = 0; |
832 |
|
833 |
this->filename = filename ? filename : _("Unsaved Gig File"); |
834 |
set_title(Glib::filename_display_basename(this->filename)); |
835 |
file_has_name = filename; |
836 |
file_is_changed = false; |
837 |
|
838 |
propDialog.set_info(gig->pInfo); |
839 |
|
840 |
Gtk::MenuItem* instrument_menu = |
841 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/MenuBar/MenuInstrument")); |
842 |
|
843 |
int instrument_index = 0; |
844 |
Gtk::RadioMenuItem::Group instrument_group; |
845 |
for (gig::Instrument* instrument = gig->GetFirstInstrument() ; instrument ; |
846 |
instrument = gig->GetNextInstrument()) { |
847 |
Gtk::TreeModel::iterator iter = m_refTreeModel->append(); |
848 |
Gtk::TreeModel::Row row = *iter; |
849 |
row[m_Columns.m_col_name] = instrument->pInfo->Name.c_str(); |
850 |
row[m_Columns.m_col_instr] = instrument; |
851 |
// create a menu item for this instrument |
852 |
Gtk::RadioMenuItem* item = |
853 |
new Gtk::RadioMenuItem(instrument_group, instrument->pInfo->Name.c_str()); |
854 |
instrument_menu->get_submenu()->append(*item); |
855 |
item->signal_activate().connect( |
856 |
sigc::bind( |
857 |
sigc::mem_fun(*this, &MainWindow::on_instrument_selection_change), |
858 |
instrument_index |
859 |
) |
860 |
); |
861 |
instrument_index++; |
862 |
} |
863 |
instrument_menu->show(); |
864 |
instrument_menu->get_submenu()->show_all_children(); |
865 |
|
866 |
for (gig::Group* group = gig->GetFirstGroup(); group; group = gig->GetNextGroup()) { |
867 |
if (group->Name != "") { |
868 |
Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append(); |
869 |
Gtk::TreeModel::Row rowGroup = *iterGroup; |
870 |
rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str(); |
871 |
rowGroup[m_SamplesModel.m_col_group] = group; |
872 |
rowGroup[m_SamplesModel.m_col_sample] = NULL; |
873 |
for (gig::Sample* sample = group->GetFirstSample(); |
874 |
sample; sample = group->GetNextSample()) { |
875 |
Gtk::TreeModel::iterator iterSample = |
876 |
m_refSamplesTreeModel->append(rowGroup.children()); |
877 |
Gtk::TreeModel::Row rowSample = *iterSample; |
878 |
rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str(); |
879 |
rowSample[m_SamplesModel.m_col_sample] = sample; |
880 |
rowSample[m_SamplesModel.m_col_group] = NULL; |
881 |
} |
882 |
} |
883 |
} |
884 |
|
885 |
file = gig; |
886 |
|
887 |
// select the first instrument |
888 |
Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection(); |
889 |
tree_sel_ref->select(Gtk::TreePath("0")); |
890 |
} |
891 |
|
892 |
void MainWindow::show_instr_props() |
893 |
{ |
894 |
Glib::RefPtr<Gtk::TreeSelection> tree_sel_ref = m_TreeView.get_selection(); |
895 |
Gtk::TreeModel::iterator it = tree_sel_ref->get_selected(); |
896 |
if (it) |
897 |
{ |
898 |
Gtk::TreeModel::Row row = *it; |
899 |
if (row[m_Columns.m_col_instr]) |
900 |
{ |
901 |
instrumentProps.set_instrument(row[m_Columns.m_col_instr]); |
902 |
instrumentProps.show(); |
903 |
instrumentProps.deiconify(); |
904 |
} |
905 |
} |
906 |
} |
907 |
|
908 |
void MainWindow::on_button_release(GdkEventButton* button) |
909 |
{ |
910 |
if (button->type == GDK_2BUTTON_PRESS) { |
911 |
show_instr_props(); |
912 |
} else if (button->type == GDK_BUTTON_PRESS && button->button == 3) { |
913 |
popup_menu->popup(button->button, button->time); |
914 |
} |
915 |
} |
916 |
|
917 |
void MainWindow::on_instrument_selection_change(int index) { |
918 |
m_RegionChooser.set_instrument(file->GetInstrument(index)); |
919 |
} |
920 |
|
921 |
void MainWindow::on_sample_treeview_button_release(GdkEventButton* button) { |
922 |
if (button->type == GDK_BUTTON_PRESS && button->button == 3) { |
923 |
Gtk::Menu* sample_popup = |
924 |
dynamic_cast<Gtk::Menu*>(uiManager->get_widget("/SamplePopupMenu")); |
925 |
// update enabled/disabled state of sample popup items |
926 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection(); |
927 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
928 |
bool group_selected = false; |
929 |
bool sample_selected = false; |
930 |
if (it) { |
931 |
Gtk::TreeModel::Row row = *it; |
932 |
group_selected = row[m_SamplesModel.m_col_group]; |
933 |
sample_selected = row[m_SamplesModel.m_col_sample]; |
934 |
} |
935 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/SampleProperties"))-> |
936 |
set_sensitive(group_selected || sample_selected); |
937 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddSample"))-> |
938 |
set_sensitive(group_selected || sample_selected); |
939 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/AddGroup"))-> |
940 |
set_sensitive(file); |
941 |
dynamic_cast<Gtk::MenuItem*>(uiManager->get_widget("/SamplePopupMenu/RemoveSample"))-> |
942 |
set_sensitive(group_selected || sample_selected); |
943 |
// show sample popup |
944 |
sample_popup->popup(button->button, button->time); |
945 |
} |
946 |
} |
947 |
|
948 |
void MainWindow::on_action_add_instrument() { |
949 |
static int __instrument_indexer = 0; |
950 |
if (!file) return; |
951 |
gig::Instrument* instrument = file->AddInstrument(); |
952 |
__instrument_indexer++; |
953 |
instrument->pInfo->Name = |
954 |
"Unnamed Instrument " + ToString(__instrument_indexer); |
955 |
// update instrument tree view |
956 |
Gtk::TreeModel::iterator iterInstr = m_refTreeModel->append(); |
957 |
Gtk::TreeModel::Row rowInstr = *iterInstr; |
958 |
rowInstr[m_Columns.m_col_name] = instrument->pInfo->Name.c_str(); |
959 |
rowInstr[m_Columns.m_col_instr] = instrument; |
960 |
file_changed(); |
961 |
} |
962 |
|
963 |
void MainWindow::on_action_remove_instrument() { |
964 |
if (!file) return; |
965 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeView.get_selection(); |
966 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
967 |
if (it) { |
968 |
Gtk::TreeModel::Row row = *it; |
969 |
gig::Instrument* instr = row[m_Columns.m_col_instr]; |
970 |
try { |
971 |
// remove instrument from the gig file |
972 |
if (instr) file->DeleteInstrument(instr); |
973 |
// remove respective row from instruments tree view |
974 |
m_refTreeModel->erase(it); |
975 |
file_changed(); |
976 |
} catch (RIFF::Exception e) { |
977 |
Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR); |
978 |
msg.run(); |
979 |
} |
980 |
} |
981 |
} |
982 |
|
983 |
void MainWindow::on_action_sample_properties() { |
984 |
//TODO: show a dialog where the selected sample's properties can be edited |
985 |
Gtk::MessageDialog msg( |
986 |
*this, "Sorry, yet to be implemented!", false, Gtk::MESSAGE_INFO |
987 |
); |
988 |
msg.run(); |
989 |
} |
990 |
|
991 |
void MainWindow::on_action_add_group() { |
992 |
static int __sample_indexer = 0; |
993 |
if (!file) return; |
994 |
gig::Group* group = file->AddGroup(); |
995 |
group->Name = "Unnamed Group"; |
996 |
if (__sample_indexer) group->Name += " " + ToString(__sample_indexer); |
997 |
__sample_indexer++; |
998 |
// update sample tree view |
999 |
Gtk::TreeModel::iterator iterGroup = m_refSamplesTreeModel->append(); |
1000 |
Gtk::TreeModel::Row rowGroup = *iterGroup; |
1001 |
rowGroup[m_SamplesModel.m_col_name] = group->Name.c_str(); |
1002 |
rowGroup[m_SamplesModel.m_col_sample] = NULL; |
1003 |
rowGroup[m_SamplesModel.m_col_group] = group; |
1004 |
file_changed(); |
1005 |
} |
1006 |
|
1007 |
void MainWindow::on_action_add_sample() { |
1008 |
if (!file) return; |
1009 |
// get selected group |
1010 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection(); |
1011 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
1012 |
if (!it) return; |
1013 |
Gtk::TreeModel::Row row = *it; |
1014 |
gig::Group* group = row[m_SamplesModel.m_col_group]; |
1015 |
if (!group) { // not a group, but a sample is selected (probably) |
1016 |
gig::Sample* sample = row[m_SamplesModel.m_col_sample]; |
1017 |
if (!sample) return; |
1018 |
it = row.parent(); // resolve parent (that is the sample's group) |
1019 |
if (!it) return; |
1020 |
row = *it; |
1021 |
group = row[m_SamplesModel.m_col_group]; |
1022 |
if (!group) return; |
1023 |
} |
1024 |
// show 'browse for file' dialog |
1025 |
Gtk::FileChooserDialog dialog(*this, _("Add Sample(s)")); |
1026 |
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
1027 |
dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); |
1028 |
dialog.set_select_multiple(true); |
1029 |
Gtk::FileFilter soundfilter; // matches all file types supported by libsndfile |
1030 |
const char* supportedFileTypes[] = { |
1031 |
"*.wav", "*.WAV", "*.aiff", "*.AIFF", "*.aifc", "*.AIFC", "*.snd", |
1032 |
"*.SND", "*.au", "*.AU", "*.paf", "*.PAF", "*.iff", "*.IFF", |
1033 |
"*.svx", "*.SVX", "*.sf", "*.SF", "*.voc", "*.VOC", "*.w64", |
1034 |
"*.W64", "*.pvf", "*.PVF", "*.xi", "*.XI", "*.htk", "*.HTK", |
1035 |
"*.caf", "*.CAF", NULL |
1036 |
}; |
1037 |
for (int i = 0; supportedFileTypes[i]; i++) |
1038 |
soundfilter.add_pattern(supportedFileTypes[i]); |
1039 |
soundfilter.set_name("Sound Files"); |
1040 |
Gtk::FileFilter allpassfilter; // matches every file |
1041 |
allpassfilter.add_pattern("*.*"); |
1042 |
allpassfilter.set_name("All Files"); |
1043 |
dialog.add_filter(soundfilter); |
1044 |
dialog.add_filter(allpassfilter); |
1045 |
if (dialog.run() == Gtk::RESPONSE_OK) { |
1046 |
Glib::ustring error_files; |
1047 |
Glib::SListHandle<Glib::ustring> filenames = dialog.get_filenames(); |
1048 |
for (Glib::SListHandle<Glib::ustring>::iterator iter = filenames.begin(); |
1049 |
iter != filenames.end(); ++iter) { |
1050 |
printf("Adding sample %s\n",(*iter).c_str()); |
1051 |
// use libsndfile to retrieve file informations |
1052 |
SF_INFO info; |
1053 |
info.format = 0; |
1054 |
SNDFILE* hFile = sf_open((*iter).c_str(), SFM_READ, &info); |
1055 |
try { |
1056 |
if (!hFile) throw std::string("could not open file"); |
1057 |
int bitdepth; |
1058 |
switch (info.format & 0xff) { |
1059 |
case SF_FORMAT_PCM_S8: |
1060 |
bitdepth = 16; // we simply convert to 16 bit for now |
1061 |
break; |
1062 |
case SF_FORMAT_PCM_16: |
1063 |
bitdepth = 16; |
1064 |
break; |
1065 |
case SF_FORMAT_PCM_24: |
1066 |
bitdepth = 32; // we simply convert to 32 bit for now |
1067 |
break; |
1068 |
case SF_FORMAT_PCM_32: |
1069 |
bitdepth = 32; |
1070 |
break; |
1071 |
case SF_FORMAT_PCM_U8: |
1072 |
bitdepth = 16; // we simply convert to 16 bit for now |
1073 |
break; |
1074 |
case SF_FORMAT_FLOAT: |
1075 |
bitdepth = 32; |
1076 |
break; |
1077 |
case SF_FORMAT_DOUBLE: |
1078 |
bitdepth = 32; // I guess we will always truncate this to 32 bit |
1079 |
break; |
1080 |
default: |
1081 |
sf_close(hFile); // close sound file |
1082 |
throw std::string("format not supported"); // unsupported subformat (yet?) |
1083 |
} |
1084 |
// add a new sample to the .gig file |
1085 |
gig::Sample* sample = file->AddSample(); |
1086 |
// file name without path |
1087 |
sample->pInfo->Name = (*iter).substr((*iter).rfind('/') + 1).raw(); |
1088 |
sample->Channels = info.channels; |
1089 |
sample->BitDepth = bitdepth; |
1090 |
sample->FrameSize = bitdepth / 8/*1 byte are 8 bits*/ * info.channels; |
1091 |
sample->SamplesPerSecond = info.samplerate; |
1092 |
// schedule resizing the sample (which will be done |
1093 |
// physically when File::Save() is called) |
1094 |
sample->Resize(info.frames); |
1095 |
// make sure sample is part of the selected group |
1096 |
group->AddSample(sample); |
1097 |
// schedule that physical resize and sample import |
1098 |
// (data copying), performed when "Save" is requested |
1099 |
SampleImportItem sched_item; |
1100 |
sched_item.gig_sample = sample; |
1101 |
sched_item.sample_path = *iter; |
1102 |
m_SampleImportQueue.push_back(sched_item); |
1103 |
// add sample to the tree view |
1104 |
Gtk::TreeModel::iterator iterSample = |
1105 |
m_refSamplesTreeModel->append(row.children()); |
1106 |
Gtk::TreeModel::Row rowSample = *iterSample; |
1107 |
rowSample[m_SamplesModel.m_col_name] = sample->pInfo->Name.c_str(); |
1108 |
rowSample[m_SamplesModel.m_col_sample] = sample; |
1109 |
rowSample[m_SamplesModel.m_col_group] = NULL; |
1110 |
// close sound file |
1111 |
sf_close(hFile); |
1112 |
file_changed(); |
1113 |
} catch (std::string what) { // remember the files that made trouble (and their cause) |
1114 |
if (error_files.size()) error_files += "\n"; |
1115 |
error_files += *iter += " (" + what + ")"; |
1116 |
} |
1117 |
} |
1118 |
// show error message box when some file(s) could not be opened / added |
1119 |
if (error_files.size()) { |
1120 |
Glib::ustring txt = "Could not add the following sample(s):\n" + error_files; |
1121 |
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); |
1122 |
msg.run(); |
1123 |
} |
1124 |
} |
1125 |
} |
1126 |
|
1127 |
void MainWindow::on_action_remove_sample() { |
1128 |
if (!file) return; |
1129 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection(); |
1130 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
1131 |
if (it) { |
1132 |
Gtk::TreeModel::Row row = *it; |
1133 |
gig::Group* group = row[m_SamplesModel.m_col_group]; |
1134 |
gig::Sample* sample = row[m_SamplesModel.m_col_sample]; |
1135 |
Glib::ustring name = row[m_SamplesModel.m_col_name]; |
1136 |
try { |
1137 |
// remove group or sample from the gig file |
1138 |
if (group) { |
1139 |
// temporarily remember the samples that bolong to |
1140 |
// that group (we need that to clean the queue) |
1141 |
std::list<gig::Sample*> members; |
1142 |
for (gig::Sample* pSample = group->GetFirstSample(); |
1143 |
pSample; pSample = group->GetNextSample()) { |
1144 |
members.push_back(pSample); |
1145 |
} |
1146 |
// delete the group in the .gig file including the |
1147 |
// samples that belong to the group |
1148 |
file->DeleteGroup(group); |
1149 |
// if sample(s) were just previously added, remove |
1150 |
// them from the import queue |
1151 |
for (std::list<gig::Sample*>::iterator member = members.begin(); |
1152 |
member != members.end(); ++member) { |
1153 |
for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin(); |
1154 |
iter != m_SampleImportQueue.end(); ++iter) { |
1155 |
if ((*iter).gig_sample == *member) { |
1156 |
printf("Removing previously added sample '%s' from group '%s'\n", |
1157 |
(*iter).sample_path.c_str(), name.c_str()); |
1158 |
m_SampleImportQueue.erase(iter); |
1159 |
break; |
1160 |
} |
1161 |
} |
1162 |
} |
1163 |
file_changed(); |
1164 |
} else if (sample) { |
1165 |
// remove sample from the .gig file |
1166 |
file->DeleteSample(sample); |
1167 |
// if sample was just previously added, remove it from |
1168 |
// the import queue |
1169 |
for (std::list<SampleImportItem>::iterator iter = m_SampleImportQueue.begin(); |
1170 |
iter != m_SampleImportQueue.end(); ++iter) { |
1171 |
if ((*iter).gig_sample == sample) { |
1172 |
printf("Removing previously added sample '%s'\n", |
1173 |
(*iter).sample_path.c_str()); |
1174 |
m_SampleImportQueue.erase(iter); |
1175 |
break; |
1176 |
} |
1177 |
} |
1178 |
file_changed(); |
1179 |
} |
1180 |
// remove respective row(s) from samples tree view |
1181 |
m_refSamplesTreeModel->erase(it); |
1182 |
} catch (RIFF::Exception e) { |
1183 |
Gtk::MessageDialog msg(*this, e.Message.c_str(), false, Gtk::MESSAGE_ERROR); |
1184 |
msg.run(); |
1185 |
} |
1186 |
} |
1187 |
} |
1188 |
|
1189 |
void MainWindow::on_sample_treeview_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&, |
1190 |
Gtk::SelectionData& selection_data, guint, guint) |
1191 |
{ |
1192 |
// get selected sample |
1193 |
gig::Sample* sample = NULL; |
1194 |
Glib::RefPtr<Gtk::TreeSelection> sel = m_TreeViewSamples.get_selection(); |
1195 |
Gtk::TreeModel::iterator it = sel->get_selected(); |
1196 |
if (it) { |
1197 |
Gtk::TreeModel::Row row = *it; |
1198 |
sample = row[m_SamplesModel.m_col_sample]; |
1199 |
} |
1200 |
// pass the gig::Sample as pointer |
1201 |
selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&sample, |
1202 |
sizeof(sample)/*length of data in bytes*/); |
1203 |
} |
1204 |
|
1205 |
void MainWindow::on_sample_label_drop_drag_data_received( |
1206 |
const Glib::RefPtr<Gdk::DragContext>& context, int, int, |
1207 |
const Gtk::SelectionData& selection_data, guint, guint time) |
1208 |
{ |
1209 |
gig::DimensionRegion* dimregion = m_DimRegionChooser.get_dimregion(); |
1210 |
gig::Sample* sample = *((gig::Sample**) selection_data.get_data()); |
1211 |
|
1212 |
if (sample && dimregion && selection_data.get_length() == sizeof(gig::Sample*)) { |
1213 |
if (sample != dimregion->pSample) { |
1214 |
dimregion->pSample = sample; |
1215 |
dimreg_edit.wSample->set_text(dimregion->pSample->pInfo->Name.c_str()); |
1216 |
std::cout << "Drop received sample \"" << |
1217 |
dimregion->pSample->pInfo->Name.c_str() << "\"" << std::endl; |
1218 |
// drop success |
1219 |
context->drop_reply(true, time); |
1220 |
file_changed(); |
1221 |
return; |
1222 |
} |
1223 |
} |
1224 |
// drop failed |
1225 |
context->drop_reply(false, time); |
1226 |
} |
1227 |
|
1228 |
void MainWindow::sample_name_changed(const Gtk::TreeModel::Path& path, |
1229 |
const Gtk::TreeModel::iterator& iter) { |
1230 |
if (!iter) return; |
1231 |
Gtk::TreeModel::Row row = *iter; |
1232 |
Glib::ustring name = row[m_SamplesModel.m_col_name]; |
1233 |
gig::Group* group = row[m_SamplesModel.m_col_group]; |
1234 |
gig::Sample* sample = row[m_SamplesModel.m_col_sample]; |
1235 |
if (group) { |
1236 |
if (group->Name != name) { |
1237 |
group->Name = name; |
1238 |
printf("group name changed\n"); |
1239 |
file_changed(); |
1240 |
} |
1241 |
} else if (sample) { |
1242 |
if (sample->pInfo->Name != name.raw()) { |
1243 |
sample->pInfo->Name = name.raw(); |
1244 |
printf("sample name changed\n"); |
1245 |
file_changed(); |
1246 |
} |
1247 |
} |
1248 |
} |
1249 |
|
1250 |
void MainWindow::instrument_name_changed(const Gtk::TreeModel::Path& path, |
1251 |
const Gtk::TreeModel::iterator& iter) { |
1252 |
if (!iter) return; |
1253 |
Gtk::TreeModel::Row row = *iter; |
1254 |
Glib::ustring name = row[m_Columns.m_col_name]; |
1255 |
gig::Instrument* instrument = row[m_Columns.m_col_instr]; |
1256 |
if (instrument && instrument->pInfo->Name != name.raw()) { |
1257 |
instrument->pInfo->Name = name.raw(); |
1258 |
file_changed(); |
1259 |
} |
1260 |
} |