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)
39 void Widget::set_size(unsigned w, unsigned h)
45 void Widget::set_geometry(const Geometry &g)
50 void Widget::set_style(const string &s)
56 void Widget::set_visible(bool v)
63 if(!visible && parent)
64 parent->child_hidden(*this);
67 void Widget::render() const
70 throw InvalidState("Attempt to render a widget without a style");
73 GL::translate(geom.x, geom.y, 0);
74 for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
76 if(i->get_name().empty())
84 void Widget::render_graphic(const Part &part) const
87 part.render(geom, state);
91 void Widget::render_text(const Part &part, const string &text) const
93 const GL::Font *const font=style->get_font();
94 const float font_size=font->get_default_size();
97 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
98 rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
99 part.get_alignment().apply(rgeom, geom, part.get_margin());
102 GL::translate(rgeom.x, rgeom.y-font->get_descent()*font_size, 0);
103 GL::scale_uniform(font_size);
105 const GL::Color &color=style->get_font_color();
106 glColor3f(color.r, color.g, color.b);
107 font->draw_string(text);
113 void Widget::update_style()
115 string sname=get_class();
116 if(!style_name.empty())
121 style=res.get<Style>(sname);
124 void Widget::set_parent(Panel *p)
127 throw InvalidState("Widget is already in a Panel");
131 void Widget::set_parent(Widget &w, Panel *p)
137 Widget::Loader::Loader(Widget &w):
140 add("position", &Loader::position);
141 add("size", &Loader::size);
142 add("style", &Loader::style);
143 add("visible", &Widget::visible);
146 void Widget::Loader::position(int x, int y)
148 wdg.set_position(x, y);
151 void Widget::Loader::size(unsigned w, unsigned h)
156 void Widget::Loader::style(const string &s)