]> git.tdb.fi Git - libs/gltk.git/commitdiff
Enable loading of entry widgets from datafiles
authorMikko Rasa <tdb@tdb.fi>
Tue, 11 Dec 2007 13:30:49 +0000 (13:30 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 11 Dec 2007 13:30:49 +0000 (13:30 +0000)
Fix focus control logic
Refactor part specification and rendering
Change Alignment to use floats and move it to geometry.*
Add margins for parts
Add cursor to entry widget
Require full filenames of external resources to avoid name conflicts

23 files changed:
source/alignment.cpp [deleted file]
source/alignment.h [deleted file]
source/button.cpp
source/button.h
source/entry.cpp
source/entry.h
source/geometry.cpp
source/geometry.h
source/hslider.cpp
source/hslider.h
source/label.cpp
source/label.h
source/panel.cpp
source/panel.h
source/part.cpp
source/part.h
source/resources.cpp
source/resources.h
source/root.cpp
source/style.cpp
source/style.h
source/widget.cpp
source/widget.h

diff --git a/source/alignment.cpp b/source/alignment.cpp
deleted file mode 100644 (file)
index 110aa09..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/gl/transform.h>
-#include "alignment.h"
-#include "geometry.h"
-
-namespace Msp {
-namespace GLtk {
-
-void Alignment::apply(const Geometry &geom, unsigned wd, unsigned ht) const
-{
-       GL::translate((geom.w-wd)*(x+1)/2, (geom.h-ht)*(y+1)/2, 0);
-}
-
-} // namespace GLtk
-} // namespace Msp
diff --git a/source/alignment.h b/source/alignment.h
deleted file mode 100644 (file)
index 56d6766..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#ifndef MSP_GLTK_ALIGNMENT_H_
-#define MSP_GLTK_ALIGNMENT_H_
-
-namespace Msp {
-namespace GLtk {
-
-class Geometry;
-
-/**
-Handles alignment of widget parts.  Both x and y components can have values -1
-(left / bottom), 0 (center) or 1 (right / top).
-*/
-struct Alignment
-{
-       int x, y;
-
-       Alignment(): x(0), y(0) { }
-       Alignment(int x_, int y_): x(x_), y(y_) { }
-       void apply(const Geometry &, unsigned, unsigned) const;
-};
-
-} // namespace GLtk
-} // namespace Msp
-
-#endif
index f078ca6fcae2420ed88bdc8b41e32d786366d08e..58e69bfdad42543216da7dbb7ff6c7001835669c 100644 (file)
@@ -35,8 +35,8 @@ void Button::button_release(int x, int y, unsigned btn)
        {
                if(geom.is_inside(x, y))
                {
-                       signal_clicked.emit();
                        state=HOVER;
+                       signal_clicked.emit();
                }
                else
                        state=NORMAL;
@@ -53,12 +53,10 @@ void Button::pointer_leave()
        state=NORMAL;
 }
 
-void Button::render_part(const Part &part) const
+void Button::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
                render_text(part, text);
-       else
-               Widget::render_part(part);
 }
 
 
index 55cb2b0a28af82c7e930ee4603f166cf06a84850..ea96a7d01b69518d47d5bcb406406f1f653e186e 100644 (file)
@@ -43,7 +43,7 @@ public:
        virtual void pointer_leave();
 private:
        virtual const char *get_class() const { return "button"; }
-       virtual void render_part(const Part &) const;
+       virtual void render_special(const Part &) const;
 };
 
 } // namespace GLtk
index 2c9b9d68ce5caa5a9cf585ebb99f3879dc827603..eed2c39a942ced2e7299a6f357be9142cd6b4c15 100644 (file)
@@ -5,11 +5,11 @@ Copyright © 2007  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
-#include <SDL/SDL_keysym.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/texture.h>
 #include <msp/gl/transform.h>
 #include "entry.h"
+#include "graphic.h"
 #include "part.h"
 #include "style.h"
 
@@ -35,17 +35,17 @@ void Entry::set_text(const string &t)
 
 void Entry::key_press(unsigned key, unsigned, wchar_t ch)
 {
-       if(key==SDLK_LEFT)
+       if(key==100)
        {
                if(edit_pos>0)
                        --edit_pos;
        }
-       else if(key==SDLK_RIGHT)
+       else if(key==102)
        {
                if(edit_pos<text.size())
                        ++edit_pos;
        }
-       else if(key==SDLK_BACKSPACE)
+       else if(key==22)
        {
                if(edit_pos>0)
                        text.erase(--edit_pos, 1);
@@ -53,6 +53,7 @@ void Entry::key_press(unsigned key, unsigned, wchar_t ch)
        else
        {
                text+=ch;
+               ++edit_pos;
        }
 }
 
@@ -68,58 +69,41 @@ void Entry::focus_out()
                state=NORMAL;
 }
 
-void Entry::render_part(const Part &part) const
+void Entry::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
                render_text(part, text);
-       /*{
-               const GL::Font *const font=style->get_font();
+       else if(part.get_name()=="cursor")
+       {
+               if(!part.get_graphic(state))
+                       return;
 
+               const GL::Font *const font=style->get_font();
                const float font_size=font->get_default_size();
-               unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
 
-               GL::push_matrix();
+               const Geometry &pgeom=part.get_geometry();
+               unsigned gw=pgeom.w;
+               unsigned gh=(part.get_fill_y() ? geom.h : pgeom.h);
 
-               part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
-               GL::scale_uniform(font_size);
-
-               const Color &color=style->get_font_color();
-               glColor3f(color.r, color.g, color.b);
-               if(state==ACTIVE)
-               {
-                       cout<<"foo\n";
-                       font->draw_string(text.substr(0, edit_pos));
-                       GL::Texture::unbind();
-                       glBegin(GL_LINES);
-                       glVertex2f(0, 0);
-                       glVertex2f(0, 1);
-                       glEnd();
-                       font->draw_string(text.substr(edit_pos));
-               }
-               else
-                       font->draw_string(text);
-               glColor3f(1, 1, 1);
+               Geometry rgeom;
+               rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+               rgeom.h=(part.get_fill_y() ? geom.h : pgeom.h);
+               part.get_alignment().apply(rgeom, geom, part.get_margin());
+               rgeom.x+=static_cast<unsigned>(font->get_string_width(text.substr(0, edit_pos))*font_size);
 
-               GL::pop_matrix();
-               render_text(part, text);
-       }*/
-       /*else if(part.get_name()=="cursor")
-       {
-               unsigned gw=part.get_width();
-               unsigned gh=(part.get_fill_y() ? geom.h : part.get_height());
+               GL::push_matrix();
+               GL::translate(rgeom.x, rgeom.y, 0);
 
-               const float font_size=font->get_default_size();
-               unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+               part.get_graphic(state)->render(gw, gh);
 
-               GL::push_matrix();
-               GL::translate((geom.w-gw)*(value-min)/(max-min), (geom.h-gh)*(part.get_alignment().y+1)/2, 0);
-               const Graphic *graphic=part.get_graphic(state);
-               graphic->render(gw, gh);
                GL::pop_matrix();
-       }*/
-       else
-               Widget::render_part(part);
+       }
 }
 
+
+Entry::Loader::Loader(Entry &ent):
+       Widget::Loader(ent)
+{ }
+
 } // namespace GLtk
 } // namespace Msp
index 4cbb2a9687d76397408fdba9d35345058a5bd922..c880bb4f8f639324e880cdce656a9c517173ad1f 100644 (file)
@@ -23,6 +23,13 @@ Special parts:
 */
 class Entry: public Widget
 {
+public:
+       class Loader: public Widget::Loader
+       {
+       public:
+               Loader(Entry &);
+       };
+
 private:
        std::string text;
        unsigned edit_pos;
@@ -38,7 +45,7 @@ public:
        virtual void focus_out();
 private:
        virtual const char *get_class() const { return "entry"; }
-       virtual void render_part(const Part &) const;
+       virtual void render_special(const Part &) const;
 };
 
 } // namespace GLtk
index 6183928673f2e51b1344335550dfe76cf8d99b05..81efcd05bad2e69d22325eb3e7df851f638d3644 100644 (file)
@@ -33,5 +33,18 @@ Sides::Loader::Loader(Sides &s):
        add("left",   &Sides::left);
 }
 
+
+void Alignment::apply(Geometry &geom, const Geometry &parent) const
+{
+       geom.x+=static_cast<int>((parent.w-geom.w)*x);
+       geom.y+=static_cast<int>((parent.h-geom.h)*y);
+}
+
+void Alignment::apply(Geometry &geom, const Geometry &parent, const Sides &margin) const
+{
+       geom.x+=static_cast<int>(margin.left+(parent.w-margin.left-margin.right-geom.w)*x);
+       geom.y+=static_cast<int>(margin.bottom+(parent.h-margin.bottom-margin.top-geom.h)*y);
+}
+
 } // namespace GLtk
 } // namespace Msp
index 6dc982727070ed2f09dbd357f94963922ee8d8e1..6559414dd0497ae5cdd8a8e8a0136ed6af062876 100644 (file)
@@ -26,6 +26,7 @@ struct Geometry
        bool is_inside(int, int) const;
 };
 
+
 /**
 Specifies margins on the sides of an element.
 */
@@ -48,6 +49,20 @@ struct Sides
        Sides();
 };
 
+
+/**
+Performs alignment of nested geometries, such as widgets and their parts.
+*/
+struct Alignment
+{
+       float x, y;
+
+       Alignment(): x(0), y(0) { }
+       Alignment(float x_, float y_): x(x_), y(y_) { }
+       void apply(Geometry &, const Geometry &) const;
+       void apply(Geometry &, const Geometry &, const Sides &) const;
+};
+
 } // namespace GLtk
 } // namespace Msp
 
index f19eafee419db334409007df3eb1e02be402856a..d764797971029c08da73015f391e6ab6a9867795 100644 (file)
@@ -55,27 +55,26 @@ void HSlider::pointer_motion(int x, int)
        }
 }
 
-void HSlider::render_part(const Part &part) const
+void HSlider::render_special(const Part &part) const
 {
        if(part.get_name()=="slider")
        {
-               unsigned gw=part.get_width();
-               unsigned gh=(part.get_fill_y() ? geom.h : part.get_height());
+               const Geometry &pgeom=part.get_geometry();
+               unsigned gw=pgeom.w;
+               unsigned gh=(part.get_fill_y() ? geom.h : pgeom.h);
                GL::push_matrix();
                GL::translate((geom.w-gw)*(value-min)/(max-min), (geom.h-gh)*(part.get_alignment().y+1)/2, 0);
                const Graphic *graphic=part.get_graphic(state);
                graphic->render(gw, gh);
                GL::pop_matrix();
        }
-       else
-               Widget::render_part(part);
 }
 
 unsigned HSlider::get_slider_width() const
 {
        for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
                if(i->get_name()=="slider")
-                       return i->get_width();
+                       return i->get_geometry().w;
 
        return 0;
 }
index 4f05d6d6f4e095b478346226d7d908ddcc9fa6ff..310cc4e3c073dcdffc3021405f5f228c1c10dd13 100644 (file)
@@ -31,7 +31,7 @@ public:
        virtual void pointer_motion(int, int);
 private:
        virtual const char *get_class() const { return "hslider"; }
-       virtual void render_part(const Part &) const;
+       virtual void render_special(const Part &) const;
        unsigned get_slider_width() const;
 };
 
index 36637d0828e66995c8c8519b06c38e4cab48dbc6..0e0134d73766a4cb5f05830386d772790d6dca7d 100644 (file)
@@ -23,12 +23,10 @@ void Label::set_text(const std::string &t)
        text=t;
 }
 
-void Label::render_part(const Part &part) const
+void Label::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
                render_text(part, text);
-       else
-               Widget::render_part(part);
 }
 
 
index 0bd326f7a1aa054b40551282ef329f92ab6d627f..baa0ddc7123f5381e0208976b38b2d09b0375838 100644 (file)
@@ -35,7 +35,7 @@ public:
 
 private:
        virtual const char *get_class() const { return "label"; }
-       virtual void render_part(const Part &) const;
+       virtual void render_special(const Part &) const;
 };
 
 } // namespace GLtk
index 36585c97716778431c96cdcf4285c2cbaedc1b5b..bfd0aa8eca7f8f33e5d0ab0ab9688ac1431de911 100644 (file)
@@ -7,6 +7,7 @@ Distributed under the LGPL
 
 #include <msp/core/refptr.h>
 #include "button.h"
+#include "entry.h"
 #include "label.h"
 #include "panel.h"
 #include "part.h"
@@ -55,9 +56,10 @@ void Panel::button_press(int x, int y, unsigned btn)
        {
                if(Widget *wdg=get_child_at(x, y))
                {
-                       wdg->button_press(x-geom.x, y-geom.y, btn);
-                       pointer_grab=btn;
+                       set_pointer_focus(wdg, btn);
                        set_input_focus(wdg);
+
+                       wdg->button_press(x-geom.x, y-geom.y, btn);
                }
        }
 }
@@ -69,11 +71,7 @@ void Panel::button_release(int x, int y, unsigned btn)
                pointer_focus->button_release(x-geom.x, y-geom.y, btn);
 
                if(btn==pointer_grab)
-               {
-                       pointer_grab=0;
-
-                       set_pointer_focus(get_child_at(x, y));
-               }
+                       set_pointer_focus(get_child_at(x, y), 0);
        }
        else if(geom.is_inside(x, y))
        {
@@ -89,12 +87,17 @@ void Panel::pointer_motion(int x, int y)
        else if(geom.is_inside(x, y))
        {
                Widget *wdg=get_child_at(x, y);
-               set_pointer_focus(wdg);
+               set_pointer_focus(wdg, 0);
                if(wdg)
                        wdg->pointer_motion(x-geom.x, y-geom.y);
        }
 }
 
+void Panel::pointer_leave()
+{
+       set_pointer_focus(0, 0);
+}
+
 void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
 {
        if(input_focus)
@@ -112,7 +115,13 @@ void Panel::focus_out()
        set_input_focus(0);
 }
 
-void Panel::render_part(const Part &part) const
+void Panel::child_hidden(Widget &wdg)
+{
+       if(&wdg==pointer_focus)
+               set_pointer_focus(0, 0);
+}
+
+void Panel::render_special(const Part &part) const
 {
        if(part.get_name()=="children")
        {
@@ -120,13 +129,14 @@ void Panel::render_part(const Part &part) const
                        if((*i)->is_visible())
                                (*i)->render();
        }
-       else
-               Widget::render_part(part);
 }
 
-void Panel::set_pointer_focus(Widget *wdg)
+void Panel::set_pointer_focus(Widget *wdg, int grab)
 {
-       if(wdg!=pointer_focus && pointer_grab==0)
+       if(grab>0 && !wdg)
+               throw InvalidParameterValue("Can't grab on null widget");
+
+       if(wdg!=pointer_focus)
        {
                if(pointer_focus)
                        pointer_focus->pointer_leave();
@@ -136,6 +146,8 @@ void Panel::set_pointer_focus(Widget *wdg)
                if(pointer_focus)
                        pointer_focus->pointer_enter();
        }
+
+       pointer_grab=grab;
 }
 
 void Panel::set_input_focus(Widget *wdg)
@@ -168,6 +180,7 @@ Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        wdg_map(m)
 {
        add("button", &Loader::child<Button>);
+       add("entry",  &Loader::child<Entry>);
        add("label",  &Loader::child<Label>);
        add("panel",  &Loader::panel);
 }
index 3d268751979b0b0a6f9097f167881d4412448e8a..ad89a52b8f03ee20eed1dd71416e23c68484bccd 100644 (file)
@@ -55,13 +55,15 @@ public:
        virtual void button_press(int, int, unsigned);
        virtual void button_release(int, int, unsigned);
        virtual void pointer_motion(int, int);
+       virtual void pointer_leave();
        virtual void key_press(unsigned, unsigned, wchar_t);
        virtual void key_release(unsigned, unsigned);
        virtual void focus_out();
+       void child_hidden(Widget &);
 private:
        virtual const char *get_class() const { return "panel"; }
-       virtual void render_part(const Part &) const;
-       void set_pointer_focus(Widget *);
+       virtual void render_special(const Part &) const;
+       void set_pointer_focus(Widget *, int);
        void set_input_focus(Widget *);
        Widget *get_child_at(int, int);
 };
index f5e4b79ef5c36a674f329fd9ca3dbc9a575c5e1c..a92e7208ca6883015cd978eb5c9218700538faec 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2007  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/gl/transform.h>
 #include "geometry.h"
 #include "part.h"
 #include "resources.h"
@@ -16,8 +17,6 @@ namespace GLtk {
 
 Part::Part(const string &n):
        name(n),
-       width(1),
-       height(1),
        fill_x(true),
        fill_y(true)
 {
@@ -33,12 +32,19 @@ const Graphic *Part::get_graphic(State state) const
        return graphic[state];
 }
 
-void Part::render(const Geometry &geom, State state) const
+void Part::render(const Geometry &parent, State state) const
 {
-       unsigned gw=(fill_x ? geom.w : width);
-       unsigned gh=(fill_y ? geom.h : height);
-       align.apply(geom, gw, gh);
-       graphic[state]->render(gw, gh);
+       if(!graphic[state])
+               return;
+
+       Geometry rgeom=geom;
+       if(fill_x)
+               rgeom.w=parent.w-margin.left-margin.right;
+       if(fill_y)
+               rgeom.h=parent.h-margin.bottom-margin.top;
+       align.apply(rgeom, parent, margin);
+       GL::translate(rgeom.x, rgeom.y, 0);
+       graphic[state]->render(rgeom.w, rgeom.h);
 }
 
 
@@ -49,6 +55,7 @@ Part::Loader::Loader(Part &p, Resources &r):
        add("graphic", &Loader::graphic);
        add("align",   &Loader::align);
        add("fill",    &Loader::fill);
+       add("margin",  &Loader::margin);
 }
 
 Part::Loader::~Loader()
@@ -58,8 +65,8 @@ Part::Loader::~Loader()
                if(part.graphic[i])
                {
                        const Sides &shadow=part.graphic[i]->get_shadow();
-                       part.width=max(part.width, part.graphic[i]->get_width()-shadow.left-shadow.right);
-                       part.height=max(part.height, part.graphic[i]->get_height()-shadow.bottom-shadow.top);
+                       part.geom.w=max(part.geom.w, part.graphic[i]->get_width()-shadow.left-shadow.right);
+                       part.geom.h=max(part.geom.h, part.graphic[i]->get_height()-shadow.bottom-shadow.top);
                }
                else
                        part.graphic[i]=part.graphic[NORMAL];
@@ -71,7 +78,7 @@ void Part::Loader::graphic(State s, const string &n)
        part.graphic[s]=res.get<Graphic>(n);
 }
 
-void Part::Loader::align(int x, int y)
+void Part::Loader::align(float x, float y)
 {
        part.align.x=x;
        part.align.y=y;
@@ -83,5 +90,10 @@ void Part::Loader::fill(bool x, bool y)
        part.fill_y=y;
 }
 
+void Part::Loader::margin()
+{
+       load_sub(part.margin);
+}
+
 } // namespace GLtk
 } // namespace Msp
index 5736f7a476d9a5d72b3ded4c040138b38ef0888c..a94f29db87421d5fdff157cbae4462f89884f1d6 100644 (file)
@@ -11,7 +11,8 @@ Distributed under the LGPL
 #include <map>
 #include <string>
 #include <msp/datafile/loader.h>
-#include "alignment.h"
+//#include "alignment.h"
+#include "geometry.h"
 #include "state.h"
 
 namespace Msp {
@@ -37,15 +38,16 @@ public:
                ~Loader();
        private:
                void graphic(State, const std::string &);
-               void align(int, int);
+               void align(float, float);
                void fill(bool, bool);
+               void margin();
        };
 
 private:
        std::string name;
        const Graphic *graphic[N_STATES_];
-       unsigned width;
-       unsigned height;
+       Geometry geom;
+       Sides margin;
        Alignment align;
        bool fill_x;
        bool fill_y;
@@ -54,8 +56,8 @@ public:
        Part(const std::string &);
        const std::string &get_name() const { return name; }
        const Graphic *get_graphic(State) const;
-       unsigned get_width() const { return width; }
-       unsigned get_height() const { return height; }
+       const Geometry &get_geometry() const { return geom; }
+       const Sides &get_margin() const { return margin; }
        const Alignment &get_alignment() const { return align; }
        bool get_fill_x() const { return fill_x; }
        bool get_fill_y() const { return fill_y; }
index 35af009697647a8a2900718b46728ff3a0c8e2c3..2cfe022c1a1d74d5035aa855922dcc7ba9e5f0c8 100644 (file)
@@ -17,9 +17,7 @@ Resources::Resources():
        path("."),
        default_font(0)
 {
-       add_keyword<GL::Font>("font");
        add_keyword<Graphic>("graphic");
-       add_keyword<Style>("style");
 
        add_creator(&Resources::create_font);
        add_creator(&Resources::create_texture);
@@ -41,7 +39,7 @@ const GL::Font &Resources::get_default_font() const
 GL::Font *Resources::create_font(const string &name)
 {
        RefPtr<GL::Font> fnt=new GL::Font;
-       DataFile::load<GL::Font, Resources &>(*fnt, (path/(name+".font")).str(), *this);
+       DataFile::load<GL::Font, Resources &>(*fnt, (path/name).str(), *this);
        if(!default_font)
                default_font=fnt.get();
        return fnt.release();
@@ -50,7 +48,7 @@ GL::Font *Resources::create_font(const string &name)
 GL::Texture2D *Resources::create_texture(const string &name)
 {
        RefPtr<GL::Texture2D> tex=new GL::Texture2D;
-       tex->load_image((path/(name+".png")).str());
+       tex->load_image((path/name).str());
        tex->set_min_filter(GL::LINEAR);
        return tex.release();
 }
@@ -62,6 +60,7 @@ Resources::Loader::Loader(Resources &r):
 {
        add("default_font", &Loader::default_font);
        add("font", &Loader::font);
+       add("style", &Loader::style);
 }
 
 void Resources::Loader::default_font(const string &name)
@@ -79,5 +78,13 @@ void Resources::Loader::font(const string &name)
        fnt.release();
 }
 
+void Resources::Loader::style(const string &name)
+{
+       RefPtr<Style> stl=new Style(res);
+       load_sub(*stl, res);
+       res.add(name, stl.get());
+       stl.release();
+}
+
 } // namespace GLtk
 } // namespace Msp
index 8fb2185448b7d20c291b07e3560486a75660e779..c12215c04eb9c72095a22f55b7f921e36ee03607 100644 (file)
@@ -39,6 +39,7 @@ public:
        private:
                void default_font(const std::string &);
                void font(const std::string &);
+               void style(const std::string &);
        };
 
        Resources();
index a59c939b5b1415d1e918c7fe7c7a023652303ee6..8dfe491d14f2d16336c43af653d746ad89bd3686 100644 (file)
@@ -21,6 +21,8 @@ Root::Root(Resources &r, Window &w):
        window.signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
        window.signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
        window.signal_pointer_motion.connect(sigc::mem_fun(this, &Root::pointer_motion_event));
+       window.signal_key_press.connect(sigc::mem_fun(this, &Root::key_press));
+       window.signal_key_release.connect(sigc::mem_fun(this, &Root::key_release));
 }
 
 void Root::button_press_event(int x, int y, unsigned btn, unsigned)
index 6c116570fe24677fbb9002eb3061d2e3f7f0c9c2..0d6e424e2163b048d65c3c76af46b7792cb87acc 100644 (file)
@@ -13,8 +13,8 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-Style::Style():
-       font(0)
+Style::Style(Resources &r):
+       font(&r.get_default_font())
 { }
 
 
@@ -25,6 +25,7 @@ Style::Loader::Loader(Style &s, Resources &r):
        add("font",       &Style::font);
        add("font_color", &Loader::font_color);
        add("part",       &Loader::part);
+       add("special",    &Loader::special);
 }
 
 void Style::Loader::font_color(float r, float g, float b)
@@ -32,7 +33,14 @@ void Style::Loader::font_color(float r, float g, float b)
        style.font_color=GL::Color(r, g, b);
 }
 
-void Style::Loader::part(const string &n)
+void Style::Loader::part()
+{
+       Part p((string()));
+       load_sub(p, res);
+       style.parts.push_back(p);
+}
+
+void Style::Loader::special(const string &n)
 {
        Part p(n);
        load_sub(p, res);
index 0bbdc21d23d59eb4269bee0dbed3888cc11d1412..f17735f05e07c7efb4a3aa88c9e4bb6f4db79724 100644 (file)
@@ -40,7 +40,8 @@ public:
        private:
                void font(const std::string &);
                void font_color(float, float, float);
-               void part(const std::string &);
+               void part();
+               void special(const std::string &);
        };
 
 private:
@@ -49,7 +50,7 @@ private:
        PartSeq parts;
 
 public:
-       Style();
+       Style(Resources &);
        const GL::Font *get_font() const  { return font; }
        const GL::Color &get_font_color() const { return font_color; }
        const PartSeq &get_parts() const { return parts; }
index e41310a50c5fd5cba82298dd3149983b16a0f8a1..2eade3913f4d9c9e4530675657873ee080a2d265 100644 (file)
@@ -53,6 +53,17 @@ void Widget::set_style(const string &s)
        update_style();
 }
 
+void Widget::set_visible(bool v)
+{
+       if(v==visible)
+               return;
+
+       visible=v;
+
+       if(!visible && parent)
+               parent->child_hidden(*this);
+}
+
 void Widget::render() const
 {
        if(!style)
@@ -61,15 +72,15 @@ void Widget::render() const
        GL::push_matrix();
        GL::translate(geom.x, geom.y, 0);
        for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
-               render_part(*i);
+       {
+               if(i->get_name().empty())
+                       render_graphic(*i);
+               else
+                       render_special(*i);
+       }
        GL::pop_matrix();
 }
 
-void Widget::render_part(const Part &part) const
-{
-       render_graphic(part);
-}
-
 void Widget::render_graphic(const Part &part) const
 {
        GL::push_matrix();
@@ -80,13 +91,15 @@ void Widget::render_graphic(const Part &part) const
 void Widget::render_text(const Part &part, const string &text) const
 {
        const GL::Font *const font=style->get_font();
-
        const float font_size=font->get_default_size();
-       unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
 
-       GL::push_matrix();
+       Geometry rgeom;
+       rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+       rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
+       part.get_alignment().apply(rgeom, geom, part.get_margin());
 
-       part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
+       GL::push_matrix();
+       GL::translate(rgeom.x, rgeom.y-font->get_descent()*font_size, 0);
        GL::scale_uniform(font_size);
 
        const GL::Color &color=style->get_font_color();
index 6cb5e4a1bd670ae13bb930a6b06c9db7af887e40..feed5b7df014a7fa646f56ee87e7865adfad2be8 100644 (file)
@@ -63,14 +63,16 @@ public:
        */
        void set_style(const std::string &);
 
+       void set_visible(bool);
+
        const Geometry &get_geometry() const { return geom; }
        bool is_visible() const { return visible; }
 
        void render() const;
 protected:
-       virtual void render_part(const Part &) const;
        void render_graphic(const Part &) const;
        void render_text(const Part &, const std::string &) const;
+       virtual void render_special(const Part &) const { }
 
 public:
        // Events