Home | History | Annotate | Download | only in examples
      1 // Copyright 2013 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/multiline_example.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "ui/base/events/event.h"
      9 #include "ui/gfx/render_text.h"
     10 #include "ui/views/controls/label.h"
     11 #include "ui/views/controls/textfield/textfield.h"
     12 #include "ui/views/layout/grid_layout.h"
     13 #include "ui/views/view.h"
     14 
     15 namespace views {
     16 namespace examples {
     17 
     18 // A simple View that hosts a RenderText object.
     19 class MultilineExample::RenderTextView : public View {
     20  public:
     21   explicit RenderTextView(gfx::RenderText* render_text)
     22       : render_text_(render_text) {
     23     render_text_->SetColor(SK_ColorBLACK);
     24     set_border(Border::CreateSolidBorder(2, SK_ColorGRAY));
     25   }
     26 
     27   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
     28     View::OnPaint(canvas);
     29     render_text_->Draw(canvas);
     30   }
     31 
     32   virtual gfx::Size GetPreferredSize() OVERRIDE {
     33     return gfx::Size(0,
     34         render_text_->font_list().GetHeight() + GetInsets().height());
     35   }
     36 
     37   void SetText(const string16& new_contents) {
     38     render_text_->SetText(new_contents);
     39     SchedulePaint();
     40   }
     41 
     42  private:
     43   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE {
     44     gfx::Rect bounds = GetLocalBounds();
     45     bounds.Inset(GetInsets());
     46     render_text_->SetDisplayRect(bounds);
     47   }
     48 
     49   scoped_ptr<gfx::RenderText> render_text_;
     50 
     51   DISALLOW_COPY_AND_ASSIGN(RenderTextView);
     52 };
     53 
     54 MultilineExample::MultilineExample()
     55     : ExampleBase("Multiline RenderText"),
     56       render_text_view_(NULL),
     57       label_(NULL),
     58       textfield_(NULL) {
     59 }
     60 
     61 MultilineExample::~MultilineExample() {
     62 }
     63 
     64 void MultilineExample::CreateExampleView(View* container) {
     65   const char kTestString[] = "test string asdf 1234 test string asdf 1234 "
     66                              "test string asdf 1234 test string asdf 1234";
     67 
     68   gfx::RenderText* render_text = gfx::RenderText::CreateInstance();
     69   render_text->SetText(ASCIIToUTF16(kTestString));
     70   render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER);
     71 
     72   render_text_view_ = new RenderTextView(render_text);
     73 
     74   label_ = new Label();
     75   label_->SetText(ASCIIToUTF16(kTestString));
     76   label_->SetMultiLine(true);
     77   label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN));
     78 
     79   textfield_ = new Textfield();
     80   textfield_->SetController(this);
     81   textfield_->SetText(ASCIIToUTF16(kTestString));
     82 
     83   GridLayout* layout = new GridLayout(container);
     84   container->SetLayoutManager(layout);
     85 
     86   ColumnSet* column_set = layout->AddColumnSet(0);
     87   column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL,
     88       0.0f, GridLayout::USE_PREF, 0, 0);
     89   column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
     90       1.0f, GridLayout::FIXED, 0, 0);
     91 
     92   layout->StartRow(0, 0);
     93   layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:")));
     94   layout->AddView(render_text_view_);
     95 
     96   layout->StartRow(0, 0);
     97   layout->AddView(new Label(ASCIIToUTF16("views::Label:")));
     98   layout->AddView(label_);
     99 
    100   layout->StartRow(0, 0);
    101   layout->AddView(new Label(ASCIIToUTF16("Sample Text:")));
    102   layout->AddView(textfield_);
    103 }
    104 
    105 void MultilineExample::ContentsChanged(Textfield* sender,
    106                                        const string16& new_contents) {
    107   render_text_view_->SetText(new_contents);
    108   label_->SetText(new_contents);
    109 }
    110 
    111 bool MultilineExample::HandleKeyEvent(Textfield* sender,
    112                                       const ui::KeyEvent& key_event) {
    113   return false;
    114 }
    115 
    116 }  // namespace examples
    117 }  // namespace views
    118