diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-22 01:15:18 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-22 01:15:18 +0000 |
commit | fe00335bb1d7dab7143a3f77cac9526d53f08c7a (patch) | |
tree | 02f9a379a10865c2b8762f663552588ab3816979 /views/examples | |
parent | 5b69e884ffd49dd64835a87d95b282377d6ad1c3 (diff) | |
download | chromium_src-fe00335bb1d7dab7143a3f77cac9526d53f08c7a.zip chromium_src-fe00335bb1d7dab7143a3f77cac9526d53f08c7a.tar.gz chromium_src-fe00335bb1d7dab7143a3f77cac9526d53f08c7a.tar.bz2 |
View examples:
I'm planning to move these to unit_test using Jay's testing framework,
but let's check in it first, and then I will refactor and move them to unittest.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26780 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/examples')
-rw-r--r-- | views/examples/button_example.h | 42 | ||||
-rw-r--r-- | views/examples/combobox_example.h | 61 | ||||
-rw-r--r-- | views/examples/example_base.cc | 38 | ||||
-rw-r--r-- | views/examples/example_base.h | 48 | ||||
-rw-r--r-- | views/examples/examples_main_base.cc | 79 | ||||
-rw-r--r-- | views/examples/examples_main_base.h | 41 | ||||
-rw-r--r-- | views/examples/examples_main_gtk.cc | 36 |
7 files changed, 345 insertions, 0 deletions
diff --git a/views/examples/button_example.h b/views/examples/button_example.h new file mode 100644 index 0000000..ef93376 --- /dev/null +++ b/views/examples/button_example.h @@ -0,0 +1,42 @@ +// Copyright (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. + +#ifndef VIEWS_EXAMPLES_BUTTON_EXAMPLE_H_ +#define VIEWS_EXAMPLES_BUTTON_EXAMPLE_H_ + +#include "base/string_util.h" +#include "views/controls/button/text_button.h" +#include "views/controls/tabbed_pane/tabbed_pane.h" +#include "views/examples/example_base.h" + +namespace examples { + +// ButtonExample simply counts the number of clicks. +class ButtonExample : protected ExampleBase, private views::ButtonListener { + public: + ButtonExample(views::TabbedPane* tabbed_pane, views::Label* message) + : ExampleBase(message), + count_(0) { + views::TextButton* button = new views::TextButton(this, L"Button"); + tabbed_pane->AddTab(L"Text Button", button); + } + + virtual ~ButtonExample() {} + + private: + // ButtonListner implementation. + virtual void ButtonPressed(views::Button* sender, const views::Event& event) { + PrintStatus(L"Pressed! count:%d", ++count_); + } + + // The number of times the button is pressed. + int count_; + + DISALLOW_COPY_AND_ASSIGN(ButtonExample); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_BUTTON_EXAMPLE_H_ + diff --git a/views/examples/combobox_example.h b/views/examples/combobox_example.h new file mode 100644 index 0000000..28c8cc3 --- /dev/null +++ b/views/examples/combobox_example.h @@ -0,0 +1,61 @@ +// Copyright (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. + +#ifndef VIEWS_EXAMPLES_COMBOBOX_EXAMPLE_H_ +#define VIEWS_EXAMPLES_COMBOBOX_EXAMPLE_H_ + +#include "app/combobox_model.h" +#include "base/string_util.h" +#include "views/controls/combobox/combobox.h" +#include "views/examples/example_base.h" + +namespace examples { + +// ComboboxExample +class ComboboxExample : protected ExampleBase, + private views::Combobox::Listener { + public: + ComboboxExample(views::TabbedPane* tabbed_pane, views::Label* message) + : ExampleBase(message) { + views::Combobox* cb = new views::Combobox(new ComboboxModelExample()); + cb->set_listener(this); + tabbed_pane->AddTab(L"Combo Box", cb); + } + virtual ~ComboboxExample() {} + + private: + // An sample combobox model that generates list of "Item <index>". + class ComboboxModelExample : public ComboboxModel { + public: + ComboboxModelExample() {} + virtual ~ComboboxModelExample() {} + + virtual int GetItemCount() { + return 10; + } + + virtual std::wstring GetItemAt(int index) { + return StringPrintf(L"Item %d", index); + } + + private: + DISALLOW_COPY_AND_ASSIGN(ComboboxModelExample); + }; + + // Lister implementation. + virtual void ItemChanged(views::Combobox* combo_box, + int prev_index, + int new_index) { + PrintStatus(L"Selected: index=%d, label=%ls", + new_index, combo_box->model()->GetItemAt(new_index).c_str()); + } + + DISALLOW_COPY_AND_ASSIGN(ComboboxExample); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_COMBOBOX_EXAMPLE_H_ + + diff --git a/views/examples/example_base.cc b/views/examples/example_base.cc new file mode 100644 index 0000000..09231dd --- /dev/null +++ b/views/examples/example_base.cc @@ -0,0 +1,38 @@ +// Copyright (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. + +#include "views/examples/example_base.h" + +#include <stdarg.h> +#include <string> + +#include "base/string_util.h" +#include "views/controls/button/text_button.h" +#include "views/controls/label.h" +#include "views/controls/tabbed_pane/tabbed_pane.h" + +namespace examples { + +using views::Label; +using views::TabbedPane; +using views::TextButton; + +// Prints a message in the status area, at the bottom of the window. +void ExampleBase::PrintStatus(const wchar_t* format, ...) { + va_list ap; + va_start(ap, format); + std::wstring msg; + StringAppendV(&msg, format, ap); + status_->SetText(msg); +} + +// static +void ExampleBase::AddButton(TabbedPane* tabbed_pane, + const std::wstring& label) { + TextButton* button = new TextButton(NULL, label); + tabbed_pane->AddTab(label, button); +} + +} // namespace examples + diff --git a/views/examples/example_base.h b/views/examples/example_base.h new file mode 100644 index 0000000..b17065c --- /dev/null +++ b/views/examples/example_base.h @@ -0,0 +1,48 @@ +// Copyright (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. + +#ifndef VIEWS_EXAMPLES_EXAMPLE_BASE_H_ +#define VIEWS_EXAMPLES_EXAMPLE_BASE_H_ + +#include <string> + +#include "base/basictypes.h" + +namespace views { +class Label; +class TabbedPane; +} // namespace views + +namespace examples { + +// ExampleBase defines utility functions for examples. +class ExampleBase { + protected: + explicit ExampleBase(views::Label* status) : status_(status) {} + + virtual ~ExampleBase() {} + + // Prints a message in the status area, at the bottom of the window. + void PrintStatus(const wchar_t* format, ...); + + // Converts an integer/boolean to wchat "on"/"off". + static const wchar_t* IntToOnOff(int value) { + return value ? L"on" : L"off"; + } + + // Adds a new tab with the button with given label to the tabbed pane. + static void AddButton(views::TabbedPane* tabbed_pane, + const std::wstring& label); + + private: + // A label to show a message at the bottom of example app. + views::Label* status_; + + DISALLOW_COPY_AND_ASSIGN(ExampleBase); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_EXAMPLE_BASE_H_ + diff --git a/views/examples/examples_main_base.cc b/views/examples/examples_main_base.cc new file mode 100644 index 0000000..917f96e --- /dev/null +++ b/views/examples/examples_main_base.cc @@ -0,0 +1,79 @@ +// Copyright (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. + +#include "views/examples/examples_main_base.h" + +#include "app/app_paths.h" +#include "app/resource_bundle.h" +#include "base/at_exit.h" +#include "base/process_util.h" +#include "views/controls/label.h" +#include "views/focus/accelerator_handler.h" +#include "views/grid_layout.h" +#include "views/widget/widget.h" + +// Examples +#include "views/examples/button_example.h" +#include "views/examples/combobox_example.h" + +namespace examples { + +using views::Background; +using views::ColumnSet; +using views::GridLayout; +using views::Label; +using views::TabbedPane; +using views::View; +using views::Widget; + +void ExamplesMainBase::Run() { + base::EnableTerminationOnHeapCorruption(); + + // The exit manager is in charge of calling the dtors of singleton objects. + base::AtExitManager exit_manager; + + app::RegisterPathProvider(); + + // This requires chrome to be built first right now. + // TODO(oshima): fix build to include resource file. + ResourceBundle::InitSharedInstance(L"en-US"); + ResourceBundle::GetSharedInstance().LoadThemeResources(); + + MessageLoop main_message_loop(MessageLoop::TYPE_UI); + + Widget* widget = CreateTopLevelWidget(); + widget->Init(NULL, gfx::Rect(0, 0, 500, 300)); + + // Creates a window with the tabbed pane for each examples, and + // a label to print messages from each examples. + View* container = new View(); + container->set_background(Background::CreateStandardPanelBackground()); + GridLayout* layout = new GridLayout(container); + container->SetLayoutManager(layout); + + widget->SetContentsView(container); + + ColumnSet* column_set = layout->AddColumnSet(0); + column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, + GridLayout::USE_PREF, 0, 0); + + TabbedPane* tabbed_pane = new TabbedPane(); + Label* message = new Label(); + + layout->StartRow(1, 0); + layout->AddView(tabbed_pane); + layout->StartRow(0 /* no expand */, 0); + layout->AddView(message); + + ButtonExample button_example(tabbed_pane, message); + ComboboxExample combobox_example(tabbed_pane, message); + + widget->Show(); + + views::AcceleratorHandler accelerator_handler; + MessageLoopForUI::current()->Run(&accelerator_handler); +} + +} // namespace examples + diff --git a/views/examples/examples_main_base.h b/views/examples/examples_main_base.h new file mode 100644 index 0000000..2645408 --- /dev/null +++ b/views/examples/examples_main_base.h @@ -0,0 +1,41 @@ +// Copyright (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. + +#ifndef VIEWS_EXAMPLES_EXAMPLE_MAIN_BASE_H_ +#define VIEWS_EXAMPLES_EXAMPLE_MAIN_BASE_H_ + +#include "base/basictypes.h" + +namespace views { +class Widget; +} // namespace views + +namespace examples { + +// ExamplesMainBase creates all view examples and start event loop. +// Each platform specific main class should extend this base class and +// provide the implementation of CreateTopLevelWidget. The main program +// has to perform platform specific initializations before calling Run(). +class ExamplesMainBase { + public: + ExamplesMainBase() {} + virtual ~ExamplesMainBase() {} + + // Creates all examples and start UI event loop. + void Run(); + + protected: + // Returns a widget for a top level, decorated window. + // Each platform must implement this method and return platform specific + // widget. + virtual views::Widget* CreateTopLevelWidget() = 0; + + private: + DISALLOW_COPY_AND_ASSIGN(ExamplesMainBase); +}; + +} // namespace examples + +#endif // VIEWS_EXAMPLES_EXAMPLE_MAIN_BASE_H_ + diff --git a/views/examples/examples_main_gtk.cc b/views/examples/examples_main_gtk.cc new file mode 100644 index 0000000..041c8d4 --- /dev/null +++ b/views/examples/examples_main_gtk.cc @@ -0,0 +1,36 @@ +// Copyright (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. + +#include "views/widget/widget_gtk.h" +#include "views/examples/examples_main_base.h" + +namespace { + +class ExamplesMainGtk : public examples::ExamplesMainBase { + public: + ExamplesMainGtk() {} + virtual ~ExamplesMainGtk() {} + + // Overrides ExamplesMainBase. + virtual views::Widget* CreateTopLevelWidget() { + return new views::WidgetGtk(views::WidgetGtk::TYPE_DECORATED_WINDOW); + } + + private: + DISALLOW_COPY_AND_ASSIGN(ExamplesMainGtk); +}; + +} // namespace + +int main(int argc, char** argv) { + // Initializes gtk stuff. + g_thread_init(NULL); + g_type_init(); + gtk_init(&argc, &argv); + + ExamplesMainGtk main; + main.Run(); + return 0; +} + |