summaryrefslogtreecommitdiff
path: root/plugins/Skins/SkinLib/FieldState.cpp
blob: 68510051dae0bc9469f32cb83de8166f20991799 (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
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
#include "globals.h"
#include "FieldState.h"
#include "DialogState.h"
#include "BorderState.h"

#define START		1<<0
#define LEN			1<<1
#define END			1<<2
#define USING_MASK	0xFF
#define LAST_SHIFT	8
#define SET(_FIELD_, _ITEM_)		_FIELD_ = (((_FIELD_ | _ITEM_) & USING_MASK) | (_ITEM_ << LAST_SHIFT))
#define LAST_SET(_FIELD_)			(_FIELD_ >> LAST_SHIFT)


FieldState::FieldState(DialogState *aDialog, Field *aField) 
		: field(aField), dialog(aDialog), size(-1, -1), pos(0, 0), 
		  usingX(0), usingY(0), visible(aField->isEnabled()), borders(0,0,0,0),
		  tooltipSet(false), halign(HORIZONTAL_ALIGN_LEFT), valign(VERTICAL_ALIGN_TOP)
{
}

FieldState::~FieldState()
{
}

Field * FieldState::getField() const
{
	return field;
}

DialogState * FieldState::getDialog() const
{
	return dialog;
}

int FieldState::getX() const
{
	return pos.x;
}

void FieldState::setX(int x)
{
	if (usingX & END)
	{
		int diff = x - getX();
		size.x = max(0, getWidth() - getHorizontalBorders() - diff);
	}

	pos.x = x;

	SET(usingX, START);
}

int FieldState::getY() const
{
	return pos.y;
}

void FieldState::setY(int y)
{
	if (usingY & END)
	{
		int diff = y - getY();
		size.y = max(0, getHeight() - getVerticalBorders() - diff);
	}

	pos.y = y;

	SET(usingY, START);
}

int FieldState::getWidth() const
{
	if (size.x >= 0)
		return size.x + getHorizontalBorders();

	return getPreferedSize().x + getHorizontalBorders();
}

void FieldState::setWidth(int width)
{
	width = max(0, width - getHorizontalBorders()) + getHorizontalBorders();

	if (LAST_SET(usingX) == END)
	{
		int diff = width - getWidth();
		pos.x = getX() - diff;
	}

	size.x = width - getHorizontalBorders();

	usingX |= LEN;
}

int FieldState::getHeight() const
{
	if (size.y >= 0)
		return size.y + getVerticalBorders();

	return getPreferedSize().y + getVerticalBorders();
}

void FieldState::setHeight(int height)
{
	height = max(0, height - getVerticalBorders()) + getVerticalBorders();

	if (LAST_SET(usingY) == END)
	{
		int diff = height - getHeight();
		pos.y = getY() - diff;
	}

	size.y = height - getVerticalBorders();

	usingY |= LEN;
}

bool FieldState::isVisible() const
{
	if (!visible)
		return false;

	RECT rc = getRect();
	if (rc.right <= rc.left || rc.bottom <= rc.top)
		return false;

	return true;
}

void FieldState::setVisible(bool visible)
{
	this->visible = visible;
}

bool FieldState::isEnabled() const
{
	return field->isEnabled();
}

int FieldState::getLeft() const
{
	return getX();
}

void FieldState::setLeft(int left)
{
	setX(left);
}

int FieldState::getTop() const
{
	return getY();
}

void FieldState::setTop(int top)
{
	setY(top);
}

int FieldState::getRight() const
{
	return getX() + getWidth();
}

void FieldState::setRight(int right)
{
	if (usingX & START)
	{
		size.x = max(0, right - getX());
	}
	else
	{
		pos.x = right - getWidth();
	}

	SET(usingX, END);
}

int FieldState::getBottom() const
{
	return getY() + getHeight();
}

void FieldState::setBottom(int botom)
{
	if (usingY & START)
	{
		size.y = max(0, botom - getY());
	}
	else
	{
		pos.y = botom - getHeight();
	}

	SET(usingY, END);
}

const TCHAR * FieldState::getToolTip() const
{
	if (tooltipSet)
		return tooltip.c_str();
	else
		return field->getToolTip();
}

void FieldState::setToolTip(const TCHAR *tooltip)
{
	this->tooltip = tooltip;
	tooltipSet = true;
}

BorderState * FieldState::getBorders()
{
	return &borders;
}

const BorderState * FieldState::getBorders() const
{
	return &borders;
}

int FieldState::getHorizontalBorders() const
{
	return borders.getLeft() + borders.getRight();
}

int FieldState::getVerticalBorders() const
{
	return borders.getTop() + borders.getBottom();
}

static inline int beetween(int val, int minVal, int maxVal)
{
	return max(minVal, min(maxVal, val));
}

static inline void intersection(RECT &main, const RECT &other)
{
	main.left = beetween(main.left, other.left, other.right);
	main.right = beetween(main.right, other.left, other.right);
	main.top = beetween(main.top, other.top, other.bottom);
	main.bottom = beetween(main.bottom, other.top, other.bottom);
}

RECT FieldState::getRect(bool raw) const
{
	RECT ret = {0};

	if (!visible)
		return ret;

	RECT inside = dialog->getInsideRect();

	ret.left = getLeft() + inside.left;
	ret.right = getRight() + inside.left;
	ret.top = getTop() + inside.top;
	ret.bottom = getBottom() + inside.top;

	if (!raw)
		intersection(ret, inside);

	return ret;
}

RECT FieldState::getInsideRect(bool raw) const
{
	RECT ret = {0};

	if (!visible)
		return ret;

	RECT inside = dialog->getInsideRect();

	ret.left = getLeft() + borders.getLeft() + inside.left;
	ret.right = getRight() - borders.getRight() + inside.left;
	ret.top = getTop() + borders.getTop() + inside.top;
	ret.bottom = getBottom() - borders.getBottom() + inside.top;

	if (!raw)
		intersection(ret, inside);

	return ret;
}

HORIZONTAL_ALIGN FieldState::getHAlign() const
{
	return halign;
}

void FieldState::setHAlign(HORIZONTAL_ALIGN halign)
{
	this->halign = halign;
}

VERTICAL_ALIGN FieldState::getVAlign() const
{
	return valign;
}

void FieldState::setVAlign(VERTICAL_ALIGN valign)
{
	this->valign = valign;
}