]> git.tdb.fi Git - libs/gltk.git/blob - source/geometry.cpp
8202774d62913cf894c147aec7d7e287b3828bdc
[libs/gltk.git] / source / geometry.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 "geometry.h"
9
10 namespace Msp {
11 namespace GLtk {
12
13 bool Geometry::is_inside(int x_, int y_) const
14 {
15         return (x_>=x && x_<x+static_cast<int>(w) && y_>=y && y_<y+static_cast<int>(h));
16 }
17
18 bool Geometry::is_inside_relative(int x_, int y_) const
19 {
20         return (x_>=0 && x_<static_cast<int>(w) && y_>=0 && y_<static_cast<int>(h));
21 }
22
23
24 Sides::Sides():
25         top(0),
26         right(0),
27         bottom(0),
28         left(0)
29 { }
30
31
32 Sides::Loader::Loader(Sides &s):
33         sides(s)
34 {
35         add("top",    &Sides::top);
36         add("right",  &Sides::right);
37         add("bottom", &Sides::bottom);
38         add("left",   &Sides::left);
39 }
40
41
42 void Alignment::apply(Geometry &geom, const Geometry &parent) const
43 {
44         if(parent.w>geom.w)
45                 geom.w+=static_cast<unsigned>((parent.w-geom.w)*w);
46         if(parent.h>geom.h)
47                 geom.h+=static_cast<unsigned>((parent.h-geom.h)*h);
48
49         geom.x+=static_cast<int>((parent.w-geom.w)*x);
50         geom.y+=static_cast<int>((parent.h-geom.h)*y);
51 }
52
53 void Alignment::apply(Geometry &geom, const Geometry &parent, const Sides &margin) const
54 {
55         unsigned pw=parent.w-margin.left-margin.right;
56         unsigned ph=parent.h-margin.bottom-margin.top;
57
58         if(parent.w>geom.w)
59                 geom.w+=static_cast<unsigned>((pw-geom.w)*w);
60         if(parent.h>geom.h)
61                 geom.h+=static_cast<unsigned>((ph-geom.h)*h);
62
63         geom.x+=static_cast<int>(margin.left+(pw-geom.w)*x);
64         geom.y+=static_cast<int>(margin.bottom+(ph-geom.h)*y);
65 }
66
67 } // namespace GLtk
68 } // namespace Msp