]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/hslider.cpp
Fix HSlider
[libs/gltk.git] / source / hslider.cpp
index c1d334d3d3cbff366b37b59bae72ee63fbf7e40c..e9db05f16cf950860cccd69002e82356c79c71dd 100644 (file)
@@ -1,4 +1,16 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <msp/gl/matrix.h>
+#include <msp/gl/transform.h>
+#include "graphic.h"
 #include "hslider.h"
+#include "part.h"
+#include "style.h"
 
 namespace Msp {
 namespace GLtk {
@@ -6,19 +18,62 @@ namespace GLtk {
 HSlider::HSlider(const Resources &r):
        Slider(r)
 {
+       update_style();
+}
+
+void HSlider::button_press(int x, int y, unsigned btn)
+{
+       if(geom.is_inside_relative(x, y) && max>min)
+       {
+               int sx=static_cast<int>((geom.w-slider_size)*(value-min)/(max-min));
+               if(btn==1 && x>=sx && x<static_cast<int>(sx+slider_size))
+                       start_drag(x);
+       }
+}
+
+void HSlider::button_release(int, int, unsigned btn)
+{
+       if(btn==1 && dragging)
+               end_drag();
+}
+
+void HSlider::pointer_motion(int x, int)
+{
+       if(dragging)
+               drag(x);
 }
 
-void HSlider::render_part(const Part &part)
+void HSlider::render_special(const Part &part) const
 {
        if(part.get_name()=="slider")
        {
-               const Graphic *graphic=part.get_graphic(state);
-               const Graphic::Sides &shadow=graphic->get_shadow();
-               unsigned gw=graphic->get_slice().w-shadow.left-shadow.right;
-               unsigned gh=(part.get_fill_x() ? geom.h, graphic->get_slice().h)-shadow.bottom-shadow.top;
-               GL::translate((geom.w-gw)*(value-min)/(max-min), part.get_alignment().y
+               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;
+}
+
+void HSlider::on_style_change()
+{
+       for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
+               if(i->get_name()=="slider")
+                       slider_size=i->get_geometry().w;
+
+       on_geometry_change();
+}
+
 } // namespace GLtk
 } // namespace Msp