1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
// Copyright (c) 2009 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 "views/controls/native_control_win.h"
#include "app/l10n_util_win.h"
#include "base/logging.h"
#include "base/win_util.h"
namespace views {
// static
const wchar_t* NativeControlWin::kNativeControlWinKey =
L"__NATIVE_CONTROL_WIN__";
static const wchar_t* kNativeControlOriginalWndProcKey =
L"__NATIVE_CONTROL_ORIGINAL_WNDPROC__";
// static
WNDPROC NativeControlWin::original_wndproc_ = NULL;
////////////////////////////////////////////////////////////////////////////////
// NativeControlWin, public:
NativeControlWin::NativeControlWin() : HWNDView() {
}
NativeControlWin::~NativeControlWin() {
HWND hwnd = GetHWND();
if (hwnd) {
// Destroy the hwnd if it still exists. Otherwise we won't have shut things
// down correctly, leading to leaking and crashing if another message
// comes in for the hwnd.
Detach();
DestroyWindow(hwnd);
}
}
bool NativeControlWin::ProcessMessage(UINT message, WPARAM w_param,
LPARAM l_param, LRESULT* result) {
switch (message) {
case WM_CONTEXTMENU:
ShowContextMenu(gfx::Point(LOWORD(l_param), HIWORD(l_param)));
*result = 0;
return true;
case WM_CTLCOLORBTN:
case WM_CTLCOLORSTATIC:
*result = GetControlColor(message, reinterpret_cast<HDC>(w_param),
GetHWND());
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// NativeControlWin, View overrides:
void NativeControlWin::SetEnabled(bool enabled) {
if (IsEnabled() != enabled) {
View::SetEnabled(enabled);
if (GetHWND())
EnableWindow(GetHWND(), IsEnabled());
}
}
void NativeControlWin::ViewHierarchyChanged(bool is_add, View* parent,
View* child) {
// Create the HWND when we're added to a valid Widget. Many controls need a
// parent HWND to function properly.
if (is_add && GetWidget() && !GetHWND())
CreateNativeControl();
// Call the base class to hide the view if we're being removed.
HWNDView::ViewHierarchyChanged(is_add, parent, child);
}
void NativeControlWin::VisibilityChanged(View* starting_from, bool is_visible) {
if (!is_visible) {
// We destroy the child control HWND when we become invisible because of the
// performance cost of maintaining many HWNDs.
HWND hwnd = GetHWND();
Detach();
DestroyWindow(hwnd);
} else if (!GetHWND()) {
CreateNativeControl();
}
}
void NativeControlWin::Focus() {
DCHECK(GetHWND());
SetFocus(GetHWND());
}
////////////////////////////////////////////////////////////////////////////////
// NativeControlWin, protected:
void NativeControlWin::ShowContextMenu(const gfx::Point& location) {
if (!GetContextMenuController())
return;
int x = location.x();
int y = location.y();
bool is_mouse = true;
if (x == -1 && y == -1) {
gfx::Point point = GetKeyboardContextMenuLocation();
x = point.x();
y = point.y();
is_mouse = false;
}
View::ShowContextMenu(x, y, is_mouse);
}
void NativeControlWin::NativeControlCreated(HWND native_control) {
// Associate this object with the control's HWND so that WidgetWin can find
// this object when it receives messages from it.
SetProp(native_control, kNativeControlWinKey, this);
// Subclass the window so we can monitor for key presses.
original_wndproc_ =
win_util::SetWindowProc(native_control,
&NativeControlWin::NativeControlWndProc);
SetProp(native_control, kNativeControlOriginalWndProcKey, original_wndproc_);
Attach(native_control);
// GetHWND() is now valid.
// Update the newly created HWND with any resident enabled state.
EnableWindow(GetHWND(), IsEnabled());
// This message ensures that the focus border is shown.
SendMessage(GetHWND(), WM_CHANGEUISTATE,
MAKEWPARAM(UIS_CLEAR, UISF_HIDEFOCUS), 0);
}
DWORD NativeControlWin::GetAdditionalExStyle() const {
// If the UI for the view is mirrored, we should make sure we add the
// extended window style for a right-to-left layout so the subclass creates
// a mirrored HWND for the underlying control.
DWORD ex_style = 0;
if (UILayoutIsRightToLeft())
ex_style |= l10n_util::GetExtendedStyles();
return ex_style;
}
DWORD NativeControlWin::GetAdditionalRTLStyle() const {
// If the UI for the view is mirrored, we should make sure we add the
// extended window style for a right-to-left layout so the subclass creates
// a mirrored HWND for the underlying control.
DWORD ex_style = 0;
if (UILayoutIsRightToLeft())
ex_style |= l10n_util::GetExtendedTooltipStyles();
return ex_style;
}
////////////////////////////////////////////////////////////////////////////////
// NativeControlWin, private:
LRESULT NativeControlWin::GetControlColor(UINT message, HDC dc, HWND sender) {
View *ancestor = this;
while (ancestor) {
const Background* background = ancestor->background();
if (background) {
HBRUSH brush = background->GetNativeControlBrush();
if (brush)
return reinterpret_cast<LRESULT>(brush);
}
ancestor = ancestor->GetParent();
}
// COLOR_BTNFACE is the default for dialog box backgrounds.
return reinterpret_cast<LRESULT>(GetSysColorBrush(COLOR_BTNFACE));
}
// static
LRESULT NativeControlWin::NativeControlWndProc(HWND window,
UINT message,
WPARAM w_param,
LPARAM l_param) {
NativeControlWin* native_control =
static_cast<NativeControlWin*>(GetProp(window, kNativeControlWinKey));
DCHECK(native_control);
if (message == WM_KEYDOWN && native_control->NotifyOnKeyDown()) {
if (native_control->OnKeyDown(static_cast<int>(w_param)))
return 0;
} else if (message == WM_DESTROY) {
win_util::SetWindowProc(window, native_control->original_wndproc_);
RemoveProp(window, kNativeControlWinKey);
}
return CallWindowProc(native_control->original_wndproc_, window, message,
w_param, l_param);
}
} // namespace views
|