Appearance: Added BSpinners for setting font sizes.

Change-Id: Ic8036e512cda3e30b653092fa718b03e1b042ecc
Reviewed-on: https://review.haiku-os.org/710
Reviewed-by: Kacper Kasper <[email protected]>
This commit is contained in:
Mikael Konradsson
2018-11-20 20:36:24 +00:00
committed by waddlesplash
parent 35eb380e1a
commit 2d0c3dd649
3 changed files with 60 additions and 95 deletions
@@ -23,6 +23,7 @@
#include <PopUpMenu.h> #include <PopUpMenu.h>
#include <String.h> #include <String.h>
#include <StringView.h> #include <StringView.h>
#include <Spinner.h>
#include <FontPrivate.h> #include <FontPrivate.h>
@@ -38,7 +39,12 @@
// only on exit // only on exit
static const float kMinSize = 8.0; static const float kMinSize = 8.0;
static const float kMaxSize = 24.0; static const float kMaxSize = 72.0;
static const char *kPreviewText = B_TRANSLATE_COMMENT(
"The quick brown fox jumps over the lazy dog.",
"Don't translate this literally ! Use a phrase showing all chars "
"from A to Z.");
// private font API // private font API
@@ -98,36 +104,38 @@ FontSelectionView::FontSelectionView(const char* name,
fSavedFont = fCurrentFont; fSavedFont = fCurrentFont;
fSizesMenu = new BPopUpMenu("size menu");
_BuildSizesMenu();
fFontsMenu = new BPopUpMenu("font menu"); fFontsMenu = new BPopUpMenu("font menu");
// font menu // font menu
fFontsMenuField = new BMenuField("fonts", label, fFontsMenu); fFontsMenuField = new BMenuField("fonts", label, fFontsMenu);
fFontsMenuField->SetAlignment(B_ALIGN_RIGHT); fFontsMenuField->SetAlignment(B_ALIGN_RIGHT);
// size menu // font size
fSizesMenuField = new BMenuField("size", B_TRANSLATE("Size:"), fSizesMenu); BMessage* fontSizeMessage = new BMessage(kMsgSetSize);
fSizesMenuField->SetAlignment(B_ALIGN_RIGHT); fontSizeMessage->AddString("name", Name());
fFontSizeSpinner = new BSpinner("font size", B_TRANSLATE("Size:"),
fontSizeMessage);
fFontSizeSpinner->SetRange(kMinSize, kMaxSize);
fFontSizeSpinner->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
// preview // preview
fPreviewText = new BStringView("preview text", fPreviewTextView = new BStringView("preview text", kPreviewText);
B_TRANSLATE_COMMENT("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); fPreviewTextView->SetFont(&fCurrentFont);
fPreviewText->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); fPreviewTextView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
// box around preview // box around preview
fPreviewBox = new BBox("preview box", B_WILL_DRAW | B_FRAME_EVENTS); fPreviewBox = new BBox("preview box", B_WILL_DRAW | B_FRAME_EVENTS);
fPreviewBox->AddChild(BGroupLayoutBuilder(B_HORIZONTAL) fPreviewBox->AddChild(BGroupLayoutBuilder(B_HORIZONTAL)
.Add(fPreviewText) .Add(fPreviewTextView)
.SetInsets(B_USE_SMALL_SPACING, B_USE_SMALL_SPACING, .SetInsets(B_USE_SMALL_SPACING, B_USE_SMALL_SPACING,
B_USE_SMALL_SPACING, B_USE_SMALL_SPACING) B_USE_SMALL_SPACING, B_USE_SMALL_SPACING)
.TopView() .TopView()
); );
_SelectCurrentSize();
} }
@@ -143,26 +151,18 @@ void
FontSelectionView::SetTarget(BHandler* messageTarget) FontSelectionView::SetTarget(BHandler* messageTarget)
{ {
fMessageTarget = messageTarget; fMessageTarget = messageTarget;
fSizesMenu->SetTargetForItems(fMessageTarget); fFontSizeSpinner->SetTarget(messageTarget);
}
BView*
FontSelectionView::GetPreviewBox()
{
return fPreviewBox;
} }
void void
FontSelectionView::MessageReceived(BMessage *msg) FontSelectionView::MessageReceived(BMessage* msg)
{ {
switch (msg->what) { switch (msg->what) {
case kMsgSetSize: case kMsgSetSize:
{ {
int32 size; int32 size = fFontSizeSpinner->Value();
if (msg->FindInt32("size", &size) != B_OK if (size == fCurrentFont.Size())
|| size == fCurrentFont.Size())
break; break;
fCurrentFont.SetSize(size); fCurrentFont.SetSize(size);
@@ -179,11 +179,11 @@ FontSelectionView::MessageReceived(BMessage *msg)
font_style style; font_style style;
fCurrentFont.GetFamilyAndStyle(NULL, &style); fCurrentFont.GetFamilyAndStyle(NULL, &style);
BMenuItem *familyItem = fFontsMenu->FindItem(family); BMenuItem* familyItem = fFontsMenu->FindItem(family);
if (familyItem != NULL) { if (familyItem != NULL) {
_SelectCurrentFont(false); _SelectCurrentFont(false);
BMenuItem *item = familyItem->Submenu()->FindItem(style); BMenuItem* item = familyItem->Submenu()->FindItem(style);
if (item == NULL) if (item == NULL)
item = familyItem->Submenu()->ItemAt(0); item = familyItem->Submenu()->ItemAt(0);
@@ -221,63 +221,34 @@ FontSelectionView::MessageReceived(BMessage *msg)
} }
} }
BView*
BLayoutItem* FontSelectionView::GetPreviewBox() const
FontSelectionView::CreateSizesLabelLayoutItem()
{ {
return fSizesMenuField->CreateLabelLayoutItem(); return fPreviewBox;
}
BView*
FontSelectionView::GetFontSizeSpinner() const
{
return fFontSizeSpinner;
} }
BLayoutItem* BLayoutItem*
FontSelectionView::CreateSizesMenuBarLayoutItem() FontSelectionView::CreateFontsLabelLayoutItem() const
{
return fSizesMenuField->CreateMenuBarLayoutItem();
}
BLayoutItem*
FontSelectionView::CreateFontsLabelLayoutItem()
{ {
return fFontsMenuField->CreateLabelLayoutItem(); return fFontsMenuField->CreateLabelLayoutItem();
} }
BLayoutItem* BLayoutItem*
FontSelectionView::CreateFontsMenuBarLayoutItem() FontSelectionView::CreateFontsMenuBarLayoutItem() const
{ {
return fFontsMenuField->CreateMenuBarLayoutItem(); return fFontsMenuField->CreateMenuBarLayoutItem();
} }
void
FontSelectionView::_BuildSizesMenu()
{
const int32 sizes[] = {7, 8, 9, 10, 11, 12, 13, 14, 16, 18, 19, 20,
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), "%" B_PRId32, 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);
}
}
void void
FontSelectionView::_SelectCurrentFont(bool select) FontSelectionView::_SelectCurrentFont(bool select)
{ {
@@ -299,21 +270,19 @@ FontSelectionView::_SelectCurrentFont(bool select)
void void
FontSelectionView::_SelectCurrentSize(bool select) FontSelectionView::_SelectCurrentSize()
{ {
char label[16]; fFontSizeSpinner->SetValue((int32)fCurrentFont.Size());
snprintf(label, sizeof(label), "%" B_PRId32, (int32)fCurrentFont.Size());
BMenuItem* item = fSizesMenu->FindItem(label);
if (item != NULL)
item->SetMarked(select);
} }
void void
FontSelectionView::_UpdateFontPreview() FontSelectionView::_UpdateFontPreview()
{ {
fPreviewText->SetFont(&fCurrentFont); const BRect textFrame = fPreviewTextView->Bounds();
BString text = BString(kPreviewText);
fCurrentFont.TruncateString(&text, B_TRUNCATE_END, textFrame.Width());
fPreviewTextView->SetFont(&fCurrentFont);
fPreviewTextView->SetText(text);
#ifdef INSTANT_UPDATE #ifdef INSTANT_UPDATE
_UpdateSystemFont(); _UpdateSystemFont();
@@ -374,7 +343,7 @@ FontSelectionView::SetDefaults()
_UpdateFontPreview(); _UpdateFontPreview();
_SelectCurrentFont(true); _SelectCurrentFont(true);
_SelectCurrentSize(true); _SelectCurrentSize();
} }
@@ -390,7 +359,7 @@ FontSelectionView::Revert()
_UpdateFontPreview(); _UpdateFontPreview();
_SelectCurrentFont(true); _SelectCurrentFont(true);
_SelectCurrentSize(true); _SelectCurrentSize();
} }
@@ -486,7 +455,7 @@ FontSelectionView::UpdateFontsMenu()
message->AddString("style", (char*)style); message->AddString("style", (char*)style);
message->AddString("name", Name()); message->AddString("name", Name());
BMenuItem *item = new BMenuItem(style, message); BMenuItem* item = new BMenuItem(style, message);
if (!strcmp(style, currentStyle) if (!strcmp(style, currentStyle)
&& !strcmp(family, currentFamily)) { && !strcmp(family, currentFamily)) {
+9 -11
View File
@@ -19,6 +19,7 @@ class BBox;
class BMenuField; class BMenuField;
class BPopUpMenu; class BPopUpMenu;
class BStringView; class BStringView;
class BSpinner;
static const int32 kMsgSetFamily = 'fmly'; static const int32 kMsgSetFamily = 'fmly';
static const int32 kMsgSetStyle = 'styl'; static const int32 kMsgSetStyle = 'styl';
@@ -43,17 +44,14 @@ public:
void UpdateFontsMenu(); void UpdateFontsMenu();
BLayoutItem* CreateSizesLabelLayoutItem(); BLayoutItem* CreateFontsLabelLayoutItem() const;
BLayoutItem* CreateSizesMenuBarLayoutItem(); BLayoutItem* CreateFontsMenuBarLayoutItem() const;
BView* GetFontSizeSpinner() const;
BView* GetPreviewBox() const;
BLayoutItem* CreateFontsLabelLayoutItem();
BLayoutItem* CreateFontsMenuBarLayoutItem();
BView* GetPreviewBox();
private: private:
void _SelectCurrentFont(bool select); void _SelectCurrentFont(bool select);
void _SelectCurrentSize(bool select); void _SelectCurrentSize();
void _UpdateFontPreview(); void _UpdateFontPreview();
void _UpdateSystemFont(); void _UpdateSystemFont();
void _BuildSizesMenu(); void _BuildSizesMenu();
@@ -62,12 +60,12 @@ protected:
BHandler* fMessageTarget; BHandler* fMessageTarget;
BMenuField* fFontsMenuField; BMenuField* fFontsMenuField;
BMenuField* fSizesMenuField;
BPopUpMenu* fFontsMenu; BPopUpMenu* fFontsMenu;
BPopUpMenu* fSizesMenu;
BSpinner* fFontSizeSpinner;
BBox* fPreviewBox; BBox* fPreviewBox;
BStringView* fPreviewText; BStringView* fPreviewTextView;
BFont fSavedFont; BFont fSavedFont;
BFont fCurrentFont; BFont fCurrentFont;
+1 -3
View File
@@ -26,7 +26,6 @@
#include "APRWindow.h" #include "APRWindow.h"
#include "FontSelectionView.h" #include "FontSelectionView.h"
#undef B_TRANSLATION_CONTEXT #undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Font view" #define B_TRANSLATION_CONTEXT "Font view"
@@ -43,8 +42,7 @@ add_font_selection_view(BGridLayout* layout, FontSelectionView* view,
layout->AddItem(BSpaceLayoutItem::CreateGlue(), 2, row); layout->AddItem(BSpaceLayoutItem::CreateGlue(), 2, row);
layout->AddItem(view->CreateSizesLabelLayoutItem(), 3, row); layout->AddView(view->GetFontSizeSpinner(), 4, row);
layout->AddItem(view->CreateSizesMenuBarLayoutItem(), 4, row);
row++; row++;