diff --git a/src/GUI/new_gui.cxx b/src/GUI/new_gui.cxx index 4b85d0df2..a7b1ddb2e 100644 --- a/src/GUI/new_gui.cxx +++ b/src/GUI/new_gui.cxx @@ -13,8 +13,6 @@ #include "menubar.hxx" #include "dialog.hxx" -SG_USING_STD(map); - extern puFont FONT_HELVETICA_14; extern puFont FONT_SANS_12B; @@ -183,6 +181,7 @@ NewGUI::clear () delete _menubar; _menubar = 0; _dialog_props.clear(); + _colors.clear(); } static bool @@ -269,12 +268,12 @@ NewGUI::setStyle (void) _colors.clear(); // set up the traditional colors as default - _colors["background"] = FGColor(0.8f, 0.8f, 0.9f, 0.85f); - _colors["foreground"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f); - _colors["highlight"] = FGColor(0.7f, 0.7f, 0.7f, 1.0f); - _colors["label"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f); - _colors["legend"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f); - _colors["misc"] = FGColor(0.0f, 0.0f, 0.0f, 1.0f); + _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); //puSetDefaultStyle(); @@ -289,11 +288,11 @@ NewGUI::setStyle (void) for (int i = 0; i < n->nChildren(); i++) { SGPropertyNode *child = n->getChild(i); - _colors[child->getName()] = FGColor(child); + _colors[child->getName()] = new FGColor(child); } - FGColor c = _colors["background"]; - puSetDefaultColourScheme(c.red(), c.green(), c.blue(), c.alpha()); + FGColor *c = _colors["background"]; + puSetDefaultColourScheme(c->red(), c->green(), c->blue(), c->alpha()); } @@ -394,4 +393,67 @@ FGColor::merge(const FGColor& color) return dirty; } + +// +FGFontCache::FGFontCache() +{ + char *envp = ::getenv("FG_FONTS"); + if (envp != NULL) { + _path.set(envp); + } else { + _path.set(globals->get_fg_root()); + _path.append("Fonts"); + } + + for (int i=0; guifonts[i].name; i++) + _fonts[guifonts[i].name] = guifonts[i].font; +} + +FGFontCache::~FGFontCache() +{ + _fonts.clear(); +} + +puFont * +FGFontCache::get(const char *name, float size, float slant) +{ + puFont *font; + _itt_t it; + + if ((it = _fonts.find(name)) == _fonts.end()) + { + SGPath path(_path); + path.append(name); + + fntTexFont tex_font; + if (tex_font.load((char *)path.c_str())) + { + font = new puFont; + font->initialize((fntFont *)&tex_font, size, slant); + _fonts[name] = font; + } + else + { + font = _fonts["default"]; + // puSetDefaultFonts(font, font); + } + } + else + { + font = it->second; + } + + return font; +} + +puFont * +FGFontCache::get(SGPropertyNode *node) +{ + 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); +} + // end of new_gui.cxx diff --git a/src/GUI/new_gui.hxx b/src/GUI/new_gui.hxx index 15de193d8..dbe1904fc 100644 --- a/src/GUI/new_gui.hxx +++ b/src/GUI/new_gui.hxx @@ -16,12 +16,14 @@ #include // for SG_USING_STD #include #include +#include #include -SG_USING_STD(vector); - #include + +SG_USING_STD(vector); SG_USING_STD(map); +SG_USING_STD(string); #include
@@ -29,6 +31,7 @@ class FGMenuBar; class FGDialog; class FGBinding; class FGColor; +class FGFontCache; /** @@ -151,13 +154,27 @@ public: */ virtual FGDialog * getActiveDialog (); - virtual const FGColor& getColor (const char * which) { return _colors[which]; } - virtual const FGColor& getColor (string which) { return _colors[which.c_str()]; } + virtual FGColor *getColor (const char * name) const { + _itt_t it = _colors.find(name); + return it->second; + } + virtual FGColor *getColor (const string &name) const { + _itt_t it = _colors.find(name.c_str()); + return it->second; + } virtual puFont *getDefaultFont() { return &_font; } + + /** + * menu wide font cache, accessible from other classes as well. + */ + FGFontCache *get_fontcache() { return _fontcache; } + protected: + FGFontCache * _fontcache; + /** * Test if the menubar is visible. * @@ -178,7 +195,8 @@ protected: private: fntTexFont _tex_font; puFont _font; - map _colors; + map _colors; + typedef map::const_iterator _itt_t; // Free all allocated memory. void clear (); @@ -199,14 +217,7 @@ public: FGColor() { clear(); } FGColor(float r, float g, float b, float a = 1.0f) { set(r, g, b, a); } FGColor(const SGPropertyNode *prop) { set(prop); } - - FGColor& operator=(const FGColor& c) { - _red = c._red; - _green = c._green; - _blue = c._blue; - _alpha = c._alpha; - return *this; - } + FGColor(FGColor *c) { set(c->_red, c->_green, c->_blue, c->_alpha); } inline void clear() { _red = _green = _blue = _alpha = -1.0f; } // merges in non-negative components from property with children etc. @@ -245,5 +256,26 @@ private: }; + +/** + * A small class to keep all fonts available for future use. + * This also assures a font isn't resident more than once. + */ +class FGFontCache { +private: + SGPath _path; + + map _fonts; + typedef map::iterator _itt_t; + +public: + FGFontCache(); + ~FGFontCache(); + + puFont *get(const char *name, float size=15.0, float slant=0.0); + puFont *get(SGPropertyNode *node); +}; + + #endif // __NEW_GUI_HXX