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>
10 #include <msp/strings/formatter.h>
12 #include "resources.h"
20 Widget::Widget(const Resources &r):
31 parent->remove(*this);
34 void Widget::set_position(int x, int y)
41 void Widget::set_size(unsigned w, unsigned h)
48 void Widget::set_geometry(const Geometry &g)
54 void Widget::set_style(const string &s)
60 void Widget::set_visible(bool v)
67 if(!visible && parent)
68 parent->child_hidden(*this);
71 void Widget::render() const
74 throw InvalidState(format("Attempt to render a widget without a style (class=\"%s\")", get_class()));
77 GL::translate(geom.x, geom.y, 0);
78 for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
80 if(i->get_name().empty())
88 void Widget::render_graphic(const Part &part) const
91 part.render(geom, state);
95 void Widget::render_text(const Part &part, const string &text) const
97 const GL::Font *const font=style->get_font();
98 const float font_size=font->get_default_size();
101 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
102 rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
103 rgeom.y=static_cast<int>(-font->get_descent()*font_size);
104 part.get_alignment().apply(rgeom, geom, part.get_margin());
107 GL::translate(rgeom.x, rgeom.y, 0);
108 GL::scale_uniform(font_size);
110 const GL::Color &color=style->get_font_color();
111 glColor3f(color.r, color.g, color.b);
112 font->draw_string(text);
118 void Widget::update_style()
120 string sname=get_class();
121 if(!style_name.empty())
126 style=res.get<Style>(sname);
130 void Widget::set_parent(Panel *p)
133 throw InvalidState("Widget is already in a Panel");
139 void Widget::set_parent(Widget &w, Panel *p)
145 Widget::Loader::Loader(Widget &w):
148 add("position", &Loader::position);
149 add("size", &Loader::size);
150 add("style", &Loader::style);
151 add("visible", &Widget::visible);
154 void Widget::Loader::position(int x, int y)
156 wdg.set_position(x, y);
159 void Widget::Loader::size(unsigned w, unsigned h)
164 void Widget::Loader::style(const string &s)