From ac4420cdc50b7d7410781bc1bbbd1e89e18f256d Mon Sep 17 00:00:00 2001 From: fluxgen Date: Tue, 26 Nov 2002 16:01:28 +0000 Subject: initial import --- src/FbTk/App.cc | 51 ++++++ src/FbTk/App.hh | 50 ++++++ src/FbTk/Color.cc | 166 ++++++++++++++++++++ src/FbTk/Color.hh | 73 +++++++++ src/FbTk/EventHandler.hh | 43 ++++++ src/FbTk/Font.cc | 184 ++++++++++++++++++++++ src/FbTk/Font.hh | 84 ++++++++++ src/FbTk/FontImp.hh | 57 +++++++ src/FbTk/Makefile.am | 14 ++ src/FbTk/NotCopyable.hh | 42 +++++ src/FbTk/SignalHandler.cc | 81 ++++++++++ src/FbTk/SignalHandler.hh | 71 +++++++++ src/FbTk/Texture.cc | 99 ++++++++++++ src/FbTk/Texture.hh | 97 ++++++++++++ src/FbTk/XFontImp.cc | 383 ++++++++++++++++++++++++++++++++++++++++++++++ src/FbTk/XFontImp.hh | 81 ++++++++++ src/FbTk/XftFontImp.cc | 143 +++++++++++++++++ src/FbTk/XftFontImp.hh | 49 ++++++ src/FbTk/XmbFontImp.cc | 241 +++++++++++++++++++++++++++++ src/FbTk/XmbFontImp.hh | 49 ++++++ 20 files changed, 2058 insertions(+) create mode 100644 src/FbTk/App.cc create mode 100644 src/FbTk/App.hh create mode 100644 src/FbTk/Color.cc create mode 100644 src/FbTk/Color.hh create mode 100644 src/FbTk/EventHandler.hh create mode 100644 src/FbTk/Font.cc create mode 100644 src/FbTk/Font.hh create mode 100644 src/FbTk/FontImp.hh create mode 100644 src/FbTk/Makefile.am create mode 100644 src/FbTk/NotCopyable.hh create mode 100644 src/FbTk/SignalHandler.cc create mode 100644 src/FbTk/SignalHandler.hh create mode 100644 src/FbTk/Texture.cc create mode 100644 src/FbTk/Texture.hh create mode 100644 src/FbTk/XFontImp.cc create mode 100644 src/FbTk/XFontImp.hh create mode 100644 src/FbTk/XftFontImp.cc create mode 100644 src/FbTk/XftFontImp.hh create mode 100644 src/FbTk/XmbFontImp.cc create mode 100644 src/FbTk/XmbFontImp.hh diff --git a/src/FbTk/App.cc b/src/FbTk/App.cc new file mode 100644 index 0000000..89b5921 --- /dev/null +++ b/src/FbTk/App.cc @@ -0,0 +1,51 @@ +// App.cc +// Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#include "App.hh" +#include +#include + +namespace FbTk { + +App *App::s_app = 0; + +App *App::instance() { + if (s_app == 0) + throw std::string("You must create an instance of FbTk::App first!"); + return s_app; +} + +App::App(const char *displayname) { + if (s_app != 0) + throw std::string("Can't create more than one instance of FbTk::App"); + s_app = this; + m_display = XOpenDisplay(displayname); +} + +App::~App() { + if (m_display != 0) { + XCloseDisplay(m_display); + m_display = 0; + } + s_app = 0; +} + +}; // end namespace FbTk diff --git a/src/FbTk/App.hh b/src/FbTk/App.hh new file mode 100644 index 0000000..ef5836c --- /dev/null +++ b/src/FbTk/App.hh @@ -0,0 +1,50 @@ +// App.hh +// Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#ifndef FBTK_APP_HH +#define FBTK_APP_HH + +#include + +namespace FbTk { +/** + Main class for applications, every application must create an instance of this class +*/ +class App { +public: + /// @return singleton instance of App + static App *instance(); + explicit App(const char *displayname); + virtual ~App(); + /// display connection + Display *display() const { return m_display; } + /// starts event loop + void eventLoop(); + /// ends event loop + void end(); +private: + static App *s_app; + Display *m_display; +}; + +}; // end namespace FbTk + +#endif // FBTK_APP_HH diff --git a/src/FbTk/Color.cc b/src/FbTk/Color.cc new file mode 100644 index 0000000..2e69f1c --- /dev/null +++ b/src/FbTk/Color.cc @@ -0,0 +1,166 @@ +// Color.cc for Fluxbox window manager +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@users.sourceforge.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Color.cc,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#include "Color.hh" + +#include "App.hh" + +#include +using namespace std; + +namespace { +unsigned char maxValue(unsigned short colval) { + if (colval == 65535) + return 0xFF; + + return static_cast(colval/0xFF); +} + +}; + +namespace FbTk { +Color::Color(): +m_allocated(false), +m_screen(0) { + +} + +Color::Color(const Color &col_copy) { + copy(col_copy); +} + +Color::Color(unsigned char red, unsigned char green, unsigned char blue, int screen): +m_red(red), m_green(green), m_blue(blue), +m_pixel(0), m_allocated(false), +m_screen(screen) { + allocate(red, green, blue, screen); +} + +Color::Color(const char *color_string, int screen): +m_allocated(false), +m_screen(screen) { + setFromString(color_string, screen); +} + +Color::~Color() { + free(); +} + +bool Color::setFromString(const char *color_string, int screen) { + + if (color_string == 0) { + free(); + return false; + } + + Display *disp = App::instance()->display(); + Colormap colm = DefaultColormap(disp, screen); + + XColor color; + + if (! XParseColor(disp, colm, color_string, &color)) + cerr<<"FbTk::Color: Parse color error: \""<display(); + XFreeColors(disp, DefaultColormap(disp, m_screen), &pixel, 1, 0); + setPixel(0); + setRGB(0, 0, 0); + setAllocated(false); + } +} + +void Color::copy(const Color &col_copy) { + if (!col_copy.isAllocated()) { + free(); + setRGB(col_copy.red(), col_copy.green(), col_copy.blue()); + setPixel(col_copy.pixel()); + return; + } + + free(); + + allocate(col_copy.red(), + col_copy.green(), + col_copy.blue(), + col_copy.m_screen); + +} + +void Color::allocate(unsigned char red, unsigned char green, unsigned char blue, int screen) { + + Display *disp = App::instance()->display(); + XColor color; + // fill xcolor structure + color.red = red; + color.green = green; + color.blue = blue; + + + if (!XAllocColor(disp, DefaultColormap(disp, screen), &color)) { + cerr<<"FbTk::Color: Allocation error."< +class EventHandler { +public: + virtual ~EventHandler() { } + + virtual Event_ret_T handleEvent(Event_T * const) = 0; +}; + +}; + +#endif // FBTK_EVENTHANDLER_HH diff --git a/src/FbTk/Font.cc b/src/FbTk/Font.cc new file mode 100644 index 0000000..949890e --- /dev/null +++ b/src/FbTk/Font.cc @@ -0,0 +1,184 @@ +// Font.cc +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +//$Id: Font.cc,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + + +#include "Font.hh" +#include "FontImp.hh" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif // HAVE_CONFIG_H + +// for antialias +#ifdef USE_XFT +#include "XftFontImp.hh" +#endif // USE_XFT + +// for multibyte support +#ifdef USE_XMB +#include "XmbFontImp.hh" +#endif //USE_XMB + +// standard font system +#include "XFontImp.hh" + +//use gnu extensions +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif //_GNU_SOURCE + +#ifndef __USE_GNU +#define __USE_GNU +#endif //__USE_GNU + +#include +#include +#include +using namespace std; + +#ifdef HAVE_SETLOCALE +#include +#endif //HAVE_SETLOCALE + +namespace FbTk { + +bool Font::m_multibyte = false; +bool Font::m_utf8mode = false; + +Font::Font(const char *name, bool antialias): +m_fontimp(0), +m_antialias(false), m_rotated(false) { + + // MB_CUR_MAX returns the size of a char in the current locale + if (MB_CUR_MAX > 1) // more than one byte, then we're multibyte + m_multibyte = true; + + char *s; // temporary string for enviroment variable + // check for utf-8 mode + if (((s = getenv("LC_ALL")) && *s) || + ((s = getenv("LC_CTYPE")) && *s) || + ((s = getenv("LANG")) && *s)) { + if (strstr(s, "UTF-8")) + m_utf8mode = true; + } + + // create the right font implementation + // antialias is prio 1 +#ifdef USE_XFT + if (antialias) { + m_fontimp.reset(new XftFontImp(0, m_utf8mode)); + m_antialias = true; + } +#endif //USE_XFT + // if we didn't create a Xft font then create basic font + if (m_fontimp.get() == 0) { +#ifdef USE_XMB + if (m_multibyte || m_utf8mode) + m_fontimp.reset(new XmbFontImp(0, m_utf8mode)); + else // basic font implementation +#endif // USE_XMB + m_fontimp.reset(new XFontImp()); + } + + if (name != 0) { + load(name); + } + +} + +Font::~Font() { + +} + +void Font::setAntialias(bool flag) { + bool loaded = m_fontimp->loaded(); +#ifdef USE_XFT + if (flag && !isAntialias() && !m_rotated) { + m_fontimp.reset(new XftFontImp(m_fontstr.c_str(), m_utf8mode)); + } else if (!flag && isAntialias()) +#endif // USE_XFT + { +#ifdef USE_XMB + if (m_multibyte || m_utf8mode) + m_fontimp.reset(new XmbFontImp(m_fontstr.c_str(), m_utf8mode)); + else +#endif // USE_XMB + m_fontimp.reset(new XFontImp(m_fontstr.c_str())); + } + + if (m_fontimp->loaded() != loaded) { // if the new font failed to load, fall back to 'fixed' + if (!m_fontimp->load("fixed")) // if that failes too, output warning + cerr<<"Warning: can't load fallback font 'fixed'."<load(name); +} + +unsigned int Font::textWidth(const char * const text, unsigned int size) const { + return m_fontimp->textWidth(text, size); +} + +unsigned int Font::height() const { + return m_fontimp->height(); +} + +int Font::ascent() const { + return m_fontimp->ascent(); +} + +int Font::descent() const { + return m_fontimp->descent(); +} +void Font::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const { + if (text == 0 || len == 0) + return; + m_fontimp->drawText(w, screen, gc, text, len, x, y); +} + +void Font::rotate(float angle) { +#ifdef USE_XFT + // if we are rotated and we are changing to horiz text + // and we were antialiased before we rotated then change to XftFontImp + if (isRotated() && angle == 0 && isAntialias()) + m_fontimp.reset(new XftFontImp(m_fontstr.c_str(), m_utf8mode)); +#endif // USE_XFT + // change to a font imp that handles rotated fonts (i.e just XFontImp at the moment) + // if we're going to rotate this font + if (angle != 0 && isAntialias() && !isRotated()) { + m_fontimp.reset(new XFontImp(m_fontstr.c_str())); + } + + //Note: only XFontImp implements FontImp::rotate + m_fontimp->rotate(angle); + + m_rotated = (angle == 0 ? false : true); +} + +}; diff --git a/src/FbTk/Font.hh b/src/FbTk/Font.hh new file mode 100644 index 0000000..60281ba --- /dev/null +++ b/src/FbTk/Font.hh @@ -0,0 +1,84 @@ +// Font.cc +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +//$Id: Font.hh,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#ifndef FBTK_FONT_HH +#define FBTK_FONT_HH + +#include +#include + +#include +#include + +namespace FbTk { + +class FontImp; + +/** + Handles the client to fontimp bridge. +*/ +class Font { +public: + Font(const char *name=0, bool antialias = false); + virtual ~Font(); + /** + Load a font + @return true on success, else false and it'll fall back on the last + loaded font + */ + bool load(const char *name); + + /// @return true if multibyte is enabled, else false + static bool multibyte() { return m_multibyte; } + /// @return true if utf-8 mode is enabled, else false + static bool utf8() { return m_utf8mode; } + void setAntialias(bool flag); + /** + @param text text to check size + @param size length of text in bytes + @return size of text in pixels + */ + unsigned int textWidth(const char * const text, unsigned int size) const; + unsigned int height() const; + int ascent() const; + int descent() const; + /** + Rotate font in any angle (currently only 90 degrees supported and just XFont implementation) + */ + void rotate(float angle); + void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const; + bool isAntialias() const { return m_antialias; } + bool isRotated() const { return m_rotated; } +private: + + std::auto_ptr m_fontimp; + std::string m_fontstr; + static bool m_multibyte; + static bool m_utf8mode; + bool m_antialias; + bool m_rotated; ///< wheter we're rotated or not +}; + +}; //end namespace FbTk + +#endif //FBTK_FONT_HH diff --git a/src/FbTk/FontImp.hh b/src/FbTk/FontImp.hh new file mode 100644 index 0000000..5414daf --- /dev/null +++ b/src/FbTk/FontImp.hh @@ -0,0 +1,57 @@ +// FontImp.cc for FbTk +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: FontImp.hh,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#ifndef FBTK_FONTIMP_HH +#define FBTK_FONTIMP_HH + +#include "Color.hh" + +#include + +#include + +namespace FbTk { + +/** + FontImp, second part of the bridge pattern for fonts + pure interface class. + @see Font +*/ +class FontImp { +public: + virtual ~FontImp() { } + virtual bool load(const std::string &name) = 0; + virtual void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const = 0; + virtual unsigned int textWidth(const char * const text, unsigned int size) const = 0; + virtual int ascent() const = 0; + virtual int descent() const = 0; + virtual unsigned int height() const = 0; + virtual bool loaded() const = 0; + virtual void rotate(float angle) { } // by default, no rotate support +protected: + FontImp() { } +}; + +}; // end namespace FbTk + +#endif // FBTK_FONTIMP_HH diff --git a/src/FbTk/Makefile.am b/src/FbTk/Makefile.am new file mode 100644 index 0000000..8ec4cef --- /dev/null +++ b/src/FbTk/Makefile.am @@ -0,0 +1,14 @@ +lib_LTLIBRARIES = libFbTk.la + +if XFT +xft_SOURCE= XftFontImp.hh XftFontImp.cc +endif +if MULTIBYTE +xmb_SOURCE= XmbFontImp.hh XmbFontImp.cc +endif + +libFbTk_la_SOURCES = App.hh App.cc Color.cc Color.hh EventHandler.hh Font.cc Font.hh FontImp.hh NotCopyable.hh \ + SignalHandler.cc SignalHandler.hh Texture.cc Texture.hh \ + XFontImp.cc XFontImp.hh \ + ${xft_SOURCE} \ + ${xmb_SOURCE} diff --git a/src/FbTk/NotCopyable.hh b/src/FbTk/NotCopyable.hh new file mode 100644 index 0000000..e575b9e --- /dev/null +++ b/src/FbTk/NotCopyable.hh @@ -0,0 +1,42 @@ +// NotCopyable.hh +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#ifndef FBTK_NOTCOPYABLE_HH +#define FBTK_NOTCOPYABLE_HH + +namespace FbTk { + +/** Makes the inherited class not copyable. + inherit this class to + disable assignment and copy +*/ +class NotCopyable +{ +protected: + NotCopyable() {} +private: + NotCopyable(const NotCopyable &rhs); // copy constructor + NotCopyable &operator=(const NotCopyable &rhs); // assignment operator +}; + +}; // end namespace FbTk + +#endif //FBTK_NOTCOPYABLE_HH diff --git a/src/FbTk/SignalHandler.cc b/src/FbTk/SignalHandler.cc new file mode 100644 index 0000000..d78c42c --- /dev/null +++ b/src/FbTk/SignalHandler.cc @@ -0,0 +1,81 @@ +// SignalHandler.cc for Fluxbox Window Manager +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: SignalHandler.cc,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#include "SignalHandler.hh" + +namespace FbTk { + +EventHandler *SignalHandler::s_signal_handler[NSIG]; + +SignalHandler::SignalHandler() { + // clear signal list + for (int i=0; i < NSIG; ++i) + s_signal_handler[i] = 0; +} + +SignalHandler *SignalHandler::instance() { + static SignalHandler singleton; + return &singleton; +} + +bool SignalHandler::registerHandler(int signum, EventHandler *eh, + EventHandler **oldhandler_ret) { + // must be less than NSIG + if (signum >= NSIG) + return false; + + // get old signal handler for this signum + if (oldhandler_ret != 0) + *oldhandler_ret = s_signal_handler[signum]; + + struct sigaction sa; + // set callback + sa.sa_handler = SignalHandler::handleSignal; + sigemptyset (&sa.sa_mask); + sa.sa_flags = 0; + + if (sigaction(signum, &sa, 0) == -1) + return false; + + s_signal_handler[signum] = eh; + + return true; +} + +void SignalHandler::removeHandler(int signum) { + if (signum < NSIG) + s_signal_handler[signum] = 0; // clear handler pointer +} + +void SignalHandler::handleSignal(int signum) { + if (signum >= NSIG) + return; + // make sure we got a handler for this signal + if (s_signal_handler[signum] != 0) { + SignalEvent sigev; + sigev.signum = signum; + s_signal_handler[signum]->handleEvent(&sigev); + } +} + +}; diff --git a/src/FbTk/SignalHandler.hh b/src/FbTk/SignalHandler.hh new file mode 100644 index 0000000..e05fb3a --- /dev/null +++ b/src/FbTk/SignalHandler.hh @@ -0,0 +1,71 @@ +// SignalHandler.hh for Fluxbox Window Manager +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: SignalHandler.hh,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#ifndef FBTK_SIGNALHANDLER_HH +#define FBTK_SIGNALHANDLER_HH + +#include "EventHandler.hh" + +#include + +namespace FbTk { + +struct SignalEvent { + int signum; +}; + +/** + Handles system signals, singleton. + Usage: inherit the class EventHandler and then register + it to SignalHandler by calling registerHandler with + a signal number +*/ +class SignalHandler { +public: + + /// get singleton object + static SignalHandler *instance(); + /** + Register an event handler + @return true on success else false + @param signum signal number + @param eh event handler + @param oldhandler_ret return handler to old sighandler + */ + bool registerHandler(int signum, EventHandler *eh, EventHandler **oldhandler_ret = 0); + /** + removes the signum handler + @param signum signal number + */ + void removeHandler(int signum); +private: + SignalHandler(); + + static void handleSignal(int signum); + + static EventHandler *s_signal_handler[NSIG]; ///< NSIG defined in signal.h +}; + +}; // end namespace FbTk + +#endif // FBTK_SIGNALHANDLER_HH diff --git a/src/FbTk/Texture.cc b/src/FbTk/Texture.cc new file mode 100644 index 0000000..73c5ce3 --- /dev/null +++ b/src/FbTk/Texture.cc @@ -0,0 +1,99 @@ +// Texture.hh for Fluxbox Window Manager +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@users.sourceforge.net) +// +// from Image.cc for Blackbox - an X11 Window manager +// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Texture.cc,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#include "Texture.hh" + +#include +#include + +namespace FbTk { + +void Texture::setFromString(const char * const texture_str) { + if (texture_str == 0) + return; + int t_len = strlen(texture_str) + 1; + char *ts = new char[t_len]; + strcpy(ts, texture_str); + + // to lower + for (size_t byte_pos = 0; byte_pos < strlen(ts); ++byte_pos) + ts[byte_pos] = tolower(ts[byte_pos]); + + if (strstr(ts, "parentrelative")) { + setType(Texture::PARENTRELATIVE); + } else { + setType(Texture::NONE); + + if (strstr(ts, "solid")) + addType(Texture::SOLID); + else if (strstr(ts, "gradient")) { + addType(Texture::GRADIENT); + if (strstr(ts, "crossdiagonal")) + addType(Texture::CROSSDIAGONAL); + else if (strstr(ts, "rectangle")) + addType(Texture::RECTANGLE); + else if (strstr(ts, "pyramid")) + addType(Texture::PYRAMID); + else if (strstr(ts, "pipecross")) + addType(Texture::PIPECROSS); + else if (strstr(ts, "elliptic")) + addType(Texture::ELLIPTIC); + else if (strstr(ts, "diagonal")) + addType(Texture::DIAGONAL); + else if (strstr(ts, "horizontal")) + addType(Texture::HORIZONTAL); + else if (strstr(ts, "vertical")) + addType(Texture::VERTICAL); + else + addType(Texture::DIAGONAL); + } else + addType(Texture::SOLID); + + if (strstr(ts, "raised")) + addType(Texture::RAISED); + else if (strstr(ts, "sunken")) + addType(Texture::SUNKEN); + else if (strstr(ts, "flat")) + addType(Texture::FLAT); + else + addType(Texture::RAISED); + + if (! (type() & Texture::FLAT)) + if (strstr(ts, "bevel2")) + addType(Texture::BEVEL2); + else + addType(Texture::BEVEL1); + +#ifdef INTERLACE + if (strstr(ts, "interlaced")) + addType(Texture::INTERLACED); +#endif // INTERLACE + } + + delete [] ts; +} + +}; // end namespace FbTk diff --git a/src/FbTk/Texture.hh b/src/FbTk/Texture.hh new file mode 100644 index 0000000..ea846dc --- /dev/null +++ b/src/FbTk/Texture.hh @@ -0,0 +1,97 @@ +// Texture.hh for Fluxbox Window Manager +// Copyright (c) 2002 Henrik Kinnunen (fluxbox@linuxmail.org) +// +// from Image.hh for Blackbox - an X11 Window manager +// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Texture.hh,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#ifndef FBTK_TEXTURE_HH +#define FBTK_TEXTURE_HH + +#include "Color.hh" + +namespace FbTk { + +/** + Holds texture type and info +*/ +class Texture { +public: + + enum Bevel { + FLAT = 0x00002, + SUNKEN = 0x00004, + RAISED = 0x00008 + }; + + enum Textures { + NONE = 0x00000, + SOLID = 0x00010, + GRADIENT = 0x00020 + }; + + enum Gradients { + HORIZONTAL = 0x00040, + VERTICAL = 0x00080, + DIAGONAL = 0x00100, + CROSSDIAGONAL = 0x00200, + RECTANGLE = 0x00400, + PYRAMID = 0x00800, + PIPECROSS = 0x01000, + ELLIPTIC = 0x02000 + }; + + enum { + BEVEL1 = 0x04000, + BEVEL2 = 0x08000, // bevel types + INVERT = 0x010000, //inverted image + PARENTRELATIVE = 0x20000, + INTERLACED = 0x40000 + }; + + Texture():m_type(0) { } + + void setType(unsigned long t) { m_type = t; } + void addType(unsigned long t) { m_type |= t; } + void setFromString(const char * const str); + + Color &color() { return m_color; } + Color &colorTo() { return m_color_to; } + Color &hiColor() { return m_hicolor; } + Color &loColor() { return m_locolor; } + + const Color &color() const { return m_color; } + const Color &colorTo() const { return m_color_to; } + const Color &hiColor() const { return m_hicolor; } + const Color &loColor() const { return m_locolor; } + + inline unsigned long type() const { return m_type; } + + +private: + FbTk::Color m_color, m_color_to, m_hicolor, m_locolor; + unsigned long m_type; +}; + +}; // end namespace FbTk + +#endif // FBTK_TEXTURE_HH diff --git a/src/FbTk/XFontImp.cc b/src/FbTk/XFontImp.cc new file mode 100644 index 0000000..a9091e7 --- /dev/null +++ b/src/FbTk/XFontImp.cc @@ -0,0 +1,383 @@ +// XFontImp.cc for FbTk fluxbox toolkit +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: XFontImp.cc,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#include "XFontImp.hh" +#include "App.hh" + +#include + +#include +using namespace std; +namespace FbTk { + +XFontImp::XFontImp(const char *fontname):m_rotfont(0), m_fontstruct(0), +m_angle(0) { + if (fontname != 0) + load(fontname); +} + +XFontImp::~XFontImp() { + if (m_fontstruct != 0) + XFreeFont(App::instance()->display(), m_fontstruct); + if (m_rotfont != 0) + freeRotFont(); +} + +int XFontImp::ascent() const { + if (m_fontstruct == 0) + return 0; + if (m_rotfont != 0) + return m_rotfont->max_ascent; + + return m_fontstruct->ascent; +} + +bool XFontImp::load(const std::string &fontname) { + XFontStruct *font = XLoadQueryFont(App::instance()->display(), fontname.c_str()); + if (font == 0) + return false; + if (m_fontstruct != 0) // free old font struct, if any + XFreeFont(App::instance()->display(), m_fontstruct); + + m_fontstruct = font; //set new font + return true; +} + +void XFontImp::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const { + if (m_fontstruct == 0) + return; + // use roated font functions? + if (m_rotfont != 0) { + drawRotText(w, screen, gc, text, len, x, y); + return; + } + + Display *disp = App::instance()->display(); + XSetFont(disp, gc, m_fontstruct->fid); + XDrawString(disp, w, gc, x, y, text, len); +} + +unsigned int XFontImp::textWidth(const char * const text, unsigned int size) const { + if (text == 0) + return 0; + if (m_fontstruct == 0) + return 0; + // check rotated font? + if (m_rotfont != 0) + return rotTextWidth(text, size); + + return XTextWidth(m_fontstruct, text, size); +} + +unsigned int XFontImp::height() const { + if (m_fontstruct == 0) + return 0; + + return m_fontstruct->ascent + m_fontstruct->descent; +} + +void XFontImp::rotate(float angle) { + //we must have a font loaded before we rotate + if (m_fontstruct == 0) + return; + if (m_rotfont != 0) + freeRotFont(); + // no need for rotating, use regular font + if (angle == 0) + return; + + //get positive angle + while (angle < 0) + angle += 360; + + char val; + XImage *I1, *I2; + // X system default vars + Display *dpy = App::instance()->display(); + Window rootwin = DefaultRootWindow(dpy); + int screen = DefaultScreen(dpy); + + GC font_gc; + char text[3]; + int ichar, i, j, index, boxlen = 60; + int vert_w, vert_h, vert_len, bit_w, bit_h, bit_len; + int min_char, max_char; + unsigned char *vertdata, *bitdata; + int ascent, descent, lbearing, rbearing; + + // get nearest vertical or horizontal direction + int dir = (int)((angle+45.0)/90.0)%4; + + if (dir == 0) // no rotation + return; + + // create the depth 1 canvas bitmap + Pixmap canvas = XCreatePixmap(dpy, rootwin, boxlen, boxlen, 1); + + // create graphic context for our canvas + font_gc = XCreateGC(dpy, canvas, 0, 0); + + XSetBackground(dpy, font_gc, None); + + XSetFont(dpy, font_gc, m_fontstruct->fid); + + // allocate space for rotated font + m_rotfont = new(nothrow) XRotFontStruct; + + if (m_rotfont == 0) { + cerr<<"RotFont: out of memory"<min_char_or_byte2; + max_char = m_fontstruct->max_char_or_byte2; + + // we only want printable chars + if (min_char<32) + min_char = 32; + if (max_char>126) + max_char = 126; + + /* some overall font data ... */ + m_rotfont->dir = dir; + m_rotfont->min_char = min_char; + m_rotfont->max_char = max_char; + m_rotfont->max_ascent = m_fontstruct->max_bounds.ascent; + m_rotfont->max_descent = m_fontstruct->max_bounds.descent; + m_rotfont->height = m_rotfont->max_ascent + m_rotfont->max_descent; + + // font needs rotation + // loop through each character + for (ichar = min_char; ichar <= max_char; ichar++) { + index = ichar - m_fontstruct->min_char_or_byte2; + + // per char dimensions ... + ascent = m_rotfont->per_char[ichar-32].ascent = m_fontstruct->per_char[index].ascent; + descent = m_rotfont->per_char[ichar-32].descent = m_fontstruct->per_char[index].descent; + lbearing = m_rotfont->per_char[ichar-32].lbearing = m_fontstruct->per_char[index].lbearing; + rbearing = m_rotfont->per_char[ichar-32].rbearing = m_fontstruct->per_char[index].rbearing; + m_rotfont->per_char[ichar-32].width = m_fontstruct->per_char[index].width; + + // some space chars have zero body, but a bitmap can't have + if (!ascent && !descent) + ascent = m_rotfont->per_char[ichar-32].ascent = 1; + if (!lbearing && !rbearing) + rbearing = m_rotfont->per_char[ichar-32].rbearing = 1; + + // glyph width and height when vertical + vert_w = rbearing - lbearing; + vert_h = ascent + descent; + + // width in bytes + vert_len = (vert_w-1)/8+1; + + XSetForeground(dpy, font_gc, None); + XFillRectangle(dpy, canvas, font_gc, 0, 0, boxlen, boxlen); + + // draw the character centre top right on canvas + sprintf(text, "%c", ichar); + XSetForeground(dpy, font_gc, 1); + XDrawImageString(dpy, canvas, font_gc, boxlen/2 - lbearing, + boxlen/2 - descent, text, 1); + + // reserve memory for first XImage + vertdata = (unsigned char *) malloc((unsigned)(vert_len*vert_h)); + + /* create the XImage ... */ + I1 = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap, + 0, (char *)vertdata, vert_w, vert_h, 8, 0); + + if (I1 == None) { + cerr<<"RotFont: Cant create ximage."<byte_order = I1->bitmap_bit_order = MSBFirst; + + /* extract character from canvas ... */ + XGetSubImage(dpy, canvas, boxlen/2, boxlen/2 - vert_h, + vert_w, vert_h, 1, XYPixmap, I1, 0, 0); + I1->format = XYBitmap; + + /* width, height of rotated character ... */ + if (dir == 2) { + bit_w = vert_w; + bit_h = vert_h; + } else { + bit_w = vert_h; + bit_h = vert_w; + } + + /* width in bytes ... */ + bit_len = (bit_w-1)/8 + 1; + + m_rotfont->per_char[ichar-32].glyph.bit_w = bit_w; + m_rotfont->per_char[ichar-32].glyph.bit_h = bit_h; + + /* reserve memory for the rotated image ... */ + bitdata = (unsigned char *)calloc((unsigned)(bit_h * bit_len), 1); + + /* create the image ... */ + I2 = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYBitmap, 0, + (char *)bitdata, bit_w, bit_h, 8, 0); + + if (I2 == None) { + cerr<<"XFontImp: Cant create ximage!"<byte_order = I2->bitmap_bit_order = MSBFirst; + + /* map vertical data to rotated character ... */ + for (j = 0; j < bit_h; j++) { + for (i = 0; i < bit_w; i++) { + /* map bits ... */ + if (dir == 1) { + val = vertdata[i*vert_len + (vert_w-j-1)/8] & + (128>>((vert_w-j-1)%8)); + } else if (dir == 2) { + val = vertdata[(vert_h-j-1)*vert_len + + (vert_w-i-1)/8] & (128>>((vert_w-i-1)%8)); + } else { + val = vertdata[(vert_h-i-1)*vert_len + j/8] & + (128>>(j%8)); + } + if (val) { + bitdata[j*bit_len + i/8] = bitdata[j*bit_len + i/8] | + (128>>(i%8)); + } + } + } + + // create this character's bitmap + m_rotfont->per_char[ichar-32].glyph.bm = + XCreatePixmap(dpy, rootwin, bit_w, bit_h, 1); + + // put the image into the bitmap + XPutImage(dpy, m_rotfont->per_char[ichar-32].glyph.bm, + font_gc, I2, 0, 0, 0, 0, bit_w, bit_h); + + // free the image and data + XDestroyImage(I1); + XDestroyImage(I2); + } + + /* free pixmap and GC ... */ + XFreePixmap(dpy, canvas); + XFreeGC(dpy, font_gc); + +} + +void XFontImp::freeRotFont() { + if (m_rotfont == 0) + return; + // loop through each character and free its pixmap + for (int ichar = m_rotfont->min_char - 32; + ichar <= m_rotfont->max_char - 32; ++ichar) { + XFreePixmap(App::instance()->display(), m_rotfont->per_char[ichar].glyph.bm); + } + + delete m_rotfont; + m_rotfont = 0; +} + +void XFontImp::drawRotText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const { + + Display *dpy = App::instance()->display(); + static GC my_gc = 0; + int xp, yp, dir, ichar; + + if (text == NULL || len<1) + return; + + dir = m_rotfont->dir; + if (my_gc == 0) + my_gc = XCreateGC(dpy, w, 0, 0); + + XCopyGC(dpy, gc, GCForeground|GCBackground, my_gc); + + // vertical or upside down + + XSetFillStyle(dpy, my_gc, FillStippled); + + // loop through each character in texting + for (size_t i = 0; i= 0 && ichar<95) { + // suitable offset + if (dir == 1) { + xp = x-m_rotfont->per_char[ichar].ascent; + yp = y-m_rotfont->per_char[ichar].rbearing; + } else if (dir == 2) { + xp = x-m_rotfont->per_char[ichar].rbearing; + yp = y-m_rotfont->per_char[ichar].descent+1; + } else { + xp = x-m_rotfont->per_char[ichar].descent+1; + yp = y+m_rotfont->per_char[ichar].lbearing; + } + + // draw the glyph + XSetStipple(dpy, my_gc, m_rotfont->per_char[ichar].glyph.bm); + + XSetTSOrigin(dpy, my_gc, xp, yp); + + XFillRectangle(dpy, w, my_gc, xp, yp, + m_rotfont->per_char[ichar].glyph.bit_w, + m_rotfont->per_char[ichar].glyph.bit_h); + + // advance position + if (dir == 1) + y -= m_rotfont->per_char[ichar].width; + else if (dir == 2) + x -= m_rotfont->per_char[ichar].width; + else + y += m_rotfont->per_char[ichar].width; + } + } +} + + +unsigned int XFontImp::rotTextWidth(const char * const text, unsigned int size) const { + + if (text == 0) + return 0; + + unsigned int width = 0; + for (size_t i = 0; i= 0 && ichar < 95) + width += m_rotfont->per_char[ichar].width; + } + + return width; +} + +}; diff --git a/src/FbTk/XFontImp.hh b/src/FbTk/XFontImp.hh new file mode 100644 index 0000000..1ecc8cd --- /dev/null +++ b/src/FbTk/XFontImp.hh @@ -0,0 +1,81 @@ +// XFontImp.hh for FbTk fluxbox toolkit +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: XFontImp.hh,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#ifndef FBTK_XFONTIMP_HH +#define FBTK_XFONTIMP_HH + +#include "FontImp.hh" +namespace FbTk { + +class XFontImp:public FbTk::FontImp { +public: + explicit XFontImp(const char *filename = 0); + ~XFontImp(); + bool load(const std::string &filename); + unsigned int textWidth(const char * const text, unsigned int size) const; + unsigned int height() const; + float angle() const { return m_angle; } + int ascent() const; + int descent() const { return m_fontstruct ? m_fontstruct->descent : 0; } + void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const; + bool loaded() const { return m_fontstruct != 0; } + void rotate(float angle); +private: + void freeRotFont(); + void drawRotText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const; + unsigned int rotTextWidth(const char * const text, unsigned int size) const; + struct BitmapStruct { + int bit_w; + int bit_h; + + Pixmap bm; + }; + + struct XRotCharStruct { + int ascent; + int descent; + int lbearing; + int rbearing; + int width; + + BitmapStruct glyph; + }; + + struct XRotFontStruct { + int dir; + int height; + int max_ascent; + int max_descent; + int max_char; + int min_char; + + XRotCharStruct per_char[95]; + }; + XRotFontStruct *m_rotfont; + XFontStruct *m_fontstruct; + float m_angle; +}; + +}; // end namespace FbTk + +#endif // XFONTIMP_HH diff --git a/src/FbTk/XftFontImp.cc b/src/FbTk/XftFontImp.cc new file mode 100644 index 0000000..0a68518 --- /dev/null +++ b/src/FbTk/XftFontImp.cc @@ -0,0 +1,143 @@ +// XftFontImp.cc Xft font implementation for FbTk +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +//$Id: XftFontImp.cc,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#include "XftFontImp.hh" +#include "App.hh" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif //HAVE_CONFIG_H +namespace FbTk { + +XftFontImp::XftFontImp(const char *name, bool utf8):m_xftfont(0), +m_utf8mode(utf8) { + if (name != 0) + load(name); +} + +XftFontImp::~XftFontImp() { + if (m_xftfont != 0) + XftFontClose(App::instance()->display(), m_xftfont); +} + +bool XftFontImp::load(const std::string &name) { + //Note: assumes screen 0 for now, changes on draw if needed + + Display *disp = App::instance()->display(); + XftFont *newxftfont = XftFontOpenName(disp, 0, name.c_str()); + + if (newxftfont == 0) { // failed to open font, lets test with XLFD + newxftfont = XftFontOpenXlfd(disp, 0, name.c_str()); + if (newxftfont == 0) + return false; + } + // destroy old font and set new + if (m_xftfont != 0) + XftFontClose(disp, m_xftfont); + + m_xftfont = newxftfont; + + return true; +} + +void XftFontImp::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const { + if (m_xftfont == 0) + return; + Display *disp = App::instance()->display(); + XftDraw *draw = XftDrawCreate(disp, + w, + DefaultVisual(disp, screen), + DefaultColormap(disp, screen)); + + XGCValues gc_val; + + // get foreground pixel value and convert it to XRenderColor value + // TODO: we should probably check return status + XGetGCValues(disp, gc, GCForeground, &gc_val); + + // get red, green, blue values + XColor xcol; + xcol.pixel = gc_val.foreground; + XQueryColor(disp, DefaultColormap(disp, screen), &xcol); + + // convert xcolor to XftColor + XRenderColor rendcol; + rendcol.red = xcol.red; + rendcol.green = xcol.green; + rendcol.blue = xcol.blue; + rendcol.alpha = 0xFFFF; + XftColor xftcolor; + XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), + &rendcol, &xftcolor); + + // draw string +#ifdef HAVE_XFT_UTF8_STRING + if (m_utf8mode) { + XftDrawStringUtf8(draw, + &xftcolor, + m_xftfont, + x, y, + (XftChar8 *)(text), len); + } else +#endif // HAVE_XFT_UTF8_STRING + { + XftDrawString8(draw, + &xftcolor, + m_xftfont, + x, y, + (XftChar8 *)(text), len); + } + + XftColorFree(disp, DefaultVisual(disp, screen), + DefaultColormap(disp, screen), &xftcolor); + XftDrawDestroy(draw); +} + +unsigned int XftFontImp::textWidth(const char * const text, unsigned int len) const { + if (m_xftfont == 0) + return 0; + XGlyphInfo ginfo; +#ifdef HAVE_XFT_UTF8_STRING + if (m_utf8mode) { + XftTextExtentsUtf8(App::instance()->display(), + m_xftfont, + (XftChar8 *)text, len, + &ginfo); + } else +#endif //HAVE_XFT_UTF8_STRING + { + XftTextExtents8(App::instance()->display(), + m_xftfont, + (XftChar8 *)text, len, + &ginfo); + } + return ginfo.xOff; +} + +unsigned int XftFontImp::height() const { + if (m_xftfont == 0) + return 0; + return m_xftfont->height; +} + +}; // end namespace FbTk diff --git a/src/FbTk/XftFontImp.hh b/src/FbTk/XftFontImp.hh new file mode 100644 index 0000000..d89f4bd --- /dev/null +++ b/src/FbTk/XftFontImp.hh @@ -0,0 +1,49 @@ +// XftFontImp.hh Xft font implementation for FbTk +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +//$Id: XftFontImp.hh,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ + +#ifndef XFTFONTIMP_HH +#define XFTFONTIMP_HH + +#include "FontImp.hh" +#include +namespace FbTk { + +class XftFontImp:public FbTk::FontImp { +public: + XftFontImp(const char *fontname, bool utf8); + ~XftFontImp(); + bool load(const std::string &name); + void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const; + unsigned int textWidth(const char * const text, unsigned int len) const; + unsigned int height() const; + int ascent() const { return m_xftfont ? m_xftfont->ascent : 0; } + int descent() const { return m_xftfont ? m_xftfont->descent : 0; } + bool loaded() const { return m_xftfont != 0; } +private: + XftFont *m_xftfont; + bool m_utf8mode; +}; + +}; // end namespace FbTk + +#endif // XFTFONTIMP_HH diff --git a/src/FbTk/XmbFontImp.cc b/src/FbTk/XmbFontImp.cc new file mode 100644 index 0000000..f0c323d --- /dev/null +++ b/src/FbTk/XmbFontImp.cc @@ -0,0 +1,241 @@ +// XmbFontImp.cc for FbTk fluxbox toolkit +// Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: XmbFontImp.cc,v 1.1 2002/11/26 16:01:28 fluxgen Exp $ + +#include "XmbFontImp.hh" + +#include "App.hh" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif //HAVE_CONFIG_H + +#ifdef HAVE_SETLOCALE +#include +#endif // HAVE_SETLOCALE + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif // _GNU_SOURCE + +#include +#include +#include +using namespace std; + +namespace { + +const char *getFontSize(const char *pattern, int *size) { + const char *p; + const char *p2=0; + int n=0; + + for (p=pattern; 1; p++) { + if (!*p) { + if (p2!=0 && n>1 && n<72) { + *size = n; return p2+1; + } else { + *size = 16; return 0; + } + } else if (*p=='-') { + if (n>1 && n<72 && p2!=0) { + *size = n; + return p2+1; + } + p2=p; n=0; + } else if (*p>='0' && *p<='9' && p2!=0) { + n *= 10; + n += *p-'0'; + } else { + p2=0; n=0; + } + } +} + +const char *getFontElement(const char *pattern, char *buf, int bufsiz, ...) { + const char *p, *v; + char *p2; + va_list va; + + va_start(va, bufsiz); + buf[bufsiz-1] = 0; + buf[bufsiz-2] = '*'; + while((v = va_arg(va, char *)) != 0) { + p = strcasestr(pattern, v); + if (p) { + std::strncpy(buf, p+1, bufsiz-2); + p2 = strchr(buf, '-'); + if (p2) *p2=0; + va_end(va); + return p; + } + } + va_end(va); + std::strncpy(buf, "*", bufsiz); + return 0; +} + +XFontSet createFontSet(const char *fontname) { + Display *display = FbTk::App::instance()->display(); + XFontSet fs; + const int FONT_ELEMENT_SIZE=50; + char **missing, *def = "-"; + int nmissing, pixel_size = 0, buf_size = 0; + char weight[FONT_ELEMENT_SIZE], slant[FONT_ELEMENT_SIZE]; + + fs = XCreateFontSet(display, + fontname, &missing, &nmissing, &def); + if (fs && (! nmissing)) return fs; + +#ifdef HAVE_SETLOCALE + if (! fs) { + if (nmissing) XFreeStringList(missing); + + setlocale(LC_CTYPE, "C"); + fs = XCreateFontSet(display, fontname, + &missing, &nmissing, &def); + setlocale(LC_CTYPE, ""); + } +#endif // HAVE_SETLOCALE + + if (fs) { + XFontStruct **fontstructs; + char **fontnames; + XFontsOfFontSet(fs, &fontstructs, &fontnames); + fontname = fontnames[0]; + } + + getFontElement(fontname, weight, FONT_ELEMENT_SIZE, + "-medium-", "-bold-", "-demibold-", "-regular-", 0); + getFontElement(fontname, slant, FONT_ELEMENT_SIZE, + "-r-", "-i-", "-o-", "-ri-", "-ro-", 0); + getFontSize(fontname, &pixel_size); + + if (! strcmp(weight, "*")) + std::strncpy(weight, "medium", FONT_ELEMENT_SIZE); + if (! strcmp(slant, "*")) + std::strncpy(slant, "r", FONT_ELEMENT_SIZE); + if (pixel_size < 3) + pixel_size = 3; + else if (pixel_size > 97) + pixel_size = 97; + + buf_size = strlen(fontname) + (FONT_ELEMENT_SIZE * 2) + 64; + char *pattern2 = new char[buf_size]; + snprintf(pattern2, buf_size - 1, + "%s," + "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*," + "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*", + fontname, weight, slant, pixel_size, pixel_size); + fontname = pattern2; + + if (nmissing) + XFreeStringList(missing); + if (fs) + XFreeFontSet(display, fs); + + fs = XCreateFontSet(display, fontname, + &missing, &nmissing, &def); + delete [] pattern2; + + return fs; +} + +}; +namespace FbTk { + +XmbFontImp::XmbFontImp(const char *filename, bool utf8):m_fontset(0), m_utf8mode(utf8) { +#ifdef DEBUG +#ifdef X_HAVE_UTF8_STRING + cerr<<"Using utf8 = "<display(), m_fontset); +} + +bool XmbFontImp::load(const std::string &fontname) { + if (fontname.size() == 0) + return false; + XFontSet set = createFontSet(fontname.c_str()); + if (set == 0) + return false; + if (m_fontset != 0) + XFreeFontSet(App::instance()->display(), m_fontset); + m_fontset = set; + m_setextents = XExtentsOfFontSet(m_fontset); + + return true; +} + +void XmbFontImp::drawText(Drawable w, int screen, GC gc, const char *text, + size_t len, int x, int y) const { + + if (text == 0 || len == 0 || w == 0 || m_fontset == 0) + return; +#ifdef X_HAVE_UTF8_STRING + if (m_utf8mode) { + Xutf8DrawString(App::instance()->display(), w, m_fontset, + gc, x, y, + text, len); + } else +#endif //X_HAVE_UTF8_STRING + { + XmbDrawString(App::instance()->display(), w, m_fontset, + gc, x, y, + text, len); + } +} + +unsigned int XmbFontImp::textWidth(const char * const text, unsigned int len) const { + if (m_fontset == 0) + return 0; + XRectangle ink, logical; +#ifdef X_HAVE_UTF8_STRING + if (m_utf8mode) { + Xutf8TextExtents(m_fontset, text, len, + &ink, &logical); + } else +#endif // X_HAVE_UTF8_STRING + { + XmbTextExtents(m_fontset, text, len, + &ink, &logical); + } + + return logical.width; +} + +unsigned int XmbFontImp::height() const { + if (m_fontset == 0) + return 0; + return m_setextents->max_ink_extent.height; +} + +}; // end namespace FbTk + diff --git a/src/FbTk/XmbFontImp.hh b/src/FbTk/XmbFontImp.hh new file mode 100644 index 0000000..62ccafd --- /dev/null +++ b/src/FbTk/XmbFontImp.hh @@ -0,0 +1,49 @@ +// XmbFontImp.hh for FbTk fluxbox toolkit +// Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: XmbFontImp.hh,v 1.1 2002/11/26 16:01:28 fluxgen Exp $ + +#ifndef FBTK_XMBFONTIMP_HH +#define FBTK_XMBFONTIMP_HH + +#include "FontImp.hh" +namespace FbTk { + +class XmbFontImp:public FbTk::FontImp { +public: + XmbFontImp(const char *fontname, bool utf8); + ~XmbFontImp(); + bool load(const std::string &name); + virtual void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const; + unsigned int textWidth(const char * const text, unsigned int len) const; + unsigned int height() const; + int ascent() const { return m_setextents ? -m_setextents->max_ink_extent.y : 0; } + int descent() const { return m_setextents ? m_setextents->max_ink_extent.height + m_setextents->max_ink_extent.y : 0; } + bool loaded() const { return m_fontset != 0; } +private: + XFontSet m_fontset; + XFontSetExtents *m_setextents; + const bool m_utf8mode; +}; + +}; // end namespace FbTk + +#endif // FBTK_XMBFONTIMP_HH -- cgit v0.11.2