summaryrefslogtreecommitdiffstats
path: root/views/examples/text_example.cc
blob: cf484e85c194d1c85139d52d57bd3b0b33095607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/examples/text_example.h"

#include "base/utf_string_conversions.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/canvas_skia.h"
#include "views/controls/label.h"
#include "views/controls/button/checkbox.h"
#include "views/examples/example_combobox_model.h"
#include "views/view.h"
#include "views/layout/grid_layout.h"

namespace {

const char kShortText[] = "Batman";
const char kMediumText[] = "The quick brown fox jumps over the lazy dog.";
const char kLongText[] =
    "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod "
    "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
    "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
    "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
    "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
    "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
    "mollit anim id est laborum.";
const char kAmpersandText[] =
    "The quick && &brown fo&x jumps over the lazy dog.";

const char* kTextExamples[] = {
    "Short",
    "Medium",
    "Long",
    "Ampersands",
};

const char* kElidingBehaviors[] = {
    "Ellipsis",
    "None",
#if defined(OS_WIN)
    "Fade Tail",
    "Fade Head",
    "Fade Head and Tail",
#endif
};

const char* kPrefixOptions[] = {
    "Default",
    "Show",
    "Hide",
};

const char* kHorizontalAligments[] = {
    "Default",
    "Left",
    "Center",
    "Right",
};

const char* kVerticalAlignments[] = {
    "Default",
    "Top",
    "Middle",
    "Bottom",
};

}  // namespace

namespace examples {

// TextExample's content view, which is responsible for drawing a string with
// the specified style.
class TextExample::TextExampleView : public views::View {
 public:
  TextExampleView()
    : font_(ResourceBundle::GetSharedInstance().GetFont(
          ResourceBundle::BaseFont)),
      text_(ASCIIToUTF16(kShortText)),
      text_flags_(0),
      fade_(false),
      fade_mode_(gfx::CanvasSkia::TruncateFadeTail) {
  }

  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
#if defined(OS_WIN)
    if (fade_) {
      gfx::Rect rect(0, 0, width(), height());
      size_t characters_to_truncate_from_head =
          gfx::CanvasSkia::TruncateFadeHeadAndTail ? 10 : 0;
      canvas->AsCanvasSkia()->DrawFadeTruncatingString(text_, fade_mode_,
          characters_to_truncate_from_head, font_, SK_ColorDKGRAY, rect);
      return;
    }
#endif

    canvas->DrawStringInt(text_, font_, SK_ColorDKGRAY,
                          0, 0, width(), height(), text_flags_);
  }

  int text_flags() const { return text_flags_; }
  void set_text_flags(int text_flags) { text_flags_ = text_flags; }

  const string16& text() const { return text_; }
  void set_text(const string16& text) { text_ = text; }

  bool fade() const { return fade_; }
  void set_fade(bool fade) { fade_ = fade; }

  gfx::CanvasSkia::TruncateFadeMode fade_mode() const { return fade_mode_; }
  void set_fade_mode(gfx::CanvasSkia::TruncateFadeMode fade_mode) {
    fade_mode_ = fade_mode;
  }

 private:
  // The font used for drawing the text.
  gfx::Font font_;

  // The text to draw.
  string16 text_;

  // Text flags for passing to |DrawStringInt()|.
  int text_flags_;

  // If |true|, specifies to call |DrawFadeTruncatingString()| instead of
  // |DrawStringInt()|.
  bool fade_;

  // If |fade_| is |true|, fade mode parameter to |DrawFadeTruncatingString()|.
  gfx::CanvasSkia::TruncateFadeMode fade_mode_;

  DISALLOW_COPY_AND_ASSIGN(TextExampleView);
};

TextExample::TextExample(ExamplesMain* main)
    : ExampleBase(main, "Text Styles") {
}

TextExample::~TextExample() {
}

views::Combobox* TextExample::AddCombobox(views::GridLayout* layout,
                                          const char* name,
                                          const char** strings,
                                          int count) {
  layout->StartRow(0, 0);
  layout->AddView(new views::Label(ASCIIToUTF16(name)));
  views::Combobox* combo_box =
      new views::Combobox(new ExampleComboboxModel(strings, count));
  combo_box->SetSelectedItem(0);
  combo_box->set_listener(this);
  layout->AddView(combo_box);
  return combo_box;
}

void TextExample::CreateExampleView(views::View* container) {
  text_view_ = new TextExampleView;

  views::GridLayout* layout = new views::GridLayout(container);
  container->SetLayoutManager(layout);

  layout->AddPaddingRow(0, 8);

  views::ColumnSet* column_set = layout->AddColumnSet(0);
  column_set->AddPaddingColumn(0, 8);
  column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL,
                        0.1f, views::GridLayout::USE_PREF, 0, 0);
  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
                        0.9f, views::GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, 8);

  h_align_cb_ = AddCombobox(layout,
                            "H-Align",
                            kHorizontalAligments,
                            arraysize(kHorizontalAligments));
  v_align_cb_ = AddCombobox(layout,
                            "V-Align",
                            kVerticalAlignments,
                            arraysize(kVerticalAlignments));
  eliding_cb_ = AddCombobox(layout,
                            "Eliding",
                            kElidingBehaviors,
                            arraysize(kElidingBehaviors));
  prefix_cb_ = AddCombobox(layout,
                           "Prefix",
                           kPrefixOptions,
                           arraysize(kPrefixOptions));
  text_cb_ = AddCombobox(layout,
                         "Example Text",
                         kTextExamples,
                         arraysize(kTextExamples));

  layout->StartRow(0, 0);
  multiline_checkbox_ = new views::Checkbox(L"Multiline");
  multiline_checkbox_->set_listener(this);
  layout->AddView(multiline_checkbox_);
  break_checkbox_ = new views::Checkbox(L"Character Break");
  break_checkbox_->set_listener(this);
  layout->AddView(break_checkbox_);

  layout->AddPaddingRow(0, 32);

  column_set = layout->AddColumnSet(1);
  column_set->AddPaddingColumn(0, 16);
  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
                        1, views::GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, 16);
  layout->StartRow(1, 1);
  layout->AddView(text_view_);

  layout->AddPaddingRow(0, 8);
}

void TextExample::ButtonPressed(views::Button* button,
                                const views::Event& event) {
  int text_flags = text_view_->text_flags();
  if (button == multiline_checkbox_) {
    if (multiline_checkbox_->checked())
      text_flags |= gfx::Canvas::MULTI_LINE;
    else
      text_flags &= ~gfx::Canvas::MULTI_LINE;
  } else if (button == break_checkbox_) {
    if (break_checkbox_->checked())
      text_flags |= gfx::Canvas::CHARACTER_BREAK;
    else
      text_flags &= ~gfx::Canvas::CHARACTER_BREAK;
  }
  text_view_->set_text_flags(text_flags);
  text_view_->SchedulePaint();
}

void TextExample::ItemChanged(views::Combobox* combo_box,
                              int prev_index,
                              int new_index) {
  int text_flags = text_view_->text_flags();
  if (combo_box == h_align_cb_) {
    text_flags &= ~(gfx::Canvas::TEXT_ALIGN_LEFT |
                    gfx::Canvas::TEXT_ALIGN_CENTER |
                    gfx::Canvas::TEXT_ALIGN_RIGHT);
    switch (new_index) {
      case 0:
        break;
      case 1:
        text_flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
        break;
      case 2:
        text_flags |= gfx::Canvas::TEXT_ALIGN_CENTER;
        break;
      case 3:
        text_flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
        break;
    }
  } else if (combo_box == v_align_cb_) {
    text_flags &= ~(gfx::Canvas::TEXT_VALIGN_TOP |
                    gfx::Canvas::TEXT_VALIGN_MIDDLE |
                    gfx::Canvas::TEXT_VALIGN_BOTTOM);
    switch (new_index) {
      case 0:
        break;
      case 1:
        text_flags |= gfx::Canvas::TEXT_VALIGN_TOP;
        break;
      case 2:
        text_flags |= gfx::Canvas::TEXT_VALIGN_MIDDLE;
        break;
      case 3:
        text_flags |= gfx::Canvas::TEXT_VALIGN_BOTTOM;
        break;
    }
  } else if (combo_box == text_cb_) {
    switch (new_index) {
      case 0:
        text_view_->set_text(ASCIIToUTF16(kShortText));
        break;
      case 1:
        text_view_->set_text(ASCIIToUTF16(kMediumText));
        break;
      case 2:
        text_view_->set_text(ASCIIToUTF16(kLongText));
        break;
      case 3:
        text_view_->set_text(ASCIIToUTF16(kAmpersandText));
        break;
    }
  } else if (combo_box == eliding_cb_) {
    switch (new_index) {
      case 0:
        text_flags &= ~gfx::Canvas::NO_ELLIPSIS;
        text_view_->set_fade(false);
        break;
      case 1:
        text_flags |= gfx::Canvas::NO_ELLIPSIS;
        text_view_->set_fade(false);
        break;
#if defined(OS_WIN)
      case 2:
        text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeTail);
        text_view_->set_fade(true);
        break;
      case 3:
        text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeHead);
        text_view_->set_fade(true);
        break;
      case 4:
        text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeHeadAndTail);
        text_view_->set_fade(true);
        break;
#endif
    }
  } else if (combo_box == prefix_cb_) {
    text_flags &= ~(gfx::Canvas::SHOW_PREFIX | gfx::Canvas::HIDE_PREFIX);
    switch (new_index) {
      case 0:
        break;
      case 1:
        text_flags |= gfx::Canvas::SHOW_PREFIX;
        break;
      case 2:
        text_flags |= gfx::Canvas::HIDE_PREFIX;
        break;
    }
  }
  text_view_->set_text_flags(text_flags);
  text_view_->SchedulePaint();
}

}  // namespace examples