1 // Copyright 2013 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 "mojo/services/native_viewport/native_viewport.h"
6 #include "ui/events/event.h"
7 #include "ui/gfx/win/window_impl.h"
12 class NativeViewportWin : public gfx::WindowImpl,
13 public NativeViewport {
15 explicit NativeViewportWin(NativeViewportDelegate* delegate)
16 : delegate_(delegate) {
17 Init(NULL, gfx::Rect(10, 10, 500, 500));
18 ShowWindow(hwnd(), SW_SHOWNORMAL);
19 SetWindowText(hwnd(), L"native_viewport::NativeViewportWin!");
21 virtual ~NativeViewportWin() {
23 DestroyWindow(hwnd());
27 // Overridden from NativeViewport:
28 virtual void Close() OVERRIDE {
29 DestroyWindow(hwnd());
32 BEGIN_MSG_MAP_EX(NativeViewportWin)
33 MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange)
37 MSG_WM_DESTROY(OnDestroy)
40 LRESULT OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param) {
41 MSG msg = { hwnd(), message, w_param, l_param, 0,
42 { GET_X_LPARAM(l_param), GET_Y_LPARAM(l_param) } };
43 ui::MouseEvent event(msg);
44 bool handled = delegate_->OnEvent(&event);
45 SetMsgHandled(handled);
50 GetClientRect(hwnd(), &cr);
53 HDC dc = BeginPaint(hwnd(), &ps);
54 HBRUSH red_brush = CreateSolidBrush(RGB(255, 0, 0));
55 HGDIOBJ old_object = SelectObject(dc, red_brush);
56 Rectangle(dc, cr.left, cr.top, cr.right, cr.bottom);
57 SelectObject(dc, old_object);
58 DeleteObject(red_brush);
59 EndPaint(hwnd(), &ps);
61 void OnSize(UINT param, const CSize& size) {
62 delegate_->OnResized(gfx::Size(size.cx, size.cy));
65 delegate_->OnDestroyed();
68 NativeViewportDelegate* delegate_;
70 DISALLOW_COPY_AND_ASSIGN(NativeViewportWin);
74 scoped_ptr<NativeViewport> NativeViewport::Create(
75 shell::Context* context,
76 NativeViewportDelegate* delegate) {
77 return scoped_ptr<NativeViewport>(new NativeViewportWin(delegate)).Pass();
80 } // namespace services