]> git.tdb.fi Git - libs/gltk.git/blob - source/geometry.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / geometry.cpp
1 #include "geometry.h"
2
3 namespace Msp {
4 namespace GLtk {
5
6 bool Geometry::is_inside(int x_, int y_) const
7 {
8         return (x_>=x && x_<x+static_cast<int>(w) && y_>=y && y_<y+static_cast<int>(h));
9 }
10
11 bool Geometry::is_inside_relative(int x_, int y_) const
12 {
13         return (x_>=0 && x_<static_cast<int>(w) && y_>=0 && y_<static_cast<int>(h));
14 }
15
16
17 Sides::Sides():
18         top(0),
19         right(0),
20         bottom(0),
21         left(0)
22 { }
23
24 Sides::Sides(unsigned s):
25         top(s),
26         right(s),
27         bottom(s),
28         left(s)
29 { }
30
31 Sides::Sides(unsigned v, unsigned h):
32         top(v),
33         right(h),
34         bottom(v),
35         left(h)
36 { }
37
38 Sides::Sides(unsigned t, unsigned h, unsigned b):
39         top(t),
40         right(h),
41         bottom(b),
42         left(h)
43 { }
44
45 Sides::Sides(unsigned t, unsigned r, unsigned b, unsigned l):
46         top(t),
47         right(r),
48         bottom(b),
49         left(l)
50 { }
51
52
53 Sides::Loader::Loader(Sides &s):
54         DataFile::ObjectLoader<Sides>(s)
55 {
56         add("horizontal", &Loader::horizontal);
57         add("vertical",   &Loader::vertical);
58         add("top",    &Sides::top);
59         add("right",  &Sides::right);
60         add("bottom", &Sides::bottom);
61         add("left",   &Sides::left);
62 }
63
64 void Sides::Loader::horizontal(unsigned h)
65 {
66         obj.right = h;
67         obj.left = h;
68 }
69
70 void Sides::Loader::vertical(unsigned v)
71 {
72         obj.top = v;
73         obj.bottom = v;
74 }
75
76
77 void Alignment::apply(Geometry &geom, const Geometry &parent) const
78 {
79         if(parent.w>geom.w)
80         {
81                 geom.w += static_cast<unsigned>((parent.w-geom.w)*w);
82                 geom.x += static_cast<int>((parent.w-geom.w)*x);
83         }
84         if(parent.h>geom.h)
85         {
86                 geom.h += static_cast<unsigned>((parent.h-geom.h)*h);
87                 geom.y += static_cast<int>((parent.h-geom.h)*y);
88         }
89 }
90
91 void Alignment::apply(Geometry &geom, const Geometry &parent, const Sides &margin) const
92 {
93         Geometry content = parent;
94         content.w -= margin.left+margin.right;
95         content.h -= margin.bottom+margin.top;
96
97         geom.x += margin.left;
98         geom.y += margin.bottom;
99
100         apply(geom, content);
101 }
102
103 } // namespace GLtk
104 } // namespace Msp