Chromium Code Reviews| Index: ui/views/examples/multiline_example.cc |
| diff --git a/ui/views/examples/multiline_example.cc b/ui/views/examples/multiline_example.cc |
| index 04fdde7c96b916cdfae5c174ff0abe07210bb0dd..1f9396d0fc61e0e5fda072cf1899ae7e98da2d6c 100644 |
| --- a/ui/views/examples/multiline_example.cc |
| +++ b/ui/views/examples/multiline_example.cc |
| @@ -7,8 +7,10 @@ |
| #include "base/strings/utf_string_conversions.h" |
| #include "ui/base/events/event.h" |
| #include "ui/gfx/render_text.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/fill_layout.h" |
| #include "ui/views/layout/grid_layout.h" |
| #include "ui/views/view.h" |
| @@ -20,7 +22,9 @@ class MultilineExample::RenderTextView : public View { |
| public: |
| explicit RenderTextView(gfx::RenderText* render_text) |
| : render_text_(render_text) { |
| + render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| render_text_->SetColor(SK_ColorBLACK); |
| + render_text_->set_multiline(true); |
| set_border(Border::CreateSolidBorder(2, SK_ColorGRAY)); |
| } |
| @@ -30,12 +34,31 @@ class MultilineExample::RenderTextView : public View { |
| } |
| virtual gfx::Size GetPreferredSize() OVERRIDE { |
| - return gfx::Size(0, |
| - render_text_->GetFont().GetHeight() + GetInsets().height()); |
| + gfx::Size content(render_text_->GetContentWidth(), |
| + render_text_->GetStringSize().height()); |
| + content.Enlarge(GetInsets().width(), GetInsets().height()); |
| + return content; |
| + } |
| + |
| + virtual int GetHeightForWidth(int w) OVERRIDE { |
| + if (w == 0) // TODO(ckocagil): Why does this happen? |
| + return View::GetHeightForWidth(w); |
| + gfx::Rect rect = render_text_->display_rect(); |
| + rect.set_width(w - GetInsets().width()); |
| + render_text_->SetDisplayRect(rect); |
| + return render_text_->GetMultilineTextSize().height() + GetInsets().height(); |
| } |
| void SetText(const string16& new_contents) { |
| + ui::Range test_range(1, 11); |
|
msw
2013/07/17 06:47:18
nit: const. and use this for lines 59 and 60.
ckocagil
2013/07/19 19:40:50
Done.
|
| render_text_->SetText(new_contents); |
| + render_text_->MoveCursorTo(gfx::SelectionModel(test_range, |
| + gfx::CURSOR_FORWARD)); |
| + render_text_->set_selection_background_focused_color(0xFFFF0000); |
| + render_text_->set_focused(true); |
| + render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, ui::Range(1, 11)); |
| + render_text_->ApplyStyle(gfx::UNDERLINE, true, ui::Range(1, 11)); |
| + InvalidateLayout(); |
| SchedulePaint(); |
| } |
| @@ -53,8 +76,11 @@ class MultilineExample::RenderTextView : public View { |
| MultilineExample::MultilineExample() |
| : ExampleBase("Multiline RenderText"), |
| + container_(NULL), |
| render_text_view_(NULL), |
| label_(NULL), |
| + label_container_(NULL), |
| + label_checkbox_(NULL), |
| textfield_(NULL) { |
| } |
| @@ -65,17 +91,27 @@ void MultilineExample::CreateExampleView(View* container) { |
| const char kTestString[] = "test string asdf 1234 test string asdf 1234 " |
| "test string asdf 1234 test string asdf 1234"; |
| + container_ = container; |
| + |
| gfx::RenderText* render_text = gfx::RenderText::CreateInstance(); |
| - render_text->SetText(ASCIIToUTF16(kTestString)); |
| - render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| render_text_view_ = new RenderTextView(render_text); |
| + render_text_view_->SetText(ASCIIToUTF16(kTestString)); |
| label_ = new Label(); |
| label_->SetText(ASCIIToUTF16(kTestString)); |
| label_->SetMultiLine(true); |
| label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN)); |
| + label_container_ = new View; |
| + label_container_->SetLayoutManager(new FillLayout); |
| + label_container_->AddChildView(label_); |
| + |
| + label_checkbox_ = new Checkbox(string16()); |
| + label_checkbox_->SetChecked(true); |
| + label_checkbox_->set_listener(this); |
| + label_checkbox_->set_request_focus_on_press(false); |
| + |
| textfield_ = new Textfield(); |
| textfield_->SetController(this); |
| textfield_->SetText(ASCIIToUTF16(kTestString)); |
| @@ -84,20 +120,25 @@ void MultilineExample::CreateExampleView(View* container) { |
| container->SetLayoutManager(layout); |
| ColumnSet* column_set = layout->AddColumnSet(0); |
| - column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, |
| + column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, |
| + 0.0f, GridLayout::USE_PREF, 0, 0); |
| + column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, |
| 0.0f, GridLayout::USE_PREF, 0, 0); |
| column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, |
| 1.0f, GridLayout::FIXED, 0, 0); |
| layout->StartRow(0, 0); |
| + layout->AddView(new View); |
|
msw
2013/07/17 06:47:18
I think you can skip a column instead of adding a
ckocagil
2013/07/19 19:40:50
Done.
|
| layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:"))); |
| layout->AddView(render_text_view_); |
| layout->StartRow(0, 0); |
| + layout->AddView(label_checkbox_); |
| layout->AddView(new Label(ASCIIToUTF16("views::Label:"))); |
| - layout->AddView(label_); |
| + layout->AddView(label_container_); |
| layout->StartRow(0, 0); |
| + layout->AddView(new View); |
| layout->AddView(new Label(ASCIIToUTF16("Sample Text:"))); |
| layout->AddView(textfield_); |
| } |
| @@ -106,6 +147,8 @@ void MultilineExample::ContentsChanged(Textfield* sender, |
| const string16& new_contents) { |
| render_text_view_->SetText(new_contents); |
| label_->SetText(new_contents); |
| + container_->Layout(); |
| + container_->SchedulePaint(); |
| } |
| bool MultilineExample::HandleKeyEvent(Textfield* sender, |
| @@ -113,5 +156,21 @@ bool MultilineExample::HandleKeyEvent(Textfield* sender, |
| return false; |
| } |
| +void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) { |
| + if (sender != label_checkbox_) |
| + return; |
| + if (label_checkbox_->checked()) { |
|
msw
2013/07/17 06:47:18
Could you set the Label text to empty or the textf
ckocagil
2013/07/19 19:40:50
Done.
|
| + label_container_->RemoveAllChildViews(true); |
| + label_container_->AddChildView(label_); |
| + } else { |
| + label_container_->RemoveAllChildViews(false); |
| + label_container_->AddChildView(new View); |
| + } |
| + // TODO(ckocagil): we seem to require laying out twice. why? |
| + container_->Layout(); |
| + container_->Layout(); |
| + container_->SchedulePaint(); |
| +} |
| + |
| } // namespace examples |
| } // namespace views |