From 2bdaf4955fdb94e73704adcdcf0adc2b353f0ff0 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 2 Feb 2011 12:05:28 +0000 Subject: [PATCH] Rearrange members Make Container a friend of Widget and get rid of the static helpers --- source/button.cpp | 58 ++++++++++++++++----------------- source/button.h | 16 ++++++---- source/container.cpp | 18 +++++------ source/container.h | 6 ++-- source/dialog.h | 7 ++-- source/dropdown.cpp | 24 +++++++------- source/dropdown.h | 16 ++++++---- source/entry.cpp | 66 +++++++++++++++++++------------------- source/entry.h | 16 ++++++---- source/hslider.cpp | 38 +++++++++++----------- source/hslider.h | 12 ++++--- source/image.h | 6 ++-- source/indicator.h | 7 ++-- source/label.h | 5 +-- source/list.cpp | 38 +++++++++++----------- source/list.h | 15 +++++---- source/panel.cpp | 32 +++++++++---------- source/panel.h | 17 +++++++--- source/slider.h | 10 +++--- source/toggle.cpp | 32 +++++++++---------- source/toggle.h | 20 +++++++----- source/vslider.cpp | 37 +++++++++++---------- source/vslider.h | 12 ++++--- source/widget.cpp | 76 +++++++++++++++++++------------------------- source/widget.h | 51 ++++++++++++++--------------- 25 files changed, 330 insertions(+), 305 deletions(-) diff --git a/source/button.cpp b/source/button.cpp index 8e6e713..e81fe02 100644 --- a/source/button.cpp +++ b/source/button.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 */ @@ -30,6 +30,34 @@ void Button::set_icon(const GL::Texture2D *i) icon = i; } +void Button::render_special(const Part &part) const +{ + if(part.get_name()=="text") + text.render(part, geom); + if(part.get_name()=="icon" && icon) + { + Geometry rgeom; + rgeom.w = icon->get_width(); + rgeom.h = icon->get_height(); + part.get_alignment().apply(rgeom, geom, part.get_margin()); + + icon->bind(); + GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2)); + imm.color(1.0f, 1.0f, 1.0f); + imm.begin(GL::QUADS); + imm.texcoord(0, 0); + imm.vertex(rgeom.x, rgeom.y); + imm.texcoord(1, 0); + imm.vertex(rgeom.x+rgeom.w, rgeom.y); + imm.texcoord(1, 1); + imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h); + imm.texcoord(0, 1); + imm.vertex(rgeom.x, rgeom.y+rgeom.h); + imm.end(); + GL::Texture::unbind(); + } +} + void Button::button_press(int, int, unsigned btn) { if(btn==1) @@ -62,34 +90,6 @@ void Button::pointer_motion(int x, int y) } } -void Button::render_special(const Part &part) const -{ - if(part.get_name()=="text") - text.render(part, geom); - if(part.get_name()=="icon" && icon) - { - Geometry rgeom; - rgeom.w = icon->get_width(); - rgeom.h = icon->get_height(); - part.get_alignment().apply(rgeom, geom, part.get_margin()); - - icon->bind(); - GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2)); - imm.color(1.0f, 1.0f, 1.0f); - imm.begin(GL::QUADS); - imm.texcoord(0, 0); - imm.vertex(rgeom.x, rgeom.y); - imm.texcoord(1, 0); - imm.vertex(rgeom.x+rgeom.w, rgeom.y); - imm.texcoord(1, 1); - imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h); - imm.texcoord(0, 1); - imm.vertex(rgeom.x, rgeom.y+rgeom.h); - imm.end(); - GL::Texture::unbind(); - } -} - void Button::on_style_change() { text.set_style(style); diff --git a/source/button.h b/source/button.h index b84511e..8d4c3c6 100644 --- a/source/button.h +++ b/source/button.h @@ -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 */ @@ -31,25 +31,29 @@ public: void text(const std::string &); }; + sigc::signal signal_clicked; + private: Text text; const GL::Texture2D *icon; bool pressed; public: - sigc::signal signal_clicked; - Button(const std::string & = std::string()); + virtual const char *get_class() const { return "button"; } + void set_text(const std::string &); void set_icon(const GL::Texture2D *); + +private: + virtual void render_special(const Part &) const; + +public: virtual void button_press(int, int, unsigned); virtual void button_release(int, int, unsigned); virtual void pointer_motion(int, int); private: - virtual const char *get_class() const { return "button"; } - virtual void render_special(const Part &) const; - virtual void on_style_change(); }; diff --git a/source/container.cpp b/source/container.cpp index b2b8c56..9ad6648 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -25,7 +25,7 @@ Container::~Container() void Container::add(Widget &wdg) { - set_parent(wdg, this); + wdg.set_parent(this); children.push_back(create_child(&wdg)); } @@ -34,7 +34,7 @@ void Container::remove(Widget &wdg) for(list::iterator i=children.begin(); i!=children.end(); ++i) if((*i)->widget==&wdg) { - set_parent(wdg, 0); + wdg.set_parent(0); delete *i; children.erase(i); return; @@ -43,6 +43,11 @@ void Container::remove(Widget &wdg) throw InvalidState("That Widget is not in this Container"); } +Container::Child *Container::create_child(Widget *wdg) +{ + return new Child(*this, wdg); +} + list Container::get_children() const { list result; @@ -139,18 +144,13 @@ void Container::pointer_leave() click_focus = 0; } -Container::Child *Container::create_child(Widget *wdg) -{ - return new Child(*this, wdg); -} - void Container::on_reparent() { for(list::iterator i=children.begin(); i!=children.end(); ++i) { if(Container *c = dynamic_cast((*i)->widget)) c->on_reparent(); - update_style(*(*i)->widget); + (*i)->widget->update_style(); } } diff --git a/source/container.h b/source/container.h index 0384521..ddccb06 100644 --- a/source/container.h +++ b/source/container.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -39,6 +39,9 @@ public: void add(Widget &); void remove(Widget &); +protected: + virtual Child *create_child(Widget *); +public: std::list get_children() const; Widget *get_child_at(int, int); Widget *get_descendant_at(int, int); @@ -48,7 +51,6 @@ public: virtual void pointer_motion(int, int); virtual void pointer_leave(); protected: - virtual Child *create_child(Widget *); virtual void on_reparent(); }; diff --git a/source/dialog.h b/source/dialog.h index 89065dc..eb2d176 100644 --- a/source/dialog.h +++ b/source/dialog.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -21,12 +21,13 @@ Dialog's action buttons are clicked, it will emit a signal and delete itself. */ class Dialog: public Panel { +public: + sigc::signal signal_response; + private: bool stale; public: - sigc::signal signal_response; - Dialog(); /** Adds an action button to the dialog. Pressing the button will invoke diff --git a/source/dropdown.cpp b/source/dropdown.cpp index bcd5019..e9634f5 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 */ @@ -69,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) @@ -89,17 +100,6 @@ void Dropdown::button_press(int x, int y, unsigned btn) } } -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::on_geometry_change() { resize_list(); diff --git a/source/dropdown.h b/source/dropdown.h index 2fe82d0..51ba724 100644 --- a/source/dropdown.h +++ b/source/dropdown.h @@ -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 */ @@ -28,15 +28,17 @@ public: void item(const std::string &); }; + sigc::signal signal_item_selected; + private: List list; bool dropped; public: - sigc::signal signal_item_selected; - Dropdown(); + virtual const char *get_class() const { return "dropdown"; } + void append(const std::string &); void insert(unsigned, const std::string &); void remove(unsigned); @@ -47,16 +49,16 @@ public: const std::string &get_selected() const; int get_selected_index() const; - virtual void button_press(int, int, unsigned); - private: - virtual const char *get_class() const { return "dropdown"; } virtual void render_special(const Part &) const; +public: + virtual void button_press(int, int, unsigned); +private: virtual void on_geometry_change(); virtual void on_style_change(); - void resize_list(); + void resize_list(); void list_item_selected(unsigned, const std::string &); }; diff --git a/source/entry.cpp b/source/entry.cpp index 2c9cb35..271c49a 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -58,6 +58,32 @@ void Entry::set_multiline(bool m) } } +void Entry::render_special(const Part &part) const +{ + if(part.get_name()=="text") + text.render(part, geom, first_row); + else if(part.get_name()=="cursor") + { + if(!text_part || !part.get_graphic(state)) + return; + + unsigned row, col; + text.offset_to_coords(edit_pos, row, col); + + if(row=first_row+visible_rows) + return; + + Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col); + + GL::push_matrix(); + GL::translate(rgeom.x, rgeom.y, 0); + part.get_graphic(state)->render(part.get_geometry().w, part.get_geometry().h); + GL::pop_matrix(); + } + else if(part.get_name()=="slider") + slider->render(); +} + void Entry::key_press(unsigned key, unsigned, wchar_t ch) { if(key==Input::KEY_LEFT) @@ -120,32 +146,6 @@ void Entry::key_press(unsigned key, unsigned, wchar_t ch) } } -void Entry::render_special(const Part &part) const -{ - if(part.get_name()=="text") - text.render(part, geom, first_row); - else if(part.get_name()=="cursor") - { - if(!text_part || !part.get_graphic(state)) - return; - - unsigned row, col; - text.offset_to_coords(edit_pos, row, col); - - if(row=first_row+visible_rows) - return; - - Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col); - - GL::push_matrix(); - GL::translate(rgeom.x, rgeom.y, 0); - part.get_graphic(state)->render(part.get_geometry().w, part.get_geometry().h); - GL::pop_matrix(); - } - else if(part.get_name()=="slider") - slider->render(); -} - void Entry::on_geometry_change() { reposition_slider(); @@ -185,12 +185,6 @@ void Entry::reposition_slider() } } -void Entry::slider_value_changed(double value) -{ - if(text.get_n_lines()>visible_rows) - first_row = text.get_n_lines()-visible_rows-static_cast(value); -} - void Entry::check_view_range() { if(!multiline || !text_part) @@ -219,6 +213,12 @@ void Entry::check_view_range() } } +void Entry::slider_value_changed(double value) +{ + if(text.get_n_lines()>visible_rows) + first_row = text.get_n_lines()-visible_rows-static_cast(value); +} + Entry::Loader::Loader(Entry &ent): Widget::Loader(ent) diff --git a/source/entry.h b/source/entry.h index 5327064..bc4a26b 100644 --- a/source/entry.h +++ b/source/entry.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -35,6 +35,8 @@ public: Loader(Entry &); }; + sigc::signal signal_enter; + private: Text text; bool multiline; @@ -45,25 +47,27 @@ private: VSlider *slider; public: - sigc::signal signal_enter; - Entry(const std::string & = std::string()); + virtual const char *get_class() const { return "entry"; } + void set_text(const std::string &); const std::string &get_text() const { return text.get(); } void set_multiline(bool); bool is_multiline() const { return multiline; } - virtual void key_press(unsigned, unsigned, wchar_t); private: - virtual const char *get_class() const { return "entry"; } virtual void render_special(const Part &) const; +public: + virtual void key_press(unsigned, unsigned, wchar_t); +private: virtual void on_geometry_change(); virtual void on_style_change(); + void reposition_slider(); - void slider_value_changed(double); void check_view_range(); + void slider_value_changed(double); }; } // namespace GLtk diff --git a/source/hslider.cpp b/source/hslider.cpp index a4378f0..e86cc4b 100644 --- a/source/hslider.cpp +++ b/source/hslider.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008, 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -19,6 +19,24 @@ HSlider::HSlider(): slider_size(1) { } +void HSlider::render_special(const Part &part) const +{ + if(part.get_name()=="slider") + { + Alignment align = part.get_alignment(); + if(max>min) + align.x = (value-min)/(max-min); + + Geometry pgeom = part.get_geometry(); + align.apply(pgeom, geom, part.get_margin()); + + GL::push_matrix(); + GL::translate(pgeom.x, pgeom.y, 0); + part.get_graphic(state)->render(pgeom.w, pgeom.h); + GL::pop_matrix(); + } +} + void HSlider::button_press(int x, int y, unsigned btn) { if(btn==1 && geom.is_inside_relative(x, y) && max>min) @@ -45,24 +63,6 @@ void HSlider::pointer_motion(int x, int) drag(x); } -void HSlider::render_special(const Part &part) const -{ - if(part.get_name()=="slider") - { - Alignment align = part.get_alignment(); - if(max>min) - align.x = (value-min)/(max-min); - - Geometry pgeom = part.get_geometry(); - align.apply(pgeom, geom, part.get_margin()); - - GL::push_matrix(); - GL::translate(pgeom.x, pgeom.y, 0); - part.get_graphic(state)->render(pgeom.w, pgeom.h); - GL::pop_matrix(); - } -} - void HSlider::on_geometry_change() { drag_area_size = geom.w-slider_size; diff --git a/source/hslider.h b/source/hslider.h index 4706699..493f00c 100644 --- a/source/hslider.h +++ b/source/hslider.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -24,13 +24,17 @@ private: public: HSlider(); + + virtual const char *get_class() const { return "hslider"; } + +private: + virtual void render_special(const Part &) const; + +public: virtual void button_press(int, int, unsigned); virtual void button_release(int, int, unsigned); virtual void pointer_motion(int, int); private: - virtual const char *get_class() const { return "hslider"; } - virtual void render_special(const Part &) const; - virtual void on_geometry_change(); virtual void on_style_change(); }; diff --git a/source/image.h b/source/image.h index 8256a8d..efc8144 100644 --- a/source/image.h +++ b/source/image.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -26,10 +26,12 @@ private: public: Image(const GL::Texture2D * = 0); + virtual const char *get_class() const { return "image"; } + void set_image(const GL::Texture2D *); void set_keep_aspect(bool); + private: - virtual const char *get_class() const { return "image"; } virtual void render_special(const Part &) const; }; diff --git a/source/indicator.h b/source/indicator.h index ce95151..fdc83b4 100644 --- a/source/indicator.h +++ b/source/indicator.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -20,9 +20,10 @@ class Indicator: public Widget { public: Indicator(); - void set_active(bool); -private: + virtual const char *get_class() const { return "indicator"; } + + void set_active(bool); }; } // namespace GLtk diff --git a/source/label.h b/source/label.h index 87835da..20219c4 100644 --- a/source/label.h +++ b/source/label.h @@ -1,7 +1,7 @@ /* $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 */ @@ -34,11 +34,12 @@ private: public: Label(const std::string & = std::string()); + virtual const char *get_class() const { return "label"; } + virtual void autosize(); void set_text(const std::string &); private: - virtual const char *get_class() const { return "label"; } virtual void render_special(const Part &) const; virtual void on_style_change(); diff --git a/source/list.cpp b/source/list.cpp index 672cf49..ac8b48c 100644 --- a/source/list.cpp +++ b/source/list.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 */ @@ -114,24 +114,6 @@ const string &List::get_selected() const return items[sel_index]; } -void List::button_press(int x, int y, unsigned btn) -{ - Container::button_press(x, y, btn); - if(!click_focus && btn==1) - { - if(items_part) - y += items_part->get_margin().top; - - unsigned i = (geom.h-1-y)/row_height; - if(iget_margin().top; + + unsigned i = (geom.h-1-y)/row_height; + if(i signal_item_selected; + private: std::vector items; int sel_index; @@ -42,10 +44,10 @@ private: VSlider slider; public: - sigc::signal signal_item_selected; - List(); + virtual const char *get_class() const { return "list"; } + virtual void autosize(); void append(const std::string &); @@ -58,14 +60,15 @@ public: const std::string &get_selected() const; int get_selected_index() const { return sel_index; } - virtual void button_press(int, int, unsigned); - private: - virtual const char *get_class() const { return "list"; } virtual void render_special(const Part &) const; +public: + virtual void button_press(int, int, unsigned); +private: virtual void on_geometry_change(); virtual void on_style_change(); + void reposition_slider(); void check_view_range(); void slider_value_changed(double); diff --git a/source/panel.cpp b/source/panel.cpp index f0c1c7c..0ecdf10 100644 --- a/source/panel.cpp +++ b/source/panel.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 */ @@ -31,6 +31,11 @@ Panel::Panel(): input_focus(0) { } +Panel::Child *Panel::create_child(Widget *wdg) +{ + return new Child(*this, wdg); +} + void Panel::raise(Widget &wdg) { for(list::iterator i=children.begin(); i!=children.end(); ++i) @@ -54,6 +59,16 @@ Widget *Panel::get_final_input_focus() const return input_focus; } +void Panel::render_special(const Part &part) const +{ + if(part.get_name()=="children") + { + for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + if((*i)->widget->is_visible()) + (*i)->widget->render(); + } +} + void Panel::button_press(int x, int y, unsigned btn) { if(pointer_grabbed) @@ -121,21 +136,6 @@ void Panel::focus_out() set_input_focus(0); } -void Panel::render_special(const Part &part) const -{ - if(part.get_name()=="children") - { - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) - if((*i)->widget->is_visible()) - (*i)->widget->render(); - } -} - -Panel::Child *Panel::create_child(Widget *wdg) -{ - return new Child(*this, wdg); -} - void Panel::set_pointer_focus(Widget *wdg) { if(wdg!=pointer_focus) diff --git a/source/panel.h b/source/panel.h index dc98416..748ecb4 100644 --- a/source/panel.h +++ b/source/panel.h @@ -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 */ @@ -57,10 +57,20 @@ protected: public: Panel(); + virtual const char *get_class() const { return "panel"; } + +protected: + virtual Child *create_child(Widget *); + +public: void raise(Widget &); Widget *get_input_focus() const { return input_focus; } Widget *get_final_input_focus() const; +protected: + virtual void render_special(const Part &) const; + +public: virtual void button_press(int, int, unsigned); virtual void button_release(int, int, unsigned); virtual void pointer_motion(int, int); @@ -68,11 +78,8 @@ public: virtual void key_press(unsigned, unsigned, wchar_t); virtual void key_release(unsigned, unsigned); virtual void focus_out(); -protected: - virtual const char *get_class() const { return "panel"; } - virtual void render_special(const Part &) const; - virtual Child *create_child(Widget *); +protected: void set_pointer_focus(Widget *); void set_input_focus(Widget *); }; diff --git a/source/slider.h b/source/slider.h index c4326e3..edaa5d8 100644 --- a/source/slider.h +++ b/source/slider.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -29,6 +29,8 @@ public: Slider &get_object() const; }; + sigc::signal signal_value_changed; + protected: double min, max; double value; @@ -39,16 +41,14 @@ protected: double drag_start_value; unsigned drag_area_size; -public: - sigc::signal signal_value_changed; - -protected: Slider(); + public: void set_value(double); void set_range(double, double); void set_step(double); double get_value() const { return value; } + protected: void start_drag(int); void drag(int); diff --git a/source/toggle.cpp b/source/toggle.cpp index a950b97..7edbbb0 100644 --- a/source/toggle.cpp +++ b/source/toggle.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -35,6 +35,15 @@ void Toggle::set_exclusive(bool e) 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; @@ -48,6 +57,12 @@ void Toggle::set_value(bool v) 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) @@ -68,21 +83,6 @@ void Toggle::button_release(int x, int y, unsigned btn) } } -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.set_style(style); diff --git a/source/toggle.h b/source/toggle.h index 24dfe48..ff65440 100644 --- a/source/toggle.h +++ b/source/toggle.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -31,6 +31,8 @@ public: void text(const std::string &); }; + sigc::signal signal_toggled; + private: Text text; bool pressed; @@ -38,24 +40,26 @@ private: bool exclusive; public: - sigc::signal signal_toggled; - Toggle(const std::string & = std::string()); + virtual const char *get_class() const { return "toggle"; } + void set_text(const std::string &); void set_exclusive(bool); bool get_exclusive() const { return exclusive; } +private: + void exclude_siblings(); +public: void set_value(bool); bool get_value() const { return value; } - virtual void button_press(int, int, unsigned); - virtual void button_release(int, int, unsigned); private: - virtual const char *get_class() const { return "toggle"; } virtual void render_special(const Part &) const; - void exclude_siblings(); - +public: + virtual void button_press(int, int, unsigned); + virtual void button_release(int, int, unsigned); +private: virtual void on_style_change(); }; diff --git a/source/vslider.cpp b/source/vslider.cpp index cdd2675..65cac69 100644 --- a/source/vslider.cpp +++ b/source/vslider.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008, 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -17,7 +17,24 @@ namespace GLtk { VSlider::VSlider(): slider_size(1) +{ } + +void VSlider::render_special(const Part &part) const { + if(part.get_name()=="slider") + { + Alignment align = part.get_alignment(); + if(max>min) + align.y = (value-min)/(max-min); + + Geometry pgeom = part.get_geometry(); + align.apply(pgeom, geom, part.get_margin()); + + GL::push_matrix(); + GL::translate(pgeom.x, pgeom.y, 0); + part.get_graphic(state)->render(pgeom.w, pgeom.h); + GL::pop_matrix(); + } } void VSlider::button_press(int x, int y, unsigned btn) @@ -46,24 +63,6 @@ void VSlider::pointer_motion(int, int y) drag(y); } -void VSlider::render_special(const Part &part) const -{ - if(part.get_name()=="slider") - { - Alignment align = part.get_alignment(); - if(max>min) - align.y = (value-min)/(max-min); - - Geometry pgeom = part.get_geometry(); - align.apply(pgeom, geom, part.get_margin()); - - GL::push_matrix(); - GL::translate(pgeom.x, pgeom.y, 0); - part.get_graphic(state)->render(pgeom.w, pgeom.h); - GL::pop_matrix(); - } -} - void VSlider::on_geometry_change() { drag_area_size = geom.h-slider_size; diff --git a/source/vslider.h b/source/vslider.h index 2d2ac58..5c5e61f 100644 --- a/source/vslider.h +++ b/source/vslider.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -20,13 +20,17 @@ private: public: VSlider(); + + virtual const char *get_class() const { return "vslider"; } + +private: + virtual void render_special(const Part &) const; + +public: virtual void button_press(int, int, unsigned); virtual void button_release(int, int, unsigned); virtual void pointer_motion(int, int); private: - virtual const char *get_class() const { return "vslider"; } - virtual void render_special(const Part &) const; - virtual void on_geometry_change(); virtual void on_style_change(); }; diff --git a/source/widget.cpp b/source/widget.cpp index 434ec45..9cd9066 100644 --- a/source/widget.cpp +++ b/source/widget.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 */ @@ -53,12 +53,44 @@ void Widget::set_geometry(const Geometry &g) on_geometry_change(); } +void Widget::set_parent(Container *p) +{ + if(parent && p) + throw InvalidState("Widget is already in a Container"); + parent = p; + + on_reparent(); + update_style(); +} + void Widget::set_style(const string &s) { style_name = s; update_style(); } +void Widget::update_style() +{ + Widget *top; + for(top=this; top->parent; top=top->parent) ; + Root *root = dynamic_cast(top); + if(!root) + style = 0; + else + { + string sname = get_class(); + if(!style_name.empty()) + { + sname += '-'; + sname += style_name; + } + + style = root->get_resources().get