X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdropdown.cpp;h=e48303317b809601514ce71db7dded49dffbc068;hb=96df5b14a964da5a592a36e10a23022d5649462a;hp=cff5387408a2b7551d308f014fcf91f7e1f674c5;hpb=b92c878a286036af106e969a29b2689876aa5f65;p=libs%2Fgltk.git diff --git a/source/dropdown.cpp b/source/dropdown.cpp index cff5387..e483033 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #include "dropdown.h" #include "list.h" @@ -19,81 +12,75 @@ using namespace std; namespace Msp { namespace GLtk { -Dropdown::Dropdown(): - dropped(false) +Dropdown::Dropdown() { - 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; - - 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((font->get_ascent()-font->get_descent())*font_size); - geom.h = max(geom.h, line_height+margin.top+margin.bottom); - } + init(); } -void Dropdown::append(const string &item) +Dropdown::Dropdown(ListData &d): + list(d) { - list.append(item); + init(); } -void Dropdown::insert(unsigned i, const string &v) +void Dropdown::init() { - list.insert(i, v); -} + dropped = false; -void Dropdown::remove(unsigned i) -{ - list.remove(i); + add(list); + list.set_visible(false); + list.set_view_all(); + 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::clear() +void Dropdown::autosize_special(const Part &part, Geometry &ageom) const { - list.clear(); -} + if(part.get_name()=="list") + { + Geometry lgeom; + list.autosize(lgeom); + ageom.w = max(ageom.w, list.get_geometry().w); + } + else if(part.get_name()=="text") + { + const Sides &margin = part.get_margin(); + const GL::Font &font = style->get_font(); + float font_size = style->get_font_size(); -unsigned Dropdown::get_n_items() const -{ - return list.get_n_items(); -} + unsigned max_w = 0; + const ListData &data = list.get_data(); + for(unsigned i=0; i(font.get_string_width(data.get_string(i))*font_size); + max_w = max(max_w, w); + } + ageom.w = max(ageom.w, max_w+margin.left+margin.right); -void Dropdown::set_selected_index(int i) -{ - list.set_selected_index(i); + unsigned line_height = static_cast((font.get_ascent()-font.get_descent())*font_size); + ageom.h = max(ageom.h, line_height+margin.top+margin.bottom); + } } -const string &Dropdown::get_selected() const +void Dropdown::set_selected_index(int index) { - return list.get_selected(); + list.set_selected_index(index); + if(index<0) + text.set(string()); } -int Dropdown::get_selected_index() const +void Dropdown::rebuild_special(const Part &part) { - return list.get_selected_index(); + if(part.get_name()=="text") + text.build(part, state, geom, part_cache); + else + Widget::rebuild_special(part); } -void Dropdown::render_special(const Part &part) const +void Dropdown::render_special(const Part &part, GL::Renderer &renderer) 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(); + if(part.get_name()=="list" && dropped) + list.render(renderer); } void Dropdown::button_press(int x, int y, unsigned btn) @@ -104,38 +91,45 @@ void Dropdown::button_press(int x, int y, unsigned btn) if(!click_focus) { dropped = false; - state &= ~ACTIVE; + list.set_visible(false); + clear_state(ACTIVE); signal_ungrab_pointer.emit(); } } else if(btn==1) { dropped = true; - state |= ACTIVE; + list.set_visible(true); + resize_list(); + set_state(ACTIVE); signal_grab_pointer.emit(); } } void Dropdown::on_geometry_change() { - resize_list(); + if(dropped) + resize_list(); } void Dropdown::on_style_change() { - resize_list(); + text.set_style(style); + if(dropped) + resize_list(); } void Dropdown::list_autosize_changed() { - resize_list(); + if(dropped) + resize_list(); signal_autosize_changed.emit(); } void Dropdown::resize_list() { - list.autosize_all(); - Geometry lgeom = list.get_geometry(); + Geometry lgeom; + list.autosize(lgeom); lgeom.x = 0; lgeom.y = -lgeom.h; lgeom.w = max(geom.w, lgeom.w); @@ -149,7 +143,10 @@ void Dropdown::resize_list() { const Geometry &rgeom = root->get_geometry(); if(lgeom.h*2>rgeom.h) + { lgeom.h = rgeom.h/2; + lgeom.y = -lgeom.h; + } if(root_y+lgeom.y<0) lgeom.y = -root_y; if(root_y+lgeom.y+lgeom.h>rgeom.h) @@ -162,28 +159,31 @@ void Dropdown::resize_list() list.set_geometry(lgeom); } -void Dropdown::list_item_selected(unsigned index, const std::string &item) +void Dropdown::list_item_selected(unsigned index) { if(dropped) { dropped = false; - state &= ~ACTIVE; + clear_state(ACTIVE); signal_ungrab_pointer.emit(); } - signal_item_selected.emit(index, item); + text.set(list.get_data().get_string(index)); + + signal_item_selected.emit(index); + rebuild(); } Dropdown::Loader::Loader(Dropdown &d): - Widget::Loader(d) + DataFile::DerivedObjectLoader(d) { add("item", &Loader::item); } -void Dropdown::Loader::item(const string &str) +void Dropdown::Loader::item(const string &v) { - dynamic_cast(obj).append(str); + dynamic_cast &>(obj.list.get_data()).append(v); } } // namespace GLtk