3 This file is part of libmspgltk
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/matrix.h>
9 #include <msp/gl/transform.h>
11 #include "resources.h"
19 Widget::Widget(const Resources &r):
30 parent->remove(*this);
33 void Widget::set_position(int x, int y)
40 void Widget::set_size(unsigned w, unsigned h)
47 void Widget::set_geometry(const Geometry &g)
53 void Widget::set_style(const string &s)
59 void Widget::set_visible(bool v)
66 if(!visible && parent)
67 parent->child_hidden(*this);
70 void Widget::render() const
73 throw InvalidState("Attempt to render a widget without a style");
76 GL::translate(geom.x, geom.y, 0);
77 for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
79 if(i->get_name().empty())
87 void Widget::render_graphic(const Part &part) const
90 part.render(geom, state);
94 void Widget::render_text(const Part &part, const string &text) const
96 const GL::Font *const font=style->get_font();
97 const float font_size=font->get_default_size();
100 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
101 rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
102 rgeom.y=static_cast<int>(-font->get_descent()*font_size);
103 part.get_alignment().apply(rgeom, geom, part.get_margin());
106 GL::translate(rgeom.x, rgeom.y, 0);
107 GL::scale_uniform(font_size);
109 const GL::Color &color=style->get_font_color();
110 glColor3f(color.r, color.g, color.b);
111 font->draw_string(text);
117 void Widget::update_style()
119 string sname=get_class();
120 if(!style_name.empty())
125 style=res.get<Style>(sname);
129 void Widget::set_parent(Panel *p)
132 throw InvalidState("Widget is already in a Panel");
138 void Widget::set_parent(Widget &w, Panel *p)
144 Widget::Loader::Loader(Widget &w):
147 add("position", &Loader::position);
148 add("size", &Loader::size);
149 add("style", &Loader::style);
150 add("visible", &Widget::visible);
153 void Widget::Loader::position(int x, int y)
155 wdg.set_position(x, y);
158 void Widget::Loader::size(unsigned w, unsigned h)
163 void Widget::Loader::style(const string &s)