This source file includes following definitions.
- UsingNativeTheme
- GetImageSkiaNamed
- GetColor
- GetDisplayProperty
- ShouldUseNativeFrame
- HasCustomImage
- GetRawData
- did_gdi_clear_
- GetSystemMenu
- AsDesktopWindowTreeHost
- GetMinimizeButtonOffset
- UsesNativeSystemMenu
- GetInitialShowState
- GetClientAreaInsets
- HandleCreate
- HandleFrameChanged
- PreHandleMSG
- PostHandleMSG
- IsUsingCustomFrame
- ShouldUseNativeFrame
- FrameTypeChanged
- UpdateDWMFrame
- GetDWMFrameMargins
- CreateBrowserDesktopWindowTreeHost
#include "chrome/browser/ui/views/frame/browser_desktop_window_tree_host_win.h"
#include <dwmapi.h>
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/views/frame/browser_frame.h"
#include "chrome/browser/ui/views/frame/browser_frame_common_win.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/browser_window_property_manager_win.h"
#include "chrome/browser/ui/views/frame/system_menu_insertion_delegate_win.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/theme_image_mapper.h"
#include "grit/theme_resources.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/win/dpi.h"
#include "ui/views/controls/menu/native_menu_win.h"
#pragma comment(lib, "dwmapi.lib")
namespace {
const int kClientEdgeThickness = 3;
const int kDWMFrameTopOffset = 3;
class DesktopThemeProvider : public ui::ThemeProvider {
public:
explicit DesktopThemeProvider(ui::ThemeProvider* delegate)
: delegate_(delegate) {
}
virtual bool UsingNativeTheme() const OVERRIDE {
return delegate_->UsingNativeTheme();
}
virtual gfx::ImageSkia* GetImageSkiaNamed(int id) const OVERRIDE {
return delegate_->GetImageSkiaNamed(
chrome::MapThemeImage(chrome::HOST_DESKTOP_TYPE_NATIVE, id));
}
virtual SkColor GetColor(int id) const OVERRIDE {
return delegate_->GetColor(id);
}
virtual int GetDisplayProperty(int id) const OVERRIDE {
return delegate_->GetDisplayProperty(id);
}
virtual bool ShouldUseNativeFrame() const OVERRIDE {
return delegate_->ShouldUseNativeFrame();
}
virtual bool HasCustomImage(int id) const OVERRIDE {
return delegate_->HasCustomImage(
chrome::MapThemeImage(chrome::HOST_DESKTOP_TYPE_NATIVE, id));
}
virtual base::RefCountedMemory* GetRawData(
int id,
ui::ScaleFactor scale_factor) const OVERRIDE {
return delegate_->GetRawData(id, scale_factor);
}
private:
ui::ThemeProvider* delegate_;
DISALLOW_COPY_AND_ASSIGN(DesktopThemeProvider);
};
}
BrowserDesktopWindowTreeHostWin::BrowserDesktopWindowTreeHostWin(
views::internal::NativeWidgetDelegate* native_widget_delegate,
views::DesktopNativeWidgetAura* desktop_native_widget_aura,
BrowserView* browser_view,
BrowserFrame* browser_frame)
: DesktopWindowTreeHostWin(native_widget_delegate,
desktop_native_widget_aura),
browser_view_(browser_view),
browser_frame_(browser_frame),
did_gdi_clear_(false) {
scoped_ptr<ui::ThemeProvider> theme_provider(
new DesktopThemeProvider(ThemeServiceFactory::GetForProfile(
browser_view->browser()->profile())));
browser_frame->SetThemeProvider(theme_provider.Pass());
}
BrowserDesktopWindowTreeHostWin::~BrowserDesktopWindowTreeHostWin() {
}
views::NativeMenuWin* BrowserDesktopWindowTreeHostWin::GetSystemMenu() {
if (!system_menu_.get()) {
SystemMenuInsertionDelegateWin insertion_delegate;
system_menu_.reset(
new views::NativeMenuWin(browser_frame_->GetSystemMenuModel(),
GetHWND()));
system_menu_->Rebuild(&insertion_delegate);
}
return system_menu_.get();
}
views::DesktopWindowTreeHost*
BrowserDesktopWindowTreeHostWin::AsDesktopWindowTreeHost() {
return this;
}
int BrowserDesktopWindowTreeHostWin::GetMinimizeButtonOffset() const {
return minimize_button_metrics_.GetMinimizeButtonOffsetX();
}
bool BrowserDesktopWindowTreeHostWin::UsesNativeSystemMenu() const {
return true;
}
int BrowserDesktopWindowTreeHostWin::GetInitialShowState() const {
STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
GetStartupInfo(&si);
return si.wShowWindow;
}
bool BrowserDesktopWindowTreeHostWin::GetClientAreaInsets(
gfx::Insets* insets) const {
if (!GetWidget()->ShouldUseNativeFrame() ||
!browser_view_->IsBrowserTypeNormal()) {
return false;
}
int border_thickness = GetSystemMetrics(SM_CXSIZEFRAME);
if (GetWidget()->IsFullscreen())
border_thickness = 0;
else if (!IsMaximized())
border_thickness -= kClientEdgeThickness;
insets->Set(0, border_thickness, border_thickness, border_thickness);
return true;
}
void BrowserDesktopWindowTreeHostWin::HandleCreate() {
DesktopWindowTreeHostWin::HandleCreate();
browser_window_property_manager_ =
BrowserWindowPropertyManager::CreateBrowserWindowPropertyManager(
browser_view_);
if (browser_window_property_manager_)
browser_window_property_manager_->UpdateWindowProperties(GetHWND());
}
void BrowserDesktopWindowTreeHostWin::HandleFrameChanged() {
browser_view_->InitStatusBubble();
UpdateDWMFrame();
DesktopWindowTreeHostWin::HandleFrameChanged();
}
bool BrowserDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
WPARAM w_param,
LPARAM l_param,
LRESULT* result) {
switch (message) {
case WM_ACTIVATE:
if (LOWORD(w_param) != WA_INACTIVE)
minimize_button_metrics_.OnHWNDActivated();
return false;
case WM_ENDSESSION:
chrome::SessionEnding();
return true;
case WM_INITMENUPOPUP:
GetSystemMenu()->UpdateStates();
return true;
}
return DesktopWindowTreeHostWin::PreHandleMSG(
message, w_param, l_param, result);
}
void BrowserDesktopWindowTreeHostWin::PostHandleMSG(UINT message,
WPARAM w_param,
LPARAM l_param) {
switch (message) {
case WM_CREATE:
minimize_button_metrics_.Init(GetHWND());
break;
case WM_WINDOWPOSCHANGED: {
UpdateDWMFrame();
WINDOWPOS* window_pos = reinterpret_cast<WINDOWPOS*>(l_param);
if (window_pos->flags & SWP_SHOWWINDOW && GetWidget()->non_client_view()) {
GetWidget()->non_client_view()->Layout();
GetWidget()->non_client_view()->SchedulePaint();
}
break;
}
case WM_ERASEBKGND:
if (!did_gdi_clear_ && DesktopWindowTreeHostWin::ShouldUseNativeFrame()) {
HDC dc = GetDC(GetHWND());
MARGINS margins = GetDWMFrameMargins();
RECT client_rect;
GetClientRect(GetHWND(), &client_rect);
HBRUSH brush = CreateSolidBrush(0);
RECT rect = { 0, 0, client_rect.right, margins.cyTopHeight };
FillRect(dc, &rect, brush);
DeleteObject(brush);
ReleaseDC(GetHWND(), dc);
did_gdi_clear_ = true;
}
break;
}
}
bool BrowserDesktopWindowTreeHostWin::IsUsingCustomFrame() const {
if (!browser_view_->IsBrowserTypeNormal() &&
!DesktopWindowTreeHostWin::IsUsingCustomFrame()) {
return false;
}
return !GetWidget()->GetThemeProvider()->ShouldUseNativeFrame();
}
bool BrowserDesktopWindowTreeHostWin::ShouldUseNativeFrame() const {
if (!views::DesktopWindowTreeHostWin::ShouldUseNativeFrame())
return false;
if (!browser_view_->browser())
return false;
return chrome::ShouldUseNativeFrame(browser_view_,
GetWidget()->GetThemeProvider());
}
void BrowserDesktopWindowTreeHostWin::FrameTypeChanged() {
views::DesktopWindowTreeHostWin::FrameTypeChanged();
did_gdi_clear_ = false;
}
void BrowserDesktopWindowTreeHostWin::UpdateDWMFrame() {
if (!GetWidget()->client_view() || !browser_view_->IsBrowserTypeNormal() ||
!DesktopWindowTreeHostWin::ShouldUseNativeFrame())
return;
MARGINS margins = GetDWMFrameMargins();
DwmExtendFrameIntoClientArea(GetHWND(), &margins);
}
MARGINS BrowserDesktopWindowTreeHostWin::GetDWMFrameMargins() const {
MARGINS margins = { 0 };
if (GetWidget()->ShouldUseNativeFrame()) {
if (!IsMaximized() && !GetWidget()->IsFullscreen()) {
margins.cxLeftWidth = kClientEdgeThickness + 1;
margins.cxRightWidth = kClientEdgeThickness + 1;
margins.cyBottomHeight = kClientEdgeThickness + 1;
margins.cyTopHeight = kClientEdgeThickness + 1;
}
if (!browser_view_->IsFullscreen()) {
gfx::Rect tabstrip_bounds(
browser_frame_->GetBoundsForTabStrip(browser_view_->tabstrip()));
tabstrip_bounds = gfx::win::DIPToScreenRect(tabstrip_bounds);
margins.cyTopHeight = tabstrip_bounds.bottom() + kDWMFrameTopOffset;
}
}
return margins;
}
BrowserDesktopWindowTreeHost*
BrowserDesktopWindowTreeHost::CreateBrowserDesktopWindowTreeHost(
views::internal::NativeWidgetDelegate* native_widget_delegate,
views::DesktopNativeWidgetAura* desktop_native_widget_aura,
BrowserView* browser_view,
BrowserFrame* browser_frame) {
return new BrowserDesktopWindowTreeHostWin(native_widget_delegate,
desktop_native_widget_aura,
browser_view,
browser_frame);
}