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