]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add Toggle widget
authorMikko Rasa <tdb@tdb.fi>
Wed, 26 Dec 2007 14:21:05 +0000 (14:21 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 26 Dec 2007 14:21:05 +0000 (14:21 +0000)
Only process events in Root if visible

source/panel.cpp
source/root.cpp
source/toggle.cpp [new file with mode: 0644]
source/toggle.h [new file with mode: 0644]
source/widget.cpp

index 9373ff2ebd7559fecccc0e058a140a1c647ca55a..22ccb1b97fbe9bde56fd38186e7233157f698f46 100644 (file)
@@ -14,6 +14,7 @@ Distributed under the LGPL
 #include "list.h"
 #include "panel.h"
 #include "part.h"
+#include "toggle.h"
 #include "vslider.h"
 
 using namespace std;
@@ -197,14 +198,15 @@ Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        pnl(p),
        wdg_map(m)
 {
-       add("button", &Loader::child<Button>);
-       add("entry",  &Loader::child<Entry>);
-       add("hslider", &Loader::child<HSlider>);
+       add("button",    &Loader::child<Button>);
+       add("entry",     &Loader::child<Entry>);
+       add("hslider",   &Loader::child<HSlider>);
        add("indicator", &Loader::child<Indicator>);
-       add("label",  &Loader::child<Label>);
-       add("list",   &Loader::child<List>);
-       add("panel",  &Loader::panel);
-       add("vslider", &Loader::child<VSlider>);
+       add("label",     &Loader::child<Label>);
+       add("list",      &Loader::child<List>);
+       add("panel",     &Loader::panel);
+       add("toggle",    &Loader::child<Toggle>);
+       add("vslider",   &Loader::child<VSlider>);
 }
 
 template<typename T>
index bb3913c73fbf5db85fc85217a98dce872417666a..224e5bc30ef3a975fb022da83a6ba19e4be64779 100644 (file)
@@ -21,26 +21,47 @@ Root::Root(Resources &r, Graphics::Window &w):
        window.signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
        window.signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
        window.signal_pointer_motion.connect(sigc::mem_fun(this, &Root::pointer_motion_event));
-       window.signal_key_press.connect(sigc::mem_fun(this, &Root::key_press));
-       window.signal_key_release.connect(sigc::mem_fun(this, &Root::key_release));
+       window.signal_key_press.connect(sigc::mem_fun(this, &Root::key_press_event));
+       window.signal_key_release.connect(sigc::mem_fun(this, &Root::key_release_event));
 }
 
 void Root::button_press_event(int x, int y, unsigned btn, unsigned)
 {
-       translate_coords(x, y);
-       button_press(x, y, btn);
+       if(visible)
+       {
+               translate_coords(x, y);
+               button_press(x, y, btn);
+       }
 }
 
 void Root::button_release_event(int x, int y, unsigned btn, unsigned)
 {
-       translate_coords(x, y);
-       button_release(x, y, btn);
+       if(visible)
+       {
+               translate_coords(x, y);
+               button_release(x, y, btn);
+       }
 }
 
 void Root::pointer_motion_event(int x, int y)
 {
-       translate_coords(x, y);
-       pointer_motion(x, y);
+       if(visible)
+       {
+               translate_coords(x, y);
+               pointer_motion(x, y);
+       }
+}
+
+void Root::key_press_event(unsigned key, unsigned mod, wchar_t ch)
+{
+       if(visible)
+               key_press(key, mod, ch);
+}
+
+void Root::key_release_event(unsigned key, unsigned mod)
+{
+       if(visible)
+               key_release(key, mod);
 }
 
 void Root::translate_coords(int &x, int &y)
diff --git a/source/toggle.cpp b/source/toggle.cpp
new file mode 100644 (file)
index 0000000..2e3488a
--- /dev/null
@@ -0,0 +1,68 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "part.h"
+#include "toggle.h"
+
+namespace Msp {
+namespace GLtk {
+
+Toggle::Toggle(const Resources &r):
+       Widget(r),
+       value(false)
+{
+       update_style();
+}
+
+void Toggle::button_release(int x, int y, unsigned btn)
+{
+       if(btn==1)
+       {
+               if(geom.is_inside_relative(x, y))
+               {
+                       value=!value;
+                       state=(value ? ACTIVE : HOVER);
+                       signal_toggled.emit(value);
+               }
+               else
+                       state=NORMAL;
+       }
+}
+
+void Toggle::pointer_enter()
+{
+       if(!value)
+               state=HOVER;
+}
+
+void Toggle::pointer_leave()
+{
+       if(!value)
+               state=NORMAL;
+}
+
+void Toggle::render_special(const Part &part) const
+{
+       if(part.get_name()=="text")
+               render_text(part, text);
+}
+
+
+Toggle::Loader::Loader(Toggle &t):
+       Widget::Loader(t)
+{
+       add("text",  &Toggle::text);
+       add("value", &Toggle::value);
+}
+
+Toggle &Toggle::Loader::get_object() const
+{
+       return static_cast<Toggle &>(wdg);
+}
+
+} // namespace GLtk
+} // namespace Msp
diff --git a/source/toggle.h b/source/toggle.h
new file mode 100644 (file)
index 0000000..305c3ca
--- /dev/null
@@ -0,0 +1,52 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GLTK_TOGGLE_H_
+#define MSP_GLTK_TOGGLE_H_
+
+#include <sigc++/signal.h>
+#include "widget.h"
+
+namespace Msp {
+namespace GLtk {
+
+/**
+Allows toggling a value between two states.
+*/
+class Toggle: public Widget
+{
+public:
+       class Loader: public Widget::Loader
+       {
+       public:
+               Loader(Toggle &);
+               Toggle &get_object() const;
+       };
+
+private:
+       std::string text;
+       bool value;
+
+public:
+       sigc::signal<void, bool> signal_toggled;
+
+       Toggle(const Resources &);
+
+       bool get_value() const { return value; }
+
+       virtual void button_release(int, int, unsigned);
+       virtual void pointer_enter();
+       virtual void pointer_leave();
+private:
+       virtual const char *get_class() const { return "toggle"; }
+       virtual void render_special(const Part &) const;
+};
+
+} // namespace GLtk
+} // namespace Msp
+
+#endif
index 7d608f2ce8d32980505f3fde37e8df91a3bb35de..0128367c2b32a6a6c54ae458b3d3a994b38cdf2e 100644 (file)
@@ -7,6 +7,7 @@ Distributed under the LGPL
 
 #include <msp/gl/matrix.h>
 #include <msp/gl/transform.h>
+#include <msp/strings/formatter.h>
 #include "panel.h"
 #include "resources.h"
 #include "widget.h"
@@ -70,7 +71,7 @@ void Widget::set_visible(bool v)
 void Widget::render() const
 {
        if(!style)
-               throw InvalidState("Attempt to render a widget without a style");
+               throw InvalidState(format("Attempt to render a widget without a style (class=\"%s\")", get_class()));
 
        GL::push_matrix();
        GL::translate(geom.x, geom.y, 0);