• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #include "ui/events/event.h"
8 #include "ui/gfx/win/window_impl.h"
9 
10 namespace mojo {
11 namespace services {
12 
13 class NativeViewportWin : public gfx::WindowImpl,
14                           public NativeViewport {
15  public:
NativeViewportWin(NativeViewportDelegate * delegate)16   explicit NativeViewportWin(NativeViewportDelegate* delegate)
17       : delegate_(delegate) {
18   }
~NativeViewportWin()19   virtual ~NativeViewportWin() {
20     if (IsWindow(hwnd()))
21       DestroyWindow(hwnd());
22   }
23 
24  private:
25   // Overridden from NativeViewport:
GetSize()26   virtual gfx::Size GetSize() OVERRIDE {
27     RECT cr;
28     GetClientRect(hwnd(), &cr);
29     return gfx::Size(cr.right - cr.left, cr.bottom - cr.top);
30   }
31 
Init()32   virtual void Init() OVERRIDE {
33     gfx::WindowImpl::Init(NULL, gfx::Rect(10, 10, 500, 500));
34     ShowWindow(hwnd(), SW_SHOWNORMAL);
35     SetWindowText(hwnd(), L"native_viewport::NativeViewportWin!");
36   }
37 
Close()38   virtual void Close() OVERRIDE {
39     DestroyWindow(hwnd());
40   }
41 
SetCapture()42   virtual void SetCapture() OVERRIDE {
43     DCHECK(::GetCapture() != hwnd());
44     ::SetCapture(hwnd());
45   }
46 
ReleaseCapture()47   virtual void ReleaseCapture() OVERRIDE {
48     if (::GetCapture() == hwnd())
49       ::ReleaseCapture();
50   }
51 
52   BEGIN_MSG_MAP_EX(NativeViewportWin)
MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST,WM_MOUSELAST,OnMouseRange)53     MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange)
54 
55     MSG_WM_CREATE(OnCreate)
56     MSG_WM_PAINT(OnPaint)
57     MSG_WM_SIZE(OnSize)
58     MSG_WM_DESTROY(OnDestroy)
59   END_MSG_MAP()
60 
61   LRESULT OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param) {
62     MSG msg = { hwnd(), message, w_param, l_param, 0,
63                 { GET_X_LPARAM(l_param), GET_Y_LPARAM(l_param) } };
64     ui::MouseEvent event(msg);
65     bool handled = delegate_->OnEvent(&event);
66     SetMsgHandled(handled);
67     return 0;
68   }
OnCreate(CREATESTRUCT * create_struct)69   LRESULT OnCreate(CREATESTRUCT* create_struct) {
70     delegate_->OnAcceleratedWidgetAvailable(hwnd());
71     return 0;
72   }
OnPaint(HDC)73   void OnPaint(HDC) {
74     RECT cr;
75     GetClientRect(hwnd(), &cr);
76 
77     PAINTSTRUCT ps;
78     HDC dc = BeginPaint(hwnd(), &ps);
79     HBRUSH red_brush = CreateSolidBrush(RGB(255, 0, 0));
80     HGDIOBJ old_object = SelectObject(dc, red_brush);
81     Rectangle(dc, cr.left, cr.top, cr.right, cr.bottom);
82     SelectObject(dc, old_object);
83     DeleteObject(red_brush);
84     EndPaint(hwnd(), &ps);
85   }
OnSize(UINT param,const CSize & size)86   void OnSize(UINT param, const CSize& size) {
87     delegate_->OnResized(gfx::Size(size.cx, size.cy));
88   }
OnDestroy()89   void OnDestroy() {
90     delegate_->OnDestroyed();
91   }
92 
93   NativeViewportDelegate* delegate_;
94 
95   DISALLOW_COPY_AND_ASSIGN(NativeViewportWin);
96 };
97 
98 // static
Create(shell::Context * context,NativeViewportDelegate * delegate)99 scoped_ptr<NativeViewport> NativeViewport::Create(
100     shell::Context* context,
101     NativeViewportDelegate* delegate) {
102   return scoped_ptr<NativeViewport>(new NativeViewportWin(delegate)).Pass();
103 }
104 
105 }  // namespace services
106 }  // namespace mojo
107