Patch by Andrej Spielmann (GSOC):

* Extend the Fonts preflet to offer the new subpixel rendering and hinting
  options in a new "Advanced Settings" tab.
* Remove trailing white space.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26364 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2008-07-10 08:33:15 +00:00
parent 1d6aab3303
commit 66058774c8
9 changed files with 399 additions and 32 deletions

View File

@ -0,0 +1,279 @@
/*
* Copyright 2008, Haiku. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andrej Spielmann, <andrej.spielmann@seh.ox.ac.uk>
*/
#include "AdvancedSettingsView.h"
#include "MainWindow.h"
#include <Box.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <PopUpMenu.h>
#include <String.h>
#include <stdio.h>
#define INSTANT_UPDATE
// if defined, the changes will take place immediately and not only on exit
#define DISABLE_HINTING_CONTROL
// if defined, the hinting menu is disabled (hinting not properly
// implemented)
static const int32 kMsgSetAntialiasing = 'anti';
static const int32 kMsgSetHinting = 'hint';
static const char* kSubpixelLabel = "Subpixel antialiasing";
static const char* kGrayscaleLabel = "Grayscale antialiasing";
static const char* kNoHintingLabel = "Off";
static const char* kFullHintingLabel = "On";
// private font API
extern void _set_font_subpixel_antialiasing_(bool subpix);
extern status_t _get_font_subpixel_antialiasing_(bool* subpix);
extern void _set_hinting_(bool subpix);
extern status_t _get_hinting_(bool* subpix);
// #pragma mark -
AdvancedSettingsView::AdvancedSettingsView(BRect _rect, const char* name)
: BView(_rect, name, B_FOLLOW_LEFT_RIGHT, B_WILL_DRAW)
{
if (_get_font_subpixel_antialiasing_(&fCurrentSubpixelAntialiasing) != B_OK)
fCurrentSubpixelAntialiasing = false;
fSavedSubpixelAntialiasing = fCurrentSubpixelAntialiasing;
if (_get_hinting_(&fCurrentHinting) != B_OK)
fCurrentHinting = true;
fSavedHinting = fCurrentHinting;
fDivider = StringWidth("Character hinting:") + 5;
fAntialiasingMenu = new BPopUpMenu("Antialiasing menu");
fHintingMenu = new BPopUpMenu("Hinting menu");
// antialiasing menu
BRect rect(Bounds());
fAntialiasingMenuField = new BMenuField(rect, "antialiasing",
"Antialiasing type:", fAntialiasingMenu, false);
fAntialiasingMenuField->SetDivider(fDivider);
fAntialiasingMenuField->SetAlignment(B_ALIGN_RIGHT);
fAntialiasingMenuField->ResizeToPreferred();
AddChild(fAntialiasingMenuField);
_BuildAntialiasingMenu();
// hinting menu
float shift = fAntialiasingMenuField->Bounds().Height()+5;
rect.top += shift;
rect.bottom += shift;
fHintingMenuField = new BMenuField(rect, "hinting",
"Character hinting:", fHintingMenu, false);
fHintingMenuField->SetDivider(fDivider);
fHintingMenuField->SetAlignment(B_ALIGN_RIGHT);
fHintingMenuField->ResizeToPreferred();
AddChild(fHintingMenuField);
_BuildHintingMenu();
#ifdef DISABLE_HINTING_CONTROL
fHintingMenuField->SetEnabled(false);
#endif
_SetCurrentAntialiasing();
_SetCurrentHinting();
}
AdvancedSettingsView::~AdvancedSettingsView()
{
#ifndef INSTANT_UPDATE
_set_font_subpixel_antialiasing_(fCurrentSubpixelAntialiasing);
_set_hinting(fCurrentHinting);
#endif
}
void
AdvancedSettingsView::GetPreferredSize(float *_width, float *_height)
{
// don't change the width if it is large enough
if (_width) {
*_width = (StringWidth(kSubpixelLabel) > StringWidth(kGrayscaleLabel) ?
StringWidth(kSubpixelLabel) : StringWidth(kGrayscaleLabel))
+ fDivider + 30;
if (*_width < Bounds().Width())
*_width = Bounds().Width();
}
*_height = fHintingMenuField->Frame().bottom;
}
void
AdvancedSettingsView::SetDivider(float divider)
{
fAntialiasingMenuField->SetDivider(divider);
fHintingMenuField->SetDivider(divider);
fDivider = divider;
}
void
AdvancedSettingsView::RelayoutIfNeeded()
{
float width, height;
GetPreferredSize(&width, &height);
if (width > Bounds().Width() || height > Bounds().Height()) {
ResizeTo(width, height);
}
}
void
AdvancedSettingsView::AttachedToWindow()
{
if (Parent() != NULL)
SetViewColor(Parent()->ViewColor());
else
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fAntialiasingMenu->SetTargetForItems(this);
fHintingMenu->SetTargetForItems(this);
}
void
AdvancedSettingsView::MessageReceived(BMessage *msg)
{
switch (msg->what) {
case kMsgSetAntialiasing:
{
bool subpixelAntialiasing;
if (msg->FindBool("antialiasing", &subpixelAntialiasing) != B_OK
|| subpixelAntialiasing == fCurrentSubpixelAntialiasing)
break;
fCurrentSubpixelAntialiasing = subpixelAntialiasing;
#ifdef INSTANT_UPDATE
_set_font_subpixel_antialiasing_(fCurrentSubpixelAntialiasing);
#endif
Window()->PostMessage(kMsgUpdate);
break;
}
case kMsgSetHinting:
{
bool hinting;
if (msg->FindBool("hinting", &hinting) != B_OK
|| hinting == fCurrentHinting)
break;
fCurrentHinting = hinting;
#ifdef INSTANT_UPDATE
_set_hinting_(fCurrentHinting);
#endif
Window()->PostMessage(kMsgUpdate);
break;
}
default:
BView::MessageReceived(msg);
}
}
void
AdvancedSettingsView::_BuildAntialiasingMenu()
{
BMessage* message = new BMessage(kMsgSetAntialiasing);
message->AddBool("antialiasing", false);
BMenuItem* item = new BMenuItem(kGrayscaleLabel, message);
fAntialiasingMenu->AddItem(item);
BMessage* message2 = new BMessage(kMsgSetAntialiasing);
message2->AddBool("antialiasing", true);
BMenuItem* item2 = new BMenuItem(kSubpixelLabel, message2);
fAntialiasingMenu->AddItem(item2);
}
void
AdvancedSettingsView::_BuildHintingMenu()
{
BMessage* message = new BMessage(kMsgSetHinting);
message->AddBool("hinting", false);
BMenuItem* item = new BMenuItem(kNoHintingLabel, message);
fHintingMenu->AddItem(item);
BMessage* message2 = new BMessage(kMsgSetHinting);
message2->AddBool("hinting", true);
BMenuItem* item2 = new BMenuItem(kFullHintingLabel, message2);
fHintingMenu->AddItem(item2);
}
void
AdvancedSettingsView::_SetCurrentAntialiasing()
{
BMenuItem *item = fAntialiasingMenu->FindItem(
fCurrentSubpixelAntialiasing ? kSubpixelLabel : kGrayscaleLabel);
if (item != NULL)
item->SetMarked(true);
}
void
AdvancedSettingsView::_SetCurrentHinting()
{
BMenuItem *item = fHintingMenu->FindItem(
fCurrentHinting ? kFullHintingLabel : kNoHintingLabel);
if (item != NULL)
item->SetMarked(true);
}
void
AdvancedSettingsView::SetDefaults()
{
}
bool
AdvancedSettingsView::IsDefaultable()
{
return false;
}
bool
AdvancedSettingsView::IsRevertable()
{
return (fCurrentSubpixelAntialiasing != fSavedSubpixelAntialiasing)
|| (fCurrentHinting != fSavedHinting);
}
void
AdvancedSettingsView::Revert()
{
if (!IsRevertable())
return;
fCurrentSubpixelAntialiasing = fSavedSubpixelAntialiasing;
fCurrentHinting = fSavedHinting;
#ifdef INSTANT_UPDATE
_set_font_subpixel_antialiasing_(fCurrentSubpixelAntialiasing);
_set_hinting_(fCurrentHinting);
#endif
_SetCurrentAntialiasing();
_SetCurrentHinting();
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2001-2005, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andrej Spielmann, <andrej.spielmann@seh.ox.ac.uk>
*/
#ifndef ADVANCED_SETTINGS_VIEW_H
#define ADVANCED_SETTINGS_VIEW_H
#include <View.h>
class BBox;
class BMenuField;
class BPopUpMenu;
class AdvancedSettingsView : public BView {
public:
AdvancedSettingsView(BRect rect, const char* name);
virtual ~AdvancedSettingsView();
virtual void GetPreferredSize(float *_width, float *_height);
virtual void RelayoutIfNeeded();
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage *msg);
void SetDivider(float divider);
void SetDefaults();
void Revert();
bool IsDefaultable();
bool IsRevertable();
private:
void _BuildAntialiasingMenu();
void _SetCurrentAntialiasing();
void _BuildHintingMenu();
void _SetCurrentHinting();
protected:
float fDivider;
BMenuField* fAntialiasingMenuField;
BPopUpMenu* fAntialiasingMenu;
BMenuField* fHintingMenuField;
BPopUpMenu* fHintingMenu;
bool fSavedSubpixelAntialiasing;
bool fCurrentSubpixelAntialiasing;
bool fSavedHinting;
bool fCurrentHinting;
};
#endif /* ADVANCED_SETTINGS_VIEW_H */

View File

@ -114,7 +114,7 @@ FontSelectionView::FontSelectionView(BRect _rect, const char* name,
// it doesn't overlap the box outline.
rect = fPreviewBox->Bounds().InsetByCopy(3, 3);
fPreviewText = new BStringView(rect, "preview text",
"The quick brown fox jumps over the lazy dog.",
"The quick brown fox jumps over the lazy dog.",
B_FOLLOW_LEFT_RIGHT);
fPreviewText->SetFont(&fCurrentFont);
fPreviewBox->AddChild(fPreviewText);
@ -172,7 +172,7 @@ FontSelectionView::RelayoutIfNeeded()
GetPreferredSize(&width, &height);
if (width > Bounds().Width()) {
fSizesMenuField->MoveTo(fMaxFontNameWidth + fDivider + 40.0f,
fSizesMenuField->MoveTo(fMaxFontNameWidth + fDivider + 40.0f,
fFontsMenuField->Bounds().top);
ResizeTo(width, height);
}
@ -424,7 +424,7 @@ FontSelectionView::IsRevertable()
void
FontSelectionView::UpdateFontsMenu()
{
int32 numFamilies = count_font_families();
int32 numFamilies = count_font_families();
fFontsMenu->RemoveItems(0, fFontsMenu->CountItems(), true);
BFont font;
@ -434,8 +434,8 @@ FontSelectionView::UpdateFontsMenu()
font_style currentStyle;
fCurrentFont.GetFamilyAndStyle(&currentFamily, &currentStyle);
for (int32 i = 0; i < numFamilies; i++) {
font_family family;
for (int32 i = 0; i < numFamilies; i++) {
font_family family;
uint32 flags;
if (get_font_family(i, &family, &flags) != B_OK)
continue;
@ -457,10 +457,10 @@ FontSelectionView::UpdateFontsMenu()
BMenuItem* familyItem = new BMenuItem(stylesMenu, message);
fFontsMenu->AddItem(familyItem);
int32 numStyles = count_font_styles(family);
int32 numStyles = count_font_styles(family);
for (int32 j = 0; j < numStyles; j++) {
font_style style;
font_style style;
if (get_font_style(family, j, &style, &flags) != B_OK)
continue;

View File

@ -61,5 +61,5 @@ class FontSelectionView : public BView {
BFont fCurrentFont;
float fMaxFontNameWidth;
};
#endif /* FONT_SELECTION_VIEW_H */

View File

@ -12,6 +12,7 @@
#include "FontSelectionView.h"
#include "AdvancedSettingsView.h"
class FontView : public BView {
@ -29,9 +30,9 @@ class FontView : public BView {
bool IsRevertable();
private:
FontSelectionView *fPlainView;
FontSelectionView *fBoldView;
FontSelectionView *fFixedView;
FontSelectionView *fPlainView;
FontSelectionView *fBoldView;
FontSelectionView *fFixedView;
};
#endif /* FONT_VIEW_H */

View File

@ -32,27 +32,27 @@ FontsSettings::FontsSettings()
path.Append(kSettingsFile);
BFile file(path.Path(), B_READ_ONLY);
if (file.InitCheck() != B_OK)
if (file.InitCheck() != B_OK)
SetDefaults();
else if (msg.Unflatten(&file) != B_OK)
SetDefaults();
else
msg.FindPoint("windowlocation", &fCorner);
}
}
}
FontsSettings::~FontsSettings()
{
BPath path;
BMessage msg;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) < B_OK)
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) < B_OK)
return;
path.Append(kSettingsFile);
BFile file(path.Path(), B_WRITE_ONLY|B_CREATE_FILE);
if (file.InitCheck() == B_OK) {
msg.AddPoint("windowlocation", fCorner);
msg.Flatten(&file);

View File

@ -5,6 +5,7 @@ AddSubDirSupportedPlatforms libbe_test ;
Preference Fonts :
FontSelectionView.cpp
AdvancedSettingsView.cpp
FontsSettings.cpp
FontView.cpp
main.cpp

View File

@ -6,6 +6,7 @@
* Mark Hogben
* DarkWyrm <bpmagic@columbus.rr.com>
* Axel Dörfler, axeld@pinc-software.de
* Andrej Spielmann, <andrej.spielmann@seh.ox.ac.uk>
*/
@ -25,7 +26,7 @@ static const uint32 kMsgCheckFonts = 'chkf';
MainWindow::MainWindow()
: BWindow(BRect(100, 100, 445, 340), "Fonts", B_TITLED_WINDOW,
: BWindow(BRect(100, 100, 445, 410), "Fonts", B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
{
BRect rect = Bounds();
@ -36,7 +37,8 @@ MainWindow::MainWindow()
rect.left = 10;
rect.top = rect.bottom - 10;
fDefaultsButton = new BButton(rect, "defaults", "Defaults",
new BMessage(kMsgSetDefaults), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM, B_WILL_DRAW);
new BMessage(kMsgSetDefaults), B_FOLLOW_LEFT
| B_FOLLOW_BOTTOM, B_WILL_DRAW);
fDefaultsButton->ResizeToPreferred();
fDefaultsButton->SetEnabled(false);
float buttonHeight = fDefaultsButton->Bounds().Height();
@ -56,11 +58,13 @@ MainWindow::MainWindow()
rect.top += 5;
rect.bottom -= 20 + buttonHeight;
rect.left += 5;
BTabView *tabView = new BTabView(rect, "tabview", B_WIDTH_FROM_LABEL);
BTabView *tabView = new BTabView(rect, "tabview", B_WIDTH_FROM_LABEL);
rect = tabView->ContainerView()->Bounds().InsetByCopy(5, 8);
fFontsView = new FontView(rect);
fAdvancedSettings = new AdvancedSettingsView(rect, "Advanced");
tabView->AddTab(fFontsView);
fFontsView->UpdateFonts();
@ -83,7 +87,8 @@ MainWindow::MainWindow()
tabView->ContainerView()->ResizeBy(0, heightDiff);
}
ResizeTo(tabView->Bounds().Width() + 10, tabView->Frame().bottom + 20 + buttonHeight);
ResizeTo(tabView->Bounds().Width() + 10, tabView->Frame().bottom + 20
+ buttonHeight);
view->AddChild(tabView);
fFontsView->ResizeToPreferred();
@ -99,6 +104,23 @@ MainWindow::MainWindow()
_Center();
}
tabView->AddTab(fAdvancedSettings);
fAdvancedSettings->RelayoutIfNeeded();
fAdvancedSettings->GetPreferredSize(&width, &height);
widthDiff = width + 10 - tabView->ContainerView()->Bounds().Width();
if (widthDiff > 0) {
tabView->ResizeBy(widthDiff, 0);
tabView->ContainerView()->ResizeBy(widthDiff, 0);
}
heightDiff = height + 16 - tabView->ContainerView()->Bounds().Height();
if (heightDiff > 0) {
tabView->ResizeBy(0, heightDiff);
tabView->ContainerView()->ResizeBy(0, heightDiff);
}
fRunner = new BMessageRunner(this, new BMessage(kMsgCheckFonts), 3000000);
// every 3 seconds
@ -127,19 +149,25 @@ MainWindow::MessageReceived(BMessage *message)
{
switch (message->what) {
case kMsgUpdate:
fDefaultsButton->SetEnabled(fFontsView->IsDefaultable());
fRevertButton->SetEnabled(fFontsView->IsRevertable());
fDefaultsButton->SetEnabled(fFontsView->IsDefaultable()
|| fAdvancedSettings->IsDefaultable());
fRevertButton->SetEnabled(fFontsView->IsRevertable()
|| fAdvancedSettings->IsRevertable());
break;
case kMsgSetDefaults:
fFontsView->SetDefaults();
fAdvancedSettings->SetDefaults();
fDefaultsButton->SetEnabled(false);
fRevertButton->SetEnabled(fFontsView->IsRevertable());
fRevertButton->SetEnabled(fFontsView->IsRevertable()
|| fAdvancedSettings->IsRevertable());
break;
case kMsgRevert:
fFontsView->Revert();
fDefaultsButton->SetEnabled(fFontsView->IsDefaultable());
fAdvancedSettings->Revert();
fDefaultsButton->SetEnabled(fFontsView->IsDefaultable()
|| fAdvancedSettings->IsDefaultable());
fRevertButton->SetEnabled(false);
break;

View File

@ -12,6 +12,7 @@
#include "FontsSettings.h"
#include "AdvancedSettingsView.h"
#include <Window.h>
@ -31,12 +32,13 @@ class MainWindow : public BWindow {
private:
void _Center();
BMessageRunner* fRunner;
FontView* fFontsView;
BButton* fDefaultsButton;
BButton* fRevertButton;
BMessageRunner* fRunner;
FontView* fFontsView;
BButton* fDefaultsButton;
BButton* fRevertButton;
FontsSettings fSettings;
FontsSettings fSettings;
AdvancedSettingsView* fAdvancedSettings;
};
static const int32 kMsgUpdate = 'updt';