diff options
author | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-05 17:00:01 +0000 |
---|---|---|
committer | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-05 17:00:01 +0000 |
commit | 3fd71b5b1d5e20c58bcf9be023ba021bab29b95b (patch) | |
tree | 0cf62ae5fdcc84f1e8bc8c53d18b564ab0d7a45f /views/examples | |
parent | f4412a888519d241a603e8a4b7bc36db1fda4851 (diff) | |
download | chromium_src-3fd71b5b1d5e20c58bcf9be023ba021bab29b95b.zip chromium_src-3fd71b5b1d5e20c58bcf9be023ba021bab29b95b.tar.gz chromium_src-3fd71b5b1d5e20c58bcf9be023ba021bab29b95b.tar.bz2 |
Add views_example that exercises drawing different text styles.
Also, a little bit of misc housekeeping.
Reland, original CL reverted: http://codereview.chromium.org/8072004/
BUG=none
TEST=manual
Review URL: http://codereview.chromium.org/8137032
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104118 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/examples')
-rw-r--r-- | views/examples/example_combobox_model.cc | 28 | ||||
-rw-r--r-- | views/examples/example_combobox_model.h | 32 | ||||
-rw-r--r-- | views/examples/examples_main.cc | 6 | ||||
-rw-r--r-- | views/examples/native_theme_button_example.cc | 24 | ||||
-rw-r--r-- | views/examples/scroll_view_example.h | 6 | ||||
-rw-r--r-- | views/examples/text_example.cc | 302 | ||||
-rw-r--r-- | views/examples/text_example.h | 71 |
7 files changed, 441 insertions, 28 deletions
diff --git a/views/examples/example_combobox_model.cc b/views/examples/example_combobox_model.cc new file mode 100644 index 0000000..ec55093 --- /dev/null +++ b/views/examples/example_combobox_model.cc @@ -0,0 +1,28 @@ +// Copyright (c) 2011 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 "views/examples/example_combobox_model.h" + +#include "base/stringprintf.h" +#include "base/utf_string_conversions.h" +#include "ui/base/models/combobox_model.h" + +namespace examples { + +ExampleComboboxModel::ExampleComboboxModel(const char** strings, int count) + : strings_(strings), count_(count) { +} + +ExampleComboboxModel::~ExampleComboboxModel() { +} + +int ExampleComboboxModel::GetItemCount() { + return count_; +} + +string16 ExampleComboboxModel::GetItemAt(int index) { + return ASCIIToUTF16(strings_[index]); +} + +} // namespace examples diff --git a/views/examples/example_combobox_model.h b/views/examples/example_combobox_model.h new file mode 100644 index 0000000..281eada --- /dev/null +++ b/views/examples/example_combobox_model.h @@ -0,0 +1,32 @@ +// Copyright (c) 2011 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. + +#ifndef VIEWS_EXAMPLES_EXAMPLE_COMBOBOX_MODEL_H_ +#define VIEWS_EXAMPLES_EXAMPLE_COMBOBOX_MODEL_H_ +#pragma once + +#include "base/compiler_specific.h" +#include "ui/base/models/combobox_model.h" + +namespace examples { + +class ExampleComboboxModel : public ui::ComboboxModel { + public: + ExampleComboboxModel(const char** strings, int count); + virtual ~ExampleComboboxModel(); + + // Overridden from ui::ComboboxModel: + virtual int GetItemCount() OVERRIDE; + virtual string16 GetItemAt(int index) OVERRIDE; + + private: + const char** strings_; + int count_; + + DISALLOW_COPY_AND_ASSIGN(ExampleComboboxModel); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_EXAMPLE_COMBOBOX_MODEL_H_ diff --git a/views/examples/examples_main.cc b/views/examples/examples_main.cc index 3287cbf..8533fdc 100644 --- a/views/examples/examples_main.cc +++ b/views/examples/examples_main.cc @@ -30,6 +30,7 @@ #include "views/examples/single_split_view_example.h" #include "views/examples/tabbed_pane_example.h" #include "views/examples/table2_example.h" +#include "views/examples/text_example.h" #include "views/examples/textfield_example.h" #include "views/examples/throbber_example.h" #include "views/examples/widget_example.h" @@ -86,6 +87,7 @@ void ExamplesMain::Init() { examples_.push_back(new NativeWidgetViewsExample(this)); examples_.push_back(new TextfieldExample(this)); examples_.push_back(new ButtonExample(this)); + examples_.push_back(new TextExample(this)); examples_.push_back(new BubbleExample(this)); examples_.push_back(new ThrobberExample(this)); examples_.push_back(new ComboboxExample(this)); @@ -103,8 +105,8 @@ void ExamplesMain::Init() { examples_.push_back(new WidgetExample(this)); examples_.push_back(new MenuExample(this)); - for(std::vector<ExampleBase*>::const_iterator i(examples_.begin()); - i != examples_.end(); ++i) + for (std::vector<ExampleBase*>::const_iterator i(examples_.begin()); + i != examples_.end(); ++i) AddExample(*i); window->Show(); diff --git a/views/examples/native_theme_button_example.cc b/views/examples/native_theme_button_example.cc index d907893..15c8385 100644 --- a/views/examples/native_theme_button_example.cc +++ b/views/examples/native_theme_button_example.cc @@ -10,35 +10,13 @@ #include "ui/base/animation/throb_animation.h" #include "ui/base/models/combobox_model.h" #include "ui/gfx/canvas.h" +#include "views/examples/example_combobox_model.h" #include "views/controls/label.h" #include "views/layout/grid_layout.h" #include "views/native_theme_painter.h" namespace { -class ExampleComboboxModel : public ui::ComboboxModel { - public: - ExampleComboboxModel(const char** strings, int count) - : strings_(strings), count_(count) { - } - - virtual ~ExampleComboboxModel() {} - - // Overridden from ui::ComboboxModel: - virtual int GetItemCount() OVERRIDE { - return count_; - } - virtual string16 GetItemAt(int index) OVERRIDE { - return ASCIIToUTF16(strings_[index]); - } - - private: - const char** strings_; - int count_; - - DISALLOW_COPY_AND_ASSIGN(ExampleComboboxModel); -}; - const char* kParts[] = { "PushButton", "RadioButton", diff --git a/views/examples/scroll_view_example.h b/views/examples/scroll_view_example.h index 7441b13..41f41b6 100644 --- a/views/examples/scroll_view_example.h +++ b/views/examples/scroll_view_example.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef VIEWS_EXAMPLES_SCROLL_BAR_EXAMPLE_H_ -#define VIEWS_EXAMPLES_SCROLL_BAR_EXAMPLE_H_ +#ifndef VIEWS_EXAMPLES_SCROLL_VIEW_EXAMPLE_H_ +#define VIEWS_EXAMPLES_SCROLL_VIEW_EXAMPLE_H_ #pragma once #include <string> @@ -50,4 +50,4 @@ class ScrollViewExample : public ExampleBase, } // namespace examples -#endif // VIEWS_EXAMPLES_SCROLL_BAR_EXAMPLE_H_ +#endif // VIEWS_EXAMPLES_SCROLL_VIEW_EXAMPLE_H_ diff --git a/views/examples/text_example.cc b/views/examples/text_example.cc new file mode 100644 index 0000000..1a6db93 --- /dev/null +++ b/views/examples/text_example.cc @@ -0,0 +1,302 @@ +// Copyright (c) 2011 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 "views/examples/text_example.h" + +#include "base/stringprintf.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/gfx/canvas.h" +#include "ui/gfx/canvas_skia.h" +#include "views/controls/label.h" +#include "views/examples/example_combobox_model.h" +#include "views/view.h" + +namespace { + +const char* kShortText = "Batman"; +const char* kMediumText = "The quick brown fox jumps over the lazy dog."; +const char* kLongText = + "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " + "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " + "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " + "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " + "mollit anim id est laborum."; + +const char* kTextExamples[] = { + "Short", + "Medium", + "Long", +}; + +const char* kElidingBehaviors[] = { + "Ellipsis", + "None", +#if defined(OS_WIN) + "Fade Tail", + "Fade Head", + "Fade Head and Tail", +#endif +}; + +const char* kHorizontalAligments[] = { + "Default", + "Left", + "Center", + "Right", +}; + +const char* kVerticalAlignments[] = { + "Default", + "Top", + "Middle", + "Bottom", +}; + +} // namespace + +namespace examples { + +// TextExample's content view, which is responsible for drawing a string with +// the specified style. +class TextExample::TextExampleView : public views::View { + public: + TextExampleView() + : font_(ResourceBundle::GetSharedInstance().GetFont( + ResourceBundle::BaseFont)), + text_(ASCIIToUTF16(kShortText)), + text_flags_(0), + fade_(false), + fade_mode_(gfx::CanvasSkia::TruncateFadeTail) { + } + + virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { +#if defined(OS_WIN) + if (fade_) { + gfx::Rect rect(0, 0, width(), height()); + size_t characters_to_truncate_from_head = + gfx::CanvasSkia::TruncateFadeHeadAndTail ? 10 : 0; + canvas->AsCanvasSkia()->DrawFadeTruncatingString(text_, fade_mode_, + characters_to_truncate_from_head, font_, SK_ColorDKGRAY, rect); + return; + } +#endif + + canvas->DrawStringInt(text_, font_, SK_ColorDKGRAY, + 0, 0, width(), height(), text_flags_); + } + + int text_flags() const { return text_flags_; } + void set_text_flags(int text_flags) { text_flags_ = text_flags; } + + const string16& text() const { return text_; } + void set_text(const string16& text) { text_ = text; } + + bool fade() const { return fade_; } + void set_fade(bool fade) { fade_ = fade; } + + gfx::CanvasSkia::TruncateFadeMode fade_mode() const { return fade_mode_; } + void set_fade_mode(gfx::CanvasSkia::TruncateFadeMode fade_mode) { + fade_mode_ = fade_mode; + } + + private: + // The font used for drawing the text. + gfx::Font font_; + + // The text to draw. + string16 text_; + + // Text flags for passing to |DrawStringInt()|. + int text_flags_; + + // If |true|, specifies to call |DrawFadeTruncatingString()| instead of + // |DrawStringInt()|. + bool fade_; + + // If |fade_| is |true|, fade mode parameter to |DrawFadeTruncatingString()|. + gfx::CanvasSkia::TruncateFadeMode fade_mode_; + + DISALLOW_COPY_AND_ASSIGN(TextExampleView); +}; + + +TextExample::TextExample(ExamplesMain* main) + : ExampleBase(main, "Text Styles") { +} + +TextExample::~TextExample() { +} + +views::Combobox* TextExample::AddCombobox(views::GridLayout* layout, + const char *name, + const char** strings, + int count) { + layout->StartRow(0, 0); + layout->AddView(new views::Label(ASCIIToWide(name))); + views::Combobox* combo_box = + new views::Combobox(new ExampleComboboxModel(strings, count)); + combo_box->SetSelectedItem(0); + combo_box->set_listener(this); + layout->AddView(combo_box); + return combo_box; +} + +void TextExample::CreateExampleView(views::View* container) { + text_view_ = new TextExampleView; + + views::GridLayout* layout = new views::GridLayout(container); + container->SetLayoutManager(layout); + + layout->AddPaddingRow(0, 8); + + views::ColumnSet* column_set = layout->AddColumnSet(0); + column_set->AddPaddingColumn(0, 8); + column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, + 0.1f, views::GridLayout::USE_PREF, 0, 0); + column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, + 0.9f, views::GridLayout::USE_PREF, 0, 0); + column_set->AddPaddingColumn(0, 8); + + h_align_cb_ = AddCombobox(layout, + "H-Align", + kHorizontalAligments, + arraysize(kHorizontalAligments)); + v_align_cb_ = AddCombobox(layout, + "V-Align", + kVerticalAlignments, + arraysize(kVerticalAlignments)); + eliding_cb_ = AddCombobox(layout, + "Eliding", + kElidingBehaviors, + arraysize(kElidingBehaviors)); + text_cb_ = AddCombobox(layout, + "Example Text", + kTextExamples, + arraysize(kTextExamples)); + + layout->StartRow(0, 0); + multiline_checkbox_ = new views::Checkbox(L"Multiline"); + multiline_checkbox_->set_listener(this); + layout->AddView(multiline_checkbox_); + break_checkbox_ = new views::Checkbox(L"Character Break"); + break_checkbox_->set_listener(this); + layout->AddView(break_checkbox_); + + layout->AddPaddingRow(0, 32); + + column_set = layout->AddColumnSet(1); + column_set->AddPaddingColumn(0, 16); + column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, + 1, views::GridLayout::USE_PREF, 0, 0); + column_set->AddPaddingColumn(0, 16); + layout->StartRow(1, 1); + layout->AddView(text_view_); + + layout->AddPaddingRow(0, 8); +} + + +void TextExample::ItemChanged(views::Combobox* combo_box, + int prev_index, + int new_index) { + int text_flags = text_view_->text_flags(); + if (combo_box == h_align_cb_) { + text_flags &= ~(gfx::Canvas::TEXT_ALIGN_LEFT | + gfx::Canvas::TEXT_ALIGN_CENTER | + gfx::Canvas::TEXT_ALIGN_RIGHT); + switch (new_index) { + case 0: + break; + case 1: + text_flags |= gfx::Canvas::TEXT_ALIGN_LEFT; + break; + case 2: + text_flags |= gfx::Canvas::TEXT_ALIGN_CENTER; + break; + case 3: + text_flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; + break; + } + } else if (combo_box == v_align_cb_) { + text_flags &= ~(gfx::Canvas::TEXT_VALIGN_TOP | + gfx::Canvas::TEXT_VALIGN_MIDDLE | + gfx::Canvas::TEXT_VALIGN_BOTTOM); + switch (new_index) { + case 0: + break; + case 1: + text_flags |= gfx::Canvas::TEXT_VALIGN_TOP; + break; + case 2: + text_flags |= gfx::Canvas::TEXT_VALIGN_MIDDLE; + break; + case 3: + text_flags |= gfx::Canvas::TEXT_VALIGN_BOTTOM; + break; + } + } else if (combo_box == text_cb_) { + switch (new_index) { + case 0: + text_view_->set_text(ASCIIToUTF16(kShortText)); + break; + case 1: + text_view_->set_text(ASCIIToUTF16(kMediumText)); + break; + case 2: + text_view_->set_text(ASCIIToUTF16(kLongText)); + break; + } + } else if (combo_box == eliding_cb_) { + switch (new_index) { + case 0: + text_flags &= ~gfx::Canvas::NO_ELLIPSIS; + text_view_->set_fade(false); + break; + case 1: + text_flags |= gfx::Canvas::NO_ELLIPSIS; + text_view_->set_fade(false); + break; +#if defined(OS_WIN) + case 2: + text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeTail); + text_view_->set_fade(true); + break; + case 3: + text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeHead); + text_view_->set_fade(true); + break; + case 4: + text_view_->set_fade_mode(gfx::CanvasSkia::TruncateFadeHeadAndTail); + text_view_->set_fade(true); + break; +#endif + } + } + text_view_->set_text_flags(text_flags); + text_view_->SchedulePaint(); +} + + +void TextExample::ButtonPressed(views::Button* button, + const views::Event& event) { + int text_flags = text_view_->text_flags(); + if (button == multiline_checkbox_) { + if (multiline_checkbox_->checked()) + text_flags |= gfx::Canvas::MULTI_LINE; + else + text_flags &= ~gfx::Canvas::MULTI_LINE; + } else if (button == break_checkbox_) { + if (break_checkbox_->checked()) + text_flags |= gfx::Canvas::CHARACTER_BREAK; + else + text_flags &= ~gfx::Canvas::CHARACTER_BREAK; + } + text_view_->set_text_flags(text_flags); + text_view_->SchedulePaint(); +} + + +} // namespace examples diff --git a/views/examples/text_example.h b/views/examples/text_example.h new file mode 100644 index 0000000..da088ad --- /dev/null +++ b/views/examples/text_example.h @@ -0,0 +1,71 @@ +// Copyright (c) 2011 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. + +#ifndef VIEWS_EXAMPLES_TEXT_EXAMPLE_H_ +#define VIEWS_EXAMPLES_TEXT_EXAMPLE_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "views/controls/button/checkbox.h" +#include "views/controls/combobox/combobox.h" +#include "views/examples/example_base.h" +#include "views/layout/grid_layout.h" + +namespace examples { + +class TextExample : public ExampleBase, + public views::ButtonListener, + public views::Combobox::Listener { + public: + explicit TextExample(ExamplesMain* main); + virtual ~TextExample(); + + // Overridden from ExampleBase: + virtual void CreateExampleView(views::View* container) OVERRIDE; + + private: + // Create and add a combo box to the layout. + views::Combobox* AddCombobox(views::GridLayout* layout, + const char *name, + const char** strings, + int count); + + // Overridden from views::Combobox::Listener: + virtual void ItemChanged(views::Combobox* combo_box, + int prev_index, + int new_index) OVERRIDE; + + // Overridden from views::ButtonListener: + virtual void ButtonPressed(views::Button* button, + const views::Event& event) OVERRIDE; + + class TextExampleView; + // The content of the scroll view. + TextExampleView* text_view_; + + // Combo box for horizontal text alignment. + views::Combobox* h_align_cb_; + + // Combo box for vertical text alignment. + views::Combobox* v_align_cb_; + + // Combo box for text eliding style. + views::Combobox* eliding_cb_; + + // Combo box to choose one of the sample texts. + views::Combobox* text_cb_; + + // Check box to enable/disable multiline text drawing. + views::Checkbox* multiline_checkbox_; + + // Check box to enable/disable character break behavior. + views::Checkbox* break_checkbox_; + + DISALLOW_COPY_AND_ASSIGN(TextExample); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_TEXT_EXAMPLE_H_ |