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.
5 #include "ui/views/examples/examples_window.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/base/models/combobox_model.h"
13 #include "ui/base/ui_base_paths.h"
14 #include "ui/views/background.h"
15 #include "ui/views/controls/combobox/combobox.h"
16 #include "ui/views/controls/label.h"
17 #include "ui/views/examples/bubble_example.h"
18 #include "ui/views/examples/button_example.h"
19 #include "ui/views/examples/checkbox_example.h"
20 #include "ui/views/examples/combobox_example.h"
21 #include "ui/views/examples/double_split_view_example.h"
22 #include "ui/views/examples/label_example.h"
23 #include "ui/views/examples/link_example.h"
24 #include "ui/views/examples/menu_example.h"
25 #include "ui/views/examples/message_box_example.h"
26 #include "ui/views/examples/multiline_example.h"
27 #include "ui/views/examples/progress_bar_example.h"
28 #include "ui/views/examples/radio_button_example.h"
29 #include "ui/views/examples/scroll_view_example.h"
30 #include "ui/views/examples/single_split_view_example.h"
31 #include "ui/views/examples/slider_example.h"
32 #include "ui/views/examples/tabbed_pane_example.h"
33 #include "ui/views/examples/table_example.h"
34 #include "ui/views/examples/text_example.h"
35 #include "ui/views/examples/textfield_example.h"
36 #include "ui/views/examples/throbber_example.h"
37 #include "ui/views/examples/tree_view_example.h"
38 #include "ui/views/examples/vector_example.h"
39 #include "ui/views/examples/widget_example.h"
40 #include "ui/views/layout/fill_layout.h"
41 #include "ui/views/layout/grid_layout.h"
42 #include "ui/views/widget/widget.h"
43 #include "ui/views/widget/widget_delegate.h"
48 typedef scoped_ptr
<ScopedVector
<ExampleBase
> > ScopedExamples
;
52 // Creates the default set of examples. Caller owns the result.
53 ScopedExamples
CreateExamples() {
54 ScopedExamples
examples(new ScopedVector
<ExampleBase
>);
55 examples
->push_back(new BubbleExample
);
56 examples
->push_back(new ButtonExample
);
57 examples
->push_back(new CheckboxExample
);
58 examples
->push_back(new ComboboxExample
);
59 examples
->push_back(new DoubleSplitViewExample
);
60 examples
->push_back(new LabelExample
);
61 examples
->push_back(new LinkExample
);
62 examples
->push_back(new MenuExample
);
63 examples
->push_back(new MessageBoxExample
);
64 examples
->push_back(new MultilineExample
);
65 examples
->push_back(new ProgressBarExample
);
66 examples
->push_back(new RadioButtonExample
);
67 examples
->push_back(new ScrollViewExample
);
68 examples
->push_back(new SingleSplitViewExample
);
69 examples
->push_back(new SliderExample
);
70 examples
->push_back(new TabbedPaneExample
);
71 examples
->push_back(new TableExample
);
72 examples
->push_back(new TextExample
);
73 examples
->push_back(new TextfieldExample
);
74 examples
->push_back(new ThrobberExample
);
75 examples
->push_back(new TreeViewExample
);
76 examples
->push_back(new VectorExample
);
77 examples
->push_back(new WidgetExample
);
78 return examples
.Pass();
81 struct ExampleTitleCompare
{
82 bool operator() (ExampleBase
* a
, ExampleBase
* b
) {
83 return a
->example_title() < b
->example_title();
87 ScopedExamples
GetExamplesToShow(ScopedExamples extra
) {
88 ScopedExamples
examples(CreateExamples());
90 examples
->insert(examples
->end(), extra
->begin(), extra
->end());
93 std::sort(examples
->begin(), examples
->end(), ExampleTitleCompare());
94 return examples
.Pass();
99 // Model for the examples that are being added via AddExample().
100 class ComboboxModelExampleList
: public ui::ComboboxModel
{
102 ComboboxModelExampleList() {}
103 ~ComboboxModelExampleList() override
{}
105 void SetExamples(ScopedExamples examples
) {
106 example_list_
.swap(*examples
);
109 // ui::ComboboxModel:
110 int GetItemCount() const override
{ return example_list_
.size(); }
111 base::string16
GetItemAt(int index
) override
{
112 return base::UTF8ToUTF16(example_list_
[index
]->example_title());
115 View
* GetItemViewAt(int index
) {
116 return example_list_
[index
]->example_view();
120 ScopedVector
<ExampleBase
> example_list_
;
122 DISALLOW_COPY_AND_ASSIGN(ComboboxModelExampleList
);
125 class ExamplesWindowContents
: public WidgetDelegateView
,
126 public ComboboxListener
{
128 ExamplesWindowContents(Operation operation
, ScopedExamples examples
)
129 : combobox_(new Combobox(&combobox_model_
)),
130 example_shown_(new View
),
131 status_label_(new Label
),
132 operation_(operation
) {
134 combobox_
->set_listener(this);
135 combobox_model_
.SetExamples(examples
.Pass());
136 combobox_
->ModelChanged();
138 set_background(Background::CreateStandardPanelBackground());
139 GridLayout
* layout
= new GridLayout(this);
140 SetLayoutManager(layout
);
141 ColumnSet
* column_set
= layout
->AddColumnSet(0);
142 column_set
->AddPaddingColumn(0, 5);
143 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
144 GridLayout::USE_PREF
, 0, 0);
145 column_set
->AddPaddingColumn(0, 5);
146 layout
->AddPaddingRow(0, 5);
147 layout
->StartRow(0 /* no expand */, 0);
148 layout
->AddView(combobox_
);
150 if (combobox_model_
.GetItemCount() > 0) {
151 layout
->StartRow(1, 0);
152 example_shown_
->SetLayoutManager(new FillLayout());
153 example_shown_
->AddChildView(combobox_model_
.GetItemViewAt(0));
154 layout
->AddView(example_shown_
);
157 layout
->StartRow(0 /* no expand */, 0);
158 layout
->AddView(status_label_
);
159 layout
->AddPaddingRow(0, 5);
162 ~ExamplesWindowContents() override
{
163 // Delete |combobox_| first as it references |combobox_model_|.
168 // Prints a message in the status area, at the bottom of the window.
169 void SetStatus(const std::string
& status
) {
170 status_label_
->SetText(base::UTF8ToUTF16(status
));
173 static ExamplesWindowContents
* instance() { return instance_
; }
176 // WidgetDelegateView:
177 bool CanResize() const override
{ return true; }
178 bool CanMaximize() const override
{ return true; }
179 bool CanMinimize() const override
{ return true; }
180 base::string16
GetWindowTitle() const override
{
181 return base::ASCIIToUTF16("Views Examples");
183 View
* GetContentsView() override
{ return this; }
184 void WindowClosing() override
{
186 if (operation_
== QUIT_ON_CLOSE
)
187 base::MessageLoopForUI::current()->Quit();
191 void OnPerformAction(Combobox
* combobox
) override
{
192 DCHECK_EQ(combobox
, combobox_
);
193 DCHECK(combobox
->selected_index() < combobox_model_
.GetItemCount());
194 example_shown_
->RemoveAllChildViews(false);
195 example_shown_
->AddChildView(combobox_model_
.GetItemViewAt(
196 combobox
->selected_index()));
197 example_shown_
->RequestFocus();
198 SetStatus(std::string());
202 static ExamplesWindowContents
* instance_
;
203 ComboboxModelExampleList combobox_model_
;
205 View
* example_shown_
;
206 Label
* status_label_
;
207 const Operation operation_
;
209 DISALLOW_COPY_AND_ASSIGN(ExamplesWindowContents
);
213 ExamplesWindowContents
* ExamplesWindowContents::instance_
= NULL
;
215 void ShowExamplesWindow(Operation operation
,
216 gfx::NativeWindow window_context
,
217 ScopedExamples extra_examples
) {
218 if (ExamplesWindowContents::instance()) {
219 ExamplesWindowContents::instance()->GetWidget()->Activate();
221 ScopedExamples
examples(GetExamplesToShow(extra_examples
.Pass()));
222 Widget
* widget
= new Widget
;
223 Widget::InitParams params
;
224 params
.delegate
= new ExamplesWindowContents(operation
, examples
.Pass());
225 params
.context
= window_context
;
226 params
.bounds
= gfx::Rect(0, 0, 850, 300);
227 widget
->Init(params
);
232 void LogStatus(const std::string
& string
) {
233 ExamplesWindowContents::instance()->SetStatus(string
);
236 } // namespace examples