Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / ui / views / examples / label_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/label_example.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/views/border.h"
9 #include "ui/views/controls/label.h"
10 #include "ui/views/layout/box_layout.h"
11 #include "ui/views/view.h"
12
13 using base::ASCIIToUTF16;
14 using base::WideToUTF16;
15
16 namespace views {
17 namespace examples {
18
19 namespace {
20
21 // A Label with a constrained preferred size to demonstrate eliding or wrapping.
22 class PreferredSizeLabel : public Label {
23  public:
24   PreferredSizeLabel();
25   virtual ~PreferredSizeLabel();
26
27   // Label:
28   virtual gfx::Size GetPreferredSize() OVERRIDE;
29
30  private:
31   DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel);
32 };
33
34 PreferredSizeLabel::PreferredSizeLabel() : Label() {
35   SetBorder(Border::CreateSolidBorder(2, SK_ColorCYAN));
36 }
37
38 PreferredSizeLabel::~PreferredSizeLabel() {}
39
40 gfx::Size PreferredSizeLabel::GetPreferredSize() { return gfx::Size(100, 40); }
41
42 }  // namespace
43
44 LabelExample::LabelExample() : ExampleBase("Label") {}
45
46 LabelExample::~LabelExample() {}
47
48 void LabelExample::CreateExampleView(View* container) {
49   // A very simple label example, followed by additional helpful examples.
50   container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 0, 0, 10));
51   Label* label = new Label(ASCIIToUTF16("Hello world!"));
52   container->AddChildView(label);
53
54   const wchar_t hello_world_hebrew[] =
55       L"\x5e9\x5dc\x5d5\x5dd \x5d4\x5e2\x5d5\x5dc\x5dd!";
56   label = new Label(WideToUTF16(hello_world_hebrew));
57   label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
58   container->AddChildView(label);
59
60   label = new Label(WideToUTF16(L"A UTF16 surrogate pair: \x5d0\x5b0"));
61   label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
62   container->AddChildView(label);
63
64   label = new Label(ASCIIToUTF16("A left-aligned blue label."));
65   label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
66   label->SetEnabledColor(SK_ColorBLUE);
67   container->AddChildView(label);
68
69   label = new Label(ASCIIToUTF16("A Courier-18 label with a shadow."));
70   label->SetFontList(gfx::FontList("Courier, 18px"));
71   label->SetShadowColors(SK_ColorGRAY, SK_ColorLTGRAY);
72   label->SetShadowOffset(1, 1);
73   container->AddChildView(label);
74
75   label = new PreferredSizeLabel();
76   label->SetText(ASCIIToUTF16("A long label will elide toward its logical end "
77       "if the text's width exceeds the label's available width."));
78   container->AddChildView(label);
79
80   label = new PreferredSizeLabel();
81   label->SetText(ASCIIToUTF16("A multi-line label will wrap onto subsequent "
82       "lines if the text's width exceeds the label's available width."));
83   label->SetMultiLine(true);
84   container->AddChildView(label);
85 }
86
87 }  // namespace examples
88 }  // namespace views