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/button/checkbox.h" | |
10 #include "ui/views/controls/label.h" | 11 #include "ui/views/controls/label.h" |
11 #include "ui/views/controls/textfield/textfield.h" | 12 #include "ui/views/controls/textfield/textfield.h" |
13 #include "ui/views/layout/fill_layout.h" | |
12 #include "ui/views/layout/grid_layout.h" | 14 #include "ui/views/layout/grid_layout.h" |
13 #include "ui/views/view.h" | 15 #include "ui/views/view.h" |
14 | 16 |
15 namespace views { | 17 namespace views { |
16 namespace examples { | 18 namespace examples { |
17 | 19 |
18 // A simple View that hosts a RenderText object. | 20 // A simple View that hosts a RenderText object. |
19 class MultilineExample::RenderTextView : public View { | 21 class MultilineExample::RenderTextView : public View { |
20 public: | 22 public: |
21 explicit RenderTextView(gfx::RenderText* render_text) | 23 explicit RenderTextView(gfx::RenderText* render_text) |
22 : render_text_(render_text) { | 24 : render_text_(render_text) { |
25 render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
23 render_text_->SetColor(SK_ColorBLACK); | 26 render_text_->SetColor(SK_ColorBLACK); |
27 render_text_->set_multiline(true); | |
24 set_border(Border::CreateSolidBorder(2, SK_ColorGRAY)); | 28 set_border(Border::CreateSolidBorder(2, SK_ColorGRAY)); |
25 } | 29 } |
26 | 30 |
27 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | 31 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |
28 View::OnPaint(canvas); | 32 View::OnPaint(canvas); |
29 render_text_->Draw(canvas); | 33 render_text_->Draw(canvas); |
30 } | 34 } |
31 | 35 |
32 virtual gfx::Size GetPreferredSize() OVERRIDE { | 36 virtual gfx::Size GetPreferredSize() OVERRIDE { |
33 return gfx::Size(0, | 37 return gfx::Size(render_text_->GetMultilineTextSize().width(), |
msw
2013/07/10 04:01:56
This probably creates an odd feedback loop, since
ckocagil
2013/07/13 16:05:10
Done except for the newline characters: this class
| |
34 render_text_->GetFont().GetHeight() + GetInsets().height()); | 38 render_text_->GetMultilineTextSize().height() + |
39 GetInsets().height()); | |
35 } | 40 } |
36 | 41 |
37 void SetText(const string16& new_contents) { | 42 void SetText(const string16& new_contents) { |
38 render_text_->SetText(new_contents); | 43 render_text_->SetText(new_contents); |
44 render_text_->MoveCursorTo(gfx::SelectionModel(ui::Range(1, 11), | |
Alexei Svitkine (slow)
2013/07/09 15:35:13
Nit: Extract the range to a local variable.
ckocagil
2013/07/13 16:05:10
Done.
| |
45 gfx::CURSOR_FORWARD)); | |
46 render_text_->set_selection_background_focused_color(0xFFFF0000); | |
47 render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, ui::Range(1, 11)); | |
48 render_text_->ApplyStyle(gfx::UNDERLINE, true, ui::Range(1, 11)); | |
49 InvalidateLayout(); | |
39 SchedulePaint(); | 50 SchedulePaint(); |
40 } | 51 } |
41 | 52 |
42 private: | 53 private: |
43 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { | 54 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { |
44 gfx::Rect bounds = GetLocalBounds(); | 55 gfx::Rect bounds = GetLocalBounds(); |
45 bounds.Inset(GetInsets()); | 56 bounds.Inset(GetInsets()); |
46 render_text_->SetDisplayRect(bounds); | 57 render_text_->SetDisplayRect(bounds); |
58 InvalidateLayout(); | |
47 } | 59 } |
48 | 60 |
49 scoped_ptr<gfx::RenderText> render_text_; | 61 scoped_ptr<gfx::RenderText> render_text_; |
50 | 62 |
51 DISALLOW_COPY_AND_ASSIGN(RenderTextView); | 63 DISALLOW_COPY_AND_ASSIGN(RenderTextView); |
52 }; | 64 }; |
53 | 65 |
54 MultilineExample::MultilineExample() | 66 MultilineExample::MultilineExample() |
55 : ExampleBase("Multiline RenderText"), | 67 : ExampleBase("Multiline RenderText"), |
68 container_(NULL), | |
56 render_text_view_(NULL), | 69 render_text_view_(NULL), |
57 label_(NULL), | 70 label_(NULL), |
71 label_container_(NULL), | |
72 label_checkbox_(NULL), | |
58 textfield_(NULL) { | 73 textfield_(NULL) { |
59 } | 74 } |
60 | 75 |
61 MultilineExample::~MultilineExample() { | 76 MultilineExample::~MultilineExample() { |
62 } | 77 } |
63 | 78 |
64 void MultilineExample::CreateExampleView(View* container) { | 79 void MultilineExample::CreateExampleView(View* container) { |
65 const char kTestString[] = "test string asdf 1234 test string asdf 1234 " | 80 const char kTestString[] = "test string asdf 1234 test string asdf 1234 " |
66 "test string asdf 1234 test string asdf 1234"; | 81 "test string asdf 1234 test string asdf 1234"; |
67 | 82 |
83 container_ = container; | |
84 | |
68 gfx::RenderText* render_text = gfx::RenderText::CreateInstance(); | 85 gfx::RenderText* render_text = gfx::RenderText::CreateInstance(); |
69 render_text->SetText(ASCIIToUTF16(kTestString)); | |
70 render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
71 | 86 |
72 render_text_view_ = new RenderTextView(render_text); | 87 render_text_view_ = new RenderTextView(render_text); |
88 render_text_view_->SetText(ASCIIToUTF16(kTestString)); | |
73 | 89 |
74 label_ = new Label(); | 90 label_ = new Label(); |
75 label_->SetText(ASCIIToUTF16(kTestString)); | 91 label_->SetText(ASCIIToUTF16(kTestString)); |
76 label_->SetMultiLine(true); | 92 label_->SetMultiLine(true); |
77 label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN)); | 93 label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN)); |
78 | 94 |
95 label_container_ = new View; | |
96 label_container_->SetLayoutManager(new FillLayout); | |
97 label_container_->AddChildView(label_); | |
98 | |
99 label_checkbox_ = new Checkbox(string16()); | |
100 label_checkbox_->SetChecked(true); | |
101 label_checkbox_->set_listener(this); | |
102 label_checkbox_->set_request_focus_on_press(false); | |
103 | |
79 textfield_ = new Textfield(); | 104 textfield_ = new Textfield(); |
80 textfield_->SetController(this); | 105 textfield_->SetController(this); |
81 textfield_->SetText(ASCIIToUTF16(kTestString)); | 106 textfield_->SetText(ASCIIToUTF16(kTestString)); |
82 | 107 |
83 GridLayout* layout = new GridLayout(container); | 108 GridLayout* layout = new GridLayout(container); |
84 container->SetLayoutManager(layout); | 109 container->SetLayoutManager(layout); |
85 | 110 |
86 ColumnSet* column_set = layout->AddColumnSet(0); | 111 ColumnSet* column_set = layout->AddColumnSet(0); |
87 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, | 112 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, |
88 0.0f, GridLayout::USE_PREF, 0, 0); | 113 0.0f, GridLayout::USE_PREF, 0, 0); |
114 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, | |
115 0.0f, GridLayout::USE_PREF, 0, 0); | |
89 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, | 116 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, |
90 1.0f, GridLayout::FIXED, 0, 0); | 117 1.0f, GridLayout::FIXED, 0, 0); |
91 | 118 |
92 layout->StartRow(0, 0); | 119 layout->StartRow(0, 0); |
120 layout->AddView(new View); | |
93 layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:"))); | 121 layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:"))); |
94 layout->AddView(render_text_view_); | 122 layout->AddView(render_text_view_); |
95 | 123 |
96 layout->StartRow(0, 0); | 124 layout->StartRow(0, 0); |
125 layout->AddView(label_checkbox_); | |
97 layout->AddView(new Label(ASCIIToUTF16("views::Label:"))); | 126 layout->AddView(new Label(ASCIIToUTF16("views::Label:"))); |
98 layout->AddView(label_); | 127 layout->AddView(label_container_); |
99 | 128 |
100 layout->StartRow(0, 0); | 129 layout->StartRow(0, 0); |
130 layout->AddView(new View); | |
101 layout->AddView(new Label(ASCIIToUTF16("Sample Text:"))); | 131 layout->AddView(new Label(ASCIIToUTF16("Sample Text:"))); |
102 layout->AddView(textfield_); | 132 layout->AddView(textfield_); |
103 } | 133 } |
104 | 134 |
105 void MultilineExample::ContentsChanged(Textfield* sender, | 135 void MultilineExample::ContentsChanged(Textfield* sender, |
106 const string16& new_contents) { | 136 const string16& new_contents) { |
107 render_text_view_->SetText(new_contents); | 137 render_text_view_->SetText(new_contents); |
108 label_->SetText(new_contents); | 138 label_->SetText(new_contents); |
139 container_->Layout(); | |
140 container_->SchedulePaint(); | |
109 } | 141 } |
110 | 142 |
111 bool MultilineExample::HandleKeyEvent(Textfield* sender, | 143 bool MultilineExample::HandleKeyEvent(Textfield* sender, |
112 const ui::KeyEvent& key_event) { | 144 const ui::KeyEvent& key_event) { |
113 return false; | 145 return false; |
114 } | 146 } |
115 | 147 |
148 void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) { | |
149 if (sender != label_checkbox_) | |
150 return; | |
151 if (label_checkbox_->checked()) { | |
152 label_container_->RemoveAllChildViews(true); | |
153 label_container_->AddChildView(label_); | |
154 } else { | |
155 label_container_->RemoveAllChildViews(false); | |
156 label_container_->AddChildView(new View); | |
157 } | |
158 container_->Layout(); // TODO: we seem to require laying out twice. why? | |
Alexei Svitkine (slow)
2013/07/09 15:35:13
TODO -> TODO(ckocagil)
Also, consider filing a bu
ckocagil
2013/07/13 16:05:10
Done. I don't think I will forget this, resolving
| |
159 container_->Layout(); | |
160 container_->SchedulePaint(); | |
161 } | |
162 | |
116 } // namespace examples | 163 } // namespace examples |
117 } // namespace views | 164 } // namespace views |
OLD | NEW |