]> git.tdb.fi Git - libs/gltk.git/blob - source/geometry.cpp
Prevent problems if a button press handler throws
[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         {
46                 geom.w+=static_cast<unsigned>((parent.w-geom.w)*w);
47                 geom.x+=static_cast<int>((parent.w-geom.w)*x);
48         }
49         if(parent.h>geom.h)
50         {
51                 geom.h+=static_cast<unsigned>((parent.h-geom.h)*h);
52                 geom.y+=static_cast<int>((parent.h-geom.h)*y);
53         }
54 }
55
56 void Alignment::apply(Geometry &geom, const Geometry &parent, const Sides &margin) const
57 {
58         unsigned pw=parent.w-margin.left-margin.right;
59         unsigned ph=parent.h-margin.bottom-margin.top;
60
61         geom.x+=margin.left;
62         geom.y+=margin.bottom;
63
64         if(pw>geom.w)
65         {
66                 geom.w+=static_cast<unsigned>((pw-geom.w)*w);
67                 geom.x+=static_cast<int>((pw-geom.w)*x);
68         }
69         if(ph>geom.h)
70         {
71                 geom.h+=static_cast<unsigned>((ph-geom.h)*h);
72                 geom.y+=static_cast<int>((ph-geom.h)*y);
73         }
74 }
75
76 } // namespace GLtk
77 } // namespace Msp