1 // Copyright (c) 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 "ui/views/win/hwnd_util.h"
6 
7 #include "base/i18n/rtl.h"
8 #include "base/trace_event/base_tracing.h"
9 #include "ui/aura/window.h"
10 #include "ui/aura/window_tree_host.h"
11 #include "ui/views/widget/widget.h"
12 
13 namespace views {
14 
HWNDForView(const View * view)15 HWND HWNDForView(const View* view) {
16   return view->GetWidget() ? HWNDForWidget(view->GetWidget()) : nullptr;
17 }
18 
HWNDForWidget(const Widget * widget)19 HWND HWNDForWidget(const Widget* widget) {
20   return HWNDForNativeWindow(widget->GetNativeWindow());
21 }
22 
HWNDForNativeView(const gfx::NativeView view)23 HWND HWNDForNativeView(const gfx::NativeView view) {
24   return view && view->GetRootWindow() ? view->GetHost()->GetAcceleratedWidget()
25                                        : nullptr;
26 }
27 
HWNDForNativeWindow(const gfx::NativeWindow window)28 HWND HWNDForNativeWindow(const gfx::NativeWindow window) {
29   return window && window->GetRootWindow()
30              ? window->GetHost()->GetAcceleratedWidget()
31              : nullptr;
32 }
33 
GetWindowBoundsForClientBounds(View * view,const gfx::Rect & client_bounds)34 gfx::Rect GetWindowBoundsForClientBounds(View* view,
35                                          const gfx::Rect& client_bounds) {
36   DCHECK(view);
37   aura::WindowTreeHost* host = view->GetWidget()->GetNativeWindow()->GetHost();
38   if (host) {
39     HWND hwnd = host->GetAcceleratedWidget();
40     RECT rect = client_bounds.ToRECT();
41     DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
42     DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
43     AdjustWindowRectEx(&rect, style, FALSE, ex_style);
44     return gfx::Rect(rect);
45   }
46   return client_bounds;
47 }
48 
ShowSystemMenuAtScreenPixelLocation(HWND window,const gfx::Point & point)49 void ShowSystemMenuAtScreenPixelLocation(HWND window, const gfx::Point& point) {
50   TRACE_EVENT0("ui", "ShowSystemMenuAtScreenPixelLocation");
51 
52   UINT flags = TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD;
53   if (base::i18n::IsRTL())
54     flags |= TPM_RIGHTALIGN;
55   HMENU menu = GetSystemMenu(window, FALSE);
56 
57   const int command =
58       TrackPopupMenu(menu, flags, point.x(), point.y(), 0, window, nullptr);
59 
60   if (command)
61     SendMessage(window, WM_SYSCOMMAND, command, 0);
62 }
63 
64 }  // namespace views
65