File: HyperTextBox.cpp

package info (click to toggle)
mygui 3.4.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,792 kB
  • sloc: cpp: 133,849; ansic: 30,249; xml: 15,794; cs: 12,601; tcl: 776; python: 400; makefile: 35; sh: 4
file content (653 lines) | stat: -rw-r--r-- 15,814 bytes parent folder | download
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*!
	@file
	@author		Albert Semenov
	@date		03/2011
*/

#include "MyGUI_Precompiled.h"
#include <MyGUI_ResourceImageSet.h>
#include <MyGUI_ImageBox.h>
#include <MyGUI_TextBox.h>
#include <MyGUI_WidgetManager.h>
#include <MyGUI_FontManager.h>
#include <MyGUI_IFont.h>
#include "HyperTextBox.h"

namespace MyGUI
{

	HyperTextBox::HyperTextBox() :
		mTextSkin("TextBox"),
		mImageSkin("ImageBox"),
		mParagraphSkin("Default"),
		mLineSkin("WhiteSkin"),
		mLinkPoiner("link"),
		mHeader1Font("Default"),
		mHeader2Font("Default"),
		mHeader3Font("Default"),
		mDefaultFont("Default"),
		mBoldFont("Default"),
		mItalicFont("Default"),
		mBoldItalicFont("Default")
	{
	}

	void HyperTextBox::initialiseOverride()
	{
		Base::initialiseOverride();

		assignWidget(mScrollViewPanel, "ScrollViewPanel");
		assignWidget(mStackPanel, "StackPanel");

		if (isUserString("TextSkin"))
			mTextSkin = getUserString("TextSkin");
		if (isUserString("ImageSkin"))
			mImageSkin = getUserString("ImageSkin");
		if (isUserString("ParagraphSkin"))
			mParagraphSkin = getUserString("ParagraphSkin");
		if (isUserString("LineSkin"))
			mLineSkin = getUserString("LineSkin");
		if (isUserString("LinkPointer"))
			mLinkPoiner = getUserString("LinkPointer");

		updateBreakHeight();
	}

	void HyperTextBox::shutdownOverride()
	{
		Base::shutdownOverride();
	}

	void HyperTextBox::addItem(std::string_view _value)
	{
		parseParagraph(mStackPanel, _value);
		mCurrentWrapPanel = nullptr;
	}

	void HyperTextBox::parseParagraph(Widget* _parent, std::string_view _value)
	{
		auto textItem = _value.end();
		for (auto item = _value.begin(); item != _value.end(); ++item)
		{
			if ((*item) == '<')
			{
				// отрезаем текст
				if (textItem < item)
				{
					if (mCurrentWrapPanel != nullptr)
						parseText(mCurrentWrapPanel, _value.substr(textItem - _value.begin(), item - textItem));
				}

				// ищем конец тега
				for (auto tagItem = item; tagItem != _value.end(); ++tagItem)
				{
					if ((*tagItem) == '>')
					{
						parseTag(_value.substr(item - _value.begin(), tagItem - item + 1));

						item = tagItem;
						textItem = tagItem + 1;
						break;
					}
				}
			}
		}
	}

	void HyperTextBox::parseText(Widget* _parent, std::string_view _value)
	{
		const int defaultSize = 16;

		if (mImage)
		{
			if (mImageSize.width == 0 || mImageSize.height == 0)
			{
				IResource* resource = ResourceManager::getInstance().getByName(_value, false);
				if (resource != nullptr)
				{
					ResourceImageSet* imageSet = resource->castType<ResourceImageSet>(false);
					if (imageSet != nullptr)
					{
						ImageIndexInfo info = imageSet->getIndexInfo(0, 0);
						mImageSize = info.size;
					}
				}
			}

			ImageBox* image = _parent->createWidget<ImageBox>(
				mImageSkin,
				IntCoord(0, 0, mImageSize.width, mImageSize.height),
				Align::Default);
			image->setItemResource(_value);
			// картинка как урл
			if (mUrl)
			{
				image->setPointer(mLinkPoiner);
				image->eventMouseButtonClick += newDelegate(this, &HyperTextBox::OnTextClick);
				image->setUserString("URL", mUrlValue);
			}
		}
		else if (mUrl)
		{
			TextBox* text =
				_parent->createWidget<TextBox>(mTextSkin, IntCoord(0, 0, defaultSize, defaultSize), Align::Default);
			text->setCaption(MyGUI::UString(_value));
			text->setPointer(mLinkPoiner);
			if (mBold)
			{
				if (mItalic)
					text->setFontName(mBoldItalicFont);
				else
					text->setFontName(mBoldFont);
			}
			else if (mItalic)
			{
				text->setFontName(mItalicFont);
			}

			Widget* line = text->createWidget<Widget>(
				mLineSkin,
				IntCoord(0, defaultSize - 1, defaultSize, 1),
				Align::HStretch | Align::Bottom);
			line->setColour(Colour::Black);
			line->setVisible(false);
			line->setNeedMouseFocus(false);

			text->eventMouseLostFocus += newDelegate(this, &HyperTextBox::OnTextLostFocus);
			text->eventMouseSetFocus += newDelegate(this, &HyperTextBox::OnTextSetFocus);
			text->eventMouseButtonClick += newDelegate(this, &HyperTextBox::OnTextClick);
			text->setUserString("URL", mUrlValue);
		}
		else
		{
			VectorString result = utility::split(_value);
			for (const auto& item : result)
			{
				TextBox* text =
					_parent->createWidget<TextBox>(mTextSkin, IntCoord(0, 0, defaultSize, defaultSize), Align::Default);
				text->setCaption(item);
				if (mBold)
				{
					if (mItalic)
						text->setFontName(mBoldItalicFont);
					else
						text->setFontName(mBoldFont);
				}
				else if (mItalic)
				{
					text->setFontName(mItalicFont);
				}
				else if (mHeader1)
				{
					text->setFontName(mHeader1Font);
				}
				else if (mHeader2)
				{
					text->setFontName(mHeader2Font);
				}
				else if (mHeader3)
				{
					text->setFontName(mHeader3Font);
				}

				if (mColour)
				{
					text->setTextColour(mColourValue);
				}

				if (mStrike)
				{
					Widget* line = text->createWidget<Widget>(
						mLineSkin,
						IntCoord(0, 0, defaultSize, 1),
						Align::HStretch | Align::VCenter);
					line->setColour(Colour::Black);
					line->setNeedMouseFocus(false);
					if (mColour)
						line->setColour(mColourValue);
				}

				if (mUnder)
				{
					Widget* line = text->createWidget<Widget>(
						mLineSkin,
						IntCoord(0, defaultSize - 1, defaultSize, 1),
						Align::HStretch | Align::Bottom);
					line->setColour(Colour::Black);
					line->setNeedMouseFocus(false);
					if (mColour)
						line->setColour(mColourValue);
				}
			}
		}
	}

	void HyperTextBox::parseTag(std::string_view _value)
	{
		const std::string_view imageStartTagName = "<img";
		const std::string_view imageEndTagName = ">";

		const std::string_view paragraphStartTagName = "<p";
		const std::string_view paragraphEndTagName = ">";

		const std::string_view colourStartTagName = "<color";
		const std::string_view colourEndTagName = ">";

		const std::string_view urlStartTagName = "<url";
		const std::string_view urlEndTagName = ">";

		if (_value == "<h1>")
		{
			mHeader1 = true;
		}
		else if (_value == "</h1>")
		{
			mHeader1 = false;
		}
		else if (_value == "<h2>")
		{
			mHeader2 = true;
		}
		else if (_value == "</h2>")
		{
			mHeader2 = false;
		}
		else if (_value == "<h3>")
		{
			mHeader3 = true;
		}
		else if (_value == "</h3>")
		{
			mHeader3 = false;
		}
		else if (_value == "<b>")
		{
			mBold = true;
		}
		else if (_value == "</b>")
		{
			mBold = false;
		}
		else if (_value == "<i>")
		{
			mItalic = true;
		}
		else if (_value == "</i>")
		{
			mItalic = false;
		}
		else if (_value == "<s>")
		{
			mStrike = true;
		}
		else if (_value == "</s>")
		{
			mStrike = false;
		}
		else if (_value == "<u>")
		{
			mUnder = true;
		}
		else if (_value == "</u>")
		{
			mUnder = false;
		}
		else if (utility::startWith(_value, colourStartTagName))
		{
			mColour = true;
			mColourValue = Colour::Black;

			const std::string_view colourTagName = "value=";

			std::string_view valueColour = _value.substr(
				colourStartTagName.size(),
				_value.size() - (colourStartTagName.size() + colourEndTagName.size()));
			VectorString result = utility::split(valueColour);
			for (const auto& item : result)
			{
				if (utility::startWith(item, colourTagName))
				{
					if ((colourTagName.size() + 2) < (item.size()))
					{
						std::string_view value = std::string_view(item).substr(
							colourTagName.size() + 1,
							item.size() - (colourTagName.size() + 2));
						mColourValue = Colour::parse(value);
					}
				}
			}
		}
		else if (_value == "</color>")
		{
			mColour = false;
		}
		else if (utility::startWith(_value, urlStartTagName))
		{
			mUrl = true;
			mUrlValue.clear();

			const std::string_view urlTagName = "value=";

			std::string_view valueUrl =
				_value.substr(urlStartTagName.size(), _value.size() - (urlStartTagName.size() + urlEndTagName.size()));
			VectorString result = utility::split(valueUrl);
			for (const auto& item : result)
			{
				if (utility::startWith(item, urlTagName))
				{
					if ((urlTagName.size() + 2) < (item.size()))
					{
						mUrlValue = item.substr(urlTagName.size() + 1, item.size() - (urlTagName.size() + 2));
					}
				}
			}
		}
		else if (_value == "</url>")
		{
			mUrl = false;
		}
		else if (utility::startWith(_value, paragraphStartTagName))
		{
			mCurrentWrapPanel = mStackPanel->createWidget<WrapPanel>(mParagraphSkin, IntCoord(), Align::Default);
			mCurrentWrapPanel->setContentAlign(Align::Left | Align::Bottom);
			mCurrentWrapPanel->setSpacer(mSpacer);

			Align alignResult = Align::Default;
			bool needAlign = false;

			Align floatResult = Align::Default;
			bool needFloat = false;

			const std::string_view alightTagName = "align=";
			const std::string_view floatTagName = "float=";

			std::string_view valueParagraph = _value.substr(
				paragraphStartTagName.size(),
				_value.size() - (paragraphStartTagName.size() + paragraphEndTagName.size()));
			VectorString result = utility::split(valueParagraph);
			for (const auto& item : result)
			{
				if (utility::startWith(item, alightTagName))
				{
					if ((alightTagName.size() + 2) < (item.size()))
					{
						needAlign = true;
						std::string_view value = std::string_view(item).substr(
							alightTagName.size() + 1,
							item.size() - (alightTagName.size() + 2));
						if (value == "left")
							alignResult = Align::Default;
						else if (value == "center")
							alignResult = Align::HCenter | Align::Top;
						else if (value == "right")
							alignResult = Align::Right | Align::Top;
						else
							alignResult = Align::Default;
					}
				}
				else if (utility::startWith(item, floatTagName))
				{
					if ((floatTagName.size() + 2) < (item.size()))
					{
						needFloat = true;
						std::string_view value = std::string_view(item).substr(
							floatTagName.size() + 1,
							item.size() - (floatTagName.size() + 2));
						if (value == "left")
							floatResult = Align::Default;
						else if (value == "center")
							floatResult = Align::HCenter | Align::Top;
						else if (value == "right")
							floatResult = Align::Right | Align::Top;
						else
							floatResult = Align::Default;
					}
				}
			}

			if (needAlign)
			{
				mCurrentWrapPanel->setContentAlign(alignResult);
			}
			if (needFloat)
			{
				mCurrentWrapPanel->setContentFloat(true);
				mCurrentWrapPanel->setSnapFloat(floatResult);
			}
		}
		else if (_value == "</p>")
		{
			mCurrentWrapPanel = nullptr;
		}
		else if (utility::startWith(_value, imageStartTagName))
		{
			mImageSize.clear();
			mImage = true;

			const std::string_view widthTagName = "width=";
			const std::string_view heightTagName = "height=";

			std::string_view valueImage = _value.substr(
				imageStartTagName.size(),
				_value.size() - (imageStartTagName.size() + imageEndTagName.size()));
			VectorString result = utility::split(valueImage);
			for (const auto& item : result)
			{
				if (utility::startWith(item, widthTagName))
				{
					if ((widthTagName.size() + 2) < (item.size()))
					{
						std::string_view value = std::string_view(item).substr(
							widthTagName.size() + 1,
							item.size() - (widthTagName.size() + 2));
						mImageSize.width = utility::parseValue<int>(value);
					}
				}
				else if (utility::startWith(item, heightTagName))
				{
					if ((heightTagName.size() + 2) < (item.size()))
					{
						std::string_view value = std::string_view(item).substr(
							heightTagName.size() + 1,
							item.size() - (heightTagName.size() + 2));
						mImageSize.height = utility::parseValue<int>(value);
					}
				}
			}
		}
		else if (_value == "</img>")
		{
			mImage = false;
		}
		else if (_value == "<br/>")
		{
			if (mCurrentWrapPanel == nullptr)
			{
				WrapPanel* panel = mStackPanel->createWidget<WrapPanel>(mParagraphSkin, IntCoord(), Align::Default);
				panel->setContentAlign(Align::Left | Align::Bottom);
				panel->setSpacer(mSpacer);
				panel->createWidget<Widget>("Default", IntCoord(0, 0, 0, mBreakHeight), Align::Default);
			}
		}
		else if (_value == "<t/>")
		{
			if (mCurrentWrapPanel != nullptr)
			{
				const int countInTab = 4;
				mCurrentWrapPanel->createWidget<Widget>(
					"Default",
					IntCoord(0, 0, countInTab * mSpacer.width, 0),
					Align::Default);
			}
		}
	}

	void HyperTextBox::OnTextLostFocus(Widget* _sender, Widget* _new)
	{
		size_t count = _sender->getChildCount();
		if (count > 0)
			_sender->getChildAt(0)->setVisible(false);
	}

	void HyperTextBox::OnTextSetFocus(Widget* _sender, Widget* _old)
	{
		size_t count = _sender->getChildCount();
		if (count > 0)
			_sender->getChildAt(0)->setVisible(true);
	}

	void HyperTextBox::OnTextClick(Widget* _sender)
	{
		std::string_view url = _sender->getUserString("URL");
		eventUrlClick(this, url);
	}

	void HyperTextBox::updateContent()
	{
		mStackPanel->setSpacer(IntSize(0, mSpacer.height));
		mScrollViewPanel->updateContent();
	}

	const IntSize& HyperTextBox::getSpacer() const
	{
		return mSpacer;
	}

	void HyperTextBox::setSpacer(const IntSize& _value)
	{
		mSpacer = _value;
	}

	const std::string& HyperTextBox::getHeader1Font() const
	{
		return mHeader1Font;
	}

	void HyperTextBox::setHeader1Font(std::string_view _value)
	{
		mHeader1Font = _value;
	}

	const std::string& HyperTextBox::getHeader2Font() const
	{
		return mHeader2Font;
	}

	void HyperTextBox::setHeader2Font(std::string_view _value)
	{
		mHeader2Font = _value;
	}

	const std::string& HyperTextBox::getHeader3Font() const
	{
		return mHeader3Font;
	}

	void HyperTextBox::setHeader3Font(std::string_view _value)
	{
		mHeader3Font = _value;
	}

	const std::string& HyperTextBox::getDefaultFont() const
	{
		return mDefaultFont;
	}

	void HyperTextBox::setDefaultFont(std::string_view _value)
	{
		mDefaultFont = _value;
		updateBreakHeight();
	}

	const std::string& HyperTextBox::getBoldFont() const
	{
		return mBoldFont;
	}

	void HyperTextBox::setBoldFont(std::string_view _value)
	{
		mBoldFont = _value;
	}

	const std::string& HyperTextBox::getItalicFont() const
	{
		return mItalicFont;
	}

	void HyperTextBox::setItalicFont(std::string_view _value)
	{
		mItalicFont = _value;
	}

	const std::string& HyperTextBox::getBoldItalicFont() const
	{
		return mBoldItalicFont;
	}

	void HyperTextBox::setBoldItalicFont(std::string_view _value)
	{
		mBoldItalicFont = _value;
	}

	void HyperTextBox::setPropertyOverride(std::string_view _key, std::string_view _value)
	{
		if (_key == "Spacer")
			setSpacer(utility::parseValue<IntSize>(_value));
		else if (_key == "DefaultFont")
			setDefaultFont(_value);
		else if (_key == "BoldFont")
			setBoldFont(_value);
		else if (_key == "ItalicFont")
			setItalicFont(_value);
		else if (_key == "BoldItalicFont")
			setBoldItalicFont(_value);
		else if (_key == "Header1Font")
			setHeader1Font(_value);
		else if (_key == "Header2Font")
			setHeader2Font(_value);
		else if (_key == "Header3Font")
			setHeader3Font(_value);
		else
		{
			Base::setPropertyOverride(_key, _value);
			return;
		}
		eventChangeProperty(this, _key, _value);
	}

	void HyperTextBox::setCaption(const UString& _value)
	{
		removeAllItems();

		addItem(_value);

		updateContent();
	}

	void HyperTextBox::removeAllItems()
	{
		while (mStackPanel->getChildCount() != 0)
			WidgetManager::getInstancePtr()->destroyWidget(mStackPanel->getChildAt(0));
	}

	size_t HyperTextBox::getItemsCount() const
	{
		return mStackPanel->getChildCount();
	}

	void HyperTextBox::removeItemAt(size_t _index)
	{
		MYGUI_ASSERT_RANGE(_index, mStackPanel->getChildCount(), "HyperTextBox::removeItemAt");
		WidgetManager::getInstancePtr()->destroyWidget(mStackPanel->getChildAt(_index));
	}

	void HyperTextBox::updateBreakHeight()
	{
		mBreakHeight = 0;

		IFont* font = FontManager::getInstancePtr()->getByName(getDefaultFont());
		if (font != nullptr)
			mBreakHeight = font->getDefaultHeight();
	}

} // namespace MyGUI