// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "ui/views/examples/text_example.h" #include #include #include "base/stl_util.h" #include "base/strings/utf_string_conversions.h" #include "ui/gfx/canvas.h" #include "ui/gfx/font_list.h" #include "ui/native_theme/native_theme.h" #include "ui/views/border.h" #include "ui/views/controls/button/checkbox.h" #include "ui/views/controls/combobox/combobox.h" #include "ui/views/controls/label.h" #include "ui/views/examples/example_combobox_model.h" #include "ui/views/layout/grid_layout.h" #include "ui/views/view.h" namespace views { namespace examples { namespace { // Number of columns in the view layout. constexpr int kNumColumns = 10; // Toggles bit |flag| on |flags| based on state of |checkbox|. void SetFlagFromCheckbox(Checkbox* checkbox, int* flags, int flag) { if (checkbox->GetChecked()) *flags |= flag; else *flags &= ~flag; } } // namespace // TextExample's content view, which draws stylized string. class TextExample::TextExampleView : public View { public: TextExampleView() = default; TextExampleView(const TextExampleView&) = delete; TextExampleView& operator=(const TextExampleView&) = delete; ~TextExampleView() override = default; void OnPaint(gfx::Canvas* canvas) override { View::OnPaint(canvas); const gfx::Rect bounds = GetContentsBounds(); const SkColor color = GetNativeTheme()->GetSystemColor( ui::NativeTheme::kColorId_TextfieldDefaultColor); canvas->DrawStringRectWithFlags(text_, font_list_, color, bounds, flags_); } int GetFlags() const { return flags_; } void SetFlags(int flags) { flags_ = flags; SchedulePaint(); } void SetText(const base::string16& text) { text_ = text; SchedulePaint(); } void SetElide(gfx::ElideBehavior elide) { elide_ = elide; SchedulePaint(); } int GetStyle() const { return font_list_.GetFontStyle(); } void SetStyle(int style) { base_font_ = base_font_.DeriveWithStyle(style); font_list_ = font_list_.DeriveWithStyle(style); SchedulePaint(); } gfx::Font::Weight GetWeight() const { return font_list_.GetFontWeight(); } void SetWeight(gfx::Font::Weight weight) { font_list_ = base_font_.DeriveWithWeight(weight); SchedulePaint(); } private: // The font used for drawing the text. gfx::FontList font_list_; // The font without any bold attributes. Mac font APIs struggle to derive UI // fonts from a base font that isn't NORMAL or BOLD. gfx::FontList base_font_; // The text to draw. base::string16 text_; // Text flags for passing to |DrawStringRect()|. int flags_ = 0; // The eliding, fading, or truncating behavior. gfx::ElideBehavior elide_ = gfx::NO_ELIDE; }; TextExample::TextExample() : ExampleBase("Text Styles") {} TextExample::~TextExample() = default; Checkbox* TextExample::AddCheckbox(GridLayout* layout, const char* name) { return layout->AddView(std::make_unique( base::ASCIIToUTF16(name), base::BindRepeating(&TextExample::UpdateStyle, base::Unretained(this)))); } Combobox* TextExample::AddCombobox(GridLayout* layout, const char* name, const char* const* strings, int count, void (TextExample::*combobox_callback)()) { layout->StartRow(0, 0); layout->AddView(std::make_unique