2002-11-07 16:27:47 +00:00
|
|
|
|
// new_gui.cxx: implementation of XML-configurable GUI support.
|
2010-09-30 17:58:01 +00:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
# include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "new_gui.hxx"
|
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
#include <algorithm>
|
2008-06-02 21:07:35 +00:00
|
|
|
|
#include <iostream>
|
2008-06-06 19:02:17 +00:00
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <sys/types.h>
|
2002-11-07 16:27:47 +00:00
|
|
|
|
|
|
|
|
|
#include <plib/pu.h>
|
|
|
|
|
|
2003-04-13 21:25:46 +00:00
|
|
|
|
#include <simgear/compiler.h>
|
2003-09-24 17:20:55 +00:00
|
|
|
|
#include <simgear/structure/exception.hxx>
|
2008-07-31 12:04:32 +00:00
|
|
|
|
#include <simgear/props/props_io.hxx>
|
2010-10-24 00:09:06 +00:00
|
|
|
|
#include <simgear/misc/sg_dir.hxx>
|
2003-09-24 17:20:55 +00:00
|
|
|
|
|
2010-02-10 19:28:35 +00:00
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
|
|
|
|
|
2002-11-07 16:27:47 +00:00
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
|
|
2011-10-01 07:50:34 +00:00
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#include "GL/glx.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-01-16 18:06:27 +00:00
|
|
|
|
#include "menubar.hxx"
|
2003-01-18 15:16:34 +00:00
|
|
|
|
#include "dialog.hxx"
|
2002-11-07 22:59:46 +00:00
|
|
|
|
|
2005-07-07 21:28:15 +00:00
|
|
|
|
extern puFont FONT_HELVETICA_14;
|
2005-07-08 13:28:40 +00:00
|
|
|
|
extern puFont FONT_SANS_12B;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-11-08 15:24:14 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of NewGUI.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
2005-07-07 21:28:15 +00:00
|
|
|
|
|
2002-11-08 15:24:14 +00:00
|
|
|
|
NewGUI::NewGUI ()
|
2006-06-05 20:23:56 +00:00
|
|
|
|
: _menubar(new FGMenuBar),
|
2003-01-20 16:03:09 +00:00
|
|
|
|
_active_dialog(0)
|
2002-11-08 15:24:14 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NewGUI::~NewGUI ()
|
|
|
|
|
{
|
2005-11-09 17:16:59 +00:00
|
|
|
|
delete _menubar;
|
|
|
|
|
_dialog_props.clear();
|
2007-10-20 08:36:21 +00:00
|
|
|
|
for (_itt_t it = _colors.begin(); it != _colors.end(); ++it)
|
|
|
|
|
delete it->second;
|
2002-11-08 15:24:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
NewGUI::init ()
|
|
|
|
|
{
|
2005-07-13 10:16:42 +00:00
|
|
|
|
setStyle();
|
2010-10-24 00:09:06 +00:00
|
|
|
|
SGPath p(globals->get_fg_root(), "gui/dialogs");
|
|
|
|
|
readDir(p);
|
2003-01-16 18:06:27 +00:00
|
|
|
|
_menubar->init();
|
2002-11-08 15:24:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-21 02:09:27 +00:00
|
|
|
|
void
|
|
|
|
|
NewGUI::reinit ()
|
2005-11-09 17:16:59 +00:00
|
|
|
|
{
|
|
|
|
|
reset(true);
|
2007-04-02 12:12:23 +00:00
|
|
|
|
fgSetBool("/sim/signals/reinit-gui", true);
|
2005-11-09 17:16:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
NewGUI::redraw ()
|
|
|
|
|
{
|
|
|
|
|
reset(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
NewGUI::reset (bool reload)
|
2003-01-21 02:09:27 +00:00
|
|
|
|
{
|
2005-07-07 21:28:15 +00:00
|
|
|
|
map<string,FGDialog *>::iterator iter;
|
|
|
|
|
vector<string> dlg;
|
|
|
|
|
// close all open dialogs and remember them ...
|
2005-11-08 18:18:55 +00:00
|
|
|
|
for (iter = _active_dialogs.begin(); iter != _active_dialogs.end(); ++iter)
|
2005-07-07 21:28:15 +00:00
|
|
|
|
dlg.push_back(iter->first);
|
2005-11-08 18:18:55 +00:00
|
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
|
for (i = 0; i < dlg.size(); i++)
|
|
|
|
|
closeDialog(dlg[i]);
|
2005-07-07 21:28:15 +00:00
|
|
|
|
|
|
|
|
|
setStyle();
|
2005-11-09 17:16:59 +00:00
|
|
|
|
|
|
|
|
|
unbind();
|
2005-11-08 18:18:55 +00:00
|
|
|
|
delete _menubar;
|
2003-01-21 02:09:27 +00:00
|
|
|
|
_menubar = new FGMenuBar;
|
2005-11-09 17:16:59 +00:00
|
|
|
|
|
|
|
|
|
if (reload) {
|
|
|
|
|
_dialog_props.clear();
|
|
|
|
|
init();
|
|
|
|
|
} else {
|
|
|
|
|
_menubar->init();
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-21 02:09:27 +00:00
|
|
|
|
bind();
|
2005-07-07 21:28:15 +00:00
|
|
|
|
|
2005-11-09 17:16:59 +00:00
|
|
|
|
// open dialogs again
|
2005-11-08 18:18:55 +00:00
|
|
|
|
for (i = 0; i < dlg.size(); i++)
|
|
|
|
|
showDialog(dlg[i]);
|
2003-01-21 02:09:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-18 21:11:51 +00:00
|
|
|
|
void
|
|
|
|
|
NewGUI::bind ()
|
|
|
|
|
{
|
|
|
|
|
fgTie("/sim/menubar/visibility", this,
|
|
|
|
|
&NewGUI::getMenuBarVisible, &NewGUI::setMenuBarVisible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
NewGUI::unbind ()
|
|
|
|
|
{
|
|
|
|
|
fgUntie("/sim/menubar/visibility");
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-08 15:24:14 +00:00
|
|
|
|
void
|
|
|
|
|
NewGUI::update (double delta_time_sec)
|
|
|
|
|
{
|
2004-05-14 17:16:35 +00:00
|
|
|
|
map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
|
|
|
|
|
for(/**/; iter != _active_dialogs.end(); iter++)
|
|
|
|
|
iter->second->update();
|
2002-11-08 15:24:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-20 16:03:09 +00:00
|
|
|
|
bool
|
|
|
|
|
NewGUI::showDialog (const string &name)
|
2002-11-08 15:24:14 +00:00
|
|
|
|
{
|
2003-01-20 16:03:09 +00:00
|
|
|
|
if (_dialog_props.find(name) == _dialog_props.end()) {
|
2002-11-08 15:24:14 +00:00
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
|
2003-01-20 16:03:09 +00:00
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2003-12-08 02:05:10 +00:00
|
|
|
|
if(!_active_dialogs[name])
|
|
|
|
|
_active_dialogs[name] = new FGDialog(_dialog_props[name]);
|
2003-01-20 16:03:09 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
NewGUI::closeActiveDialog ()
|
|
|
|
|
{
|
2003-12-08 02:05:10 +00:00
|
|
|
|
if (_active_dialog == 0)
|
2003-01-20 16:03:09 +00:00
|
|
|
|
return false;
|
2003-12-08 02:05:10 +00:00
|
|
|
|
|
|
|
|
|
// Kill any entries in _active_dialogs... Is there an STL
|
|
|
|
|
// algorithm to do (delete map entries by value, not key)? I hate
|
|
|
|
|
// the STL :) -Andy
|
|
|
|
|
map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
|
2004-04-05 11:29:12 +00:00
|
|
|
|
for(/**/; iter != _active_dialogs.end(); iter++) {
|
|
|
|
|
if(iter->second == _active_dialog) {
|
2003-12-08 02:05:10 +00:00
|
|
|
|
_active_dialogs.erase(iter);
|
2004-04-05 11:29:12 +00:00
|
|
|
|
// iter is no longer valid
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-12-08 02:05:10 +00:00
|
|
|
|
|
|
|
|
|
delete _active_dialog;
|
|
|
|
|
_active_dialog = 0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
NewGUI::closeDialog (const string& name)
|
|
|
|
|
{
|
|
|
|
|
if(_active_dialogs.find(name) != _active_dialogs.end()) {
|
|
|
|
|
if(_active_dialog == _active_dialogs[name])
|
|
|
|
|
_active_dialog = 0;
|
|
|
|
|
delete _active_dialogs[name];
|
|
|
|
|
_active_dialogs.erase(name);
|
2003-01-20 16:03:09 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2003-12-08 02:05:10 +00:00
|
|
|
|
return false; // dialog wasn't open...
|
2002-12-22 19:52:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-10-20 11:15:00 +00:00
|
|
|
|
SGPropertyNode_ptr
|
2006-04-28 09:59:31 +00:00
|
|
|
|
NewGUI::getDialogProperties (const string &name)
|
2005-10-20 11:15:00 +00:00
|
|
|
|
{
|
|
|
|
|
if(_dialog_props.find(name) != _dialog_props.end())
|
|
|
|
|
return _dialog_props[name];
|
2005-10-22 16:51:27 +00:00
|
|
|
|
|
2006-04-28 10:27:54 +00:00
|
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGDialog *
|
|
|
|
|
NewGUI::getDialog (const string &name)
|
|
|
|
|
{
|
|
|
|
|
if(_active_dialogs.find(name) != _active_dialogs.end())
|
|
|
|
|
return _active_dialogs[name];
|
|
|
|
|
|
|
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "dialog '" << name << "' missing");
|
2005-10-22 16:51:27 +00:00
|
|
|
|
return 0;
|
2005-10-20 11:15:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-12-22 19:52:26 +00:00
|
|
|
|
void
|
2003-01-20 16:03:09 +00:00
|
|
|
|
NewGUI::setActiveDialog (FGDialog * dialog)
|
2002-12-22 19:52:26 +00:00
|
|
|
|
{
|
2003-01-20 16:03:09 +00:00
|
|
|
|
_active_dialog = dialog;
|
2002-12-22 19:52:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-18 15:16:34 +00:00
|
|
|
|
FGDialog *
|
2003-01-20 16:03:09 +00:00
|
|
|
|
NewGUI::getActiveDialog ()
|
2002-12-22 19:52:26 +00:00
|
|
|
|
{
|
2003-01-20 16:03:09 +00:00
|
|
|
|
return _active_dialog;
|
2002-11-08 15:24:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-16 18:06:27 +00:00
|
|
|
|
FGMenuBar *
|
|
|
|
|
NewGUI::getMenuBar ()
|
|
|
|
|
{
|
|
|
|
|
return _menubar;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-18 21:11:51 +00:00
|
|
|
|
bool
|
|
|
|
|
NewGUI::getMenuBarVisible () const
|
|
|
|
|
{
|
|
|
|
|
return _menubar->isVisible();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
NewGUI::setMenuBarVisible (bool visible)
|
|
|
|
|
{
|
|
|
|
|
if (visible)
|
|
|
|
|
_menubar->show();
|
|
|
|
|
else
|
|
|
|
|
_menubar->hide();
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-08 02:05:10 +00:00
|
|
|
|
void
|
|
|
|
|
NewGUI::newDialog (SGPropertyNode* props)
|
|
|
|
|
{
|
|
|
|
|
const char* cname = props->getStringValue("name");
|
|
|
|
|
if(!cname) {
|
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-03-29 08:35:13 +00:00
|
|
|
|
string name = cname;
|
2005-11-08 11:05:50 +00:00
|
|
|
|
if(_active_dialogs.find(name) == _active_dialogs.end())
|
2005-03-29 08:35:13 +00:00
|
|
|
|
_dialog_props[name] = props;
|
2003-12-08 02:05:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-08 15:24:14 +00:00
|
|
|
|
void
|
2010-10-24 00:09:06 +00:00
|
|
|
|
NewGUI::readDir (const SGPath& path)
|
2002-11-08 15:24:14 +00:00
|
|
|
|
{
|
2010-10-24 00:09:06 +00:00
|
|
|
|
simgear::Dir dir(path);
|
|
|
|
|
simgear::PathList xmls = dir.children(simgear::Dir::TYPE_FILE, ".xml");
|
|
|
|
|
|
|
|
|
|
for (unsigned int i=0; i<xmls.size(); ++i) {
|
|
|
|
|
SGPropertyNode * props = new SGPropertyNode;
|
|
|
|
|
try {
|
|
|
|
|
readProperties(xmls[i].str(), props);
|
|
|
|
|
} catch (const sg_exception &) {
|
|
|
|
|
SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
|
|
|
|
|
<< xmls[i].str());
|
|
|
|
|
delete props;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
SGPropertyNode *nameprop = props->getNode("name");
|
|
|
|
|
if (!nameprop) {
|
|
|
|
|
SG_LOG(SG_INPUT, SG_WARN, "dialog " << xmls[i].str()
|
|
|
|
|
<< " has no name; skipping.");
|
|
|
|
|
delete props;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
string name = nameprop->getStringValue();
|
|
|
|
|
_dialog_props[name] = props;
|
2002-11-08 15:24:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-07 21:28:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Style handling.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
NewGUI::setStyle (void)
|
|
|
|
|
{
|
2005-10-14 16:25:14 +00:00
|
|
|
|
_itt_t it;
|
|
|
|
|
for (it = _colors.begin(); it != _colors.end(); ++it)
|
|
|
|
|
delete it->second;
|
2005-07-13 10:16:42 +00:00
|
|
|
|
_colors.clear();
|
|
|
|
|
|
|
|
|
|
// set up the traditional colors as default
|
2005-07-18 09:18:24 +00:00
|
|
|
|
_colors["background"] = new FGColor(0.8f, 0.8f, 0.9f, 0.85f);
|
|
|
|
|
_colors["foreground"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
_colors["highlight"] = new FGColor(0.7f, 0.7f, 0.7f, 1.0f);
|
|
|
|
|
_colors["label"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
_colors["legend"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
_colors["misc"] = new FGColor(0.0f, 0.0f, 0.0f, 1.0f);
|
2005-11-09 17:16:59 +00:00
|
|
|
|
_colors["inputfield"] = new FGColor(0.8f, 0.7f, 0.7f, 1.0f);
|
2005-07-07 21:28:15 +00:00
|
|
|
|
|
|
|
|
|
//puSetDefaultStyle();
|
|
|
|
|
|
2006-03-09 23:04:41 +00:00
|
|
|
|
int which = fgGetInt("/sim/gui/current-style", 0);
|
2006-03-19 07:10:10 +00:00
|
|
|
|
SGPropertyNode *sim = globals->get_props()->getNode("sim/gui", true);
|
2006-03-09 23:04:41 +00:00
|
|
|
|
SGPropertyNode *n = sim->getChild("style", which);
|
2005-07-13 10:57:27 +00:00
|
|
|
|
if (!n)
|
2006-03-09 23:04:41 +00:00
|
|
|
|
n = sim->getChild("style", 0, true);
|
2005-07-13 10:57:27 +00:00
|
|
|
|
|
2006-02-04 13:06:47 +00:00
|
|
|
|
setupFont(n->getNode("fonts/gui", true));
|
2005-07-13 10:57:27 +00:00
|
|
|
|
n = n->getNode("colors", true);
|
2005-07-07 21:28:15 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < n->nChildren(); i++) {
|
|
|
|
|
SGPropertyNode *child = n->getChild(i);
|
2005-07-18 09:18:24 +00:00
|
|
|
|
_colors[child->getName()] = new FGColor(child);
|
2005-07-07 21:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-18 09:18:24 +00:00
|
|
|
|
FGColor *c = _colors["background"];
|
|
|
|
|
puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha());
|
2005-07-07 21:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-07-13 10:16:42 +00:00
|
|
|
|
NewGUI::setupFont (SGPropertyNode *node)
|
2005-07-07 21:28:15 +00:00
|
|
|
|
{
|
2006-06-05 20:23:56 +00:00
|
|
|
|
_font = globals->get_fontcache()->get(node);
|
2006-01-26 21:40:37 +00:00
|
|
|
|
puSetDefaultFonts(*_font, *_font);
|
|
|
|
|
return;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// FGColor class.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2008-06-02 21:07:35 +00:00
|
|
|
|
void
|
|
|
|
|
FGColor::print() const {
|
|
|
|
|
std::cerr << "red=" << _red << ", green=" << _green
|
|
|
|
|
<< ", blue=" << _blue << ", alpha=" << _alpha << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-11 08:01:28 +00:00
|
|
|
|
bool
|
2005-07-07 21:28:15 +00:00
|
|
|
|
FGColor::merge(const SGPropertyNode *node)
|
|
|
|
|
{
|
|
|
|
|
if (!node)
|
2005-07-11 08:01:28 +00:00
|
|
|
|
return false;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
|
2005-07-11 08:01:28 +00:00
|
|
|
|
bool dirty = false;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
const SGPropertyNode * n;
|
|
|
|
|
if ((n = node->getNode("red")))
|
2005-07-11 08:01:28 +00:00
|
|
|
|
_red = n->getFloatValue(), dirty = true;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
if ((n = node->getNode("green")))
|
2005-07-11 08:01:28 +00:00
|
|
|
|
_green = n->getFloatValue(), dirty = true;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
if ((n = node->getNode("blue")))
|
2005-07-11 08:01:28 +00:00
|
|
|
|
_blue = n->getFloatValue(), dirty = true;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
if ((n = node->getNode("alpha")))
|
2005-07-11 08:01:28 +00:00
|
|
|
|
_alpha = n->getFloatValue(), dirty = true;
|
|
|
|
|
return dirty;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-11 08:01:28 +00:00
|
|
|
|
bool
|
2005-07-18 10:00:02 +00:00
|
|
|
|
FGColor::merge(const FGColor *color)
|
2005-07-07 21:28:15 +00:00
|
|
|
|
{
|
2005-07-11 08:01:28 +00:00
|
|
|
|
bool dirty = false;
|
2005-07-18 10:00:02 +00:00
|
|
|
|
if (color && color->_red >= 0.0)
|
|
|
|
|
_red = color->_red, dirty = true;
|
|
|
|
|
if (color && color->_green >= 0.0)
|
|
|
|
|
_green = color->_green, dirty = true;
|
|
|
|
|
if (color && color->_blue >= 0.0)
|
|
|
|
|
_blue = color->_blue, dirty = true;
|
|
|
|
|
if (color && color->_alpha >= 0.0)
|
|
|
|
|
_alpha = color->_alpha, dirty = true;
|
2005-07-11 08:01:28 +00:00
|
|
|
|
return dirty;
|
2005-07-07 21:28:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-18 09:18:24 +00:00
|
|
|
|
|
2006-06-06 12:49:42 +00:00
|
|
|
|
|
2006-06-05 20:23:56 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// FGFontCache class.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2005-07-18 09:18:24 +00:00
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
struct GuiFont
|
|
|
|
|
{
|
2008-03-11 15:58:57 +00:00
|
|
|
|
const char *name;
|
2006-06-05 20:23:56 +00:00
|
|
|
|
puFont *font;
|
2008-06-06 19:02:17 +00:00
|
|
|
|
struct Predicate
|
|
|
|
|
: public std::unary_function<const GuiFont, bool>
|
|
|
|
|
{
|
|
|
|
|
Predicate(const char* name_) : name(name_) {}
|
|
|
|
|
bool operator() (const GuiFont& f1) const
|
|
|
|
|
{
|
|
|
|
|
return std::strcmp(f1.name, name) == 0;
|
|
|
|
|
}
|
|
|
|
|
const char* name;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const GuiFont guifonts[] = {
|
2006-06-05 20:23:56 +00:00
|
|
|
|
{ "default", &FONT_HELVETICA_14 },
|
|
|
|
|
{ "FIXED_8x13", &PUFONT_8_BY_13 },
|
|
|
|
|
{ "FIXED_9x15", &PUFONT_9_BY_15 },
|
|
|
|
|
{ "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
|
|
|
|
|
{ "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
|
|
|
|
|
{ "HELVETICA_10", &PUFONT_HELVETICA_10 },
|
|
|
|
|
{ "HELVETICA_12", &PUFONT_HELVETICA_12 },
|
|
|
|
|
{ "HELVETICA_14", &FONT_HELVETICA_14 },
|
|
|
|
|
{ "HELVETICA_18", &PUFONT_HELVETICA_18 },
|
2008-06-06 19:02:17 +00:00
|
|
|
|
{ "SANS_12B", &FONT_SANS_12B }
|
2006-06-05 20:23:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
|
|
|
|
|
}
|
2006-06-05 20:23:56 +00:00
|
|
|
|
|
|
|
|
|
FGFontCache::FGFontCache() :
|
|
|
|
|
_initialized(false)
|
|
|
|
|
{
|
2005-07-18 09:18:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FGFontCache::~FGFontCache()
|
|
|
|
|
{
|
2011-10-01 07:50:34 +00:00
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
// Ugly workaround for a crash on exit with multiple screens configured
|
|
|
|
|
if (!glXGetCurrentContext())
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
2008-06-06 19:02:17 +00:00
|
|
|
|
PuFontMap::iterator it, end = _puFonts.end();
|
|
|
|
|
for (it = _puFonts.begin(); it != end; ++it)
|
2006-06-21 21:17:21 +00:00
|
|
|
|
delete it->second;
|
2005-07-18 09:18:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
|
|
|
|
|
const FntParams& f2) const
|
|
|
|
|
{
|
|
|
|
|
int comp = f1.name.compare(f2.name);
|
|
|
|
|
if (comp < 0)
|
|
|
|
|
return true;
|
|
|
|
|
else if (comp > 0)
|
|
|
|
|
return false;
|
|
|
|
|
if (f1.size < f2.size)
|
|
|
|
|
return true;
|
|
|
|
|
else if (f1.size > f2.size)
|
|
|
|
|
return false;
|
|
|
|
|
return f1.slant < f2.slant;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-06 12:49:42 +00:00
|
|
|
|
struct FGFontCache::fnt *
|
|
|
|
|
FGFontCache::getfnt(const char *name, float size, float slant)
|
2005-07-18 09:18:24 +00:00
|
|
|
|
{
|
2010-02-10 19:28:35 +00:00
|
|
|
|
string fontName = boost::to_lower_copy(string(name));
|
2008-06-06 19:02:17 +00:00
|
|
|
|
FntParams fntParams(fontName, size, slant);
|
|
|
|
|
PuFontMap::iterator i = _puFonts.find(fntParams);
|
2010-02-10 19:28:35 +00:00
|
|
|
|
if (i != _puFonts.end()) {
|
|
|
|
|
// found in the puFonts map, all done
|
2008-06-06 19:02:17 +00:00
|
|
|
|
return i->second;
|
2010-02-10 19:28:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
// fntTexFont s are all preloaded into the _texFonts map
|
|
|
|
|
TexFontMap::iterator texi = _texFonts.find(fontName);
|
2010-02-10 19:28:35 +00:00
|
|
|
|
fntTexFont* texfont = NULL;
|
|
|
|
|
puFont* pufont = NULL;
|
2008-06-06 19:02:17 +00:00
|
|
|
|
if (texi != _texFonts.end()) {
|
|
|
|
|
texfont = texi->second;
|
|
|
|
|
} else {
|
2010-02-10 19:28:35 +00:00
|
|
|
|
// check the built-in PUI fonts (in guifonts array)
|
2008-06-06 19:02:17 +00:00
|
|
|
|
const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
|
|
|
|
|
GuiFont::Predicate(name));
|
|
|
|
|
if (guifont != guifontsEnd) {
|
|
|
|
|
pufont = guifont->font;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-10 19:28:35 +00:00
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
fnt* f = new fnt;
|
|
|
|
|
if (pufont) {
|
|
|
|
|
f->pufont = pufont;
|
|
|
|
|
} else if (texfont) {
|
|
|
|
|
f->texfont = texfont;
|
2006-01-26 22:22:37 +00:00
|
|
|
|
f->pufont = new puFont;
|
|
|
|
|
f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
|
2008-06-06 19:02:17 +00:00
|
|
|
|
} else {
|
|
|
|
|
f->pufont = guifonts[0].font;
|
2006-01-26 21:40:37 +00:00
|
|
|
|
}
|
2008-06-06 19:02:17 +00:00
|
|
|
|
_puFonts[fntParams] = f;
|
|
|
|
|
return f;
|
2006-06-06 12:49:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
puFont *
|
|
|
|
|
FGFontCache::get(const char *name, float size, float slant)
|
|
|
|
|
{
|
|
|
|
|
return getfnt(name, size, slant)->pufont;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fntTexFont *
|
|
|
|
|
FGFontCache::getTexFont(const char *name, float size, float slant)
|
|
|
|
|
{
|
|
|
|
|
return getfnt(name, size, slant)->texfont;
|
2005-07-18 09:18:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
puFont *
|
|
|
|
|
FGFontCache::get(SGPropertyNode *node)
|
|
|
|
|
{
|
2006-06-05 21:55:18 +00:00
|
|
|
|
if (!node)
|
|
|
|
|
return get("Helvetica.txf", 15.0, 0.0);
|
|
|
|
|
|
2005-07-18 09:18:24 +00:00
|
|
|
|
const char *name = node->getStringValue("name", "Helvetica.txf");
|
|
|
|
|
float size = node->getFloatValue("size", 15.0);
|
|
|
|
|
float slant = node->getFloatValue("slant", 0.0);
|
|
|
|
|
|
|
|
|
|
return get(name, size, slant);
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
void FGFontCache::init()
|
2007-05-03 18:12:29 +00:00
|
|
|
|
{
|
2010-02-10 19:28:35 +00:00
|
|
|
|
if (_initialized) {
|
|
|
|
|
return;
|
2007-05-03 18:12:29 +00:00
|
|
|
|
}
|
2010-02-10 19:28:35 +00:00
|
|
|
|
|
|
|
|
|
char *envp = ::getenv("FG_FONTS");
|
|
|
|
|
if (envp != NULL) {
|
|
|
|
|
_path.set(envp);
|
|
|
|
|
} else {
|
|
|
|
|
_path.set(globals->get_fg_root());
|
|
|
|
|
_path.append("Fonts");
|
|
|
|
|
}
|
|
|
|
|
_initialized = true;
|
2008-06-06 19:02:17 +00:00
|
|
|
|
}
|
2007-05-03 18:12:29 +00:00
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
SGPath
|
|
|
|
|
FGFontCache::getfntpath(const char *name)
|
|
|
|
|
{
|
|
|
|
|
init();
|
2007-05-03 18:12:29 +00:00
|
|
|
|
SGPath path(_path);
|
|
|
|
|
if (name && std::string(name) != "") {
|
|
|
|
|
path.append(name);
|
|
|
|
|
if (path.exists())
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path = SGPath(_path);
|
|
|
|
|
path.append("Helvetica.txf");
|
2010-02-10 19:28:35 +00:00
|
|
|
|
SG_LOG(SG_GENERAL, SG_WARN, "Unknown font name '" << name << "', defaulting to Helvetica");
|
2007-05-03 18:12:29 +00:00
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-06 19:02:17 +00:00
|
|
|
|
bool FGFontCache::initializeFonts()
|
|
|
|
|
{
|
|
|
|
|
static string fontext("txf");
|
|
|
|
|
init();
|
2008-06-07 08:10:46 +00:00
|
|
|
|
ulDir* fontdir = ulOpenDir(_path.c_str());
|
2008-06-06 19:02:17 +00:00
|
|
|
|
if (!fontdir)
|
|
|
|
|
return false;
|
2008-06-07 08:10:46 +00:00
|
|
|
|
const ulDirEnt *dirEntry;
|
|
|
|
|
while ((dirEntry = ulReadDir(fontdir)) != 0) {
|
2008-06-06 19:02:17 +00:00
|
|
|
|
SGPath path(_path);
|
|
|
|
|
path.append(dirEntry->d_name);
|
|
|
|
|
if (path.extension() == fontext) {
|
|
|
|
|
fntTexFont* f = new fntTexFont;
|
2010-02-10 19:28:35 +00:00
|
|
|
|
if (f->load((char *)path.c_str())) {
|
|
|
|
|
// convert font names in the map to lowercase for matching
|
|
|
|
|
string fontName = boost::to_lower_copy(string(dirEntry->d_name));
|
|
|
|
|
_texFonts[fontName] = f;
|
|
|
|
|
} else
|
2008-06-06 19:02:17 +00:00
|
|
|
|
delete f;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-06-07 08:10:46 +00:00
|
|
|
|
ulCloseDir(fontdir);
|
2008-06-06 19:02:17 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-07 16:27:47 +00:00
|
|
|
|
// end of new_gui.cxx
|