summaryrefslogtreecommitdiffstats
path: root/views/examples/native_widget_views_example.cc
blob: 067cf01198007fc4fc844ee63d3d82231d73ba92 (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
// Copyright (c) 2011 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.

#include "views/examples/native_widget_views_example.h"

#include "ui/gfx/canvas.h"
#include "views/controls/button/text_button.h"
#include "views/examples/example_base.h"
#include "views/view.h"
#include "views/widget/widget.h"
#include "views/widget/native_widget_views.h"

namespace examples {

// A ContentView for our example widget. Contains a variety of controls for
// testing NativeWidgetViews event handling. If any part of the Widget's bounds
// are rendered red, something went wrong.
class TestContentView : public views::View,
                        public views::ButtonListener {
 public:
  TestContentView()
      : click_count_(0),
        ALLOW_THIS_IN_INITIALIZER_LIST(
            button_(new views::TextButton(this, L"Click me!"))) {
    AddChildView(button_);
  }
  virtual ~TestContentView() {
  }

  // Overridden from views::View:
  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
    SkColor color = click_count_ % 2 == 0 ? SK_ColorGREEN : SK_ColorBLUE;
    canvas->FillRectInt(color, 0, 0, width(), height());
  }
  virtual void Layout() OVERRIDE {
    button_->SetBounds(10, 10, width() - 20, height() - 20);
  }

  // Overridden from views::ButtonListener:
  virtual void ButtonPressed(views::Button* sender,
                             const views::Event& event) OVERRIDE {
    if (sender == button_) {
      ++click_count_;
      SchedulePaint();
    }
  }

 private:
  int click_count_;
  views::TextButton* button_;

  DISALLOW_COPY_AND_ASSIGN(TestContentView);
};

NativeWidgetViewsExample::NativeWidgetViewsExample(ExamplesMain* main)
    : ExampleBase(main) {
}

NativeWidgetViewsExample::~NativeWidgetViewsExample() {
}

std::wstring NativeWidgetViewsExample::GetExampleTitle() {
  return L"NativeWidgetViews";
}

void NativeWidgetViewsExample::CreateExampleView(views::View* container) {
  views::Widget* widget = new views::Widget;
  views::NativeWidgetViews* nwv = new views::NativeWidgetViews(widget);
  views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
  params.native_widget = nwv;
  widget->Init(params);
  widget->SetContentsView(new TestContentView);
  widget->SetBounds(gfx::Rect(10, 10, 300, 150));
}

}  // namespace examples