3 This file is part of libmspgltk
4 Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/immediate.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/transform.h>
11 #include <msp/strings/formatter.h>
13 #include "resources.h"
21 Widget::Widget(const Resources &r):
32 parent->remove(*this);
35 void Widget::set_position(int x, int y)
42 void Widget::set_size(unsigned w, unsigned h)
49 void Widget::set_geometry(const Geometry &g)
55 void Widget::set_style(const string &s)
61 void Widget::set_visible(bool v)
68 signal_visibility_changed.emit(visible);
71 void Widget::set_focus()
74 throw InvalidState("No parent");
76 throw InvalidState("Can't set focus on invisible widget");
78 signal_request_focus.emit();
81 void Widget::render() const
84 throw InvalidState(format("Attempt to render a widget without a style (class=\"%s\")", get_class()));
87 GL::translate(geom.x, geom.y, 0);
88 for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
90 if(i->get_name().empty())
98 void Widget::render_graphic(const Part &part) const
101 part.render(geom, state);
105 void Widget::render_text(const Part &part, const string &text) const
107 const GL::Font *const font=style->get_font();
108 const float font_size=font->get_default_size();
111 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
112 rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
113 rgeom.y=static_cast<int>(-font->get_descent()*font_size);
114 part.get_alignment().apply(rgeom, geom, part.get_margin());
117 GL::translate(rgeom.x, rgeom.y, 0);
118 GL::scale_uniform(font_size);
120 const GL::Color &color=style->get_font_color();
121 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
122 imm.color(color.r, color.g, color.b);
123 font->draw_string(text, imm);
128 void Widget::pointer_enter()
133 void Widget::pointer_leave()
138 void Widget::focus_in()
143 void Widget::focus_out()
148 void Widget::update_style()
150 string sname=get_class();
151 if(!style_name.empty())
156 style=res.get<Style>(sname);
160 void Widget::set_parent(Container *p)
163 throw InvalidState("Widget is already in a Container");
169 void Widget::set_parent(Widget &w, Container *p)
175 Widget::Loader::Loader(Widget &w):
178 add("position", &Loader::position);
179 add("size", &Loader::size);
180 add("style", &Loader::style);
181 add("visible", &Widget::visible);
184 void Widget::Loader::position(int x, int y)
186 wdg.set_position(x, y);
189 void Widget::Loader::size(unsigned w, unsigned h)
194 void Widget::Loader::style(const string &s)