| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/examples/examples_window.h" | 5 #include "ui/views/examples/examples_window.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 7 #include <string> | 8 #include <string> |
| 8 | 9 |
| 9 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "ui/base/models/combobox_model.h" | 12 #include "ui/base/models/combobox_model.h" |
| 12 #include "ui/base/ui_base_paths.h" | 13 #include "ui/base/ui_base_paths.h" |
| 13 #include "ui/views/background.h" | 14 #include "ui/views/background.h" |
| 14 #include "ui/views/controls/combobox/combobox.h" | 15 #include "ui/views/controls/combobox/combobox.h" |
| 15 #include "ui/views/controls/label.h" | 16 #include "ui/views/controls/label.h" |
| 16 #include "ui/views/examples/bubble_example.h" | 17 #include "ui/views/examples/bubble_example.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 36 #include "ui/views/examples/tree_view_example.h" | 37 #include "ui/views/examples/tree_view_example.h" |
| 37 #include "ui/views/examples/widget_example.h" | 38 #include "ui/views/examples/widget_example.h" |
| 38 #include "ui/views/layout/fill_layout.h" | 39 #include "ui/views/layout/fill_layout.h" |
| 39 #include "ui/views/layout/grid_layout.h" | 40 #include "ui/views/layout/grid_layout.h" |
| 40 #include "ui/views/widget/widget.h" | 41 #include "ui/views/widget/widget.h" |
| 41 #include "ui/views/widget/widget_delegate.h" | 42 #include "ui/views/widget/widget_delegate.h" |
| 42 | 43 |
| 43 namespace views { | 44 namespace views { |
| 44 namespace examples { | 45 namespace examples { |
| 45 | 46 |
| 47 typedef scoped_ptr<ScopedVector<ExampleBase> > ScopedExamples; |
| 48 |
| 49 namespace { |
| 50 |
| 51 // Creates the default set of examples. Caller owns the result. |
| 52 ScopedExamples CreateExamples() { |
| 53 ScopedExamples examples(new ScopedVector<ExampleBase>); |
| 54 examples->push_back(new BubbleExample); |
| 55 examples->push_back(new ButtonExample); |
| 56 examples->push_back(new CheckboxExample); |
| 57 examples->push_back(new ComboboxExample); |
| 58 examples->push_back(new DoubleSplitViewExample); |
| 59 examples->push_back(new LabelExample); |
| 60 examples->push_back(new LinkExample); |
| 61 examples->push_back(new MenuExample); |
| 62 examples->push_back(new MessageBoxExample); |
| 63 examples->push_back(new MultilineExample); |
| 64 examples->push_back(new ProgressBarExample); |
| 65 examples->push_back(new RadioButtonExample); |
| 66 examples->push_back(new ScrollViewExample); |
| 67 examples->push_back(new SingleSplitViewExample); |
| 68 examples->push_back(new SliderExample); |
| 69 examples->push_back(new TabbedPaneExample); |
| 70 examples->push_back(new TableExample); |
| 71 examples->push_back(new TextExample); |
| 72 examples->push_back(new TextfieldExample); |
| 73 examples->push_back(new ThrobberExample); |
| 74 examples->push_back(new TreeViewExample); |
| 75 examples->push_back(new WidgetExample); |
| 76 return examples.Pass(); |
| 77 } |
| 78 |
| 79 struct ExampleTitleCompare { |
| 80 bool operator() (ExampleBase* a, ExampleBase* b) { |
| 81 return a->example_title() < b->example_title(); |
| 82 } |
| 83 }; |
| 84 |
| 85 ScopedExamples GetExamplesToShow(ScopedExamples extra) { |
| 86 ScopedExamples examples(CreateExamples()); |
| 87 if (extra.get()) { |
| 88 examples->insert(examples->end(), extra->begin(), extra->end()); |
| 89 extra->weak_clear(); |
| 90 } |
| 91 std::sort(examples->begin(), examples->end(), ExampleTitleCompare()); |
| 92 return examples.Pass(); |
| 93 } |
| 94 |
| 95 } // namespace |
| 96 |
| 46 // Model for the examples that are being added via AddExample(). | 97 // Model for the examples that are being added via AddExample(). |
| 47 class ComboboxModelExampleList : public ui::ComboboxModel { | 98 class ComboboxModelExampleList : public ui::ComboboxModel { |
| 48 public: | 99 public: |
| 49 ComboboxModelExampleList() {} | 100 ComboboxModelExampleList() {} |
| 50 virtual ~ComboboxModelExampleList() {} | 101 virtual ~ComboboxModelExampleList() {} |
| 51 | 102 |
| 103 void SetExamples(ScopedExamples examples) { |
| 104 example_list_.swap(*examples); |
| 105 } |
| 106 |
| 52 // Overridden from ui::ComboboxModel: | 107 // Overridden from ui::ComboboxModel: |
| 53 virtual int GetItemCount() const OVERRIDE { return example_list_.size(); } | 108 virtual int GetItemCount() const OVERRIDE { return example_list_.size(); } |
| 54 virtual base::string16 GetItemAt(int index) OVERRIDE { | 109 virtual base::string16 GetItemAt(int index) OVERRIDE { |
| 55 return base::UTF8ToUTF16(example_list_[index]->example_title()); | 110 return base::UTF8ToUTF16(example_list_[index]->example_title()); |
| 56 } | 111 } |
| 57 | 112 |
| 58 View* GetItemViewAt(int index) { | 113 View* GetItemViewAt(int index) { |
| 59 return example_list_[index]->example_view(); | 114 return example_list_[index]->example_view(); |
| 60 } | 115 } |
| 61 | 116 |
| 62 void AddExample(ExampleBase* example) { | |
| 63 example_list_.push_back(example); | |
| 64 } | |
| 65 | |
| 66 private: | 117 private: |
| 67 ScopedVector<ExampleBase> example_list_; | 118 ScopedVector<ExampleBase> example_list_; |
| 68 | 119 |
| 69 DISALLOW_COPY_AND_ASSIGN(ComboboxModelExampleList); | 120 DISALLOW_COPY_AND_ASSIGN(ComboboxModelExampleList); |
| 70 }; | 121 }; |
| 71 | 122 |
| 72 class ExamplesWindowContents : public WidgetDelegateView, | 123 class ExamplesWindowContents : public WidgetDelegateView, |
| 73 public ComboboxListener { | 124 public ComboboxListener { |
| 74 public: | 125 public: |
| 75 ExamplesWindowContents(Operation operation) | 126 ExamplesWindowContents(Operation operation, ScopedExamples examples) |
| 76 : combobox_(new Combobox(&combobox_model_)), | 127 : combobox_(new Combobox(&combobox_model_)), |
| 77 example_shown_(new View), | 128 example_shown_(new View), |
| 78 status_label_(new Label), | 129 status_label_(new Label), |
| 79 operation_(operation) { | 130 operation_(operation) { |
| 80 instance_ = this; | 131 instance_ = this; |
| 81 combobox_->set_listener(this); | 132 combobox_->set_listener(this); |
| 133 combobox_model_.SetExamples(examples.Pass()); |
| 134 |
| 135 set_background(Background::CreateStandardPanelBackground()); |
| 136 GridLayout* layout = new GridLayout(this); |
| 137 SetLayoutManager(layout); |
| 138 ColumnSet* column_set = layout->AddColumnSet(0); |
| 139 column_set->AddPaddingColumn(0, 5); |
| 140 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, |
| 141 GridLayout::USE_PREF, 0, 0); |
| 142 column_set->AddPaddingColumn(0, 5); |
| 143 layout->AddPaddingRow(0, 5); |
| 144 layout->StartRow(0 /* no expand */, 0); |
| 145 layout->AddView(combobox_); |
| 146 |
| 147 if (combobox_model_.GetItemCount() > 0) { |
| 148 layout->StartRow(1, 0); |
| 149 example_shown_->SetLayoutManager(new FillLayout()); |
| 150 example_shown_->AddChildView(combobox_model_.GetItemViewAt(0)); |
| 151 layout->AddView(example_shown_); |
| 152 } |
| 153 |
| 154 layout->StartRow(0 /* no expand */, 0); |
| 155 layout->AddView(status_label_); |
| 156 layout->AddPaddingRow(0, 5); |
| 82 } | 157 } |
| 158 |
| 83 virtual ~ExamplesWindowContents() { | 159 virtual ~ExamplesWindowContents() { |
| 84 // Delete |combobox_| first as it references |combobox_model_|. | 160 // Delete |combobox_| first as it references |combobox_model_|. |
| 85 delete combobox_; | 161 delete combobox_; |
| 86 combobox_ = NULL; | 162 combobox_ = NULL; |
| 87 } | 163 } |
| 88 | 164 |
| 89 // Prints a message in the status area, at the bottom of the window. | 165 // Prints a message in the status area, at the bottom of the window. |
| 90 void SetStatus(const std::string& status) { | 166 void SetStatus(const std::string& status) { |
| 91 status_label_->SetText(base::UTF8ToUTF16(status)); | 167 status_label_->SetText(base::UTF8ToUTF16(status)); |
| 92 } | 168 } |
| 93 | 169 |
| 94 static ExamplesWindowContents* instance() { return instance_; } | 170 static ExamplesWindowContents* instance() { return instance_; } |
| 95 | 171 |
| 96 private: | 172 private: |
| 97 // Overridden from WidgetDelegateView: | 173 // Overridden from WidgetDelegateView: |
| 98 virtual bool CanResize() const OVERRIDE { return true; } | 174 virtual bool CanResize() const OVERRIDE { return true; } |
| 99 virtual bool CanMaximize() const OVERRIDE { return true; } | 175 virtual bool CanMaximize() const OVERRIDE { return true; } |
| 100 virtual base::string16 GetWindowTitle() const OVERRIDE { | 176 virtual base::string16 GetWindowTitle() const OVERRIDE { |
| 101 return base::ASCIIToUTF16("Views Examples"); | 177 return base::ASCIIToUTF16("Views Examples"); |
| 102 } | 178 } |
| 103 virtual View* GetContentsView() OVERRIDE { return this; } | 179 virtual View* GetContentsView() OVERRIDE { return this; } |
| 104 virtual void WindowClosing() OVERRIDE { | 180 virtual void WindowClosing() OVERRIDE { |
| 105 instance_ = NULL; | 181 instance_ = NULL; |
| 106 if (operation_ == QUIT_ON_CLOSE) | 182 if (operation_ == QUIT_ON_CLOSE) |
| 107 base::MessageLoopForUI::current()->Quit(); | 183 base::MessageLoopForUI::current()->Quit(); |
| 108 } | 184 } |
| 109 | 185 |
| 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: | 186 // Overridden from ComboboxListener: |
| 118 virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { | 187 virtual void OnSelectedIndexChanged(Combobox* combobox) OVERRIDE { |
| 119 DCHECK_EQ(combobox, combobox_); | 188 DCHECK_EQ(combobox, combobox_); |
| 120 DCHECK(combobox->selected_index() < combobox_model_.GetItemCount()); | 189 DCHECK(combobox->selected_index() < combobox_model_.GetItemCount()); |
| 121 example_shown_->RemoveAllChildViews(false); | 190 example_shown_->RemoveAllChildViews(false); |
| 122 example_shown_->AddChildView(combobox_model_.GetItemViewAt( | 191 example_shown_->AddChildView(combobox_model_.GetItemViewAt( |
| 123 combobox->selected_index())); | 192 combobox->selected_index())); |
| 124 example_shown_->RequestFocus(); | 193 example_shown_->RequestFocus(); |
| 125 SetStatus(std::string()); | 194 SetStatus(std::string()); |
| 126 Layout(); | 195 Layout(); |
| 127 } | 196 } |
| 128 | 197 |
| 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_; | 198 static ExamplesWindowContents* instance_; |
| 185 ComboboxModelExampleList combobox_model_; | 199 ComboboxModelExampleList combobox_model_; |
| 186 Combobox* combobox_; | 200 Combobox* combobox_; |
| 187 View* example_shown_; | 201 View* example_shown_; |
| 188 Label* status_label_; | 202 Label* status_label_; |
| 189 const Operation operation_; | 203 const Operation operation_; |
| 190 | 204 |
| 191 DISALLOW_COPY_AND_ASSIGN(ExamplesWindowContents); | 205 DISALLOW_COPY_AND_ASSIGN(ExamplesWindowContents); |
| 192 }; | 206 }; |
| 193 | 207 |
| 194 // static | 208 // static |
| 195 ExamplesWindowContents* ExamplesWindowContents::instance_ = NULL; | 209 ExamplesWindowContents* ExamplesWindowContents::instance_ = NULL; |
| 196 | 210 |
| 197 void ShowExamplesWindow(Operation operation) { | 211 void ShowExamplesWindow(Operation operation, |
| 212 aura::Window* window_context, |
| 213 ScopedExamples extra_examples) { |
| 198 if (ExamplesWindowContents::instance()) { | 214 if (ExamplesWindowContents::instance()) { |
| 199 ExamplesWindowContents::instance()->GetWidget()->Activate(); | 215 ExamplesWindowContents::instance()->GetWidget()->Activate(); |
| 200 } else { | 216 } else { |
| 201 Widget::CreateWindowWithBounds(new ExamplesWindowContents(operation), | 217 ScopedExamples examples(GetExamplesToShow(extra_examples.Pass())); |
| 202 gfx::Rect(0, 0, 850, 300))->Show(); | 218 Widget* widget = new Widget; |
| 219 Widget::InitParams params; |
| 220 params.delegate = new ExamplesWindowContents(operation, examples.Pass()); |
| 221 params.context = window_context; |
| 222 params.bounds = gfx::Rect(0, 0, 850, 300); |
| 223 params.top_level = true; |
| 224 widget->Init(params); |
| 225 widget->Show(); |
| 203 } | 226 } |
| 204 } | 227 } |
| 205 | 228 |
| 206 void LogStatus(const std::string& string) { | 229 void LogStatus(const std::string& string) { |
| 207 ExamplesWindowContents::instance()->SetStatus(string); | 230 ExamplesWindowContents::instance()->SetStatus(string); |
| 208 } | 231 } |
| 209 | 232 |
| 210 } // namespace examples | 233 } // namespace examples |
| 211 } // namespace views | 234 } // namespace views |
| OLD | NEW |