]> git.tdb.fi Git - libs/gltk.git/blob - source/hslider.cpp
Change State into a bitmask to allow more fine-grained control of styles
[libs/gltk.git] / source / hslider.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/matrix.h>
9 #include <msp/gl/transform.h>
10 #include "graphic.h"
11 #include "hslider.h"
12 #include "part.h"
13 #include "style.h"
14
15 namespace Msp {
16 namespace GLtk {
17
18 HSlider::HSlider(const Resources &r):
19         Slider(r)
20 {
21         update_style();
22 }
23
24 void HSlider::button_press(int x, int y, unsigned btn)
25 {
26         if(geom.is_inside(x, y))
27         {
28                 int sx=geom.x+static_cast<int>((geom.w-slider_size)*(value-min)/(max-min));
29                 if(btn==1 && x>=sx && x<static_cast<int>(sx+slider_size))
30                         start_drag(x);
31         }
32 }
33
34 void HSlider::button_release(int, int, unsigned btn)
35 {
36         if(btn==1)
37                 end_drag();
38 }
39
40 void HSlider::pointer_motion(int x, int)
41 {
42         if(dragging)
43                 drag(x);
44 }
45
46 void HSlider::render_special(const Part &part) const
47 {
48         if(part.get_name()=="slider")
49         {
50                 Alignment align=part.get_alignment();
51                 if(max>min)
52                         align.x=(value-min)/(max-min);
53
54                 Geometry pgeom=part.get_geometry();
55                 align.apply(pgeom, geom, part.get_margin());
56
57                 GL::push_matrix();
58                 GL::translate(pgeom.x, pgeom.y, 0);
59                 part.get_graphic(state)->render(pgeom.w, pgeom.h);
60                 GL::pop_matrix();
61         }
62 }
63
64 void HSlider::on_geometry_change()
65 {
66         drag_area_size=geom.w-slider_size;
67 }
68
69 void HSlider::on_style_change()
70 {
71         for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
72                 if(i->get_name()=="slider")
73                         slider_size=i->get_geometry().w;
74
75         on_geometry_change();
76 }
77
78 } // namespace GLtk
79 } // namespace Msp