X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdropdown.cpp;h=cab6d918e536210190a256b67b7965c0851c2f1b;hb=4ab33a06c9f8a85b193d7db8bc6ee9b8895aab09;hp=5901414f3a033a9ad786a537d70e93163840da4e;hpb=787dd5572e904a87686e7309401b86e0c528d823;p=libs%2Fgltk.git diff --git a/source/dropdown.cpp b/source/dropdown.cpp index 5901414..cab6d91 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -1,15 +1,9 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #include "dropdown.h" #include "list.h" #include "panel.h" #include "part.h" +#include "root.h" #include "style.h" #include "text.h" @@ -18,40 +12,50 @@ using namespace std; namespace Msp { namespace GLtk { -Dropdown::Dropdown(const Resources &r): - Widget(r), - Container(r), - list(r), +Dropdown::Dropdown(): dropped(false) { add(list); list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected)); + list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed)); +} + +void Dropdown::autosize() +{ + if(!style) + return; - update_style(); + 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 = style->get_font_size(); + unsigned line_height = static_cast((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); - resize_list(); } void Dropdown::insert(unsigned i, const string &v) { list.insert(i, v); - resize_list(); } void Dropdown::remove(unsigned i) { list.remove(i); - resize_list(); } void Dropdown::clear() { list.clear(); - resize_list(); } unsigned Dropdown::get_n_items() const @@ -74,6 +78,17 @@ int Dropdown::get_selected_index() const return list.get_selected_index(); } +void Dropdown::render_special(const Part &part) const +{ + if(part.get_name()=="text") + { + if(list.get_selected_index()>=0) + Text(*style, list.get_selected()).render(part, geom); + } + else if(part.get_name()=="list" && dropped) + list.render(); +} + void Dropdown::button_press(int x, int y, unsigned btn) { if(dropped) @@ -94,35 +109,50 @@ void Dropdown::button_press(int x, int y, unsigned btn) } } -void Dropdown::render_special(const Part &part) const +void Dropdown::on_geometry_change() { - if(part.get_name()=="text") - { - if(list.get_selected_index()>=0) - Text(*style, list.get_selected()).render(part, geom); - } - else if(part.get_name()=="list" && dropped) - list.render(); + resize_list(); } -void Dropdown::on_geometry_change() +void Dropdown::on_style_change() +{ + resize_list(); +} + +void Dropdown::list_autosize_changed() { resize_list(); + signal_autosize_changed.emit(); } void Dropdown::resize_list() { - // XXX This is a hack. - unsigned n_items = list.get_n_items(); - const Style &stl = list.get_style(); - const GL::Font &font = *stl.get_font(); - unsigned h = min(max(n_items, 1U), 10U)*static_cast((font.get_ascent()-font.get_descent())*font.get_default_size()); - if(const Part *items_part = stl.get_part("items")) + 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()) { - const Sides &margin = items_part->get_margin(); - h += margin.top+margin.bottom; + root_x += p->get_geometry().x; + root_y += p->get_geometry().y; + if(Root *root = dynamic_cast(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(Geometry(0, -h, geom.w, h)); + list.set_geometry(lgeom); } void Dropdown::list_item_selected(unsigned index, const std::string &item) @@ -146,7 +176,7 @@ Dropdown::Loader::Loader(Dropdown &d): void Dropdown::Loader::item(const string &str) { - dynamic_cast(wdg).append(str); + dynamic_cast(obj).append(str); } } // namespace GLtk