Commit 6e8974bf authored by sadrul@chromium.org's avatar sadrul@chromium.org

win32: Add a PlatformWindow implementation for Windows.

Use this implementation for mojo native_viewport. It will also be used for
aura WindowTreeHost in subsequent patches.

BUG=none
R=ben@chromium.org

Review URL: https://codereview.chromium.org/405813002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284752 0039d316-1c4b-4281-b951-d872f2087c98
parent f701987e
......@@ -329,6 +329,11 @@
'mojo_jni_headers',
],
}],
['OS=="win"', {
'dependencies': [
'../ui/platform_window/win/win_window.gyp:win_window',
],
}],
['use_x11==1', {
'dependencies': [
'../ui/platform_window/x11/x11_window.gyp:x11_window',
......
......@@ -4,156 +4,92 @@
#include "mojo/services/native_viewport/native_viewport.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/win/msg_util.h"
#include "ui/gfx/win/window_impl.h"
#include "base/memory/scoped_ptr.h"
#include "ui/gfx/rect.h"
#include "ui/platform_window/platform_window_delegate.h"
#include "ui/platform_window/win/win_window.h"
namespace mojo {
namespace services {
namespace {
gfx::Rect GetWindowBoundsForClientBounds(DWORD style, DWORD ex_style,
const gfx::Rect& bounds) {
RECT wr;
wr.left = bounds.x();
wr.top = bounds.y();
wr.right = bounds.x() + bounds.width();
wr.bottom = bounds.y() + bounds.height();
AdjustWindowRectEx(&wr, style, FALSE, ex_style);
// Make sure to keep the window onscreen, as AdjustWindowRectEx() may have
// moved part of it offscreen.
gfx::Rect window_bounds(wr.left, wr.top,
wr.right - wr.left, wr.bottom - wr.top);
window_bounds.set_x(std::max(0, window_bounds.x()));
window_bounds.set_y(std::max(0, window_bounds.y()));
return window_bounds;
}
}
class NativeViewportWin : public gfx::WindowImpl,
public NativeViewport {
class NativeViewportWin : public NativeViewport,
public ui::PlatformWindowDelegate {
public:
explicit NativeViewportWin(NativeViewportDelegate* delegate)
: delegate_(delegate) {
}
virtual ~NativeViewportWin() {
if (IsWindow(hwnd()))
DestroyWindow(hwnd());
// Destroy the platform-window while |this| is still alive.
platform_window_.reset();
}
private:
// Overridden from NativeViewport:
virtual void Init(const gfx::Rect& bounds) OVERRIDE {
gfx::Rect window_bounds = GetWindowBoundsForClientBounds(
WS_OVERLAPPEDWINDOW, window_ex_style(), bounds);
gfx::WindowImpl::Init(NULL, window_bounds);
SetWindowText(hwnd(), L"native_viewport::NativeViewportWin!");
platform_window_.reset(new ui::WinWindow(this, bounds));
}
virtual void Show() OVERRIDE {
ShowWindow(hwnd(), SW_SHOWNORMAL);
platform_window_->Show();
}
virtual void Hide() OVERRIDE {
ShowWindow(hwnd(), SW_HIDE);
platform_window_->Hide();
}
virtual void Close() OVERRIDE {
DestroyWindow(hwnd());
platform_window_->Close();
}
virtual gfx::Size GetSize() OVERRIDE {
RECT cr;
GetClientRect(hwnd(), &cr);
return gfx::Size(cr.right - cr.left, cr.bottom - cr.top);
return platform_window_->GetBounds().size();
}
virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
gfx::Rect window_bounds = GetWindowBoundsForClientBounds(
GetWindowLong(hwnd(), GWL_STYLE),
GetWindowLong(hwnd(), GWL_EXSTYLE),
bounds);
SetWindowPos(hwnd(), NULL, window_bounds.x(), window_bounds.y(),
window_bounds.width(), window_bounds.height(),
SWP_NOREPOSITION);
platform_window_->SetBounds(bounds);
}
virtual void SetCapture() OVERRIDE {
DCHECK(::GetCapture() != hwnd());
::SetCapture(hwnd());
platform_window_->SetCapture();
}
virtual void ReleaseCapture() OVERRIDE {
if (::GetCapture() == hwnd())
::ReleaseCapture();
}
CR_BEGIN_MSG_MAP_EX(NativeViewportWin)
CR_MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange)
CR_MESSAGE_HANDLER_EX(WM_KEYDOWN, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_KEYUP, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_SYSKEYDOWN, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_SYSKEYUP, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_CHAR, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_SYSCHAR, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_IME_CHAR, OnKeyEvent)
CR_MSG_WM_CREATE(OnCreate)
CR_MSG_WM_DESTROY(OnDestroy)
CR_MSG_WM_PAINT(OnPaint)
CR_MSG_WM_WINDOWPOSCHANGED(OnWindowPosChanged)
CR_END_MSG_MAP()
LRESULT OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param) {
MSG msg = { hwnd(), message, w_param, l_param, 0,
{ CR_GET_X_LPARAM(l_param), CR_GET_Y_LPARAM(l_param) } };
ui::MouseEvent event(msg);
if (ui::IsMouseEventFromTouch(message))
event.set_flags(event.flags() | ui::EF_FROM_TOUCH);
SetMsgHandled(delegate_->OnEvent(&event));
return 0;
}
LRESULT OnKeyEvent(UINT message, WPARAM w_param, LPARAM l_param) {
MSG msg = { hwnd(), message, w_param, l_param };
ui::KeyEvent event(msg, message == WM_CHAR);
SetMsgHandled(delegate_->OnEvent(&event));
return 0;
}
LRESULT OnCreate(CREATESTRUCT* create_struct) {
delegate_->OnAcceleratedWidgetAvailable(hwnd());
return 0;
}
void OnDestroy() {
platform_window_->ReleaseCapture();
}
// ui::PlatformWindowDelegate:
virtual void OnBoundsChanged(const gfx::Rect& new_bounds) OVERRIDE {
delegate_->OnBoundsChanged(new_bounds);
}
virtual void OnDamageRect(const gfx::Rect& damaged_region) OVERRIDE {
}
virtual void DispatchEvent(ui::Event* event) OVERRIDE {
delegate_->OnEvent(event);
}
virtual void OnCloseRequest() OVERRIDE {
platform_window_->Close();
}
virtual void OnClosed() OVERRIDE {
delegate_->OnDestroyed();
}
void OnPaint(HDC) {
RECT cr;
GetClientRect(hwnd(), &cr);
PAINTSTRUCT ps;
HDC dc = BeginPaint(hwnd(), &ps);
HBRUSH red_brush = CreateSolidBrush(RGB(255, 0, 0));
HGDIOBJ old_object = SelectObject(dc, red_brush);
Rectangle(dc, cr.left, cr.top, cr.right, cr.bottom);
SelectObject(dc, old_object);
DeleteObject(red_brush);
EndPaint(hwnd(), &ps);
}
void OnWindowPosChanged(WINDOWPOS* window_pos) {
if (!(window_pos->flags & SWP_NOSIZE) ||
!(window_pos->flags & SWP_NOMOVE)) {
RECT cr;
GetClientRect(hwnd(), &cr);
delegate_->OnBoundsChanged(
gfx::Rect(window_pos->x, window_pos->y,
cr.right - cr.left, cr.bottom - cr.top));
}
virtual void OnWindowStateChanged(ui::PlatformWindowState state) OVERRIDE {
}
virtual void OnLostCapture() OVERRIDE {
}
virtual void OnAcceleratedWidgetAvailable(
gfx::AcceleratedWidget widget) OVERRIDE {
delegate_->OnAcceleratedWidgetAvailable(widget);
}
scoped_ptr<ui::PlatformWindow> platform_window_;
NativeViewportDelegate* delegate_;
DISALLOW_COPY_AND_ASSIGN(NativeViewportWin);
......
......@@ -6,7 +6,6 @@
#define UI_PLATFORM_WINDOW_PLATFORM_WINDOW_H_
#include "base/memory/scoped_ptr.h"
#include "ui/platform_window/platform_window_export.h"
namespace gfx {
class Rect;
......@@ -20,7 +19,7 @@ class PlatformWindowDelegate;
//
// Each instance of PlatformWindow represents a single window in the
// underlying platform windowing system (i.e. X11/Win/OSX).
class PLATFORM_WINDOW_EXPORT PlatformWindow {
class PlatformWindow {
public:
virtual ~PlatformWindow() {}
......
......@@ -6,7 +6,6 @@
#define UI_PLATFORM_WINDOW_PLATFORM_WINDOW_DELEGATE_H_
#include "ui/gfx/native_widget_types.h"
#include "ui/platform_window/platform_window_export.h"
namespace gfx {
class Rect;
......@@ -24,7 +23,7 @@ enum PlatformWindowState {
PLATFORM_WINDOW_STATE_FULLSCREEN,
};
class PLATFORM_WINDOW_EXPORT PlatformWindowDelegate {
class PlatformWindowDelegate {
public:
virtual ~PlatformWindowDelegate() {}
......
include_rules = [
"+ui/events",
"+ui/gfx",
]
// Copyright 2014 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 "ui/platform_window/win/win_window.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/win/msg_util.h"
#include "ui/platform_window/platform_window_delegate.h"
namespace ui {
namespace {
gfx::Rect GetWindowBoundsForClientBounds(DWORD style, DWORD ex_style,
const gfx::Rect& bounds) {
RECT wr;
wr.left = bounds.x();
wr.top = bounds.y();
wr.right = bounds.x() + bounds.width();
wr.bottom = bounds.y() + bounds.height();
AdjustWindowRectEx(&wr, style, FALSE, ex_style);
// Make sure to keep the window onscreen, as AdjustWindowRectEx() may have
// moved part of it offscreen.
gfx::Rect window_bounds(wr.left, wr.top,
wr.right - wr.left, wr.bottom - wr.top);
window_bounds.set_x(std::max(0, window_bounds.x()));
window_bounds.set_y(std::max(0, window_bounds.y()));
return window_bounds;
}
} // namespace
WinWindow::WinWindow(PlatformWindowDelegate* delegate,
const gfx::Rect& bounds)
: delegate_(delegate) {
CHECK(delegate_);
gfx::Rect window_bounds = GetWindowBoundsForClientBounds(
WS_OVERLAPPEDWINDOW, window_ex_style(), bounds);
gfx::WindowImpl::Init(NULL, window_bounds);
SetWindowText(hwnd(), L"WinWindow");
}
WinWindow::~WinWindow() {
Destroy();
}
void WinWindow::Destroy() {
if (IsWindow(hwnd()))
DestroyWindow(hwnd());
}
void WinWindow::Show() {
ShowWindow(hwnd(), SW_SHOWNORMAL);
}
void WinWindow::Hide() {
ShowWindow(hwnd(), SW_HIDE);
}
void WinWindow::Close() {
Destroy();
}
void WinWindow::SetBounds(const gfx::Rect& bounds) {
gfx::Rect window_bounds = GetWindowBoundsForClientBounds(
GetWindowLong(hwnd(), GWL_STYLE),
GetWindowLong(hwnd(), GWL_EXSTYLE),
bounds);
SetWindowPos(hwnd(), NULL, window_bounds.x(), window_bounds.y(),
window_bounds.width(), window_bounds.height(),
SWP_NOREPOSITION);
}
gfx::Rect WinWindow::GetBounds() {
RECT cr;
GetClientRect(hwnd(), &cr);
return gfx::Rect(cr);
}
void WinWindow::SetCapture() {
DCHECK(::GetCapture() != hwnd());
::SetCapture(hwnd());
}
void WinWindow::ReleaseCapture() {
if (::GetCapture() == hwnd())
::ReleaseCapture();
}
void WinWindow::ToggleFullscreen() {}
void WinWindow::Maximize() {}
void WinWindow::Minimize() {}
void WinWindow::Restore() {}
LRESULT WinWindow::OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param) {
MSG msg = { hwnd(), message, w_param, l_param, 0,
{ CR_GET_X_LPARAM(l_param), CR_GET_Y_LPARAM(l_param) } };
MouseEvent event(msg);
if (IsMouseEventFromTouch(message))
event.set_flags(event.flags() | EF_FROM_TOUCH);
if (!(event.flags() & ui::EF_IS_NON_CLIENT))
delegate_->DispatchEvent(&event);
SetMsgHandled(event.handled());
return 0;
}
LRESULT WinWindow::OnCaptureChanged(UINT message,
WPARAM w_param,
LPARAM l_param) {
delegate_->OnLostCapture();
return 0;
}
LRESULT WinWindow::OnKeyEvent(UINT message, WPARAM w_param, LPARAM l_param) {
MSG msg = { hwnd(), message, w_param, l_param };
KeyEvent event(msg, message == WM_CHAR);
delegate_->DispatchEvent(&event);
SetMsgHandled(event.handled());
return 0;
}
LRESULT WinWindow::OnNCActivate(UINT message, WPARAM w_param, LPARAM l_param) {
return DefWindowProc(hwnd(), message, w_param, l_param);
}
void WinWindow::OnClose() {
delegate_->OnCloseRequest();
}
LRESULT WinWindow::OnCreate(CREATESTRUCT* create_struct) {
delegate_->OnAcceleratedWidgetAvailable(hwnd());
return 0;
}
void WinWindow::OnDestroy() {
delegate_->OnClosed();
}
void WinWindow::OnPaint(HDC) {
gfx::Rect damage_rect;
RECT update_rect = {0};
if (GetUpdateRect(hwnd(), &update_rect, FALSE))
damage_rect = gfx::Rect(update_rect);
delegate_->OnDamageRect(damage_rect);
ValidateRect(hwnd(), NULL);
}
void WinWindow::OnWindowPosChanged(WINDOWPOS* window_pos) {
if (!(window_pos->flags & SWP_NOSIZE) ||
!(window_pos->flags & SWP_NOMOVE)) {
RECT cr;
GetClientRect(hwnd(), &cr);
delegate_->OnBoundsChanged(
gfx::Rect(window_pos->x, window_pos->y,
cr.right - cr.left, cr.bottom - cr.top));
}
}
} // namespace ui
# Copyright 2014 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.
{
'variables': {
'chromium_code': 1,
},
'targets': [{
'target_name': 'win_window',
'type': '<(component)',
'dependencies': [
'../../../base/base.gyp:base',
'../../../skia/skia.gyp:skia',
'../../events/events.gyp:events',
'../../gfx/gfx.gyp:gfx',
'../../gfx/gfx.gyp:gfx_geometry',
'../platform_window.gyp:platform_window',
],
'defines': [ 'WIN_WINDOW_IMPLEMENTATION' ],
'sources': [
'win_window.cc',
'win_window.h',
'win_window_export.h',
],
}],
}
// Copyright 2014 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 UI_PLATFORM_WINDOW_WIN_WIN_WINDOW_H_
#define UI_PLATFORM_WINDOW_WIN_WIN_WINDOW_H_
#include "base/compiler_specific.h"
#include "ui/gfx/win/window_impl.h"
#include "ui/platform_window/platform_window.h"
#include "ui/platform_window/win/win_window_export.h"
namespace ui {
class WIN_WINDOW_EXPORT WinWindow : public NON_EXPORTED_BASE(PlatformWindow),
public gfx::WindowImpl {
public:
WinWindow(PlatformWindowDelegate* delegate, const gfx::Rect& bounds);
virtual ~WinWindow();
private:
void Destroy();
// PlatformWindow:
virtual void Show() OVERRIDE;
virtual void Hide() OVERRIDE;
virtual void Close() OVERRIDE;
virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
virtual gfx::Rect GetBounds() OVERRIDE;
virtual void SetCapture() OVERRIDE;
virtual void ReleaseCapture() OVERRIDE;
virtual void ToggleFullscreen() OVERRIDE;
virtual void Maximize() OVERRIDE;
virtual void Minimize() OVERRIDE;
virtual void Restore() OVERRIDE;
CR_BEGIN_MSG_MAP_EX(WinWindow)
CR_MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange)
CR_MESSAGE_RANGE_HANDLER_EX(WM_NCMOUSEMOVE,
WM_NCXBUTTONDBLCLK,
OnMouseRange)
CR_MESSAGE_HANDLER_EX(WM_CAPTURECHANGED, OnCaptureChanged)
CR_MESSAGE_HANDLER_EX(WM_KEYDOWN, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_KEYUP, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_SYSKEYDOWN, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_SYSKEYUP, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_CHAR, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_SYSCHAR, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_IME_CHAR, OnKeyEvent)
CR_MESSAGE_HANDLER_EX(WM_NCACTIVATE, OnNCActivate)
CR_MSG_WM_CLOSE(OnClose)
CR_MSG_WM_CREATE(OnCreate)
CR_MSG_WM_DESTROY(OnDestroy)
CR_MSG_WM_PAINT(OnPaint)
CR_MSG_WM_WINDOWPOSCHANGED(OnWindowPosChanged)
CR_END_MSG_MAP()
LRESULT OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param);
LRESULT OnCaptureChanged(UINT message, WPARAM w_param, LPARAM l_param);
LRESULT OnKeyEvent(UINT message, WPARAM w_param, LPARAM l_param);
LRESULT OnNCActivate(UINT message, WPARAM w_param, LPARAM l_param);
void OnClose();
LRESULT OnCreate(CREATESTRUCT* create_struct);
void OnDestroy();
void OnPaint(HDC);
void OnWindowPosChanged(WINDOWPOS* window_pos);
PlatformWindowDelegate* delegate_;
DISALLOW_COPY_AND_ASSIGN(WinWindow);
};
} // namespace ui
#endif // UI_PLATFORM_WINDOW_WIN_WIN_WINDOW_H_
// Copyright 2014 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 UI_PLATFORM_WINDOW_WIN_WIN_WINDOW_EXPORT_H_
#define UI_PLATFORM_WINDOW_WIN_WIN_WINDOW_EXPORT_H_
#if defined(COMPONENT_BUILD)
#if defined(WIN32)
#if defined(WIN_WINDOW_IMPLEMENTATION)
#define WIN_WINDOW_EXPORT __declspec(dllexport)
#else
#define WIN_WINDOW_EXPORT __declspec(dllimport)
#endif // defined(WIN_WINDOW_IMPLEMENTATION)
#else // defined(WIN32)
#if defined(WIN_WINDOW_IMPLEMENTATION)
#define WIN_WINDOW_EXPORT __attribute__((visibility("default")))
#else
#define WIN_WINDOW_EXPORT
#endif
#endif
#else // defined(COMPONENT_BUILD)
#define WIN_WINDOW_EXPORT
#endif
#endif // UI_PLATFORM_WINDOW_WIN_WIN_WINDOW_EXPORT_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment