summaryrefslogtreecommitdiff
path: root/plugins/Skins/SkinLib/ControlFieldState.cpp
blob: 37c412058abe4b07572c740670065040482e480b (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
#include "globals.h"
#include "ControlFieldState.h"


ControlFieldState::ControlFieldState(DialogState *dialog, ControlField *field) 
		: FieldState(dialog, field), textSet(false), font(field->getFont(), field->getFontColor())
{
}


ControlFieldState::~ControlFieldState()
{
}


ControlField * ControlFieldState::getField() const
{
	return (ControlField *) FieldState::getField();
}


Size ControlFieldState::getTextPreferedSize(unsigned int format) const
{
	HDC hdc = CreateCompatibleDC(NULL);

	HFONT newFont = getFont()->getHFONT();
	HFONT oldFont = (HFONT) SelectObject(hdc, newFont);

	int width = 0;
	int height = 0;

	const TCHAR *text = getText();
	int len = lstrlen(text);
	if (len <= 0)
	{
		TEXTMETRIC tm = {0};
		GetTextMetrics(hdc, &tm);
		height = tm.tmHeight;
	}
	else
	{
		RECT rc = {0};
		if ((format & DT_SINGLELINE) == 0 && size.x >= 0)
		{
			format |= DT_WORDBREAK;
			rc.right = size.x;
		}
		DrawText(hdc, text, len, &rc, DT_CALCRECT | format);
		width = rc.right - rc.left;
		height = rc.bottom - rc.top;
	}

	SelectObject(hdc, oldFont);

	DeleteDC(hdc);

	return Size(width, height);
}


const TCHAR * ControlFieldState::getText() const
{
	if (textSet)
		return text.c_str();

	return getField()->getText();
}


void ControlFieldState::setText(const TCHAR *text)
{
	this->text = text;
	textSet = true;
}


FontState * ControlFieldState::getFont()
{
	return &font;
}


const FontState * ControlFieldState::getFont() const
{
	return &font;
}