diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-30 16:45:34 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-30 16:45:34 +0000 |
commit | 4a672db19685df00f748681c57a00ee253a2047e (patch) | |
tree | 6c64d27e9cdb0ced677f20c0afa4cf084d25e3ee /views/examples | |
parent | bab6cac3c2ad3f335a4694f3ea0b33477a98508b (diff) | |
download | chromium_src-4a672db19685df00f748681c57a00ee253a2047e.zip chromium_src-4a672db19685df00f748681c57a00ee253a2047e.tar.gz chromium_src-4a672db19685df00f748681c57a00ee253a2047e.tar.bz2 |
Fixed view example. It was failing because some of view classes requires WidgetGTK in
view hierarchy to call methods on it.
* added ContainerExamlpeBase that defers the creation of such views after WidgetGTK available.
minor fixes
* replaced unnecessary includes with forward decls.
* make sure we don't try to set negative hight to viewport.
BUG=None
TEST=build & run view_examples
Review URL: http://codereview.chromium.org/347010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30586 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/examples')
-rw-r--r-- | views/examples/button_example.h | 9 | ||||
-rw-r--r-- | views/examples/combobox_example.h | 7 | ||||
-rw-r--r-- | views/examples/example_base.cc | 43 | ||||
-rw-r--r-- | views/examples/example_base.h | 23 | ||||
-rw-r--r-- | views/examples/examples_main.cc | 6 | ||||
-rw-r--r-- | views/examples/message_box_example.h | 31 | ||||
-rw-r--r-- | views/examples/radio_button_example.h | 32 | ||||
-rw-r--r-- | views/examples/scroll_view_example.h | 31 | ||||
-rw-r--r-- | views/examples/tabbed_pane_example.h | 32 | ||||
-rw-r--r-- | views/examples/textfield_example.h | 37 |
10 files changed, 140 insertions, 111 deletions
diff --git a/views/examples/button_example.h b/views/examples/button_example.h index dacbc8c..f55baa9 100644 --- a/views/examples/button_example.h +++ b/views/examples/button_example.h @@ -8,11 +8,13 @@ #include "base/string_util.h" #include "views/controls/button/text_button.h" #include "views/examples/example_base.h" +#include "views/fill_layout.h" +#include "views/view.h" namespace examples { // ButtonExample simply counts the number of clicks. -class ButtonExample : protected ExampleBase, private views::ButtonListener { +class ButtonExample : public ExampleBase, public views::ButtonListener { public: explicit ButtonExample(ExamplesMain* main) : ExampleBase(main), count_(0) { button_ = new views::TextButton(this, L"Button"); @@ -24,8 +26,9 @@ class ButtonExample : protected ExampleBase, private views::ButtonListener { return L"Text Button"; } - virtual views::View* GetExampleView() { - return button_; + virtual void CreateExampleView(views::View* container) { + container->SetLayoutManager(new views::FillLayout); + container->AddChildView(button_); } private: diff --git a/views/examples/combobox_example.h b/views/examples/combobox_example.h index f65508f..ad5abd1 100644 --- a/views/examples/combobox_example.h +++ b/views/examples/combobox_example.h @@ -9,6 +9,7 @@ #include "base/string_util.h" #include "views/controls/combobox/combobox.h" #include "views/examples/example_base.h" +#include "views/fill_layout.h" namespace examples { @@ -25,8 +26,9 @@ class ComboboxExample : public ExampleBase, public views::Combobox::Listener { return L"Combo Box"; } - virtual views::View* GetExampleView() { - return combobox_; + virtual void CreateExampleView(views::View* container) { + container->SetLayoutManager(new views::FillLayout); + container->AddChildView(combobox_); } private: @@ -66,4 +68,3 @@ class ComboboxExample : public ExampleBase, public views::Combobox::Listener { #endif // VIEWS_EXAMPLES_COMBOBOX_EXAMPLE_H_ - diff --git a/views/examples/example_base.cc b/views/examples/example_base.cc index 70185b5..149ea52 100644 --- a/views/examples/example_base.cc +++ b/views/examples/example_base.cc @@ -10,9 +10,52 @@ #include "base/string_util.h" #include "views/controls/button/text_button.h" #include "views/controls/tabbed_pane/tabbed_pane.h" +#include "views/examples/examples_main.h" + +namespace { + +using views::View; + +// Some of GTK based view classes require WidgetGTK in the view +// parent chain. This class is used to defer the creation of such +// views until a WidgetGTK is added to the view hierarchy. +class ContainerView : public View { + public: + explicit ContainerView(examples::ExampleBase* base) + : example_view_created_(false), + example_base_(base) { + } + + protected: + // views::View overrides: + virtual void ViewHierarchyChanged(bool is_add, View* parent, View* child) { + View::ViewHierarchyChanged(is_add, parent, child); + // We're not using child == this because a Widget may not be + // availalbe when this is added to the hierarchy. + if (is_add && GetWidget() && !example_view_created_) { + example_view_created_ = true; + example_base_->CreateExampleView(this); + } + } + + private: + // true if the example view has already been created, or false otherwise. + bool example_view_created_; + + examples::ExampleBase* example_base_; + + DISALLOW_COPY_AND_ASSIGN(ContainerView); +}; + +} // namespace namespace examples { +ExampleBase::ExampleBase(ExamplesMain* main) + : main_(main) { + container_ = new ContainerView(this); +} + // Prints a message in the status area, at the bottom of the window. void ExampleBase::PrintStatus(const wchar_t* format, ...) { va_list ap; diff --git a/views/examples/example_base.h b/views/examples/example_base.h index 35898eb..fbe5cbc 100644 --- a/views/examples/example_base.h +++ b/views/examples/example_base.h @@ -8,8 +8,6 @@ #include <string> #include "base/basictypes.h" -#include "views/examples/examples_main.h" -#include "views/window/window_delegate.h" namespace views { class View; @@ -17,19 +15,27 @@ class View; namespace examples { +class ExamplesMain; + class ExampleBase { + public: + // Returns the view containing this example controls. + // This view is added as a tab to the example application. + views::View* GetExampleView() { + return container_; + } + + // Sub-classes should creates and add the views to the given parent. + virtual void CreateExampleView(views::View* parent) = 0; + protected: - explicit ExampleBase(ExamplesMain* main) : main_(main) {} + explicit ExampleBase(ExamplesMain* main); virtual ~ExampleBase() {} // Sub-classes should return the name of this test. // It is used as the title of the tab displaying this test's controls. virtual std::wstring GetExampleTitle() = 0; - // Sub-classes should return the view containing this example controls. - // This view is added as a tab to the example application. - virtual views::View* GetExampleView() = 0; - // Prints a message in the status area, at the bottom of the window. void PrintStatus(const wchar_t* format, ...); @@ -42,6 +48,9 @@ class ExampleBase { // The runner actually running this test. ExamplesMain* main_; + // The view containing example views. + views::View* container_; + DISALLOW_COPY_AND_ASSIGN(ExampleBase); }; diff --git a/views/examples/examples_main.cc b/views/examples/examples_main.cc index 6d65e8a..2dc6c96 100644 --- a/views/examples/examples_main.cc +++ b/views/examples/examples_main.cc @@ -69,6 +69,9 @@ void ExamplesMain::Run() { layout->StartRow(0 /* no expand */, 0); layout->AddView(status_label_); + views::Window* window = + views::Window::CreateChromeWindow(NULL, gfx::Rect(0, 0, 600, 300), this); + examples::TextfieldExample textfield_example(this); tabbed_pane->AddTab(textfield_example.GetExampleTitle(), textfield_example.GetExampleView()); @@ -97,10 +100,7 @@ void ExamplesMain::Run() { tabbed_pane->AddTab(scroll_view_example.GetExampleTitle(), scroll_view_example.GetExampleView()); - views::Window* window = - views::Window::CreateChromeWindow(NULL, gfx::Rect(0, 0, 600, 300), this); window->Show(); - views::AcceleratorHandler accelerator_handler; MessageLoopForUI::current()->Run(&accelerator_handler); } diff --git a/views/examples/message_box_example.h b/views/examples/message_box_example.h index 3edd210..9d3631b 100644 --- a/views/examples/message_box_example.h +++ b/views/examples/message_box_example.h @@ -16,17 +16,25 @@ namespace examples { // A MessageBoxView example. This tests some of checkbox features as well. -class MessageBoxExample : protected ExampleBase, private views::ButtonListener { +class MessageBoxExample : public ExampleBase, + public views::ButtonListener { public: - explicit MessageBoxExample(ExamplesMain* main) : ExampleBase(main) { + explicit MessageBoxExample(ExamplesMain* main) : ExampleBase(main) {} + + virtual ~MessageBoxExample() {} + + virtual std::wstring GetExampleTitle() { + return L"Message Box View"; + } + + virtual void CreateExampleView(views::View* container) { message_box_view_ = new MessageBoxView(0, L"Message Box Message", L"Default Prompt"); status_ = new views::TextButton(this, L"Show Status"); toggle_ = new views::TextButton(this, L"Toggle Checkbox"); - container_ = new views::View(); - views::GridLayout* layout = new views::GridLayout(container_); - container_->SetLayoutManager(layout); + views::GridLayout* layout = new views::GridLayout(container); + container->SetLayoutManager(layout); message_box_view_->SetCheckBoxLabel(L"Check Box"); @@ -50,16 +58,6 @@ class MessageBoxExample : protected ExampleBase, private views::ButtonListener { layout->AddView(toggle_); } - virtual ~MessageBoxExample() {} - - virtual std::wstring GetExampleTitle() { - return L"Message Box View"; - } - - virtual views::View* GetExampleView() { - return container_; - } - private: // ButtonListener overrides. virtual void ButtonPressed(views::Button* sender, const views::Event& event) { @@ -74,9 +72,6 @@ class MessageBoxExample : protected ExampleBase, private views::ButtonListener { } } - // The view containing this test's controls. - views::View* container_; - // The MessageBoxView to be tested. MessageBoxView* message_box_view_; diff --git a/views/examples/radio_button_example.h b/views/examples/radio_button_example.h index abe5a4c..5610700 100644 --- a/views/examples/radio_button_example.h +++ b/views/examples/radio_button_example.h @@ -13,10 +13,18 @@ namespace examples { -class RadioButtonExample : protected ExampleBase, - private views::ButtonListener { +class RadioButtonExample : public ExampleBase, + public views::ButtonListener { public: - explicit RadioButtonExample(ExamplesMain* main) : ExampleBase(main) { + explicit RadioButtonExample(ExamplesMain* main): ExampleBase(main) {} + + virtual ~RadioButtonExample() {} + + virtual std::wstring GetExampleTitle() { + return L"Radio Button"; + } + + virtual void CreateExampleView(views::View* container) { select_ = new views::TextButton(this, L"Select"); status_ = new views::TextButton(this, L"Show Status"); @@ -31,9 +39,8 @@ class RadioButtonExample : protected ExampleBase, group); } - container_ = new views::View(); - views::GridLayout* layout = new views::GridLayout(container_); - container_->SetLayoutManager(layout); + views::GridLayout* layout = new views::GridLayout(container); + container->SetLayoutManager(layout); views::ColumnSet* column_set = layout->AddColumnSet(0); column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, @@ -48,16 +55,6 @@ class RadioButtonExample : protected ExampleBase, layout->AddView(status_); } - virtual ~RadioButtonExample() {} - - virtual std::wstring GetExampleTitle() { - return L"Radio Button"; - } - - virtual views::View* GetExampleView() { - return container_; - } - private: // Override from ButtonListener virtual void ButtonPressed(views::Button* sender, const views::Event& event) { @@ -76,9 +73,6 @@ class RadioButtonExample : protected ExampleBase, } } - // The view containing this test's controls. - views::View* container_; - // 6 radio buttons, 0-2 consists 1st group, and 3-5 consists // 2nd group. views::RadioButton* radio_buttons_[6]; diff --git a/views/examples/scroll_view_example.h b/views/examples/scroll_view_example.h index 2fd2d2f..035c06c 100644 --- a/views/examples/scroll_view_example.h +++ b/views/examples/scroll_view_example.h @@ -13,9 +13,18 @@ namespace examples { -class ScrollViewExample : protected ExampleBase, private views::ButtonListener { +class ScrollViewExample : public ExampleBase, + public views::ButtonListener { public: - explicit ScrollViewExample(ExamplesMain* main) : ExampleBase(main) { + explicit ScrollViewExample(ExamplesMain* main): ExampleBase(main) {} + + virtual ~ScrollViewExample() {} + + virtual std::wstring GetExampleTitle() { + return L"Scroll View"; + } + + virtual void CreateExampleView(views::View* container) { wide_ = new views::TextButton(this, L"Wide"); tall_ = new views::TextButton(this, L"Tall"); big_square_ = new views::TextButton(this, L"Big Square"); @@ -27,9 +36,8 @@ class ScrollViewExample : protected ExampleBase, private views::ButtonListener { scrollable_->SetBounds(0, 0, 1000, 100); scrollable_->SetColor(SK_ColorYELLOW, SK_ColorCYAN); - container_ = new views::View(); - views::GridLayout* layout = new views::GridLayout(container_); - container_->SetLayoutManager(layout); + views::GridLayout* layout = new views::GridLayout(container); + container->SetLayoutManager(layout); // Add scroll view. views::ColumnSet* column_set = layout->AddColumnSet(0); @@ -52,16 +60,6 @@ class ScrollViewExample : protected ExampleBase, private views::ButtonListener { layout->AddView(scroll_to_); } - virtual ~ScrollViewExample() {} - - virtual std::wstring GetExampleTitle() { - return L"Scroll View"; - } - - virtual views::View* GetExampleView() { - return container_; - } - private: // ScrollView's content, which draws gradient color on background. // TODO(oshima): add child views as well. @@ -108,9 +106,6 @@ class ScrollViewExample : protected ExampleBase, private views::ButtonListener { scroll_view_->Layout(); } - // The view containing this test's controls. - views::View* container_; - // Control buttons to change the size of scrollable and jump to // predefined position. views::TextButton* wide_, *tall_, *big_square_, *small_square_, *scroll_to_; diff --git a/views/examples/tabbed_pane_example.h b/views/examples/tabbed_pane_example.h index fe622e9..4b4bdfc 100644 --- a/views/examples/tabbed_pane_example.h +++ b/views/examples/tabbed_pane_example.h @@ -12,20 +12,27 @@ namespace examples { // A TabbedPane example tests adding/removing/selecting tabs. -class TabbedPaneExample : protected ExampleBase, - private views::ButtonListener, +class TabbedPaneExample : public ExampleBase, + public views::ButtonListener, views::TabbedPane::Listener { public: - explicit TabbedPaneExample(ExamplesMain* main) : ExampleBase(main) { + explicit TabbedPaneExample(ExamplesMain* main) : ExampleBase(main) {} + + virtual ~TabbedPaneExample() {} + + virtual std::wstring GetExampleTitle() { + return L"Tabbed Pane"; + } + + virtual void CreateExampleView(views::View* container) { tabbed_pane_ = new views::TabbedPane(); add_ = new views::TextButton(this, L"Add"); add_at_ = new views::TextButton(this, L"Add At 1"); remove_at_ = new views::TextButton(this, L"Remove At 1"); select_at_ = new views::TextButton(this, L"Select At 1"); - container_ = new views::View(); - views::GridLayout* layout = new views::GridLayout(container_); - container_->SetLayoutManager(layout); + views::GridLayout* layout = new views::GridLayout(container); + container->SetLayoutManager(layout); const int tabbed_pane_column = 0; views::ColumnSet* column_set = layout->AddColumnSet(tabbed_pane_column); @@ -53,16 +60,6 @@ class TabbedPaneExample : protected ExampleBase, layout->AddView(select_at_); } - virtual ~TabbedPaneExample() {} - - virtual std::wstring GetExampleTitle() { - return L"Tabbed Pane"; - } - - virtual views::View* GetExampleView() { - return container_; - } - private: // ButtonListener overrides. virtual void ButtonPressed(views::Button* sender, const views::Event& event) { @@ -100,9 +97,6 @@ class TabbedPaneExample : protected ExampleBase, tabbed_pane_->AddTab(label, button); } - // The view containing this test's controls. - views::View* container_; - // The tabbed pane to be tested. views::TabbedPane* tabbed_pane_; diff --git a/views/examples/textfield_example.h b/views/examples/textfield_example.h index e30f3f9..82b7b79 100644 --- a/views/examples/textfield_example.h +++ b/views/examples/textfield_example.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyight (c) 2009 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. @@ -17,11 +17,19 @@ namespace examples { using views::Textfield; // TextfieldExample mimics login screen. -class TextfieldExample : protected ExampleBase, - private Textfield::Controller, - private views::ButtonListener { +class TextfieldExample : public ExampleBase, + public Textfield::Controller, + public views::ButtonListener { public: - explicit TextfieldExample(ExamplesMain* main) : ExampleBase(main) { + explicit TextfieldExample(ExamplesMain* main) : ExampleBase(main) {} + + virtual ~TextfieldExample() {} + + virtual std::wstring GetExampleTitle() { + return L"Textfield"; + } + + virtual void CreateExampleView(views::View* container) { name_ = new Textfield(); password_ = new Textfield(Textfield::STYLE_PASSWORD); show_password_ = new views::TextButton(this, L"Show password"); @@ -30,9 +38,8 @@ class TextfieldExample : protected ExampleBase, name_->SetController(this); password_->SetController(this); - container_ = new views::View(); - views::GridLayout* layout = new views::GridLayout(container_); - container_->SetLayoutManager(layout); + views::GridLayout* layout = new views::GridLayout(container); + container->SetLayoutManager(layout); views::ColumnSet* column_set = layout->AddColumnSet(0); column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, @@ -53,16 +60,6 @@ class TextfieldExample : protected ExampleBase, layout->AddView(append_); } - virtual ~TextfieldExample() {} - - virtual std::wstring GetExampleTitle() { - return L"Textfield"; - } - - virtual views::View* GetExampleView() { - return container_; - } - private: // Textfield::Controller implementations: // This method is called whenever the text in the field changes. @@ -95,9 +92,6 @@ class TextfieldExample : protected ExampleBase, } } - // The view containing this test's controls. - views::View* container_; - // Textfields for name and password. views::Textfield* name_; views::Textfield* password_; @@ -113,3 +107,4 @@ class TextfieldExample : protected ExampleBase, } // namespace examples #endif // VIEWS_EXAMPLES_TEXTFIELD_EXAMPLE_H_ + |