#include <msp/gl/immediate.h>
#include "button.h"
#include "part.h"
+#include "style.h"
+
+using namespace std;
namespace Msp {
namespace GLtk {
set_text(t);
}
+void Button::autosize()
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+
+ if(const Part *text_part = style->get_part("text"))
+ {
+ const Sides &margin = text_part->get_margin();
+ geom.w = max(geom.w, text.get_width()+margin.left+margin.right);
+ geom.h = max(geom.h, text.get_height()+margin.top+margin.bottom);
+ }
+
+ if(icon)
+ {
+ if(const Part *icon_part = style->get_part("icon"))
+ {
+ const Sides &margin = icon_part->get_margin();
+ geom.w = max(geom.w, icon->get_width()+margin.left+margin.right);
+ geom.h = max(geom.h, icon->get_height()+margin.top+margin.bottom);
+ }
+ }
+}
+
void Button::set_text(const std::string &t)
{
text = t;
virtual const char *get_class() const { return "button"; }
+ virtual void autosize();
+
void set_text(const std::string &);
void set_icon(const GL::Texture2D *);
#include "list.h"
#include "panel.h"
#include "part.h"
+#include "root.h"
#include "style.h"
#include "text.h"
list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
}
+void Dropdown::autosize()
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+ geom.w = max(geom.w, list.get_geometry().w);
+
+ if(const Part *text_part = style->get_part("text"))
+ {
+ const Sides &margin = text_part->get_margin();
+ const GL::Font *font = style->get_font();
+ float font_size = font->get_default_size();
+ unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
+ geom.h = max(geom.h, line_height+margin.top+margin.bottom);
+ }
+}
+
void Dropdown::append(const string &item)
{
list.append(item);
void Dropdown::resize_list()
{
- list.autosize();
- const Geometry &lgeom = list.get_geometry();
- list.set_geometry(Geometry(0, -lgeom.h, max(geom.w, lgeom.w), lgeom.h));
+ list.autosize_all();
+ Geometry lgeom = list.get_geometry();
+ lgeom.x = 0;
+ lgeom.y = -lgeom.h;
+ lgeom.w = max(geom.w, lgeom.w);
+ int root_x = geom.x;
+ int root_y = geom.y;
+ for(Widget *p=parent; p; p=p->get_parent())
+ {
+ root_x += p->get_geometry().x;
+ root_y += p->get_geometry().y;
+ if(Root *root = dynamic_cast<Root *>(p))
+ {
+ const Geometry &rgeom = root->get_geometry();
+ if(lgeom.h*2>rgeom.h)
+ lgeom.h = rgeom.h/2;
+ if(root_y+lgeom.y<0)
+ lgeom.y = -root_y;
+ if(root_y+lgeom.y+lgeom.h>rgeom.h)
+ lgeom.y = rgeom.h-lgeom.h-root_y;
+ if(root_x+lgeom.x+lgeom.w>rgeom.w)
+ lgeom.x = rgeom.w-lgeom.w-root_x;
+ break;
+ }
+ }
+ list.set_geometry(lgeom);
}
void Dropdown::list_item_selected(unsigned index, const std::string &item)
virtual const char *get_class() const { return "dropdown"; }
+ virtual void autosize();
+
void append(const std::string &);
void insert(unsigned, const std::string &);
void remove(unsigned);
set_text(t);
}
+void Entry::autosize()
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+
+ if(text_part)
+ {
+ const Sides &margin = text_part->get_margin();
+ const GL::Font &font = *style->get_font();
+ unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*font.get_native_size());
+ geom.w = max(geom.w, 10*en_width+margin.left+margin.right);
+
+ unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_native_size());
+ if(multiline)
+ {
+ unsigned line_spacing = font.get_native_size()*6/5;
+ geom.h = max(geom.h, line_height+line_spacing*2+margin.top+margin.bottom);
+ }
+ else
+ geom.h = max(geom.h, line_height+margin.top+margin.bottom);
+ }
+
+ if(multiline)
+ {
+ if(const Part *slider_part = style->get_part("slider"))
+ {
+ Geometry sgeom = slider_part->get_geometry();
+ if(!sgeom.w || !sgeom.h)
+ {
+ slider->autosize();
+ if(!sgeom.w)
+ sgeom.w = slider->get_geometry().w;
+ if(!sgeom.h)
+ sgeom.h = slider->get_geometry().h;
+ }
+
+ const Sides &margin = slider_part->get_margin();
+ geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
+ geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
+
+ reposition_slider();
+ }
+
+ check_view_range();
+ }
+}
+
void Entry::set_text(const string &t)
{
text = t;
if(const Part *slider_part = style->get_part("slider"))
{
Geometry sgeom = slider_part->get_geometry();
+ if(!sgeom.w || !sgeom.h)
+ {
+ slider->autosize();
+ if(!sgeom.w)
+ sgeom.w = slider->get_geometry().w;
+ if(!sgeom.h)
+ sgeom.h = slider->get_geometry().h;
+ }
+
slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
slider->set_geometry(sgeom);
}
virtual const char *get_class() const { return "entry"; }
+ virtual void autosize();
+
void set_text(const std::string &);
const std::string &get_text() const { return text.get(); }
void set_multiline(bool);
slider_size(1)
{ }
+void HSlider::autosize()
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+
+ if(const Part *slider_part = style->get_part("slider"))
+ {
+ const Sides &margin = slider_part->get_margin();
+ const Geometry &pgeom = slider_part->get_geometry();
+ geom.w = std::max(geom.w, pgeom.w*3/2+margin.left+margin.right);
+ geom.h = std::max(geom.h, pgeom.h+margin.top+margin.bottom);
+ }
+}
+
void HSlider::render_special(const Part &part) const
{
if(part.get_name()=="slider")
virtual const char *get_class() const { return "hslider"; }
+ virtual void autosize();
+
private:
virtual void render_special(const Part &) const;
/* $Id$
This file is part of libmspgltk
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2010-2011 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
#include <msp/gl/matrix.h>
#include "image.h"
#include "part.h"
+#include "style.h"
using namespace std;
focusable = false;
}
+void Image::autosize()
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+
+ if(const Part *image_part = style->get_part("image"))
+ {
+ const Sides &margin = image_part->get_margin();
+ if(image)
+ {
+ geom.w = max(geom.w, image->get_width()+margin.left+margin.right);
+ geom.h = max(geom.h, image->get_height()+margin.top+margin.bottom);
+ }
+ else
+ {
+ geom.w = max(geom.w, margin.left+margin.right);
+ geom.h = max(geom.h, margin.top+margin.bottom);
+ }
+ }
+}
+
void Image::set_image(const GL::Texture2D *i)
{
image = i;
virtual const char *get_class() const { return "image"; }
+ virtual void autosize();
+
void set_image(const GL::Texture2D *);
void set_keep_aspect(bool);
/* $Id$
This file is part of libmspgltk
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2007, 2009-2011 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
if(!style)
return;
- geom.h = text.get_height();
- geom.w = text.get_width();
+ Widget::autosize();
+
if(const Part *text_part = style->get_part("text"))
{
const Sides &margin = text_part->get_margin();
- geom.w += margin.left+margin.right;
- geom.h += margin.top+margin.bottom;
+ geom.w = max(geom.w, text.get_width()+margin.left+margin.right);
+ geom.h = max(geom.h, text.get_height()+margin.top+margin.bottom);
}
}
}
void List::autosize()
+{
+ autosize_rows(5);
+}
+
+void List::autosize_rows(unsigned n)
{
if(!style)
return;
- float font_size = style->get_font()->get_default_size();
+ Widget::autosize();
- geom.w = 0;
- for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
+ if(items_part)
{
- unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
- geom.w = max(geom.w, w);
- }
+ const Sides &margin = items_part->get_margin();
+ float font_size = style->get_font()->get_default_size();
- geom.h = items.size()*row_height;
+ unsigned max_w = 0;
+ for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
+ {
+ unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
+ max_w = max(max_w, w);
+ }
- if(items_part)
+ geom.w = max(geom.w, max_w+margin.left+margin.right);
+ geom.h = max(geom.h, n*row_height+margin.top+margin.bottom);
+ }
+
+ if(const Part *slider_part = style->get_part("slider"))
{
- const Sides &margin = items_part->get_margin();
- geom.w += margin.left+margin.right;
- geom.h += margin.top+margin.bottom;
+ Geometry sgeom = slider_part->get_geometry();
+ if(!sgeom.w || !sgeom.h)
+ {
+ slider.autosize();
+ if(!sgeom.w)
+ sgeom.w = slider.get_geometry().w;
+ if(!sgeom.h)
+ sgeom.h = slider.get_geometry().h;
+ }
+
+ const Sides &margin = slider_part->get_margin();
+ geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
+ geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
+
+ reposition_slider();
}
+
+ check_view_range();
+}
+
+void List::autosize_all()
+{
+ autosize_rows(items.size());
}
void List::append(const string &v)
virtual const char *get_class() const { return "list"; }
virtual void autosize();
+ void autosize_rows(unsigned);
+ void autosize_all();
void append(const std::string &);
void insert(unsigned, const std::string &);
#include "panel.h"
#include "part.h"
+#include "style.h"
#include "toggle.h"
using namespace std;
set_text(t);
}
+void Toggle::autosize()
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+
+ if(const Part *text_part = style->get_part("text"))
+ {
+ const Sides &margin = text_part->get_margin();
+ geom.w = max(geom.w, text.get_width()+margin.left+margin.right);
+ geom.h = max(geom.h, text.get_height()+margin.top+margin.bottom);
+ }
+}
+
void Toggle::set_text(const string &t)
{
text = t;
virtual const char *get_class() const { return "toggle"; }
+ virtual void autosize();
+
void set_text(const std::string &);
void set_exclusive(bool);
bool get_exclusive() const { return exclusive; }
slider_size(1)
{ }
+void VSlider::autosize()
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+
+ if(const Part *slider_part = style->get_part("slider"))
+ {
+ const Sides &margin = slider_part->get_margin();
+ const Geometry &pgeom = slider_part->get_geometry();
+ geom.w = std::max(geom.w, pgeom.w+margin.left+margin.right);
+ geom.h = std::max(geom.h, pgeom.h*3/2+margin.top+margin.bottom);
+ }
+}
+
void VSlider::render_special(const Part &part) const
{
if(part.get_name()=="slider")
virtual const char *get_class() const { return "vslider"; }
+ virtual void autosize();
+
private:
virtual void render_special(const Part &) const;
on_geometry_change();
}
+void Widget::autosize()
+{
+ geom.w = 0;
+ geom.h = 0;
+ const Style::PartSeq &parts = style->get_parts();
+ for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+ if(i->get_name().empty())
+ {
+ geom.w = max(geom.w, i->get_geometry().w);
+ geom.h = max(geom.h, i->get_geometry().h);
+ }
+}
+
void Widget::set_geometry(const Geometry &g)
{
geom = g;
void set_position(int, int);
void set_size(unsigned, unsigned);
- virtual void autosize() { }
+ virtual void autosize();
void set_geometry(const Geometry &);
const Geometry &get_geometry() const { return geom; }
calling this function with a non-null parameter.
*/
void set_parent(Container *);
-
public:
+ Container *get_parent() const { return parent; }
+
/**
Sets the widget style. The final style name is constructed by concatenating
the widget class and the style name with a dash.