From 4b31e304e7248148abc0f8812839d7a59d8ebbd8 Mon Sep 17 00:00:00 2001 From: DarkWyrm Date: Thu, 9 Jun 2005 16:08:39 +0000 Subject: [PATCH] More fixes to the prefs panel. In fact, for all practical purposes, it should be done. Added private function _set_system_font_() to not use R5's hack git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13031 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/interface/Font.cpp | 32 ++ src/prefs/fonts/ButtonView.cpp | 21 +- src/prefs/fonts/ButtonView.h | 12 +- src/prefs/fonts/CacheView.cpp | 107 +++-- src/prefs/fonts/CacheView.h | 9 +- src/prefs/fonts/FontSelectionView.cpp | 607 +++++++++++++------------- src/prefs/fonts/FontSelectionView.h | 44 +- src/prefs/fonts/FontView.cpp | 41 +- src/prefs/fonts/FontView.h | 17 +- src/prefs/fonts/FontsSettings.h | 2 +- src/prefs/fonts/MainWindow.cpp | 134 +----- src/prefs/fonts/MainWindow.h | 22 +- src/prefs/fonts/Pref_Utils.h | 4 +- 13 files changed, 480 insertions(+), 572 deletions(-) diff --git a/src/kits/interface/Font.cpp b/src/kits/interface/Font.cpp index d374fe7e9d..6ac88511fb 100644 --- a/src/kits/interface/Font.cpp +++ b/src/kits/interface/Font.cpp @@ -107,6 +107,38 @@ _font_control_(BFont *font, int32 cmd, void *data) link.Read(&font->fFlags); } +/*! + \brief Private function used to replace the R5 hack which sets a system font + \param which string denoting which font to set + \param family the new family for the system font + \param style the new style for the system font + \param size the size for the system font to have + + R5 used a global area offset table to set the system fonts in the Font + preferences panel. Bleah. +*/ +void +_set_system_font_(const char *which, font_family family, font_style style, + float size) +{ + if(!which) + return; + + if( (strcmp(which,"plain")==0) || + (strcmp(which,"bold")==0) || + (strcmp(which,"fixed")==0) ) + { + BPrivate::BAppServerLink link; + + link.StartMessage(AS_SET_SYSTEM_FONT); + link.AttachString(which); + link.AttachString(family); + link.AttachString(style); + link.Attach(size); + link.Flush(); + } +} + /*! \brief Returns the number of installed font families diff --git a/src/prefs/fonts/ButtonView.cpp b/src/prefs/fonts/ButtonView.cpp index 117b746f1e..71a21d63b0 100644 --- a/src/prefs/fonts/ButtonView.cpp +++ b/src/prefs/fonts/ButtonView.cpp @@ -4,6 +4,7 @@ * */ #include "ButtonView.h" +#include "MainWindow.h" ButtonView::ButtonView(BRect rect) : BView(rect, "ButtonView", B_FOLLOW_ALL, B_WILL_DRAW) @@ -13,24 +14,24 @@ ButtonView::ButtonView(BRect rect) BRect btnRect(0, 0, 75, 25); btnRect.OffsetBy(10, 8); BButton *rescanButton = new BButton(btnRect, "rescanButton", "Rescan", - new BMessage(RESCAN_FONTS_MSG), + new BMessage(M_RESCAN_FONTS), B_FOLLOW_LEFT, B_WILL_DRAW); AddChild(rescanButton); btnRect.OffsetBy(96, 0); BButton *defaultsButton = new BButton(btnRect, "defaultsButton", "Defaults", - new BMessage(RESET_FONTS_MSG), + new BMessage(M_SET_DEFAULTS), B_FOLLOW_LEFT, B_WILL_DRAW); AddChild(defaultsButton); btnRect.OffsetBy(85, 0); - revertButton = new BButton(btnRect, "revertButton", "Revert", - new BMessage(REVERT_MSG), + fRevertButton = new BButton(btnRect, "fRevertButton", "Revert", + new BMessage(M_REVERT), B_FOLLOW_LEFT, B_WILL_DRAW); - AddChild(revertButton); + AddChild(fRevertButton); - revertButton->SetEnabled(false); + fRevertButton->SetEnabled(false); } @@ -55,12 +56,12 @@ ButtonView::Draw(BRect update) bool -ButtonView::RevertState() +ButtonView::RevertState(void) const { - return revertButton->IsEnabled(); + return fRevertButton->IsEnabled(); } -void ButtonView::SetRevertState(bool b) +void ButtonView::SetRevertState(bool value) { - revertButton->SetEnabled(b); + fRevertButton->SetEnabled(value); } diff --git a/src/prefs/fonts/ButtonView.h b/src/prefs/fonts/ButtonView.h index 9a8823dc02..03e0ee15d4 100644 --- a/src/prefs/fonts/ButtonView.h +++ b/src/prefs/fonts/ButtonView.h @@ -10,25 +10,19 @@ #include // Message sent when the rescan button is sent. -#define RESCAN_FONTS_MSG 'rscn' - -// Message sent when the reset button is sent. -#define RESET_FONTS_MSG 'rset' - -// Message sent when the revert button is sent. -#define REVERT_MSG 'rvrt' +#define M_RESCAN_FONTS 'rscn' class ButtonView : public BView { public: ButtonView(BRect frame); - bool RevertState(); + bool RevertState() const; void SetRevertState(bool b); void Draw(BRect); private: - BButton *revertButton; + BButton *fRevertButton; }; #endif diff --git a/src/prefs/fonts/CacheView.cpp b/src/prefs/fonts/CacheView.cpp index febb6aa830..a9b8f91950 100644 --- a/src/prefs/fonts/CacheView.cpp +++ b/src/prefs/fonts/CacheView.cpp @@ -7,6 +7,7 @@ #include #include +#include "MainWindow.h" #include "Pref_Utils.h" #define PRINT_FCS_UPDATE_MSG 'pfum' @@ -73,10 +74,13 @@ CacheView::CacheView(const BRect &frame, const int32 &sliderMin, rect.right = rect.left +86.0; rect.bottom = rect.top +24.0; - // TODO: find out what 'Save Cache' does and implement + // TODO: figure out what to do with 'Save Cache' on R5. According to the + // BeOS Bible, it allocates a block of memory to disk, which is loaded on + // next boot. FreeType does better than this. fSaveCache = new BButton(rect, "saveCache", "Save Cache", NULL, B_FOLLOW_LEFT, B_WILL_DRAW); - AddChild(fSaveCache); + AddChild(fSaveCache); + fSaveCache->SetEnabled(false); } void @@ -94,38 +98,15 @@ CacheView::MessageReceived(BMessage *msg) { case PRINT_FCS_MODIFICATION_MSG: { - struct font_cache_info fontCacheInfo; + UpdatePrintSettings(fPrintSlider->Value()); - get_font_cache_info(B_PRINTING_FONT_CACHE|B_DEFAULT_CACHE_SETTING, - &fontCacheInfo); - fontCacheInfo.cache_size = fPrintSlider->Value() << 10; - set_font_cache_info(B_PRINTING_FONT_CACHE|B_DEFAULT_CACHE_SETTING, - &fontCacheInfo); - - BString str("Printing font cache size : "); - str << fPrintSlider->Value() << " kB"; - fPrintSlider->SetLabel(str.String()); - - // TODO: set revert state -// buttonView->SetRevertState(true); + Window()->PostMessage(M_ENABLE_REVERT); break; } case SCREEN_FCS_MODIFICATION_MSG: { - struct font_cache_info fontCacheInfo; - - get_font_cache_info(B_SCREEN_FONT_CACHE|B_DEFAULT_CACHE_SETTING, - &fontCacheInfo); - fontCacheInfo.cache_size = fScreenSlider->Value() << 10; - set_font_cache_info(B_SCREEN_FONT_CACHE|B_DEFAULT_CACHE_SETTING, - &fontCacheInfo); - - BString str("Screen font cache size : "); - str << fScreenSlider->Value() << " kB"; - fScreenSlider->SetLabel(str.String()); - - // TODO: set revert state -// buttonView->SetRevertState(true); + UpdateScreenSettings(fScreenSlider->Value()); + Window()->PostMessage(M_ENABLE_REVERT); break; } default: @@ -136,34 +117,50 @@ CacheView::MessageReceived(BMessage *msg) } } -/** - * Sets the sliders to their original values. - */ -void CacheView::revertToOriginal(){ - // TODO: fix -/* - BString label; - label << "Screen " << kLabel << getScreenFCSValue(); - updateScreenFCS(label.String()); - - label = "Printing "; - label << kLabel << getPrintFCSValue(); - updatePrintFCS(label.String()); -*/ +void CacheView::Revert(void) +{ + fPrintSlider->SetValue(fSavedPrintValue); + fScreenSlider->SetValue(fSavedScreenValue); + UpdatePrintSettings(fSavedPrintValue); + UpdateScreenSettings(fSavedScreenValue); } -/** - * Sets the sliders to their default values. - */ -void CacheView::resetToDefaults(){ - // TODO: fix -/* BString label; - label << "Screen " << kLabel << getScreenFCSValue() << " kB"; - updateScreenFCS(label.String()); +void CacheView::SetDefaults(void) +{ + fPrintSlider->SetValue(256); + fScreenSlider->SetValue(256); + UpdatePrintSettings(256); + UpdateScreenSettings(256); +} + +void +CacheView::UpdatePrintSettings(int32 value) +{ + struct font_cache_info fontCacheInfo; - label = "Printing "; - label << kLabel << getPrintFCSValue() << " kB"; - updatePrintFCS(label.String()); -*/ + get_font_cache_info(B_PRINTING_FONT_CACHE|B_DEFAULT_CACHE_SETTING, + &fontCacheInfo); + fontCacheInfo.cache_size = value << 10; + set_font_cache_info(B_PRINTING_FONT_CACHE|B_DEFAULT_CACHE_SETTING, + &fontCacheInfo); + + BString str("Printing font cache size : "); + str << value << " kB"; + fPrintSlider->SetLabel(str.String()); } +void +CacheView::UpdateScreenSettings(int32 value) +{ + struct font_cache_info fontCacheInfo; + + get_font_cache_info(B_SCREEN_FONT_CACHE|B_DEFAULT_CACHE_SETTING, + &fontCacheInfo); + fontCacheInfo.cache_size = value << 10; + set_font_cache_info(B_SCREEN_FONT_CACHE|B_DEFAULT_CACHE_SETTING, + &fontCacheInfo); + + BString str("Screen font cache size : "); + str << value << " kB"; + fScreenSlider->SetLabel(str.String()); +} diff --git a/src/prefs/fonts/CacheView.h b/src/prefs/fonts/CacheView.h index 021c40882e..f1eaba6129 100644 --- a/src/prefs/fonts/CacheView.h +++ b/src/prefs/fonts/CacheView.h @@ -20,16 +20,17 @@ public: void AttachedToWindow(void); void MessageReceived(BMessage *msg); - void revertToOriginal(); - void resetToDefaults(); + void Revert(void); + void SetDefaults(void); private: - + void UpdatePrintSettings(int32 value); + void UpdateScreenSettings(int32 value); + BSlider *fScreenSlider; BSlider *fPrintSlider; BButton *fSaveCache; - // The original slider values int32 fSavedPrintValue; int32 fSavedScreenValue; }; diff --git a/src/prefs/fonts/FontSelectionView.cpp b/src/prefs/fonts/FontSelectionView.cpp index 260948d45f..d5b3693371 100644 --- a/src/prefs/fonts/FontSelectionView.cpp +++ b/src/prefs/fonts/FontSelectionView.cpp @@ -4,125 +4,145 @@ * */ #include "FontSelectionView.h" +#include "MainWindow.h" #include "Pref_Utils.h" +#include #define SIZE_CHANGED_MSG 'plsz' #define FONT_CHANGED_MSG 'plfn' #define STYLE_CHANGED_MSG 'plst' // should be changed to allow larger and smaller -#define minSizeIndex 9 -#define maxSizeIndex 14 +#define MIN_SIZE_INDEX 9 +#define MAX_SIZE_INDEX 14 -// constants for labels -const char *kPlainFont = "Plain font:"; -const char *kBoldFont = "Bold font:"; -const char *kFixedFont = "Fixed font:"; -const char *kSize = "Size: "; +extern void _set_system_font_(const char *which, font_family family, font_style style, float size); FontSelectionView::FontSelectionView(BRect rect, const char *name, int type) - : BView(rect, name, B_FOLLOW_ALL, B_WILL_DRAW) + : BView(rect, name, B_FOLLOW_ALL, B_WILL_DRAW), + fType(type) { - switch(type) + SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); + + BString typelabel; + + switch(fType) { case BOLD_FONT_SELECTION_VIEW: { - sprintf(typeLabel, kBoldFont); - origFont = be_bold_font; - workingFont = be_bold_font; + typelabel="Bold font:"; + fSavedFont = be_bold_font; + fCurrentFont = be_bold_font; - defaultFont = new BFont(); - defaultFont->SetFamilyAndStyle("Swis721 BT", "Bold"); - defaultFont->SetSize(12.0); + fDefaultFont.SetFamilyAndStyle("Swis721 BT", "Bold"); + fDefaultFont.SetSize(12.0); break; } case FIXED_FONT_SELECTION_VIEW: { - sprintf(typeLabel, kFixedFont); - origFont = be_fixed_font; - workingFont = be_fixed_font; + typelabel="Fixed font:"; + fSavedFont = be_fixed_font; + fCurrentFont = be_fixed_font; - defaultFont = new BFont(); - defaultFont->SetFamilyAndStyle("Courier10 BT", "Roman"); - defaultFont->SetSize(12.0); + fDefaultFont = new BFont(); + fDefaultFont.SetFamilyAndStyle("Courier10 BT", "Roman"); + fDefaultFont.SetSize(12.0); break; } default: { - sprintf(typeLabel, kPlainFont); - origFont = be_plain_font; - workingFont = be_plain_font; + typelabel="Plain font:"; + fSavedFont = be_plain_font; + fCurrentFont = be_plain_font; - defaultFont = new BFont(); - defaultFont->SetFamilyAndStyle("Swis721 BT", "Roman"); - defaultFont->SetSize(10.0); + fDefaultFont = new BFont(); + fDefaultFont.SetFamilyAndStyle("Swis721 BT", "Roman"); + fDefaultFont.SetSize(10.0); break; } } float fontheight = FontHeight(false); - float divider = StringWidth(kFixedFont); + float divider = StringWidth("Fixed font:"); - sizeList = new BPopUpMenu("sizeList", true, true, B_ITEMS_IN_COLUMN); - fontList = new BPopUpMenu("fontList", true, true, B_ITEMS_IN_COLUMN); - - // create menus + fSizeMenu = new BPopUpMenu("fSizeMenu", true, true, B_ITEMS_IN_COLUMN); + fFontMenu = new BPopUpMenu("fFontMenu", true, true, B_ITEMS_IN_COLUMN); // size box - rect = Bounds(); - float x = StringWidth("999") +16; - rect.left = rect.right -(x +StringWidth(kSize)+8.0); - rect.bottom = fontheight +5; - BMenuField *sizeListField = new BMenuField(rect, "fontField", kSize, sizeList, true); - sizeListField->SetDivider(StringWidth(kSize)+5.0); - sizeListField->SetAlignment(B_ALIGN_RIGHT); + BRect r( Bounds() ); + + float x = StringWidth("999") + 16; + r.left = r.right -( x + StringWidth("Size: ") + 8.0 ); + r.bottom = fontheight +5; + + BMenuField *fSizeMenuField = new BMenuField(r, "fontField", "Size: ", fSizeMenu, true); + fSizeMenuField->SetDivider( StringWidth("Size: ") + 5.0 ); + fSizeMenuField->SetAlignment(B_ALIGN_RIGHT); + AddChild(fSizeMenuField); // font menu - rect.right = rect.left; - rect.left = 1; - rect.bottom = fontheight *1.5; - BMenuField *fontListField = new BMenuField(rect, "fontField", typeLabel, fontList, false); - fontListField->SetDivider(divider +6.0); - fontListField->SetAlignment(B_ALIGN_RIGHT); + r.right = r.left; + r.left = 1; + r.bottom = fontheight *1.5; + BMenuField *fFontMenuField = new BMenuField(r, "fontField", typelabel.String(), fFontMenu, false); + fFontMenuField->SetDivider(divider + 6.0); + fFontMenuField->SetAlignment(B_ALIGN_RIGHT); + AddChild(fFontMenuField); - rect = Bounds(); - rect.left = divider +8.0; - rect.top = fontheight *1.5 +4; - rect.InsetBy(1, 1); - BBox *testTextBox = new BBox(rect, "TestTextBox", B_FOLLOW_ALL, B_WILL_DRAW, B_FANCY_BORDER); + r = Bounds(); + r.left = divider +8.0; + r.top = fontheight *1.5 +4; + r.InsetBy(1, 1); + BBox *fPreviewTextBox = new BBox(r, "TestTextBox", B_FOLLOW_ALL, B_WILL_DRAW, B_FANCY_BORDER); + AddChild(fPreviewTextBox); // Place the text slightly inside the entire box area, so it doesn't overlap the box outline. - rect = testTextBox->Bounds().InsetByCopy(2, 2); - rect.right -= 2; - BRect testTextRect(rect); - testText = new BStringView(testTextRect, "testText", "The quick brown fox jumps over the lazy dog.", + r = fPreviewTextBox->Bounds().InsetByCopy(2, 2); + r.right -= 2; + BRect fPreviewTextRect(r); + fPreviewText = new BStringView(fPreviewTextRect, "fPreviewText", "The quick brown fox jumps over the lazy dog.", B_FOLLOW_ALL, B_WILL_DRAW); - testText->SetFont(&workingFont); - - fontList->SetLabelFromMarked(true); - - buildMenus(); - - SetViewColor(216, 216, 216, 0); - - AddChild(testTextBox); - testTextBox->AddChild(testText); - AddChild(sizeListField); - AddChild(fontListField); - + fPreviewText->SetFont(&fCurrentFont); + fPreviewTextBox->AddChild(fPreviewText); } FontSelectionView::~FontSelectionView(void) { - delete defaultFont; + font_family family; + font_style style; + fCurrentFont.GetFamilyAndStyle(&family,&style); + + switch(fType) + { + case PLAIN_FONT_SELECTION_VIEW: + { + _set_system_font_("plain", family, style, fCurrentFont.Size()); + break; + } + case BOLD_FONT_SELECTION_VIEW: + { + _set_system_font_("bold", family, style, fCurrentFont.Size()); + break; + } + case FIXED_FONT_SELECTION_VIEW: + { + _set_system_font_("fixed", family, style, fCurrentFont.Size()); + break; + } + default: + { + break; + } + } } void FontSelectionView::AttachedToWindow(void) { + BuildMenus(); } void @@ -130,52 +150,109 @@ FontSelectionView::MessageReceived(BMessage *msg) { switch(msg->what) { -/* case SIZE_CHANGED_MSG: { - - updateSize(fSelectorView->plainSelectionView); - fButtonView->SetRevertState(true); + case SIZE_CHANGED_MSG: + { + int32 size; + if(msg->FindInt32("size",&size)!=B_OK) + break; + + BString str(B_EMPTY_STRING); + str << size; + + BMenuItem *item=fSizeMenu->FindItem(str.String()); + if(item) + { + item->SetMarked(true); + + fCurrentFont.SetSize(size); + fPreviewText->SetFont(&fCurrentFont, B_FONT_ALL); + fPreviewText->Invalidate(); + } + + NotifyFontChange(); + Window()->PostMessage(M_ENABLE_REVERT); break; } - case FONT_CHANGED_MSG: { - - updateFont(fSelectorView->plainSelectionView); - fButtonView->SetRevertState(true); + case FONT_CHANGED_MSG: + { + BString str; + if(msg->FindString("family",&str)!=B_OK) + break; + + font_family family; + sprintf(family,"%s",str.String()); + BMenuItem *menu=fFontMenu->FindItem(family); + if(menu) + { + menu->SetMarked(true); + fCurrentFont.SetFamilyAndFace(family,B_REGULAR_FACE); + + font_style style; + fCurrentFont.GetFamilyAndStyle(&family,&style); + + BMenuItem *item=menu->Submenu()->FindItem(style); + if(item) + { + fCurrentStyle->SetMarked(false); + item->SetMarked(true); + fCurrentStyle=item; + + fPreviewText->SetFont(&fCurrentFont, B_FONT_ALL); + fPreviewText->Invalidate(); + } + } + + NotifyFontChange(); + Window()->PostMessage(M_ENABLE_REVERT); break; } - case STYLE_CHANGED_MSG: { - - updateStyle(fSelectorView->plainSelectionView); - fButtonView->SetRevertState(true); + case STYLE_CHANGED_MSG: + { + BString str; + if(msg->FindString("family",&str)!=B_OK) + break; + + font_family family; + font_style style; + sprintf(family,"%s",str.String()); + + if(msg->FindString("style",&str)!=B_OK) + break; + sprintf(style,"%s",str.String()); + + BMenuItem *menu=fFontMenu->FindItem(family); + if(!menu) + break; + + BMenuItem *item=menu->Submenu()->FindItem(style); + if(item) + { + fCurrentStyle->SetMarked(false); + menu->SetMarked(true); + item->SetMarked(true); + fCurrentStyle=item; + + fCurrentFont.SetFamilyAndStyle(family,style); + fPreviewText->SetFont(&fCurrentFont, B_FONT_ALL); + fPreviewText->Invalidate(); + } + + NotifyFontChange(); + Window()->PostMessage(M_ENABLE_REVERT); break; } - case RESCAN_FONTS_MSG: { - - update_font_families(false); - fSelectorView->emptyMenus(); - fSelectorView->buildMenus(); - updateFont(fSelectorView->plainSelectionView); - updateFont(fSelectorView->boldSelectionView); - updateFont(fSelectorView->fixedSelectionView); - break; - } - case RESET_FONTS_MSG: { - - fSelectorView->resetToDefaults(); - fCacheView->resetToDefaults(); - fButtonView->SetRevertState(true); - break; - } -*/ default: + default: BView::MessageReceived(msg); } } -void FontSelectionView::emptyMenus(void) +void +FontSelectionView::EmptyMenus(void) { // Empty the font list - for(int32 i = 0; i < fontList->CountItems(); i++) + for(int32 i = 0; i < fFontMenu->CountItems(); i++) { - BMenu *menu = fontList->SubmenuAt(0L); + BMenu *menu = fFontMenu->SubmenuAt(0L); // We should never have a regular menu item in the font list if(!menu) @@ -187,24 +264,27 @@ void FontSelectionView::emptyMenus(void) delete item; } - fontList->RemoveItem(menu); + fFontMenu->RemoveItem(menu); delete menu; } // empty the size list - for(int32 i = 0; i < sizeList->CountItems(); i++) + for(int32 i = 0; i < fSizeMenu->CountItems(); i++) { - BMenuItem *item = sizeList->RemoveItem(0L); + BMenuItem *item = fSizeMenu->RemoveItem(0L); delete item; } } -void FontSelectionView::buildMenus(void) +void +FontSelectionView::BuildMenus(void) { int32 numFamilies; int counter; + BMessage *msg; numFamilies = count_font_families(); + for ( int32 i = 0; i < numFamilies; i++ ) { font_family family; @@ -218,7 +298,6 @@ void FontSelectionView::buildMenus(void) if ( get_font_family(i, &family, &flags) != B_OK ) continue; - markFamily = false; tmpStyleMenu = new BMenu(family); @@ -236,246 +315,154 @@ void FontSelectionView::buildMenus(void) if (get_font_style(family, j, &style, &flags) != B_OK) continue; + fCurrentFont.GetFamilyAndStyle(&workingFamily, &workingStyle); - workingFont.GetFamilyAndStyle(&workingFamily, &workingStyle); - tmpItem = new BMenuItem(style, new BMessage(STYLE_CHANGED_MSG)); + msg = new BMessage(STYLE_CHANGED_MSG); + msg->AddString("family",(char*)family); + msg->AddString("style",(char*)style); + tmpItem = new BMenuItem(style, msg); if((strcmp(style, workingStyle) == 0) && (strcmp(family, workingFamily) == 0)) { markFamily = true; tmpItem->SetMarked(true); + fCurrentStyle=tmpItem; } tmpStyleMenu->AddItem(tmpItem); } - fontList->AddItem(new BMenuItem((tmpStyleMenu), new BMessage(FONT_CHANGED_MSG))); + msg = new BMessage(FONT_CHANGED_MSG); + msg->AddString("family",family); + fFontMenu->AddItem(new BMenuItem((tmpStyleMenu), msg)); + tmpStyleMenu->SetTargetForItems(this); if(markFamily) tmpStyleMenu->Superitem()->SetMarked(true); } + fFontMenu->SetTargetForItems(this); // build size menu - for(counter = minSizeIndex; counter < (maxSizeIndex + 1); counter++) + for(counter = MIN_SIZE_INDEX; counter < (MAX_SIZE_INDEX + 1); counter++) { char buf[1]; BMenuItem *tmp; sprintf(buf, "%d", counter); - sizeList->AddItem(tmp = new BMenuItem(buf, new BMessage(SIZE_CHANGED_MSG))); - if(counter == (int) workingFont.Size()) + msg = new BMessage(SIZE_CHANGED_MSG); + msg->AddInt32("size",counter); + fSizeMenu->AddItem(tmp = new BMenuItem(buf, msg)); + if(counter == (int) fCurrentFont.Size()) tmp->SetMarked(true); } + fSizeMenu->SetTargetForItems(this); } -/** - * Writes the test text in the given font. - * @param fnt The font to write the test text in. - */ -void FontSelectionView::SetTestTextFont(BFont *fnt) +// This method is called by outsiders only. As a result, +// the owning window is NOT notified +void +FontSelectionView::SetDefaults(void) { - testText->SetFont(fnt, B_FONT_ALL); - testText->Invalidate(); -} - -BFont FontSelectionView::GetTestTextFont() -{ - - BFont rtrnFont; + font_family family; + font_style style; - testText->GetFont(&rtrnFont); + fDefaultFont.GetFamilyAndStyle(&family, &style); - return rtrnFont; - -} - -float -FontSelectionView::GetSelectedSize() -{ - return minSizeIndex + sizeList->IndexOf(sizeList->FindMarked()); - -} - -void FontSelectionView::GetSelectedFont(font_family *family) -{ - int numFamilies = count_font_families(); - for ( int32 i = 0; i < numFamilies; i++ ) - { - font_family fam; - uint32 flags; - - if ( get_font_family(i, &fam, &flags) == B_OK ) - { - if(strcmp(fam, fontList->FindMarked()->Label()) == 0) - get_font_family(i, family, &flags); - } - } -} - -void FontSelectionView::GetSelectedStyle(font_style *style) -{ - int numFamilies = count_font_families(); - font_family curr; - - GetSelectedFont(&curr); - - for ( int32 i = 0; i < numFamilies; i++ ) - { - font_family fam; - uint32 flags; - - if (get_font_family(i, &fam, &flags) == B_OK && strcmp(fam, curr) == 0 ) - { - int32 numStyles = count_font_styles(fam); - for ( int32 j = 0; j < numStyles; j++ ) - { - font_style sty; - if ( get_font_style(fam, j, &sty, &flags) == B_OK ) - { - if(strcmp(sty, fontList->FindMarked()->Submenu()->FindMarked()->Label()) == 0) - get_font_style(fam, j, style, &flags); - } - } - } - } -} - -/** - * If a style is selected, this function is called to update the font menu, - * in case the selected style is from a non selected font. It also marks the - * selected style, and unmarks all other styles. - */ -void FontSelectionView::UpdateFontSelectionFromStyle() -{ - int i = 0; - - for(i = 0;i < fontList->CountItems();i++){ - - int j = 0; - - for(j = 0;j < fontList->ItemAt(i)->Submenu()->CountItems();j++){ - - if(fontList->ItemAt(i)->Submenu()->ItemAt(j)->IsMarked()){ + BMenuItem *menu=fFontMenu->FindItem(family); + if(!menu) + return; - if(!strcmp(fontList->ItemAt(i)->Label(), fontList->FindMarked()->Label()) == 0){ - - fontList->FindMarked()->Submenu()->FindMarked()->SetMarked(false); - fontList->ItemAt(i)->SetMarked(true); - - }//if - - }//if - - }//for - - }//for + BMenuItem *item=menu->Submenu()->FindItem(style); + if(!item) + return; + + fCurrentStyle->SetMarked(false); + menu->SetMarked(true); + item->SetMarked(true); + fCurrentStyle=item; + + char string[5]; + sprintf(string,"%d",(int)fDefaultFont.Size()); + item = fSizeMenu->FindItem(string); + if(item) + item->SetMarked(true); + + fCurrentFont=fDefaultFont; + fPreviewText->SetFont(&fCurrentFont, B_FONT_ALL); + fPreviewText->Invalidate(); } -/** - * Updates the font menu based on the user selection. - */ -void FontSelectionView::UpdateFontSelection() +// This method is called by outsiders only. As a result, +// the owning window is NOT notified +void +FontSelectionView::Revert(void) { - for(int32 i = 0; i < fontList->CountItems();i++) + font_family family; + font_style style; + + fSavedFont.GetFamilyAndStyle(&family, &style); + + BMenuItem *menu=fFontMenu->FindItem(family); + if(!menu) + return; + + BMenuItem *item=menu->Submenu()->FindItem(style); + if(!item) + return; + + fCurrentStyle->SetMarked(false); + menu->SetMarked(true); + item->SetMarked(true); + fCurrentStyle=item; + + char string[5]; + sprintf(string,"%d",(int)fSavedFont.Size()); + item = fSizeMenu->FindItem(string); + if(item) + item->SetMarked(true); + + fCurrentFont=fSavedFont; + fPreviewText->SetFont(&fCurrentFont, B_FONT_ALL); + fPreviewText->Invalidate(); +} + +void +FontSelectionView::NotifyFontChange(void) +{ + BMessage msg; + + switch(fType) { - for(int32 j = 0;j < fontList->ItemAt(i)->Submenu()->CountItems();j++) + case BOLD_FONT_SELECTION_VIEW: { - if(fontList->ItemAt(i)->Submenu()->ItemAt(j)->IsMarked()) - { - if(strcmp(fontList->ItemAt(i)->Label(), fontList->FindMarked()->Label()) > 0) - { - fontList->ItemAt(i)->Submenu()->FindMarked()->SetMarked(false); - fontList->FindMarked()->Submenu()->ItemAt(0)->SetMarked(true); - } - } + msg.what=M_SET_BOLD; + break; + } + case FIXED_FONT_SELECTION_VIEW: + { + msg.what=M_SET_FIXED; + break; + } + default: + { + msg.what=M_SET_PLAIN; + break; } } + + font_family family; + font_style style; + fCurrentFont.GetFamilyAndStyle(&family,&style); + + msg.AddInt32("size",fCurrentFont.Size()); + msg.AddString("family",family); + msg.AddString("style",style); + Window()->PostMessage(&msg); } -/** - * Updates the font and size menus based on the given font. - * @param fnt The font to set the menus to. - * \note This methd needs rewriting BADLY - it's horribly written - */ -void FontSelectionView::UpdateFontSelection(BFont *fnt){ - - int i = 0; - char style[64]; - char family[64]; - - fnt->GetFamilyAndStyle(&family, &style); - - for(i = 0;i < fontList->CountItems();i++){ - - int j = 0; - - if(strcmp(fontList->ItemAt(i)->Label(), family) == 0){ - - fontList->ItemAt(i)->SetMarked(true); - - }//if - - for(j = 0;j < fontList->ItemAt(i)->Submenu()->CountItems();j++){ - - if(fontList->ItemAt(i)->Submenu()->ItemAt(j)->IsMarked()){ - - fontList->ItemAt(i)->Submenu()->ItemAt(j)->SetMarked(false); - - }//if - if(strcmp(fontList->ItemAt(i)->Label(), family) == 0){ - - if(strcmp(fontList->ItemAt(i)->Submenu()->ItemAt(j)->Label(), style) == 0){ - - fontList->ItemAt(i)->Submenu()->ItemAt(j)->SetMarked(true); - - }//if - - }//if - - }//for - - }//for - - //Update size menu - for(i = 0;i < sizeList->CountItems();i++){ - - char size[1]; - - sprintf(size, "%d", (int)fnt->Size()); - if(strcmp(sizeList->ItemAt(i)->Label(), size) == 0){ - - sizeList->ItemAt(i)->SetMarked(true); - break; - }//if - - }//for - -}//UpdateFontSelection - -/** - * Resets the test text to the default font. - */ -void FontSelectionView::resetToDefaults(){ - - //Update menus - UpdateFontSelection(defaultFont); - - //Update test text - SetTestTextFont(defaultFont); - -}//resetToDefaults - -/** - * Resets the test text to the original font. - */ -void FontSelectionView::revertToOriginal(){ - - //Update menus - UpdateFontSelection(&origFont); - - //Update test text - SetTestTextFont(&origFont); - -}//resetToDefaults - - +void +FontSelectionView::RescanFonts(void) +{ + EmptyMenus(); + BuildMenus(); +} diff --git a/src/prefs/fonts/FontSelectionView.h b/src/prefs/fonts/FontSelectionView.h index f5bb729738..935b4fcdcc 100644 --- a/src/prefs/fonts/FontSelectionView.h +++ b/src/prefs/fonts/FontSelectionView.h @@ -13,6 +13,7 @@ #include #include #include +#include enum { @@ -29,36 +30,27 @@ public: void AttachedToWindow(void); void MessageReceived(BMessage *msg); - void SetTestTextFont(BFont *fnt); - BFont GetTestTextFont(); - float GetSelectedSize(); - void GetSelectedFont(font_family *family); - void GetSelectedStyle(font_style *style); - void UpdateFontSelectionFromStyle(); - void UpdateFontSelection(); - void resetToDefaults(); - void revertToOriginal(); - + void SetDefaults(void); + void Revert(void); + void RescanFonts(void); + private: - void buildMenus(void); - void emptyMenus(void); + void BuildMenus(void); + void EmptyMenus(void); + void NotifyFontChange(void); - void EmptyMenu(BPopUpMenu *m); - void UpdateFontSelection(BFont *fnt); + BStringView *fPreviewText; - BStringView *testText; + BPopUpMenu *fFontMenu; + BPopUpMenu *fSizeMenu; + + int fType; - BPopUpMenu *fontList; - BPopUpMenu *sizeList; - - int minSizeIndex; - int maxSizeIndex; - - char typeLabel[30]; - - BFont origFont; - BFont workingFont; - BFont *defaultFont; + BFont fSavedFont; + BFont fCurrentFont; + BFont fDefaultFont; + + BMenuItem *fCurrentStyle; }; #endif diff --git a/src/prefs/fonts/FontView.cpp b/src/prefs/fonts/FontView.cpp index be7294fef0..48639ea406 100644 --- a/src/prefs/fonts/FontView.cpp +++ b/src/prefs/fonts/FontView.cpp @@ -15,48 +15,43 @@ FontView::FontView(BRect rect) BRect rect(bounds); rect.bottom = rect.top + FontHeight(true) *3.5; - plainSelectionView = new FontSelectionView(rect, "Plain", + fPlainView = new FontSelectionView(rect, "Plain", PLAIN_FONT_SELECTION_VIEW); - AddChild(plainSelectionView); + AddChild(fPlainView); rect.OffsetBy(0, rect.Height() + 4); - boldSelectionView = new FontSelectionView(rect, "Bold", + fBoldView = new FontSelectionView(rect, "Bold", BOLD_FONT_SELECTION_VIEW); - AddChild(boldSelectionView); + AddChild(fBoldView); rect.OffsetBy(0, rect.Height() + 4); - fixedSelectionView = new FontSelectionView(rect, "Fixed", + fFixedView = new FontSelectionView(rect, "Fixed", FIXED_FONT_SELECTION_VIEW); - AddChild(fixedSelectionView); + AddChild(fFixedView); } void -FontView::AttachedToWindow(void) +FontView::SetDefaults(void) { + fPlainView->SetDefaults(); + fBoldView->SetDefaults(); + fFixedView->SetDefaults(); } -/** - * Calls each FontSelectionView's resetToDefaults() function to reset - * the font and size menus to the default font. - */ void -FontView::resetToDefaults() +FontView::Revert(void) { - plainSelectionView->resetToDefaults(); - boldSelectionView->resetToDefaults(); - fixedSelectionView->resetToDefaults(); + fPlainView->Revert(); + fBoldView->Revert(); + fFixedView->Revert(); } -/** - * Calls each FontSelectionView's revertToOriginal() function to reset - * the font and size menus to the original font. - */ void -FontView::revertToOriginal() +FontView::RescanFonts(void) { - plainSelectionView->revertToOriginal(); - boldSelectionView->revertToOriginal(); - fixedSelectionView->revertToOriginal(); + fPlainView->RescanFonts(); + fBoldView->RescanFonts(); + fFixedView->RescanFonts(); } diff --git a/src/prefs/fonts/FontView.h b/src/prefs/fonts/FontView.h index 785dd1a9c3..51d0df4c76 100644 --- a/src/prefs/fonts/FontView.h +++ b/src/prefs/fonts/FontView.h @@ -14,14 +14,15 @@ class FontView : public BView { public: FontView(BRect frame); - void AttachedToWindow(void); - - void resetToDefaults(); - void revertToOriginal(); - - FontSelectionView *plainSelectionView; - FontSelectionView *boldSelectionView; - FontSelectionView *fixedSelectionView; + void SetDefaults(void); + void Revert(void); + void RescanFonts(void); + +private: + + FontSelectionView *fPlainView; + FontSelectionView *fBoldView; + FontSelectionView *fFixedView; }; #endif diff --git a/src/prefs/fonts/FontsSettings.h b/src/prefs/fonts/FontsSettings.h index 8d61ce39ee..cea83004a7 100644 --- a/src/prefs/fonts/FontsSettings.h +++ b/src/prefs/fonts/FontsSettings.h @@ -21,4 +21,4 @@ private: BPoint fCorner; }; -#endif //FONTS_SETTINGS_H +#endif diff --git a/src/prefs/fonts/MainWindow.cpp b/src/prefs/fonts/MainWindow.cpp index fba69f1e8b..fa3f76c94c 100644 --- a/src/prefs/fonts/MainWindow.cpp +++ b/src/prefs/fonts/MainWindow.cpp @@ -67,132 +67,36 @@ MainWindow::QuitRequested(void) void MainWindow::MessageReceived(BMessage *message) { - switch(message->what) { - -/* case PLAIN_SIZE_CHANGED_MSG: { - - updateSize(fSelectorView->plainSelectionView); + switch(message->what) + { + case M_ENABLE_REVERT: + { fButtonView->SetRevertState(true); break; } - case BOLD_SIZE_CHANGED_MSG: { - - updateSize(fSelectorView->boldSelectionView); + case M_RESCAN_FONTS: + { + fSelectorView->RescanFonts(); + break; + } + case M_SET_DEFAULTS: + { + fSelectorView->SetDefaults(); + fCacheView->SetDefaults(); fButtonView->SetRevertState(true); break; } - case FIXED_SIZE_CHANGED_MSG: { - - updateSize(fSelectorView->fixedSelectionView); - fButtonView->SetRevertState(true); - break; - } - case PLAIN_FONT_CHANGED_MSG: { - - updateFont(fSelectorView->plainSelectionView); - fButtonView->SetRevertState(true); - break; - } - case BOLD_FONT_CHANGED_MSG: { - - updateFont(fSelectorView->boldSelectionView); - fButtonView->SetRevertState(true); - break; - } - case FIXED_FONT_CHANGED_MSG: { - - updateFont(fSelectorView->fixedSelectionView); - fButtonView->SetRevertState(true); - break; - } - case PLAIN_STYLE_CHANGED_MSG: { - - updateStyle(fSelectorView->plainSelectionView); - fButtonView->SetRevertState(true); - break; - } - case BOLD_STYLE_CHANGED_MSG: { - - updateStyle(fSelectorView->boldSelectionView); - fButtonView->SetRevertState(true); - break; - } - case FIXED_STYLE_CHANGED_MSG: { - - updateStyle(fSelectorView->fixedSelectionView); - fButtonView->SetRevertState(true); - break; - } - case RESCAN_FONTS_MSG: { - - update_font_families(false); - fSelectorView->emptyMenus(); - fSelectorView->buildMenus(); - updateFont(fSelectorView->plainSelectionView); - updateFont(fSelectorView->boldSelectionView); - updateFont(fSelectorView->fixedSelectionView); - break; - } - case RESET_FONTS_MSG: { - - fSelectorView->resetToDefaults(); - fCacheView->resetToDefaults(); - fButtonView->SetRevertState(true); - break; - } -*/ case REVERT_MSG: { - - fSelectorView->revertToOriginal(); - fCacheView->revertToOriginal(); + case M_REVERT: + { + fSelectorView->Revert(); + fCacheView->Revert(); fButtonView->SetRevertState(false); break; } - default: { + default: + { BWindow::MessageReceived(message); break; } } - } - -// Sets the size of the test text when a new size is picked. -void -MainWindow::updateSize(FontSelectionView *theView) -{ - BFont workingFont; - - workingFont = theView->GetTestTextFont(); - workingFont.SetSize(theView->GetSelectedSize()); - theView->SetTestTextFont(&workingFont); -} - -// Updates the test text to the selected font. -void -MainWindow::updateFont(FontSelectionView *theView) -{ - BFont workingFont; - font_family updateTo; - - theView->UpdateFontSelection(); - workingFont = theView->GetTestTextFont(); - theView->GetSelectedFont(&updateTo); - workingFont.SetFamilyAndStyle(updateTo, NULL); - theView->SetTestTextFont(&workingFont); -} - -// Updates the test text to the selected style. -void -MainWindow::updateStyle(FontSelectionView *theView) -{ - BFont workingFont; - font_style updateTo; - font_family update; - - theView->UpdateFontSelectionFromStyle(); - workingFont = theView->GetTestTextFont(); - theView->GetSelectedStyle(&updateTo); - theView->GetSelectedFont(&update); - workingFont.SetFamilyAndStyle(update, updateTo); - theView->SetTestTextFont(&workingFont); -} - diff --git a/src/prefs/fonts/MainWindow.h b/src/prefs/fonts/MainWindow.h index 1bd61dce63..60f8568955 100644 --- a/src/prefs/fonts/MainWindow.h +++ b/src/prefs/fonts/MainWindow.h @@ -15,7 +15,15 @@ #include "CacheView.h" #include "ButtonView.h" #include "FontsSettings.h" - + +#define M_ENABLE_REVERT 'enrv' +#define M_REVERT 'rvrt' +#define M_SET_DEFAULTS 'stdf' + +#define M_SET_PLAIN 'stpl' +#define M_SET_BOLD 'stbl' +#define M_SET_FIXED 'stfx' + class MainWindow : public BWindow { public: @@ -24,16 +32,12 @@ public: virtual void MessageReceived(BMessage *message); private: - - void updateSize(FontSelectionView *theView); - void updateFont(FontSelectionView *theView); - void updateStyle(FontSelectionView *theView); - FontView *fSelectorView; - ButtonView *fButtonView; - CacheView *fCacheView; + FontView *fSelectorView; + ButtonView *fButtonView; + CacheView *fCacheView; - FontsSettings fSettings; + FontsSettings fSettings; }; #endif diff --git a/src/prefs/fonts/Pref_Utils.h b/src/prefs/fonts/Pref_Utils.h index 4379a1d3d4..3c48fd6231 100644 --- a/src/prefs/fonts/Pref_Utils.h +++ b/src/prefs/fonts/Pref_Utils.h @@ -4,7 +4,7 @@ #include #include -float FontHeight(bool full, BView* view = NULL); -color_map* ColorMap(); +float FontHeight(bool full, BView* view = NULL); +color_map* ColorMap(); #endif