The stored (and default) font settings are now tested against the fonts installed

in the system; if a font chosen could not be found, a fallback font (be_fixed_font)
is used.
Cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14603 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-10-31 23:24:17 +00:00
parent 9b1ad6f45e
commit 11516cee1e
3 changed files with 199 additions and 181 deletions

View File

@ -32,13 +32,14 @@
#include "PrefHandler.h"
#include "TermConst.h"
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <Font.h>
#include <GraphicsDefs.h>
#include <Message.h>
#include <File.h>
#include <Entry.h>
#include <NodeInfo.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <Path.h>
#include <stdio.h>
@ -48,8 +49,8 @@
/*
* Startup preference settings.
*/
const prefDefaults termDefaults[] ={
*/
static const prefDefaults kTermDefaults[] = {
{ PREF_COLS, "80" },
{ PREF_ROWS, "25" },
@ -87,12 +88,15 @@ const prefDefaults termDefaults[] ={
PrefHandler::PrefHandler()
:
fContainer('Pref')
{
fContainer.what = 'Pref';
BPath path;
GetDefaultPath(path);
OpenText(path.Path(), termDefaults);
OpenText(path.Path(), kTermDefaults);
_ConfirmFont(PREF_HALF_FONT_FAMILY, be_fixed_font);
_ConfirmFont(PREF_FULL_FONT_FAMILY, be_fixed_font);
}
@ -130,9 +134,9 @@ PrefHandler::Open(const char *path, const prefDefaults *defaults)
{
BEntry entry(path);
if (entry.Exists())
return loadFromFile(&entry);
return _LoadFromFile(&entry);
return loadFromDefault(defaults);
return _LoadFromDefault(defaults);
}
@ -141,9 +145,9 @@ PrefHandler::OpenText(const char *path, const prefDefaults *defaults)
{
BEntry entry(path);
if (entry.Exists())
return loadFromTextFile(path);
return _LoadFromTextFile(path);
return loadFromDefault(defaults);
return _LoadFromDefault(defaults);
}
@ -325,83 +329,116 @@ PrefHandler::IsEmpty() const
}
status_t
PrefHandler::loadFromFile(BEntry *ent)
void
PrefHandler::_ConfirmFont(const char *key, const BFont *fallback)
{
// Future: It would be nice if we could simply use a flatened BMessage to
// save the settings. (Who cares about compatibility in this case anyway?)
int32 count = count_font_families();
const char *font = getString(key);
if (font == NULL)
count = 0;
BFile file (ent, B_READ_ONLY);
//fContainer.MakeEmpty();
//fContainer.Unflatten(&file);
off_t size;
if (file.GetSize(&size) != B_OK || size != sizeof(struct termprefs))
return B_ERROR;
font_family family;
struct termprefs prefs;
file.Read(&prefs, size);
if (prefs.magic != TP_MAGIC || prefs.version != TP_VERSION)
return B_ERROR;
for (int32 i = 0; i < count; i++) {
if (get_font_family(i, &family) != B_OK)
continue;
//Valid settings file!
setInt32(PREF_COLS, prefs.cols);
setInt32(PREF_ROWS, prefs.rows);
setInt32(PREF_HALF_FONT_SIZE, prefs.font_size);
setInt32(PREF_FULL_FONT_SIZE, prefs.font_size);
char *font_family = strtok(prefs.font, "/");
char *font_style = strtok(NULL, "");
setString(PREF_FULL_FONT_FAMILY, font_family);
setString(PREF_FULL_FONT_STYLE, font_style);
setString(PREF_HALF_FONT_FAMILY, font_family);
setString(PREF_HALF_FONT_STYLE, font_style);
setRGB(PREF_TEXT_BACK_COLOR, prefs.bg);
setRGB(PREF_TEXT_FORE_COLOR, prefs.fg);
setRGB(PREF_CURSOR_BACK_COLOR, prefs.curbg);
setRGB(PREF_CURSOR_FORE_COLOR, prefs.curfg);
setRGB(PREF_SELECT_BACK_COLOR, prefs.selbg);
setRGB(PREF_SELECT_FORE_COLOR, prefs.selfg);
setString(PREF_TEXT_ENCODING, encoding_table[prefs.encoding].name);
return B_OK;
if (!strcmp(family, font)) {
// found font family: we can safely use this font
return;
}
}
// use fall-back font
fallback->GetFamilyAndStyle(&family, NULL);
setString(key, family);
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
status_t
PrefHandler::loadFromDefault(const prefDefaults* defaluts)
PrefHandler::_LoadFromFile(BEntry *entry)
{
if(defaluts == NULL) return B_ERROR;
while(defaluts->key){
this->setString(defaluts->key, defaluts->item);
++defaluts;
}
return B_OK;
// Future: It would be nice if we could simply use a flatened BMessage to
// save the settings. (Who cares about compatibility in this case anyway?)
BFile file(entry, B_READ_ONLY);
//fContainer.MakeEmpty();
//fContainer.Unflatten(&file);
off_t size;
if (file.GetSize(&size) != B_OK || size != sizeof(struct termprefs))
return B_ERROR;
struct termprefs prefs;
file.Read(&prefs, size);
if (prefs.magic != TP_MAGIC || prefs.version != TP_VERSION)
return B_ERROR;
// Valid settings file!
setInt32(PREF_COLS, prefs.cols);
setInt32(PREF_ROWS, prefs.rows);
setInt32(PREF_HALF_FONT_SIZE, prefs.font_size);
setInt32(PREF_FULL_FONT_SIZE, prefs.font_size);
char *font_family = strtok(prefs.font, "/");
char *font_style = strtok(NULL, "");
setString(PREF_FULL_FONT_FAMILY, font_family);
setString(PREF_FULL_FONT_STYLE, font_style);
setString(PREF_HALF_FONT_FAMILY, font_family);
setString(PREF_HALF_FONT_STYLE, font_style);
setRGB(PREF_TEXT_BACK_COLOR, prefs.bg);
setRGB(PREF_TEXT_FORE_COLOR, prefs.fg);
setRGB(PREF_CURSOR_BACK_COLOR, prefs.curbg);
setRGB(PREF_CURSOR_FORE_COLOR, prefs.curfg);
setRGB(PREF_SELECT_BACK_COLOR, prefs.selbg);
setRGB(PREF_SELECT_FORE_COLOR, prefs.selfg);
setString(PREF_TEXT_ENCODING, encoding_table[prefs.encoding].name);
return B_OK;
}
/////////////////////////////////////////////////////////////////////////////
//
// Text is "key","Content"
// Comment : Start with '#'
/////////////////////////////////////////////////////////////////////////////
status_t
PrefHandler::loadFromTextFile(const char * path)
PrefHandler::_LoadFromDefault(const prefDefaults* defaults)
{
char buf[1024];
char key[B_FIELD_NAME_LENGTH], data[512];
int n;
FILE *fp;
if (defaults == NULL)
return B_ERROR;
fp = fopen(path, "r");
while (defaults->key) {
setString(defaults->key, defaults->item);
++defaults;
}
while(fgets(buf, sizeof(buf), fp) != NULL){
if (*buf == '#') continue;
n = sscanf(buf, "%*[\"]%[^\"]%*[\"]%*[^\"]%*[\"]%[^\"]", key, data);
if (n == 2) this->setString(key, data);
}
fclose(fp);
return B_OK;
return B_OK;
}
/** Text is "key","Content"
* Comment : Start with '#'
*/
status_t
PrefHandler::_LoadFromTextFile(const char * path)
{
char buffer[1024];
char key[B_FIELD_NAME_LENGTH], data[512];
int n;
FILE *file;
file = fopen(path, "r");
if (file == NULL)
return B_ENTRY_NOT_FOUND;
while (fgets(buffer, sizeof(buffer), file) != NULL) {
if (*buffer == '#')
continue;
n = sscanf(buffer, "%*[\"]%[^\"]%*[\"]%*[^\"]%*[\"]%[^\"]", key, data);
if (n == 2)
setString(key, data);
}
fclose(file);
return B_OK;
}

View File

@ -102,9 +102,10 @@ class PrefHandler {
static status_t GetDefaultPath(BPath& path);
private:
status_t loadFromFile(BEntry *ent);
status_t loadFromDefault(const prefDefaults* defaluts = NULL);
status_t loadFromTextFile(const char * path);
void _ConfirmFont(const char *key, const BFont *fallback);
status_t _LoadFromFile(BEntry *ent);
status_t _LoadFromDefault(const prefDefaults* defaluts = NULL);
status_t _LoadFromTextFile(const char * path);
BMessage fContainer;
};

View File

@ -86,134 +86,114 @@ TermWindow::~TermWindow()
delete (fWindowUpdate);
}
/*
*
* PUBLIC MEMBER FUNCTIONS.
*
*/
////////////////////////////////////////////////////////////////////////////
// Init Window (void)
// Initialize Window object.
////////////////////////////////////////////////////////////////////////////
// #pragma mark - public methods
/** Initialize Window object. */
void
TermWindow::InitWindow(void)
{
// make menu bar
SetupMenu();
// Setup font.
// make menu bar
SetupMenu();
BFont halfFont;
BFont fullFont;
halfFont.SetFamilyAndStyle (gTermPref->getString(PREF_HALF_FONT_FAMILY),
NULL);
halfFont.SetSize (gTermPref->getFloat(PREF_HALF_FONT_SIZE));
halfFont.SetSpacing (B_FIXED_SPACING);
fullFont.SetFamilyAndStyle (gTermPref->getString(PREF_FULL_FONT_FAMILY),
NULL);
fullFont.SetSize (gTermPref->getFloat(PREF_FULL_FONT_SIZE));
fullFont.SetSpacing (B_FIXED_SPACING);
// Setup font.
// Make Terminal text view.
const char *family = gTermPref->getString(PREF_HALF_FONT_FAMILY);
BRect textframe = Bounds();
textframe.top = fMenubar->Bounds().bottom + 1.0;
BFont halfFont;
halfFont.SetFamilyAndStyle(family, NULL);
halfFont.SetSize(gTermPref->getFloat(PREF_HALF_FONT_SIZE));
halfFont.SetSpacing(B_FIXED_SPACING);
fCodeConv = new CodeConv ();
fTermView = new TermView(Bounds(), fCodeConv);
family = gTermPref->getString(PREF_FULL_FONT_FAMILY);
/*
* MuTerm has two views. BaseView is window base view.
* TermView is character Terminal view on BaseView. It has paste
* on BaseView shift as VIEW_OFFSET.
*/
fBaseView = new TermBaseView (textframe, fTermView);
BFont fullFont;
fullFont.SetFamilyAndStyle(family, NULL);
fullFont.SetSize(gTermPref->getFloat(PREF_FULL_FONT_SIZE));
fullFont.SetSpacing(B_FIXED_SPACING);
//
//Initialize TermView. (font, size and color)
//
fTermView->SetTermFont (&halfFont, &fullFont);
BRect r = fTermView->SetTermSize (gTermPref->getInt32 (PREF_ROWS),
gTermPref->getInt32 (PREF_COLS),
1);
// Make Terminal text view.
int width, height;
fTermView->GetFontSize (&width, &height);
SetSizeLimits (MIN_COLS * width, MAX_COLS * width,
MIN_COLS * height, MAX_COLS * height);
BRect textframe = Bounds();
textframe.top = fMenubar->Bounds().bottom + 1.0;
fCodeConv = new CodeConv();
fTermView = new TermView(Bounds(), fCodeConv);
fTermView->SetTermColor ();
fBaseView->SetViewColor (gTermPref->getRGB (PREF_TEXT_BACK_COLOR));
/*
* MuTerm has two views. BaseView is window base view.
* TermView is character Terminal view on BaseView. It has paste
* on BaseView shift as VIEW_OFFSET.
*/
fBaseView = new TermBaseView(textframe, fTermView);
// Add offset to baseview.
r.InsetBy (-VIEW_OFFSET, -VIEW_OFFSET);
// Initialize TermView. (font, size and color)
//
// Resize Window
//
ResizeTo (r.Width()+ B_V_SCROLL_BAR_WIDTH,
r.Height()+fMenubar->Bounds().Height());
fTermView->SetTermFont(&halfFont, &fullFont);
BRect rect = fTermView->SetTermSize(gTermPref->getInt32(PREF_ROWS),
gTermPref->getInt32(PREF_COLS), 1);
fBaseView-> ResizeTo (r.Width(), r.Height());
fBaseView->AddChild (fTermView);
fTermView->MoveBy (VIEW_OFFSET, VIEW_OFFSET);
//
// Make Scroll Bar.
//
BRect scr(0, 0,
B_V_SCROLL_BAR_WIDTH,
r.Height() - B_H_SCROLL_BAR_HEIGHT + 1);
int width, height;
scr.OffsetBy (r.Width() + 1 , fMenubar->Bounds().Height());
BScrollBar *scrbar =
new BScrollBar (scr, "scrollbar", fTermView, 0, 0, B_VERTICAL);
fTermView->GetFontSize(&width, &height);
SetSizeLimits(MIN_COLS * width, MAX_COLS * width,
MIN_COLS * height, MAX_COLS * height);
fTermView->SetScrollBar (scrbar);
fTermView->SetTermColor();
fBaseView->SetViewColor(gTermPref->getRGB(PREF_TEXT_BACK_COLOR));
this->AddChild (scrbar);
this->AddChild (fBaseView);
// Add offset to baseview.
rect.InsetBy(-VIEW_OFFSET, -VIEW_OFFSET);
// Resize Window
// scrview->SetTarget (fTermView);
// Set fEditmenu's target to fTermView. (Oh!...)
fEditmenu->SetTargetForItems(fTermView);
ResizeTo(rect.Width()+ B_V_SCROLL_BAR_WIDTH,
rect.Height() + fMenubar->Bounds().Height());
//
// Initialize TermParse
//
gNowCoding = longname2op(gTermPref->getString(PREF_TEXT_ENCODING));
fTermParse = new TermParse ();
fTermParse->InitPtyReader (this);
fTermParse->InitTermParse(fTermView, fCodeConv);
fBaseView->ResizeTo(rect.Width(), rect.Height());
fBaseView->AddChild(fTermView);
fTermView->MoveBy(VIEW_OFFSET, VIEW_OFFSET);
// Set Coding.
// Make Scroll Bar.
// Initialize MessageRunner.
fWindowUpdate = new BMessageRunner (BMessenger (this),
new BMessage (MSGRUN_WINDOW),
500000);
return;
BRect scrollRect(0, 0, B_V_SCROLL_BAR_WIDTH,
rect.Height() - B_H_SCROLL_BAR_HEIGHT + 1);
scrollRect.OffsetBy(rect.Width() + 1, fMenubar->Bounds().Height());
BScrollBar *scrollBar = new BScrollBar(scrollRect, "scrollbar",
fTermView, 0, 0, B_VERTICAL);
fTermView->SetScrollBar(scrollBar);
AddChild(scrollBar);
AddChild(fBaseView);
// Set fEditmenu's target to fTermView. (Oh!...)
fEditmenu->SetTargetForItems(fTermView);
// Initialize TermParse
gNowCoding = longname2op(gTermPref->getString(PREF_TEXT_ENCODING));
fTermParse = new TermParse();
fTermParse->InitPtyReader(this);
fTermParse->InitTermParse(fTermView, fCodeConv);
// Set Coding.
// Initialize MessageRunner.
fWindowUpdate = new BMessageRunner(BMessenger(this),
new BMessage (MSGRUN_WINDOW), 500000);
}
////////////////////////////////////////////////////////////////////////////
// void MenusBeginning (void)
// Dispatch MenuBegininng
////////////////////////////////////////////////////////////////////////////
void
TermWindow::MenusBeginning(void)
{
// Syncronize Encode Menu Pop-up menu and Preference.
(fEncodingmenu->FindItem(op2longname(gNowCoding)))->SetMarked(true);
BWindow::MenusBeginning();
// Syncronize Encode Menu Pop-up menu and Preference.
(fEncodingmenu->FindItem(op2longname(gNowCoding)))->SetMarked(true);
BWindow::MenusBeginning();
}