Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(983)

Side by Side Diff: ui/views/examples/multiline_example.cc

Issue 16867016: Windows implementation of multiline RenderText (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: min height/baseline; update tests Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/examples/multiline_example.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Turn off multiline mode to get the single-line text size, which is the
34 render_text_->font_list().GetHeight() + GetInsets().height()); 35 // preferred size for this view.
36 render_text_->SetMultiline(false);
37 gfx::Size size(render_text_->GetContentWidth(),
38 render_text_->GetStringSize().height());
39 size.Enlarge(GetInsets().width(), GetInsets().height());
40 render_text_->SetMultiline(true);
41 return size;
42 }
43
44 virtual int GetHeightForWidth(int w) OVERRIDE {
45 // TODO(ckocagil): Why does this happen?
46 if (w == 0)
47 return View::GetHeightForWidth(w);
48 gfx::Rect rect = render_text_->display_rect();
49 rect.set_width(w - GetInsets().width());
50 render_text_->SetDisplayRect(rect);
51 return render_text_->GetStringSize().height() + GetInsets().height();
35 } 52 }
36 53
37 void SetText(const string16& new_contents) { 54 void SetText(const string16& new_contents) {
55 // Color and style the text inside |test_range| to test colors and styles.
56 gfx::Range test_range(1, 21);
57 test_range.set_start(std::min(test_range.start(), new_contents.length()));
58 test_range.set_end(std::min(test_range.end(), new_contents.length()));
59
38 render_text_->SetText(new_contents); 60 render_text_->SetText(new_contents);
39 SchedulePaint(); 61 render_text_->SetColor(SK_ColorBLACK);
62 render_text_->ApplyColor(0xFFFF0000, test_range);
63 render_text_->SetStyle(gfx::DIAGONAL_STRIKE, false);
64 render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, test_range);
65 render_text_->SetStyle(gfx::UNDERLINE, false);
66 render_text_->ApplyStyle(gfx::UNDERLINE, true, test_range);
67 InvalidateLayout();
40 } 68 }
41 69
42 private: 70 private:
43 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { 71 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE {
44 gfx::Rect bounds = GetLocalBounds(); 72 gfx::Rect bounds = GetLocalBounds();
45 bounds.Inset(GetInsets()); 73 bounds.Inset(GetInsets());
46 render_text_->SetDisplayRect(bounds); 74 render_text_->SetDisplayRect(bounds);
47 } 75 }
48 76
49 scoped_ptr<gfx::RenderText> render_text_; 77 scoped_ptr<gfx::RenderText> render_text_;
50 78
51 DISALLOW_COPY_AND_ASSIGN(RenderTextView); 79 DISALLOW_COPY_AND_ASSIGN(RenderTextView);
52 }; 80 };
53 81
54 MultilineExample::MultilineExample() 82 MultilineExample::MultilineExample()
55 : ExampleBase("Multiline RenderText"), 83 : ExampleBase("Multiline RenderText"),
56 render_text_view_(NULL), 84 render_text_view_(NULL),
57 label_(NULL), 85 label_(NULL),
58 textfield_(NULL) { 86 textfield_(NULL),
87 label_checkbox_(NULL) {
59 } 88 }
60 89
61 MultilineExample::~MultilineExample() { 90 MultilineExample::~MultilineExample() {
62 } 91 }
63 92
64 void MultilineExample::CreateExampleView(View* container) { 93 void MultilineExample::CreateExampleView(View* container) {
65 const char kTestString[] = "test string asdf 1234 test string asdf 1234 " 94 const char kTestString[] = "test string asdf 1234 test string asdf 1234 "
66 "test string asdf 1234 test string asdf 1234"; 95 "test string asdf 1234 test string asdf 1234";
67 96
68 gfx::RenderText* render_text = gfx::RenderText::CreateInstance(); 97 render_text_view_ = new RenderTextView();
69 render_text->SetText(ASCIIToUTF16(kTestString)); 98 render_text_view_->SetText(ASCIIToUTF16(kTestString));
70 render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER);
71
72 render_text_view_ = new RenderTextView(render_text);
73 99
74 label_ = new Label(); 100 label_ = new Label();
75 label_->SetText(ASCIIToUTF16(kTestString)); 101 label_->SetText(ASCIIToUTF16(kTestString));
76 label_->SetMultiLine(true); 102 label_->SetMultiLine(true);
77 label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN)); 103 label_->set_border(Border::CreateSolidBorder(2, SK_ColorCYAN));
78 104
105 label_checkbox_ = new Checkbox(ASCIIToUTF16("views::Label:"));
106 label_checkbox_->SetChecked(true);
107 label_checkbox_->set_listener(this);
108 label_checkbox_->set_request_focus_on_press(false);
109
79 textfield_ = new Textfield(); 110 textfield_ = new Textfield();
80 textfield_->SetController(this); 111 textfield_->SetController(this);
81 textfield_->SetText(ASCIIToUTF16(kTestString)); 112 textfield_->SetText(ASCIIToUTF16(kTestString));
82 113
83 GridLayout* layout = new GridLayout(container); 114 GridLayout* layout = new GridLayout(container);
84 container->SetLayoutManager(layout); 115 container->SetLayoutManager(layout);
85 116
86 ColumnSet* column_set = layout->AddColumnSet(0); 117 ColumnSet* column_set = layout->AddColumnSet(0);
87 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 118 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER,
88 0.0f, GridLayout::USE_PREF, 0, 0); 119 0.0f, GridLayout::USE_PREF, 0, 0);
89 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 120 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
90 1.0f, GridLayout::FIXED, 0, 0); 121 1.0f, GridLayout::FIXED, 0, 0);
91 122
92 layout->StartRow(0, 0); 123 layout->StartRow(0, 0);
93 layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:"))); 124 layout->AddView(new Label(ASCIIToUTF16("gfx::RenderText:")));
94 layout->AddView(render_text_view_); 125 layout->AddView(render_text_view_);
95 126
96 layout->StartRow(0, 0); 127 layout->StartRow(0, 0);
97 layout->AddView(new Label(ASCIIToUTF16("views::Label:"))); 128 layout->AddView(label_checkbox_);
98 layout->AddView(label_); 129 layout->AddView(label_);
99 130
100 layout->StartRow(0, 0); 131 layout->StartRow(0, 0);
101 layout->AddView(new Label(ASCIIToUTF16("Sample Text:"))); 132 layout->AddView(new Label(ASCIIToUTF16("Sample Text:")));
102 layout->AddView(textfield_); 133 layout->AddView(textfield_);
103 } 134 }
104 135
105 void MultilineExample::ContentsChanged(Textfield* sender, 136 void MultilineExample::ContentsChanged(Textfield* sender,
106 const string16& new_contents) { 137 const string16& new_contents) {
107 render_text_view_->SetText(new_contents); 138 render_text_view_->SetText(new_contents);
108 label_->SetText(new_contents); 139 if (label_checkbox_->checked())
140 label_->SetText(new_contents);
141 container()->Layout();
142 container()->SchedulePaint();
109 } 143 }
110 144
111 bool MultilineExample::HandleKeyEvent(Textfield* sender, 145 bool MultilineExample::HandleKeyEvent(Textfield* sender,
112 const ui::KeyEvent& key_event) { 146 const ui::KeyEvent& key_event) {
113 return false; 147 return false;
114 } 148 }
115 149
150 void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) {
151 DCHECK_EQ(sender, label_checkbox_);
152 label_->SetText(label_checkbox_->checked() ? textfield_->text() : string16());
153 container()->Layout();
154 container()->SchedulePaint();
155 }
156
116 } // namespace examples 157 } // namespace examples
117 } // namespace views 158 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/examples/multiline_example.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698