YSTest: TextBox.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

TextBox.h
浏览该文件的文档.
1 /*
2 © 2014-2018, 2020 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YSL_INC_UI_TextBox_h_
29 #define YSL_INC_UI_TextBox_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_UI_YControl
33 #include YFM_YSLib_UI_Label
34 #include YFM_YSLib_Service_YTimer
35 #include YFM_YSLib_UI_YGUI
36 #include YFM_YSLib_UI_Animation
37
38 namespace YSLib
39 {
40
41 namespace UI
42 {
43
48 class YF_API TextPlaceholder
49 {
50 private:
52 observer_ptr<IWidget> p_captured = {};
53
54 public:
55 Drawing::Font Font{};
56 Color ForeColor{ColorSpace::Gray};
57 String Text{};
58 char32_t MaskChar = char32_t();
59
61 DefGetter(const ynothrow, observer_ptr<IWidget>, CapturedPtr, p_captured)
62
63 template<class _tControl, typename _fSwap>
64 void
65 BindByFocus(_tControl& ctl, _fSwap f)
66 {
67 yunseq(
68 FetchEvent<GotFocus>(ctl) += [&, f, this]{
69 if(p_captured.get() == std::addressof(ctl) && Text.empty())
70 {
71 ystdex::invoke(f, this, ctl);
72 p_captured = {};
73 }
74 },
75 FetchEvent<LostFocus>(ctl) += [&, f, this]{
76 if(!p_captured && ctl.Text.empty())
77 {
78 ystdex::invoke(f, this, ctl);
79 p_captured = YSLib::make_observer(std::addressof(ctl));
80 }
81 }
82 );
83 }
84
85 template<class _tControl>
86 void
87 SwapLabel(_tControl& ctl)
88 {
89 SwapText(ctl);
90 ystdex::swap_dependent(ctl.Font, Font),
91 ystdex::swap_dependent(ctl.ForeColor, ForeColor);
92 }
93
94 template<class _tControl>
95 void
96 SwapText(_tControl& ctl)
97 {
98 ystdex::swap_dependent(ctl.Text, Text);
99 }
100
101 template<class _tControl>
102 void
103 SwapTextBox(_tControl& ctl)
104 {
105 SwapLabel(ctl);
106 ystdex::swap_dependent(ctl.MaskChar, MaskChar);
107 }
108 };
109
110
115 class YF_API Caret final
116 {
117 private:
122 static GAnimationSession<InvalidationUpdater> caret_animation;
123
124 public:
126 Timers::Timer CaretTimer{std::chrono::seconds(1)};
131 HBrush CaretBrush;
136 InvalidationUpdater::Invalidator CursorInvalidator;
137
143 Caret(IWidget&, HBrush, InvalidationUpdater::Invalidator);
144 ~Caret();
145
147 bool
148 Check(IWidget&);
149
154 static void
155 Stop() ynothrow;
156 };
157
158
163 struct YF_API TextSelection final
164 {
166 using Position = GBinaryGroup<size_t>;
171 using Span = pair<Position, Position>;
172
174 Span Range;
175
180 DefPred(const ynothrow, Empty, Range.first == Range.second)
181
182
186 PDefH(void, Collapse, )
187 ImplExpr(Range.first = Range.second)
188 };
189
190
197 class YF_API TextBox : public Control, protected MLabel, protected MHilightText
198 {
199 public:
201 using MLabel::ForeColor;
202 using MLabel::Font;
203 using MLabel::Margin;
204 using MLabel::HorizontalAlignment;
205 using MLabel::VerticalAlignment;
206 using MLabel::AutoWrapLine;
207 using MLabel::Text;
209 using MHilightText::HilightBackColor;
211 using MHilightText::HilightTextColor;
212
217 TextSelection Selection;
222 Caret CursorCaret;
228 size_t MaxLength{size_t(-1)};
237 char32_t MaskChar = char32_t();
238
239 private:
244 SDst h_offset;
245
246 public:
251 explicit
252 TextBox(const Rect& = {}, const Drawing::Font& = {}, const
253 pair<Drawing::Color, Drawing::Color>& = FetchGUIConfiguration().Colors
254 .GetPair(Styles::Highlight, Styles::HighlightText));
255
262 YB_ATTR_nodiscard YB_PURE Point
263 GetCaretLocation() const;
270 YB_ATTR_nodiscard YB_PURE TextSelection::Position
271 GetCaretPosition(const Point&) const;
276 YB_ATTR_nodiscard YB_PURE Point
277 GetPenOffset() const;
278
284 void
285 CollapseCaret();
286
292 void
293 ExportCaretLocation() const;
294
301 static bool
302 InvalidateDefaultCaret(IWidget&);
303
305 void
306 PaintDefaultCaret(PaintEventArgs&&);
307
309 void
310 Refresh(PaintEventArgs&&) override;
311
316 void
317 SelectAll();
318
323 PDefH(void, RestoreCaretTimer, )
324 ImplExpr(Activate(CursorCaret.CaretTimer))
325
333 void
334 ReplaceSelection(const String&);
335
337 void
338 UpdateTextBoxClippedText(const PaintContext&, TextState&);
339 };
340
341 } // namespace UI;
342
343 } // namespace YSLib;
344
345 #endif
346
YSLib::UI::InvalidationUpdater::Invalidator
function< bool(IWidget &)> Invalidator
Definition: Animation.h:130
YSLib::Drawing::TextState
文本状态。
Definition: TextBase.h:87
YSLib::UI::Caret::CaretBrush
HBrush CaretBrush
插入符画刷。
Definition: TextBox.h:131
YB_ATTR_nodiscard
#define YB_ATTR_nodiscard
标记返回值不被忽略的属性。
Definition: ydef.h:715
YSLib::UI::MLabel::HorizontalAlignment
TextAlignment HorizontalAlignment
文本水平和竖直对齐属性。
Definition: Label.h:85
YSLib::UI::MHilightText::HilightTextColor
Color HilightTextColor
高亮文本色。
Definition: Label.h:217
YSLib::UI::TextBox::h_offset
SDst h_offset
文字区域水平基准负偏移：文本内容在光标回退方向超出的未显示部分大小。
Definition: TextBox.h:244
YSLib::UI::Caret::caret_animation
static GAnimationSession< InvalidationUpdater > caret_animation
插入符闪烁动画。
Definition: TextBox.h:122
YSLib::UI::ynothrow
AView &y ynothrow
Definition: YWidgetView.cpp:47
ystdex::invoke
invoke_result_t< _fCallable, _tParams... > invoke(_fCallable &&f, _tParams &&... args)
调用可调用对象。
Definition: invoke.hpp:538
YSLib::UI::MHilightText::HilightBackColor
Color HilightBackColor
高亮背景色。
Definition: Label.h:215
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
YSLib::UI::TextBox::Selection
TextSelection Selection
选择区域：结束位置指示插入符光标逻辑位置。
Definition: TextBox.h:217
YSLib::Consoles::Gray
@ Gray
Definition: YConsole.h:60
YSLib::UI::Caret
插入符光标。
Definition: TextBox.h:116
ystdex::function_base
函数包装类模板。
Definition: function.hpp:578
YSLib::UI::Control
控件。
Definition: YControl.h:271
YSLib::UI::MLabel::VerticalAlignment
TextAlignment VerticalAlignment
Definition: Label.h:86
YSLib::UI::TextPlaceholder::DefGetter
DefGetter(const ynothrow, observer_ptr< IWidget >, CapturedPtr, p_captured) template< class _tControl
YSLib::UI::Styles::Highlight
@ Highlight
高亮背景。
Definition: YStyle.h:319
YF_API
#define YF_API
Definition: Platform.h:56
YSLib::UI::TextBox
文本框。
Definition: TextBox.h:198
YSLib::UI::Caret::CursorInvalidator
InvalidationUpdater::Invalidator CursorInvalidator
供闪烁动画使用的指定无效化区域的光标位置刷新器。
Definition: TextBox.h:136
YSLib::UI::TextPlaceholder::SwapLabel
void SwapLabel(_tControl &ctl)
Definition: TextBox.h:87
YSLib::UI::TextPlaceholder::SwapText
void SwapText(_tControl &ctl)
Definition: TextBox.h:96
CHRLib::f
f(d, s)
ystdex::swap_dependent
void swap_dependent(_type &x, _type &y)(is_nothrow_swappable< _type >())
支持基于 ystdex_swap::swap 的 ADL 的左值交换。
Definition: swap.hpp:371
YSLib::UI::GAnimationSession< InvalidationUpdater >
YSLib::UI::MLabel::AutoWrapLine
bool AutoWrapLine
启用自动换行。
Definition: Label.h:91
NPL::Collapse
pair< TermReference, bool > Collapse(TermReference ref)
Definition: NPLA.cpp:614
YSLib::UI::TextSelection::DefPred
DefPred(const ynothrow, Empty, Range.first==Range.second) PDefH(void
判断是否为空。
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: pointer.hpp:405
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: YString.h:47
YSLib::UI::TextBox::PDefH
PDefH(void, RestoreCaretTimer,) ImplExpr(Activate(CursorCaret.CaretTimer)) void ReplaceSelection(const String &)
重置插入符光标计时器。
YSLib::UI::TextSelection::Range
Span Range
选择的范围。
Definition: TextBox.h:174
YSLib::UI::MLabel::Font
Drawing::Font Font
字体。
Definition: Label.h:78
YSLib::UI::FetchGUIConfiguration
GUIConfiguration & FetchGUIConfiguration()
Definition: YGUI.cpp:517
platform::FAT::LFN::MaxLength
@ MaxLength
UCS-2 项最大长度。
Definition: YCLib/FileSystem.h:1157
YSLib::Drawing::GBinaryGroup
屏幕二元组。
Definition: YGDIBase.h:295
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1535
YSLib::UI::TextPlaceholder::SwapTextBox
void SwapTextBox(_tControl &ctl)
Definition: TextBox.h:103
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:44
YSLib::UI::MLabel::ForeColor
Color ForeColor
默认前景色。
Definition: Label.h:77
YSLib::UI::TextPlaceholder
添加默认文本到特定的部件。
Definition: TextBox.h:49
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:788
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
Color
Pixel Color
Definition: CharRenderer.cpp:59
YSLib::Activate
Activate
Definition: YApplication.h:210
YSLib::UI::MLabel
标签模块。
Definition: Label.h:62
YModules.h
YSLib 用户界面模块配置文件。
YSLib::UI::MLabel::Margin
Drawing::Padding Margin
文本和容器的间距。
Definition: Label.h:79
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
YSLib::UI::TextSelection
文本选择区域。
Definition: TextBox.h:164
YSLib::UI::TextSelection::Span
pair< Position, Position > Span
文本区段：表示起始和结束位置。
Definition: TextBox.h:171
YSLib::UI::PaintEventArgs
部件绘制参数。
Definition: YWidgetEvent.h:330
YSLib::Timers::Timer
计时器。
Definition: YTimer.h:78
YSLib::UI::MHilightText
高亮文本模块。
Definition: Label.h:212
YSLib::UI::MLabel::Text
String Text
标签文本。
Definition: Label.h:94
ystdex::addressof
enable_if_t< has_no_overloaded_addressof< _type >::value, _type * > addressof(_type &r)
尝试对非重载 operator& 提供 constexpr 的 std::addressof 替代。
Definition: addressof.hpp:131
YSLib::UI::TextBox::CursorCaret
Caret CursorCaret
插入符光标。
Definition: TextBox.h:222
ImplExpr
#define ImplExpr(...)
简单通用函数实现。
Definition: YBaseMacro.h:96
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:589
YSLib::size_t
YF_API const char size_t
Definition: YException.h:149
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
YSLib::Drawing::char32_t
YB_ATTR_nodiscard YB_PURE tr TextRegion &tr char32_t
Definition: TextLayout.h:93
YSLib::UI::TextPlaceholder::BindByFocus
_fSwap void BindByFocus(_tControl &ctl, _fSwap f)
Definition: TextBox.h:65
YSLib::UI::Styles::HighlightText
@ HighlightText
Definition: YStyle.h:327
YSLib::PDefH
PDefH(Environment &, FetchEnvironment,) ImplRet(FetchGlobalInstance().GetEnvironmentRef()) inline PDefH(GUIHost &
取单一环境对象引用。