OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/examples/multiline_example.h" | 5 #include "ui/views/examples/multiline_example.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "ui/base/events/event.h" | 8 #include "ui/base/events/event.h" |
9 #include "ui/gfx/render_text.h" | 9 #include "ui/gfx/render_text.h" |
10 #include "ui/views/controls/label.h" | 10 #include "ui/views/controls/label.h" |
11 #include "ui/views/controls/textfield/textfield.h" | 11 #include "ui/views/controls/textfield/textfield.h" |
12 #include "ui/views/layout/grid_layout.h" | 12 #include "ui/views/layout/grid_layout.h" |
13 #include "ui/views/view.h" | 13 #include "ui/views/view.h" |
14 | 14 |
15 namespace views { | 15 namespace views { |
16 namespace examples { | 16 namespace examples { |
17 | 17 |
18 // A simple View that hosts a RenderText object. | 18 // A simple View that hosts a RenderText object. |
19 class MultilineExample::RenderTextView : public View { | 19 class MultilineExample::RenderTextView : public View { |
20 public: | 20 public: |
21 explicit RenderTextView(gfx::RenderText* render_text) | 21 RenderTextView() : render_text_(gfx::RenderText::CreateInstance()) { |
22 : render_text_(render_text) { | 22 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
23 render_text_->SetColor(SK_ColorBLACK); | 23 render_text_->SetColor(SK_ColorBLACK); |
24 render_text_->SetMultiline(true); | |
24 set_border(Border::CreateSolidBorder(2, SK_ColorGRAY)); | 25 set_border(Border::CreateSolidBorder(2, SK_ColorGRAY)); |
25 } | 26 } |
26 | 27 |
27 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | 28 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |
28 View::OnPaint(canvas); | 29 View::OnPaint(canvas); |
29 render_text_->Draw(canvas); | 30 render_text_->Draw(canvas); |
30 } | 31 } |
31 | 32 |
32 virtual gfx::Size GetPreferredSize() OVERRIDE { | 33 virtual gfx::Size GetPreferredSize() OVERRIDE { |
33 return gfx::Size(0, | 34 render_text_->SetMultiline(false); |
34 render_text_->font_list().GetHeight() + GetInsets().height()); | 35 gfx::Size size(render_text_->GetContentWidth(), |
msw
2013/09/06 23:47:45
Can you simply init |size| to render_text_->GetStr
ckocagil
2013/09/11 14:59:49
If I do that, |size| won't reserve space for the c
msw
2013/09/11 17:32:37
Okay
| |
36 render_text_->GetStringSize().height()); | |
37 size.Enlarge(GetInsets().width(), GetInsets().height()); | |
38 render_text_->SetMultiline(true); | |
39 return size; | |
40 } | |
41 | |
42 virtual int GetHeightForWidth(int w) OVERRIDE { | |
43 if (w == 0) // TODO(ckocagil): Why does this happen? | |
msw
2013/09/06 23:47:45
nit: Move the comment up to the previous line.
ckocagil
2013/09/11 14:59:49
Done.
| |
44 return View::GetHeightForWidth(w); | |
45 gfx::Rect rect = render_text_->display_rect(); | |
46 rect.set_width(w - GetInsets().width()); | |
47 render_text_->SetDisplayRect(rect); | |
48 return render_text_->GetStringSize().height() + GetInsets().height(); | |
35 } | 49 } |
36 | 50 |
37 void SetText(const string16& new_contents) { | 51 void SetText(const string16& new_contents) { |
38 render_text_->SetText(new_contents); | 52 render_text_->SetText(new_contents); |
39 SchedulePaint(); | 53 ui::Range test_range(1, 21); |
msw
2013/09/06 23:47:45
Ensure new_contents is at least 12 chars. Also nit
ckocagil
2013/09/11 14:59:49
I now ensure that test_range.start() and test_rang
| |
54 render_text_->MoveCursorTo(gfx::SelectionModel(test_range, | |
msw
2013/09/06 23:47:45
nit: can you call RenderText::ApplyColor additiona
ckocagil
2013/09/11 14:59:49
Done, called instead.
| |
55 gfx::CURSOR_FORWARD)); | |
56 render_text_->set_selection_background_focused_color(0xFFFF0000); | |
57 render_text_->set_focused(true); | |
58 render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, test_range); | |
59 render_text_->ApplyStyle(gfx::UNDERLINE, true, test_range); | |
60 InvalidateLayout(); | |
40 } | 61 } |
41 | 62 |
42 private: | 63 private: |
43 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { | 64 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { |
44 gfx::Rect bounds = GetLocalBounds(); | 65 gfx::Rect bounds = GetLocalBounds(); |
45 bounds.Inset(GetInsets()); | 66 bounds.Inset(GetInsets()); |
46 render_text_->SetDisplayRect(bounds); | 67 render_text_->SetDisplayRect(bounds); |
47 } | 68 } |
48 | 69 |
49 scoped_ptr<gfx::RenderText> render_text_; | 70 scoped_ptr<gfx::RenderText> render_text_; |
50 | 71 |
51 DISALLOW_COPY_AND_ASSIGN(RenderTextView); | 72 DISALLOW_COPY_AND_ASSIGN(RenderTextView); |
52 }; | 73 }; |
53 | 74 |
54 MultilineExample::MultilineExample() | 75 MultilineExample::MultilineExample() |
55 : ExampleBase("Multiline RenderText"), | 76 : ExampleBase("Multiline RenderText"), |
77 example_container_(NULL), | |
56 render_text_view_(NULL), | 78 render_text_view_(NULL), |
57 label_(NULL), | 79 label_(NULL), |
80 label_checkbox_(NULL), | |
58 textfield_(NULL) { | 81 textfield_(NULL) { |
59 } | 82 } |
60 | 83 |
61 MultilineExample::~MultilineExample() { | 84 MultilineExample::~MultilineExample() { |
62 } | 85 } |
63 | 86 |
64 void MultilineExample::CreateExampleView(View* container) { | 87 void MultilineExample::CreateExampleView(View* container) { |
65 const char kTestString[] = "test string asdf 1234 test string asdf 1234 " | 88 const char kTestString[] = "test string asdf 1234 test string asdf 1234 " |
66 "test string asdf 1234 test string asdf 1234"; | 89 "test string asdf 1234 test string asdf 1234"; |
67 | 90 |
68 gfx::RenderText* render_text = gfx::RenderText::CreateInstance(); | 91 example_container_ = container; |
69 render_text->SetText(ASCIIToUTF16(kTestString)); | |
70 render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
71 | 92 |
72 render_text_view_ = new RenderTextView(render_text); | 93 render_text_view_ = new RenderTextView(); |
94 render_text_view_->SetText(ASCIIToUTF16(kTestString)); | |
73 | 95 |
74 label_ = new Label(); | 96 label_ = new Label(); |
75 label_->SetText(ASCIIToUTF16(kTestString)); | 97 label_->SetText(ASCIIToUTF16(kTestString)); |
76 label_->SetMultiLine(true); | 98 label_->SetMultiLine(true); |
77 label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN)); | 99 label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN)); |
78 | 100 |
101 label_checkbox_ = new Checkbox(string16()); | |
102 label_checkbox_->SetChecked(true); | |
103 label_checkbox_->set_listener(this); | |
104 label_checkbox_->set_request_focus_on_press(false); | |
105 | |
79 textfield_ = new Textfield(); | 106 textfield_ = new Textfield(); |
80 textfield_->SetController(this); | 107 textfield_->SetController(this); |
81 textfield_->SetText(ASCIIToUTF16(kTestString)); | 108 textfield_->SetText(ASCIIToUTF16(kTestString)); |
82 | 109 |
83 GridLayout* layout = new GridLayout(container); | 110 GridLayout* layout = new GridLayout(container); |
84 container->SetLayoutManager(layout); | 111 container->SetLayoutManager(layout); |
85 | 112 |
86 ColumnSet* column_set = layout->AddColumnSet(0); | 113 ColumnSet* column_set = layout->AddColumnSet(0); |
87 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, | 114 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, |
115 0.0f, GridLayout::USE_PREF, 0, 0); | |
116 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, | |
msw
2013/09/06 23:47:45
Instead of adding a new column to place the empty-
ckocagil
2013/09/11 14:59:49
Done.
| |
88 0.0f, GridLayout::USE_PREF, 0, 0); | 117 0.0f, GridLayout::USE_PREF, 0, 0); |
89 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, | 118 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, |
90 1.0f, GridLayout::FIXED, 0, 0); | 119 1.0f, GridLayout::FIXED, 0, 0); |
91 | 120 |
92 layout->StartRow(0, 0); | 121 layout->StartRow(0, 0); |
122 layout->SkipColumns(1); | |
93 layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:"))); | 123 layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:"))); |
94 layout->AddView(render_text_view_); | 124 layout->AddView(render_text_view_); |
95 | 125 |
96 layout->StartRow(0, 0); | 126 layout->StartRow(0, 0); |
127 layout->AddView(label_checkbox_); | |
97 layout->AddView(new Label(ASCIIToUTF16("views::Label:"))); | 128 layout->AddView(new Label(ASCIIToUTF16("views::Label:"))); |
98 layout->AddView(label_); | 129 layout->AddView(label_); |
99 | 130 |
100 layout->StartRow(0, 0); | 131 layout->StartRow(0, 0); |
132 layout->SkipColumns(1); | |
101 layout->AddView(new Label(ASCIIToUTF16("Sample Text:"))); | 133 layout->AddView(new Label(ASCIIToUTF16("Sample Text:"))); |
102 layout->AddView(textfield_); | 134 layout->AddView(textfield_); |
103 } | 135 } |
104 | 136 |
105 void MultilineExample::ContentsChanged(Textfield* sender, | 137 void MultilineExample::ContentsChanged(Textfield* sender, |
106 const string16& new_contents) { | 138 const string16& new_contents) { |
107 render_text_view_->SetText(new_contents); | 139 render_text_view_->SetText(new_contents); |
108 label_->SetText(new_contents); | 140 if (label_checkbox_->checked()) |
141 label_->SetText(new_contents); | |
142 example_container_->Layout(); | |
143 example_container_->SchedulePaint(); | |
109 } | 144 } |
110 | 145 |
111 bool MultilineExample::HandleKeyEvent(Textfield* sender, | 146 bool MultilineExample::HandleKeyEvent(Textfield* sender, |
112 const ui::KeyEvent& key_event) { | 147 const ui::KeyEvent& key_event) { |
113 return false; | 148 return false; |
114 } | 149 } |
115 | 150 |
151 void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) { | |
152 DCHECK_EQ(sender, label_checkbox_); | |
153 if (label_checkbox_->checked()) | |
msw
2013/09/06 23:47:45
optional nit: label_->SetText(label_checkbox_->ch
ckocagil
2013/09/11 14:59:49
Done.
| |
154 label_->SetText(textfield_->text()); | |
155 else | |
156 label_->SetText(string16()); | |
157 example_container_->Layout(); | |
158 example_container_->SchedulePaint(); | |
159 } | |
160 | |
116 } // namespace examples | 161 } // namespace examples |
117 } // namespace views | 162 } // namespace views |
OLD | NEW |