X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fdropdown.cpp;h=b00438e54c40ef7bc21dfb887ecd1945229a72bb;hb=73afd124ab87e8bace98db55517a56c797a9b8c7;hp=e0710a864c085cc52d67b7b4fd8d9cb2c1100078;hpb=900becb7550d179d169e7d049b3aef5e0c9b7758;p=libs%2Fgltk.git diff --git a/source/dropdown.cpp b/source/dropdown.cpp index e0710a8..b00438e 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -18,16 +18,11 @@ 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)); - - update_style(); } void Dropdown::append(const string &item) @@ -74,6 +69,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) @@ -81,57 +87,42 @@ void Dropdown::button_press(int x, int y, unsigned btn) Container::button_press(x, y, btn); if(!click_focus) { - dropped=false; - state&=~ACTIVE; + dropped = false; + state &= ~ACTIVE; signal_ungrab_pointer.emit(); } } else if(btn==1) { - dropped=true; - state|=ACTIVE; + dropped = true; + state |= ACTIVE; signal_grab_pointer.emit(); } } -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::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()); - for(std::list::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i) - if(i->get_name()=="items") - { - const Sides &margin=i->get_margin(); - h+=margin.top+margin.bottom; - } - list.set_geometry(Geometry(0, -h, geom.w, h)); + list.autosize(); + const Geometry &lgeom = list.get_geometry(); + list.set_geometry(Geometry(0, -lgeom.h, max(geom.w, lgeom.w), lgeom.h)); } void Dropdown::list_item_selected(unsigned index, const std::string &item) { if(dropped) { - dropped=false; - state&=~ACTIVE; + dropped = false; + state &= ~ACTIVE; signal_ungrab_pointer.emit(); } @@ -147,7 +138,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