Home | History | Annotate | Download | only in examples
      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() : Label() {
     25     SetBorder(Border::CreateSolidBorder(2, SK_ColorCYAN));
     26   }
     27   virtual ~PreferredSizeLabel() {}
     28 
     29   // Label:
     30   virtual gfx::Size GetPreferredSize() const OVERRIDE {
     31     return gfx::Size(100, 40);
     32   }
     33 
     34  private:
     35   DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel);
     36 };
     37 
     38 }  // namespace
     39 
     40 LabelExample::LabelExample() : ExampleBase("Label") {}
     41 
     42 LabelExample::~LabelExample() {}
     43 
     44 void LabelExample::CreateExampleView(View* container) {
     45   // A very simple label example, followed by additional helpful examples.
     46   container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 0, 0, 10));
     47   Label* label = new Label(ASCIIToUTF16("Hello world!"));
     48   container->AddChildView(label);
     49 
     50   const wchar_t hello_world_hebrew[] =
     51       L"\x5e9\x5dc\x5d5\x5dd \x5d4\x5e2\x5d5\x5dc\x5dd!";
     52   label = new Label(WideToUTF16(hello_world_hebrew));
     53   label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
     54   container->AddChildView(label);
     55 
     56   label = new Label(WideToUTF16(L"A UTF16 surrogate pair: \x5d0\x5b0"));
     57   label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
     58   container->AddChildView(label);
     59 
     60   label = new Label(ASCIIToUTF16("A left-aligned blue label."));
     61   label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
     62   label->SetEnabledColor(SK_ColorBLUE);
     63   container->AddChildView(label);
     64 
     65   label = new Label(ASCIIToUTF16("A Courier-18 label with shadows."));
     66   label->SetFontList(gfx::FontList("Courier, 18px"));
     67   gfx::ShadowValues shadows(1, gfx::ShadowValue(gfx::Point(), 1, SK_ColorRED));
     68   gfx::ShadowValue shadow(gfx::Point(2, 2), 0, SK_ColorGRAY);
     69   shadows.push_back(shadow);
     70   label->set_shadows(shadows);
     71   container->AddChildView(label);
     72 
     73   label = new PreferredSizeLabel();
     74   label->SetText(ASCIIToUTF16("A long label will elide toward its logical end "
     75       "if the text's width exceeds the label's available width."));
     76   container->AddChildView(label);
     77 
     78   label = new PreferredSizeLabel();
     79   label->SetElideBehavior(gfx::FADE_TAIL);
     80   label->SetText(ASCIIToUTF16("Some long labels will fade, rather than elide, "
     81       "if the text's width exceeds the label's available width."));
     82   container->AddChildView(label);
     83 
     84   label = new PreferredSizeLabel();
     85   label->SetText(ASCIIToUTF16("A multi-line label will wrap onto subsequent "
     86       "lines if the text's width exceeds the label's available width."));
     87   label->SetMultiLine(true);
     88   container->AddChildView(label);
     89 
     90   label = new Label(WideToUTF16(L"Password!"));
     91   label->SetObscured(true);
     92   container->AddChildView(label);
     93 }
     94 
     95 }  // namespace examples
     96 }  // namespace views
     97