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