Add font sizes to Font Size menu in addition to in settings

This commit is contained in:
John Scipione 2012-12-07 03:45:40 -05:00
parent e206ff5692
commit dc1456f662
5 changed files with 117 additions and 26 deletions

View File

@ -94,11 +94,11 @@ AppearancePrefView::AppearancePrefView(const char* name,
PrefHandler::Default()->getString(PREF_HALF_FONT_FAMILY),
PrefHandler::Default()->getString(PREF_HALF_FONT_STYLE));
BMenu* sizeMenu = _MakeSizeMenu(MSG_HALF_SIZE_CHANGED,
BMenu* fontSizeMenu = _MakeFontSizeMenu(MSG_HALF_SIZE_CHANGED,
PrefHandler::Default()->getInt32(PREF_HALF_FONT_SIZE));
fFont = new BMenuField(B_TRANSLATE("Font:"), fontMenu);
fFontSize = new BMenuField(B_TRANSLATE("Size:"), sizeMenu);
fFontSize = new BMenuField(B_TRANSLATE("Font size:"), fontSizeMenu);
BPopUpMenu* schemesPopUp = _MakeColorSchemeMenu(MSG_COLOR_SCHEME_CHANGED,
gPredefinedColorSchemes, gPredefinedColorSchemes[0]);
@ -456,7 +456,7 @@ AppearancePrefView::_MakeFontMenu(uint32 command,
/*static*/ BMenu*
AppearancePrefView::_MakeSizeMenu(uint32 command, uint8 defaultSize)
AppearancePrefView::_MakeFontSizeMenu(uint32 command, uint8 defaultSize)
{
BPopUpMenu* menu = new BPopUpMenu("size");
int32 sizes[] = {9, 10, 11, 12, 14, 16, 18, 0};
@ -467,7 +467,8 @@ AppearancePrefView::_MakeSizeMenu(uint32 command, uint8 defaultSize)
BString string;
string << sizes[i];
BMenuItem* item = new BMenuItem(string.String(), new BMessage(command));
BMenuItem* item = new BMenuItem(string.String(),
new BMessage(command));
menu->AddItem(item);
if (sizes[i] == defaultSize) {
@ -475,6 +476,7 @@ AppearancePrefView::_MakeSizeMenu(uint32 command, uint8 defaultSize)
found = true;
}
}
if (!found) {
for (uint32 i = 0; sizes[i]; i++) {
if (sizes[i] > defaultSize) {

View File

@ -62,7 +62,7 @@ private:
static BMenu* _MakeFontMenu(uint32 command,
const char* defaultFamily,
const char* defaultStyle);
static BMenu* _MakeSizeMenu(uint32 command,
static BMenu* _MakeFontSizeMenu(uint32 command,
uint8 defaultSize);
static BPopUpMenu* _MakeMenu(uint32 msg, const char** items,

View File

@ -945,8 +945,8 @@ TermView::SetTermFont(const BFont *font)
fHalfFont.SetSpacing(B_FIXED_SPACING);
// calculate half font's max width
// Not Bounding, check only A-Z(For case of fHalfFont is KanjiFont. )
for (int c = 0x20 ; c <= 0x7e; c++){
// Not Bounding, check only A-Z (For case of fHalfFont is KanjiFont.)
for (int c = 0x20; c <= 0x7e; c++) {
char buf[4];
sprintf(buf, "%c", c);
int tmpWidth = (int)fHalfFont.StringWidth(buf);

View File

@ -11,6 +11,7 @@
#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
@ -449,6 +450,16 @@ TermWindow::_MakeEncodingMenu()
void
TermWindow::_SetupMenu()
{
fFontSizeMenu = _MakeFontSizeMenu(MSG_HALF_SIZE_CHANGED,
PrefHandler::Default()->getInt32(PREF_HALF_FONT_SIZE));
fIncreaseFontSizeMenuItem = new BMenuItem(B_TRANSLATE("Increase"),
new BMessage(kIncreaseFontSize), '+', B_COMMAND_KEY);
fDecreaseFontSizeMenuItem = new BMenuItem(B_TRANSLATE("Decrease"),
new BMessage(kDecreaseFontSize), '-', B_COMMAND_KEY);
fFontSizeMenu->AddSeparatorItem();
fFontSizeMenu->AddItem(fIncreaseFontSizeMenuItem);
fFontSizeMenu->AddItem(fDecreaseFontSizeMenuItem);
BLayoutBuilder::Menu<>(fMenuBar = new BMenuBar(Bounds(), "mbar"))
// Terminal
.AddMenu(B_TRANSLATE_SYSTEM_NAME("Terminal"))
@ -491,14 +502,7 @@ TermWindow::_SetupMenu()
.AddMenu(B_TRANSLATE("Settings"))
.AddItem(_MakeWindowSizeMenu())
.AddItem(fEncodingMenu = _MakeEncodingMenu())
.AddMenu(B_TRANSLATE("Text size"))
.AddItem(B_TRANSLATE("Increase"), kIncreaseFontSize, '+',
B_COMMAND_KEY)
.GetItem(fIncreaseFontSizeMenuItem)
.AddItem(B_TRANSLATE("Decrease"), kDecreaseFontSize, '-',
B_COMMAND_KEY)
.GetItem(fDecreaseFontSizeMenuItem)
.End()
.AddItem(fFontSizeMenu)
.AddSeparator()
.AddItem(B_TRANSLATE("Settings" B_UTF8_ELLIPSIS), MENU_PREF_OPEN)
.AddSeparator()
@ -779,10 +783,9 @@ TermWindow::MessageReceived(BMessage *message)
_ResizeView(_ActiveTermView());
break;
}
case MSG_HALF_FONT_CHANGED:
case MSG_FULL_FONT_CHANGED:
case MSG_HALF_SIZE_CHANGED:
case MSG_FULL_SIZE_CHANGED:
{
BFont font;
_GetPreferredFont(font);
@ -792,6 +795,36 @@ TermWindow::MessageReceived(BMessage *message)
break;
}
case MSG_HALF_SIZE_CHANGED:
case MSG_FULL_SIZE_CHANGED:
{
TermView* view = _ActiveTermView();
BFont font;
view->GetTermFont(&font);
int32 size;
if (message->FindInt32("font size", &size) != B_OK)
break;
// mark the font size menu item
for (int32 i = 0; i < fFontSizeMenu->CountItems(); i++) {
BMenuItem* item = fFontSizeMenu->ItemAt(i);
if (item == NULL)
continue;
item->SetMarked(false);
if (atoi(item->Label()) == size)
item->SetMarked(true);
}
font.SetSize(size);
view->SetTermFont(&font);
PrefHandler::Default()->setInt32(PREF_HALF_FONT_SIZE, (int32)size);
_ResizeView(view);
break;
}
case FULLSCREEN:
if (!fSavedFrame.IsValid()) { // go fullscreen
_ActiveTermView()->DisableResizeView();
@ -960,19 +993,30 @@ TermWindow::MessageReceived(BMessage *message)
TermView* view = _ActiveTermView();
BFont font;
view->GetTermFont(&font);
float size = font.Size();
if (message->what == kIncreaseFontSize)
size += 1;
else
size -= 1;
// limit the font size
if (message->what == kIncreaseFontSize)
size < 12 ? size += 1 : size += 2;
else
size < 14 ? size -= 1 : size -= 2;
// constrain the font size
if (size < 9)
size = 9;
if (size > 18)
size = 18;
// mark the font size menu item
for (int32 i = 0; i < fFontSizeMenu->CountItems(); i++) {
BMenuItem* item = fFontSizeMenu->ItemAt(i);
if (item == NULL)
continue;
item->SetMarked(false);
if (atoi(item->Label()) == size)
item->SetMarked(true);
}
font.SetSize(size);
view->SetTermFont(&font);
PrefHandler::Default()->setInt32(PREF_HALF_FONT_SIZE, (int32)size);
@ -1546,8 +1590,7 @@ TermWindow::_ResizeView(TermView *view)
}
/* static */
BMenu*
/* static */ BMenu*
TermWindow::_MakeWindowSizeMenu()
{
BMenu* menu = new (std::nothrow) BMenu(B_TRANSLATE("Window size"));
@ -1581,6 +1624,49 @@ TermWindow::_MakeWindowSizeMenu()
}
/*static*/ BMenu*
TermWindow::_MakeFontSizeMenu(uint32 command, uint8 defaultSize)
{
BMenu* menu = new (std::nothrow) BMenu(B_TRANSLATE("Font size"));
if (menu == NULL)
return NULL;
int32 sizes[] = {9, 10, 11, 12, 14, 16, 18, 0};
bool found = false;
for (uint32 i = 0; sizes[i]; i++) {
BString string;
string << sizes[i];
BMessage* message = new BMessage(command);
message->AddInt32("font size", sizes[i]);
BMenuItem* item = new BMenuItem(string.String(), message);
menu->AddItem(item);
if (sizes[i] == defaultSize) {
item->SetMarked(true);
found = true;
}
}
if (!found) {
for (uint32 i = 0; sizes[i]; i++) {
if (sizes[i] > defaultSize) {
BString string;
string << defaultSize;
BMessage* message = new BMessage(command);
message->AddInt32("font size", sizes[i]);
BMenuItem* item = new BMenuItem(string.String(), message);
item->SetMarked(true);
menu->AddItem(item, i);
break;
}
}
}
return menu;
}
void
TermWindow::_UpdateSwitchTerminalsMenuItem()
{

View File

@ -136,6 +136,8 @@ private:
void _SetupMenu();
static BMenu* _MakeEncodingMenu();
static BMenu* _MakeWindowSizeMenu();
static BMenu* _MakeFontSizeMenu(uint32 command,
uint8 defaultSize);
void _UpdateSwitchTerminalsMenuItem();
status_t _GetWindowPositionFile(BFile* file,
@ -198,6 +200,7 @@ private:
BMenuBar* fMenuBar;
BMenuItem* fSwitchTerminalsMenuItem;
BMenu* fEncodingMenu;
BMenu* fFontSizeMenu;
BMessage* fPrintSettings;
PrefWindow* fPrefWindow;
@ -212,7 +215,7 @@ private:
// Saved search parameters
BString fFindString;
BMenuItem* fFindNextMenuItem;
BMenuItem * fFindPreviousMenuItem;
BMenuItem* fFindPreviousMenuItem;
BMenuItem* fIncreaseFontSizeMenuItem;
BMenuItem* fDecreaseFontSizeMenuItem;