Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 068d60f2f58aa2ba6150450766d9c779a6c510f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
   ui/widget.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "auxiliary/functions.h"
#include "render/primitives.h"
#include "sys/sys.h"
#include "ui/widget.h"

namespace ui {

Widget::Widget(Widget *parent) {
	widget_parent = parent;
	widget_visible = true;
	widget_border = true;
	widget_background = false;
	widget_label.assign("widget");

	if (widget_parent) {
		parent->add_child(this);
		widget_palette = parent->widget_palette;
	} else {
		widget_palette = 0;
	}
}

Widget::~Widget()
{
	for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) {
		delete (*it);
		(*it) = 0;
	}
	widget_children.clear();
}

size_t Widget::list(size_t indent)
{
	print(indent);
	size_t n = 1;
	for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) {
		n += (*it)->list(indent+1);
	}
	return n;
}

void Widget::print(size_t indent)
{
	std::string marker("");
	con_print << aux::pad_left(marker, indent*2) << label() << std::endl;
}

void Widget::show()
{
	widget_visible = true;
}

void Widget::hide()
{
	widget_visible = false;
}

void Widget::set_border(bool border)
{
	widget_border = border;
}


void Widget::set_background(bool background)
{
	widget_background = background;
}

void Widget::set_label(std::string const & label)
{
	set_label(label.c_str());
}

void Widget::set_label(char const *label)
{
	widget_label.assign(label);
	aux::to_label(widget_label);
}

void Widget::set_palette(Palette *palette)
{
	widget_palette = palette;
}

void Widget::set_location(float const x, float const y) {
	widget_location.assign(x, y);
}

void Widget::set_size(float const w, float const h)
{
	widget_size.assign(w, h);
}

void Widget::set_width(float const w)
{
	widget_size.x = w;
}
void Widget::set_height(float const h)
{
	widget_size.y = h;
}

Widget::Children::iterator Widget::find_child(Widget *child)
{
	Children::iterator it;
	for (it = widget_children.begin(); it != widget_children.end(); it++) {
		if ((*it) == child)
			return it;
	}

	return it;
}

void Widget::add_child(Widget *child)
{
	Children::iterator it = find_child(child);
	if (it == widget_children.end()) {
		widget_children.push_back(child);
	}
}
	
void Widget::remove_child(Widget *child)
{
	Children::iterator it = find_child(child);
	if (it != widget_children.end()) {
		delete (*it);
		widget_children.erase(it);
	}
}

void Widget::resize_event()
{
	resize();
	for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) {
		(*it)->resize_event();
	}
}

void Widget::resize()
{
}

void Widget::draw_event()
{
	if (!widget_visible)
		return;

	draw();
	for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) {
		if ((*it)->visible())
			(*it)->draw_event();
	}
}

void Widget::draw()
{
	draw_background();
	draw_border();
}

void Widget::draw_background()
{
	if (!widget_background)
		return;

	if (palette())
		render::gl::color(palette()->background());
	render::primitives::rectangle(global_location(), size());
}

void Widget::draw_border()
{
	if (!widget_border)
		return;

	if (palette())
		render::gl::color(palette()->border());
	render::primitives::border(global_location(), size());
}

}