]> git.tdb.fi Git - libs/gltk.git/blob - source/vslider.cpp
Implement autosize() method for most widgets
[libs/gltk.git] / source / vslider.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2008, 2010-2011  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():
19         slider_size(1)
20 { }
21
22 void VSlider::autosize()
23 {
24         if(!style)
25                 return;
26
27         Widget::autosize();
28
29         if(const Part *slider_part = style->get_part("slider"))
30         {
31                 const Sides &margin = slider_part->get_margin();
32                 const Geometry &pgeom = slider_part->get_geometry();
33                 geom.w = std::max(geom.w, pgeom.w+margin.left+margin.right);
34                 geom.h = std::max(geom.h, pgeom.h*3/2+margin.top+margin.bottom);
35         }
36 }
37
38 void VSlider::render_special(const Part &part) const
39 {
40         if(part.get_name()=="slider")
41         {
42                 Alignment align = part.get_alignment();
43                 if(max>min)
44                         align.y = (value-min)/(max-min);
45
46                 Geometry pgeom = part.get_geometry();
47                 align.apply(pgeom, geom, part.get_margin());
48
49                 GL::push_matrix();
50                 GL::translate(pgeom.x, pgeom.y, 0);
51                 part.get_graphic(state)->render(pgeom.w, pgeom.h);
52                 GL::pop_matrix();
53         }
54 }
55
56 void VSlider::button_press(int x, int y, unsigned btn)
57 {
58         if(btn==1 && geom.is_inside_relative(x, y) && max>min)
59         {
60                 int sy = static_cast<int>((geom.h-slider_size)*(value-min)/(max-min));
61                 if(y<sy)
62                         set_value(value-step*10);
63                 else if(y>=static_cast<int>(sy+slider_size))
64                         set_value(value+step*10);
65                 else
66                         start_drag(y);
67         }
68 }
69
70 void VSlider::button_release(int, int, unsigned btn)
71 {
72         if(btn==1 && dragging)
73                 end_drag();
74 }
75
76 void VSlider::pointer_motion(int, int y)
77 {
78         if(dragging)
79                 drag(y);
80 }
81
82 void VSlider::on_geometry_change()
83 {
84         drag_area_size = geom.h-slider_size;
85 }
86
87 void VSlider::on_style_change()
88 {
89         if(!style)
90                 return;
91
92         if(const Part *slider_part = style->get_part("slider"))
93                 slider_size = slider_part->get_geometry().h;
94
95         on_geometry_change();
96 }
97
98 } // namespace GLtk
99 } // namespace Msp
100