File: component_resize_handler.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 (112 lines) | stat: -rw-r--r-- 2,831 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
/*
	$Id: component_resize_handler.cpp,v 1.8 2001/12/22 17:03:35 sphair 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/GUI/component_resize_handler.h"
#include "API/GUI/component.h"
#include "API/GUI/component_options.h"
#include "API/Display/Input/input.h"
#include "API/Display/Input/inputdevice.h"

class CL_ComponentResizeHandler_Generic
{
public:
	CL_ComponentResizeHandler_Generic()
	: owner(NULL), client_area(NULL)
	{
	}

	void on_key_down(CL_Component *comp, CL_InputDevice *device, const CL_Key &key);
	void on_key_up(CL_Component *comp, CL_InputDevice *device, const CL_Key &key);
	void on_mouse_move(CL_Component *comp, int x, int y);

	CL_Slot slot_key_down;
	CL_Slot slot_key_up;
	CL_Slot slot_mouse_move;

	bool is_active;
	CL_Point mouse_origin;

	CL_Component *owner;
	CL_Component *client_area;
};

CL_ComponentResizeHandler::CL_ComponentResizeHandler(
	const CL_Rect &resize_area,
	CL_Component *owner,
	CL_StyleManager *style)
:
	impl(new CL_ComponentResizeHandler_Generic)
{
	impl->owner = owner;
	impl->is_active = false;

	impl->client_area = new CL_Component(resize_area, impl->owner, style);

	impl->slot_key_down = impl->client_area->sig_key_down().connect(
		impl, &CL_ComponentResizeHandler_Generic::on_key_down);

	impl->slot_key_up = impl->client_area->sig_key_up().connect(
		impl, &CL_ComponentResizeHandler_Generic::on_key_up);

	impl->slot_mouse_move = impl->client_area->sig_mouse_move().connect(
		impl, &CL_ComponentResizeHandler_Generic::on_mouse_move);
}

CL_ComponentResizeHandler::~CL_ComponentResizeHandler()
{
	if (impl->client_area != NULL)
	{
		impl->owner->remove_child(impl->client_area);
		delete impl->client_area;
	}
	delete impl;
}

void CL_ComponentResizeHandler_Generic::on_key_down(
	CL_Component *comp,
	CL_InputDevice *device,
	const CL_Key &key)
{
	if (device->get_type() != CL_InputDevice::type_mouse) return;

	mouse_origin = CL_Point((int)key.x, (int)key.y);
	is_active = true;
	client_area->capture_mouse();
}

void CL_ComponentResizeHandler_Generic::on_key_up(
	CL_Component *comp,
	CL_InputDevice *device,
	const CL_Key &key)
{
	if (device->get_type() != CL_InputDevice::type_mouse) return;

	is_active = false;
	client_area->release_mouse();
}

void CL_ComponentResizeHandler_Generic::on_mouse_move(
	CL_Component *comp,
	int x,
	int y)
{
	if(is_active == false)
		return;

	int delta_x = x - mouse_origin.x;
	int delta_y = y - mouse_origin.y;

	owner->set_size(owner->get_width() + delta_x, owner->get_height() + delta_y);

	CL_Rect rect = client_area->get_position();

	client_area->set_position(rect.x1 + delta_x, rect.y1 + delta_y);
}