1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/examples/text_example.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/base/resource/resource_bundle.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/font_list.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/button/checkbox.h"
13 #include "ui/views/controls/combobox/combobox.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/examples/example_combobox_model.h"
16 #include "ui/views/layout/grid_layout.h"
17 #include "ui/views/view.h"
19 using base::ASCIIToUTF16
;
26 // Number of columns in the view layout.
27 const int kNumColumns
= 10;
29 const char kShortText
[] = "Batman";
30 const char kMediumText
[] = "The quick brown fox jumps over the lazy dog.";
31 const char kLongText
[] =
32 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod "
33 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
34 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
35 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
36 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
37 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
38 "mollit anim id est laborum.";
39 const char kAmpersandText
[] =
40 "The quick && &brown fo&x jumps over the lazy dog.";
41 const char kNewlineText
[] =
42 "The quick \nbrown fox jumps\n\n over the lazy dog.";
44 const char* kTextExamples
[] = {
52 const char* kElidingBehaviors
[] = {
59 const char* kPrefixOptions
[] = {
65 const char* kHorizontalAligments
[] = {
72 // Toggles bit |flag| on |flags| based on state of |checkbox|.
73 void SetFlagFromCheckbox(Checkbox
* checkbox
, int* flags
, int flag
) {
74 if (checkbox
->checked())
82 // TextExample's content view, which is responsible for drawing a string with
83 // the specified style.
84 class TextExample::TextExampleView
: public View
{
87 : text_(ASCIIToUTF16(kShortText
)),
91 fade_mode_(gfx::Canvas::TruncateFadeTail
) {
94 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
95 View::OnPaint(canvas
);
96 const gfx::Rect bounds
= GetContentsBounds();
99 canvas
->DrawFadeTruncatingStringRect(text_
, fade_mode_
, font_list_
,
100 SK_ColorDKGRAY
, bounds
);
102 canvas
->DrawStringRectWithHalo(text_
, font_list_
, SK_ColorDKGRAY
,
103 SK_ColorWHITE
, bounds
, text_flags_
);
105 canvas
->DrawStringRectWithFlags(text_
, font_list_
, SK_ColorDKGRAY
, bounds
,
110 int text_flags() const { return text_flags_
; }
111 void set_text_flags(int text_flags
) { text_flags_
= text_flags
; }
113 const base::string16
& text() const { return text_
; }
114 void set_text(const base::string16
& text
) { text_
= text
; }
116 bool halo() const { return halo_
; }
117 void set_halo(bool halo
) { halo_
= halo
; }
119 bool fade() const { return fade_
; }
120 void set_fade(bool fade
) { fade_
= fade
; }
122 gfx::Canvas::TruncateFadeMode
fade_mode() const { return fade_mode_
; }
123 void set_fade_mode(gfx::Canvas::TruncateFadeMode mode
) { fade_mode_
= mode
; }
125 int GetFontStyle() const {
126 return font_list_
.GetFontStyle();
128 void SetFontStyle(int style
) {
129 font_list_
= font_list_
.DeriveWithStyle(style
);
133 // The font used for drawing the text.
134 gfx::FontList font_list_
;
137 base::string16 text_
;
139 // Text flags for passing to |DrawStringRect()|.
142 // If |true|, specifies to call |DrawStringWithHalo()| instead of
143 // |DrawStringRect()|.
146 // If |true|, specifies to call |DrawFadeTruncatingString()| instead of
147 // |DrawStringRect()|.
150 // If |fade_| is |true|, fade mode parameter to |DrawFadeTruncatingString()|.
151 gfx::Canvas::TruncateFadeMode fade_mode_
;
153 DISALLOW_COPY_AND_ASSIGN(TextExampleView
);
156 TextExample::TextExample() : ExampleBase("Text Styles") {
159 TextExample::~TextExample() {
160 // Remove all the views first as some reference models in
161 // |example_combobox_model_|.
162 container()->RemoveAllChildViews(true);
165 Checkbox
* TextExample::AddCheckbox(GridLayout
* layout
, const char* name
) {
166 Checkbox
* checkbox
= new Checkbox(ASCIIToUTF16(name
));
167 checkbox
->set_listener(this);
168 layout
->AddView(checkbox
);
172 Combobox
* TextExample::AddCombobox(GridLayout
* layout
,
174 const char** strings
,
176 layout
->StartRow(0, 0);
177 layout
->AddView(new Label(ASCIIToUTF16(name
)));
178 ExampleComboboxModel
* combobox_model
= new ExampleComboboxModel(strings
,
180 example_combobox_model_
.push_back(combobox_model
);
181 Combobox
* combobox
= new Combobox(combobox_model
);
182 combobox
->SetSelectedIndex(0);
183 combobox
->set_listener(this);
184 layout
->AddView(combobox
, kNumColumns
- 1, 1);
188 void TextExample::CreateExampleView(View
* container
) {
189 text_view_
= new TextExampleView
;
190 text_view_
->SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY
));
192 GridLayout
* layout
= new GridLayout(container
);
193 container
->SetLayoutManager(layout
);
195 layout
->AddPaddingRow(0, 8);
197 ColumnSet
* column_set
= layout
->AddColumnSet(0);
198 column_set
->AddPaddingColumn(0, 8);
199 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
,
200 0.1f
, GridLayout::USE_PREF
, 0, 0);
201 for (int i
= 0; i
< kNumColumns
- 1; i
++)
202 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
,
203 0.1f
, GridLayout::USE_PREF
, 0, 0);
204 column_set
->AddPaddingColumn(0, 8);
206 h_align_cb_
= AddCombobox(layout
,
208 kHorizontalAligments
,
209 arraysize(kHorizontalAligments
));
210 eliding_cb_
= AddCombobox(layout
,
213 arraysize(kElidingBehaviors
));
214 prefix_cb_
= AddCombobox(layout
,
217 arraysize(kPrefixOptions
));
218 text_cb_
= AddCombobox(layout
,
221 arraysize(kTextExamples
));
223 layout
->StartRow(0, 0);
224 multiline_checkbox_
= AddCheckbox(layout
, "Multiline");
225 break_checkbox_
= AddCheckbox(layout
, "Character Break");
226 halo_checkbox_
= AddCheckbox(layout
, "Text Halo");
227 bold_checkbox_
= AddCheckbox(layout
, "Bold");
228 italic_checkbox_
= AddCheckbox(layout
, "Italic");
229 underline_checkbox_
= AddCheckbox(layout
, "Underline");
231 layout
->AddPaddingRow(0, 32);
233 column_set
= layout
->AddColumnSet(1);
234 column_set
->AddPaddingColumn(0, 16);
235 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
,
236 1, GridLayout::USE_PREF
, 0, 0);
237 column_set
->AddPaddingColumn(0, 16);
238 layout
->StartRow(1, 1);
239 layout
->AddView(text_view_
);
241 layout
->AddPaddingRow(0, 8);
244 void TextExample::ButtonPressed(Button
* button
, const ui::Event
& event
) {
245 int flags
= text_view_
->text_flags();
246 int style
= text_view_
->GetFontStyle();
247 SetFlagFromCheckbox(multiline_checkbox_
, &flags
, gfx::Canvas::MULTI_LINE
);
248 SetFlagFromCheckbox(break_checkbox_
, &flags
, gfx::Canvas::CHARACTER_BREAK
);
249 SetFlagFromCheckbox(bold_checkbox_
, &style
, gfx::Font::BOLD
);
250 SetFlagFromCheckbox(italic_checkbox_
, &style
, gfx::Font::ITALIC
);
251 SetFlagFromCheckbox(underline_checkbox_
, &style
, gfx::Font::UNDERLINE
);
252 text_view_
->set_halo(halo_checkbox_
->checked());
253 text_view_
->set_text_flags(flags
);
254 text_view_
->SetFontStyle(style
);
255 text_view_
->SchedulePaint();
258 void TextExample::OnPerformAction(Combobox
* combobox
) {
259 int text_flags
= text_view_
->text_flags();
260 if (combobox
== h_align_cb_
) {
261 text_flags
&= ~(gfx::Canvas::TEXT_ALIGN_LEFT
|
262 gfx::Canvas::TEXT_ALIGN_CENTER
|
263 gfx::Canvas::TEXT_ALIGN_RIGHT
);
264 switch (combobox
->selected_index()) {
268 text_flags
|= gfx::Canvas::TEXT_ALIGN_LEFT
;
271 text_flags
|= gfx::Canvas::TEXT_ALIGN_CENTER
;
274 text_flags
|= gfx::Canvas::TEXT_ALIGN_RIGHT
;
277 } else if (combobox
== text_cb_
) {
278 switch (combobox
->selected_index()) {
280 text_view_
->set_text(ASCIIToUTF16(kShortText
));
283 text_view_
->set_text(ASCIIToUTF16(kMediumText
));
286 text_view_
->set_text(ASCIIToUTF16(kLongText
));
289 text_view_
->set_text(ASCIIToUTF16(kAmpersandText
));
292 text_view_
->set_text(ASCIIToUTF16(kNewlineText
));
295 } else if (combobox
== eliding_cb_
) {
296 switch (combobox
->selected_index()) {
298 text_flags
&= ~gfx::Canvas::NO_ELLIPSIS
;
299 text_view_
->set_fade(false);
302 text_flags
|= gfx::Canvas::NO_ELLIPSIS
;
303 text_view_
->set_fade(false);
306 text_view_
->set_fade_mode(gfx::Canvas::TruncateFadeTail
);
307 text_view_
->set_fade(true);
310 text_view_
->set_fade_mode(gfx::Canvas::TruncateFadeHead
);
311 text_view_
->set_fade(true);
314 } else if (combobox
== prefix_cb_
) {
315 text_flags
&= ~(gfx::Canvas::SHOW_PREFIX
| gfx::Canvas::HIDE_PREFIX
);
316 switch (combobox
->selected_index()) {
320 text_flags
|= gfx::Canvas::SHOW_PREFIX
;
323 text_flags
|= gfx::Canvas::HIDE_PREFIX
;
327 text_view_
->set_text_flags(text_flags
);
328 text_view_
->SchedulePaint();
331 } // namespace examples