OSDN Git Service

イニシャルコミット。
[marathon/ShapeFusion.git] / Sounds / SoundsView.cpp
1 /*
2  * This file is part of ShapeFusion (Copyright 2000 Tito Dal Canton)
3  *
4  * ShapeFusion is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * ShapeFusion is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with ShapeFusion; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29
30 #include "wx/wfstream.h"
31
32 #include "../ShapeFusionApp.h"
33 #include "../ShapeFusionMenus.h"
34 #include "../DefaultNames.h"
35 #include "SoundsView.h"
36
37 BEGIN_EVENT_TABLE(SoundsView, wxView)
38         EVT_LISTBOX(SOUND_CLASS_LIST, SoundsView::SoundClassChanged)
39         EVT_TEXT(SOUND_CLASS_ID_FIELD, SoundsView::SoundClassIdChanged)
40         EVT_RADIOBOX(SOUND_VOLUME_RADIO_BUTTON, SoundsView::VolumeButtonChanged)
41         EVT_CHOICE(SOUND_CHANCE_MENU, SoundsView::ChanceMenuChanged)
42         EVT_CHECKBOX(SOUND_FLAGS_RESTART, SoundsView::FlagsChanged)
43         EVT_CHECKBOX(SOUND_FLAGS_ABORT, SoundsView::FlagsChanged)
44         EVT_CHECKBOX(SOUND_FLAGS_RESIST, SoundsView::FlagsChanged)
45         EVT_CHECKBOX(SOUND_FLAGS_CHANGE, SoundsView::FlagsChanged)
46         EVT_CHECKBOX(SOUND_FLAGS_OBSTRUCTED, SoundsView::FlagsChanged)
47         EVT_CHECKBOX(SOUND_FLAGS_MOBSTRUCTED, SoundsView::FlagsChanged)
48         EVT_CHECKBOX(SOUND_FLAGS_AMBIENT, SoundsView::FlagsChanged)
49         EVT_LISTBOX(SOUND_EIGHT_BIT_PERMUTATIONS_LIST, SoundsView::SoundPermutationSelected)
50         EVT_LISTBOX(SOUND_SIXTEEN_BIT_PERMUTATIONS_LIST, SoundsView::SoundPermutationSelected)
51         EVT_LISTBOX_DCLICK(SOUND_EIGHT_BIT_PERMUTATIONS_LIST, SoundsView::SoundPermutationDoubleClicked)
52         EVT_LISTBOX_DCLICK(SOUND_SIXTEEN_BIT_PERMUTATIONS_LIST, SoundsView::SoundPermutationDoubleClicked)
53         EVT_MENU(EDIT_MENU_DELETE, SoundsView::MenuDelete)
54         EVT_MENU(SOUNDS_MENU_ADDCLASS, SoundsView::MenuAddSoundClass)
55         EVT_MENU(SOUNDS_MENU_EXPORT, SoundsView::MenuExportSound)
56         EVT_MENU(SOUNDS_MENU_IMPORT, SoundsView::MenuImportSound)
57 END_EVENT_TABLE()
58
59 IMPLEMENT_DYNAMIC_CLASS(SoundsView, wxView)
60
61 SoundsView::SoundsView(): mSoundClass(wxNOT_FOUND), mSoundSource(wxNOT_FOUND), mSoundPermutation(wxNOT_FOUND)
62 {
63         frame = NULL;
64         menubar = NULL;
65         payload = NULL;
66 }
67
68 // What to do when a view is created. Creates actual
69 // windows for displaying the view.
70 bool SoundsView::OnCreate(wxDocument *doc, long WXUNUSED(flags))
71 {
72         wxString frameTitle = _T("ShapeFusion : Sounds : ");
73
74         frameTitle.Append(doc->GetFilename());
75         
76     frame = wxGetApp().CreateChildFrame(doc, this, frameTitle, wxPoint(0, 0), wxSize(600, 400), wxDEFAULT_FRAME_STYLE);// & ~ (wxRESIZE_BORDER | wxRESIZE_BOX | wxMAXIMIZE_BOX));
77         
78         payload = (SoundsDocument*)doc;
79         
80         menubar = frame->GetMenuBar();  
81         CreateSoundsMenu(menubar);
82         
83         // Because we can always add sound classes
84         menubar->Enable(SOUNDS_MENU_ADDCLASS, true);
85
86         wxString volume_labels[] = { wxT("Soft"), wxT("Medium"), wxT("Loud") };
87         wxString chances_labels[] = { wxT("100%"), wxT("90%"), wxT("80%"), wxT("70%"), wxT("60%"), wxT("50%"), wxT("40%"), wxT("30%"), wxT("20%"), wxT("10%") };
88
89         main_panel = new wxPanel(frame);
90         main_panel->Show();
91         
92         sound_class_text = new wxStaticText(main_panel, wxID_ANY, wxT("Sound classes: "));
93         sound_class_id_text = new wxStaticText(main_panel, wxID_ANY, wxT("Class ID: "));
94         sound_class_id_field = new wxTextCtrl(main_panel, SOUND_CLASS_ID_FIELD, wxT(""));
95         
96         sound_class_number_text = new wxStaticText(main_panel, wxID_ANY, wxT("Class number: "));
97         sound_class_number_field = new wxStaticText(main_panel, SOUND_CLASS_NUMBER_FIELD, wxT(""));
98         
99         sound_class_list = new wxListBox(main_panel, (wxWindowID)SOUND_CLASS_LIST);
100         
101         sound_flag_restart_checkbox = new wxCheckBox(main_panel, SOUND_FLAGS_RESTART, wxT("Cannot be restarted"));
102         sound_flag_abort_checkbox = new wxCheckBox(main_panel, SOUND_FLAGS_ABORT, wxT("Does not self-abort"));
103         sound_flag_resist_checkbox = new wxCheckBox(main_panel, SOUND_FLAGS_RESIST, wxT("Resists pitch changes"));
104         sound_flag_change_checkbox = new wxCheckBox(main_panel, SOUND_FLAGS_CHANGE, wxT("Can't change pitch"));
105         sound_flag_obstructed_checkbox = new wxCheckBox(main_panel, SOUND_FLAGS_OBSTRUCTED, wxT("Can't be obstructed"));
106         sound_flag_mobstructed_checkbox = new wxCheckBox(main_panel, SOUND_FLAGS_MOBSTRUCTED, wxT("Can't be media obstructed"));
107         sound_flag_ambient_checkbox = new wxCheckBox(main_panel, SOUND_FLAGS_AMBIENT, wxT("Is ambient"));
108         
109         sound_volume_radio_button = new wxRadioBox(main_panel, SOUND_VOLUME_RADIO_BUTTON, wxT("Volume"), wxDefaultPosition, wxDefaultSize, 3, volume_labels, 3, wxRA_SPECIFY_COLS);
110         
111         sound_chance_text = new wxStaticText(main_panel, wxID_ANY, wxT("Chance: "));
112         sound_chance_menu = new wxChoice(main_panel, SOUND_CHANCE_MENU, wxDefaultPosition, wxDefaultSize, 10, chances_labels);
113         
114         sound_low_pitch_text = new wxStaticText(main_panel, wxID_ANY, wxT("Low pitch: "));
115         sound_low_pitch_field = new wxTextCtrl(main_panel, SOUND_LOW_PITCH_FIELD);
116         sound_high_pitch_text = new wxStaticText(main_panel, wxID_ANY, wxT("High pitch: "));
117         sound_high_pitch_field = new wxTextCtrl(main_panel, SOUND_HIGH_PITCH_FIELD);
118         
119         sound_eight_bit_text = new wxStaticText(main_panel, wxID_ANY, wxT("8-bit sounds:"));
120         sound_eight_bit_list = new wxListBox(main_panel, (wxWindowID)SOUND_EIGHT_BIT_PERMUTATIONS_LIST);
121         
122         sound_sixteen_bit_text = new wxStaticText(main_panel, wxID_ANY, wxT("16-bit sounds: "));
123         sound_sixteen_bit_list = new wxListBox(main_panel, (wxWindowID)SOUND_SIXTEEN_BIT_PERMUTATIONS_LIST);
124         
125         sound_remap_check_box = new wxCheckBox(main_panel, SOUND_REMAP_CHECK_BOX, wxT("Remap 8-bit"));
126         
127         frame_sizer = new wxBoxSizer(wxHORIZONTAL);
128         sound_class_sizer = new wxBoxSizer(wxVERTICAL);
129         sound_class_header_sizer = new wxFlexGridSizer(2, 2, 0, 0);
130         sound_editor_sizer = new wxBoxSizer(wxVERTICAL);
131         sound_flags_sizer = new wxStaticBoxSizer(wxVERTICAL, main_panel, wxT("Flags"));
132         sound_menus_sizer = new wxFlexGridSizer(2, 3, 0, 0);
133         sound_permutation_sizer = new wxBoxSizer(wxHORIZONTAL);
134         sound_eight_bit_sizer = new wxBoxSizer(wxVERTICAL);
135         sound_sixteen_bit_sizer = new wxBoxSizer(wxVERTICAL);
136         
137         sound_class_header_sizer->Add(sound_class_id_text, 0, wxALIGN_CENTER_VERTICAL, 0);
138         sound_class_header_sizer->Add(sound_class_id_field, 0, 0, 0);
139         sound_class_header_sizer->Add(sound_class_number_text, 0, wxALIGN_CENTER_VERTICAL, 0);
140         sound_class_header_sizer->Add(sound_class_number_field, 0, 0, 0);
141         
142         sound_class_sizer->Add(sound_class_text, 0, 0, 0);
143         sound_class_sizer->Add(sound_class_header_sizer, 0, 0, 0);
144         sound_class_sizer->Add(sound_class_list, 1, wxEXPAND, 0);
145         
146         sound_flags_sizer->Add(sound_flag_restart_checkbox, 0, 0, 0);
147         sound_flags_sizer->Add(sound_flag_abort_checkbox, 0, 0, 0);
148         sound_flags_sizer->Add(sound_flag_resist_checkbox, 0, 0, 0);
149         sound_flags_sizer->Add(sound_flag_change_checkbox, 0, 0, 0);
150         sound_flags_sizer->Add(sound_flag_obstructed_checkbox, 0, 0, 0);
151         sound_flags_sizer->Add(sound_flag_mobstructed_checkbox, 0, 0, 0);
152         sound_flags_sizer->Add(sound_flag_ambient_checkbox, 0, 0, 0);
153         
154         sound_menus_sizer->Add(sound_chance_text, 0, wxALIGN_CENTER_VERTICAL, 0);
155         sound_menus_sizer->Add(sound_chance_menu, 0, 0, 0);
156         sound_menus_sizer->Add(sound_low_pitch_text, 0, wxALIGN_CENTER_VERTICAL, 0);
157         sound_menus_sizer->Add(sound_low_pitch_field, 0, 0, 0);
158         sound_menus_sizer->Add(sound_high_pitch_text, 0, wxALIGN_CENTER_VERTICAL, 0);
159         sound_menus_sizer->Add(sound_high_pitch_field, 0, 0, 0);
160         
161         sound_eight_bit_sizer->Add(sound_eight_bit_text, 0, 0, 0);
162         sound_eight_bit_sizer->Add(sound_eight_bit_list, 1, wxEXPAND | wxRIGHT, 5);
163         sound_sixteen_bit_sizer->Add(sound_sixteen_bit_text, 0, 0, 0);
164         sound_sixteen_bit_sizer->Add(sound_sixteen_bit_list, 1, wxEXPAND, 0);
165         sound_sixteen_bit_sizer->Add(sound_remap_check_box, 0, 0, 0);
166         
167         sound_permutation_sizer->Add(sound_eight_bit_sizer, 1, wxEXPAND, 0);
168         sound_permutation_sizer->Add(sound_sixteen_bit_sizer, 1, wxEXPAND, 0);
169         
170         sound_editor_sizer->Add(sound_flags_sizer, 0, 0, 0);
171         sound_editor_sizer->AddSpacer(1);
172         sound_editor_sizer->Add(sound_volume_radio_button, 0, 0, 0);
173         sound_editor_sizer->AddSpacer(1);
174         sound_editor_sizer->Add(sound_menus_sizer, 0, 0, 0);
175         sound_editor_sizer->AddSpacer(5);
176         sound_editor_sizer->Add(sound_permutation_sizer, 1, wxEXPAND, 0);
177         
178         frame_sizer->Add(sound_class_sizer, 0, wxEXPAND | wxALL, 5);
179         frame_sizer->AddSpacer(5);
180         frame_sizer->Add(sound_editor_sizer, 0, wxEXPAND | wxALL, 5);
181         
182         main_panel->SetSizer(frame_sizer);
183         frame_sizer->Layout();
184         frame_sizer->SetSizeHints(frame);
185         
186         frame->Show(true);
187 #ifdef __X__
188         // X seems to require a forced resize
189         int x, y;
190         frame->GetSize(&x, &y);
191         frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
192 #endif
193         
194         return true;
195 }
196
197 // Sneakily gets used for default print/preview
198 // as well as drawing on the screen.
199 void SoundsView::OnDraw(wxDC *dc)
200 {
201 }
202
203 void SoundsView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
204 {
205         bool    gequals = true;
206
207         sound_class_list->Clear();
208         for (unsigned int i = 0; i < payload->GetSoundCount(); i++) {
209                 sound_class_list->Append(GetName(wxT("sound"), i));
210                 // We check if there is a difference between 8-bit and 16-bit
211                 // SoundsDefinitions
212                 SoundsDefinition        *def8 = payload->Get8BitSoundDefinition(i),
213                                                         *def16 = payload->Get16BitSoundDefinition(i);
214                 bool                            equals = false;
215
216                 if (def8 != NULL && def16 != NULL)
217                         equals = def8->HaveSameAttributesAs(*def16);
218                 else if (def8 == NULL && def16 == NULL)
219                         equals = true;
220
221                 if (!equals)
222                         wxLogDebug(wxT("Sound source different at %d"), i);
223                 gequals = gequals && equals;
224         }
225         if (!gequals) {
226                 // FIXME : Update this when we have a "complete" editor...
227                 wxMessageDialog msg(frame,
228                                                 wxT("It seems 8-bit and 16-bit versions of some of this Sound file "
229                                                 "sounds have differences. This editor will replace 16-bit sounds "
230                                                 "flags with those from 8-bit sounds, to ensure consistency. "
231                                                 "If you really need to be able to change 16-bit flags independently, "
232                                                 "please file a feature request."),
233                                                 wxT("Warning !"), wxOK | wxICON_WARNING);
234                                                 
235                 msg.ShowModal();
236         }
237         Update();
238 }
239
240 void SoundsView::Update(void)
241 {
242         // We disable our menuitems, in case selection is invalid 
243         menubar->Enable(SOUNDS_MENU_IMPORT, false);
244         menubar->Enable(SOUNDS_MENU_EXPORT, false);
245         menubar->Enable(EDIT_MENU_DELETE, false);
246         
247         if (sound_class_list->GetCount() == 0) {
248                 // There is no sound class
249                 // We cannot have a selection
250                 mSoundPermutation = wxNOT_FOUND;
251                 mSoundSource = wxNOT_FOUND;
252
253         } else {
254                 // We have a sound class
255                 
256                 // Make sure we always have something selected
257                 mSoundClass = sound_class_list->GetSelection();
258                 if (mSoundClass == wxNOT_FOUND) {
259                         wxLogDebug(wxT("[SoundsView] There is no sound selected. Selecting first item..."));
260                         sound_class_list->SetSelection(0);
261                         mSoundClass = sound_class_list->GetSelection();
262                 }
263                 
264                 // We build the permutations listbox
265                 sound_eight_bit_list->Clear();
266                 sound_sixteen_bit_list->Clear();
267                 
268                 SoundsDefinition *def = payload->Get8BitSoundDefinition(mSoundClass);
269                 if (def) {
270                         for (unsigned int i = 0; i < def->GetPermutationCount(); i++) {
271                                 sound_eight_bit_list->Append(wxString::Format(wxT("%d"), def->GetPermutation(i)->Size()));
272                         }
273                 }
274                 
275                 def = payload->Get16BitSoundDefinition(mSoundClass);
276                 if (def) {
277                         for (unsigned int i = 0; i < def->GetPermutationCount(); i++) {
278                                 sound_sixteen_bit_list->Append(wxString::Format(wxT("%d"), def->GetPermutation(i)->Size()));
279                         }
280                 }
281                 
282                 // As soon as we have a sound class selected, we can
283                 // - import a sound into it
284                 // - delete it
285                 menubar->Enable(SOUNDS_MENU_IMPORT, true);
286                 menubar->Enable(EDIT_MENU_DELETE, true);
287                 
288                 if (payload->Get8BitSoundDefinition(mSoundClass)->GetPermutationCount() != 0) {
289                         // There is 8-bit sounds, we select first
290                         mSoundSource = 0;
291                         mSoundPermutation = 0;
292                         sound_eight_bit_list->SetSelection(0);
293                         // We deselect 16-bit list
294                         sound_sixteen_bit_list->SetSelection(wxNOT_FOUND);
295                 } else {
296                         // There is no 8-bit sounds
297                         if (payload->Get16BitSoundDefinition(mSoundClass)->GetPermutationCount() != 0) {
298                                 // We have 16-bit sounds, we select this one...
299                                 mSoundSource = 1;
300                                 mSoundPermutation = 0;
301                                 sound_sixteen_bit_list->SetSelection(0);
302                                 // We deselect 8-bit list
303                                 sound_eight_bit_list->SetSelection(wxNOT_FOUND);
304                         } else {
305                                 // There is neither 8-bit nor 16-bit sounds, don't select anything
306                                 sound_eight_bit_list->SetSelection(wxNOT_FOUND);
307                                 sound_sixteen_bit_list->SetSelection(wxNOT_FOUND);
308                                 mSoundSource = wxNOT_FOUND;
309                                 mSoundPermutation = wxNOT_FOUND;
310                         }
311                 }
312                 
313                 // We enable this, our selection is valid...
314                 menubar->Enable(SOUNDS_MENU_EXPORT, true);
315                 
316                 def = payload->Get8BitSoundDefinition(mSoundClass);
317                                 
318                 sound_class_number_field->SetLabel(wxString::Format(wxT("%d"), mSoundClass));
319                 sound_class_id_field->ChangeValue(wxString::Format(wxT("%d"), def->GetSoundCode()));
320                 
321                 sound_volume_radio_button->SetSelection(def->GetBehaviorIndex());
322                 sound_chance_menu->SetSelection(def->GetChance());
323                 
324                 sound_flag_restart_checkbox->SetValue(def->IsNotRestartable());
325                 sound_flag_abort_checkbox->SetValue(def->IsNotSelfAbortable());
326                 sound_flag_resist_checkbox->SetValue(def->IsPitchChangeResistant());
327                 sound_flag_change_checkbox->SetValue(def->IsNotPitchChangeable());
328                 sound_flag_obstructed_checkbox->SetValue(def->IsNotObstructed());
329                 sound_flag_mobstructed_checkbox->SetValue(def->IsNotMediaObstructed());
330                 sound_flag_ambient_checkbox->SetValue(def->IsAmbient());
331                 sound_low_pitch_field->ChangeValue(wxString::Format(wxT("%g"), def->GetLowPitch()));
332                 sound_high_pitch_field->ChangeValue(wxString::Format(wxT("%g"), def->GetHighPitch()));
333         }
334 }
335
336 // Clean up windows used for displaying the view.
337 bool SoundsView::OnClose(bool deleteWindow)
338 {
339         if (!GetDocument()->Close())
340                 return false;
341         SetFrame((wxFrame *) NULL);
342         Activate(false);
343         if (deleteWindow) {
344                 delete frame;
345                 return true;
346         }
347         return true;
348 }
349
350 void SoundsView::SoundClassChanged(wxCommandEvent &e)
351 {
352         Update();
353 }
354
355 void SoundsView::SoundClassIdChanged(wxCommandEvent& e)
356 {
357         long v;
358         if (e.GetString().ToLong(&v))
359         {
360                 SoundsDefinition* def = payload->Get8BitSoundDefinition(mSoundClass);
361                 def->SetSoundCode(v);
362                 
363                 def = payload->Get16BitSoundDefinition(mSoundClass);
364                 def->SetSoundCode(v);
365         }
366         GetDocument()->Modify(true);
367 }
368
369 void SoundsView::VolumeButtonChanged(wxCommandEvent &e)
370 {
371         SoundsDefinition *def = payload->Get8BitSoundDefinition(mSoundClass);
372         def->SetBehaviorIndex(sound_volume_radio_button->GetSelection());
373         
374         def = payload->Get16BitSoundDefinition(mSoundClass);
375         def->SetBehaviorIndex(sound_volume_radio_button->GetSelection());
376         GetDocument()->Modify(true);
377 }
378
379 void SoundsView::ChanceMenuChanged(wxCommandEvent &e)
380 {
381         SoundsDefinition *def = payload->Get8BitSoundDefinition(mSoundClass);
382         def->SetChance(sound_chance_menu->GetSelection());
383         
384         def = payload->Get16BitSoundDefinition(mSoundClass);
385         def->SetChance(sound_chance_menu->GetSelection());
386         GetDocument()->Modify(true);
387 }
388
389 void SoundsView::FlagsChanged(wxCommandEvent &e)
390 {
391         SoundsDefinition *def8 = payload->Get8BitSoundDefinition(mSoundClass);
392         SoundsDefinition *def16 = payload->Get16BitSoundDefinition(mSoundClass);
393         switch (e.GetId()) {
394                 case SOUND_FLAGS_RESTART:
395                         def8->SetNotRestartable(e.IsChecked());
396                         def16->SetNotRestartable(e.IsChecked());
397                         break;
398                 case SOUND_FLAGS_ABORT:
399                         def8->SetNotSelfAbortable(e.IsChecked());
400                         def16->SetNotSelfAbortable(e.IsChecked());
401                         break;
402                 case SOUND_FLAGS_RESIST:
403                         def8->SetPitchChangeResistant(e.IsChecked());
404                         def16->SetPitchChangeResistant(e.IsChecked());
405                         break;
406                 case SOUND_FLAGS_CHANGE:
407                         def8->SetNotPitchChangeable(e.IsChecked());
408                         def16->SetNotPitchChangeable(e.IsChecked());
409                         break;
410                 case SOUND_FLAGS_OBSTRUCTED:
411                         def8->SetNotObstructed(e.IsChecked());
412                         def16->SetNotObstructed(e.IsChecked());
413                         break;
414                 case SOUND_FLAGS_MOBSTRUCTED:
415                         def8->SetNotMediaObstructed(e.IsChecked());
416                         def16->SetNotMediaObstructed(e.IsChecked());
417                         break;
418                 case SOUND_FLAGS_AMBIENT:
419                         def8->SetAmbient(e.IsChecked());
420                         def16->SetAmbient(e.IsChecked());
421                         break;
422                 default:
423                         wxLogDebug(wxT("Invalid control id in FlagsChanged"));
424                         break;
425         }
426         GetDocument()->Modify(true);
427 }
428
429 void SoundsView::LowPitchValueChanged(wxScrollEvent &e)
430 {
431         long int l;
432
433         sound_low_pitch_field->GetValue().ToLong(&l);
434         
435         SoundsDefinition *def = payload->Get8BitSoundDefinition(mSoundClass);
436         def->SetLowPitch(l);
437         
438         def = payload->Get16BitSoundDefinition(mSoundClass);
439         def->SetLowPitch(l);
440         GetDocument()->Modify(true);
441 }
442
443 void SoundsView::HighPitchValueChanged(wxScrollEvent &e)
444 {
445         long int l;
446
447         sound_high_pitch_field->GetValue().ToLong(&l);
448         
449         SoundsDefinition *def = payload->Get8BitSoundDefinition(mSoundClass);
450         def->SetHighPitch(l);
451         
452         def = payload->Get16BitSoundDefinition(mSoundClass);
453         def->SetHighPitch(l);
454         GetDocument()->Modify(true);
455 }
456
457 void SoundsView::MenuDelete(wxCommandEvent &e)
458 {
459         wxWindow *win = sound_class_list->FindFocus();
460
461         switch (win->GetId()) {
462                 case SOUND_CLASS_LIST:
463                         wxLogDebug(wxT("Delete Sound Class"));
464                         break;
465                 case SOUND_EIGHT_BIT_PERMUTATIONS_LIST:
466                 case SOUND_SIXTEEN_BIT_PERMUTATIONS_LIST:
467                 {
468                         if (mSoundClass == wxNOT_FOUND || mSoundSource == wxNOT_FOUND || mSoundPermutation == wxNOT_FOUND) {
469                                 return;
470                         }
471                         
472                         SoundsDefinition* def = payload->GetSoundDefinition(mSoundSource, mSoundClass);
473                         def->DeletePermutation(mSoundPermutation);
474                         Update();
475                         GetDocument()->Modify(true);
476                         break;
477                 }
478                 default:
479                         break;
480         }
481 }
482
483 void SoundsView::MenuAddSoundClass(wxCommandEvent &e)
484 {
485         wxLogDebug(wxT("Adding an item"));
486         payload->AddSoundDefinition();
487         
488         // We add the new Sound class item by hand
489         sound_class_list->Append(wxString::Format(wxT("Sound %d"), sound_class_list->GetCount()));
490
491         GetDocument()->Modify(true);
492         
493         Update();
494 }
495
496 void SoundsView::MenuImportSound(wxCommandEvent &e)
497 {
498         wxWindow* w = wxWindow::FindFocus();
499         SoundsDefinition* definition = 0;
500         if (w == static_cast<wxWindow*>(sound_sixteen_bit_list)) {
501                 definition = payload->Get16BitSoundDefinition(mSoundClass);
502         } else if (w == static_cast<wxWindow*>(sound_eight_bit_list)) {
503                 definition = payload->Get8BitSoundDefinition(mSoundClass);
504         } else {
505                 wxMessageDialog msg(frame, wxT("Sorry, you need to select a sound class and 8-bit or 16-bit to import a sound"), wxT("Error: No selection"), wxOK | wxICON_EXCLAMATION);
506                 msg.ShowModal();
507                 return;
508         }
509
510         if (definition->GetPermutationCount() >= MAXIMUM_PERMUTATIONS_PER_SOUND) {
511                 wxMessageDialog msg(frame, wxT("There are already five permutations for this sound"), wxT("Error: permutation limit reached"), wxOK | wxICON_EXCLAMATION);
512                 msg.ShowModal();
513                 return;
514         }
515         
516         wxFileDialog dlg(frame, wxT("Choose a sound file to add"), wxT(""), wxT(""), wxT("Common sound files (AIFF, WAV)|*.aif;*.wav"), wxOPEN);
517         if (dlg.ShowModal() == wxID_OK) {
518                 if (definition->NewPermutation(dlg.GetPath()) == NULL) {
519                         wxMessageDialog msg(frame, wxT("Error importing sound"), wxT("Error"), wxOK | wxICON_EXCLAMATION);
520                         msg.ShowModal();
521                         return;
522                 }
523         }
524
525         GetDocument()->Modify(true);
526
527         Update();
528 }
529
530 void SoundsView::MenuExportSound(wxCommandEvent &e)
531 {
532         if (mSoundClass == wxNOT_FOUND || mSoundSource == wxNOT_FOUND || mSoundPermutation == wxNOT_FOUND) {
533                 wxMessageDialog msg(frame, wxT("Sorry, you need to select a sound class and a permutation to export a sound"), wxT("Error : No selection"), wxOK | wxICON_EXCLAMATION);
534                 msg.ShowModal();
535                 return;
536         }
537         
538         wxFileDialog dlg(frame, wxT("Choose a file name :"), wxT(""), wxString::Format(wxT("Sound %d-%d.wav"), mSoundClass, mSoundPermutation), wxT("WAV files (*.wav)|*.wav|AIFF files (*.aif)|*.aif"), wxSAVE | wxOVERWRITE_PROMPT);
539
540         if (dlg.ShowModal() == wxID_OK) {
541                 SoundsDefinition        *def = payload->GetSoundDefinition(mSoundSource, mSoundClass);
542                 AppleSoundHeader        *sound = def->GetPermutation(mSoundPermutation);
543                 bool                            result = false;
544
545                 switch (dlg.GetFilterIndex()) {
546                         case 0: // Selected *.wav
547                                 result = sound->SaveToWave(dlg.GetPath());
548                                 break;
549                         case 1: // Selected *.aif
550                                 result = sound->SaveToAiff(dlg.GetPath());
551                                 break;
552                         default:
553                                 break;
554                 }
555
556                 if (!result)
557                         wxLogDebug(wxT("[SoundsView] Error exporting sound"));
558         }
559 }
560
561 void SoundsView::SoundPermutationSelected(wxCommandEvent &e)
562 {
563         // We unselect the other permutation field
564         if (e.GetId() == SOUND_EIGHT_BIT_PERMUTATIONS_LIST) {
565                 wxLogDebug(wxT("Selected 8-bit"));
566                 sound_sixteen_bit_list->SetSelection(wxNOT_FOUND);
567                 mSoundSource = 0;
568                 mSoundPermutation = sound_eight_bit_list->GetSelection();
569         } else if (e.GetId() == SOUND_SIXTEEN_BIT_PERMUTATIONS_LIST) {
570                 wxLogDebug(wxT("Selected 16-bit"));
571                 sound_eight_bit_list->SetSelection(wxNOT_FOUND);
572                 mSoundSource = 1;
573                 mSoundPermutation = sound_sixteen_bit_list->GetSelection();
574         }
575 }
576
577 void SoundsView::SoundPermutationDoubleClicked(wxCommandEvent &e)
578 {
579         SoundsDefinition *def = payload->GetSoundDefinition(mSoundSource, mSoundClass);
580         def->GetPermutation(mSoundPermutation)->PlaySound();
581 }
582