]> git.tdb.fi Git - libs/gltk.git/blob - source/vslider.cpp
397713b6c7c9bda5529fa93661205f85d402aee8
[libs/gltk.git] / source / vslider.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 "part.h"
12 #include "style.h"
13 #include "vslider.h"
14
15 namespace Msp {
16 namespace GLtk {
17
18 VSlider::VSlider(const Resources &r):
19         Slider(r),
20         dragging(false)
21 {
22         update_style();
23 }
24
25 void VSlider::button_press(int x, int y, unsigned btn)
26 {
27         if(geom.is_inside(x, y))
28         {
29                 int sh=get_slider_height();
30                 int sy=geom.y+static_cast<int>((geom.h-sh)*(value-min)/(max-min));
31                 if(btn==1 && y>=sy && y<sy+sh)
32                 {
33                         dragging=true;
34                         state=ACTIVE;
35                         drag_start_y=y;
36                         drag_start_value=value;
37                 }
38         }
39 }
40
41 void VSlider::button_release(int, int, unsigned btn)
42 {
43         if(btn==1)
44         {
45                 dragging=false;
46                 state=NORMAL;
47         }
48 }
49
50 void VSlider::pointer_motion(int, int y)
51 {
52         if(dragging)
53         {
54                 set_value(drag_start_value+(y-drag_start_y)*(max-min)/(geom.h-get_slider_height()));
55         }
56 }
57
58 void VSlider::render_special(const Part &part) const
59 {
60         if(part.get_name()=="slider")
61         {
62                 Alignment align=part.get_alignment();
63                 align.y=(value-min)/(max-min);
64
65                 Geometry pgeom=part.get_geometry();
66                 align.apply(pgeom, geom, part.get_margin());
67
68                 GL::push_matrix();
69                 GL::translate(pgeom.x, pgeom.y, 0);
70                 part.get_graphic(state)->render(pgeom.w, pgeom.h);
71                 GL::pop_matrix();
72         }
73 }
74
75 unsigned VSlider::get_slider_height() const
76 {
77         for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
78                 if(i->get_name()=="slider")
79                         return i->get_geometry().h;
80
81         return 0;
82 }
83
84 } // namespace GLtk
85 } // namespace Msp
86