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.h" 6 7#include <string> 8 9#include "base/memory/scoped_vector.h" 10#include "base/strings/utf_string_conversions.h" 11#include "ui/base/models/combobox_model.h" 12#include "ui/base/ui_base_paths.h" 13#include "ui/views/background.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/widget_example.h" 38#include "ui/views/layout/fill_layout.h" 39#include "ui/views/layout/grid_layout.h" 40#include "ui/views/widget/widget.h" 41#include "ui/views/widget/widget_delegate.h" 42 43namespace views { 44namespace examples { 45 46// Model for the examples that are being added via AddExample(). 47class ComboboxModelExampleList : public ui::ComboboxModel { 48 public: 49 ComboboxModelExampleList() {} 50 virtual ~ComboboxModelExampleList() {} 51 52 // Overridden from ui::ComboboxModel: 53 virtual int GetItemCount() const OVERRIDE { return example_list_.size(); } 54 virtual string16 GetItemAt(int index) OVERRIDE { 55 return UTF8ToUTF16(example_list_[index]->example_title()); 56 } 57 58 View* GetItemViewAt(int index) { 59 return example_list_[index]->example_view(); 60 } 61 62 void AddExample(ExampleBase* example) { 63 example_list_.push_back(example); 64 } 65 66 private: 67 ScopedVector<ExampleBase> example_list_; 68 69 DISALLOW_COPY_AND_ASSIGN(ComboboxModelExampleList); 70}; 71 72class ExamplesWindowContents : public WidgetDelegateView, 73 public ComboboxListener { 74 public: 75 ExamplesWindowContents(Operation operation) 76 : combobox_(new Combobox(&combobox_model_)), 77 example_shown_(new View), 78 status_label_(new Label), 79 operation_(operation) { 80 instance_ = this; 81 combobox_->set_listener(this); 82 } 83 virtual ~ExamplesWindowContents() { 84 // Delete |combobox_| first as it references |combobox_model_|. 85 delete combobox_; 86 combobox_ = NULL; 87 } 88 89 // Prints a message in the status area, at the bottom of the window. 90 void SetStatus(const std::string& status) { 91 status_label_->SetText(UTF8ToUTF16(status)); 92 } 93 94 static ExamplesWindowContents* instance() { return instance_; } 95 96 private: 97 // Overridden from WidgetDelegateView: 98 virtual bool CanResize() const OVERRIDE { return true; } 99 virtual bool CanMaximize() const OVERRIDE { return true; } 100 virtual string16 GetWindowTitle() const OVERRIDE { 101 return ASCIIToUTF16("Views Examples"); 102 } 103 virtual View* GetContentsView() OVERRIDE { return this; } 104 virtual void WindowClosing() OVERRIDE { 105 instance_ = NULL; 106 if (operation_ == QUIT_ON_CLOSE) 107 base::MessageLoopForUI::current()->Quit(); 108 } 109 110 // Overridden from View: 111 virtual void ViewHierarchyChanged( 112 const ViewHierarchyChangedDetails& details) OVERRIDE { 113 if (details.is_add && details.child == this) 114 InitExamplesWindow(); 115 } 116 117 // Overridden from ComboboxListener: 118 virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { 119 DCHECK_EQ(combobox, combobox_); 120 DCHECK(combobox->selected_index() < combobox_model_.GetItemCount()); 121 example_shown_->RemoveAllChildViews(false); 122 example_shown_->AddChildView(combobox_model_.GetItemViewAt( 123 combobox->selected_index())); 124 example_shown_->RequestFocus(); 125 SetStatus(std::string()); 126 Layout(); 127 } 128 129 // Creates the layout within the examples window. 130 void InitExamplesWindow() { 131 AddExamples(); 132 133 set_background(Background::CreateStandardPanelBackground()); 134 GridLayout* layout = new GridLayout(this); 135 SetLayoutManager(layout); 136 ColumnSet* column_set = layout->AddColumnSet(0); 137 column_set->AddPaddingColumn(0, 5); 138 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, 139 GridLayout::USE_PREF, 0, 0); 140 column_set->AddPaddingColumn(0, 5); 141 layout->AddPaddingRow(0, 5); 142 layout->StartRow(0 /* no expand */, 0); 143 layout->AddView(combobox_); 144 145 if (combobox_model_.GetItemCount() > 0) { 146 layout->StartRow(1, 0); 147 example_shown_->SetLayoutManager(new FillLayout()); 148 example_shown_->AddChildView(combobox_model_.GetItemViewAt(0)); 149 layout->AddView(example_shown_); 150 } 151 152 layout->StartRow(0 /* no expand */, 0); 153 layout->AddView(status_label_); 154 layout->AddPaddingRow(0, 5); 155 } 156 157 // Adds all the individual examples to the combobox model. 158 void AddExamples() { 159 // Please keep this list in alphabetical order! 160 combobox_model_.AddExample(new BubbleExample); 161 combobox_model_.AddExample(new ButtonExample); 162 combobox_model_.AddExample(new CheckboxExample); 163 combobox_model_.AddExample(new ComboboxExample); 164 combobox_model_.AddExample(new DoubleSplitViewExample); 165 combobox_model_.AddExample(new LabelExample); 166 combobox_model_.AddExample(new LinkExample); 167 combobox_model_.AddExample(new MenuExample); 168 combobox_model_.AddExample(new MessageBoxExample); 169 combobox_model_.AddExample(new MultilineExample); 170 combobox_model_.AddExample(new ProgressBarExample); 171 combobox_model_.AddExample(new RadioButtonExample); 172 combobox_model_.AddExample(new ScrollViewExample); 173 combobox_model_.AddExample(new SingleSplitViewExample); 174 combobox_model_.AddExample(new SliderExample); 175 combobox_model_.AddExample(new TabbedPaneExample); 176 combobox_model_.AddExample(new TableExample); 177 combobox_model_.AddExample(new TextExample); 178 combobox_model_.AddExample(new TextfieldExample); 179 combobox_model_.AddExample(new ThrobberExample); 180 combobox_model_.AddExample(new TreeViewExample); 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 191 DISALLOW_COPY_AND_ASSIGN(ExamplesWindowContents); 192}; 193 194// static 195ExamplesWindowContents* ExamplesWindowContents::instance_ = NULL; 196 197void ShowExamplesWindow(Operation operation) { 198 if (ExamplesWindowContents::instance()) { 199 ExamplesWindowContents::instance()->GetWidget()->Activate(); 200 } else { 201 Widget::CreateWindowWithBounds(new ExamplesWindowContents(operation), 202 gfx::Rect(0, 0, 850, 300))->Show(); 203 } 204} 205 206void LogStatus(const std::string& string) { 207 ExamplesWindowContents::instance()->SetStatus(string); 208} 209 210} // namespace examples 211} // namespace views 212