diff --git a/src/preferences/appearance/APRView.cpp b/src/preferences/appearance/APRView.cpp index 8dbb5f1632..952efad6c4 100644 --- a/src/preferences/appearance/APRView.cpp +++ b/src/preferences/appearance/APRView.cpp @@ -37,9 +37,9 @@ #define DECORATOR_CHANGED 'dcch' -APRView::APRView(const char* name, uint32 flags) +APRView::APRView(const char* name) : - BView(name, flags), + BView(name, B_WILL_DRAW), fDefaultSet(ColorSet::DefaultColorSet()) { SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); @@ -136,7 +136,7 @@ APRView::MessageReceived(BMessage *msg) if (msg->FindData("RGBColor", (type_code)'RGBC', (const void**)&color, &size) == B_OK) { - SetCurrentColor(*color); + _SetCurrentColor(*color); } } @@ -145,7 +145,7 @@ APRView::MessageReceived(BMessage *msg) { // Received from the color fPicker when its color changes rgb_color color = fPicker->ValueAsColor(); - SetCurrentColor(color); + _SetCurrentColor(color); Window()->PostMessage(kMsgUpdate); break; @@ -161,27 +161,7 @@ APRView::MessageReceived(BMessage *msg) fWhich = item->ColorWhich(); rgb_color color = fCurrentSet.GetColor(fWhich); - SetCurrentColor(color); - - Window()->PostMessage(kMsgUpdate); - break; - } - case REVERT_SETTINGS: - { - fCurrentSet = fPrevSet; - - UpdateControls(); - UpdateAllColors(); - - Window()->PostMessage(kMsgUpdate); - break; - } - case DEFAULT_SETTINGS: - { - fCurrentSet = ColorSet::DefaultColorSet(); - - UpdateControls(); - UpdateAllColors(); + _SetCurrentColor(color); Window()->PostMessage(kMsgUpdate); break; @@ -205,6 +185,30 @@ APRView::LoadSettings() } +void +APRView::SetDefaults() +{ + fCurrentSet = ColorSet::DefaultColorSet(); + + _UpdateControls(); + _UpdateAllColors(); + + Window()->PostMessage(kMsgUpdate); +} + + +void +APRView::Revert() +{ + fCurrentSet = fPrevSet; + + _UpdateControls(); + _UpdateAllColors(); + + Window()->PostMessage(kMsgUpdate); +} + + bool APRView::IsDefaultable() { @@ -217,8 +221,39 @@ APRView::IsDefaultable() } +bool +APRView::IsRevertable() +{ + for (int32 i = 0; i < color_description_count(); i++) { + color_which which = get_color_description(i)->which; + if (fCurrentSet.GetColor(which) != fPrevSet.GetColor(which)) + return true; + } + return false; +} + + void -APRView::UpdateAllColors() +APRView::_SetCurrentColor(rgb_color color) +{ + fCurrentSet.SetColor(fWhich, color); + set_ui_color(fWhich, color); + _UpdateControls(); +} + + +void +APRView::_UpdateControls() +{ + rgb_color color = fCurrentSet.GetColor(fWhich); + fPicker->SetValue(color); + fColorWell->SetColor(color); + fColorWell->Invalidate(); +} + + +void +APRView::_UpdateAllColors() { for (int32 i = 0; i < color_description_count(); i++) { color_which which = get_color_description(i)->which; @@ -226,22 +261,3 @@ APRView::UpdateAllColors() set_ui_color(which, color); } } - - -void -APRView::SetCurrentColor(rgb_color color) -{ - fCurrentSet.SetColor(fWhich, color); - set_ui_color(fWhich, color); - UpdateControls(); -} - - -void -APRView::UpdateControls() -{ - rgb_color color = fCurrentSet.GetColor(fWhich); - fPicker->SetValue(color); - fColorWell->SetColor(color); - fColorWell->Invalidate(); -} diff --git a/src/preferences/appearance/APRView.h b/src/preferences/appearance/APRView.h index b447927a15..1cfd6003da 100644 --- a/src/preferences/appearance/APRView.h +++ b/src/preferences/appearance/APRView.h @@ -1,10 +1,11 @@ /* - * Copyright 2002-2008, Haiku. All rights reserved. + * Copyright 2002-2012, Haiku. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * DarkWyrm (darkwyrm@earthlink.net) * Rene Gollent (rene@gollent.com) + * Stephan Aßmus */ #ifndef APR_VIEW_H_ #define APR_VIEW_H_ @@ -36,33 +37,39 @@ class APRWindow; class APRView : public BView { public: - APRView(const char *name, uint32 flags); - ~APRView(void); - void AttachedToWindow(void); - void MessageReceived(BMessage *msg); + APRView(const char *name); + virtual ~APRView(); - void LoadSettings(void); - bool IsDefaultable(void); + virtual void AttachedToWindow(); + virtual void MessageReceived(BMessage *msg); -protected: + void LoadSettings(); - void SetCurrentColor(rgb_color color); - void UpdateControls(); - void UpdateAllColors(); + void SetDefaults(); + void Revert(); - BColorControl *fPicker; + bool IsDefaultable(); + bool IsRevertable(); - BListView *fAttrList; +private: + void _SetCurrentColor(rgb_color color); + void _UpdateControls(); + void _UpdateAllColors(); - color_which fWhich; +private: + BColorControl* fPicker; - BScrollView *fScrollView; + BListView* fAttrList; - ColorWell *fColorWell; + color_which fWhich; - ColorSet fCurrentSet; - ColorSet fPrevSet; - ColorSet fDefaultSet; + BScrollView* fScrollView; + + ColorWell* fColorWell; + + ColorSet fCurrentSet; + ColorSet fPrevSet; + ColorSet fDefaultSet; }; #endif diff --git a/src/preferences/appearance/APRWindow.cpp b/src/preferences/appearance/APRWindow.cpp index 2c05d13e4c..dbc2ff5329 100644 --- a/src/preferences/appearance/APRWindow.cpp +++ b/src/preferences/appearance/APRWindow.cpp @@ -5,6 +5,7 @@ * Authors: * DarkWyrm (darkwyrm@earthlink.net) * Alexander von Gluck, kallisti5@unixzen.com + * Stephan Aßmus */ @@ -18,8 +19,11 @@ #include #include +#include "AntialiasingSettingsView.h" #include "APRView.h" +#include "DecorSettingsView.h" #include "defs.h" +#include "FontView.h" #undef B_TRANSLATE_CONTEXT @@ -33,9 +37,9 @@ static const uint32 kMsgRevert = 'rvrt'; APRWindow::APRWindow(BRect frame) : BWindow(frame, B_TRANSLATE_SYSTEM_NAME("Appearance"), B_TITLED_WINDOW, - B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS, B_ALL_WORKSPACES) + B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_QUIT_ON_WINDOW_CLOSE, + B_ALL_WORKSPACES) { - SetLayout(new BGroupLayout(B_HORIZONTAL)); fDefaultsButton = new BButton("defaults", B_TRANSLATE("Defaults"), @@ -46,22 +50,22 @@ APRWindow::APRWindow(BRect frame) BTabView* tabView = new BTabView("tabview", B_WIDTH_FROM_LABEL); - fAntialiasingSettings = new AntialiasingSettingsView( - B_TRANSLATE("Antialiasing")); + fFontSettings = new FontView(B_TRANSLATE("Fonts")); + + fColorsView = new APRView(B_TRANSLATE("Colors")); fDecorSettings = new DecorSettingsView( B_TRANSLATE("Window Decorator")); - fColorsView = new APRView(B_TRANSLATE("Colors"), B_WILL_DRAW); + fAntialiasingSettings = new AntialiasingSettingsView( + B_TRANSLATE("Antialiasing")); + tabView->AddTab(fFontSettings); tabView->AddTab(fColorsView); - tabView->AddTab(fAntialiasingSettings); tabView->AddTab(fDecorSettings); + tabView->AddTab(fAntialiasingSettings); - fDefaultsButton->SetEnabled(fColorsView->IsDefaultable() - || fAntialiasingSettings->IsDefaultable() - || fDecorSettings->IsDefaultable()); - fRevertButton->SetEnabled(false); + _UpdateButtons(); AddChild(BGroupLayoutBuilder(B_VERTICAL, 0) .Add(tabView) @@ -81,23 +85,25 @@ APRWindow::MessageReceived(BMessage *message) { switch (message->what) { case kMsgUpdate: - fDefaultsButton->SetEnabled(fColorsView->IsDefaultable() - || fAntialiasingSettings->IsDefaultable()); - fRevertButton->SetEnabled(true); + _UpdateButtons(); break; + case kMsgSetDefaults: - fColorsView -> MessageReceived(new BMessage(DEFAULT_SETTINGS)); + fFontSettings->SetDefaults(); + fColorsView->SetDefaults(); fAntialiasingSettings->SetDefaults(); - fDefaultsButton->SetEnabled(false); - fRevertButton->SetEnabled(true); + fDecorSettings->SetDefaults(); + + _UpdateButtons(); break; case kMsgRevert: - fColorsView -> MessageReceived(new BMessage(REVERT_SETTINGS)); + fColorsView->Revert(); fAntialiasingSettings->Revert(); - fDefaultsButton->SetEnabled(fColorsView->IsDefaultable() - || fAntialiasingSettings->IsDefaultable()); - fRevertButton->SetEnabled(false); + fFontSettings->Revert(); + fDecorSettings->Revert(); + + _UpdateButtons(); break; default: @@ -107,9 +113,37 @@ APRWindow::MessageReceived(BMessage *message) } -bool -APRWindow::QuitRequested(void) +void +APRWindow::_UpdateButtons() { - be_app->PostMessage(B_QUIT_REQUESTED); - return(true); + fDefaultsButton->SetEnabled(_IsDefaultable()); + fRevertButton->SetEnabled(_IsRevertable()); +} + + +bool +APRWindow::_IsDefaultable() const +{ +// printf("fonts defaultable: %d\n", fFontSettings->IsDefaultable()); +// printf("colors defaultable: %d\n", fColorsView->IsDefaultable()); +// printf("AA defaultable: %d\n", fAntialiasingSettings->IsDefaultable()); +// printf("decor defaultable: %d\n", fDecorSettings->IsDefaultable()); + return fColorsView->IsDefaultable() + || fFontSettings->IsDefaultable() + || fAntialiasingSettings->IsDefaultable() + || fDecorSettings->IsDefaultable(); +} + + +bool +APRWindow::_IsRevertable() const +{ +// printf("fonts revertable: %d\n", fFontSettings->IsRevertable()); +// printf("colors revertable: %d\n", fColorsView->IsRevertable()); +// printf("AA revertable: %d\n", fAntialiasingSettings->IsRevertable()); +// printf("decor revertable: %d\n", fDecorSettings->IsRevertable()); + return fColorsView->IsRevertable() + || fFontSettings->IsRevertable() + || fAntialiasingSettings->IsRevertable() + || fDecorSettings->IsRevertable(); } diff --git a/src/preferences/appearance/APRWindow.h b/src/preferences/appearance/APRWindow.h index 6fbecdf7b4..ab6f582f16 100644 --- a/src/preferences/appearance/APRWindow.h +++ b/src/preferences/appearance/APRWindow.h @@ -1,10 +1,11 @@ /* - * Copyright 2002-2011, Haiku. All rights reserved. + * Copyright 2002-2012, Haiku. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * DarkWyrm (darkwyrm@earthlink.net) * Alexander von Gluck, kallisti5@unixzen.com + * Stephan Aßmus */ #ifndef APR_WINDOW_H #define APR_WINDOW_H @@ -16,27 +17,32 @@ #include #include -#include "APRView.h" -#include "AntialiasingSettingsView.h" -#include "DecorSettingsView.h" +class APRView; +class AntialiasingSettingsView; +class FontView; +class DecorSettingsView; -class APRWindow : public BWindow -{ +class APRWindow : public BWindow { public: APRWindow(BRect frame); - bool QuitRequested(void); void MessageReceived(BMessage *message); private: + void _UpdateButtons(); + bool _IsDefaultable() const; + bool _IsRevertable() const; + APRView* fColorsView; BButton* fDefaultsButton; BButton* fRevertButton; AntialiasingSettingsView* fAntialiasingSettings; + FontView* fFontSettings; DecorSettingsView* fDecorSettings; }; + static const int32 kMsgUpdate = 'updt'; diff --git a/src/preferences/appearance/FontSelectionView.cpp b/src/preferences/appearance/FontSelectionView.cpp new file mode 100644 index 0000000000..3f00f53b35 --- /dev/null +++ b/src/preferences/appearance/FontSelectionView.cpp @@ -0,0 +1,494 @@ +/* + * Copyright 2001-2011, 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 B_TRANSLATE_CONTEXT +#define B_TRANSLATE_CONTEXT "Font Selection view" + + +#define INSTANT_UPDATE + // if defined, the system font will be updated immediately, and not + // only on exit + +static const float kMinSize = 8.0; +static const float kMaxSize = 18.0; + + +// private font API +extern void _set_system_font_(const char *which, font_family family, + font_style style, float size); +extern status_t _get_system_default_font_(const char* which, + font_family family, font_style style, float* _size); + + +#ifdef B_BEOS_VERSION_DANO +// this call only exists under R5 +void +_set_system_font_(const char *which, font_family family, + font_style style, float size) +{ + puts("you don't have _set_system_font_()"); +} +#endif + +#if !defined(HAIKU_TARGET_PLATFORM_HAIKU) && !defined(HAIKU_TARGET_PLATFORM_LIBBE_TEST) +// this call only exists under Haiku (and the test environment) +status_t +_get_system_default_font_(const char* which, font_family family, + font_style style, float* _size) +{ + puts("you don't have _get_system_default_font_()"); + return B_ERROR; +} +#endif + + +// #pragma mark - + + +FontSelectionView::FontSelectionView(const char* name, + const char* label, const BFont* currentFont) + : + BView(name, B_WILL_DRAW), + fMessageTarget(this) +{ + if (currentFont == NULL) { + if (!strcmp(Name(), "plain")) + fCurrentFont = *be_plain_font; + else if (!strcmp(Name(), "bold")) + fCurrentFont = *be_bold_font; + else if (!strcmp(Name(), "fixed")) + fCurrentFont = *be_fixed_font; + else if (!strcmp(Name(), "menu")) { + menu_info info; + get_menu_info(&info); + + fCurrentFont.SetFamilyAndStyle(info.f_family, info.f_style); + fCurrentFont.SetSize(info.font_size); + } + } else + fCurrentFont = *currentFont; + + fSavedFont = fCurrentFont; + + fSizesMenu = new BPopUpMenu("size menu"); + _BuildSizesMenu(); + + fFontsMenu = new BPopUpMenu("font menu"); + + // font menu + fFontsMenuField = new BMenuField("fonts", label, fFontsMenu); + fFontsMenuField->SetAlignment(B_ALIGN_RIGHT); + + // size menu + fSizesMenuField = new BMenuField("size", B_TRANSLATE("Size:"), fSizesMenu); + fSizesMenuField->SetAlignment(B_ALIGN_RIGHT); + + // preview + fPreviewText = new BStringView("preview text", + 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); + fPreviewText->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); + + // box around preview + fPreviewBox = new BBox("preview box", B_WILL_DRAW | B_FRAME_EVENTS); + fPreviewBox->AddChild(BGroupLayoutBuilder(B_HORIZONTAL) + .Add(fPreviewText) + .SetInsets(5, 5, 5, 5) + .TopView() + ); +} + + +FontSelectionView::~FontSelectionView() +{ +#ifndef INSTANT_UPDATE + _UpdateSystemFont(); +#endif +} + + +void +FontSelectionView::SetTarget(BHandler* messageTarget) +{ + fMessageTarget = messageTarget; + fSizesMenu->SetTargetForItems(fMessageTarget); +} + + +BView* +FontSelectionView::GetPreviewBox() +{ + return fPreviewBox; +} + + +void +FontSelectionView::MessageReceived(BMessage *msg) +{ + switch (msg->what) { + case kMsgSetSize: + { + int32 size; + if (msg->FindInt32("size", &size) != B_OK + || size == fCurrentFont.Size()) + break; + + fCurrentFont.SetSize(size); + _UpdateFontPreview(); + break; + } + + case kMsgSetFamily: + { + const char* family; + if (msg->FindString("family", &family) != B_OK) + break; + + font_style style; + fCurrentFont.GetFamilyAndStyle(NULL, &style); + + BMenuItem *familyItem = fFontsMenu->FindItem(family); + if (familyItem != NULL) { + _SelectCurrentFont(false); + + BMenuItem *item = familyItem->Submenu()->FindItem(style); + if (item == NULL) + item = familyItem->Submenu()->ItemAt(0); + + if (item != NULL) { + item->SetMarked(true); + fCurrentFont.SetFamilyAndStyle(family, item->Label()); + _UpdateFontPreview(); + } + } + break; + } + + case kMsgSetStyle: + { + const char* family; + const char* style; + if (msg->FindString("family", &family) != B_OK + || msg->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: + BView::MessageReceived(msg); + } +} + + +BLayoutItem* +FontSelectionView::CreateSizesLabelLayoutItem() +{ + return fSizesMenuField->CreateLabelLayoutItem(); +} + + +BLayoutItem* +FontSelectionView::CreateSizesMenuBarLayoutItem() +{ + return fSizesMenuField->CreateMenuBarLayoutItem(); +} + + +BLayoutItem* +FontSelectionView::CreateFontsLabelLayoutItem() +{ + return fFontsMenuField->CreateLabelLayoutItem(); +} + + +BLayoutItem* +FontSelectionView::CreateFontsMenuBarLayoutItem() +{ + return fFontsMenuField->CreateMenuBarLayoutItem(); +} + + +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); + } +} + + +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); + +#ifdef INSTANT_UPDATE + _UpdateSystemFont(); +#endif +} + + +void +FontSelectionView::_UpdateSystemFont() +{ + font_family family; + font_style style; + fCurrentFont.GetFamilyAndStyle(&family, &style); + + if (strcmp(Name(), "menu") == 0) { + // The menu font is not handled as a system font + menu_info info; + get_menu_info(&info); + + strlcpy(info.f_family, (const char*)family, B_FONT_FAMILY_LENGTH); + strlcpy(info.f_style, (const char*)style, B_FONT_STYLE_LENGTH); + info.font_size = fCurrentFont.Size(); + + set_menu_info(&info); + } else + _set_system_font_(Name(), family, style, fCurrentFont.Size()); +} + + +void +FontSelectionView::SetDefaults() +{ + font_family family; + font_style style; + float size; + const char* fontName; + + if (strcmp(Name(), "menu") == 0) + fontName = "plain"; + else + fontName = Name(); + + if (_get_system_default_font_(fontName, family, style, &size) != B_OK) { + Revert(); + return; + } + + BFont defaultFont; + defaultFont.SetFamilyAndStyle(family, style); + defaultFont.SetSize(size); + + 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() +{ + font_family defaultFamily; + font_style defaultStyle; + float defaultSize; + const char* fontName; + + if (strcmp(Name(), "menu") == 0) + fontName = "plain"; + else + fontName = Name(); + + if (_get_system_default_font_(fontName, defaultFamily, defaultStyle, + &defaultSize) != B_OK) { + return false; + } + + font_family currentFamily; + font_style currentStyle; + float currentSize; + + fCurrentFont.GetFamilyAndStyle(¤tFamily, ¤tStyle); + currentSize = fCurrentFont.Size(); + + return strcmp(currentFamily, defaultFamily) != 0 + || strcmp(currentStyle, defaultStyle) != 0 + || currentSize != defaultSize; +} + + +bool +FontSelectionView::IsRevertable() +{ + return fCurrentFont != fSavedFont; +} + + +void +FontSelectionView::UpdateFontsMenu() +{ + int32 numFamilies = count_font_families(); + + fFontsMenu->RemoveItems(0, fFontsMenu->CountItems(), true); + BFont font; + fFontsMenu->GetFont(&font); + + font_family currentFamily; + font_style currentStyle; + fCurrentFont.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; + + float width = font.StringWidth(family); + if (width > fMaxFontNameWidth) + fMaxFontNameWidth = width; + + BMenu* stylesMenu = new BMenu(family); + stylesMenu->SetRadioMode(true); + stylesMenu->SetFont(&font); + + BMessage* message = new BMessage(kMsgSetFamily); + message->AddString("family", family); + message->AddString("name", Name()); + + BMenuItem* familyItem = new BMenuItem(stylesMenu, message); + fFontsMenu->AddItem(familyItem); + + int32 numStyles = count_font_styles(family); + + for (int32 j = 0; j < numStyles; j++) { + font_style style; + if (get_font_style(family, j, &style, &flags) != B_OK) + continue; + + message = new BMessage(kMsgSetStyle); + message->AddString("family", (char*)family); + message->AddString("style", (char*)style); + message->AddString("name", Name()); + + BMenuItem *item = new BMenuItem(style, message); + + if (!strcmp(style, currentStyle) + && !strcmp(family, currentFamily)) { + item->SetMarked(true); + familyItem->SetMarked(true); + } + stylesMenu->AddItem(item); + } + + stylesMenu->SetTargetForItems(fMessageTarget); + } + + fFontsMenu->SetTargetForItems(fMessageTarget); +} diff --git a/src/preferences/appearance/FontSelectionView.h b/src/preferences/appearance/FontSelectionView.h new file mode 100644 index 0000000000..130a8fe19f --- /dev/null +++ b/src/preferences/appearance/FontSelectionView.h @@ -0,0 +1,77 @@ +/* + * Copyright 2001-2009, 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 + */ +#ifndef FONT_SELECTION_VIEW_H +#define FONT_SELECTION_VIEW_H + + +#include + +class BLayoutItem; +class BBox; +class BMenuField; +class BPopUpMenu; +class BStringView; + +static const int32 kMsgSetFamily = 'fmly'; +static const int32 kMsgSetStyle = 'styl'; +static const int32 kMsgSetSize = 'size'; + + +class FontSelectionView : public BView { +public: + FontSelectionView(const char* name, + const char* label, + const BFont* font = NULL); + virtual ~FontSelectionView(); + + virtual void MessageReceived(BMessage* message); + + void SetTarget(BHandler* messageTarget); + + void SetDefaults(); + void Revert(); + bool IsDefaultable(); + bool IsRevertable(); + + void UpdateFontsMenu(); + + BLayoutItem* CreateSizesLabelLayoutItem(); + BLayoutItem* CreateSizesMenuBarLayoutItem(); + + BLayoutItem* CreateFontsLabelLayoutItem(); + BLayoutItem* CreateFontsMenuBarLayoutItem(); + + BView* GetPreviewBox(); + +private: + void _SelectCurrentFont(bool select); + void _SelectCurrentSize(bool select); + void _UpdateFontPreview(); + void _UpdateSystemFont(); + void _BuildSizesMenu(); + +protected: + BHandler* fMessageTarget; + + BMenuField* fFontsMenuField; + BMenuField* fSizesMenuField; + BPopUpMenu* fFontsMenu; + BPopUpMenu* fSizesMenu; + + BBox* fPreviewBox; + BStringView* fPreviewText; + + BFont fSavedFont; + BFont fCurrentFont; + float fMaxFontNameWidth; +}; + +#endif // FONT_SELECTION_VIEW_H diff --git a/src/preferences/appearance/FontView.cpp b/src/preferences/appearance/FontView.cpp new file mode 100644 index 0000000000..0159dcf45e --- /dev/null +++ b/src/preferences/appearance/FontView.cpp @@ -0,0 +1,193 @@ +/* + * Copyright 2001-2012, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Mark Hogben + * DarkWyrm + * Axel Dörfler, axeld@pinc-software.de + * Philippe St-Pierre, stpere@gmail.com + * Stephan Aßmus + */ + + +#include "FontView.h" + +#include + +#include +#include +#include +#include +#include +#include + +#include "APRWindow.h" +#include "FontSelectionView.h" + + +#undef B_TRANSLATE_CONTEXT +#define B_TRANSLATE_CONTEXT "Font view" + + +static const uint32 kMsgCheckFonts = 'chkf'; + + +static void +add_font_selection_view(BGridLayout* layout, FontSelectionView* view, + int32& row, bool withExtraSpace) +{ + layout->AddItem(view->CreateFontsLabelLayoutItem(), 0, row); + layout->AddItem(view->CreateFontsMenuBarLayoutItem(), 1, row); + + layout->AddItem(BSpaceLayoutItem::CreateGlue(), 2, row); + + layout->AddItem(view->CreateSizesLabelLayoutItem(), 3, row); + layout->AddItem(view->CreateSizesMenuBarLayoutItem(), 4, row); + + row++; + + layout->AddItem(BSpaceLayoutItem::CreateGlue(), 0, row); + layout->AddView(view->GetPreviewBox(), 1, row, 4); + + row++; + + if (withExtraSpace) { + layout->AddItem(BSpaceLayoutItem::CreateVerticalStrut(5), 0, row, 5); + row++; + } +} + + +FontView::FontView(const char* name) + : + BView(name, B_WILL_DRAW ) +{ + SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); + + fPlainView = new FontSelectionView("plain", B_TRANSLATE("Plain font:")); + fBoldView = new FontSelectionView("bold", B_TRANSLATE("Bold font:")); + fFixedView = new FontSelectionView("fixed", B_TRANSLATE("Fixed font:")); + fMenuView = new FontSelectionView("menu", B_TRANSLATE("Menu font:")); + + BGridLayout* layout = new BGridLayout(5, 5); + layout->SetInsets(10, 10, 10, 10); + SetLayout(layout); + + int32 row = 0; + add_font_selection_view(layout, fPlainView, row, true); + add_font_selection_view(layout, fBoldView, row, true); + add_font_selection_view(layout, fFixedView, row, true); + add_font_selection_view(layout, fMenuView, row, false); +} + + +void +FontView::AttachedToWindow() +{ + fPlainView->SetTarget(this); + fBoldView->SetTarget(this); + fFixedView->SetTarget(this); + fMenuView->SetTarget(this); + + UpdateFonts(); + fRunner = new BMessageRunner(this, new BMessage(kMsgCheckFonts), 3000000); + // every 3 seconds +} + + +void +FontView::DetachedFromWindow() +{ + delete fRunner; + fRunner = NULL; +} + + +void +FontView::SetDefaults() +{ + fPlainView->SetDefaults(); + fBoldView->SetDefaults(); + fFixedView->SetDefaults(); + fMenuView->SetDefaults(); +} + + +void +FontView::MessageReceived(BMessage* message) +{ + switch (message->what) { + case kMsgSetSize: + case kMsgSetFamily: + case kMsgSetStyle: + { + const char* name; + if (message->FindString("name", &name) != B_OK) + break; + + if (!strcmp(name, "plain")) + fPlainView->MessageReceived(message); + else if (!strcmp(name, "bold")) + fBoldView->MessageReceived(message); + else if (!strcmp(name, "fixed")) + fFixedView->MessageReceived(message); + else if (!strcmp(name, "menu")) + fMenuView->MessageReceived(message); + else + break; + + Window()->PostMessage(kMsgUpdate); + break; + } + + case kMsgCheckFonts: + if (update_font_families(true)) + UpdateFonts(); + break; + + default: + BView::MessageReceived(message); + } +} + + +void +FontView::Revert() +{ + fPlainView->Revert(); + fBoldView->Revert(); + fFixedView->Revert(); + fMenuView->Revert(); +} + + +void +FontView::UpdateFonts() +{ + fPlainView->UpdateFontsMenu(); + fBoldView->UpdateFontsMenu(); + fFixedView->UpdateFontsMenu(); + fMenuView->UpdateFontsMenu(); +} + + +bool +FontView::IsDefaultable() +{ + return fPlainView->IsDefaultable() + || fBoldView->IsDefaultable() + || fFixedView->IsDefaultable() + || fMenuView->IsDefaultable(); +} + + +bool +FontView::IsRevertable() +{ + return fPlainView->IsRevertable() + || fBoldView->IsRevertable() + || fFixedView->IsRevertable() + || fMenuView->IsRevertable(); +} + diff --git a/src/preferences/appearance/FontView.h b/src/preferences/appearance/FontView.h new file mode 100644 index 0000000000..0b13afd41f --- /dev/null +++ b/src/preferences/appearance/FontView.h @@ -0,0 +1,51 @@ +/* + * Copyright 2001-2012, 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_VIEW_H +#define FONT_VIEW_H + + +#include + + +class BMessageRunner; +class FontSelectionView; + + +class FontView : public BView { +public: + FontView(const char* name); + + virtual void AttachedToWindow(); + virtual void DetachedFromWindow(); + + virtual void MessageReceived(BMessage* message); + + void SetDefaults(); + void Revert(); + void UpdateFonts(); + + bool IsDefaultable(); + bool IsRevertable(); + + + +private: + FontSelectionView* fPlainView; + FontSelectionView* fBoldView; + FontSelectionView* fFixedView; + FontSelectionView* fMenuView; + + BMessageRunner* fRunner; +}; + + +#endif /* FONT_VIEW_H */ diff --git a/src/preferences/appearance/Jamfile b/src/preferences/appearance/Jamfile index a52f9e6937..498bfdd0a5 100644 --- a/src/preferences/appearance/Jamfile +++ b/src/preferences/appearance/Jamfile @@ -7,6 +7,8 @@ Preference Appearance : APRMain.cpp AntialiasingSettingsView.cpp DecorSettingsView.cpp + FontSelectionView.cpp + FontView.cpp APRView.cpp APRWindow.cpp ColorSet.cpp @@ -30,11 +32,13 @@ DoCatalogs Appearance : x-vnd.Haiku-Appearance : AntialiasingSettingsView.cpp - DecorSettingsView.cpp APRView.cpp APRWindow.cpp ColorSet.cpp ColorWell.cpp ColorWhichItem.cpp + DecorSettingsView.cpp + FontView.cpp + FontSelectionView.cpp ; diff --git a/src/preferences/appearance/defs.h b/src/preferences/appearance/defs.h index 5755621963..d14a0f19fe 100644 --- a/src/preferences/appearance/defs.h +++ b/src/preferences/appearance/defs.h @@ -20,8 +20,6 @@ #define APPEARANCE_APP_SIGNATURE "application/x-vnd.Haiku-Appearance" #define APPLY_SETTINGS 'aply' -#define REVERT_SETTINGS 'rvrt' -#define DEFAULT_SETTINGS 'dflt' #define TRY_SETTINGS 'trys' #define ATTRIBUTE_CHOSEN 'atch'