Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / views / examples / multiline_example.cc
index dd8a633..8c86d9e 100644 (file)
@@ -7,14 +7,44 @@
 #include "base/strings/utf_string_conversions.h"
 #include "ui/events/event.h"
 #include "ui/gfx/render_text.h"
+#include "ui/views/background.h"
+#include "ui/views/border.h"
+#include "ui/views/controls/button/checkbox.h"
 #include "ui/views/controls/label.h"
 #include "ui/views/controls/textfield/textfield.h"
 #include "ui/views/layout/grid_layout.h"
 #include "ui/views/view.h"
 
+using base::ASCIIToUTF16;
+
 namespace views {
 namespace examples {
 
+namespace {
+
+gfx::Range ClampRange(gfx::Range range, size_t max) {
+  range.set_start(std::min(range.start(), max));
+  range.set_end(std::min(range.end(), max));
+  return range;
+}
+
+// A Label with a clamped preferred width to demonstrate wrapping.
+class PreferredSizeLabel : public Label {
+ public:
+  PreferredSizeLabel() : Label() {}
+  virtual ~PreferredSizeLabel() {}
+
+  // Label:
+  virtual gfx::Size GetPreferredSize() const OVERRIDE {
+    return gfx::Size(50, Label::GetPreferredSize().height());
+  }
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel);
+};
+
+}  // namespace
+
 // A simple View that hosts a RenderText object.
 class MultilineExample::RenderTextView : public View {
  public:
@@ -22,7 +52,7 @@ class MultilineExample::RenderTextView : public View {
     render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
     render_text_->SetColor(SK_ColorBLACK);
     render_text_->SetMultiline(true);
-    set_border(Border::CreateSolidBorder(2, SK_ColorGRAY));
+    SetBorder(Border::CreateSolidBorder(2, SK_ColorGRAY));
   }
 
   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
@@ -30,7 +60,7 @@ class MultilineExample::RenderTextView : public View {
     render_text_->Draw(canvas);
   }
 
-  virtual gfx::Size GetPreferredSize() OVERRIDE {
+  virtual gfx::Size GetPreferredSize() const OVERRIDE {
     // Turn off multiline mode to get the single-line text size, which is the
     // preferred size for this view.
     render_text_->SetMultiline(false);
@@ -41,29 +71,35 @@ class MultilineExample::RenderTextView : public View {
     return size;
   }
 
-  virtual int GetHeightForWidth(int w) OVERRIDE {
+  virtual int GetHeightForWidth(int w) const OVERRIDE {
     // TODO(ckocagil): Why does this happen?
     if (w == 0)
       return View::GetHeightForWidth(w);
-    gfx::Rect rect = render_text_->display_rect();
+    const gfx::Rect old_rect = render_text_->display_rect();
+    gfx::Rect rect = old_rect;
     rect.set_width(w - GetInsets().width());
     render_text_->SetDisplayRect(rect);
-    return render_text_->GetStringSize().height() + GetInsets().height();
+    int height = render_text_->GetStringSize().height() + GetInsets().height();
+    render_text_->SetDisplayRect(old_rect);
+    return height;
   }
 
-  void SetText(const string16& new_contents) {
+  void SetText(const base::string16& new_contents) {
     // Color and style the text inside |test_range| to test colors and styles.
-    gfx::Range test_range(1, 21);
-    test_range.set_start(std::min(test_range.start(), new_contents.length()));
-    test_range.set_end(std::min(test_range.end(), new_contents.length()));
+    const size_t range_max = new_contents.length();
+    gfx::Range color_range = ClampRange(gfx::Range(1, 21), range_max);
+    gfx::Range bold_range = ClampRange(gfx::Range(4, 10), range_max);
+    gfx::Range italic_range = ClampRange(gfx::Range(7, 13), range_max);
 
     render_text_->SetText(new_contents);
     render_text_->SetColor(SK_ColorBLACK);
-    render_text_->ApplyColor(0xFFFF0000, test_range);
+    render_text_->ApplyColor(0xFFFF0000, color_range);
     render_text_->SetStyle(gfx::DIAGONAL_STRIKE, false);
-    render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, test_range);
+    render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, color_range);
     render_text_->SetStyle(gfx::UNDERLINE, false);
-    render_text_->ApplyStyle(gfx::UNDERLINE, true, test_range);
+    render_text_->ApplyStyle(gfx::UNDERLINE, true, color_range);
+    render_text_->ApplyStyle(gfx::BOLD, true, bold_range);
+    render_text_->ApplyStyle(gfx::ITALIC, true, italic_range);
     InvalidateLayout();
   }
 
@@ -91,16 +127,17 @@ MultilineExample::~MultilineExample() {
 }
 
 void MultilineExample::CreateExampleView(View* container) {
-  const char kTestString[] = "test string asdf 1234 test string asdf 1234 "
-                             "test string asdf 1234 test string asdf 1234";
+  const base::string16 kTestString = base::WideToUTF16(L"qwerty"
+      L"\x627\x644\x631\x626\x64A\x633\x64A\x629"
+      L"asdfgh");
 
   render_text_view_ = new RenderTextView();
-  render_text_view_->SetText(ASCIIToUTF16(kTestString));
+  render_text_view_->SetText(kTestString);
 
-  label_ = new Label();
-  label_->SetText(ASCIIToUTF16(kTestString));
+  label_ = new PreferredSizeLabel();
+  label_->SetText(kTestString);
   label_->SetMultiLine(true);
-  label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN));
+  label_->SetBorder(Border::CreateSolidBorder(2, SK_ColorCYAN));
 
   label_checkbox_ = new Checkbox(ASCIIToUTF16("views::Label:"));
   label_checkbox_->SetChecked(true);
@@ -108,8 +145,8 @@ void MultilineExample::CreateExampleView(View* container) {
   label_checkbox_->set_request_focus_on_press(false);
 
   textfield_ = new Textfield();
-  textfield_->SetController(this);
-  textfield_->SetText(ASCIIToUTF16(kTestString));
+  textfield_->set_controller(this);
+  textfield_->SetText(kTestString);
 
   GridLayout* layout = new GridLayout(container);
   container->SetLayoutManager(layout);
@@ -134,7 +171,7 @@ void MultilineExample::CreateExampleView(View* container) {
 }
 
 void MultilineExample::ContentsChanged(Textfield* sender,
-                                       const string16& new_contents) {
+                                       const base::string16& new_contents) {
   render_text_view_->SetText(new_contents);
   if (label_checkbox_->checked())
     label_->SetText(new_contents);
@@ -142,14 +179,10 @@ void MultilineExample::ContentsChanged(Textfield* sender,
   container()->SchedulePaint();
 }
 
-bool MultilineExample::HandleKeyEvent(Textfield* sender,
-                                      const ui::KeyEvent& key_event) {
-  return false;
-}
-
 void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) {
   DCHECK_EQ(sender, label_checkbox_);
-  label_->SetText(label_checkbox_->checked() ? textfield_->text() : string16());
+  label_->SetText(label_checkbox_->checked() ? textfield_->text() :
+                                               base::string16());
   container()->Layout();
   container()->SchedulePaint();
 }