OLD | NEW |
| (Empty) |
1 // Copyright 2006-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 // | |
17 | |
18 #include "omaha/ui/uilib/node_state.h" | |
19 | |
20 // Add a list item tag | |
21 NodeState::Tags NodeState::tags_[] = { | |
22 // name_to_match length_name_to_match action no_parameters; | |
23 { _T("<b>"), static_cast<int>(_tcslen(_T("<b>"))), BOLD_ON,
true }, // NOLINT | |
24 { _T("</b>"), static_cast<int>(_tcslen(_T("</b>"))), BOLD_OFF,
true }, // NOLINT | |
25 { _T("<i>"), static_cast<int>(_tcslen(_T("<i>"))), ITALIC_ON,
true }, // NOLINT | |
26 { _T("</i>"), static_cast<int>(_tcslen(_T("</i>"))), ITALIC_OFF,
true }, // NOLINT | |
27 { _T("<u>"), static_cast<int>(_tcslen(_T("<u>"))), UNDERLINE_ON,
true }, // NOLINT | |
28 { _T("</u>"), static_cast<int>(_tcslen(_T("</u>"))), UNDERLINE_OFF,
true }, // NOLINT | |
29 { _T("<color="), static_cast<int>(_tcslen(_T("<color="))), TEXTCOLOR_ON,
false }, // NOLINT | |
30 { _T("</color>"), static_cast<int>(_tcslen(_T("</color>"))), TEXTCOLOR_OFF,
true }, // NOLINT | |
31 { _T("<size="), static_cast<int>(_tcslen(_T("<size="))), TEXTSIZE_ON,
false }, // NOLINT | |
32 { _T("</size>"), static_cast<int>(_tcslen(_T("</size>"))), TEXTSIZE_OFF,
true }, // NOLINT | |
33 { _T("<a="), static_cast<int>(_tcslen(_T("<a="))), URL_ON,
false }, // NOLINT | |
34 { _T("</a>"), static_cast<int>(_tcslen(_T("</a>"))), URL_OFF,
true }, // NOLINT | |
35 }; | |
36 | |
37 NodeState::NodeState(HWND window) | |
38 : default_font_(NULL), | |
39 font_(NULL), | |
40 bold_(false), | |
41 italic_(false), | |
42 underline_(false), | |
43 text_color_(0), | |
44 text_size_(8), | |
45 owner_window_(window) { | |
46 } | |
47 | |
48 NodeState::~NodeState() { | |
49 } | |
50 | |
51 | |
52 void NodeState::SetStdFont(HFONT font) { | |
53 default_font_ = font; | |
54 } | |
55 | |
56 | |
57 HFONT NodeState::GetFont() const { | |
58 if (IsDefaultFont()) | |
59 return default_font_; | |
60 | |
61 if (font_) | |
62 return font_; | |
63 | |
64 if (default_font_) { | |
65 HDC dc = GetDC(owner_window_); | |
66 | |
67 LOGFONT log_font; | |
68 GetObject(default_font_, sizeof(LOGFONT), &log_font); | |
69 log_font.lfWeight = bold_ ? FW_BOLD : FW_NORMAL; | |
70 log_font.lfItalic = italic_; | |
71 log_font.lfUnderline = underline_; | |
72 log_font.lfHeight = | |
73 -MulDiv(text_size_, GetDeviceCaps(dc, LOGPIXELSY), 72); | |
74 font_ = CreateFontIndirect(&log_font); | |
75 } | |
76 | |
77 return font_; | |
78 } | |
79 | |
80 | |
81 bool NodeState::IsDefaultFont() const { | |
82 if (bold_ || italic_ || underline_) | |
83 return false; | |
84 | |
85 if (text_size_ != 8) | |
86 return false; | |
87 | |
88 return true; | |
89 } | |
90 | |
91 | |
92 int NodeState::ConsumeTag(const TCHAR* string) { | |
93 int size = sizeof(tags_) / sizeof(tags_[0]); | |
94 for (int i = 0; i < size; i++) { | |
95 if (_tcsnicmp(string, tags_[i].name_to_match, | |
96 tags_[i].length_name_to_match) == 0) { | |
97 if (tags_[i].no_parameters) { | |
98 ApplyAction(tags_[i].action, NULL); | |
99 return tags_[i].length_name_to_match; | |
100 } else { | |
101 return tags_[i].length_name_to_match + | |
102 ApplyAction(tags_[i].action, string + | |
103 tags_[i].length_name_to_match) + 1; | |
104 } | |
105 } | |
106 } | |
107 | |
108 return 0; | |
109 } | |
110 | |
111 | |
112 int NodeState::ApplyAction(Actions action, const TCHAR* string/*=NULL*/) { | |
113 int read = 0; | |
114 switch (action) { | |
115 case BOLD_ON : | |
116 bold_ = true; | |
117 break; | |
118 | |
119 case BOLD_OFF : | |
120 bold_ = false; | |
121 break; | |
122 | |
123 case ITALIC_ON : | |
124 italic_ = true; | |
125 break; | |
126 | |
127 case ITALIC_OFF : | |
128 italic_ = false; | |
129 break; | |
130 | |
131 case UNDERLINE_ON : | |
132 underline_ = true; | |
133 break; | |
134 | |
135 case UNDERLINE_OFF : | |
136 underline_ = false; | |
137 break; | |
138 | |
139 case TEXTCOLOR_ON : | |
140 ATLASSERT(string); | |
141 if (string) { | |
142 int nParam = 0; | |
143 read = ReadColorRef(string, &nParam); | |
144 text_color_ = nParam; | |
145 } | |
146 break; | |
147 | |
148 case TEXTCOLOR_OFF : | |
149 text_color_ = 0; | |
150 break; | |
151 | |
152 case TEXTSIZE_ON : | |
153 ATLASSERT(string); | |
154 if (string) | |
155 read = ReadNumParameter(string, &text_size_); | |
156 break; | |
157 | |
158 case TEXTSIZE_OFF : | |
159 text_size_ = 8; | |
160 break; | |
161 | |
162 case URL_ON : | |
163 underline_ = true; | |
164 text_color_ = RGB(0, 0, 0xff); | |
165 ATLASSERT(string); | |
166 if (string) | |
167 read = ReadString(string, &url_); | |
168 break; | |
169 | |
170 case URL_OFF : | |
171 underline_ = false; | |
172 text_color_ = 0; | |
173 url_ = _T(""); | |
174 break; | |
175 | |
176 case UNKNOWN: | |
177 // fall thru | |
178 | |
179 default: | |
180 ATLASSERT(false); | |
181 } | |
182 return read; | |
183 } | |
184 | |
185 int NodeState::ReadNumParameter(const TCHAR* string, int* param) { | |
186 if (!param) | |
187 return 0; | |
188 | |
189 *param = 0; | |
190 const TCHAR* current_pos = string; | |
191 while (current_pos && _istdigit(*current_pos)) { | |
192 *param *= 10; | |
193 *param += *current_pos - _T('0'); | |
194 current_pos++; | |
195 } | |
196 return static_cast<int>(current_pos - string); | |
197 } | |
198 | |
199 int NodeState::ReadHexParameter(const TCHAR* string, int* param) { | |
200 if (!param) | |
201 return 0; | |
202 | |
203 *param = 0; | |
204 const TCHAR* current_pos = string; | |
205 while (current_pos && _istxdigit(*current_pos)) { | |
206 *param = ((*param) << 4); | |
207 if (_istdigit(*current_pos)) | |
208 *param += *current_pos - _T('0'); | |
209 else | |
210 *param += (*current_pos | 0x20) - _T('a') + 10; | |
211 current_pos++; | |
212 } | |
213 return static_cast<int>(current_pos - string); | |
214 } | |
215 | |
216 int NodeState::ReadColorRef(const TCHAR* string, int* param) { | |
217 if (!param) | |
218 return 0; | |
219 | |
220 int read = ReadHexParameter(string, param); | |
221 *param = RGB((*param) >> 16, ((*param) >> 8) & 0xff, (*param) & 0xff); | |
222 return read; | |
223 } | |
224 | |
225 int NodeState::ReadString(const TCHAR* string, CString* string_out) { | |
226 if (!string_out) | |
227 return 0; | |
228 | |
229 int length = 0; | |
230 int position = _tcscspn(string, _T(" >")); | |
231 if (position >= 0) { | |
232 *string_out = CString(string, position); | |
233 length = position; | |
234 } | |
235 | |
236 return length; | |
237 } | |
OLD | NEW |