summaryrefslogtreecommitdiff
path: root/chromium/ui/views/examples/label_example.cc
blob: 05e30aa2e1254a27dde912746d34a2bc56f6b96e (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
// Copyright (c) 2012 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 "ui/views/examples/label_example.h"

#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/combobox/combobox.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/examples/example_combobox_model.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/view.h"

using base::ASCIIToUTF16;
using base::WideToUTF16;

namespace views {
namespace examples {

namespace {

const char* kAlignments[] = { "Left", "Center", "Right", "Head" };

// A Label with a clamped preferred width to demonstrate eliding or wrapping.
class ExamplePreferredSizeLabel : public Label {
 public:
  ExamplePreferredSizeLabel() : Label() {
    SetBorder(CreateSolidBorder(1, SK_ColorGRAY));
  }
  ~ExamplePreferredSizeLabel() override {}

  // Label:
  gfx::Size CalculatePreferredSize() const override {
    return gfx::Size(50, Label::CalculatePreferredSize().height());
  }

  static const char* kElideBehaviors[];

 private:
  DISALLOW_COPY_AND_ASSIGN(ExamplePreferredSizeLabel);
};

// static
const char* ExamplePreferredSizeLabel::kElideBehaviors[] = {
    "No Elide",   "Truncate",    "Elide Head", "Elide Middle",
    "Elide Tail", "Elide Email", "Fade Tail"};

}  // namespace

LabelExample::LabelExample()
    : ExampleBase("Label"),
      textfield_(NULL),
      alignment_(NULL),
      elide_behavior_(NULL),
      multiline_(NULL),
      shadows_(NULL),
      custom_label_(NULL) {
}

LabelExample::~LabelExample() {
  // Remove the views first as some reference combobox models.
  container()->RemoveAllChildViews(true);
}

void LabelExample::CreateExampleView(View* container) {
  // A very simple label example, followed by additional helpful examples.
  container->SetLayoutManager(
      std::make_unique<BoxLayout>(BoxLayout::kVertical, gfx::Insets(), 10));
  Label* label = new Label(ASCIIToUTF16("Hello world!"));
  container->AddChildView(label);

  const wchar_t hello_world_hebrew[] =
      L"\x5e9\x5dc\x5d5\x5dd \x5d4\x5e2\x5d5\x5dc\x5dd!";
  label = new Label(WideToUTF16(hello_world_hebrew));
  label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
  container->AddChildView(label);

  label = new Label(WideToUTF16(L"A UTF16 surrogate pair: \x5d0\x5b0"));
  label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
  container->AddChildView(label);

  label = new Label(ASCIIToUTF16("A left-aligned blue label."));
  label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label->SetEnabledColor(SK_ColorBLUE);
  container->AddChildView(label);

  label = new Label(WideToUTF16(L"Password!"));
  label->SetObscured(true);
  container->AddChildView(label);

  label = new Label(ASCIIToUTF16("A Courier-18 label with shadows."));
  label->SetFontList(gfx::FontList("Courier, 18px"));
  gfx::ShadowValues shadows(1,
                            gfx::ShadowValue(gfx::Vector2d(), 1, SK_ColorRED));
  gfx::ShadowValue shadow(gfx::Vector2d(2, 2), 0, SK_ColorGRAY);
  shadows.push_back(shadow);
  label->SetShadows(shadows);
  container->AddChildView(label);

  label = new ExamplePreferredSizeLabel();
  label->SetText(ASCIIToUTF16("A long label will elide toward its logical end "
      "if the text's width exceeds the label's available width."));
  container->AddChildView(label);

  label = new ExamplePreferredSizeLabel();
  label->SetText(ASCIIToUTF16("A multi-line label will wrap onto subsequent "
    "lines if the text's width exceeds the label's available width, which is "
    "helpful for extemely long text used to demonstrate line wrapping."));
  label->SetMultiLine(true);
  container->AddChildView(label);

  label = new Label(ASCIIToUTF16("Label with thick border"));
  label->SetBorder(CreateSolidBorder(20, SK_ColorRED));
  container->AddChildView(label);

  label = new Label(
      ASCIIToUTF16("A multiline label...\n\n...which supports text selection"));
  label->SetSelectable(true);
  label->SetMultiLine(true);
  container->AddChildView(label);

  AddCustomLabel(container);
}

void LabelExample::ButtonPressed(Button* button, const ui::Event& event) {
  if (button == multiline_) {
    custom_label_->SetMultiLine(multiline_->checked());
  } else if (button == shadows_) {
    gfx::ShadowValues shadows;
    if (shadows_->checked()) {
      shadows.push_back(gfx::ShadowValue(gfx::Vector2d(), 1, SK_ColorRED));
      shadows.push_back(gfx::ShadowValue(gfx::Vector2d(2, 2), 0, SK_ColorGRAY));
    }
    custom_label_->SetShadows(shadows);
  } else if (button == selectable_) {
    custom_label_->SetSelectable(selectable_->checked());
  }
  custom_label_->parent()->parent()->Layout();
  custom_label_->SchedulePaint();
}

void LabelExample::OnPerformAction(Combobox* combobox) {
  if (combobox == alignment_) {
    custom_label_->SetHorizontalAlignment(
        static_cast<gfx::HorizontalAlignment>(combobox->selected_index()));
  } else if (combobox == elide_behavior_) {
    custom_label_->SetElideBehavior(
        static_cast<gfx::ElideBehavior>(combobox->selected_index()));
  }
}

void LabelExample::ContentsChanged(Textfield* sender,
                                   const base::string16& new_contents) {
  custom_label_->SetText(new_contents);
  custom_label_->parent()->parent()->Layout();
}

void LabelExample::AddCustomLabel(View* container) {
  View* control_container = new View();
  control_container->SetBorder(CreateSolidBorder(2, SK_ColorGRAY));
  control_container->SetBackground(CreateSolidBackground(SK_ColorLTGRAY));
  GridLayout* layout = control_container->SetLayoutManager(
      std::make_unique<views::GridLayout>(control_container));

  ColumnSet* column_set = layout->AddColumnSet(0);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL,
                        0.0f, GridLayout::USE_PREF, 0, 0);
  column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
                        1.0f, GridLayout::USE_PREF, 0, 0);

  layout->StartRow(0, 0);
  layout->AddView(new Label(ASCIIToUTF16("Content: ")));
  textfield_ = new Textfield();
  textfield_->SetText(ASCIIToUTF16("Use the provided controls to configure the "
      "content and presentation of this custom label."));
  textfield_->SetSelectionRange(gfx::Range());
  textfield_->set_controller(this);
  layout->AddView(textfield_);

  alignment_ = AddCombobox(layout, "Alignment: ", kAlignments,
                           arraysize(kAlignments));
  elide_behavior_ = AddCombobox(
      layout, "Elide Behavior: ", ExamplePreferredSizeLabel::kElideBehaviors,
      arraysize(ExamplePreferredSizeLabel::kElideBehaviors));

  column_set = layout->AddColumnSet(1);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING,
                        0, GridLayout::USE_PREF, 0, 0);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING,
                        0, GridLayout::USE_PREF, 0, 0);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0,
                        GridLayout::USE_PREF, 0, 0);
  layout->StartRow(0, 1);
  multiline_ = new Checkbox(base::ASCIIToUTF16("Multiline"));
  multiline_->set_listener(this);
  layout->AddView(multiline_);
  shadows_ = new Checkbox(base::ASCIIToUTF16("Shadows"));
  shadows_->set_listener(this);
  layout->AddView(shadows_);
  selectable_ = new Checkbox(base::ASCIIToUTF16("Selectable"));
  selectable_->set_listener(this);
  layout->AddView(selectable_);
  layout->AddPaddingRow(0, 8);

  column_set = layout->AddColumnSet(2);
  column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
                        1, GridLayout::USE_PREF, 0, 0);
  layout->StartRow(0, 2);
  custom_label_ = new ExamplePreferredSizeLabel();
  custom_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  custom_label_->SetElideBehavior(gfx::NO_ELIDE);
  custom_label_->SetText(textfield_->text());
  layout->AddView(custom_label_);

  // Disable the text selection checkbox if |custom_label_| does not support
  // text selection.
  selectable_->SetEnabled(custom_label_->IsSelectionSupported());

  container->AddChildView(control_container);
}

Combobox* LabelExample::AddCombobox(GridLayout* layout,
                                    const char* name,
                                    const char** strings,
                                    int count) {
  layout->StartRow(0, 0);
  layout->AddView(new Label(base::ASCIIToUTF16(name)));
  ExampleComboboxModel* model = new ExampleComboboxModel(strings, count);
  example_combobox_models_.push_back(base::WrapUnique(model));
  Combobox* combobox = new Combobox(model);
  combobox->SetSelectedIndex(0);
  combobox->set_listener(this);
  layout->AddView(combobox);
  return combobox;
}

}  // namespace examples
}  // namespace views