-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathctrlEdit.cpp
More file actions
351 lines (312 loc) · 9.62 KB
/
Copy pathctrlEdit.cpp
File metadata and controls
351 lines (312 loc) · 9.62 KB
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
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "ctrlEdit.h"
#include "CollisionDetection.h"
#include "RTTR_Assert.h"
#include "ctrlTextDeepening.h"
#include "driver/MouseCoords.h"
#include "drivers/VideoDriverWrapper.h"
#include "helpers/containerUtils.h"
#include "ogl/FontStyle.h"
#include "ogl/glFont.h"
#include "s25util/StringConversion.h"
#include "s25util/fileFuncs.h"
#include <s25util/strAlgos.h>
#include <s25util/utf8.h>
#include <boost/algorithm/string/trim.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/nowide/detail/utf.hpp>
#include <numeric>
ctrlEdit::ctrlEdit(Window* parent, unsigned id, const DrawPoint& pos, const Extent& size, TextureColor tc,
const glFont* font, unsigned short maxlength, bool password, bool disabled, bool notify)
: Window(parent, id, pos, size), maxLength_(maxlength), isPassword_(password), isDisabled_(disabled),
notify_(notify)
{
txtCtrl = static_cast<ctrlTextDeepening*>(
AddTextDeepening(0, DrawPoint(0, 0), size, tc, "", font, COLOR_YELLOW, FontStyle::LEFT | FontStyle::VCENTER));
UpdateInternalText();
}
/**
* setzt den Text.
*
* @param[in] text Der Text.
*/
void ctrlEdit::SetText(const std::string& text)
{
text_ = s25util::utf8to32(text);
if(editType_ == EditType::Number)
helpers::erase_if(text_, [](char32_t c) { return c < '0' || c > '9'; });
if(editType_ == EditType::Filename)
helpers::erase_if(text_, [](char32_t c) { return !isValidFileNameChar(c); });
if(maxLength_ > 0 && text_.size() > maxLength_)
text_.resize(maxLength_);
viewStart_ = 0;
cursorPos_ = text_.length();
UpdateInternalText();
Notify();
}
void ctrlEdit::SetText(const unsigned text)
{
SetText(s25util::toStringClassic(text));
}
std::string ctrlEdit::GetText() const
{
return s25util::utf32to8(text_);
}
GetFileNameResult ctrlEdit::GetFileName(const std::string& ext) const
{
RTTR_Assert(editType_ == EditType::Filename);
std::string name = GetText();
boost::algorithm::trim_if(name, [](char c) { return c == ' ' || c == '\t' || c == '\r' || c == '\n'; });
if(name.empty())
return {FileNameStatus::Empty, {}};
if(!ext.empty() && s25util::toLower(boost::filesystem::path(name).extension().string()) != s25util::toLower(ext))
name += ext;
if(!isValidFileName(name))
return {FileNameStatus::Invalid, {}};
return {FileNameStatus::Valid, std::move(name)};
}
void ctrlEdit::SetFocus(bool focus)
{
if(focus_ != focus)
{
focus_ = focus;
txtCtrl->SetTextColor(focus_ ? 0xFFFFA000 : COLOR_YELLOW);
if(focus && GetParent())
{
for(auto* edit : GetParent()->GetCtrls<ctrlEdit>())
{
if(edit != this)
edit->SetFocus(false);
}
}
}
}
static void removeFirstCharFromString(std::string& str)
{
auto it = str.begin();
boost::nowide::detail::utf::utf_traits<char>::decode_valid(it);
str.erase(str.begin(), it);
}
void ctrlEdit::UpdateInternalText()
{
if(text_.empty())
{
viewStart_ = 0;
cursorPos_ = 0;
cursorOffsetX_ = 0;
txtCtrl->SetText("");
} else
{
std::u32string dtext = (isPassword_) ? std::u32string(text_.size(), '*') : text_;
viewStart_ = std::min<unsigned>(viewStart_, dtext.length() - 1);
const auto* font = txtCtrl->GetFont();
const unsigned max_width = GetSize().x - ctrlTextDeepening::borderSize.x * 2;
unsigned max;
std::string curText = s25util::utf32to8(dtext.substr(viewStart_));
font->getWidth(curText, max_width, &max);
// Add chars at front as long as full text is shown
while(viewStart_ > 0 && curText.length() <= max)
{
--viewStart_;
curText = s25util::utf32to8(dtext.substr(viewStart_));
font->getWidth(curText, max_width, &max);
}
// Remove chars from front until remaining string can be fully shown
while(curText.length() > max)
{
++viewStart_;
removeFirstCharFromString(curText);
font->getWidth(curText, max_width, &max);
}
// Show (up to) 5 chars before the cursor
if(viewStart_ + 5 > cursorPos_)
{
viewStart_ = std::max(0, static_cast<int>(cursorPos_) - 5);
curText = s25util::utf32to8(dtext.substr(viewStart_));
}
if(cursorPos_ > viewStart_)
cursorOffsetX_ = font->getWidth(s25util::utf32to8(dtext.substr(viewStart_, cursorPos_ - viewStart_)));
else
cursorOffsetX_ = 0;
txtCtrl->SetText(curText);
}
}
inline void ctrlEdit::CursorLeft()
{
if(cursorPos_ == 0)
return;
--cursorPos_;
UpdateInternalText();
}
inline void ctrlEdit::CursorRight()
{
if(cursorPos_ == text_.length())
return;
++cursorPos_;
UpdateInternalText();
}
/**
* zeichnet das Fenster.
*
* @todo muss alles überarbeitet werden
*/
void ctrlEdit::Draw_()
{
Window::Draw_();
// Alle 500ms Cursor für 500ms anzeigen
if(focus_ && !isDisabled_ && VIDEODRIVER.GetTickCount() % 1000 < 500)
{
const auto fontHeight = txtCtrl->GetFont()->getHeight();
DrawPoint cursorDrawPos = GetDrawPos();
cursorDrawPos.x += cursorOffsetX_ + ctrlTextDeepening::borderSize.x;
cursorDrawPos.y += (GetSize().y - (fontHeight + 2)) / 2;
DrawRectangle(Rect(cursorDrawPos, Extent(1, fontHeight + 2)), 0xFFFFA000);
}
}
/**
* fügt ein Zeichen zum Text hinzu
*
* @param[in] text Das Zeichen
*/
void ctrlEdit::AddChar(char32_t c)
{
if(editType_ == EditType::Number && (c < '0' || c > '9'))
return;
if(editType_ == EditType::Filename && !isValidFileNameChar(c))
return;
if(maxLength_ > 0 && text_.size() >= maxLength_)
return;
text_.insert(cursorPos_, 1, c);
CursorRight();
Notify();
}
/**
* entfernt ein Zeichen
*/
void ctrlEdit::RemoveChar()
{
if(cursorPos_ > 0 && !text_.empty())
{
text_.erase(cursorPos_ - 1, 1);
// View verschieben
while(!text_.empty() && text_.length() <= viewStart_)
--viewStart_;
CursorLeft();
Notify();
}
}
/**
* benachrichtigt das Parent ("OnChange")
*/
void ctrlEdit::Notify()
{
if(!notify_ || !GetParent())
return;
GetParent()->Msg_EditChange(GetID());
}
void ctrlEdit::Resize(const Extent& newSize)
{
Window::Resize(newSize);
UpdateInternalText();
}
/**
* Maustaste-gedrückt Callback
*/
bool ctrlEdit::Msg_LeftDown(const MouseCoords& mc)
{
SetFocus(IsPointInRect(mc.pos, GetDrawRect()));
return false; // "Unhandled" so other edits can handle this too and set their focus accordingly
}
/**
* Taste-gedrückt Callback
*/
bool ctrlEdit::Msg_KeyDown(const KeyEvent& ke)
{
auto isDelimiter = [](char32_t c) {
for(const auto cur : U" \t\n-+=")
{
if(cur == c)
return true;
}
return false;
};
// hat das Steuerelement den Fokus?
if(!focus_)
return false;
switch(ke.kt)
{
default: return false;
case KeyType::Left: // Cursor nach Links
// Blockweise nach links, falls Strg gedrückt
if(ke.ctrl)
{
// Erst über alle Trennzeichen hinweg
while(cursorPos_ > 0 && isDelimiter(text_[cursorPos_ - 1]))
{
--cursorPos_;
}
// Und dann über alles, was kein Trenner ist
while(cursorPos_ > 0 && !isDelimiter(text_[cursorPos_ - 1]))
{
--cursorPos_;
}
UpdateInternalText();
} else
CursorLeft(); // just one step
break;
case KeyType::Right: // Cursor nach Rechts
// Blockweise nach rechts, falls Strg gedrückt
if(ke.ctrl)
{
// Erst über alle Trennzeichen hinweg
while(cursorPos_ + 1 < text_.length() && isDelimiter(text_[cursorPos_ + 1]))
{
++cursorPos_;
}
// Und dann über alles, was kein Trenner ist
while(cursorPos_ + 1 < text_.length() && !isDelimiter(text_[cursorPos_ + 1]))
{
++cursorPos_;
}
UpdateInternalText();
} else
CursorRight(); // just one step
break;
case KeyType::Char: // Zeichen eingegeben
if(!isDisabled_ && txtCtrl->GetFont()->CharExist(ke.c))
AddChar(ke.c);
break;
case KeyType::Backspace: // Backspace gedrückt
if(!isDisabled_)
RemoveChar();
break;
case KeyType::Delete: // Entfernen gedrückt
if(!isDisabled_ && cursorPos_ < text_.length())
{
CursorRight();
RemoveChar();
}
break;
case KeyType::Return: // Enter gedrückt
if(!isDisabled_ && GetParent())
GetParent()->Msg_EditEnter(GetID());
break;
case KeyType::Home: // Pos1 gedrückt
if(cursorPos_ > 0)
{
cursorPos_ = 0;
UpdateInternalText();
}
break;
case KeyType::End: // Ende gedrückt
if(cursorPos_ < text_.length())
{
cursorPos_ = text_.length();
UpdateInternalText();
}
break;
}
return true;
}