Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / views / examples / text_example.cc
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.
4
5 #include "ui/views/examples/text_example.h"
6
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"
18
19 using base::ASCIIToUTF16;
20
21 namespace views {
22 namespace examples {
23
24 namespace {
25
26 // Number of columns in the view layout.
27 const int kNumColumns = 10;
28
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.";
43
44 const char* kTextExamples[] = {
45     "Short",
46     "Medium",
47     "Long",
48     "Ampersands",
49     "Newlines",
50 };
51
52 const char* kElidingBehaviors[] = {
53     "Ellipsis",
54     "None",
55     "Fade Tail",
56     "Fade Head",
57 };
58
59 const char* kPrefixOptions[] = {
60     "Default",
61     "Show",
62     "Hide",
63 };
64
65 const char* kHorizontalAligments[] = {
66     "Default",
67     "Left",
68     "Center",
69     "Right",
70 };
71
72 // Toggles bit |flag| on |flags| based on state of |checkbox|.
73 void SetFlagFromCheckbox(Checkbox* checkbox, int* flags, int flag) {
74   if (checkbox->checked())
75     *flags |= flag;
76   else
77     *flags &= ~flag;
78 }
79
80 }  // namespace
81
82 // TextExample's content view, which is responsible for drawing a string with
83 // the specified style.
84 class TextExample::TextExampleView : public View {
85  public:
86   TextExampleView()
87     : text_(ASCIIToUTF16(kShortText)),
88       text_flags_(0),
89       halo_(false),
90       fade_(false),
91       fade_mode_(gfx::Canvas::TruncateFadeTail) {
92   }
93
94   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
95     View::OnPaint(canvas);
96     const gfx::Rect bounds = GetContentsBounds();
97
98     if (fade_) {
99       canvas->DrawFadeTruncatingStringRect(text_, fade_mode_, font_list_,
100                                            SK_ColorDKGRAY, bounds);
101     } else if (halo_) {
102       canvas->DrawStringRectWithHalo(text_, font_list_, SK_ColorDKGRAY,
103                                      SK_ColorWHITE, bounds, text_flags_);
104     } else {
105       canvas->DrawStringRectWithFlags(text_, font_list_, SK_ColorDKGRAY, bounds,
106                                       text_flags_);
107     }
108   }
109
110   int text_flags() const { return text_flags_; }
111   void set_text_flags(int text_flags) { text_flags_ = text_flags; }
112
113   const base::string16& text() const { return text_; }
114   void set_text(const base::string16& text) { text_ = text; }
115
116   bool halo() const { return halo_; }
117   void set_halo(bool halo) { halo_ = halo; }
118
119   bool fade() const { return fade_; }
120   void set_fade(bool fade) { fade_ = fade; }
121
122   gfx::Canvas::TruncateFadeMode fade_mode() const { return fade_mode_; }
123   void set_fade_mode(gfx::Canvas::TruncateFadeMode mode) { fade_mode_ = mode; }
124
125   int GetFontStyle() const {
126     return font_list_.GetFontStyle();
127   }
128   void SetFontStyle(int style) {
129     font_list_ = font_list_.DeriveWithStyle(style);
130   }
131
132  private:
133   // The font used for drawing the text.
134   gfx::FontList font_list_;
135
136   // The text to draw.
137   base::string16 text_;
138
139   // Text flags for passing to |DrawStringRect()|.
140   int text_flags_;
141
142   // If |true|, specifies to call |DrawStringWithHalo()| instead of
143   // |DrawStringRect()|.
144   bool halo_;
145
146   // If |true|, specifies to call |DrawFadeTruncatingString()| instead of
147   // |DrawStringRect()|.
148   bool fade_;
149
150   // If |fade_| is |true|, fade mode parameter to |DrawFadeTruncatingString()|.
151   gfx::Canvas::TruncateFadeMode fade_mode_;
152
153   DISALLOW_COPY_AND_ASSIGN(TextExampleView);
154 };
155
156 TextExample::TextExample() : ExampleBase("Text Styles") {
157 }
158
159 TextExample::~TextExample() {
160   // Remove all the views first as some reference models in
161   // |example_combobox_model_|.
162   container()->RemoveAllChildViews(true);
163 }
164
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);
169   return checkbox;
170 }
171
172 Combobox* TextExample::AddCombobox(GridLayout* layout,
173                                    const char* name,
174                                    const char** strings,
175                                    int count) {
176   layout->StartRow(0, 0);
177   layout->AddView(new Label(ASCIIToUTF16(name)));
178   ExampleComboboxModel* combobox_model = new ExampleComboboxModel(strings,
179                                                                   count);
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);
185   return combobox;
186 }
187
188 void TextExample::CreateExampleView(View* container) {
189   text_view_ = new TextExampleView;
190   text_view_->SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY));
191
192   GridLayout* layout = new GridLayout(container);
193   container->SetLayoutManager(layout);
194
195   layout->AddPaddingRow(0, 8);
196
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);
205
206   h_align_cb_ = AddCombobox(layout,
207                             "H-Align",
208                             kHorizontalAligments,
209                             arraysize(kHorizontalAligments));
210   eliding_cb_ = AddCombobox(layout,
211                             "Eliding",
212                             kElidingBehaviors,
213                             arraysize(kElidingBehaviors));
214   prefix_cb_ = AddCombobox(layout,
215                            "Prefix",
216                            kPrefixOptions,
217                            arraysize(kPrefixOptions));
218   text_cb_ = AddCombobox(layout,
219                          "Example Text",
220                          kTextExamples,
221                          arraysize(kTextExamples));
222
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");
230
231   layout->AddPaddingRow(0, 32);
232
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_);
240
241   layout->AddPaddingRow(0, 8);
242 }
243
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();
256 }
257
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()) {
265       case 0:
266         break;
267       case 1:
268         text_flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
269         break;
270       case 2:
271         text_flags |= gfx::Canvas::TEXT_ALIGN_CENTER;
272         break;
273       case 3:
274         text_flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
275         break;
276     }
277   } else if (combobox == text_cb_) {
278     switch (combobox->selected_index()) {
279       case 0:
280         text_view_->set_text(ASCIIToUTF16(kShortText));
281         break;
282       case 1:
283         text_view_->set_text(ASCIIToUTF16(kMediumText));
284         break;
285       case 2:
286         text_view_->set_text(ASCIIToUTF16(kLongText));
287         break;
288       case 3:
289         text_view_->set_text(ASCIIToUTF16(kAmpersandText));
290         break;
291       case 4:
292         text_view_->set_text(ASCIIToUTF16(kNewlineText));
293         break;
294     }
295   } else if (combobox == eliding_cb_) {
296     switch (combobox->selected_index()) {
297       case 0:
298         text_flags &= ~gfx::Canvas::NO_ELLIPSIS;
299         text_view_->set_fade(false);
300         break;
301       case 1:
302         text_flags |= gfx::Canvas::NO_ELLIPSIS;
303         text_view_->set_fade(false);
304         break;
305       case 2:
306         text_view_->set_fade_mode(gfx::Canvas::TruncateFadeTail);
307         text_view_->set_fade(true);
308         break;
309       case 3:
310         text_view_->set_fade_mode(gfx::Canvas::TruncateFadeHead);
311         text_view_->set_fade(true);
312         break;
313     }
314   } else if (combobox == prefix_cb_) {
315     text_flags &= ~(gfx::Canvas::SHOW_PREFIX | gfx::Canvas::HIDE_PREFIX);
316     switch (combobox->selected_index()) {
317       case 0:
318         break;
319       case 1:
320         text_flags |= gfx::Canvas::SHOW_PREFIX;
321         break;
322       case 2:
323         text_flags |= gfx::Canvas::HIDE_PREFIX;
324         break;
325     }
326   }
327   text_view_->set_text_flags(text_flags);
328   text_view_->SchedulePaint();
329 }
330
331 }  // namespace examples
332 }  // namespace views