- add sources.
[platform/framework/web/crosswalk.git] / src / mojo / services / native_viewport / native_viewport_win.cc
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.
4
5 #include "mojo/services/native_viewport/native_viewport.h"
6 #include "ui/events/event.h"
7 #include "ui/gfx/win/window_impl.h"
8
9 namespace mojo {
10 namespace services {
11
12 class NativeViewportWin : public gfx::WindowImpl,
13                           public NativeViewport {
14  public:
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!");
20   }
21   virtual ~NativeViewportWin() {
22     if (IsWindow(hwnd()))
23       DestroyWindow(hwnd());
24   }
25
26  private:
27   // Overridden from NativeViewport:
28   virtual void Close() OVERRIDE {
29     DestroyWindow(hwnd());
30   }
31
32   BEGIN_MSG_MAP_EX(NativeViewportWin)
33     MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange)
34
35     MSG_WM_PAINT(OnPaint)
36     MSG_WM_SIZE(OnSize)
37     MSG_WM_DESTROY(OnDestroy)
38   END_MSG_MAP()
39
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);
46     return 0;
47   }
48   void OnPaint(HDC) {
49     RECT cr;
50     GetClientRect(hwnd(), &cr);
51
52     PAINTSTRUCT ps;
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);
60   }
61   void OnSize(UINT param, const CSize& size) {
62     delegate_->OnResized(gfx::Size(size.cx, size.cy));
63   }
64   void OnDestroy() {
65     delegate_->OnDestroyed();
66   }
67
68   NativeViewportDelegate* delegate_;
69
70   DISALLOW_COPY_AND_ASSIGN(NativeViewportWin);
71 };
72
73 // static
74 scoped_ptr<NativeViewport> NativeViewport::Create(
75     shell::Context* context,
76     NativeViewportDelegate* delegate) {
77   return scoped_ptr<NativeViewport>(new NativeViewportWin(delegate)).Pass();
78 }
79
80 }  // namespace services
81 }  // namespace mojo