-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguipp_text_box.cpp
304 lines (288 loc) · 7.57 KB
/
guipp_text_box.cpp
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
#include "guipp_text_box.h"
#include <iostream>
#include <cwctype>
guipp::TextBox::TextBox(ext::TextFormat font, int nMinLines, ScrollBar* sbv, ScrollBar* sbh)
:font(font), sbv(sbv), sbh(sbh), nMinLines(nMinLines), timer_blink(guipp::NewId(), 500)
{
font->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR);
font->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
font->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
layout = font(L"");
DWRITE_TEXT_METRICS metrics;
layout->GetMetrics(&metrics);
fPxFontHeight = metrics.height;
}
void guipp::TextBox::OnDraw(ext::D2DGraphics& gfx)
{
auto rrect = D2D1::RoundedRect(
D2D1::RectF(GetPos().x, GetPos().y, GetPos().x + GetSize().x, GetPos().y + GetSize().y),
guipp::fCornerRadius, guipp::fCornerRadius);
gfx.pSolidBrush->SetColor(D2D1::ColorF(0));
gfx.pRenderTarget->FillRoundedRectangle(rrect, gfx.pSolidBrush);
gfx.pSolidBrush->SetColor(D2D1::ColorF(bActive ? 0xffffff : 0x505050));
gfx.pRenderTarget->DrawRoundedRectangle(rrect, gfx.pSolidBrush, 1.5f);
gfx.pRenderTarget->PushAxisAlignedClip(
D2D1::RectF(
GetPos().x,
GetPos().y,
GetPos().x + GetSize().x - guipp::fSpacing,
GetPos().y + GetSize().y - guipp::fSpacing),
D2D1_ANTIALIAS_MODE_ALIASED);
ext::vec2d<float> offset = { 0.0f,0.0f };
DWRITE_TEXT_METRICS metrics;
if (sbv || sbh)
layout->GetMetrics(&metrics);
if (sbv)
{
offset.y = std::min(0.0f, -sbv->fSliderValue * (metrics.height - (GetSize().y - 2.0f * guipp::fSpacing)));
}
if (sbh)
{
offset.x = std::min(0.0f, -sbh->fSliderValue * (metrics.widthIncludingTrailingWhitespace - (GetSize().x - 2.0f * guipp::fSpacing)));
}
gfx.pSolidBrush->SetColor(D2D1::ColorF(0xffffff));
gfx.pRenderTarget->DrawTextLayout(
D2D1::Point2F(GetPos().x + offset.x + guipp::fSpacing, GetPos().y + offset.y + guipp::fSpacing),
layout, gfx.pSolidBrush);
if (caret.nShow % 2)
{
DWRITE_HIT_TEST_METRICS htm;
ext::vec2d<float> mark;
layout->HitTestTextPosition(caret.nPos, false, &mark.x, &mark.y, &htm);
mark += GetPos() + offset + guipp::fSpacing;
gfx.pRenderTarget->DrawLine(
D2D1::Point2F(mark.x, mark.y),
D2D1::Point2F(mark.x, mark.y + htm.height),
gfx.pSolidBrush, 1.5f);
}
gfx.pRenderTarget->PopAxisAlignedClip();
}
void guipp::TextBox::OnInitialize(guipp::Window& wnd, bool bInitialize)
{
if (bActive && !bInitialize)
wnd.Unbind(WM_TIMER, this);
}
bool guipp::TextBox::OnKbdMessage(guipp::Window& wnd, UINT msg, unsigned key_code, LPARAM lParam)
{
if (msg == WM_CHAR)
{
if (!OnChar || !OnChar(*this, key_code))
{
switch (key_code)
{
default:
if (std::isprint(key_code) || std::iswspace(key_code) || key_code > 160)
{
if (key_code == '\r')
key_code = '\n';
text.insert(text.begin() + caret.nPos, key_code);
caret.nPos++;
}
break;
case '\n':
for (caret.nPos -= caret.nPos > 0; caret.nPos > 0 && text[caret.nPos - 1] != '\n'; caret.nPos--);
text.insert(text.begin() + caret.nPos, L'\n');
break;
case '\b':
if (caret.nPos > 0)
text.erase(text.begin() + --caret.nPos);
break;
case 127:
//ctrl + backspace (delete whole word)
if (caret.nPos > 0)
{
auto first = text.begin() + --caret.nPos, last = first;
for (bool last_is_ws = std::iswspace(*last); first != text.begin(); first--, caret.nPos--)
if ((last_is_ws) ^ bool(std::iswspace(*(first - 1))))
break;
text.erase(first, last + 1);
}
break;
}
}
layout = font(text, GetSize().x - 2.0f * guipp::fSpacing, GetSize().y - 2.0f * guipp::fSpacing);
if (OnLayoutRecreated)
OnLayoutRecreated(*this);
DWRITE_TEXT_METRICS metrics;
if (sbv || sbh)
layout->GetMetrics(&metrics);
if (sbv)
{
sbv->fSliderSize =
std::min(
1.0f,
(GetSize().y - 2.0f * guipp::fSpacing) / metrics.height
);
}
if (sbh)
{
sbh->fSliderSize =
std::min(
1.0f,
(GetSize().x - 2.0f * guipp::fSpacing) / metrics.widthIncludingTrailingWhitespace
);
}
}
else if (msg == WM_KEYDOWN)
{
switch (key_code)
{
case VK_DELETE:
if (caret.nPos < text.size())
{
if (GetAsyncKeyState(VK_CONTROL))
{
auto first = text.begin() + caret.nPos, last = first;
for (bool first_is_ws = std::iswspace(*first); last + 1 != text.end(); last++)
if ((first_is_ws) ^ bool(std::iswspace(*(last + 1))))
break;
text.erase(first, last + 1);
}
else
{
text.erase(text.begin() + caret.nPos);
}
layout = font(text, GetSize().x - 2.0f * guipp::fSpacing, GetSize().y - 2.0f * guipp::fSpacing);
}
break;
case VK_RIGHT:
if (GetAsyncKeyState(VK_CONTROL))
{
if (caret.nPos < text.size())
{
auto it = text.begin() + caret.nPos;
for (bool was_ws = std::iswspace(*it); it + 1 != text.end(); it++, caret.nPos++)
if ((was_ws) ^ bool(std::iswspace(*(it + 1))))
break;
caret.nPos++;
}
}
else
caret.nPos += caret.nPos < text.size();
break;
case VK_LEFT:
if (GetAsyncKeyState(VK_CONTROL))
{
if (caret.nPos > 0)
{
auto it = text.begin() + --caret.nPos;
for (bool was_ws = std::iswspace(*it); it != text.begin(); it--, caret.nPos--)
if ((was_ws) ^ bool(std::iswspace(*(it - 1))))
break;
}
}
else
caret.nPos -= caret.nPos > 0;
break;
case VK_UP:
if (size_t n1 = text.find_last_of('\n', caret.nPos); n1 != text.npos)
{
int offset = caret.nPos - n1 - 1;
caret.nPos = text.find_last_of('\n', n1 - 1);
caret.nPos = caret.nPos == text.npos ? 0 : caret.nPos + 1;
caret.nPos += std::min((int)n1 - caret.nPos, offset);
}
break;
case VK_DOWN:
if (size_t n1 = text.find_first_of('\n', caret.nPos); n1 != text.npos)
{
size_t n0 = caret.nPos > 0 ? text.find_last_of('\n', caret.nPos - 1) : text.npos;
int offset = caret.nPos - (n0 == text.npos ? 0 : n0 + 1);
size_t n2 = text.find_first_of('\n', n1 + 1);
n2 = n2 == text.npos ? text.size() - 1 : n2;
int length = n2 - n1;
offset = std::min(offset, length);
caret.nPos = n1 + 1 + offset;
}
break;
case VK_TAB:
return true;
break;
default:
return false;
break;
}
}
else
{
return false;
}
wnd.RequestRedraw();
caret.nShow = 1;
timer_blink.Reset(wnd.hWnd);
return true;
}
bool guipp::TextBox::OnMouseMessage(guipp::Window& wnd, UINT msg, WPARAM wParam, const ext::vec2d<float>& mpos_t)
{
switch (msg)
{
case WM_LBUTTONDOWN:
//start selection
break;
default:
return false;
break;
}
return true;
}
guipp::Object* guipp::TextBox::OnMouseHitTest(const ext::vec2d<float>& mpos_t)
{
if (mpos_t.x >= GetPos().x && mpos_t.y >= GetPos().y &&
mpos_t.x < GetPos().x + GetSize().x && mpos_t.y < GetPos().y + GetSize().y)
{
return this;
}
return nullptr;
}
guipp::Object* guipp::TextBox::OnKbdFocus(Window& wnd, bool bFirst)
{
wnd.Bind(WM_TIMER, this);
timer_blink.Reset(wnd.hWnd);
caret.nShow = 1;
bActive = true;
wnd.RequestRedraw();
return this;
}
void guipp::TextBox::OnKbdUnfocus(Window& wnd)
{
timer_blink.Stop(wnd.hWnd);
caret.nShow = 0;
bActive = false;
wnd.RequestRedraw();
}
void guipp::TextBox::OnMessage(Window& wnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (wParam == timer_blink.id)
{
if (caret.nShow < 9)
{
caret.nShow++;
}
else
{
timer_blink.Stop(wnd.hWnd);
}
wnd.RequestRedraw();
}
}
ext::vec2d<float> guipp::TextBox::OnMinSizeUpdate()
{
return ext::vec2d<float>{ 0.0f,fPxFontHeight * nMinLines } + 2.0f * guipp::fSpacing;
}
void guipp::TextBox::Timer::Reset(HWND hWnd)
{
if (bSet)
{
KillTimer(hWnd, id);
}
SetTimer(hWnd, id, ms, NULL);
bSet = true;
}
void guipp::TextBox::Timer::Stop(HWND hWnd)
{
if (bSet)
{
KillTimer(hWnd, id);
bSet = false;
}
}