-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparagraphcode.hpp
More file actions
124 lines (105 loc) · 2.83 KB
/
paragraphcode.hpp
File metadata and controls
124 lines (105 loc) · 2.83 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
//
inline winrt::Microsoft::UI::Xaml::Documents::Paragraph PlainParagraph(const wchar_t* txt)
{
using namespace winrt;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Documents;
using namespace Microsoft::UI::Xaml::Media;
Paragraph paragraph;
paragraph.Margin(Microsoft::UI::Xaml::ThicknessHelper::FromLengths(0, 0, 0, 0));
Run run1;
run1.Text(txt);
paragraph.Inlines().Append(run1);
return paragraph;
}
inline winrt::Microsoft::UI::Xaml::Documents::Paragraph ColoredParagraph(const wchar_t* txt,winrt::Windows::UI::Color color = winrt::Windows::UI::Colors::Red())
{
using namespace winrt;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Documents;
using namespace Microsoft::UI::Xaml::Media;
Paragraph paragraph;
paragraph.Margin(Microsoft::UI::Xaml::ThicknessHelper::FromLengths(0, 10, 0, 0));
Run run1;
run1.Text(txt);
run1.Foreground(SolidColorBrush(color));
paragraph.Inlines().Append(run1);
return paragraph;
}
inline std::vector<winrt::Microsoft::UI::Xaml::Documents::Paragraph> PreParagraph(const wchar_t* txt)
{
using namespace winrt;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Documents;
using namespace Microsoft::UI::Xaml::Media;
std::vector<winrt::Microsoft::UI::Xaml::Documents::Paragraph> pars;
// Split text into steps with *** separator to make that paragraph red
struct STEP
{
std::wstring txt;
bool R = 0;
};
std::vector<STEP> steps;
const wchar_t* start = txt;
for (;;)
{
auto str = wcsstr(start, L"***");
if (!str)
{
if (wcslen(start) > 0)
{
STEP stp;
stp.txt = start;
stp.R = false;
steps.push_back(stp);
}
break;
}
if (str > start)
{
STEP stp;
stp.txt = std::wstring(start, str - start);
stp.R = false;
steps.push_back(stp);
}
start = str + 3; // Move past the ***
// find another ***
auto end = wcsstr(start, L"***");
if (!end)
{
if (wcslen(start) > 0)
{
STEP stp;
stp.txt = std::wstring(start);
stp.R = true;
steps.push_back(stp);
}
break;
}
if (end > start)
{
STEP stp;
stp.txt = std::wstring(start, end - start);
stp.R = true;
steps.push_back(stp);
}
start = end + 3; // Move past the ***
}
for (auto& st : steps)
{
Paragraph prePara;
prePara.Margin(Microsoft::UI::Xaml::ThicknessHelper::FromLengths(0, 10, 0, 0));
Run preRun;
preRun.Foreground(SolidColorBrush(Windows::UI::Colors::Gray()));
if (st.R)
preRun.Foreground(SolidColorBrush(Windows::UI::Colors::Red()));
preRun.Text(st.txt);
preRun.FontFamily(FontFamily(L"Consolas"));
preRun.FontSize(14);
prePara.LineStackingStrategy(winrt::Microsoft::UI::Xaml::LineStackingStrategy::BlockLineHeight);
prePara.LineHeight(18);
prePara.Inlines().Append(preRun);
pars.push_back(prePara);
}
return pars;
}