From aab3c89d03c1a99cb91ff1870775b2c44806bb79 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 5 Jul 2009 20:29:10 +0000 Subject: [PATCH] Add icon support to Button Make Root take const Resources & Propatage pointer grabs up the widget stack --- source/button.cpp | 31 ++++++++++++++++++++++++++++++- source/button.h | 5 ++++- source/panel.cpp | 8 ++++++++ source/root.cpp | 2 +- source/root.h | 2 +- 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/source/button.cpp b/source/button.cpp index f6cf56c..ec99016 100644 --- a/source/button.cpp +++ b/source/button.cpp @@ -1,10 +1,11 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2009 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #include "button.h" #include "part.h" @@ -13,6 +14,7 @@ namespace GLtk { Button::Button(const Resources &r, const std::string &t): Widget(r), + icon(0), pressed(false) { set_text(t); @@ -24,6 +26,11 @@ void Button::set_text(const std::string &t) text=t; } +void Button::set_icon(const GL::Texture2D *i) +{ + icon=i; +} + void Button::button_press(int, int, unsigned btn) { if(btn==1) @@ -60,6 +67,28 @@ void Button::render_special(const Part &part) const { if(part.get_name()=="text") render_text(part, text); + 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(); + } } diff --git a/source/button.h b/source/button.h index 4464c44..d13a4c0 100644 --- a/source/button.h +++ b/source/button.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2009 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -9,6 +9,7 @@ Distributed under the LGPL #define MSP_GLTK_BUTTON_H_ #include +#include #include "widget.h" namespace Msp { @@ -30,6 +31,7 @@ public: private: std::string text; + const GL::Texture2D *icon; bool pressed; public: @@ -37,6 +39,7 @@ public: Button(const Resources &, const std::string & =std::string()); void set_text(const std::string &); + void set_icon(const GL::Texture2D *); virtual void button_press(int, int, unsigned); virtual void button_release(int, int, unsigned); virtual void pointer_motion(int, int); diff --git a/source/panel.cpp b/source/panel.cpp index 97c9ca4..a708873 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -163,7 +163,11 @@ void Panel::child_hidden(Widget &wdg) void Panel::grab_pointer(Widget &wdg) { if(pointer_grab==0 || pointer_focus==&wdg) + { set_pointer_focus(&wdg, 255); + if(parent) + parent->grab_pointer(*this); + } else throw InvalidState("Pointer is already grabbed"); } @@ -171,7 +175,11 @@ void Panel::grab_pointer(Widget &wdg) void Panel::ungrab_pointer(Widget &wdg) { if(pointer_focus==&wdg) + { set_pointer_focus(0, 0); + if(parent) + parent->ungrab_pointer(*this); + } else if(pointer_grab>0) throw Exception("Someone is trying to steal the pointer!"); } diff --git a/source/root.cpp b/source/root.cpp index 1c030e8..9d02c4a 100644 --- a/source/root.cpp +++ b/source/root.cpp @@ -11,7 +11,7 @@ Distributed under the LGPL namespace Msp { namespace GLtk { -Root::Root(Resources &r, Graphics::Window &w): +Root::Root(const Resources &r, Graphics::Window &w): Panel(r), window(w) { diff --git a/source/root.h b/source/root.h index 2248ab8..94fb483 100644 --- a/source/root.h +++ b/source/root.h @@ -26,7 +26,7 @@ private: Graphics::Window &window; public: - Root(Resources &, Graphics::Window &); + Root(const Resources &, Graphics::Window &); private: virtual const char *get_class() const { return "root"; } void button_press_event(int, int, unsigned, unsigned); -- 2.43.0