1 // Copyright (c) 2009 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 #ifndef VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_
6 #define VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_
8 #include "views/background.h"
9 #include "views/controls/button/text_button.h"
10 #include "views/examples/example_base.h"
11 #include "views/fill_layout.h"
12 #include "views/view.h"
13 #include "views/widget/root_view.h"
14 #include "views/widget/widget.h"
18 // WidgetExample demonstrates how to create a popup widget.
19 class WidgetExample
: public ExampleBase
, public views::ButtonListener
{
21 explicit WidgetExample(ExamplesMain
* main
) : ExampleBase(main
) {
24 virtual ~WidgetExample() {}
26 virtual std::wstring
GetExampleTitle() {
30 virtual void CreateExampleView(views::View
* container
) {
31 create_button_
= new views::TextButton(this, L
"Create a popup widget");
32 container
->SetLayoutManager(new views::FillLayout
);
33 container
->AddChildView(create_button_
);
34 // We'll create close_button_ and popup_widget_, when the create button
41 // ButtonListner implementation.
42 virtual void ButtonPressed(views::Button
* sender
, const views::Event
& event
) {
43 if (sender
== create_button_
) {
44 // Disable the create button.
45 create_button_
->SetEnabled(false);
47 // Create a popup widget.
48 popup_widget_
= views::Widget::CreatePopupWidget(
49 views::Widget::NotTransparent
,
50 views::Widget::AcceptEvents
,
51 views::Widget::DeleteOnDestroy
);
53 // Compute where to place the popup widget.
54 // We'll place it right below the create_button_.
55 gfx::Point point
= create_button_
->GetPosition();
56 // The position in point is relative to the parent. Make it absolute.
57 views::View::ConvertPointToScreen(create_button_
, &point
);
58 // Add the height of create_button_.
59 point
.Offset(0, create_button_
->size().height());
60 gfx::Rect
bounds(point
.x(), point
.y(), 200, 300);
62 // Initialize the popup widget with the computed bounds.
63 popup_widget_
->Init(NULL
, bounds
);
65 // Add a button to close the popup widget.
66 close_button_
= new views::TextButton(this, L
"Close");
67 views::View
* widget_container
= new views::View
;
68 widget_container
->SetLayoutManager(new views::FillLayout
);
69 widget_container
->set_background(
70 views::Background::CreateStandardPanelBackground());
71 widget_container
->AddChildView(close_button_
);
72 popup_widget_
->GetRootView()->SetContentsView(widget_container
);
74 // Show the popup widget.
75 popup_widget_
->Show();
76 } else if (sender
== close_button_
) {
77 // Close the popup widget. This will delete popup_widget_ as
78 // DeleteOnDestroy is specified when the widget was created.
79 // Views on the widget will also be deleted.
80 popup_widget_
->Close();
81 // Re-enable the create button.
82 create_button_
->SetEnabled(true);
88 views::TextButton
* create_button_
;
89 views::TextButton
* close_button_
;
90 views::Widget
* popup_widget_
;
92 DISALLOW_COPY_AND_ASSIGN(WidgetExample
);
95 } // namespace examples
97 #endif // VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_