OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. |
| 4 |
| 5 #include "views/controls/combobox/native_combobox_win.h" |
| 6 |
| 7 #include "app/gfx/font.h" |
| 8 #include "app/l10n_util.h" |
| 9 #include "app/resource_bundle.h" |
| 10 #include "base/gfx/native_theme.h" |
| 11 #include "views/controls/combobox/combobox.h" |
| 12 #include "views/widget/widget.h" |
| 13 |
| 14 namespace views { |
| 15 |
| 16 // Limit how small a combobox can be. |
| 17 static const int kMinComboboxWidth = 148; |
| 18 |
| 19 // Add a couple extra pixels to the widths of comboboxes and combobox |
| 20 // dropdowns so that text isn't too crowded. |
| 21 static const int kComboboxExtraPaddingX = 6; |
| 22 |
| 23 //////////////////////////////////////////////////////////////////////////////// |
| 24 // NativeComboboxWin, public: |
| 25 |
| 26 NativeComboboxWin::NativeComboboxWin(Combobox* combobox) |
| 27 : combobox_(combobox), |
| 28 content_width_(0) { |
| 29 } |
| 30 |
| 31 NativeComboboxWin::~NativeComboboxWin() { |
| 32 } |
| 33 |
| 34 //////////////////////////////////////////////////////////////////////////////// |
| 35 // NativeComboboxWin, NativeComboboxWrapper implementation: |
| 36 |
| 37 void NativeComboboxWin::UpdateFromModel() { |
| 38 SendMessage(native_view(), CB_RESETCONTENT, 0, 0); |
| 39 gfx::Font font = ResourceBundle::GetSharedInstance().GetFont( |
| 40 ResourceBundle::BaseFont); |
| 41 int max_width = 0; |
| 42 int num_items = combobox_->model()->GetItemCount(combobox_); |
| 43 for (int i = 0; i < num_items; ++i) { |
| 44 const std::wstring& text = combobox_->model()->GetItemAt(combobox_, i); |
| 45 |
| 46 // Inserting the Unicode formatting characters if necessary so that the |
| 47 // text is displayed correctly in right-to-left UIs. |
| 48 std::wstring localized_text; |
| 49 const wchar_t* text_ptr = text.c_str(); |
| 50 if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text)) |
| 51 text_ptr = localized_text.c_str(); |
| 52 |
| 53 SendMessage(native_view(), CB_ADDSTRING, 0, |
| 54 reinterpret_cast<LPARAM>(text_ptr)); |
| 55 max_width = std::max(max_width, font.GetStringWidth(text)); |
| 56 } |
| 57 content_width_ = max_width; |
| 58 |
| 59 if (num_items > 0) { |
| 60 SendMessage(native_view(), CB_SETCURSEL, combobox_->selected_item(), 0); |
| 61 |
| 62 // Set the width for the drop down while accounting for the scrollbar and |
| 63 // borders. |
| 64 if (num_items > ComboBox_GetMinVisible(native_view())) |
| 65 max_width += GetSystemMetrics(SM_CXVSCROLL); |
| 66 // SM_CXEDGE would not be correct here, since the dropdown is flat, not 3D. |
| 67 int kComboboxDropdownBorderSize = 1; |
| 68 max_width += 2 * kComboboxDropdownBorderSize + kComboboxExtraPaddingX; |
| 69 SendMessage(native_view(), CB_SETDROPPEDWIDTH, max_width, 0); |
| 70 } |
| 71 } |
| 72 |
| 73 void NativeComboboxWin::UpdateSelectedItem() { |
| 74 // Note that we use CB_SETCURSEL and not CB_SELECTSTRING because on RTL |
| 75 // locales the strings we get from our ComboBox::Model might be augmented |
| 76 // with Unicode directionality marks before we insert them into the combo box |
| 77 // and therefore we can not assume that the string we get from |
| 78 // ComboBox::Model can be safely searched for and selected (which is what |
| 79 // CB_SELECTSTRING does). |
| 80 SendMessage(native_view(), CB_SETCURSEL, combobox_->selected_item(), 0); |
| 81 } |
| 82 |
| 83 void NativeComboboxWin::UpdateEnabled() { |
| 84 SetEnabled(combobox_->IsEnabled()); |
| 85 } |
| 86 |
| 87 int NativeComboboxWin::GetSelectedItem() const { |
| 88 LRESULT selected_item = SendMessage(native_view(), CB_GETCURSEL, 0, 0); |
| 89 return selected_item != CB_ERR ? selected_item : -1; |
| 90 } |
| 91 |
| 92 bool NativeComboboxWin::IsDropdownOpen() const { |
| 93 return SendMessage(native_view(), CB_GETDROPPEDSTATE, 0, 0) != 0; |
| 94 } |
| 95 |
| 96 gfx::Size NativeComboboxWin::GetPreferredSize() const { |
| 97 COMBOBOXINFO cbi = { 0 }; |
| 98 cbi.cbSize = sizeof(cbi); |
| 99 // Note: Don't use CB_GETCOMBOBOXINFO since that crashes on WOW64 systems |
| 100 // when you have a global message hook installed. |
| 101 GetComboBoxInfo(native_view(), &cbi); |
| 102 gfx::Rect rect_item(cbi.rcItem); |
| 103 gfx::Rect rect_button(cbi.rcButton); |
| 104 gfx::Size border = gfx::NativeTheme::instance()->GetThemeBorderSize( |
| 105 gfx::NativeTheme::MENULIST); |
| 106 |
| 107 // The padding value of '3' is the xy offset from the corner of the control |
| 108 // to the corner of rcItem. It does not seem to be queryable from the theme. |
| 109 // It is consistent on all versions of Windows from 2K to Vista, and is |
| 110 // invariant with respect to the combobox border size. We could conceivably |
| 111 // get this number from rect_item.x, but it seems fragile to depend on |
| 112 // position here, inside of the layout code. |
| 113 const int kItemOffset = 3; |
| 114 int item_to_button_distance = std::max(kItemOffset - border.width(), 0); |
| 115 |
| 116 // The cx computation can be read as measuring from left to right. |
| 117 int pref_width = std::max(kItemOffset + content_width_ + |
| 118 kComboboxExtraPaddingX + |
| 119 item_to_button_distance + rect_button.width() + |
| 120 border.width(), kMinComboboxWidth); |
| 121 // The two arguments to ::max below should be typically be equal. |
| 122 int pref_height = std::max(rect_item.height() + 2 * kItemOffset, |
| 123 rect_button.height() + 2 * border.height()); |
| 124 return gfx::Size(pref_width, pref_height); |
| 125 } |
| 126 |
| 127 View* NativeComboboxWin::GetView() { |
| 128 return this; |
| 129 } |
| 130 |
| 131 void NativeComboboxWin::SetFocus() { |
| 132 Focus(); |
| 133 } |
| 134 |
| 135 //////////////////////////////////////////////////////////////////////////////// |
| 136 // NativeComboboxWin, NativeControlWin overrides: |
| 137 |
| 138 bool NativeComboboxWin::ProcessMessage(UINT message, |
| 139 WPARAM w_param, |
| 140 LPARAM l_param, |
| 141 LRESULT* result) { |
| 142 if (message == WM_COMMAND && HIWORD(w_param) == CBN_SELCHANGE) { |
| 143 combobox_->SelectionChanged(); |
| 144 *result = 0; |
| 145 return true; |
| 146 } |
| 147 return NativeControlWin::ProcessMessage(message, w_param, l_param, result); |
| 148 } |
| 149 |
| 150 void NativeComboboxWin::CreateNativeControl() { |
| 151 DWORD flags = WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBS_DROPDOWNLIST; |
| 152 HWND control_hwnd = ::CreateWindowEx(GetAdditionalExStyle(), L"COMBOBOX", L"", |
| 153 flags, 0, 0, 100, 20, //width(), height()
, |
| 154 GetWidget()->GetNativeView(), NULL, NULL, |
| 155 NULL); |
| 156 NativeControlCreated(control_hwnd); |
| 157 } |
| 158 |
| 159 void NativeComboboxWin::NativeControlCreated(HWND native_control) { |
| 160 NativeControlWin::NativeControlCreated(native_control); |
| 161 |
| 162 UpdateFont(); |
| 163 UpdateFromModel(); |
| 164 } |
| 165 |
| 166 //////////////////////////////////////////////////////////////////////////////// |
| 167 // NativeComboboxWin, private: |
| 168 |
| 169 void NativeComboboxWin::UpdateFont() { |
| 170 HFONT font = ResourceBundle::GetSharedInstance(). |
| 171 GetFont(ResourceBundle::BaseFont).hfont(); |
| 172 SendMessage(native_view(), WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE); |
| 173 } |
| 174 |
| 175 //////////////////////////////////////////////////////////////////////////////// |
| 176 // NativeComboboxWrapper, public: |
| 177 |
| 178 // static |
| 179 NativeComboboxWrapper* NativeComboboxWrapper::CreateWrapper( |
| 180 Combobox* combobox) { |
| 181 return new NativeComboboxWin(combobox); |
| 182 } |
| 183 |
| 184 } // namespace views |
OLD | NEW |