summaryrefslogtreecommitdiffstats
path: root/views/examples/widget_example.h
blob: 85a71111ed6b36cfa98c01812a4da242d2e79dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_
#define VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_

#include "views/background.h"
#include "views/controls/button/text_button.h"
#include "views/examples/example_base.h"
#include "views/fill_layout.h"
#include "views/view.h"
#include "views/widget/root_view.h"
#include "views/widget/widget.h"

namespace examples {

// WidgetExample demonstrates how to create a popup widget.
class WidgetExample : public ExampleBase, public views::ButtonListener {
 public:
  explicit WidgetExample(ExamplesMain* main) : ExampleBase(main) {
  }

  virtual ~WidgetExample() {}

  virtual std::wstring GetExampleTitle() {
    return L"Widget";
  }

  virtual void CreateExampleView(views::View* container) {
    create_button_ = new views::TextButton(this, L"Create a popup widget");
    container->SetLayoutManager(new views::FillLayout);
    container->AddChildView(create_button_);
    // We'll create close_button_ and popup_widget_, when the create button
    // is clicked.
    close_button_ = NULL;
    popup_widget_ = NULL;
  }

 private:
  // ButtonListner implementation.
  virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
    if (sender == create_button_) {
      // Disable the create button.
      create_button_->SetEnabled(false);

      // Create a popup widget.
      popup_widget_ = views::Widget::CreatePopupWidget(
        views::Widget::NotTransparent,
        views::Widget::AcceptEvents,
        views::Widget::DeleteOnDestroy);

      // Compute where to place the popup widget.
      // We'll place it right below the create_button_.
      gfx::Point point = create_button_->GetPosition();
      // The position in point is relative to the parent. Make it absolute.
      views::View::ConvertPointToScreen(create_button_, &point);
      // Add the height of create_button_.
      point.Offset(0, create_button_->size().height());
      gfx::Rect bounds(point.x(), point.y(), 200, 300);

      // Initialize the popup widget with the computed bounds.
      popup_widget_->Init(NULL, bounds);

      // Add a button to close the popup widget.
      close_button_ = new views::TextButton(this, L"Close");
      views::View* widget_container = new views::View;
      widget_container->SetLayoutManager(new views::FillLayout);
      widget_container->set_background(
        views::Background::CreateStandardPanelBackground());
      widget_container->AddChildView(close_button_);
      popup_widget_->GetRootView()->SetContentsView(widget_container);

      // Show the popup widget.
      popup_widget_->Show();
    } else if (sender == close_button_) {
      // Close the popup widget. This will delete popup_widget_ as
      // DeleteOnDestroy is specified when the widget was created.
      // Views on the widget will also be deleted.
      popup_widget_->Close();
      // Re-enable the create button.
      create_button_->SetEnabled(true);
      close_button_ = NULL;
      popup_widget_ = NULL;
    }
  }

  views::TextButton* create_button_;
  views::TextButton* close_button_;
  views::Widget* popup_widget_;

  DISALLOW_COPY_AND_ASSIGN(WidgetExample);
};

}  // namespace examples

#endif  // VIEWS_EXAMPLES_WIDGET_EXAMPLE_H_