forked from rhett-lee/nim_duilib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabel.h
More file actions
614 lines (509 loc) · 17.1 KB
/
Copy pathLabel.h
File metadata and controls
614 lines (509 loc) · 17.1 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#ifndef UI_CONTROL_LABEL_H_
#define UI_CONTROL_LABEL_H_
#include "duilib/Control/LabelImpl.h"
#include "duilib/Core/DpiManager.h"
#include "duilib/Box/HBox.h"
#include "duilib/Box/VBox.h"
namespace ui
{
/** 标签控件(模板),用于显示文本
*/
template<typename T = Control>
class UILIB_API LabelTemplate : public T, public LabelOwner
{
typedef T BaseClass;
public:
explicit LabelTemplate(Window* pWindow);
virtual ~LabelTemplate() override;
/// 重写父类方法,提供个性化功能,请参考父类声明
virtual DString GetType() const override;
virtual void SetAttribute(const DString& strName, const DString& strValue) override;
virtual void SetPos(UiRect rc) override;
virtual void SetWindow(Window* pWindow) override;
virtual void PaintText(IRender* pRender) override;
virtual bool HasHotState() override;
virtual DString GetToolTipText() const override;
virtual void OnLanguageChanged() override;
virtual void ChangeDpiScale(uint32_t nOldDpiScale, uint32_t nNewDpiScale) override;
/** 计算文本区域大小(宽和高)
* @param [in] szAvailable 可用大小,不包含内边距,不包含外边距
* @return 控件的文本估算大小,包含内边距(Box),不包含外边距
*/
virtual UiSize EstimateText(UiSize szAvailable) override;
public:
/** 获取文本内容
*/
virtual DString GetText() const override;
/** 设置文本内容
* @param [in] strText 文本内容
*/
virtual void SetText(const DString& strText) override;
/** 获取文本内容ID(支持多语言)
*/
virtual DString GetTextId() const override;
/** 设置文本内容ID(支持多语言)
* @param [in] strTextId 文本内容的ID
*/
virtual void SetTextId(const DString& strTextId) override;
/** 获取文本内容(UTF8格式)
*/
std::string GetUTF8Text() const;
/** 设置文本内容(UTF8格式)
* @param [in] strText UTF8格式的文本内容
*/
void SetUTF8Text(const std::string& strText);
/** 获取文本内容ID(UTF8格式)
*/
std::string GetUTF8TextId() const;
/** 设置文本内容ID(UTF8格式)
*/
void SetUTF8TextId(const std::string& strTextId);
/** 设置文本内容是否为RichText
* @param [in] bRichText 表示支持RichText模式,设置的文本内容可以是RichText格式
* 示例:"一个简单<b>窗口</b><br/>带有<u>标题栏</u>和<u>常规按钮</u>,<b>粗体,<font color='#FF0000'>红色字体</font></b>"
* 备注:RichText模式下,不支持如下功能:
* (1)对齐方式不支持两端对齐
* (2)不支持vertical_text属性(也不支持纵向文本相关属性)
* (3)不支持end_ellipsis属性
* (4)不支持path_ellipsis属性
* (5)不支持auto_tooltip属性
* (6)不支持word_spacing属性
*/
void SetRichText(bool bRichText);
/** 获取文本内容是否为RichText
*/
bool IsRichText() const;
/** 判断文本是否相等
*/
bool IsTextEquals(const DString& text) const;
public:
/** 恢复默认的文本样式
* @param [in] bRedraw true表示重绘,false表示不重绘
*/
void SetDefaultTextStyle(bool bRedraw);
/** 设置文本样式
* @param [in] uStyle 要设置的样式
* @param [in] bRedraw true表示重绘,false表示不重绘
*/
void SetTextStyle(uint32_t uStyle, bool bRedraw);
/** 获取文本样式
* @return 返回文本样式
*/
uint32_t GetTextStyle() const;
/** 获取指定状态下的文本颜色
* @param [in] stateType 要获取的状态标志
* @return 返回指定状态下的文本颜色
*/
DString GetStateTextColor(ControlStateType stateType) const;
/** 设置指定状态下的文本颜色
* @param [in] stateType 要设置的状态标志
* @param [in] dwTextColor 要设置的状态颜色字符串,该值必须在 global.xml 中存在
* @return 无
*/
void SetStateTextColor(ControlStateType stateType, const DString& dwTextColor);
/** 获取指定状态下的实际被渲染文本颜色
* @param [in] buttonStateType 要获取何种状态下的颜色
* @param [out] stateType 实际被渲染的状态
* @return 返回颜色字符串,该值在 global.xml 中定义
*/
DString GetPaintStateTextColor(ControlStateType buttonStateType, ControlStateType& stateType);
/** 获取当前字体ID
* @return 返回字体ID,该字体ID在 global.xml 中标识
*/
DString GetFontId() const;
/** 设置当前字体ID
* @param[in] strFontId 要设置的字体ID,该字体ID必须在 global.xml 中存在
*/
void SetFontId(const DString& strFontId);
/** 获取文字内边距
* @return 返回文字的内边距信息
*/
UiPadding GetTextPadding() const;
/** 设置文字内边距信息
* @param [in] padding 内边距信息
* @param [in] bNeedDpiScale 是否支持DPI缩放
*/
void SetTextPadding(UiPadding padding, bool bNeedDpiScale);
/** 判断是否是单行模式
* @return 返回 true 表示单行模式,否则为 false
*/
bool IsSingleLine() const;
/** 设置为单行输入模式
* @param [in] bSingleLine 为 true 时为单行模式,否则为 false
*/
void SetSingleLine(bool bSingleLine);
/** 设置鼠标悬浮到控件显示的提示文本是否省略号出现时才显示
* @param [in] bAutoShow true 省略号出现才显示 false 不做任何控制
*/
void SetAutoToolTip(bool bAutoShow);
/** 获取鼠标悬浮到控件显示的提示文本是否省略号出现时才显示
*/
bool IsAutoToolTip() const;
/** 设置是否替换换行符(将字符串"\\n"替换为换行符"\n",这样可以在XML中使用括号中这两个字符(\n)来当作换行符,从而支持多行文本)
* @param [in] bReplaceNewline true表示替换,false表示不替换
*/
void SetReplaceNewline(bool bReplaceNewline);
/** 获取是否替换换行符(将字符串"\\n"替换为换行符"\n",这样可以在XML中使用括号中这两个字符(\n)来当作换行符,从而支持多行文本)
* @return true表示替换,false表示不替换
*/
bool IsReplaceNewline() const;
/** 设置行间距(横向文本和纵向文本均有效)
* 设置后,实际的行间距为:字体大小 * mul + add
* @param [in] mul 行间距的倍数, 是字体大小的倍数比例(默认值通常为 1.0,即 100% 字体大小),用于按比例调整行间距。
* @param [in] add 行间距附加量, 是固定的附加像素值(默认值通常为 0),用于在比例调整的基础上增加固定偏移(像素)
* @param [in] bNeedDpiScale 是否支持DPI缩放
*/
void SetLineSpacing(float mul, float add, bool bNeedDpiScale);
/** 获取行间距(横向文本和纵向文本均有效)
* @param [out] mul 返回行间距的倍数
* @param [out] add 返回行间距附加量(像素, 已经完成DPI缩放)
*/
void GetLineSpacing(float* mul, float* add) const;
/** 设置两个相邻的字符之间的间隔(像素)
*/
void SetWordSpacing(float fWordSpacing, bool bNeedDpiScale);
/** 设置两个相邻的字符之间的间隔(像素, 已经完成DPI缩放)
*/
float GetWordSpacing() const;
public:
/** 设置文本方向:true为纵向文本,false为横向文本
* 横向文本:从左到右,从上到下
* 纵向文本:从上到下,从右到左
*/
void SetVerticalText(bool bVerticalText);
/** 获取文本方向
@return true为纵向文本,false为横向文本
*/
bool IsVerticalText() const;
/** 当纵向绘制文本时,设置字间距使用该字体的默认高度,而不是每个字的实际高度(显示时所有字体等高)
*/
void SetUseFontHeight(bool bUseFontHeight);
/** 获取当纵向绘制文本时,字间距使用该字体的默认高度,而不是每个字的实际高度(显示时所有字体等高)
*/
bool IsUseFontHeight() const;
/** 设置当纵向绘制文本时,对于字母、数字等字符,顺时针旋转90度显示
*/
void SetRotate90ForAscii(bool bRotate90ForAscii);
/** 获取当纵向绘制文本时,对于字母、数字等字符,顺时针旋转90度显示
*/
bool IsRotate90ForAscii() const;
public:
/** 获取当前评估绘制文字的参数
* @return 返回当前设置的参数,不含rectSize字段的值
*/
MeasureStringParam GetMeasureParam() const;
/** 获取当前绘制文字的参数
* @return 返回当前设置的参数,不含textRect/dwTextColor/uFade字段的值
*/
DrawStringParam GetDrawParam() const;
/** 辅助函数,获取正确的文本风格,保留支持的风格,包括对齐方式等(详情参见DrawStringFormat定义)
* @param [in] nTextFormat 文本风格
* @return 返回支持的文本风格标志位
*/
static uint32_t GetValidTextStyle(uint32_t nTextFormat);
protected:
/** 绘制文字的实现函数
* @param [in] rc 实际绘制区域,不包含内边距(需由调用方剪去内边距)
* @param [in] pRender 渲染接口
*/
void DoPaintText(const UiRect& rc, IRender* pRender);
private:
//功能内部实现
std::unique_ptr<LabelImpl> m_impl;
};
template<typename T>
LabelTemplate<T>::LabelTemplate(Window* pWindow) :
T(pWindow)
{
m_impl = std::make_unique<LabelImpl>(this);
}
template<typename T>
LabelTemplate<T>::~LabelTemplate()
{
m_impl.reset();
}
template<typename T>
inline DString LabelTemplate<T>::GetType() const { return DUI_CTR_LABEL; }
template<>
inline DString LabelTemplate<Box>::GetType() const { return DUI_CTR_LABELBOX; }
template<>
inline DString LabelTemplate<HBox>::GetType() const { return DUI_CTR_LABELHBOX; }
template<>
inline DString LabelTemplate<VBox>::GetType() const { return DUI_CTR_LABELVBOX; }
template<typename T>
void LabelTemplate<T>::SetAttribute(const DString& strName, const DString& strValue)
{
if (!m_impl->OnSetAttribute(strName, strValue)) {
BaseClass::SetAttribute(strName, strValue);
}
}
template<typename T>
void LabelTemplate<T>::SetWindow(Window* pWindow)
{
Window* pOldWindow = this->GetWindow();
BaseClass::SetWindow(pWindow);
if (pOldWindow != pWindow) {
m_impl->OnWindowChanged();
}
}
template<typename T>
void LabelTemplate<T>::ChangeDpiScale(uint32_t nOldDpiScale, uint32_t nNewDpiScale)
{
if (!this->Dpi().CheckDisplayScaleFactor(nNewDpiScale)) {
return;
}
m_impl->OnDpiScaleChanged(nOldDpiScale, nNewDpiScale);
BaseClass::ChangeDpiScale(nOldDpiScale, nNewDpiScale);
}
template<typename T>
void LabelTemplate<T>::OnLanguageChanged()
{
BaseClass::OnLanguageChanged();
//语言发生变化,字符串长度可能发生了变化,需要重新计算布局,更新ToolTip数据
this->RelayoutOrRedraw();
m_impl->OnLanguageChanged();
}
template<typename T>
uint32_t LabelTemplate<T>::GetValidTextStyle(uint32_t nTextFormat)
{
return LabelImpl::GetValidTextStyle(nTextFormat);
}
template<typename T>
void LabelTemplate<T>::SetAutoToolTip(bool bAutoShow)
{
m_impl->SetAutoShowToolTipEnabled(bAutoShow);
}
template<typename T>
bool LabelTemplate<T>::IsAutoToolTip() const
{
return m_impl->IsAutoShowToolTipEnabled();
}
template<typename T>
void LabelTemplate<T>::SetReplaceNewline(bool bReplaceNewline)
{
m_impl->SetReplaceNewline(bReplaceNewline);
}
template<typename T>
bool LabelTemplate<T>::IsReplaceNewline() const
{
return m_impl->IsReplaceNewline();
}
template<typename T>
void LabelTemplate<T>::SetVerticalText(bool bVerticalText)
{
m_impl->SetVerticalText(bVerticalText);
}
template<typename T>
bool LabelTemplate<T>::IsVerticalText() const
{
return m_impl->IsVerticalText();
}
template<typename T>
void LabelTemplate<T>::SetLineSpacing(float mul, float add, bool bNeedDpiScale)
{
m_impl->SetLineSpacing(mul, add, bNeedDpiScale);
}
template<typename T>
void LabelTemplate<T>::GetLineSpacing(float* mul, float* add) const
{
m_impl->GetLineSpacing(mul, add);
}
template<typename T>
void LabelTemplate<T>::SetWordSpacing(float fWordSpacing, bool bNeedDpiScale)
{
m_impl->SetWordSpacing(fWordSpacing, bNeedDpiScale);
}
template<typename T>
float LabelTemplate<T>::GetWordSpacing() const
{
return m_impl->GetWordSpacing();
}
template<typename T>
void LabelTemplate<T>::SetUseFontHeight(bool bUseFontHeight)
{
m_impl->SetUseFontHeight(bUseFontHeight);
}
template<typename T>
bool LabelTemplate<T>::IsUseFontHeight() const
{
return m_impl->IsUseFontHeight();
}
template<typename T>
void LabelTemplate<T>::SetRotate90ForAscii(bool bRotate90ForAscii)
{
m_impl->SetRotate90ForAscii(bRotate90ForAscii);
}
template<typename T>
bool LabelTemplate<T>::IsRotate90ForAscii() const
{
return m_impl->IsRotate90ForAscii();
}
template<typename T>
MeasureStringParam LabelTemplate<T>::GetMeasureParam() const
{
return m_impl->GetMeasureParam();
}
template<typename T>
DrawStringParam LabelTemplate<T>::GetDrawParam() const
{
return m_impl->GetDrawParam();
}
template<typename T /*= Control*/>
void ui::LabelTemplate<T>::SetPos(UiRect rc)
{
BaseClass::SetPos(rc);
m_impl->CheckShowToolTip();
}
template<typename T>
DString LabelTemplate<T>::GetToolTipText() const
{
DString toolTip = BaseClass::GetToolTipText();
if (toolTip.empty()) {
toolTip = m_impl->GetAutoToolTipText();
}
return toolTip;
}
template<typename T>
DString LabelTemplate<T>::GetText() const
{
return m_impl->GetText();
}
template<typename T>
void LabelTemplate<T>::SetText(const DString& strText)
{
m_impl->SetText(strText);
}
template<typename T>
DString LabelTemplate<T>::GetTextId() const
{
return m_impl->GetTextId();
}
template<typename T>
void LabelTemplate<T>::SetTextId(const DString& strTextId)
{
m_impl->SetTextId(strTextId);
}
template<typename T>
std::string LabelTemplate<T>::GetUTF8Text() const
{
return m_impl->GetUTF8Text();
}
template<typename T>
void LabelTemplate<T>::SetUTF8Text(const std::string& strText)
{
m_impl->SetUTF8Text(strText);
}
template<typename T>
void LabelTemplate<T>::SetUTF8TextId(const std::string& strTextId)
{
m_impl->SetUTF8TextId(strTextId);
}
template<typename T>
std::string LabelTemplate<T>::GetUTF8TextId() const
{
return m_impl->GetUTF8TextId();
}
template<typename T>
void LabelTemplate<T>::SetRichText(bool bRichText)
{
m_impl->SetRichText(bRichText);
}
template<typename T>
bool LabelTemplate<T>::IsRichText() const
{
return m_impl->IsRichText();
}
template<typename T>
bool LabelTemplate<T>::IsTextEquals(const DString& text) const
{
return m_impl->IsTextEquals(text);
}
template<typename T>
bool LabelTemplate<T>::HasHotState()
{
if (BaseClass::HasHotState()) {
return true;
}
return m_impl->HasHotColorState();
}
template<typename T>
UiSize LabelTemplate<T>::EstimateText(UiSize szAvailable)
{
return m_impl->OnEstimateText(szAvailable);
}
template<typename T>
void LabelTemplate<T>::PaintText(IRender* pRender)
{
m_impl->OnPaintText(pRender);
}
template<typename T>
void LabelTemplate<T>::SetTextStyle(uint32_t uStyle, bool bRedraw)
{
m_impl->SetTextStyle(uStyle, bRedraw);
}
template<typename T>
void LabelTemplate<T>::SetDefaultTextStyle(bool bRedraw)
{
m_impl->SetDefaultTextStyle(bRedraw);
}
template<typename T>
uint32_t LabelTemplate<T>::GetTextStyle() const
{
return m_impl->GetTextStyle();
}
template<typename T>
DString LabelTemplate<T>::GetStateTextColor(ControlStateType stateType) const
{
return m_impl->GetStateTextColor(stateType);
}
template<typename T>
void LabelTemplate<T>::SetStateTextColor(ControlStateType stateType, const DString& dwTextColor)
{
m_impl->SetStateTextColor(stateType, dwTextColor);
}
template<typename T /*= Control*/>
DString ui::LabelTemplate<T>::GetPaintStateTextColor(ControlStateType buttonStateType, ControlStateType& stateType)
{
return m_impl->GetPaintStateTextColor(buttonStateType, stateType);
}
template<typename T>
DString LabelTemplate<T>::GetFontId() const
{
return m_impl->GetFontId();
}
template<typename T>
void LabelTemplate<T>::SetFontId(const DString& strFontId)
{
m_impl->SetFontId(strFontId);
}
template<typename T>
UiPadding LabelTemplate<T>::GetTextPadding() const
{
return m_impl->GetTextPadding();
}
template<typename T>
void LabelTemplate<T>::SetTextPadding(UiPadding padding, bool bNeedDpiScale)
{
m_impl->SetTextPadding(padding, bNeedDpiScale);
}
template<typename T>
bool LabelTemplate<T>::IsSingleLine() const
{
return m_impl->IsSingleLine();
}
template<typename T>
void LabelTemplate<T>::SetSingleLine(bool bSingleLine)
{
m_impl->SetSingleLine(bSingleLine);
}
template<typename T>
void LabelTemplate<T>::DoPaintText(const UiRect& rc, IRender* pRender)
{
m_impl->DoPaintText(rc, pRender);
}
typedef LabelTemplate<Control> Label;
typedef LabelTemplate<Box> LabelBox;
typedef LabelTemplate<HBox> LabelHBox;
typedef LabelTemplate<VBox> LabelVBox;
}
#endif // UI_CONTROL_LABEL_H_