File: component.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (603 lines) | stat: -rw-r--r-- 12,134 bytes parent folder | download
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
	$Id: component.cpp,v 1.73 2002/01/17 16:50:35 mbn Exp $
	
	ClanGUI, copyrights by various people. Have a look in the CREDITS file.
	
	This sourcecode is distributed using the Library GNU Public Licence,
	version 2 or (at your option) any later version. Please read LICENSE
	for details.
*/

#include "precomp.h"
#include "API/Core/System/cl_assert.h"
#include "API/GUI/gui_manager.h"
#include "API/GUI/component_options.h"
#include "API/Display/Input/mouse.h"
#include "component_generic.h"
#include "component_manager_generic.h"
#include <vector>

/////////////////////////////////////////////////////////////////////////////
// Construction:

CL_Component::CL_Component(
	CL_Component *parent,
	CL_StyleManager *style)
:
	impl(NULL)
{
	impl = new CL_Component_Generic(this, CL_Rect(0,0,0,0), parent, style);

	if (get_style_manager() != NULL)
		get_style_manager()->connect_styles("component", this);

	if (parent != NULL) parent->add_child(this);
}

CL_Component::CL_Component(const CL_Rect &pos, CL_Component *parent, CL_StyleManager *style)
: impl(NULL)
{
	impl = new CL_Component_Generic(this, pos, parent, style);

	if (get_style_manager() != NULL)
		get_style_manager()->connect_styles("component", this);

	if (parent != NULL) parent->add_child(this);
}

CL_Component::~CL_Component()
{
//	if (impl->parent != NULL) impl->parent->remove_child(this);
	delete impl;
}

/////////////////////////////////////////////////////////////////////////////
// Attributes:

CL_Component *CL_Component::get_parent() const
{
	return impl->parent;
}

CL_Component *CL_Component::get_root_parent()
{
	if(impl->parent)
		return impl->parent->get_root_parent();
	else
		return this;
}

CL_GUIManager *CL_Component::get_gui_manager() const
{
	if (impl->gui != NULL) return impl->gui;
	if (impl->parent != NULL) return impl->parent->get_gui_manager();
	return NULL;
}

CL_StyleManager *CL_Component::get_style_manager() const
{
	return impl->style_manager;
}

int CL_Component::get_width() const
{
	return impl->position.x2 - impl->position.x1;
}

int CL_Component::get_height() const
{
	return impl->position.y2 - impl->position.y1;
}

CL_Rect CL_Component::get_screen_rect() const
{
	if (impl->parent == NULL)
	{
		return impl->position;
	}
	else
	{
		CL_Rect r = impl->parent->get_screen_rect();

		return CL_Rect(
			r.x1 + impl->position.x1, 
			r.y1 + impl->position.y1,
			r.x1 + impl->position.x2, 
			r.y1 + impl->position.y2);
	}
}

const CL_Rect &CL_Component::get_position() const
{
	return impl->position;
}

bool CL_Component::has_child(CL_Component *component) const
{
	for (
		std::list<CL_Component *>::iterator it = impl->children.begin();
		it != impl->children.end();
		it++)
	{
			if (*it == component || (*it)->has_child(component)) return true;
	}

	return false;
}

const std::list<CL_Component *> &CL_Component::get_children() const
{
	return impl->children;
}

CL_Rect CL_Component::get_children_rect()
{
	CL_Rect rect(0, 0, 0, 0);
	
	for (
		std::list<CL_Component *>::iterator it = impl->children.begin();
		it != impl->children.end();
		it++)
	{
		CL_Component *comp = (*it);
		CL_Rect childrect = comp->get_position();

		if(childrect.x1 < rect.x1)
			rect.x1 = childrect.x1;
		if(childrect.y1 < rect.y1)
			rect.y1 = childrect.y1;
		if(childrect.x2 > rect.x2)
			rect.x2 = childrect.x2;
		if(childrect.y2 > rect.y2)
			rect.y2 = childrect.y2;
	}

	return rect;
}

CL_Component *CL_Component::get_component_at(int pos_x, int pos_y)
{
	std::list<CL_Component *>::reverse_iterator it;
	for (it = impl->children.rbegin(); it != impl->children.rend(); it++)
	{
		CL_Component *cur_component = (*it);

		if(cur_component->is_visible())
		{
			const CL_Rect &r = cur_component->get_position();

			if (pos_x >= r.x1 && pos_x < r.x2 &&
				pos_y >= r.y1 && pos_y < r.y2)
			{
				return cur_component->get_component_at(pos_x - r.x1, pos_y - r.y1);
			}
		}
	}

	return this;
}

CL_Component *CL_Component::get_focus() const
{
	return get_gui_manager()->get_focus();
}

bool CL_Component::has_focus() const
{
	return get_focus() == this;
}

bool CL_Component::has_mouse_over() const
{
	int x = CL_Mouse::get_x();
	int y = CL_Mouse::get_y();

	CL_Rect rect = get_screen_rect();

	return rect.inside(x, y);
}

bool CL_Component::is_visible() const
{
	return impl->visible;
}

bool CL_Component::is_enabled() const
{
	return impl->enabled;
}

bool CL_Component::is_popup() const
{
	return impl->popup;
}

bool CL_Component::is_mouse_captured() const
{
	return impl->captured;
}

bool CL_Component::is_removeflag_set() const
{
	return impl->removeflag;
}

CL_Point CL_Component::get_mouse_position() const
{
	CL_Point pos(CL_Mouse::get_x(), CL_Mouse::get_y());
	CL_Rect screen = get_screen_rect();
	pos.x -= screen.x1;
	pos.y -= screen.y1;
	return pos;
}

int CL_Component::get_tab_id() const
{
	return impl->tab_id;
}

/////////////////////////////////////////////////////////////////////////////
// Operations:

void CL_Component::set_tab_id(int id)
{
	impl->tab_id = id;
}

void CL_Component::set_position(const CL_Rect &new_pos)
{
	int old_x = impl->position.x1;
	int old_y = impl->position.y1;

	int old_width = get_width();
	int old_height = get_height();

	impl->position = new_pos;

	if(old_x != impl->position.x1 || old_y != impl->position.y1)
		impl->sig_move(old_x, old_y);
	if(old_width != get_width() || old_height != get_height())
		impl->sig_resize(old_width, old_height);
}

void CL_Component::set_position(int new_x, int new_y)
{
	int old_x = impl->position.x1;
	int old_y = impl->position.y1;

	impl->position.x2 = get_width() + new_x;
	impl->position.y2 = get_height() + new_y;
	impl->position.x1 = new_x;
	impl->position.y1 = new_y;

	if(old_x != impl->position.x1 || old_y != impl->position.y1)
		impl->sig_move(old_x, old_y);
}

void CL_Component::set_size(int new_width, int new_height)
{
	int old_width = get_width();
	int old_height = get_height();

	impl->position.x2 = impl->position.x1 + new_width;
	impl->position.y2 = impl->position.y1 + new_height;

	if(old_width != get_width() || old_height != get_height())
		impl->sig_resize(old_width, old_height);
}

void CL_Component::set_width(int width)
{
	int old_width = get_width();

	impl->position.x2 = impl->position.x1 + width;

	if(old_width != get_width())
		impl->sig_resize(old_width, get_height());
}

void CL_Component::set_height(int height)
{
	int old_height = get_height();

	impl->position.y2 = impl->position.y1 + height;

	if(old_height != get_height())
		impl->sig_resize(get_width(), old_height);
}

void CL_Component::set_parent(CL_Component *parent)
{
	impl->parent = parent;
}

void CL_Component::set_gui_manager(CL_GUIManager *gui)
{
	impl->gui = gui;
}

void CL_Component::show(bool show)
{
	impl->visible = show;
}

void CL_Component::enable(bool enable)
{
	impl->enabled = enable;
}

void CL_Component::popup(bool popup)
{
	impl->popup = popup;
}

void CL_Component::set_focus()
{
	get_gui_manager()->set_focus(this);
}

void CL_Component::raise()
{
	impl->parent->impl->children.remove(this);
	impl->parent->impl->children.push_front(this);
}

void CL_Component::lower()
{
	impl->parent->impl->children.remove(this);
	impl->parent->impl->children.push_back(this);
}

void CL_Component::capture_mouse()
{
	CL_GUIManager *gui = get_gui_manager();
	if (gui != NULL)
	{
		gui->gui_capture_mouse(this);
		impl->captured = true;
	}
}

void CL_Component::release_mouse()
{
	CL_GUIManager *gui = get_gui_manager();
	if (gui != NULL)
	{
		gui->gui_release_mouse();
		impl->captured = false;
	}
}

void CL_Component::update()
{
	impl->sig_begin_paint();
	impl->sig_paint();
	impl->sig_paint_children();
	impl->sig_end_paint();
}

void CL_Component::run(CL_Component *parent)
{
	if(get_parent())
		std::cout << "When creating modal components, don't create them with a parent!" << std::endl;

	CL_GUIManager modal_gui(parent, get_style_manager());
	modal_gui.add_child(this);
	modal_gui.run();
}

void CL_Component::quit()
{
	get_gui_manager()->quit();
}

void CL_Component::close()
{
//	show(false);
//	impl->removeflag = true;
	if(get_gui_manager() != NULL)
		get_gui_manager()->quit();
	else
		std::cout << "gui manager == NULL" << std::endl;

	if(get_parent() != NULL)
		get_parent()->remove_child(this);
	else
		std::cout << "parent == NULL" << std::endl;
//	set_gui_manager(NULL);
}

void CL_Component::clear_removeflag()
{
	impl->removeflag = false;
}

void CL_Component::add_child(CL_Component *child, bool delete_child)
{
	if(has_child(child))
		return;

	impl->sig_child_before_add(child);

	impl->children.push_front(child);
	child->set_parent(this);

	if (delete_child) impl->delete_children.push_front(child);

	impl->sig_child_add(child);
}

void CL_Component::remove_child(CL_Component *child)
{
	impl->sig_child_before_remove(child);

	child->set_parent(NULL);
	impl->children.remove(child);

	impl->sig_child_remove(child);
}

void CL_Component::add_components(CL_ComponentManager *component_manager)
{
	std::vector<CL_Component *> components;
	((CL_ComponentManager_Generic *) component_manager)->get_root_component_list(components);

	for (
		std::vector<CL_Component *>::iterator it = components.begin();
		it != components.end();
		it++)
	{
		add_child(*it);
	}
}

void CL_Component::attach_style(CL_ComponentStyle *style)
{
	impl->styles.push_back(style);
}

void CL_Component::detach_style(CL_ComponentStyle *style)
{
	impl->styles.remove(style);
}

void CL_Component::find_preferred_size()
{
	impl->find_preferred_size();
}

CL_Component *CL_Component::find_tab_id(int tab_id)
{
	if (impl->tab_id == tab_id) return this;

	std::list<CL_Component *>::iterator it;
	for (it = impl->children.begin(); it != impl->children.end(); it++)
	{
		CL_Component *comp = (*it)->find_tab_id(tab_id);
		if (comp) return comp;
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// Signals:

CL_Signal_v1<const CL_ComponentOptions &> &CL_Component::sig_set_options()
{
	return impl->sig_set_options;
}

CL_Signal_v0 &CL_Component::sig_paint()
{
	return impl->sig_paint;
}

CL_Signal_v0 &CL_Component::sig_paint_children()
{
	return impl->sig_paint_children;
}

CL_Signal_v0 &CL_Component::sig_begin_paint()
{
	return impl->sig_begin_paint;
}

CL_Signal_v0 &CL_Component::sig_end_paint()
{
	return impl->sig_end_paint;
}

CL_Signal_v1<const CL_Key &> &CL_Component::sig_mouse_down()
{
	return impl->sig_mouse_down;
}

CL_Signal_v1<const CL_Key &> &CL_Component::sig_mouse_up()
{
	return impl->sig_mouse_up;
}

CL_Signal_v3<CL_Component *, CL_InputDevice *, const CL_Key &> &CL_Component::sig_key_down()
{
	return impl->sig_key_down;
}

CL_Signal_v3<CL_Component *, CL_InputDevice *, const CL_Key &> &CL_Component::sig_key_up()
{
	return impl->sig_key_up;
}

CL_Signal_v3<CL_Component *, int, int> &CL_Component::sig_mouse_move()
{
	return impl->sig_mouse_move;
}

CL_Signal_v0 &CL_Component::sig_got_focus()
{
	return impl->sig_got_focus;
}

CL_Signal_v0 &CL_Component::sig_lost_focus()
{
	return impl->sig_lost_focus;
}

CL_Signal_v2<int, int> &CL_Component::sig_resize()
{
	return impl->sig_resize;
}

CL_Signal_v2<int, int> &CL_Component::sig_move()
{
	return impl->sig_move;
}

CL_Signal_v0 &CL_Component::sig_mouse_enter()
{
	return impl->sig_mouse_enter;
}

CL_Signal_v0 &CL_Component::sig_mouse_leave()
{
	return impl->sig_mouse_leave;
}

CL_Signal_v2<int &, int &> &CL_Component::sig_transform_coords()
{
	return impl->sig_transform_coords;
}

CL_Signal_v1<CL_Component *> &CL_Component::sig_child_add()
{
	return impl->sig_child_add;
}

CL_Signal_v1<CL_Component *> &CL_Component::sig_child_remove()
{
	return impl->sig_child_remove;
}

CL_Signal_v1<CL_Component *> &CL_Component::sig_child_before_add()
{
	return impl->sig_child_before_add;
}

CL_Signal_v1<CL_Component *> &CL_Component::sig_child_before_remove()
{
	return impl->sig_child_before_remove;
}

CL_Signal_v1<CL_Point &> &CL_Component::sig_get_minimum_size()
{
	return impl->sig_get_minimum_size;
}

CL_Signal_v1<CL_Point &> &CL_Component::sig_get_maximum_size()
{
	return impl->sig_get_maximum_size;
}

CL_Signal_v1<CL_Point &> &CL_Component::sig_get_preferred_size()
{
	return impl->sig_get_preferred_size;
}