Pogger/src/EntriesView.cpp

218 lines
5.2 KiB
C++
Raw Normal View History

/*
2021-01-25 19:39:31 -06:00
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "EntriesView.h"
#include <Box.h>
#include <Button.h>
#include <Message.h>
#include <LayoutBuilder.h>
#include <PopUpMenu.h>
2021-01-28 00:19:36 -06:00
#include <StringList.h>
#include <RadioButton.h>
#include <StringView.h>
#include <TextControl.h>
2021-01-28 00:19:36 -06:00
#include <iostream>
#include "App.h"
#include "Util.h"
EntriesView::EntriesView(const char* name)
:
BGroupView(name, B_VERTICAL, B_USE_DEFAULT_SPACING)
{
_InitInterface();
}
void
EntriesView::AttachedToWindow()
{
fEntryFolderText->SetTarget(this);
fEntryFolderBrowseButton->SetTarget(this);
fFileExtText->SetTarget(this);
2021-01-28 00:19:36 -06:00
fOpenAsAutoRadio->SetTarget(this);
fOpenAsHtmlRadio->SetTarget(this);
fOpenAsUrlRadio->SetTarget(this);
fOpenWithSelectButton->SetTarget(this);
}
void
EntriesView::MessageReceived(BMessage* msg)
{
switch (msg->what)
{
2021-01-28 00:19:36 -06:00
case kEntryFolderText:
{
status_t result = ((App*)be_app)->fPreferences->SetEntryDir(
fEntryFolderText->Text());
if (result != B_OK)
userFileError(result, fEntryFolderText->Text());
break;
}
case kEntryExtText:
{
((App*)be_app)->fPreferences->fEntryFileExt = fFileExtText->Text();
break;
}
case kOpenHtmlRadio:
{
((App*)be_app)->fPreferences->fOpenAs = kOpenAsHtml;
break;
}
case kOpenUrlRadio:
{
((App*)be_app)->fPreferences->fOpenAs = kOpenAsUrl;
break;
}
case kOpenAutoRadio:
{
((App*)be_app)->fPreferences->fOpenAs = kOpenAsAuto;
break;
}
case kOpenWithSelect:
{
((App*)be_app)->fPreferences->SetEntryOpenWith(
fOpenWithMenuField->MenuItem()->Label());
break;
}
default:
{
2021-01-28 00:19:36 -06:00
BGroupView::MessageReceived(msg);
break;
}
}
}
void
EntriesView::_InitInterface()
{
// Saving
fSavingBox = new BBox("saving");
fSavingBox->SetLabel("Saving");
fEntryFolderLabel = new BStringView("entryFolderLabel", "Entry folder:");
2021-01-28 00:19:36 -06:00
fEntryFolderText = new BTextControl("entryFolder", "", "",
new BMessage(kEntryFolderText));
fEntryFolderBrowseButton = new BButton("entryFolderBrowse", "Browse…",
2021-01-28 00:19:36 -06:00
new BMessage(kEntryFolderBrowse));
fFileExtLabel = new BStringView("fileExtLabel", "File extension:");
2021-01-28 00:19:36 -06:00
fFileExtText = new BTextControl("fileExt", "", "",
new BMessage(kEntryExtText));
// Opening
fOpeningBox = new BBox("opening");
fOpeningBox->SetLabel("Opening");
fOpenAsLabel = new BStringView("openAsLabel", "Open as:");
2021-01-28 00:19:36 -06:00
fOpenAsAutoRadio = new BRadioButton("asAuto", "Auto",
new BMessage(kOpenAutoRadio));
fOpenAsHtmlRadio = new BRadioButton("asHtml", "HTML",
new BMessage(kOpenHtmlRadio));
fOpenAsUrlRadio = new BRadioButton("asUrl", "URL",
new BMessage(kOpenUrlRadio));
fOpenWithLabel = new BStringView("openWithLabel", "Open with:");
fOpenWithMenu = new BPopUpMenu("openWith");
fOpenWithMenuField = new BMenuField("openWithMenu", NULL, fOpenWithMenu);
fOpenWithSelectButton = new BButton("openWithSelect", "Select…",
2021-01-28 00:19:36 -06:00
new BMessage(kOpenWithBrowse));
// Display current settings
Preferences* prefs = ((App*)be_app)->fPreferences;
if (prefs->fOpenAs == kOpenAsHtml)
fOpenAsHtmlRadio->SetValue(B_CONTROL_ON);
else if (prefs->fOpenAs == kOpenAsUrl)
fOpenAsUrlRadio->SetValue(B_CONTROL_ON);
else
fOpenAsAutoRadio->SetValue(B_CONTROL_ON);
fFileExtText->SetText(prefs->fEntryFileExt);
fEntryFolderText->SetText(prefs->EntryDir());
_PopulateOpenWithMenu();
BLayoutBuilder::Group<>(fSavingBox, B_HORIZONTAL)
.SetInsets(B_USE_ITEM_INSETS)
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
.SetInsets(0, 20, B_USE_ITEM_INSETS, 0)
.Add(fEntryFolderLabel)
.Add(fFileExtLabel)
.End()
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
.SetInsets(0, 20, B_USE_ITEM_INSETS, 0)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.Add(fEntryFolderText)
.Add(fEntryFolderBrowseButton)
.End()
.Add(fFileExtText)
.End()
.End();
BLayoutBuilder::Group<>(fOpeningBox, B_HORIZONTAL, B_USE_HALF_ITEM_SPACING)
.SetInsets(B_USE_ITEM_INSETS)
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
.SetInsets(0, 20, B_USE_ITEM_INSETS, 0)
.Add(fOpenAsLabel)
.Add(fOpenWithLabel)
.End()
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
.SetInsets(0, 20, B_USE_ITEM_INSETS, 0)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
2021-01-28 00:19:36 -06:00
.Add(fOpenAsAutoRadio)
.Add(fOpenAsHtmlRadio)
.Add(fOpenAsUrlRadio)
.AddGlue()
.End()
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.Add(fOpenWithMenuField)
.Add(fOpenWithSelectButton)
.End()
.End()
.End();
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.SetInsets(B_USE_DEFAULT_SPACING)
.Add(fSavingBox)
.AddGlue()
.Add(fOpeningBox)
.AddGlue()
.End();
}
2021-01-28 00:19:36 -06:00
void
EntriesView::_PopulateOpenWithMenu()
{
BString preferred = ((App*)be_app)->fPreferences->EntryOpenWith();
BMimeType html("text/html");
BStringList signatures;
BMessage types;
html.GetSupportingApps(&types);
if (types.FindStrings("applications", &signatures) != B_OK)
return;
for (int i = 0; i < signatures.CountStrings(); i++) {
BString string = signatures.StringAt(i);
if (string != preferred)
fOpenWithMenu->AddItem(
new BMenuItem(string, new BMessage(kOpenWithSelect)));
}
fOpenWithMenu->AddItem(
new BMenuItem(preferred, new BMessage(kOpenWithSelect)), 0);
}