Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(688)

Side by Side Diff: views/controls/combo_box.cc

Issue 113991: Make Combobox portable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « views/controls/combo_box.h ('k') | views/controls/combobox/combobox.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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/combo_box.h"
6
7 #include "app/gfx/canvas.h"
8 #include "app/gfx/font.h"
9 #include "app/l10n_util.h"
10 #include "app/resource_bundle.h"
11 #include "base/gfx/native_theme.h"
12 #include "base/gfx/rect.h"
13
14 // Limit how small a combobox can be.
15 static const int kMinComboboxWidth = 148;
16
17 // Add a couple extra pixels to the widths of comboboxes and combobox
18 // dropdowns so that text isn't too crowded.
19 static const int kComboboxExtraPaddingX = 6;
20
21 namespace views {
22
23 ComboBox::ComboBox(Model* model)
24 : model_(model), selected_item_(0), listener_(NULL), content_width_(0) {
25 }
26
27 ComboBox::~ComboBox() {
28 }
29
30 void ComboBox::SetListener(Listener* listener) {
31 listener_ = listener;
32 }
33
34 gfx::Size ComboBox::GetPreferredSize() {
35 HWND hwnd = GetNativeControlHWND();
36 if (!hwnd)
37 return gfx::Size();
38
39 COMBOBOXINFO cbi;
40 memset(reinterpret_cast<unsigned char*>(&cbi), 0, sizeof(cbi));
41 cbi.cbSize = sizeof(cbi);
42 // Note: Don't use CB_GETCOMBOBOXINFO since that crashes on WOW64 systems
43 // when you have a global message hook installed.
44 GetComboBoxInfo(hwnd, &cbi);
45 gfx::Rect rect_item(cbi.rcItem);
46 gfx::Rect rect_button(cbi.rcButton);
47 gfx::Size border = gfx::NativeTheme::instance()->GetThemeBorderSize(
48 gfx::NativeTheme::MENULIST);
49
50 // The padding value of '3' is the xy offset from the corner of the control
51 // to the corner of rcItem. It does not seem to be queryable from the theme.
52 // It is consistent on all versions of Windows from 2K to Vista, and is
53 // invariant with respect to the combobox border size. We could conceivably
54 // get this number from rect_item.x, but it seems fragile to depend on
55 // position here, inside of the layout code.
56 const int kItemOffset = 3;
57 int item_to_button_distance = std::max(kItemOffset - border.width(), 0);
58
59 // The cx computation can be read as measuring from left to right.
60 int pref_width = std::max(kItemOffset + content_width_ +
61 kComboboxExtraPaddingX +
62 item_to_button_distance + rect_button.width() +
63 border.width(), kMinComboboxWidth);
64 // The two arguments to ::max below should be typically be equal.
65 int pref_height = std::max(rect_item.height() + 2 * kItemOffset,
66 rect_button.height() + 2 * border.height());
67 return gfx::Size(pref_width, pref_height);
68 }
69
70 // VK_ESCAPE should be handled by this view when the drop down list is active.
71 // In other words, the list should be closed instead of the dialog.
72 bool ComboBox::OverrideAccelerator(const Accelerator& accelerator) {
73 if (accelerator != Accelerator(VK_ESCAPE, false, false, false))
74 return false;
75
76 HWND hwnd = GetNativeControlHWND();
77 if (!hwnd)
78 return false;
79
80 return ::SendMessage(hwnd, CB_GETDROPPEDSTATE, 0, 0) != 0;
81 }
82
83 HWND ComboBox::CreateNativeControl(HWND parent_container) {
84 HWND r = ::CreateWindowEx(GetAdditionalExStyle(), L"COMBOBOX", L"",
85 WS_CHILD | WS_VSCROLL | CBS_DROPDOWNLIST,
86 0, 0, width(), height(),
87 parent_container, NULL, NULL, NULL);
88 HFONT font = ResourceBundle::GetSharedInstance().
89 GetFont(ResourceBundle::BaseFont).hfont();
90 SendMessage(r, WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE);
91 UpdateComboBoxFromModel(r);
92 return r;
93 }
94
95 LRESULT ComboBox::OnCommand(UINT code, int id, HWND source) {
96 HWND hwnd = GetNativeControlHWND();
97 if (!hwnd)
98 return 0;
99
100 if (code == CBN_SELCHANGE && source == hwnd) {
101 LRESULT r = ::SendMessage(hwnd, CB_GETCURSEL, 0, 0);
102 if (r != CB_ERR) {
103 int prev_selected_item = selected_item_;
104 selected_item_ = static_cast<int>(r);
105 if (listener_)
106 listener_->ItemChanged(this, prev_selected_item, selected_item_);
107 }
108 }
109 return 0;
110 }
111
112 LRESULT ComboBox::OnNotify(int w_param, LPNMHDR l_param) {
113 return 0;
114 }
115
116 void ComboBox::UpdateComboBoxFromModel(HWND hwnd) {
117 ::SendMessage(hwnd, CB_RESETCONTENT, 0, 0);
118 gfx::Font font = ResourceBundle::GetSharedInstance().GetFont(
119 ResourceBundle::BaseFont);
120 int max_width = 0;
121 int num_items = model_->GetItemCount(this);
122 for (int i = 0; i < num_items; ++i) {
123 const std::wstring& text = model_->GetItemAt(this, i);
124
125 // Inserting the Unicode formatting characters if necessary so that the
126 // text is displayed correctly in right-to-left UIs.
127 std::wstring localized_text;
128 const wchar_t* text_ptr = text.c_str();
129 if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text))
130 text_ptr = localized_text.c_str();
131
132 ::SendMessage(hwnd, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(text_ptr));
133 max_width = std::max(max_width, font.GetStringWidth(text));
134
135 }
136 content_width_ = max_width;
137
138 if (num_items > 0) {
139 ::SendMessage(hwnd, CB_SETCURSEL, selected_item_, 0);
140
141 // Set the width for the drop down while accounting for the scrollbar and
142 // borders.
143 if (num_items > ComboBox_GetMinVisible(hwnd))
144 max_width += ::GetSystemMetrics(SM_CXVSCROLL);
145 // SM_CXEDGE would not be correct here, since the dropdown is flat, not 3D.
146 int kComboboxDropdownBorderSize = 1;
147 max_width += 2 * kComboboxDropdownBorderSize + kComboboxExtraPaddingX;
148 ::SendMessage(hwnd, CB_SETDROPPEDWIDTH, max_width, 0);
149 }
150 }
151
152 void ComboBox::ModelChanged() {
153 HWND hwnd = GetNativeControlHWND();
154 if (!hwnd)
155 return;
156 selected_item_ = std::min(0, model_->GetItemCount(this));
157 UpdateComboBoxFromModel(hwnd);
158 }
159
160 void ComboBox::SetSelectedItem(int index) {
161 selected_item_ = index;
162 HWND hwnd = GetNativeControlHWND();
163 if (!hwnd)
164 return;
165
166 // Note that we use CB_SETCURSEL and not CB_SELECTSTRING because on RTL
167 // locales the strings we get from our ComboBox::Model might be augmented
168 // with Unicode directionality marks before we insert them into the combo box
169 // and therefore we can not assume that the string we get from
170 // ComboBox::Model can be safely searched for and selected (which is what
171 // CB_SELECTSTRING does).
172 ::SendMessage(hwnd, CB_SETCURSEL, selected_item_, 0);
173 }
174
175 int ComboBox::GetSelectedItem() {
176 return selected_item_;
177 }
178
179 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/combo_box.h ('k') | views/controls/combobox/combobox.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698