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