]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/scout-gfx/lib/widget.cc
c0237a410fa30c3497078827c39f7f1d42cbf87b
[l4.git] / l4 / pkg / scout-gfx / lib / widget.cc
1 /*
2  * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
3  *     economic rights: Technische Universität Dresden (Germany)
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  */
9 #include <l4/scout-gfx/widget>
10
11 namespace Scout_gfx {
12
13 Widget::~Widget()
14 {
15   if (_parent)
16     _parent->forget(this);
17 }
18
19 void
20 Parent_widget::draw(Canvas *c, Point const &p)
21 {
22   for (Widget *e = _first; e; e = e->next)
23     e->try_draw(c, e->pos() + p);
24 }
25
26 void Parent_widget::append(Widget *e)
27 {
28   if (_last)
29     _last->next = e;
30   else
31     _first = e;
32
33   _last = e;
34
35   e->parent(this);
36 }
37
38
39 void Parent_widget::remove(Widget *e)
40 {
41   if (_first == e)
42     {
43       _first = e->next;
44       if (_last == e)
45         _last = 0;
46
47       return;
48     }
49
50   /* search specified element in the list */
51   Widget *ce = _first;
52   for (; ce && ce->next != e; ce = ce->next)
53     ;
54
55   if (!ce)
56     return;
57
58   if (_last == e)
59     _last = ce;
60
61   ce->next = e->next;
62 }
63
64
65 void Parent_widget::forget(Widget *e)
66 {
67   if (e->parent() == this)
68     remove(e);
69
70   _parent->forget(e);
71 }
72
73
74
75 Widget *
76 Parent_widget::find_child(Point const &p)
77 {
78   /* check if position is outside the parent element */
79   if (!geometry().contains(p))
80     return 0;
81
82   Point np = p - _pos;
83
84   /* check children */
85   for (Widget *e = _first; e; e = e->next)
86     {
87       if (e->geometry().contains(np) && e->findable())
88         return e;
89     }
90
91   return 0;
92 }
93
94 Widget *
95 Widget::find(Point const &p)
96 {
97   if (geometry().contains(p) && _flags.findable)
98     return this;
99
100   return 0;
101 }
102
103
104 Widget *
105 Parent_widget::find(Point const &p)
106 {
107   /* check if position is outside the parent element */
108   if (!geometry().contains(p))
109     return 0;
110
111   Point np = p - _pos;
112
113   /* check children */
114   Widget *ret = this;
115   for (Widget *e = _first; e; e = e->next)
116     {
117       Widget *res  = e->find(np);
118       if (res)
119         ret = res;
120     }
121
122   return ret;
123 }
124
125 void
126 Widget::redraw_area(Rect const &r) const
127 {
128   /* intersect specified area with element geometry */
129   Rect n = (r & Rect(_size));
130
131   if (!n.valid())
132     return;
133
134   /* propagate redraw request to the parent */
135   if (_parent)
136     _parent->redraw_area(n + _pos);
137 }
138
139 }