OLD | NEW |
| (Empty) |
1 | |
2 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
3 // Use of this source code is governed by a BSD-style license that can be | |
4 // found in the LICENSE file. | |
5 | |
6 #include "views/examples/text_example.h" | |
7 | |
8 #include "base/utf_string_conversions.h" | |
9 #include "ui/base/resource/resource_bundle.h" | |
10 #include "ui/gfx/canvas.h" | |
11 #include "ui/gfx/canvas_skia.h" | |
12 #include "views/controls/button/checkbox.h" | |
13 #include "views/controls/label.h" | |
14 #include "views/examples/example_combobox_model.h" | |
15 #include "views/layout/grid_layout.h" | |
16 #include "views/view.h" | |
17 | |
18 namespace { | |
19 | |
20 const char kShortText[] = "Batman"; | |
21 const char kMediumText[] = "The quick brown fox jumps over the lazy dog."; | |
22 const char kLongText[] = | |
23 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " | |
24 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " | |
25 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " | |
26 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " | |
27 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " | |
28 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " | |
29 "mollit anim id est laborum."; | |
30 const char kAmpersandText[] = | |
31 "The quick && &brown fo&x jumps over the lazy dog."; | |
32 | |
33 const char* kTextExamples[] = { | |
34 "Short", | |
35 "Medium", | |
36 "Long", | |
37 "Ampersands", | |
38 }; | |
39 | |
40 const char* kElidingBehaviors[] = { | |
41 "Ellipsis", | |
42 "None", | |
43 #if defined(OS_WIN) | |
44 "Fade Tail", | |
45 "Fade Head", | |
46 "Fade Head and Tail", | |
47 #endif | |
48 }; | |
49 | |
50 const char* kPrefixOptions[] = { | |
51 "Default", | |
52 "Show", | |
53 "Hide", | |
54 }; | |
55 | |
56 const char* kHorizontalAligments[] = { | |
57 "Default", | |
58 "Left", | |
59 "Center", | |
60 "Right", | |
61 }; | |
62 | |
63 const char* kVerticalAlignments[] = { | |
64 "Default", | |
65 "Top", | |
66 "Middle", | |
67 "Bottom", | |
68 }; | |
69 | |
70 } // namespace | |
71 | |
72 namespace examples { | |
73 | |
74 // TextExample's content view, which is responsible for drawing a string with | |
75 // the specified style. | |
76 class TextExample::TextExampleView : public views::View { | |
77 public: | |
78 TextExampleView() | |
79 : font_(ResourceBundle::GetSharedInstance().GetFont( | |
80 ResourceBundle::BaseFont)), | |
81 text_(ASCIIToUTF16(kShortText)), | |
82 text_flags_(0), | |
83 fade_(false), | |
84 fade_mode_(gfx::CanvasSkia::TruncateFadeTail) { | |
85 } | |
86 | |
87 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
88 #if defined(OS_WIN) | |
89 if (fade_) { | |
90 gfx::Rect rect(0, 0, width(), height()); | |
91 size_t characters_to_truncate_from_head = | |
92 gfx::CanvasSkia::TruncateFadeHeadAndTail ? 10 : 0; | |
93 canvas->AsCanvasSkia()->DrawFadeTruncatingString(text_, fade_mode_, | |
94 characters_to_truncate_from_head, font_, SK_ColorDKGRAY, rect); | |
95 return; | |
96 } | |
97 #endif | |
98 | |
99 canvas->DrawStringInt(text_, font_, SK_ColorDKGRAY, | |
100 0, 0, width(), height(), text_flags_); | |
101 } | |
102 | |
103 int text_flags() const { return text_flags_; } | |
104 void set_text_flags(int text_flags) { text_flags_ = text_flags; } | |
105 | |
106 const string16& text() const { return text_; } | |
107 void set_text(const string16& text) { text_ = text; } | |
108 | |
109 bool fade() const { return fade_; } | |
110 void set_fade(bool fade) { fade_ = fade; } | |
111 | |
112 gfx::CanvasSkia::TruncateFadeMode fade_mode() const { return fade_mode_; } | |
113 void set_fade_mode(gfx::CanvasSkia::TruncateFadeMode fade_mode) { | |
114 fade_mode_ = fade_mode; | |
115 } | |
116 | |
117 private: | |
118 // The font used for drawing the text. | |
119 gfx::Font font_; | |
120 | |
121 // The text to draw. | |
122 string16 text_; | |
123 | |
124 // Text flags for passing to |DrawStringInt()|. | |
125 int text_flags_; | |
126 | |
127 // If |true|, specifies to call |DrawFadeTruncatingString()| instead of | |
128 // |DrawStringInt()|. | |
129 bool fade_; | |
130 | |
131 // If |fade_| is |true|, fade mode parameter to |DrawFadeTruncatingString()|. | |
132 gfx::CanvasSkia::TruncateFadeMode fade_mode_; | |
133 | |
134 DISALLOW_COPY_AND_ASSIGN(TextExampleView); | |
135 }; | |
136 | |
137 TextExample::TextExample(ExamplesMain* main) | |
138 : ExampleBase(main, "Text Styles") { | |
139 } | |
140 | |
141 TextExample::~TextExample() { | |
142 } | |
143 | |
144 views::Combobox* TextExample::AddCombobox(views::GridLayout* layout, | |
145 const char* name, | |
146 const char** strings, | |
147 int count) { | |
148 layout->StartRow(0, 0); | |
149 layout->AddView(new views::Label(ASCIIToUTF16(name))); | |
150 views::Combobox* combo_box = | |
151 new views::Combobox(new ExampleComboboxModel(strings, count)); | |
152 combo_box->SetSelectedItem(0); | |
153 combo_box->set_listener(this); | |
154 layout->AddView(combo_box); | |
155 return combo_box; | |
156 } | |
157 | |
158 void TextExample::CreateExampleView(views::View* container) { | |
159 text_view_ = new TextExampleView; | |
160 | |
161 views::GridLayout* layout = new views::GridLayout(container); | |
162 container->SetLayoutManager(layout); | |
163 | |
164 layout->AddPaddingRow(0, 8); | |
165 | |
166 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
167 column_set->AddPaddingColumn(0, 8); | |
168 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, | |
169 0.1f, views::GridLayout::USE_PREF, 0, 0); | |
170 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
171 0.9f, views::GridLayout::USE_PREF, 0, 0); | |
172 column_set->AddPaddingColumn(0, 8); | |
173 | |
174 h_align_cb_ = AddCombobox(layout, | |
175 "H-Align", | |
176 kHorizontalAligments, | |
177 arraysize(kHorizontalAligments)); | |
178 v_align_cb_ = AddCombobox(layout, | |
179 "V-Align", | |
180 kVerticalAlignments, | |
181 arraysize(kVerticalAlignments)); | |
182 eliding_cb_ = AddCombobox(layout, | |
183 "Eliding", | |
184 kElidingBehaviors, | |
185 arraysize(kElidingBehaviors)); | |
186 prefix_cb_ = AddCombobox(layout, | |
187 "Prefix", | |
188 kPrefixOptions, | |
189 arraysize(kPrefixOptions)); | |
190 text_cb_ = AddCombobox(layout, | |
191 "Example Text", | |
192 kTextExamples, | |
193 arraysize(kTextExamples)); | |
194 | |
195 layout->StartRow(0, 0); | |
196 multiline_checkbox_ = new views::Checkbox(ASCIIToUTF16("Multiline")); | |
197 multiline_checkbox_->set_listener(this); | |
198 layout->AddView(multiline_checkbox_); | |
199 break_checkbox_ = new views::Checkbox(ASCIIToUTF16("Character Break")); | |
200 break_checkbox_->set_listener(this); | |
201 layout->AddView(break_checkbox_); | |
202 | |
203 layout->AddPaddingRow(0, 32); | |
204 | |
205 column_set = layout->AddColumnSet(1); | |
206 column_set->AddPaddingColumn(0, 16); | |
207 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
208 1, views::GridLayout::USE_PREF, 0, 0); | |
209 column_set->AddPaddingColumn(0, 16); | |
210 layout->StartRow(1, 1); | |
211 layout->AddView(text_view_); | |
212 | |
213 layout->AddPaddingRow(0, 8); | |
214 } | |
215 | |
216 void TextExample::ButtonPressed(views::Button* button, | |
217 const views::Event& event) { | |
218 int text_flags = text_view_->text_flags(); | |
219 if (button == multiline_checkbox_) { | |
220 if (multiline_checkbox_->checked()) | |
221 text_flags |= gfx::Canvas::MULTI_LINE; | |
222 else | |
223 text_flags &= ~gfx::Canvas::MULTI_LINE; | |
224 } else if (button == break_checkbox_) { | |
225 if (break_checkbox_->checked()) | |
226 text_flags |= gfx::Canvas::CHARACTER_BREAK; | |
227 else | |
228 text_flags &= ~gfx::Canvas::CHARACTER_BREAK; | |
229 } | |
230 text_view_->set_text_flags(text_flags); | |
231 text_view_->SchedulePaint(); | |
232 } | |
233 | |
234 void TextExample::ItemChanged(views::Combobox* combo_box, | |
235 int prev_index, | |
236 int new_index) { | |
237 int text_flags = text_view_->text_flags(); | |
238 if (combo_box == h_align_cb_) { | |
239 text_flags &= ~(gfx::Canvas::TEXT_ALIGN_LEFT | | |
240 gfx::Canvas::TEXT_ALIGN_CENTER | | |
241 gfx::Canvas::TEXT_ALIGN_RIGHT); | |
242 switch (new_index) { | |
243 case 0: | |
244 break; | |
245 case 1: | |
246 text_flags |= gfx::Canvas::TEXT_ALIGN_LEFT; | |
247 break; | |
248 case 2: | |
249 text_flags |= gfx::Canvas::TEXT_ALIGN_CENTER; | |
250 break; | |
251 case 3: | |
252 text_flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; | |
253 break; | |
254 } | |
255 } else if (combo_box == v_align_cb_) { | |
256 text_flags &= ~(gfx::Canvas::TEXT_VALIGN_TOP | | |
257 gfx::Canvas::TEXT_VALIGN_MIDDLE | | |
258 gfx::Canvas::TEXT_VALIGN_BOTTOM); | |
259 switch (new_index) { | |
260 case 0: | |
261 break; | |
262 case 1: | |
263 text_flags |= gfx::Canvas::TEXT_VALIGN_TOP; | |
264 break; | |
265 case 2: | |
266 text_flags |= gfx::Canvas::TEXT_VALIGN_MIDDLE; | |
267 break; | |
268 case 3: | |
269 text_flags |= gfx::Canvas::TEXT_VALIGN_BOTTOM; | |
270 break; | |
271 } | |
272 } else if (combo_box == text_cb_) { | |
273 switch (new_index) { | |
274 case 0: | |
275 text_view_->set_text(ASCIIToUTF16(kShortText)); | |
276 break; | |
277 case 1: | |
278 text_view_->set_text(ASCIIToUTF16(kMediumText)); | |
279 break; | |
280 case 2: | |
281 text_view_->set_text(ASCIIToUTF16(kLongText)); | |
282 break; | |
283 case 3: | |
284 text_view_->set_text(ASCIIToUTF16(kAmpersandText)); | |
285 break; | |
286 } | |
287 } else if (combo_box == eliding_cb_) { | |
288 switch (new_index) { | |
289 case 0: | |
290 text_flags &= ~gfx::Canvas::NO_ELLIPSIS; | |
291 text_view_->set_fade(false); | |
292 break; | |
293 case 1: | |
294 text_flags |= gfx::Canvas::NO_ELLIPSIS; | |
295 text_view_->set_fade(false); | |
296 break; | |
297 #if defined(OS_WIN) | |
298 case 2: | |
299 text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeTail); | |
300 text_view_->set_fade(true); | |
301 break; | |
302 case 3: | |
303 text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeHead); | |
304 text_view_->set_fade(true); | |
305 break; | |
306 case 4: | |
307 text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeHeadAndTail); | |
308 text_view_->set_fade(true); | |
309 break; | |
310 #endif | |
311 } | |
312 } else if (combo_box == prefix_cb_) { | |
313 text_flags &= ~(gfx::Canvas::SHOW_PREFIX | gfx::Canvas::HIDE_PREFIX); | |
314 switch (new_index) { | |
315 case 0: | |
316 break; | |
317 case 1: | |
318 text_flags |= gfx::Canvas::SHOW_PREFIX; | |
319 break; | |
320 case 2: | |
321 text_flags |= gfx::Canvas::HIDE_PREFIX; | |
322 break; | |
323 } | |
324 } | |
325 text_view_->set_text_flags(text_flags); | |
326 text_view_->SchedulePaint(); | |
327 } | |
328 | |
329 } // namespace examples | |
OLD | NEW |