Patch in large part by Philippe Saint-Pierre:

* Update Font preflet to use the layout-management. This already fixes a lot
  of issues.
* The were more issues with switching to default fonts and reverting fonts.
  Some fixed by Philippe and some fixed by myself.
* The preflet currently contains a work-arround for the problem that the window
  does not yet have it's final size until after BWindow::Show() has been
  completed. To be able to center itself anyways, it will open outside screen
  bounds and center itself then.

Note to Philippe: I had to fix some stuff in the Interface Kit to make this
more smoothly, so I was able to remove some calls to Invalidate() and such.
Also, the preview boxen and the menu fields align correctly now. Sorry if this
gave your gripes... :-)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30194 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2009-04-16 10:11:06 +00:00
parent 1b9d2885d3
commit 5f700c9b7c
6 changed files with 255 additions and 269 deletions
+62 -116
View File
@@ -1,14 +1,15 @@
/* /*
* Copyright 2001-2005, Haiku. * Copyright 2001-2009, Haiku.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Mark Hogben * Mark Hogben
* DarkWyrm <[email protected]> * DarkWyrm <[email protected]>
* Axel Dörfler, [email protected] * Axel Dörfler, [email protected]
* Philippe Saint-Pierre, [email protected]
* Stephan Aßmus <[email protected]>
*/ */
#include "FontSelectionView.h" #include "FontSelectionView.h"
#include "MainWindow.h" #include "MainWindow.h"
@@ -18,6 +19,8 @@
#include <PopUpMenu.h> #include <PopUpMenu.h>
#include <String.h> #include <String.h>
#include <StringView.h> #include <StringView.h>
#include <LayoutItem.h>
#include <GroupLayoutBuilder.h>
#include <stdio.h> #include <stdio.h>
@@ -26,10 +29,6 @@
// if defined, the system font will be updated immediately, and not // if defined, the system font will be updated immediately, and not
// only on exit // only on exit
static const int32 kMsgSetFamily = 'fmly';
static const int32 kMsgSetStyle = 'styl';
static const int32 kMsgSetSize = 'size';
static const float kMinSize = 8.0; static const float kMinSize = 8.0;
static const float kMaxSize = 18.0; static const float kMaxSize = 18.0;
@@ -66,10 +65,9 @@ _get_system_default_font_(const char* which, font_family family,
// #pragma mark - // #pragma mark -
FontSelectionView::FontSelectionView(BRect _rect, const char* name, FontSelectionView::FontSelectionView(const char* name,
const char* label, const BFont* currentFont) const char* label, const BFont* currentFont)
: BView(_rect, name, B_FOLLOW_LEFT_RIGHT, B_WILL_DRAW), : BView(name, B_WILL_DRAW)
fSavedFont(currentFont)
{ {
if (currentFont == NULL) { if (currentFont == NULL) {
if (!strcmp(Name(), "plain")) if (!strcmp(Name(), "plain"))
@@ -88,7 +86,7 @@ FontSelectionView::FontSelectionView(BRect _rect, const char* name,
} else } else
fCurrentFont = *currentFont; fCurrentFont = *currentFont;
fDivider = StringWidth(label) + 5; fSavedFont = fCurrentFont;
fSizesMenu = new BPopUpMenu("size menu"); fSizesMenu = new BPopUpMenu("size menu");
_BuildSizesMenu(); _BuildSizesMenu();
@@ -96,45 +94,26 @@ FontSelectionView::FontSelectionView(BRect _rect, const char* name,
fFontsMenu = new BPopUpMenu("font menu"); fFontsMenu = new BPopUpMenu("font menu");
// font menu // font menu
BRect rect(Bounds()); fFontsMenuField = new BMenuField("fonts", label, fFontsMenu, NULL);
fFontsMenuField = new BMenuField(rect, "fonts", label, fFontsMenu, false);
fFontsMenuField->SetDivider(fDivider);
fFontsMenuField->SetAlignment(B_ALIGN_RIGHT); fFontsMenuField->SetAlignment(B_ALIGN_RIGHT);
fFontsMenuField->ResizeToPreferred();
AddChild(fFontsMenuField);
// size menu // size menu
rect.right = rect.left + StringWidth("Size: 99") + 30.0f; fSizesMenuField = new BMenuField("size", "Size:", fSizesMenu, NULL);
fSizesMenuField = new BMenuField(rect, "sizes", "Size:", fSizesMenu, true,
B_FOLLOW_TOP | B_FOLLOW_RIGHT);
fSizesMenuField->SetDivider(StringWidth(fSizesMenuField->Label()) + 5.0f);
fSizesMenuField->SetAlignment(B_ALIGN_RIGHT); fSizesMenuField->SetAlignment(B_ALIGN_RIGHT);
fSizesMenuField->ResizeToPreferred();
rect = Bounds();
fSizesMenuField->MoveBy(rect.right - fSizesMenuField->Bounds().Width(), 0);
AddChild(fSizesMenuField);
rect.top += fFontsMenuField->Bounds().Height() + 5; // preview
rect.left = fDivider; fPreviewText = new BStringView("preview text",
BFont font = be_plain_font; "The quick brown fox jumps over the lazy dog.");
font.SetSize(kMaxSize);
font_height height;
font.GetHeight(&height);
rect.bottom = rect.top + ceil(height.ascent + height.descent
+ height.leading) + 5;
fPreviewBox = new BBox(rect, "preview", B_FOLLOW_LEFT_RIGHT,
B_WILL_DRAW | B_FRAME_EVENTS);
AddChild(fPreviewBox);
// Place the text slightly inside the entire box area, so
// 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.",
B_FOLLOW_LEFT_RIGHT);
fPreviewText->SetFont(&fCurrentFont); fPreviewText->SetFont(&fCurrentFont);
fPreviewBox->AddChild(fPreviewText); 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)
);
} }
@@ -146,66 +125,10 @@ FontSelectionView::~FontSelectionView()
} }
void BView*
FontSelectionView::GetPreferredSize(float *_width, float *_height) FontSelectionView::GetPreviewBox()
{ {
// don't change the width if it is large enough return fPreviewBox;
if (_width) {
*_width = fMaxFontNameWidth + 40 + fDivider
+ fSizesMenuField->Bounds().Width();
if (*_width < Bounds().Width())
*_width = Bounds().Width();
}
if (_height) {
BView* view = FindView("preview");
if (view != NULL)
*_height = view->Frame().bottom;
}
}
void
FontSelectionView::SetDivider(float divider)
{
fFontsMenuField->SetDivider(divider);
fPreviewBox->ResizeBy(fDivider - divider, 0);
fPreviewBox->MoveBy(divider - fDivider, 0);
// if the view is not yet attached to a window, the resizing mode is ignored
if (Window() == NULL)
fPreviewText->ResizeBy(fDivider - divider, 0);
fDivider = divider;
}
void
FontSelectionView::RelayoutIfNeeded()
{
float width, height;
GetPreferredSize(&width, &height);
if (width > Bounds().Width()) {
fSizesMenuField->MoveTo(fMaxFontNameWidth + fDivider + 40.0f,
fFontsMenuField->Bounds().top);
ResizeTo(width, height);
}
}
void
FontSelectionView::AttachedToWindow()
{
if (Parent() != NULL)
SetViewColor(Parent()->ViewColor());
else
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fSizesMenu->SetTargetForItems(this);
UpdateFontsMenu();
} }
@@ -222,8 +145,6 @@ FontSelectionView::MessageReceived(BMessage *msg)
fCurrentFont.SetSize(size); fCurrentFont.SetSize(size);
_UpdateFontPreview(); _UpdateFontPreview();
Window()->PostMessage(kMsgUpdate);
break; break;
} }
@@ -250,8 +171,6 @@ FontSelectionView::MessageReceived(BMessage *msg)
_UpdateFontPreview(); _UpdateFontPreview();
} }
} }
Window()->PostMessage(kMsgUpdate);
break; break;
} }
@@ -272,8 +191,6 @@ FontSelectionView::MessageReceived(BMessage *msg)
fCurrentFont.SetFamilyAndStyle(family, style); fCurrentFont.SetFamilyAndStyle(family, style);
_UpdateFontPreview(); _UpdateFontPreview();
Window()->PostMessage(kMsgUpdate);
break; break;
} }
@@ -283,6 +200,34 @@ FontSelectionView::MessageReceived(BMessage *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 void
FontSelectionView::_BuildSizesMenu() FontSelectionView::_BuildSizesMenu()
{ {
@@ -299,6 +244,7 @@ FontSelectionView::_BuildSizesMenu()
BMessage* message = new BMessage(kMsgSetSize); BMessage* message = new BMessage(kMsgSetSize);
message->AddInt32("size", size); message->AddInt32("size", size);
message->AddString("name", Name());
BMenuItem* item = new BMenuItem(label, message); BMenuItem* item = new BMenuItem(label, message);
if (size == fCurrentFont.Size()) if (size == fCurrentFont.Size())
@@ -345,7 +291,6 @@ void
FontSelectionView::_UpdateFontPreview() FontSelectionView::_UpdateFontPreview()
{ {
fPreviewText->SetFont(&fCurrentFont); fPreviewText->SetFont(&fCurrentFont);
fPreviewText->Invalidate();
#ifdef INSTANT_UPDATE #ifdef INSTANT_UPDATE
_UpdateSystemFont(); _UpdateSystemFont();
@@ -360,7 +305,7 @@ FontSelectionView::_UpdateSystemFont()
font_style style; font_style style;
fCurrentFont.GetFamilyAndStyle(&family, &style); fCurrentFont.GetFamilyAndStyle(&family, &style);
if (!strcmp(Name(), "menu")) { if (strcmp(Name(), "menu") == 0) {
// The menu font is not handled as a system font // The menu font is not handled as a system font
menu_info info; menu_info info;
get_menu_info(&info); get_menu_info(&info);
@@ -383,7 +328,7 @@ FontSelectionView::SetDefaults()
float size; float size;
const char* fontName; const char* fontName;
if (!strcmp(Name(), "menu")) if (strcmp(Name(), "menu") == 0)
fontName = "plain"; fontName = "plain";
else else
fontName = Name(); fontName = Name();
@@ -434,13 +379,15 @@ FontSelectionView::IsDefaultable()
float defaultSize; float defaultSize;
const char* fontName; const char* fontName;
if (!strcmp(Name(), "menu")) if (strcmp(Name(), "menu") == 0)
fontName = "plain"; fontName = "plain";
else else
fontName = Name(); fontName = Name();
if (_get_system_default_font_(fontName, defaultFamily, defaultStyle, &defaultSize) != B_OK) if (_get_system_default_font_(fontName, defaultFamily, defaultStyle,
&defaultSize) != B_OK) {
return false; return false;
}
font_family currentFamily; font_family currentFamily;
font_style currentStyle; font_style currentStyle;
@@ -495,6 +442,8 @@ FontSelectionView::UpdateFontsMenu()
BMessage* message = new BMessage(kMsgSetFamily); BMessage* message = new BMessage(kMsgSetFamily);
message->AddString("family", family); message->AddString("family", family);
message->AddString("name", Name());
BMenuItem* familyItem = new BMenuItem(stylesMenu, message); BMenuItem* familyItem = new BMenuItem(stylesMenu, message);
fFontsMenu->AddItem(familyItem); fFontsMenu->AddItem(familyItem);
@@ -508,6 +457,7 @@ FontSelectionView::UpdateFontsMenu()
message = new BMessage(kMsgSetStyle); message = new BMessage(kMsgSetStyle);
message->AddString("family", (char*)family); message->AddString("family", (char*)family);
message->AddString("style", (char*)style); message->AddString("style", (char*)style);
message->AddString("name", Name());
BMenuItem *item = new BMenuItem(style, message); BMenuItem *item = new BMenuItem(style, message);
@@ -518,9 +468,5 @@ FontSelectionView::UpdateFontsMenu()
} }
stylesMenu->AddItem(item); stylesMenu->AddItem(item);
} }
stylesMenu->SetTargetForItems(this);
} }
fFontsMenu->SetTargetForItems(this);
} }
+39 -30
View File
@@ -6,6 +6,7 @@
* Mark Hogben * Mark Hogben
* DarkWyrm <[email protected]> * DarkWyrm <[email protected]>
* Axel Dörfler, [email protected] * Axel Dörfler, [email protected]
* Philippe Saint-Pierre, [email protected]
*/ */
#ifndef FONT_SELECTION_VIEW_H #ifndef FONT_SELECTION_VIEW_H
#define FONT_SELECTION_VIEW_H #define FONT_SELECTION_VIEW_H
@@ -13,53 +14,61 @@
#include <View.h> #include <View.h>
class BLayoutItem;
class BBox; class BBox;
class BMenuField; class BMenuField;
class BPopUpMenu; class BPopUpMenu;
class BStringView; class BStringView;
static const int32 kMsgSetFamily = 'fmly';
static const int32 kMsgSetStyle = 'styl';
static const int32 kMsgSetSize = 'size';
class FontSelectionView : public BView { class FontSelectionView : public BView {
public: public:
FontSelectionView(BRect rect, const char* name, FontSelectionView(const char* name,
const char* label, const BFont* font = NULL); const char* label,
virtual ~FontSelectionView(); const BFont* font = NULL);
virtual ~FontSelectionView();
virtual void GetPreferredSize(float *_width, float *_height);
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage *msg);
void SetDivider(float divider); virtual void MessageReceived(BMessage* message);
void RelayoutIfNeeded();
void SetDefaults(); void SetDefaults();
void Revert(); void Revert();
bool IsDefaultable(); bool IsDefaultable();
bool IsRevertable(); bool IsRevertable();
void UpdateFontsMenu(); void UpdateFontsMenu();
BLayoutItem* CreateSizesLabelLayoutItem();
BLayoutItem* CreateSizesMenuBarLayoutItem();
BLayoutItem* CreateFontsLabelLayoutItem();
BLayoutItem* CreateFontsMenuBarLayoutItem();
BView* GetPreviewBox();
private: private:
void _SelectCurrentFont(bool select); void _SelectCurrentFont(bool select);
void _SelectCurrentSize(bool select); void _SelectCurrentSize(bool select);
void _UpdateFontPreview(); void _UpdateFontPreview();
void _UpdateSystemFont(); void _UpdateSystemFont();
void _BuildSizesMenu(); void _BuildSizesMenu();
protected: protected:
float fDivider; BMenuField* fFontsMenuField;
BMenuField* fSizesMenuField;
BPopUpMenu* fFontsMenu;
BPopUpMenu* fSizesMenu;
BMenuField* fFontsMenuField; BBox* fPreviewBox;
BMenuField* fSizesMenuField; BStringView* fPreviewText;
BPopUpMenu* fFontsMenu;
BPopUpMenu* fSizesMenu;
BBox* fPreviewBox; BFont fSavedFont;
BStringView* fPreviewText; BFont fCurrentFont;
float fMaxFontNameWidth;
BFont fSavedFont;
BFont fCurrentFont;
float fMaxFontNameWidth;
}; };
#endif /* FONT_SELECTION_VIEW_H */ #endif // FONT_SELECTION_VIEW_H
+78 -53
View File
@@ -6,65 +6,103 @@
* Mark Hogben * Mark Hogben
* DarkWyrm <[email protected]> * DarkWyrm <[email protected]>
* Axel Dörfler, [email protected] * Axel Dörfler, [email protected]
* Philippe St-Pierre, [email protected]
* Stephan Aßmus <[email protected]>
*/ */
#include "FontView.h" #include "FontView.h"
#include <GridLayoutBuilder.h>
#include <GroupLayoutBuilder.h>
#include <SpaceLayoutItem.h>
FontView::FontView(BRect _rect)
: BView(_rect, "Fonts", B_FOLLOW_ALL, B_WILL_DRAW) static void
add_font_selection_view(BGridLayout* layout, FontSelectionView* view,
int32& row, bool withExtraSpace)
{ {
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); layout->AddItem(view->CreateFontsLabelLayoutItem(), 0, row);
BRect rect(Bounds()); layout->AddItem(view->CreateFontsMenuBarLayoutItem(), 1, row);
float labelWidth = StringWidth("Fixed Font:") + 8; layout->AddItem(BSpaceLayoutItem::CreateGlue(), 2, row);
fPlainView = new FontSelectionView(rect, "plain", "Plain Font:"); layout->AddItem(view->CreateSizesLabelLayoutItem(), 3, row);
fPlainView->SetDivider(labelWidth); layout->AddItem(view->CreateSizesMenuBarLayoutItem(), 4, row);
fPlainView->ResizeToPreferred();
AddChild(fPlainView);
rect.OffsetBy(0, fPlainView->Bounds().Height() + 10); row++;
fBoldView = new FontSelectionView(rect, "bold", "Bold Font:");
fBoldView->SetDivider(labelWidth);
fBoldView->ResizeToPreferred();
AddChild(fBoldView);
rect.OffsetBy(0, fPlainView->Bounds().Height() + 10); layout->AddItem(BSpaceLayoutItem::CreateGlue(), 0, row);
fFixedView = new FontSelectionView(rect, "fixed", "Fixed Font:"); layout->AddView(view->GetPreviewBox(), 1, row, 4);
fFixedView->SetDivider(labelWidth);
fFixedView->ResizeToPreferred();
AddChild(fFixedView);
rect.OffsetBy(0, fFixedView->Bounds().Height() + 10); row++;
fMenuView = new FontSelectionView(rect, "menu", "Menu Font:");
fMenuView->SetDivider(labelWidth); if (withExtraSpace) {
fMenuView->ResizeToPreferred(); layout->AddItem(BSpaceLayoutItem::CreateVerticalStrut(5), 0, row, 5);
AddChild(fMenuView); row++;
}
} }
void FontView::FontView()
FontView::GetPreferredSize(float *_width, float *_height) : BView("Fonts", B_WILL_DRAW )
{ {
if (_width) SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
*_width = fPlainView->Bounds().Width();
fPlainView = new FontSelectionView("plain", "Plain Font:");
fBoldView = new FontSelectionView("bold", "Bold Font:");
fFixedView = new FontSelectionView("fixed", "Fixed Font:");
fMenuView = new FontSelectionView("menu", "Menu Font:");
if (_height) BGridLayout* layout = new BGridLayout(5, 5);
*_height = fPlainView->Bounds().Height() * 4 + 40; 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 void
FontView::SetDefaults() FontView::SetDefaults()
{ {
for (int32 i = 0; i < CountChildren(); i++) { fPlainView->SetDefaults();
FontSelectionView* view = dynamic_cast<FontSelectionView *>(ChildAt(i)); fBoldView->SetDefaults();
if (view == NULL) fFixedView->SetDefaults();
continue; fMenuView->SetDefaults();
}
view->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;
}
default:
BView::MessageReceived(message);
} }
} }
@@ -72,13 +110,10 @@ FontView::SetDefaults()
void void
FontView::Revert() FontView::Revert()
{ {
for (int32 i = 0; i < CountChildren(); i++) { fPlainView->Revert();
FontSelectionView* view = dynamic_cast<FontSelectionView *>(ChildAt(i)); fBoldView->Revert();
if (view == NULL) fFixedView->Revert();
continue; fMenuView->Revert();
view->Revert();
}
} }
@@ -92,16 +127,6 @@ FontView::UpdateFonts()
} }
void
FontView::RelayoutIfNeeded()
{
fPlainView->RelayoutIfNeeded();
fBoldView->RelayoutIfNeeded();
fFixedView->RelayoutIfNeeded();
fMenuView->RelayoutIfNeeded();
}
bool bool
FontView::IsDefaultable() FontView::IsDefaultable()
{ {
+8 -5
View File
@@ -6,28 +6,30 @@
* Mark Hogben * Mark Hogben
* DarkWyrm <[email protected]> * DarkWyrm <[email protected]>
* Axel Dörfler, [email protected] * Axel Dörfler, [email protected]
* Philippe Saint-Pierre, [email protected]
*/ */
#ifndef FONT_VIEW_H #ifndef FONT_VIEW_H
#define FONT_VIEW_H #define FONT_VIEW_H
#include "FontSelectionView.h" #include "FontSelectionView.h"
#include "MainWindow.h"
#include <Message.h>
class FontView : public BView { class FontView : public BView {
public: public:
FontView(BRect frame); FontView();
virtual void GetPreferredSize(float *_width, float *_height); virtual void MessageReceived(BMessage* message);
void SetDefaults(); void SetDefaults();
void Revert(); void Revert();
void UpdateFonts(); void UpdateFonts();
void RelayoutIfNeeded();
bool IsDefaultable(); bool IsDefaultable();
bool IsRevertable(); bool IsRevertable();
private: private:
FontSelectionView* fPlainView; FontSelectionView* fPlainView;
FontSelectionView* fBoldView; FontSelectionView* fBoldView;
@@ -35,4 +37,5 @@ private:
FontSelectionView* fMenuView; FontSelectionView* fMenuView;
}; };
#endif /* FONT_VIEW_H */ #endif /* FONT_VIEW_H */
+62 -63
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2005, Haiku. * Copyright 2001-2009, Haiku.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -7,17 +7,25 @@
* DarkWyrm <[email protected]> * DarkWyrm <[email protected]>
* Axel Dörfler, [email protected] * Axel Dörfler, [email protected]
* Andrej Spielmann, <[email protected]> * Andrej Spielmann, <[email protected]>
* Philippe Saint-Pierre, [email protected]
*/ */
#include "FontView.h"
#include "MainWindow.h" #include "MainWindow.h"
#include <stdio.h>
#include <Alert.h>
#include <Application.h> #include <Application.h>
#include <Button.h> #include <Button.h>
#include <GridLayoutBuilder.h>
#include <GroupLayoutBuilder.h>
#include <MessageRunner.h> #include <MessageRunner.h>
#include <Screen.h> #include <Screen.h>
#include <SpaceLayoutItem.h>
#include <TabView.h> #include <TabView.h>
#include <TextView.h>
#include "FontView.h"
static const uint32 kMsgSetDefaults = 'dflt'; static const uint32 kMsgSetDefaults = 'dflt';
@@ -26,83 +34,57 @@ static const uint32 kMsgCheckFonts = 'chkf';
MainWindow::MainWindow() MainWindow::MainWindow()
: BWindow(BRect(100, 100, 445, 410), "Fonts", B_TITLED_WINDOW, : BWindow(BRect(0, 0, 1, 1), "Fonts", B_TITLED_WINDOW,
B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE) B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
fCentered(false)
{ {
BRect rect = Bounds(); fDefaultsButton = new BButton("defaults", "Defaults",
BView* view = new BView(rect, "background", B_FOLLOW_ALL, B_WILL_DRAW); new BMessage(kMsgSetDefaults), B_WILL_DRAW);
view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
AddChild(view);
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);
fDefaultsButton->ResizeToPreferred();
fDefaultsButton->SetEnabled(false); fDefaultsButton->SetEnabled(false);
float buttonHeight = fDefaultsButton->Bounds().Height();
fDefaultsButton->MoveBy(0, -buttonHeight);
view->AddChild(fDefaultsButton);
rect = fDefaultsButton->Frame(); fRevertButton = new BButton("revert", "Revert",
rect.OffsetBy(fDefaultsButton->Bounds().Width() + 10, 0); new BMessage(kMsgRevert), B_WILL_DRAW);
fRevertButton->SetEnabled(false);
fRevertButton = new BButton(rect, "revert", "Revert", BTabView* tabView = new BTabView("tabview", B_WIDTH_FROM_LABEL);
new BMessage(kMsgRevert), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM, B_WILL_DRAW);
fRevertButton->ResizeToPreferred();
fRevertButton->SetEnabled(false);
view->AddChild(fRevertButton);
rect = Bounds(); fFontsView = new FontView();
rect.top += 5;
rect.bottom -= 20 + buttonHeight;
rect.left += 5;
BTabView *tabView = new BTabView(rect, "tabview", B_WIDTH_FROM_LABEL);
rect = tabView->ContainerView()->Bounds().InsetByCopy(5, 8);
fFontsView = new FontView(rect);
tabView->AddTab(fFontsView); tabView->AddTab(fFontsView);
fFontsView->UpdateFonts(); fFontsView->UpdateFonts();
fFontsView->RelayoutIfNeeded();
float width, height;
fFontsView->GetPreferredSize(&width, &height);
// make sure the tab is large enough for the fonts view SetLayout(new BGroupLayout(B_VERTICAL));
float widthDiff = width + 10
- tabView->ContainerView()->Bounds().Width();
if (widthDiff > 0) {
tabView->ResizeBy(widthDiff, 0);
tabView->ContainerView()->ResizeBy(widthDiff, 0);
}
float heightDiff = height + 16 const float kInset = 8;
- tabView->ContainerView()->Bounds().Height();
if (heightDiff > 0) {
tabView->ResizeBy(0, heightDiff);
tabView->ContainerView()->ResizeBy(0, heightDiff);
}
ResizeTo(tabView->Bounds().Width() + 10, tabView->Frame().bottom + 20 AddChild(BGroupLayoutBuilder(B_VERTICAL)
+ buttonHeight); .Add(tabView)
view->AddChild(tabView); .Add(BSpaceLayoutItem::CreateVerticalStrut(kInset))
fFontsView->ResizeToPreferred(); .Add(BGroupLayoutBuilder(B_HORIZONTAL)
.Add(fDefaultsButton)
SetSizeLimits(Bounds().Width(), 16347, .Add(BSpaceLayoutItem::CreateHorizontalStrut(kInset))
Bounds().Height(), Bounds().Height()); .Add(fRevertButton)
.Add(BSpaceLayoutItem::CreateGlue())
)
.SetInsets(kInset, kInset, kInset, kInset)
);
if (fSettings.WindowCorner() == BPoint(-1, -1)) { if (fSettings.WindowCorner() == BPoint(-1, -1)) {
// center window on screen // center window on screen
_Center(); fCentered = true;
} else { } else {
MoveTo(fSettings.WindowCorner()); MoveTo(fSettings.WindowCorner());
// make sure window is on screen // make sure window is on screen
BScreen screen(this); BScreen screen(this);
if (!screen.Frame().InsetByCopy(10, 10).Intersects(Frame())) if (!screen.Frame().InsetByCopy(10, 10).Intersects(Frame()))
_Center(); fCentered = true;
}
if (fCentered) {
// draw offscreen to avoid flashing windows
MoveTo(BPoint(-1000, -1000));
} }
fRunner = new BMessageRunner(this, new BMessage(kMsgCheckFonts), 3000000); fRunner = new BMessageRunner(this, new BMessage(kMsgCheckFonts), 3000000);
@@ -112,6 +94,16 @@ MainWindow::MainWindow()
} }
void
MainWindow::Show()
{
BWindow::Show();
if (fCentered)
_Center();
}
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
delete fRunner; delete fRunner;
@@ -132,6 +124,12 @@ void
MainWindow::MessageReceived(BMessage *message) MainWindow::MessageReceived(BMessage *message)
{ {
switch (message->what) { switch (message->what) {
case kMsgSetSize:
case kMsgSetFamily:
case kMsgSetStyle:
fFontsView->MessageReceived(message);
break;
case kMsgUpdate: case kMsgUpdate:
fDefaultsButton->SetEnabled(fFontsView->IsDefaultable()); fDefaultsButton->SetEnabled(fFontsView->IsDefaultable());
fRevertButton->SetEnabled(fFontsView->IsRevertable()); fRevertButton->SetEnabled(fFontsView->IsRevertable());
@@ -164,9 +162,10 @@ MainWindow::MessageReceived(BMessage *message)
void void
MainWindow::_Center() MainWindow::_Center()
{ {
BScreen screen; BRect screenFrame = BScreen(this).Frame();
BRect screenFrame = screen.Frame(); BRect windowRect = Frame();
MoveTo(screenFrame.left + (screenFrame.Width() - Bounds().Width()) / 2, MoveTo(
screenFrame.top + (screenFrame.Height() - Bounds().Height()) / 2); (screenFrame.Width() - windowRect.Width()) / 2,
(screenFrame.Height() - windowRect.Height()) / 2);
} }
+6 -2
View File
@@ -27,9 +27,13 @@ public:
virtual bool QuitRequested(); virtual bool QuitRequested();
virtual void MessageReceived(BMessage *message); virtual void MessageReceived(BMessage *message);
virtual void Show();
private: private:
void _Center(); void _Center();
bool fCentered;
BMessageRunner* fRunner; BMessageRunner* fRunner;
FontView* fFontsView; FontView* fFontsView;
@@ -41,4 +45,4 @@ private:
static const int32 kMsgUpdate = 'updt'; static const int32 kMsgUpdate = 'updt';
#endif /* MAIN_WINDOW_H */ #endif // MAIN_WINDOW_H