soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SNamedValue.cpp
1//////////////////////////////////////////////////////////////////////////
2// File Name: SNamedValue.cpp
3//////////////////////////////////////////////////////////////////////////
4
5#include "souistd.h"
6#include "res.mgr/SNamedValue.h"
7#include "core/SWndStyle.h"
8
9SNSBEGIN
10
11//////////////////////////////////////////////////////////////////////////
12
13const wchar_t KPrefixString[] = L"@string/";
14const wchar_t KPrefixColor[] = L"@color/";
15const wchar_t KPrefixDimension[] = L"@dim/";
16const wchar_t KPrefixFont[] = L"@font/";
17
19{
20 return 0;
21}
22
23bool SIntParser::ParseValue(const SStringW &strValue, int &value)
24{
25 value = _wtoi(strValue);
26 return true;
27}
28
29//////////////////////////////////////////////////////////////////////////
34
35bool SStringParser::ParseValue(const SStringW &strValue, SStringW &value)
36{
37 value = strValue;
38 return true;
39}
40
41BOOL SNamedString::Get(const SStringW &strValue, SStringW &ret) const
42{
43 if (strValue.IsEmpty())
44 return FALSE;
45 if (strValue.Left(ARRAYSIZE(KPrefixString) - 1) == KPrefixString)
46 {
47 return FindValue(strValue.Mid(ARRAYSIZE(KPrefixString) - 1), ret);
48 }
49 else
50 {
51 ret = strValue;
52 return TRUE;
53 }
54}
55
56BOOL SNamedFont::Get(const SStringW &strValue, SStringW &ret) const
57{
58 if (strValue.IsEmpty())
59 return FALSE;
60 if (strValue.Left(ARRAYSIZE(KPrefixFont) - 1) == KPrefixFont)
61 {
62 return FindValue(strValue.Mid(ARRAYSIZE(KPrefixFont) - 1), ret);
63 }
64 else
65 {
66 ret = strValue;
67 return TRUE;
68 }
69}
70//////////////////////////////////////////////////////////////////////////
71
73{
74 return CR_INVALID;
75}
76
77bool SColorParser::ParseValue(const SStringW &strValue, COLORREF &value)
78{
79 int r = 255, g = 255, b = 255, a = 255;
80 int nSeg = 0;
81 SStringW strValueL = strValue;
82 strValueL.MakeLower();
83 if (strValueL.Left(1) == L"#")
84 {
85 nSeg = swscanf(strValueL, L"#%02x%02x%02x%02x", &r, &g, &b, &a);
86 }
87 else if (strValueL.Left(5).CompareNoCase(L"rgba2") == 0)
88 {
89 float percent = 100.0f;
90 nSeg = swscanf(strValueL, L"rgba2(%d,%d,%d,%f%%)", &r, &g, &b, &percent);
91 if (nSeg == 4)
92 {
93 a = (int)(percent * 255 / 100);
94 }
95 }
96 else if (strValueL.Left(5).CompareNoCase(L"rgba3") == 0)
97 {
98 float ratio = 1.0f;
99 nSeg = swscanf(strValueL, L"rgba3(%d,%d,%d,%f)", &r, &g, &b, &ratio);
100 if (nSeg == 4)
101 {
102 a = (int)(ratio * 255);
103 }
104 }
105 else if (strValueL.Left(4).CompareNoCase(L"rgba") == 0)
106 {
107 nSeg = swscanf(strValueL, L"rgba(%d,%d,%d,%d)", &r, &g, &b, &a);
108 }
109 else if (strValueL.Left(3).CompareNoCase(L"rgb") == 0)
110 {
111 nSeg = swscanf(strValueL, L"rgb(%d,%d,%d)", &r, &g, &b);
112 }
113 if (nSeg != 3 && nSeg != 4)
114 {
115 return false;
116 }
117 else
118 {
119 value = RGBA(r, g, b, a);
120 return true;
121 }
122}
123
124BOOL SNamedColor::Get(const SStringW &strValue, COLORREF &cr) const
125{
126 if (strValue.Left(ARRAYSIZE(KPrefixColor) - 1) == KPrefixColor)
127 {
128 return FindValue(strValue.Mid(ARRAYSIZE(KPrefixColor) - 1), cr);
129 }
130 else
131 {
132 return SColorParser::ParseValue(strValue, cr);
133 }
134}
135
136////////////////////////////////////////////////////////////////////////////////////
138{
139 value.parseString(strValue);
140 return true;
141}
142
147
148BOOL SNamedDimension::Get(const SStringW &strValue, SLayoutSize &ret) const
149{
150 if (strValue.Left(ARRAYSIZE(KPrefixDimension) - 1) == KPrefixDimension)
151 {
152 return FindValue(strValue.Mid(ARRAYSIZE(KPrefixDimension) - 1), ret);
153 }
154 else
155 {
157 SDimensionParser::ParseValue(strValue, ret);
158 return TRUE;
159 }
160}
161
162/////////////////////////////////////////////////////////////////////////////////
163void SNamedID::Init2(const NAMEDVALUE *pValue, int nCount, BOOL bSorted)
164{
165 m_lstNamedValue.RemoveAll();
166 m_lstNamedValue.SetCount(nCount);
167 for (int i = 0; i < nCount; i++)
168 {
169 m_lstNamedValue.SetAt(i, pValue[i]);
170 }
171 if (!bSorted)
172 { //自动排序
173 qsort(m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(), sizeof(NAMEDVALUE), Compare);
174 }
175}
176
177void SNamedID::Init3(const LPCWSTR *pNames, const int *nIDs, int nCount, BOOL bSorted)
178{
179 m_lstNamedValue.RemoveAll();
180 m_lstNamedValue.SetCount(nCount);
181 for (int i = 0; i < nCount; i++)
182 {
183 NAMEDVALUE value;
184 wcscpy_s(value.strName, MAX_NAME, pNames[i]);
185 value.value = nIDs[i];
186 m_lstNamedValue.SetAt(i, value);
187 }
188 if (!bSorted)
189 { //自动排序
190 qsort(m_lstNamedValue.GetData(), m_lstNamedValue.GetCount(), sizeof(NAMEDVALUE), Compare);
191 }
192}
193
194SNSEND
SOUI窗口风格管理
static COLORREF GetNullValue()
Retrieves the null value for colors.
static bool ParseValue(const SStringW &strValue, COLORREF &value)
Parses a string to a color value.
static SLayoutSize GetNullValue()
Retrieves the null value for dimensions.
static bool ParseValue(const SStringW &strValue, SLayoutSize &value)
Parses a string to a dimension value.
static int GetNullValue()
Retrieves the null value for integers.
static bool ParseValue(const SStringW &strValue, int &value)
Parses a string to an integer value.
布局大小类
Definition SLayoutSize.h:10
void parseString(const SStringW &strSize)
从字符串解析大小
BOOL Get(const SStringW &strValue, COLORREF &cr) const
Retrieves a color value by name, automatically converting named colors.
BOOL Get(const SStringW &strValue, SLayoutSize &ret) const
Retrieves a dimension value by name, automatically converting named dimensions.
BOOL Get(const SStringW &strValue, SStringW &ret) const
Retrieves a font value by name, automatically converting named fonts.
void Init2(const NAMEDVALUE *pValue, int nCount, BOOL bSorted)
Initializes named IDs from an array.
void Init3(const LPCWSTR *pNames, const int *nIDs, int nCount, BOOL bSorted)
Initializes named IDs from arrays of names and IDs.
BOOL Get(const SStringW &strValue, SStringW &ret) const
Retrieves a string value by name, automatically converting named strings.
BOOL FindValue(const SStringW &strName, SStringW &value) const
Definition SNamedValue.h:90
static int Compare(const void *p1, const void *p2)
static SStringW GetNullValue()
Retrieves the null value for strings.
static bool ParseValue(const SStringW &strValue, SStringW &value)
Parses a string to a string value.
A class representing an ASCII string.
Definition sstringw.h:96
int CompareNoCase(const wchar_t *psz) SCONST
Compares the string with another string, ignoring case.
Definition sstringw.cpp:929
BOOL IsEmpty() SCONST
Checks if the string is empty.
SStringW Mid(int nFirst) const
Extracts a substring from the string.
Definition sstringw.cpp:924
SStringW & MakeLower()
Converts the string to lowercase.
Definition sstringw.cpp:869
SStringW Left(int nCount) const
Extracts the leftmost part of the string.
Definition sstringw.cpp:879