X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Ftoggle.cpp;h=6d2a10b16644d6254163b53b26c6f92ead7754ce;hb=c8f5fd14a1fbdaaa9e1216dd5163d1f5c1b5ff27;hp=62f46eb52ac94b0ac212535c720a82937c45b13c;hpb=a87d05583cb7dffaf0e0f5eb9f9b2fc0bcf656e1;p=libs%2Fgltk.git diff --git a/source/toggle.cpp b/source/toggle.cpp index 62f46eb..6d2a10b 100644 --- a/source/toggle.cpp +++ b/source/toggle.cpp @@ -1,12 +1,6 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include "panel.h" #include "part.h" +#include "style.h" #include "toggle.h" using namespace std; @@ -14,46 +8,75 @@ using namespace std; namespace Msp { namespace GLtk { -Toggle::Toggle(const Resources &r, const string &t): - Widget(r), - text(style), +Toggle::Toggle(const string &t): + text(), pressed(false), value(false), exclusive(false) { - update_style(); 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; + text = t; + signal_autosize_changed.emit(); } void Toggle::set_exclusive(bool e) { - exclusive=e; + exclusive = e; if(exclusive && value) exclude_siblings(); } +void Toggle::exclude_siblings() +{ + const list &siblings = parent->get_children(); + for(list::const_iterator i=siblings.begin(); i!=siblings.end(); ++i) + if(Toggle *tgl = dynamic_cast(*i)) + if(tgl!=this && tgl->get_exclusive() && tgl->get_value()) + tgl->set_value(false); +} + void Toggle::set_value(bool v) { - value=v; + value = v; if(value) { - state|=ACTIVE; + state |= ACTIVE; if(exclusive && parent) exclude_siblings(); } else - state&=~ACTIVE; + state &= ~ACTIVE; +} + +void Toggle::render_special(const Part &part) const +{ + if(part.get_name()=="text") + text.render(part, geom); } void Toggle::button_press(int, int, unsigned btn) { if(btn==1) - pressed=true; + pressed = true; } void Toggle::button_release(int x, int y, unsigned btn) @@ -66,28 +89,13 @@ void Toggle::button_release(int x, int y, unsigned btn) signal_toggled.emit(value); } - pressed=false; + pressed = false; } } -void Toggle::render_special(const Part &part) const -{ - if(part.get_name()=="text") - text.render(part, geom); -} - -void Toggle::exclude_siblings() -{ - const list &siblings=parent->get_children(); - for(list::const_iterator i=siblings.begin(); i!=siblings.end(); ++i) - if(Toggle *tgl=dynamic_cast(*i)) - if(tgl!=this && tgl->get_exclusive() && tgl->get_value()) - tgl->set_value(false); -} - void Toggle::on_style_change() { - text.update_style(); + text.set_style(style); } @@ -101,21 +109,21 @@ Toggle::Loader::Loader(Toggle &t): Toggle &Toggle::Loader::get_object() const { - return static_cast(wdg); + return static_cast(obj); } void Toggle::Loader::finish() { - Toggle &tgl=static_cast(wdg); + Toggle &tgl = get_object(); if(tgl.value) - tgl.state|=ACTIVE; + tgl.state |= ACTIVE; else - tgl.state&=~ACTIVE; + tgl.state &= ~ACTIVE; } void Toggle::Loader::text(const string &t) { - static_cast(wdg).text=t; + get_object().text = t; } } // namespace GLtk