diff --git a/src/apps/webpositive/BrowserApp.cpp b/src/apps/webpositive/BrowserApp.cpp index aaffc30a1e..89bd3aeaeb 100644 --- a/src/apps/webpositive/BrowserApp.cpp +++ b/src/apps/webpositive/BrowserApp.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +64,7 @@ BrowserApp::BrowserApp() fDownloadWindow(NULL), fSettingsWindow(NULL) { + be_locale->GetAppCatalog(&fAppCatalog); } diff --git a/src/apps/webpositive/BrowserApp.h b/src/apps/webpositive/BrowserApp.h index 531a88ffe0..d4fb9fbf59 100644 --- a/src/apps/webpositive/BrowserApp.h +++ b/src/apps/webpositive/BrowserApp.h @@ -30,6 +30,7 @@ #include +#include #include class DownloadWindow; @@ -68,6 +69,8 @@ private: DownloadWindow* fDownloadWindow; SettingsWindow* fSettingsWindow; + + BCatalog fAppCatalog; }; diff --git a/src/apps/webpositive/SettingsWindow.cpp b/src/apps/webpositive/SettingsWindow.cpp index 42c4492883..906a029dfb 100644 --- a/src/apps/webpositive/SettingsWindow.cpp +++ b/src/apps/webpositive/SettingsWindow.cpp @@ -26,62 +26,133 @@ */ #include "SettingsWindow.h" -#include "BrowserApp.h" -#include "WebSettings.h" - #include #include #include #include #include #include +#include +#include +#include #include #include #include +#include #include +#include "BrowserApp.h" +#include "FontSelectionView.h" #include "SettingsMessage.h" +#include "WebSettings.h" +#undef TR_CONTEXT +#define TR_CONTEXT "Settings Window" + enum { - MSG_OK = 'aply', - MSG_CANCEL = 'cncl', - MSG_REVERT = 'rvrt', + MSG_APPLY = 'aply', + MSG_CANCEL = 'cncl', + MSG_REVERT = 'rvrt', + MSG_STANDARD_FONT_SIZE_SELECTED = 'sfss', + MSG_FIXED_FONT_SIZE_SELECTED = 'ffss', }; +static const int32 kDefaultFontSize = 14; + SettingsWindow::SettingsWindow(BRect frame, SettingsMessage* settings) : - BWindow(frame, "Settings", B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, + BWindow(frame, TR("Settings"), B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, B_AUTO_UPDATE_SIZE_LIMITS | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE), fSettings(settings) { SetLayout(new BGroupLayout(B_VERTICAL)); - fOkButton = new BButton("OK", new BMessage(MSG_OK)); + fStandardFontView = new FontSelectionView("standard", "Standard font:", + true, be_plain_font); + BFont defaultSerifFont = _FindDefaultSerifFont(); + fSerifFontView = new FontSelectionView("serif", "Serif font:", true, + &defaultSerifFont); + fSansSerifFontView = new FontSelectionView("sans serif", "Sans serif font:", + true, be_plain_font); + fFixedFontView = new FontSelectionView("fixed", "Fixed font:", true, + be_fixed_font); + + fStandardSizesMenu = new BMenuField("standard font size", + TR("Default standard font size:"), new BPopUpMenu("sizes"), NULL); + _BuildSizesMenu(fStandardSizesMenu->Menu(), MSG_STANDARD_FONT_SIZE_SELECTED); + + fFixedSizesMenu = new BMenuField("fixed font size", + TR("Default fixed font size:"), new BPopUpMenu("sizes"), NULL); + _BuildSizesMenu(fFixedSizesMenu->Menu(), MSG_FIXED_FONT_SIZE_SELECTED); + + fApplyButton = new BButton("Apply", new BMessage(MSG_APPLY)); fCancelButton = new BButton("Cancel", new BMessage(MSG_CANCEL)); fRevertButton = new BButton("Revert", new BMessage(MSG_REVERT)); float spacing = be_control_look->DefaultItemSpacing(); AddChild(BGroupLayoutBuilder(B_VERTICAL) - .Add(BGridLayoutBuilder(spacing, spacing) + .Add(BGridLayoutBuilder(spacing / 2, spacing / 2) + .Add(fStandardFontView->CreateFontsLabelLayoutItem(), 0, 0) + .Add(fStandardFontView->CreateFontsMenuBarLayoutItem(), 1, 0) + .Add(fStandardFontView->PreviewBox(), 0, 1, 2) + .Add(BSpaceLayoutItem::CreateHorizontalStrut(spacing), 0, 2, 2) + + .Add(fSerifFontView->CreateFontsLabelLayoutItem(), 0, 3) + .Add(fSerifFontView->CreateFontsMenuBarLayoutItem(), 1, 3) + .Add(fSerifFontView->PreviewBox(), 0, 4, 2) + .Add(BSpaceLayoutItem::CreateHorizontalStrut(spacing), 0, 5, 2) + + .Add(fSansSerifFontView->CreateFontsLabelLayoutItem(), 0, 6) + .Add(fSansSerifFontView->CreateFontsMenuBarLayoutItem(), 1, 6) + .Add(fSansSerifFontView->PreviewBox(), 0, 7, 2) + .Add(BSpaceLayoutItem::CreateHorizontalStrut(spacing), 0, 8, 2) + + .Add(fFixedFontView->CreateFontsLabelLayoutItem(), 0, 9) + .Add(fFixedFontView->CreateFontsMenuBarLayoutItem(), 1, 9) + .Add(fFixedFontView->PreviewBox(), 0, 10, 2) + .Add(BSpaceLayoutItem::CreateHorizontalStrut(spacing), 0, 11, 2) + + .Add(fStandardSizesMenu->CreateLabelLayoutItem(), 0, 12) + .Add(fStandardSizesMenu->CreateMenuBarLayoutItem(), 1, 12) + .Add(fFixedSizesMenu->CreateLabelLayoutItem(), 0, 13) + .Add(fFixedSizesMenu->CreateMenuBarLayoutItem(), 1, 13) + .SetInsets(spacing, spacing, spacing, spacing) ) .Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER)) .Add(BGroupLayoutBuilder(B_HORIZONTAL, spacing) .Add(fRevertButton) .AddGlue() .Add(fCancelButton) - .Add(fOkButton) + .Add(fApplyButton) .SetInsets(spacing, spacing, spacing, spacing) ) ); - fOkButton->MakeDefault(true); + AddHandler(fStandardFontView); + fStandardFontView->AttachedToLooper(); + + AddHandler(fSerifFontView); + fSerifFontView->AttachedToLooper(); + + AddHandler(fSansSerifFontView); + fSansSerifFontView->AttachedToLooper(); + + AddHandler(fFixedFontView); + fFixedFontView->AttachedToLooper(); + + fApplyButton->MakeDefault(true); if (!frame.IsValid()) CenterOnScreen(); + // load settings from disk + _RevertSettings(); + // apply to WebKit + _ApplySettings(); + // Start hidden Hide(); Show(); @@ -90,6 +161,14 @@ SettingsWindow::SettingsWindow(BRect frame, SettingsMessage* settings) SettingsWindow::~SettingsWindow() { + RemoveHandler(fStandardFontView); + delete fStandardFontView; + RemoveHandler(fSerifFontView); + delete fSerifFontView; + RemoveHandler(fSansSerifFontView); + delete fSansSerifFontView; + RemoveHandler(fFixedFontView); + delete fFixedFontView; } @@ -97,9 +176,8 @@ void SettingsWindow::MessageReceived(BMessage* message) { switch (message->what) { - case MSG_OK: + case MSG_APPLY: _ApplySettings(); - PostMessage(B_QUIT_REQUESTED); break; case MSG_CANCEL: _RevertSettings(); @@ -108,6 +186,20 @@ SettingsWindow::MessageReceived(BMessage* message) case MSG_REVERT: _RevertSettings(); break; + + case MSG_STANDARD_FONT_SIZE_SELECTED: { + int32 size = _SizesMenuValue(fStandardSizesMenu->Menu()); + fStandardFontView->SetSize(size); + fSerifFontView->SetSize(size); + fSansSerifFontView->SetSize(size); + break; + } + case MSG_FIXED_FONT_SIZE_SELECTED: { + int32 size = _SizesMenuValue(fFixedSizesMenu->Menu()); + fFixedFontView->SetSize(size); + break; + } + default: BWindow::MessageReceived(message); break; @@ -124,18 +216,143 @@ SettingsWindow::QuitRequested() } +void +SettingsWindow::Show() +{ + // When showing the window, the this is always the + // point to which we can revert the settings. + _RevertSettings(); + BWindow::Show(); +} + + // #pragma mark - private void SettingsWindow::_ApplySettings() { + // Store settings + fSettings->SetValue("standard font", fStandardFontView->Font()); + fSettings->SetValue("serif font", fSerifFontView->Font()); + fSettings->SetValue("sans serif font", fSansSerifFontView->Font()); + fSettings->SetValue("fixed font", fFixedFontView->Font()); + int32 standardFontSize = _SizesMenuValue(fStandardSizesMenu->Menu()); + int32 fixedFontSize = _SizesMenuValue(fFixedSizesMenu->Menu()); + fSettings->SetValue("standard font size", standardFontSize); + fSettings->SetValue("fixed font size", fixedFontSize); + + fSettings->Save(); + + // Apply settings to default web page settings. + BWebSettings::Default()->SetStandardFont(fStandardFontView->Font()); + BWebSettings::Default()->SetSerifFont(fSerifFontView->Font()); + BWebSettings::Default()->SetSansSerifFont(fSansSerifFontView->Font()); + BWebSettings::Default()->SetFixedFont(fFixedFontView->Font()); + BWebSettings::Default()->SetDefaultStandardFontSize(standardFontSize); + BWebSettings::Default()->SetDefaultFixedFontSize(fixedFontSize); + + // This will find all currently instantiated page settings and apply + // the default values, unless the page settings have local overrides. + BWebSettings::Default()->Apply(); } void SettingsWindow::_RevertSettings() { + int32 defaultFontSize = fSettings->GetValue("standard font size", + kDefaultFontSize); + int32 defaultFixedFontSize = fSettings->GetValue("fixed font size", + kDefaultFontSize); + + _SetSizesMenuValue(fStandardSizesMenu->Menu(), defaultFontSize); + _SetSizesMenuValue(fFixedSizesMenu->Menu(), defaultFixedFontSize); + + fStandardFontView->SetFont(fSettings->GetValue("standard font", + *be_plain_font), defaultFontSize); + fSerifFontView->SetFont(fSettings->GetValue("serif font", + _FindDefaultSerifFont()), defaultFontSize); + fSansSerifFontView->SetFont(fSettings->GetValue("sans serif font", + *be_plain_font), defaultFontSize); + fFixedFontView->SetFont(fSettings->GetValue("fixed font", + *be_fixed_font), defaultFixedFontSize); } +void +SettingsWindow::_BuildSizesMenu(BMenu* menu, uint32 messageWhat) +{ + const float kMinSize = 8.0; + const float kMaxSize = 18.0; + + const int32 kSizes[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 21, 24, 0}; + + for (int32 i = 0; kSizes[i]; i++) { + int32 size = kSizes[i]; + if (size < kMinSize || size > kMaxSize) + continue; + + char label[32]; + snprintf(label, sizeof(label), "%ld", size); + + BMessage* message = new BMessage(messageWhat); + message->AddInt32("size", size); + + BMenuItem* item = new BMenuItem(label, message); + + menu->AddItem(item); + item->SetTarget(this); + } +} + + +void +SettingsWindow::_SetSizesMenuValue(BMenu* menu, int32 value) +{ + for (int32 i = 0; BMenuItem* item = menu->ItemAt(i); i++) { + bool marked = false; + if (BMessage* message = item->Message()) { + int32 size; + if (message->FindInt32("size", &size) == B_OK && size == value) + marked = true; + } + item->SetMarked(marked); + } +} + + +int32 +SettingsWindow::_SizesMenuValue(BMenu* menu) +{ + if (BMenuItem* item = menu->FindMarked()) { + if (BMessage* message = item->Message()) { + int32 size; + if (message->FindInt32("size", &size) == B_OK) + return size; + } + } + return kDefaultFontSize; +} + + +BFont +SettingsWindow::_FindDefaultSerifFont() const +{ + // Default to the first "serif" font we find. + BFont serifFont(*be_plain_font); + font_family family; + int32 familyCount = count_font_families(); + for (int32 i = 0; i < familyCount; i++) { + if (get_font_family(i, &family) == B_OK) { + BString familyString(family); + if (familyString.IFindFirst("sans") >= 0) + continue; + if (familyString.IFindFirst("serif") >= 0) { + serifFont.SetFamilyAndFace(family, B_REGULAR_FACE); + break; + } + } + } + return serifFont; +} diff --git a/src/apps/webpositive/SettingsWindow.h b/src/apps/webpositive/SettingsWindow.h index 76b215b03e..9a6b7d9373 100644 --- a/src/apps/webpositive/SettingsWindow.h +++ b/src/apps/webpositive/SettingsWindow.h @@ -30,6 +30,9 @@ #include class BButton; +class BMenu; +class BMenuField; +class FontSelectionView; class SettingsMessage; @@ -42,16 +45,33 @@ public: virtual void MessageReceived(BMessage* message); virtual bool QuitRequested(); + virtual void Show(); + private: void _ApplySettings(); void _RevertSettings(); + void _BuildSizesMenu(BMenu* menu, + uint32 messageWhat); + void _SetSizesMenuValue(BMenu* menu, int32 value); + int32 _SizesMenuValue(BMenu* menu); + + BFont _FindDefaultSerifFont() const; + private: SettingsMessage* fSettings; - BButton* fOkButton; + FontSelectionView* fStandardFontView; + FontSelectionView* fSerifFontView; + FontSelectionView* fSansSerifFontView; + FontSelectionView* fFixedFontView; + + BButton* fApplyButton; BButton* fCancelButton; BButton* fRevertButton; + + BMenuField* fStandardSizesMenu; + BMenuField* fFixedSizesMenu; }; diff --git a/src/apps/webpositive/support/FontSelectionView.cpp b/src/apps/webpositive/support/FontSelectionView.cpp new file mode 100644 index 0000000000..038f0cdacb --- /dev/null +++ b/src/apps/webpositive/support/FontSelectionView.cpp @@ -0,0 +1,489 @@ +/* + * Copyright 2001-2010, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Mark Hogben + * DarkWyrm + * Axel Dörfler, axeld@pinc-software.de + * Philippe Saint-Pierre, stpere@gmail.com + * Stephan Aßmus + */ + +#include "FontSelectionView.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#undef TR_CONTEXT +#define TR_CONTEXT "Font Selection view" + + +static const float kMinSize = 8.0; +static const float kMaxSize = 18.0; + +static const int32 kMsgSetFamily = 'fmly'; +static const int32 kMsgSetStyle = 'styl'; +static const int32 kMsgSetSize = 'size'; + + +// #pragma mark - + + +FontSelectionView::FontSelectionView(const char* name, const char* label, + bool separateStyles, const BFont* currentFont) + : + BHandler(name) +{ + if (currentFont == NULL) + fCurrentFont = _DefaultFont(); + else + fCurrentFont = *currentFont; + + fSavedFont = fCurrentFont; + + fSizesMenu = new BPopUpMenu("size menu"); + fFontsMenu = new BPopUpMenu("font menu"); + + // font menu + fFontsMenuField = new BMenuField("fonts", label, fFontsMenu, NULL); + fFontsMenuField->SetFont(be_bold_font); + + // styles menu, if desired + if (separateStyles) { + fStylesMenu = new BPopUpMenu("styles menu"); + fStylesMenuField = new BMenuField("styles", TR("Style:"), fStylesMenu, + NULL); + } else { + fStylesMenu = NULL; + fStylesMenuField = NULL; + } + + // size menu + fSizesMenuField = new BMenuField("size", TR("Size:"), fSizesMenu, NULL); + fSizesMenuField->SetAlignment(B_ALIGN_RIGHT); + + // preview + fPreviewText = new BStringView("preview text", + TR_CMT("The quick brown fox jumps over the lazy dog.","Don't translate this literally ! Use a phrase showing all chars from A to Z.")); + + fPreviewText->SetFont(&fCurrentFont); + fPreviewText->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED)); + fPreviewText->SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), + 1.65)); + fPreviewText->SetAlignment(B_ALIGN_RIGHT); +} + + +FontSelectionView::~FontSelectionView() +{ + // Some controls may not have been attached... + if (!fPreviewText->Window()) + delete fPreviewText; + if (!fSizesMenuField->Window()) + delete fSizesMenuField; + if (fStylesMenuField && !fStylesMenuField->Window()) + delete fStylesMenuField; + if (!fFontsMenuField->Window()) + delete fFontsMenuField; +} + + +void +FontSelectionView::AttachedToLooper() +{ + _BuildSizesMenu(); + UpdateFontsMenu(); +} + + +void +FontSelectionView::MessageReceived(BMessage* message) +{ + switch (message->what) { + case kMsgSetSize: + { + int32 size; + if (message->FindInt32("size", &size) != B_OK + || size == fCurrentFont.Size()) + break; + + fCurrentFont.SetSize(size); + _UpdateFontPreview(); + break; + } + + case kMsgSetFamily: + { + const char* family; + if (message->FindString("family", &family) != B_OK) + break; + + font_style style; + fCurrentFont.GetFamilyAndStyle(NULL, &style); + + BMenuItem* familyItem = fFontsMenu->FindItem(family); + if (familyItem != NULL) { + _SelectCurrentFont(false); + + BMenuItem* styleItem; + if (fStylesMenuField != NULL) + styleItem = fStylesMenuField->Menu()->FindMarked(); + else { + styleItem = familyItem->Submenu()->FindItem(style); + if (styleItem == NULL) + styleItem = familyItem->Submenu()->ItemAt(0); + } + + if (styleItem != NULL) { + styleItem->SetMarked(true); + fCurrentFont.SetFamilyAndStyle(family, styleItem->Label()); + _UpdateFontPreview(); + } + if (fStylesMenuField != NULL) + _AddStylesToMenu(fCurrentFont, fStylesMenuField->Menu()); + } + break; + } + + case kMsgSetStyle: + { + const char* family; + const char* style; + if (message->FindString("family", &family) != B_OK + || message->FindString("style", &style) != B_OK) + break; + + BMenuItem *familyItem = fFontsMenu->FindItem(family); + if (!familyItem) + break; + + _SelectCurrentFont(false); + familyItem->SetMarked(true); + + fCurrentFont.SetFamilyAndStyle(family, style); + _UpdateFontPreview(); + break; + } + + default: + BHandler::MessageReceived(message); + } +} + + +// #pragma mark - + + +void +FontSelectionView::SetFont(const BFont& font, float size) +{ + BFont resizedFont(font); + resizedFont.SetSize(size); + SetFont(resizedFont); +} + + +void +FontSelectionView::SetFont(const BFont& font) +{ + if (font == fCurrentFont && font == fSavedFont) + return; + + _SelectCurrentFont(false); + fSavedFont = fCurrentFont = font; + _UpdateFontPreview(); + + _SelectCurrentFont(true); + _SelectCurrentSize(true); +} + + +void +FontSelectionView::SetSize(float size) +{ + SetFont(fCurrentFont, size); +} + + +const BFont& +FontSelectionView::Font() const +{ + return fCurrentFont; +} + + +void +FontSelectionView::SetDefaults() +{ + BFont defaultFont = _DefaultFont(); + if (defaultFont == fCurrentFont) + return; + + _SelectCurrentFont(false); + + fCurrentFont = defaultFont; + _UpdateFontPreview(); + + _SelectCurrentFont(true); + _SelectCurrentSize(true); +} + + +void +FontSelectionView::Revert() +{ + if (!IsRevertable()) + return; + + _SelectCurrentFont(false); + + fCurrentFont = fSavedFont; + _UpdateFontPreview(); + + _SelectCurrentFont(true); + _SelectCurrentSize(true); +} + + +bool +FontSelectionView::IsDefaultable() +{ + return fCurrentFont != _DefaultFont(); +} + + +bool +FontSelectionView::IsRevertable() +{ + return fCurrentFont != fSavedFont; +} + + +void +FontSelectionView::UpdateFontsMenu() +{ + int32 numFamilies = count_font_families(); + + fFontsMenu->RemoveItems(0, fFontsMenu->CountItems(), true); + + BFont font = fCurrentFont; + + font_family currentFamily; + font_style currentStyle; + font.GetFamilyAndStyle(¤tFamily, ¤tStyle); + + for (int32 i = 0; i < numFamilies; i++) { + font_family family; + uint32 flags; + if (get_font_family(i, &family, &flags) != B_OK) + continue; + + // if we're setting the fixed font, we only want to show fixed fonts + if (!strcmp(Name(), "fixed") && (flags & B_IS_FIXED) == 0) + continue; + + font.SetFamilyAndFace(family, B_REGULAR_FACE); + + BMessage* message = new BMessage(kMsgSetFamily); + message->AddString("family", family); + message->AddString("name", Name()); + + BMenuItem* familyItem; + if (fStylesMenuField != NULL) { + familyItem = new BMenuItem(family, message); + } else { + // Each family item has a submenu with all styles for that font. + BMenu* stylesMenu = new BMenu(family); + _AddStylesToMenu(font, stylesMenu); + familyItem = new BMenuItem(stylesMenu, message); + } + + familyItem->SetMarked(strcmp(family, currentFamily) == 0); + fFontsMenu->AddItem(familyItem); + familyItem->SetTarget(this); + } + + // Separate styles menu for only the current font. + if (fStylesMenuField != NULL) + _AddStylesToMenu(fCurrentFont, fStylesMenuField->Menu()); +} + + +// #pragma mark - private + + +BLayoutItem* +FontSelectionView::CreateSizesLabelLayoutItem() +{ + return fSizesMenuField->CreateLabelLayoutItem(); +} + + +BLayoutItem* +FontSelectionView::CreateSizesMenuBarLayoutItem() +{ + return fSizesMenuField->CreateMenuBarLayoutItem(); +} + + +BLayoutItem* +FontSelectionView::CreateFontsLabelLayoutItem() +{ + return fFontsMenuField->CreateLabelLayoutItem(); +} + + +BLayoutItem* +FontSelectionView::CreateFontsMenuBarLayoutItem() +{ + return fFontsMenuField->CreateMenuBarLayoutItem(); +} + + +BLayoutItem* +FontSelectionView::CreateStylesLabelLayoutItem() +{ + if (fStylesMenuField) + return fStylesMenuField->CreateLabelLayoutItem(); + return NULL; +} + + +BLayoutItem* +FontSelectionView::CreateStylesMenuBarLayoutItem() +{ + if (fStylesMenuField) + return fStylesMenuField->CreateMenuBarLayoutItem(); + return NULL; +} + + +BView* +FontSelectionView::PreviewBox() const +{ + return fPreviewText; +} + + +// #pragma mark - private + + +BFont +FontSelectionView::_DefaultFont() const +{ + if (strcmp(Name(), "bold") == 0) + return *be_bold_font; + if (strcmp(Name(), "fixed") == 0) + return *be_fixed_font; + else + return *be_plain_font; +} + + +void +FontSelectionView::_SelectCurrentFont(bool select) +{ + font_family family; + font_style style; + fCurrentFont.GetFamilyAndStyle(&family, &style); + + BMenuItem *item = fFontsMenu->FindItem(family); + if (item != NULL) { + item->SetMarked(select); + + if (item->Submenu() != NULL) { + item = item->Submenu()->FindItem(style); + if (item != NULL) + item->SetMarked(select); + } + } +} + + +void +FontSelectionView::_SelectCurrentSize(bool select) +{ + char label[16]; + snprintf(label, sizeof(label), "%ld", (int32)fCurrentFont.Size()); + + BMenuItem* item = fSizesMenu->FindItem(label); + if (item != NULL) + item->SetMarked(select); +} + + +void +FontSelectionView::_UpdateFontPreview() +{ + fPreviewText->SetFont(&fCurrentFont); +} + + +void +FontSelectionView::_BuildSizesMenu() +{ + const int32 sizes[] = {7, 8, 9, 10, 11, 12, 13, 14, 18, 21, 24, 0}; + + // build size menu + for (int32 i = 0; sizes[i]; i++) { + int32 size = sizes[i]; + if (size < kMinSize || size > kMaxSize) + continue; + + char label[32]; + snprintf(label, sizeof(label), "%ld", size); + + BMessage* message = new BMessage(kMsgSetSize); + message->AddInt32("size", size); + message->AddString("name", Name()); + + BMenuItem* item = new BMenuItem(label, message); + if (size == fCurrentFont.Size()) + item->SetMarked(true); + + fSizesMenu->AddItem(item); + item->SetTarget(this); + } +} + + +void +FontSelectionView::_AddStylesToMenu(const BFont& font, BMenu* stylesMenu) const +{ + stylesMenu->RemoveItems(0, stylesMenu->CountItems(), true); + stylesMenu->SetRadioMode(true); + + font_family family; + font_style style; + font.GetFamilyAndStyle(&family, &style); + BString currentStyle(style); + + int32 numStyles = count_font_styles(family); + + for (int32 j = 0; j < numStyles; j++) { + if (get_font_style(family, j, &style) != B_OK) + continue; + + BMessage* message = new BMessage(kMsgSetStyle); + message->AddString("family", (char*)family); + message->AddString("style", (char*)style); + + BMenuItem* item = new BMenuItem(style, message); + item->SetMarked(currentStyle == style); + + stylesMenu->AddItem(item); + item->SetTarget(this); + } +} + diff --git a/src/apps/webpositive/support/FontSelectionView.h b/src/apps/webpositive/support/FontSelectionView.h new file mode 100644 index 0000000000..f25801a840 --- /dev/null +++ b/src/apps/webpositive/support/FontSelectionView.h @@ -0,0 +1,83 @@ +/* + * Copyright 2001-2010, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Mark Hogben + * DarkWyrm + * Axel Dörfler, axeld@pinc-software.de + * Philippe Saint-Pierre, stpere@gmail.com + * Stephan Aßmus + */ +#ifndef FONT_SELECTION_VIEW_H +#define FONT_SELECTION_VIEW_H + + +#include +#include + +class BLayoutItem; +class BBox; +class BMenu; +class BMenuField; +class BPopUpMenu; +class BStringView; +class BView; + + +class FontSelectionView : public BHandler { +public: + FontSelectionView(const char* name, + const char* label, bool separateStyles, + const BFont* font = NULL); + virtual ~FontSelectionView(); + + void AttachedToLooper(); + virtual void MessageReceived(BMessage* message); + + void SetFont(const BFont& font, float size); + void SetFont(const BFont& font); + void SetSize(float size); + const BFont& Font() const; + + void SetDefaults(); + void Revert(); + bool IsDefaultable(); + bool IsRevertable(); + + void UpdateFontsMenu(); + + BLayoutItem* CreateSizesLabelLayoutItem(); + BLayoutItem* CreateSizesMenuBarLayoutItem(); + + BLayoutItem* CreateFontsLabelLayoutItem(); + BLayoutItem* CreateFontsMenuBarLayoutItem(); + + BLayoutItem* CreateStylesLabelLayoutItem(); + BLayoutItem* CreateStylesMenuBarLayoutItem(); + + BView* PreviewBox() const; + +private: + BFont _DefaultFont() const; + void _SelectCurrentFont(bool select); + void _SelectCurrentSize(bool select); + void _UpdateFontPreview(); + void _BuildSizesMenu(); + void _AddStylesToMenu(const BFont& font, + BMenu* menu) const; + +protected: + BMenuField* fFontsMenuField; + BMenuField* fStylesMenuField; + BMenuField* fSizesMenuField; + BPopUpMenu* fFontsMenu; + BPopUpMenu* fStylesMenu; + BPopUpMenu* fSizesMenu; + BStringView* fPreviewText; + + BFont fSavedFont; + BFont fCurrentFont; +}; + +#endif // FONT_SELECTION_VIEW_H diff --git a/src/apps/webpositive/support/SettingsMessage.cpp b/src/apps/webpositive/support/SettingsMessage.cpp index dad98820b2..7c2edddfc5 100644 --- a/src/apps/webpositive/support/SettingsMessage.cpp +++ b/src/apps/webpositive/support/SettingsMessage.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2008, Stephan Aßmus . + * Copyright 2008-2010, Stephan Aßmus . * Copyright 1998, Eric Shepherd. * All rights reserved. Distributed under the terms of the Be Sample Code * license. @@ -203,8 +203,31 @@ SettingsMessage::SetValue(const char* name, const BFlattenable* value) } +status_t +SettingsMessage::SetValue(const char* name, const BFont& value) +{ + font_family family; + font_style style; + value.GetFamilyAndStyle(&family, &style); + + BMessage fontMessage; + status_t ret = fontMessage.AddString("family", family); + if (ret == B_OK) + ret = fontMessage.AddString("style", style); + if (ret == B_OK) + ret = fontMessage.AddFloat("size", value.Size()); + + if (ret == B_OK) { + if (ReplaceMessage(name, &fontMessage) != B_OK) + ret = AddMessage(name, &fontMessage); + } + return ret; +} + + // #pragma mark - + bool SettingsMessage::GetValue(const char* name, bool defaultValue) const { @@ -334,3 +357,29 @@ SettingsMessage::GetValue(const char* name, const BMessage& defaultValue) const return value; } + +BFont +SettingsMessage::GetValue(const char* name, const BFont& defaultValue) const +{ + BMessage fontMessage; + if (FindMessage(name, &fontMessage) != B_OK) + return defaultValue; + + const char* family; + const char* style; + float size; + if (fontMessage.FindString("family", &family) != B_OK + || fontMessage.FindString("style", &style) != B_OK + || fontMessage.FindFloat("size", &size) != B_OK) { + return defaultValue; + } + + BFont value; + if (value.SetFamilyAndStyle(family, style) != B_OK) + return defaultValue; + + value.SetSize(size); + + return value; +} + diff --git a/src/apps/webpositive/support/SettingsMessage.h b/src/apps/webpositive/support/SettingsMessage.h index f98121b5bb..85a29320b8 100644 --- a/src/apps/webpositive/support/SettingsMessage.h +++ b/src/apps/webpositive/support/SettingsMessage.h @@ -8,6 +8,7 @@ #define SETTINGS_MESSAGE_H #include +#include #include #include @@ -43,6 +44,8 @@ public: const BMessage* value); status_t SetValue(const char* name, const BFlattenable* value); + status_t SetValue(const char* name, + const BFont& value); bool GetValue(const char* name, bool defaultValue) const; @@ -70,6 +73,8 @@ public: const entry_ref& defaultValue) const; BMessage GetValue(const char* name, const BMessage& defaultValue) const; + BFont GetValue(const char* name, + const BFont& defaultValue) const; private: BPath fPath;