17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/views/examples/multiline_example.h"
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/strings/utf_string_conversions.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/base/events/event.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/gfx/render_text.h"
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/views/controls/label.h"
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/views/controls/textfield/textfield.h"
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/views/layout/grid_layout.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/views/view.h"
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace views {
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace examples {
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// A simple View that hosts a RenderText object.
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class MultilineExample::RenderTextView : public View {
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  explicit RenderTextView(gfx::RenderText* render_text)
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      : render_text_(render_text) {
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    render_text_->SetColor(SK_ColorBLACK);
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    set_border(Border::CreateSolidBorder(2, SK_ColorGRAY));
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    View::OnPaint(canvas);
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    render_text_->Draw(canvas);
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual gfx::Size GetPreferredSize() OVERRIDE {
337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return gfx::Size(0,
34a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        render_text_->font_list().GetHeight() + GetInsets().height());
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void SetText(const string16& new_contents) {
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    render_text_->SetText(new_contents);
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    SchedulePaint();
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) private:
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE {
447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    gfx::Rect bounds = GetLocalBounds();
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    bounds.Inset(GetInsets());
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    render_text_->SetDisplayRect(bounds);
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_ptr<gfx::RenderText> render_text_;
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(RenderTextView);
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)MultilineExample::MultilineExample()
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : ExampleBase("Multiline RenderText"),
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      render_text_view_(NULL),
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      label_(NULL),
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      textfield_(NULL) {
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)MultilineExample::~MultilineExample() {
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void MultilineExample::CreateExampleView(View* container) {
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  const char kTestString[] = "test string asdf 1234 test string asdf 1234 "
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                             "test string asdf 1234 test string asdf 1234";
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  gfx::RenderText* render_text = gfx::RenderText::CreateInstance();
697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  render_text->SetText(ASCIIToUTF16(kTestString));
707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER);
717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  render_text_view_ = new RenderTextView(render_text);
737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  label_ = new Label();
757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  label_->SetText(ASCIIToUTF16(kTestString));
767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  label_->SetMultiLine(true);
777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN));
787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  textfield_ = new Textfield();
807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  textfield_->SetController(this);
817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  textfield_->SetText(ASCIIToUTF16(kTestString));
827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  GridLayout* layout = new GridLayout(container);
847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  container->SetLayoutManager(layout);
857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ColumnSet* column_set = layout->AddColumnSet(0);
877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL,
887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      0.0f, GridLayout::USE_PREF, 0, 0);
897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      1.0f, GridLayout::FIXED, 0, 0);
917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->StartRow(0, 0);
937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:")));
947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->AddView(render_text_view_);
957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->StartRow(0, 0);
977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->AddView(new Label(ASCIIToUTF16("views::Label:")));
987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->AddView(label_);
997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->StartRow(0, 0);
1017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->AddView(new Label(ASCIIToUTF16("Sample Text:")));
1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  layout->AddView(textfield_);
1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void MultilineExample::ContentsChanged(Textfield* sender,
1067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                       const string16& new_contents) {
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  render_text_view_->SetText(new_contents);
1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  label_->SetText(new_contents);
1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool MultilineExample::HandleKeyEvent(Textfield* sender,
1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      const ui::KeyEvent& key_event) {
1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return false;
1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace examples
1177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace views
118