Skip to content

Commit

Permalink
Improved MIDI section of Media preferences
Browse files Browse the repository at this point in the history
* Get rid of the "Actve Sound Font:" BStringView below the BListView.
  Now the selected SoundFont is the active SF.
* If there's only one SF available, select it automatically.
* Added a BStringView below the list and only give feedback when needed:
  - if no SF is installed
  - if the user has to select a SF
* Double-clicking an entry opens its location in Tracker.
* Added node-monitoring to live-update the list of available SFs.
  Note: Un/installing a package doesn't trigger a notification (related
  to #9970 ?).
  • Loading branch information
Humdinger committed Nov 7, 2016
1 parent b83afe6 commit 9874150
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 36 deletions.
157 changes: 131 additions & 26 deletions src/preferences/media/MidiSettingsView.cpp
@@ -1,5 +1,5 @@
/*
* Copyright 2014, Haiku, Inc. All rights reserved.
* Copyright 2014-2016, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/

Expand All @@ -18,6 +18,7 @@
#include <Locale.h>
#include <Node.h>
#include <NodeInfo.h>
#include <NodeMonitor.h>
#include <Path.h>
#include <PathFinder.h>
#include <ScrollView.h>
Expand All @@ -31,33 +32,39 @@
#define B_TRANSLATION_CONTEXT "Midi View"

const static uint32 kSelectSoundFont = 'SeSf';
const static uint32 kDoubleClick = 'DClk';


MidiSettingsView::MidiSettingsView()
:
SettingsView()
{
BBox* defaultsBox = new BBox("SoundFont");
defaultsBox->SetLabel(B_TRANSLATE("Available SoundFonts"));
BBox* defaultsBox = new BBox("SoundFonts");
defaultsBox->SetLabel(B_TRANSLATE("SoundFonts"));
BGridView* defaultsGridView = new BGridView();

fListView = new BListView(B_SINGLE_SELECTION_LIST);
fListView->SetSelectionMessage(new BMessage(kSelectSoundFont));
fListView->SetInvocationMessage(new BMessage(kDoubleClick));

BScrollView *scrollView = new BScrollView("ScrollView", fListView,
BScrollView* scrollView = new BScrollView("ScrollView", fListView,
0, false, true);
BLayoutBuilder::Grid<>(defaultsGridView)
.SetInsets(B_USE_DEFAULT_SPACING, 0, B_USE_DEFAULT_SPACING,
B_USE_DEFAULT_SPACING)
.Add(scrollView, 0, 0);

defaultsBox->AddChild(defaultsGridView);
fSoundFontStatus = new BStringView("SoundFontStatus", "");

BLayoutBuilder::Group<>(this)
.SetInsets(0, 0, 0, 0)
.Add(defaultsBox)
.Add(fActiveSoundFont = new BStringView("ActiveSoundFont",
"Active Sound Font:"))
.AddGroup(B_HORIZONTAL)
.AddGlue()
.Add(fSoundFontStatus)
.AddGlue()
.End()
.AddGlue();
}

Expand All @@ -69,9 +76,21 @@ MidiSettingsView::AttachedToWindow()
SettingsView::AttachedToWindow();

fListView->SetTarget(this);
_RetrieveSoftSynthList();

_LoadSettings();
_RetrieveSoundFontList();
_SelectActiveSoundFont();
_UpdateSoundFontStatus();
_WatchFolders();
}


/* virtual */
void
MidiSettingsView::DetachedFromWindow()
{
SettingsView::DetachedFromWindow();

stop_watching(this);
}


Expand All @@ -80,14 +99,54 @@ void
MidiSettingsView::MessageReceived(BMessage* message)
{
switch (message->what) {
case kSelectSoundFont:
case B_NODE_MONITOR:
{
BString text = _SelectedSoundFont();
if (text != "") {
text.Prepend("Active Sound Font: ");
fActiveSoundFont->SetText(text);
int32 selected = fListView->CurrentSelection();
BStringItem* olditem = (BStringItem*)fListView->ItemAt(selected);

_RetrieveSoundFontList();

int32 count = fListView->CountItems();
if (count == 1) {
fListView->Select(0);
_SaveSettings();
} else if (olditem != NULL) {
for (int32 i = 0; i < fListView->CountItems(); i++) {
BStringItem* item = (BStringItem*)fListView->ItemAt(i);
if (!strcmp(item->Text(), olditem->Text())) {
fListView->Select(i);
break;
}
}
}
_UpdateSoundFontStatus();
break;
}
case kSelectSoundFont:
{
int selection = fListView->CurrentSelection();
if (selection < 0)
_SelectActiveSoundFont();
else
_SaveSettings();

_UpdateSoundFontStatus();
break;
}
case kDoubleClick:
{
int selection = fListView->CurrentSelection();
BStringItem* item = (BStringItem*)fListView->ItemAt(selection);

BEntry entry(item->Text());
BEntry parent;
entry.GetParent(&parent);
entry_ref folderRef;
parent.GetRef(&folderRef);
BMessenger msgr("application/x-vnd.Be-TRAK");
BMessage refMsg(B_REFS_RECEIVED);
refMsg.AddRef("refs",&folderRef);
msgr.SendMessage(&refMsg);
break;
}

Expand All @@ -99,7 +158,7 @@ MidiSettingsView::MessageReceived(BMessage* message)


void
MidiSettingsView::_RetrieveSoftSynthList()
MidiSettingsView::_RetrieveSoundFontList()
{
// TODO: Duplicated code between here
// and BSoftSynth::SetDefaultInstrumentsFile
Expand Down Expand Up @@ -137,30 +196,43 @@ void
MidiSettingsView::_LoadSettings()
{
struct BPrivate::midi_settings settings;
if (BPrivate::read_midi_settings(&settings) == B_OK) {
for (int32 i = 0; i < fListView->CountItems(); i++) {
BStringItem* item = (BStringItem*)fListView->ItemAt(i);
if (!strcmp(item->Text(), settings.soundfont_file)) {
fListView->Select(i);
break;
}
}
}
if (BPrivate::read_midi_settings(&settings) == B_OK)
fActiveSoundFont = new BString(settings.soundfont_file);
}


void
MidiSettingsView::_SaveSettings()
{
BString soundFont = _SelectedSoundFont();
if (soundFont.Length() > 0) {
fActiveSoundFont = new BString(_SelectedSoundFont());
if (fActiveSoundFont->Length() > 0) {
struct BPrivate::midi_settings settings;
strlcpy(settings.soundfont_file, soundFont, sizeof(settings.soundfont_file));
strlcpy(settings.soundfont_file, fActiveSoundFont->String(),
sizeof(settings.soundfont_file));
BPrivate::write_midi_settings(settings);
}
}


void
MidiSettingsView::_SelectActiveSoundFont()
{
int32 count = fListView->CountItems();
if (count == 1) {
fListView->Select(0);
_SaveSettings();
return;
}
for (int32 i = 0; i < fListView->CountItems(); i++) {
BStringItem* item = (BStringItem*)fListView->ItemAt(i);
if (!strcmp(item->Text(), fActiveSoundFont->String())) {
fListView->Select(i);
break;
}
}
}


BString
MidiSettingsView::_SelectedSoundFont() const
{
Expand All @@ -174,3 +246,36 @@ MidiSettingsView::_SelectedSoundFont() const

return string;
}


void
MidiSettingsView::_UpdateSoundFontStatus()
{
if (fListView->IsEmpty()) {
fSoundFontStatus->SetText(
B_TRANSLATE("There are no SoundFont installed."));
return;
}
int32 selection = fListView->CurrentSelection();
if (selection < 0) {
fSoundFontStatus->SetText(
B_TRANSLATE("Please select a SoundFont."));
return;
}
fSoundFontStatus->SetText("");
}


void
MidiSettingsView::_WatchFolders()
{
BStringList paths;
BPathFinder().FindPaths(B_FIND_PATH_DATA_DIRECTORY, "synth", paths);
for (int32 i = 0; i < paths.CountStrings(); i++) {
BEntry entry(paths.StringAt(i));
node_ref nref;
entry.GetNodeRef(&nref);

watch_node(&nref, B_WATCH_ALL, this);
}
}
28 changes: 18 additions & 10 deletions src/preferences/media/MidiSettingsView.h
@@ -1,5 +1,5 @@
/*
* Copyright 2014, Haiku, Inc. All rights reserved.
* Copyright 2014-2016, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/

Expand All @@ -11,21 +11,29 @@

class BButton;
class BListView;
class BString;
class BStringView;

class MidiSettingsView : public SettingsView {
public:
MidiSettingsView();
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage* message);
MidiSettingsView();

virtual void AttachedToWindow();
virtual void DetachedFromWindow();
virtual void MessageReceived(BMessage* message);

private:
void _RetrieveSoftSynthList();
void _LoadSettings();
void _SaveSettings();
BString _SelectedSoundFont() const;
void _RetrieveSoundFontList();
void _LoadSettings();
void _SaveSettings();
void _WatchFolders();
void _SelectActiveSoundFont();
BString _SelectedSoundFont() const;
void _UpdateSoundFontStatus();

BListView* fListView;
BStringView* fActiveSoundFont;
BListView* fListView;
BString* fActiveSoundFont;
BStringView* fSoundFontStatus;
};

#endif /* MIDIVIEW_H_ */

0 comments on commit 9874150

Please sign in to comment.