OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "views/controls/combobox/native_combobox_win.h" | |
6 | |
7 #include "base/i18n/rtl.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "ui/base/models/combobox_model.h" | |
10 #include "ui/base/resource/resource_bundle.h" | |
11 #include "ui/base/win/hwnd_util.h" | |
12 #include "ui/gfx/font.h" | |
13 #include "ui/gfx/native_theme_win.h" | |
14 #include "ui/views/widget/widget.h" | |
15 #include "views/controls/combobox/combobox.h" | |
16 #include "views/controls/combobox/native_combobox_views.h" | |
17 | |
18 namespace views { | |
19 | |
20 // Limit how small a combobox can be. | |
21 static const int kMinComboboxWidth = 148; | |
22 | |
23 // Add a couple extra pixels to the widths of comboboxes and combobox | |
24 // dropdowns so that text isn't too crowded. | |
25 static const int kComboboxExtraPaddingX = 6; | |
26 | |
27 //////////////////////////////////////////////////////////////////////////////// | |
28 // NativeComboboxWin, public: | |
29 | |
30 NativeComboboxWin::NativeComboboxWin(Combobox* combobox) | |
31 : combobox_(combobox), | |
32 content_width_(0) { | |
33 // Associates the actual HWND with the combobox so it is the one considered as | |
34 // having the focus (not the wrapper) when the HWND is focused directly (with | |
35 // a click for example). | |
36 set_focus_view(combobox); | |
37 } | |
38 | |
39 NativeComboboxWin::~NativeComboboxWin() { | |
40 } | |
41 | |
42 //////////////////////////////////////////////////////////////////////////////// | |
43 // NativeComboboxWin, NativeComboboxWrapper implementation: | |
44 | |
45 void NativeComboboxWin::UpdateFromModel() { | |
46 SendMessage(native_view(), CB_RESETCONTENT, 0, 0); | |
47 gfx::Font font = ResourceBundle::GetSharedInstance().GetFont( | |
48 ResourceBundle::BaseFont); | |
49 int max_width = 0; | |
50 int num_items = combobox_->model()->GetItemCount(); | |
51 for (int i = 0; i < num_items; ++i) { | |
52 string16 text = combobox_->model()->GetItemAt(i); | |
53 | |
54 // Inserting the Unicode formatting characters if necessary so that the | |
55 // text is displayed correctly in right-to-left UIs. | |
56 base::i18n::AdjustStringForLocaleDirection(&text); | |
57 | |
58 SendMessage(native_view(), CB_ADDSTRING, 0, | |
59 reinterpret_cast<LPARAM>(UTF16ToWide(text).c_str())); | |
60 max_width = std::max(max_width, font.GetStringWidth(text)); | |
61 } | |
62 content_width_ = max_width; | |
63 | |
64 if (num_items > 0) { | |
65 SendMessage(native_view(), CB_SETCURSEL, combobox_->selected_item(), 0); | |
66 | |
67 // Set the width for the drop down while accounting for the scrollbar and | |
68 // borders. | |
69 if (num_items > ComboBox_GetMinVisible(native_view())) | |
70 max_width += GetSystemMetrics(SM_CXVSCROLL); | |
71 // SM_CXEDGE would not be correct here, since the dropdown is flat, not 3D. | |
72 int kComboboxDropdownBorderSize = 1; | |
73 max_width += 2 * kComboboxDropdownBorderSize + kComboboxExtraPaddingX; | |
74 SendMessage(native_view(), CB_SETDROPPEDWIDTH, max_width, 0); | |
75 } | |
76 } | |
77 | |
78 void NativeComboboxWin::UpdateSelectedItem() { | |
79 // Note that we use CB_SETCURSEL and not CB_SELECTSTRING because on RTL | |
80 // locales the strings we get from our ComboBox::Model might be augmented | |
81 // with Unicode directionality marks before we insert them into the combo box | |
82 // and therefore we can not assume that the string we get from | |
83 // ComboBox::Model can be safely searched for and selected (which is what | |
84 // CB_SELECTSTRING does). | |
85 SendMessage(native_view(), CB_SETCURSEL, combobox_->selected_item(), 0); | |
86 } | |
87 | |
88 void NativeComboboxWin::UpdateEnabled() { | |
89 SetEnabled(combobox_->IsEnabled()); | |
90 } | |
91 | |
92 int NativeComboboxWin::GetSelectedItem() const { | |
93 LRESULT selected_item = SendMessage(native_view(), CB_GETCURSEL, 0, 0); | |
94 return selected_item != CB_ERR ? selected_item : -1; | |
95 } | |
96 | |
97 bool NativeComboboxWin::IsDropdownOpen() const { | |
98 return SendMessage(native_view(), CB_GETDROPPEDSTATE, 0, 0) != 0; | |
99 } | |
100 | |
101 gfx::Size NativeComboboxWin::GetPreferredSize() { | |
102 COMBOBOXINFO cbi = { 0 }; | |
103 cbi.cbSize = sizeof(cbi); | |
104 // Note: Don't use CB_GETCOMBOBOXINFO since that crashes on WOW64 systems | |
105 // when you have a global message hook installed. | |
106 GetComboBoxInfo(native_view(), &cbi); | |
107 gfx::Rect rect_item(cbi.rcItem); | |
108 gfx::Rect rect_button(cbi.rcButton); | |
109 gfx::Size border = gfx::NativeThemeWin::instance()->GetThemeBorderSize( | |
110 gfx::NativeThemeWin::MENULIST); | |
111 | |
112 // The padding value of '3' is the xy offset from the corner of the control | |
113 // to the corner of rcItem. It does not seem to be queryable from the theme. | |
114 // It is consistent on all versions of Windows from 2K to Vista, and is | |
115 // invariant with respect to the combobox border size. We could conceivably | |
116 // get this number from rect_item.x, but it seems fragile to depend on | |
117 // position here, inside of the layout code. | |
118 const int kItemOffset = 3; | |
119 int item_to_button_distance = std::max(kItemOffset - border.width(), 0); | |
120 | |
121 // The cx computation can be read as measuring from left to right. | |
122 int pref_width = std::max(kItemOffset + content_width_ + | |
123 kComboboxExtraPaddingX + | |
124 item_to_button_distance + rect_button.width() + | |
125 border.width(), kMinComboboxWidth); | |
126 // The two arguments to ::max below should be typically be equal. | |
127 int pref_height = std::max(rect_item.height() + 2 * kItemOffset, | |
128 rect_button.height() + 2 * border.height()); | |
129 return gfx::Size(pref_width, pref_height); | |
130 } | |
131 | |
132 View* NativeComboboxWin::GetView() { | |
133 return this; | |
134 } | |
135 | |
136 void NativeComboboxWin::SetFocus() { | |
137 OnFocus(); | |
138 } | |
139 | |
140 bool NativeComboboxWin::HandleKeyPressed(const views::KeyEvent& event) { | |
141 return false; | |
142 } | |
143 | |
144 bool NativeComboboxWin::HandleKeyReleased(const views::KeyEvent& event) { | |
145 return false; | |
146 } | |
147 | |
148 void NativeComboboxWin::HandleFocus() { | |
149 } | |
150 | |
151 void NativeComboboxWin::HandleBlur() { | |
152 } | |
153 | |
154 gfx::NativeView NativeComboboxWin::GetTestingHandle() const { | |
155 return native_view(); | |
156 } | |
157 | |
158 //////////////////////////////////////////////////////////////////////////////// | |
159 // NativeComboboxWin, NativeControlWin overrides: | |
160 | |
161 bool NativeComboboxWin::ProcessMessage(UINT message, | |
162 WPARAM w_param, | |
163 LPARAM l_param, | |
164 LRESULT* result) { | |
165 if (message == WM_COMMAND && HIWORD(w_param) == CBN_SELCHANGE) { | |
166 combobox_->SelectionChanged(); | |
167 *result = 0; | |
168 return true; | |
169 } | |
170 return NativeControlWin::ProcessMessage(message, w_param, l_param, result); | |
171 } | |
172 | |
173 void NativeComboboxWin::CreateNativeControl() { | |
174 // It's ok to add WS_VSCROLL. The scrollbar will show up only when necessary | |
175 // as long as we don't use CBS_DISABLENOSCROLL. | |
176 // See http://msdn.microsoft.com/en-us/library/7h63bxbe(VS.80).aspx | |
177 DWORD flags = WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | | |
178 CBS_DROPDOWNLIST | WS_VSCROLL; | |
179 HWND control_hwnd = ::CreateWindowEx(GetAdditionalExStyle(), L"COMBOBOX", L"", | |
180 flags, 0, 0, width(), height(), | |
181 GetWidget()->GetNativeView(), NULL, NULL, | |
182 NULL); | |
183 ui::CheckWindowCreated(control_hwnd); | |
184 NativeControlCreated(control_hwnd); | |
185 } | |
186 | |
187 void NativeComboboxWin::NativeControlCreated(HWND native_control) { | |
188 NativeControlWin::NativeControlCreated(native_control); | |
189 | |
190 UpdateFont(); | |
191 UpdateFromModel(); | |
192 UpdateSelectedItem(); | |
193 } | |
194 | |
195 //////////////////////////////////////////////////////////////////////////////// | |
196 // NativeComboboxWin, private: | |
197 | |
198 void NativeComboboxWin::UpdateFont() { | |
199 HFONT font = ResourceBundle::GetSharedInstance(). | |
200 GetFont(ResourceBundle::BaseFont).GetNativeFont(); | |
201 SendMessage(native_view(), WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE); | |
202 } | |
203 | |
204 //////////////////////////////////////////////////////////////////////////////// | |
205 // NativeComboboxWrapper, public: | |
206 | |
207 // static | |
208 NativeComboboxWrapper* NativeComboboxWrapper::CreateWrapper( | |
209 Combobox* combobox) { | |
210 if (Widget::IsPureViews()) | |
211 return new NativeComboboxViews(combobox); | |
212 return new NativeComboboxWin(combobox); | |
213 } | |
214 | |
215 } // namespace views | |
OLD | NEW |