examples_window_with_content.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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 3// found in the LICENSE file. 4 5#include "ui/views/examples/examples_window_with_content.h" 6 7#include <string> 8 9#include "base/memory/scoped_vector.h" 10#include "base/strings/utf_string_conversions.h" 11#include "content/public/browser/browser_context.h" 12#include "ui/base/models/combobox_model.h" 13#include "ui/base/ui_base_paths.h" 14#include "ui/views/controls/combobox/combobox.h" 15#include "ui/views/controls/label.h" 16#include "ui/views/examples/bubble_example.h" 17#include "ui/views/examples/button_example.h" 18#include "ui/views/examples/checkbox_example.h" 19#include "ui/views/examples/combobox_example.h" 20#include "ui/views/examples/double_split_view_example.h" 21#include "ui/views/examples/label_example.h" 22#include "ui/views/examples/link_example.h" 23#include "ui/views/examples/menu_example.h" 24#include "ui/views/examples/message_box_example.h" 25#include "ui/views/examples/multiline_example.h" 26#include "ui/views/examples/progress_bar_example.h" 27#include "ui/views/examples/radio_button_example.h" 28#include "ui/views/examples/scroll_view_example.h" 29#include "ui/views/examples/single_split_view_example.h" 30#include "ui/views/examples/slider_example.h" 31#include "ui/views/examples/tabbed_pane_example.h" 32#include "ui/views/examples/table_example.h" 33#include "ui/views/examples/text_example.h" 34#include "ui/views/examples/textfield_example.h" 35#include "ui/views/examples/throbber_example.h" 36#include "ui/views/examples/tree_view_example.h" 37#include "ui/views/examples/webview_example.h" 38#include "ui/views/examples/widget_example.h" 39#include "ui/views/layout/fill_layout.h" 40#include "ui/views/layout/grid_layout.h" 41#include "ui/views/widget/widget.h" 42#include "ui/views/widget/widget_delegate.h" 43 44namespace views { 45namespace examples { 46 47// Model for the examples that are being added via AddExample(). 48class ComboboxModelExampleList : public ui::ComboboxModel { 49 public: 50 ComboboxModelExampleList() {} 51 virtual ~ComboboxModelExampleList() {} 52 53 // Overridden from ui::ComboboxModel: 54 virtual int GetItemCount() const OVERRIDE { return example_list_.size(); } 55 virtual string16 GetItemAt(int index) OVERRIDE { 56 return UTF8ToUTF16(example_list_[index]->example_title()); 57 } 58 59 View* GetItemViewAt(int index) { 60 return example_list_[index]->example_view(); 61 } 62 63 void AddExample(ExampleBase* example) { 64 example_list_.push_back(example); 65 } 66 67 private: 68 ScopedVector<ExampleBase> example_list_; 69 70 DISALLOW_COPY_AND_ASSIGN(ComboboxModelExampleList); 71}; 72 73class ExamplesWindowContents : public WidgetDelegateView, 74 public ComboboxListener { 75 public: 76 ExamplesWindowContents(Operation operation, 77 content::BrowserContext* browser_context) 78 : combobox_(new Combobox(&combobox_model_)), 79 example_shown_(new View), 80 status_label_(new Label), 81 operation_(operation), 82 browser_context_(browser_context) { 83 instance_ = this; 84 combobox_->set_listener(this); 85 } 86 virtual ~ExamplesWindowContents() {} 87 88 // Prints a message in the status area, at the bottom of the window. 89 void SetStatus(const std::string& status) { 90 status_label_->SetText(UTF8ToUTF16(status)); 91 } 92 93 static ExamplesWindowContents* instance() { return instance_; } 94 95 private: 96 // Overridden from WidgetDelegateView: 97 virtual bool CanResize() const OVERRIDE { return true; } 98 virtual bool CanMaximize() const OVERRIDE { return true; } 99 virtual string16 GetWindowTitle() const OVERRIDE { 100 return ASCIIToUTF16("Views Examples"); 101 } 102 virtual View* GetContentsView() OVERRIDE { return this; } 103 virtual void WindowClosing() OVERRIDE { 104 instance_ = NULL; 105 if (operation_ == QUIT_ON_CLOSE) 106 base::MessageLoopForUI::current()->Quit(); 107 } 108 109 // Overridden from View: 110 virtual void ViewHierarchyChanged( 111 const ViewHierarchyChangedDetails& details) OVERRIDE { 112 if (details.is_add && details.child == this) 113 InitExamplesWindow(); 114 } 115 116 // Overridden from ComboboxListener: 117 virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { 118 DCHECK_EQ(combobox, combobox_); 119 DCHECK(combobox->selected_index() < combobox_model_.GetItemCount()); 120 example_shown_->RemoveAllChildViews(false); 121 example_shown_->AddChildView(combobox_model_.GetItemViewAt( 122 combobox->selected_index())); 123 example_shown_->RequestFocus(); 124 SetStatus(std::string()); 125 Layout(); 126 } 127 128 // Creates the layout within the examples window. 129 void InitExamplesWindow() { 130 AddExamples(); 131 132 set_background(Background::CreateStandardPanelBackground()); 133 GridLayout* layout = new GridLayout(this); 134 SetLayoutManager(layout); 135 ColumnSet* column_set = layout->AddColumnSet(0); 136 column_set->AddPaddingColumn(0, 5); 137 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, 138 GridLayout::USE_PREF, 0, 0); 139 column_set->AddPaddingColumn(0, 5); 140 layout->AddPaddingRow(0, 5); 141 layout->StartRow(0 /* no expand */, 0); 142 layout->AddView(combobox_); 143 144 if (combobox_model_.GetItemCount() > 0) { 145 layout->StartRow(1, 0); 146 example_shown_->SetLayoutManager(new FillLayout()); 147 example_shown_->AddChildView(combobox_model_.GetItemViewAt(0)); 148 layout->AddView(example_shown_); 149 } 150 151 layout->StartRow(0 /* no expand */, 0); 152 layout->AddView(status_label_); 153 layout->AddPaddingRow(0, 5); 154 } 155 156 // Adds all the individual examples to the combobox model. 157 void AddExamples() { 158 // Please keep this list in alphabetical order! 159 combobox_model_.AddExample(new BubbleExample); 160 combobox_model_.AddExample(new ButtonExample); 161 combobox_model_.AddExample(new CheckboxExample); 162 combobox_model_.AddExample(new ComboboxExample); 163 combobox_model_.AddExample(new DoubleSplitViewExample); 164 combobox_model_.AddExample(new LabelExample); 165 combobox_model_.AddExample(new LinkExample); 166 combobox_model_.AddExample(new MenuExample); 167 combobox_model_.AddExample(new MessageBoxExample); 168 combobox_model_.AddExample(new MultilineExample); 169 combobox_model_.AddExample(new ProgressBarExample); 170 combobox_model_.AddExample(new RadioButtonExample); 171 combobox_model_.AddExample(new ScrollViewExample); 172 combobox_model_.AddExample(new SingleSplitViewExample); 173 combobox_model_.AddExample(new SliderExample); 174 combobox_model_.AddExample(new TabbedPaneExample); 175 combobox_model_.AddExample(new TableExample); 176 combobox_model_.AddExample(new TextExample); 177 combobox_model_.AddExample(new TextfieldExample); 178 combobox_model_.AddExample(new ThrobberExample); 179 combobox_model_.AddExample(new TreeViewExample); 180 combobox_model_.AddExample(new WebViewExample(browser_context_)); 181 combobox_model_.AddExample(new WidgetExample); 182 } 183 184 static ExamplesWindowContents* instance_; 185 ComboboxModelExampleList combobox_model_; 186 Combobox* combobox_; 187 View* example_shown_; 188 Label* status_label_; 189 const Operation operation_; 190 content::BrowserContext* browser_context_; 191 192 DISALLOW_COPY_AND_ASSIGN(ExamplesWindowContents); 193}; 194 195// static 196ExamplesWindowContents* ExamplesWindowContents::instance_ = NULL; 197 198void ShowExamplesWindowWithContent(Operation operation, 199 content::BrowserContext* browser_context) { 200 if (ExamplesWindowContents::instance()) { 201 ExamplesWindowContents::instance()->GetWidget()->Activate(); 202 } else { 203 Widget* widget = new Widget; 204 Widget::InitParams params; 205 params.delegate = new ExamplesWindowContents(operation, browser_context); 206 params.bounds = gfx::Rect(0, 0, 850, 300); 207 params.top_level = true; 208 widget->Init(params); 209 widget->Show(); 210 } 211} 212 213void LogStatus(const std::string& string) { 214 ExamplesWindowContents::instance()->SetStatus(string); 215} 216 217} // namespace examples 218} // namespace views 219