| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/text_example.h" | 5 #include "ui/views/examples/text_example.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 // TextExample's content view, which draws stylized string. | 64 // TextExample's content view, which draws stylized string. |
| 65 class TextExample::TextExampleView : public View { | 65 class TextExample::TextExampleView : public View { |
| 66 public: | 66 public: |
| 67 TextExampleView() | 67 TextExampleView() |
| 68 : text_(base::ASCIIToUTF16(kShortText)), | 68 : text_(base::ASCIIToUTF16(kShortText)), |
| 69 flags_(0), | 69 flags_(0), |
| 70 halo_(false), | |
| 71 elide_(gfx::NO_ELIDE) { | 70 elide_(gfx::NO_ELIDE) { |
| 72 } | 71 } |
| 73 | 72 |
| 74 void OnPaint(gfx::Canvas* canvas) override { | 73 void OnPaint(gfx::Canvas* canvas) override { |
| 75 View::OnPaint(canvas); | 74 View::OnPaint(canvas); |
| 76 const gfx::Rect bounds = GetContentsBounds(); | 75 const gfx::Rect bounds = GetContentsBounds(); |
| 77 const SkColor color = SK_ColorDKGRAY; | 76 const SkColor color = SK_ColorDKGRAY; |
| 78 if (elide_ == gfx::FADE_TAIL) { | 77 if (elide_ == gfx::FADE_TAIL) { |
| 79 canvas->DrawFadedString(text_, font_list_, color, bounds, flags_); | 78 canvas->DrawFadedString(text_, font_list_, color, bounds, flags_); |
| 80 } else if (halo_) { | |
| 81 canvas->DrawStringRectWithHalo(text_, font_list_, color, SK_ColorYELLOW, | |
| 82 bounds, flags_); | |
| 83 } else { | 79 } else { |
| 84 canvas->DrawStringRectWithFlags(text_, font_list_, color, bounds, flags_); | 80 canvas->DrawStringRectWithFlags(text_, font_list_, color, bounds, flags_); |
| 85 } | 81 } |
| 86 } | 82 } |
| 87 | 83 |
| 88 int flags() const { return flags_; } | 84 int flags() const { return flags_; } |
| 89 void set_flags(int flags) { flags_ = flags; } | 85 void set_flags(int flags) { flags_ = flags; } |
| 90 void set_text(const base::string16& text) { text_ = text; } | 86 void set_text(const base::string16& text) { text_ = text; } |
| 91 void set_halo(bool halo) { halo_ = halo; } | |
| 92 void set_elide(gfx::ElideBehavior elide) { elide_ = elide; } | 87 void set_elide(gfx::ElideBehavior elide) { elide_ = elide; } |
| 93 | 88 |
| 94 int GetStyle() const { return font_list_.GetFontStyle(); } | 89 int GetStyle() const { return font_list_.GetFontStyle(); } |
| 95 void SetStyle(int style) { font_list_ = font_list_.DeriveWithStyle(style); } | 90 void SetStyle(int style) { font_list_ = font_list_.DeriveWithStyle(style); } |
| 96 | 91 |
| 97 gfx::Font::Weight GetWeight() const { return font_list_.GetFontWeight(); } | 92 gfx::Font::Weight GetWeight() const { return font_list_.GetFontWeight(); } |
| 98 void SetWeight(gfx::Font::Weight weight) { | 93 void SetWeight(gfx::Font::Weight weight) { |
| 99 font_list_ = font_list_.DeriveWithWeight(weight); | 94 font_list_ = font_list_.DeriveWithWeight(weight); |
| 100 } | 95 } |
| 101 | 96 |
| 102 private: | 97 private: |
| 103 // The font used for drawing the text. | 98 // The font used for drawing the text. |
| 104 gfx::FontList font_list_; | 99 gfx::FontList font_list_; |
| 105 | 100 |
| 106 // The text to draw. | 101 // The text to draw. |
| 107 base::string16 text_; | 102 base::string16 text_; |
| 108 | 103 |
| 109 // Text flags for passing to |DrawStringRect()|. | 104 // Text flags for passing to |DrawStringRect()|. |
| 110 int flags_; | 105 int flags_; |
| 111 | 106 |
| 112 // A flag to draw a halo around the text. | |
| 113 bool halo_; | |
| 114 | |
| 115 // The eliding, fading, or truncating behavior. | 107 // The eliding, fading, or truncating behavior. |
| 116 gfx::ElideBehavior elide_; | 108 gfx::ElideBehavior elide_; |
| 117 | 109 |
| 118 DISALLOW_COPY_AND_ASSIGN(TextExampleView); | 110 DISALLOW_COPY_AND_ASSIGN(TextExampleView); |
| 119 }; | 111 }; |
| 120 | 112 |
| 121 TextExample::TextExample() : ExampleBase("Text Styles") {} | 113 TextExample::TextExample() : ExampleBase("Text Styles") {} |
| 122 | 114 |
| 123 TextExample::~TextExample() { | 115 TextExample::~TextExample() { |
| 124 // Remove the views first as some reference combobox models. | 116 // Remove the views first as some reference combobox models. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 eliding_cb_ = AddCombobox(layout, "Eliding", kElideBehaviors, | 160 eliding_cb_ = AddCombobox(layout, "Eliding", kElideBehaviors, |
| 169 arraysize(kElideBehaviors)); | 161 arraysize(kElideBehaviors)); |
| 170 prefix_cb_ = AddCombobox(layout, "Prefix", kPrefixOptions, | 162 prefix_cb_ = AddCombobox(layout, "Prefix", kPrefixOptions, |
| 171 arraysize(kPrefixOptions)); | 163 arraysize(kPrefixOptions)); |
| 172 text_cb_ = AddCombobox(layout, "Example Text", kTextExamples, | 164 text_cb_ = AddCombobox(layout, "Example Text", kTextExamples, |
| 173 arraysize(kTextExamples)); | 165 arraysize(kTextExamples)); |
| 174 | 166 |
| 175 layout->StartRow(0, 0); | 167 layout->StartRow(0, 0); |
| 176 multiline_checkbox_ = AddCheckbox(layout, "Multiline"); | 168 multiline_checkbox_ = AddCheckbox(layout, "Multiline"); |
| 177 break_checkbox_ = AddCheckbox(layout, "Character Break"); | 169 break_checkbox_ = AddCheckbox(layout, "Character Break"); |
| 178 halo_checkbox_ = AddCheckbox(layout, "Halo"); | |
| 179 bold_checkbox_ = AddCheckbox(layout, "Bold"); | 170 bold_checkbox_ = AddCheckbox(layout, "Bold"); |
| 180 italic_checkbox_ = AddCheckbox(layout, "Italic"); | 171 italic_checkbox_ = AddCheckbox(layout, "Italic"); |
| 181 underline_checkbox_ = AddCheckbox(layout, "Underline"); | 172 underline_checkbox_ = AddCheckbox(layout, "Underline"); |
| 182 | 173 |
| 183 layout->AddPaddingRow(0, 20); | 174 layout->AddPaddingRow(0, 20); |
| 184 column_set = layout->AddColumnSet(1); | 175 column_set = layout->AddColumnSet(1); |
| 185 column_set->AddPaddingColumn(0, 16); | 176 column_set->AddPaddingColumn(0, 16); |
| 186 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, | 177 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, |
| 187 1, GridLayout::USE_PREF, 0, 0); | 178 1, GridLayout::USE_PREF, 0, 0); |
| 188 column_set->AddPaddingColumn(0, 16); | 179 column_set->AddPaddingColumn(0, 16); |
| 189 layout->StartRow(1, 1); | 180 layout->StartRow(1, 1); |
| 190 layout->AddView(text_view_); | 181 layout->AddView(text_view_); |
| 191 layout->AddPaddingRow(0, 8); | 182 layout->AddPaddingRow(0, 8); |
| 192 } | 183 } |
| 193 | 184 |
| 194 void TextExample::ButtonPressed(Button* button, const ui::Event& event) { | 185 void TextExample::ButtonPressed(Button* button, const ui::Event& event) { |
| 195 int flags = text_view_->flags(); | 186 int flags = text_view_->flags(); |
| 196 int style = text_view_->GetStyle(); | 187 int style = text_view_->GetStyle(); |
| 197 SetFlagFromCheckbox(multiline_checkbox_, &flags, gfx::Canvas::MULTI_LINE); | 188 SetFlagFromCheckbox(multiline_checkbox_, &flags, gfx::Canvas::MULTI_LINE); |
| 198 SetFlagFromCheckbox(break_checkbox_, &flags, gfx::Canvas::CHARACTER_BREAK); | 189 SetFlagFromCheckbox(break_checkbox_, &flags, gfx::Canvas::CHARACTER_BREAK); |
| 199 SetFlagFromCheckbox(italic_checkbox_, &style, gfx::Font::ITALIC); | 190 SetFlagFromCheckbox(italic_checkbox_, &style, gfx::Font::ITALIC); |
| 200 SetFlagFromCheckbox(underline_checkbox_, &style, gfx::Font::UNDERLINE); | 191 SetFlagFromCheckbox(underline_checkbox_, &style, gfx::Font::UNDERLINE); |
| 201 text_view_->set_halo(halo_checkbox_->checked()); | |
| 202 text_view_->set_flags(flags); | 192 text_view_->set_flags(flags); |
| 203 text_view_->SetStyle(style); | 193 text_view_->SetStyle(style); |
| 204 text_view_->SetWeight(bold_checkbox_->checked() ? gfx::Font::Weight::BOLD | 194 text_view_->SetWeight(bold_checkbox_->checked() ? gfx::Font::Weight::BOLD |
| 205 : gfx::Font::Weight::NORMAL); | 195 : gfx::Font::Weight::NORMAL); |
| 206 text_view_->SchedulePaint(); | 196 text_view_->SchedulePaint(); |
| 207 } | 197 } |
| 208 | 198 |
| 209 void TextExample::OnPerformAction(Combobox* combobox) { | 199 void TextExample::OnPerformAction(Combobox* combobox) { |
| 210 int flags = text_view_->flags(); | 200 int flags = text_view_->flags(); |
| 211 if (combobox == h_align_cb_) { | 201 if (combobox == h_align_cb_) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 flags |= gfx::Canvas::HIDE_PREFIX; | 254 flags |= gfx::Canvas::HIDE_PREFIX; |
| 265 break; | 255 break; |
| 266 } | 256 } |
| 267 } | 257 } |
| 268 text_view_->set_flags(flags); | 258 text_view_->set_flags(flags); |
| 269 text_view_->SchedulePaint(); | 259 text_view_->SchedulePaint(); |
| 270 } | 260 } |
| 271 | 261 |
| 272 } // namespace examples | 262 } // namespace examples |
| 273 } // namespace views | 263 } // namespace views |
| OLD | NEW |