1 // Copyright (c) 2012 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 "chrome/browser/ui/views/frame/browser_frame_win.h"
11 #include "base/command_line.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/win/metro.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search_engines/util.h"
18 #include "chrome/browser/ui/browser_commands.h"
19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/browser/ui/views/frame/browser_frame_common_win.h"
22 #include "chrome/browser/ui/views/frame/browser_view.h"
23 #include "chrome/browser/ui/views/frame/browser_window_property_manager_win.h"
24 #include "chrome/browser/ui/views/frame/system_menu_insertion_delegate_win.h"
25 #include "chrome/browser/ui/views/tabs/tab_strip.h"
26 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "content/public/browser/browser_accessibility_state.h"
29 #include "content/public/browser/page_navigator.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/common/page_transition_types.h"
32 #include "grit/generated_resources.h"
33 #include "grit/theme_resources.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/layout.h"
36 #include "ui/base/models/simple_menu_model.h"
37 #include "ui/base/resource/resource_bundle.h"
38 #include "ui/base/theme_provider.h"
39 #include "ui/base/window_open_disposition.h"
40 #include "ui/gfx/win/dpi.h"
41 #include "ui/views/controls/menu/native_menu_win.h"
42 #include "ui/views/views_delegate.h"
43 #include "ui/views/widget/native_widget_win.h"
44 #include "ui/views/widget/widget.h"
45 #include "ui/views/win/hwnd_message_handler.h"
46 #include "ui/views/window/non_client_view.h"
48 #include "win8/util/win8_util.h"
50 #pragma comment(lib, "dwmapi.lib")
53 static const int kClientEdgeThickness = 3;
54 static const int kTabDragWindowAlpha = 200;
55 // We need to offset the DWMFrame into the toolbar so that the blackness
56 // doesn't show up on our rounded corners.
57 static const int kDWMFrameTopOffset = 3;
58 // If not -1, windows are shown with this state.
59 static int explicit_show_state = -1;
61 using content::OpenURLParams;
62 using content::Referrer;
63 using content::WebContents;
65 #if !defined(USE_AURA)
67 // Windows metro exported functions from metro_driver.
68 typedef void (*SetFrameWindow)(HWND window);
69 typedef void (*CloseFrameWindow)(HWND window);
70 typedef void (*FlipFrameWindows)();
71 typedef void (*MetroSetFullscreen)(bool fullscreen);
75 views::Button* MakeWindowSwitcherButton(views::ButtonListener* listener,
76 bool is_off_the_record) {
77 views::ImageButton* switcher_button = new views::ImageButton(listener);
78 // The button in the incognito window has the hot-cold images inverted
79 // with respect to the regular browser window.
80 switcher_button->SetImage(
81 views::ImageButton::STATE_NORMAL,
82 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
83 is_off_the_record ? IDR_INCOGNITO_SWITCH_ON :
84 IDR_INCOGNITO_SWITCH_OFF));
85 switcher_button->SetImage(
86 views::ImageButton::STATE_HOVERED,
87 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
88 is_off_the_record ? IDR_INCOGNITO_SWITCH_OFF :
89 IDR_INCOGNITO_SWITCH_ON));
90 switcher_button->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
91 views::ImageButton::ALIGN_MIDDLE);
92 return switcher_button;
95 ///////////////////////////////////////////////////////////////////////////////
96 // BrowserFrameWin, public:
98 BrowserFrameWin::BrowserFrameWin(BrowserFrame* browser_frame,
99 BrowserView* browser_view)
100 : views::NativeWidgetWin(browser_frame),
101 browser_view_(browser_view),
102 browser_frame_(browser_frame) {
103 if (win8::IsSingleWindowMetroMode()) {
104 browser_view->SetWindowSwitcherButton(
105 MakeWindowSwitcherButton(this, browser_view->IsOffTheRecord()));
109 BrowserFrameWin::~BrowserFrameWin() {
113 void BrowserFrameWin::SetShowState(int state) {
114 explicit_show_state = state;
117 void BrowserFrameWin::AdjustFrameForImmersiveMode() {
118 #if defined(USE_AURA)
121 HMODULE metro = base::win::GetMetroModule();
124 // We are in metro mode.
125 browser_frame_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM);
126 SetFrameWindow set_frame_window = reinterpret_cast<SetFrameWindow>(
127 ::GetProcAddress(metro, "SetFrameWindow"));
128 set_frame_window(browser_frame_->GetNativeWindow());
131 void BrowserFrameWin::CloseImmersiveFrame() {
132 #if defined(USE_AURA)
135 HMODULE metro = base::win::GetMetroModule();
138 CloseFrameWindow close_frame_window = reinterpret_cast<CloseFrameWindow>(
139 ::GetProcAddress(metro, "CloseFrameWindow"));
140 close_frame_window(browser_frame_->GetNativeWindow());
144 views::NativeMenuWin* BrowserFrameWin::GetSystemMenu() {
145 if (!system_menu_.get()) {
146 SystemMenuInsertionDelegateWin insertion_delegate;
148 new views::NativeMenuWin(browser_frame_->GetSystemMenuModel(),
150 system_menu_->Rebuild(&insertion_delegate);
152 return system_menu_.get();
155 ///////////////////////////////////////////////////////////////////////////////
156 // BrowserFrameWin, views::NativeWidgetWin overrides:
158 int BrowserFrameWin::GetInitialShowState() const {
159 if (explicit_show_state != -1)
160 return explicit_show_state;
162 STARTUPINFO si = {0};
164 si.dwFlags = STARTF_USESHOWWINDOW;
166 return si.wShowWindow;
169 bool BrowserFrameWin::GetClientAreaInsets(gfx::Insets* insets) const {
170 // Use the default client insets for an opaque frame or a glass popup/app
172 if (!GetWidget()->ShouldUseNativeFrame() ||
173 !browser_view_->IsBrowserTypeNormal()) {
177 int border_thickness = GetSystemMetrics(SM_CXSIZEFRAME);
178 // In fullscreen mode, we have no frame. In restored mode, we draw our own
179 // client edge over part of the default frame.
181 border_thickness = 0;
182 else if (!IsMaximized())
183 border_thickness -= kClientEdgeThickness;
184 insets->Set(0, border_thickness, border_thickness, border_thickness);
188 void BrowserFrameWin::HandleCreate() {
189 NativeWidgetWin::HandleCreate();
190 browser_window_property_manager_ =
191 BrowserWindowPropertyManager::CreateBrowserWindowPropertyManager(
193 if (browser_window_property_manager_) {
194 browser_window_property_manager_->UpdateWindowProperties(
195 GetMessageHandler()->hwnd());
199 void BrowserFrameWin::HandleFrameChanged() {
200 // Handle window frame layout changes, then set the updated glass region.
201 NativeWidgetWin::HandleFrameChanged();
205 bool BrowserFrameWin::PreHandleMSG(UINT message,
209 static const UINT metro_navigation_search_message =
210 RegisterWindowMessage(chrome::kMetroNavigationAndSearchMessage);
212 static const UINT metro_get_current_tab_info_message =
213 RegisterWindowMessage(chrome::kMetroGetCurrentTabInfoMessage);
215 if (message == metro_navigation_search_message) {
216 HandleMetroNavSearchRequest(w_param, l_param);
218 } else if (message == metro_get_current_tab_info_message) {
219 GetMetroCurrentTabInfo(w_param);
225 if (LOWORD(w_param) != WA_INACTIVE)
226 minimize_button_metrics_.OnHWNDActivated();
229 if (win8::IsSingleWindowMetroMode()) {
230 // This message is sent by the AnimateWindow API which is used in metro
231 // mode to flip between active chrome windows.
232 RECT client_rect = {0};
233 ::GetClientRect(GetNativeView(), &client_rect);
234 HDC dest_dc = reinterpret_cast<HDC>(w_param);
236 HDC src_dc = ::GetDC(GetNativeView());
237 ::BitBlt(dest_dc, 0, 0, client_rect.right - client_rect.left,
238 client_rect.bottom - client_rect.top, src_dc, 0, 0,
240 ::ReleaseDC(GetNativeView(), src_dc);
246 chrome::SessionEnding();
248 case WM_INITMENUPOPUP:
249 GetSystemMenu()->UpdateStates();
255 void BrowserFrameWin::PostHandleMSG(UINT message,
260 minimize_button_metrics_.Init(GetNativeView());
262 case WM_WINDOWPOSCHANGED:
265 // Windows lies to us about the position of the minimize button before a
266 // window is visible. We use this position to place the OTR avatar in RTL
267 // mode, so when the window is shown, we need to re-layout and schedule a
268 // paint for the non-client frame view so that the icon top has the correct
269 // position when the window becomes visible. This fixes bugs where the icon
270 // appears to overlay the minimize button.
271 // Note that we will call Layout every time SetWindowPos is called with
272 // SWP_SHOWWINDOW, however callers typically are careful about not
273 // specifying this flag unless necessary to avoid flicker.
274 // This may be invoked during creation on XP and before the non_client_view
276 WINDOWPOS* window_pos = reinterpret_cast<WINDOWPOS*>(l_param);
277 if (window_pos->flags & SWP_SHOWWINDOW && GetWidget()->non_client_view()) {
278 GetWidget()->non_client_view()->Layout();
279 GetWidget()->non_client_view()->SchedulePaint();
285 bool BrowserFrameWin::ShouldUseNativeFrame() const {
286 if (!NativeWidgetWin::ShouldUseNativeFrame())
288 return chrome::ShouldUseNativeFrame(browser_view_,
289 GetWidget()->GetThemeProvider());
292 void BrowserFrameWin::Show() {
293 AdjustFrameForImmersiveMode();
294 views::NativeWidgetWin::Show();
297 void BrowserFrameWin::ShowMaximizedWithBounds(
298 const gfx::Rect& restored_bounds) {
299 AdjustFrameForImmersiveMode();
300 views::NativeWidgetWin::ShowMaximizedWithBounds(restored_bounds);
303 void BrowserFrameWin::ShowWithWindowState(ui::WindowShowState show_state) {
304 AdjustFrameForImmersiveMode();
305 views::NativeWidgetWin::ShowWithWindowState(show_state);
308 void BrowserFrameWin::Close() {
309 CloseImmersiveFrame();
310 views::NativeWidgetWin::Close();
313 void BrowserFrameWin::FrameTypeChanged() {
314 // In Windows 8 metro mode the frame type is set to FRAME_TYPE_FORCE_CUSTOM
315 // by default. We reset it back to FRAME_TYPE_DEFAULT to ensure that we
316 // don't end up defaulting to BrowserNonClientFrameView in all cases.
317 if (win8::IsSingleWindowMetroMode())
318 browser_frame_->set_frame_type(views::Widget::FRAME_TYPE_DEFAULT);
320 views::NativeWidgetWin::FrameTypeChanged();
322 // In Windows 8 metro mode we call Show on the BrowserFrame instance to
323 // ensure that the window can be styled appropriately, i.e. no sysmenu,
325 if (win8::IsSingleWindowMetroMode())
329 void BrowserFrameWin::SetFullscreen(bool fullscreen) {
330 if (win8::IsSingleWindowMetroMode()) {
331 HMODULE metro = base::win::GetMetroModule();
333 MetroSetFullscreen set_full_screen = reinterpret_cast<MetroSetFullscreen>(
334 ::GetProcAddress(metro, "SetFullscreen"));
335 DCHECK(set_full_screen);
337 set_full_screen(fullscreen);
339 NOTREACHED() << "Failed to get metro driver module";
342 views::NativeWidgetWin::SetFullscreen(fullscreen);
345 void BrowserFrameWin::Activate() {
346 // In Windows 8 metro mode we have only one window visible at any given time.
347 // The Activate code path is typically called when a new browser window is
348 // being activated. In metro we need to ensure that the window currently
349 // being displayed is hidden and the new window being activated becomes
350 // visible. This is achieved by calling AdjustFrameForImmersiveMode()
351 // followed by ShowWindow().
352 if (win8::IsSingleWindowMetroMode()) {
353 AdjustFrameForImmersiveMode();
354 ::ShowWindow(browser_frame_->GetNativeWindow(), SW_SHOWNORMAL);
356 views::NativeWidgetWin::Activate();
361 ////////////////////////////////////////////////////////////////////////////////
362 // BrowserFrameWin, NativeBrowserFrame implementation:
364 views::NativeWidget* BrowserFrameWin::AsNativeWidget() {
368 const views::NativeWidget* BrowserFrameWin::AsNativeWidget() const {
372 bool BrowserFrameWin::UsesNativeSystemMenu() const {
376 int BrowserFrameWin::GetMinimizeButtonOffset() const {
377 return minimize_button_metrics_.GetMinimizeButtonOffsetX();
380 void BrowserFrameWin::TabStripDisplayModeChanged() {
384 void BrowserFrameWin::ButtonPressed(views::Button* sender,
385 const ui::Event& event) {
386 HMODULE metro = base::win::GetMetroModule();
390 // Toggle the profile and switch to the corresponding browser window in the
391 // profile. The GetOffTheRecordProfile function is documented to create an
392 // incognito profile if one does not exist. That is not a concern as the
393 // windows 8 window switcher button shows up on the caption only when a
394 // normal window and an incognito window are open simultaneously.
395 Profile* profile_to_switch_to = NULL;
396 Profile* current_profile = browser_view()->browser()->profile();
397 if (current_profile->IsOffTheRecord())
398 profile_to_switch_to = current_profile->GetOriginalProfile();
400 profile_to_switch_to = current_profile->GetOffTheRecordProfile();
402 DCHECK(profile_to_switch_to);
404 Browser* browser_to_switch_to = chrome::FindTabbedBrowser(
405 profile_to_switch_to, false, chrome::HOST_DESKTOP_TYPE_NATIVE);
407 DCHECK(browser_to_switch_to);
409 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(
410 browser_to_switch_to);
412 // Tell the metro_driver to switch to the Browser we found above. This
413 // causes the current browser window to be hidden.
414 SetFrameWindow set_frame_window = reinterpret_cast<SetFrameWindow>(
415 ::GetProcAddress(metro, "SetFrameWindow"));
416 set_frame_window(browser_view->frame()->GetNativeWindow());
417 ::ShowWindow(browser_view->frame()->GetNativeWindow(), SW_SHOWNORMAL);
420 ///////////////////////////////////////////////////////////////////////////////
421 // BrowserFrameWin, private:
423 void BrowserFrameWin::UpdateDWMFrame() {
424 // For "normal" windows on Aero, we always need to reset the glass area
425 // correctly, even if we're not currently showing the native frame (e.g.
426 // because a theme is showing), so we explicitly check for that case rather
427 // than checking browser_frame_->ShouldUseNativeFrame() here. Using that here
428 // would mean we wouldn't reset the glass area to zero when moving from the
429 // native frame to an opaque frame, leading to graphical glitches behind the
430 // opaque frame. Instead, we use that function below to tell us whether the
431 // frame is currently native or opaque.
432 if (!GetWidget()->client_view() || !browser_view_->IsBrowserTypeNormal() ||
433 !NativeWidgetWin::ShouldUseNativeFrame())
436 MARGINS margins = { 0 };
438 // If the opaque frame is visible, we use the default (zero) margins.
439 // Otherwise, we need to figure out how to extend the glass in.
440 if (browser_frame_->ShouldUseNativeFrame()) {
441 // In fullscreen mode, we don't extend glass into the client area at all,
442 // because the GDI-drawn text in the web content composited over it will
443 // become semi-transparent over any glass area.
444 if (!IsMaximized() && !IsFullscreen()) {
445 margins.cxLeftWidth = kClientEdgeThickness + 1;
446 margins.cxRightWidth = kClientEdgeThickness + 1;
447 margins.cyBottomHeight = kClientEdgeThickness + 1;
448 margins.cyTopHeight = kClientEdgeThickness + 1;
450 // In maximized mode, we only have a titlebar strip of glass, no side/bottom
452 if (!IsFullscreen()) {
453 gfx::Rect tabstrip_bounds(
454 browser_frame_->GetBoundsForTabStrip(browser_view_->tabstrip()));
455 tabstrip_bounds = gfx::win::DIPToScreenRect(tabstrip_bounds);
456 margins.cyTopHeight = tabstrip_bounds.bottom() + kDWMFrameTopOffset;
460 DwmExtendFrameIntoClientArea(GetNativeView(), &margins);
463 void BrowserFrameWin::HandleMetroNavSearchRequest(WPARAM w_param,
465 if (!base::win::IsMetroProcess()) {
466 NOTREACHED() << "Received unexpected metro navigation request";
470 if (!w_param && !l_param) {
471 NOTREACHED() << "Invalid metro request parameters";
475 Browser* browser = browser_view()->browser();
480 request_url = GURL(reinterpret_cast<const wchar_t*>(w_param));
481 } else if (l_param) {
482 request_url = GetDefaultSearchURLForSearchTerms(
483 browser->profile(), reinterpret_cast<const wchar_t*>(l_param));
485 if (request_url.is_valid()) {
486 browser->OpenURL(OpenURLParams(request_url, Referrer(), NEW_FOREGROUND_TAB,
487 content::PAGE_TRANSITION_TYPED, false));
491 void BrowserFrameWin::GetMetroCurrentTabInfo(WPARAM w_param) {
492 if (!base::win::IsMetroProcess()) {
493 NOTREACHED() << "Received unexpected metro request";
498 NOTREACHED() << "Invalid metro request parameter";
502 base::win::CurrentTabInfo* current_tab_info =
503 reinterpret_cast<base::win::CurrentTabInfo*>(w_param);
505 Browser* browser = browser_view()->browser();
508 // We allocate memory for the title and url via LocalAlloc. The caller has to
509 // free the memory via LocalFree.
510 current_tab_info->title = base::win::LocalAllocAndCopyString(
511 browser->GetWindowTitleForCurrentTab());
513 WebContents* current_tab = browser->tab_strip_model()->GetActiveWebContents();
516 current_tab_info->url = base::win::LocalAllocAndCopyString(
517 UTF8ToWide(current_tab->GetURL().spec()));
520 ////////////////////////////////////////////////////////////////////////////////
521 // BrowserFrame, public:
523 bool BrowserFrame::ShouldLeaveOffsetNearTopBorder() {
524 if (win8::IsSingleWindowMetroMode()) {
525 if (ui::GetDisplayLayout() == ui::LAYOUT_DESKTOP)
528 return !IsMaximized();