From a0d0d38b5d45d7be151bad91a2231d3679432d13 Mon Sep 17 00:00:00 2001 From: "ben@chromium.org" Date: Tue, 2 Jun 2009 04:47:36 +0000 Subject: Make Combobox portable BUG=none TEST=none Review URL: http://codereview.chromium.org/113991 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17382 0039d316-1c4b-4281-b951-d872f2087c98 --- views/controls/combo_box.cc | 179 --------------------- views/controls/combo_box.h | 80 ---------- views/controls/combobox/combobox.cc | 104 ++++++++++++ views/controls/combobox/combobox.h | 89 +++++++++++ views/controls/combobox/native_combobox_gtk.cc | 78 +++++++++ views/controls/combobox/native_combobox_gtk.h | 43 +++++ views/controls/combobox/native_combobox_win.cc | 184 ++++++++++++++++++++++ views/controls/combobox/native_combobox_win.h | 52 ++++++ views/controls/combobox/native_combobox_wrapper.h | 48 ++++++ views/controls/native/native_view_host_win.cc | 4 + views/controls/native_control_win.cc | 18 ++- views/controls/native_control_win.h | 6 +- views/views.gyp | 9 +- 13 files changed, 624 insertions(+), 270 deletions(-) delete mode 100644 views/controls/combo_box.cc delete mode 100644 views/controls/combo_box.h create mode 100644 views/controls/combobox/combobox.cc create mode 100644 views/controls/combobox/combobox.h create mode 100644 views/controls/combobox/native_combobox_gtk.cc create mode 100644 views/controls/combobox/native_combobox_gtk.h create mode 100644 views/controls/combobox/native_combobox_win.cc create mode 100644 views/controls/combobox/native_combobox_win.h create mode 100644 views/controls/combobox/native_combobox_wrapper.h (limited to 'views') diff --git a/views/controls/combo_box.cc b/views/controls/combo_box.cc deleted file mode 100644 index 966509c..0000000 --- a/views/controls/combo_box.cc +++ /dev/null @@ -1,179 +0,0 @@ -// Copyright (c) 2006-2008 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/combo_box.h" - -#include "app/gfx/canvas.h" -#include "app/gfx/font.h" -#include "app/l10n_util.h" -#include "app/resource_bundle.h" -#include "base/gfx/native_theme.h" -#include "base/gfx/rect.h" - -// Limit how small a combobox can be. -static const int kMinComboboxWidth = 148; - -// Add a couple extra pixels to the widths of comboboxes and combobox -// dropdowns so that text isn't too crowded. -static const int kComboboxExtraPaddingX = 6; - -namespace views { - -ComboBox::ComboBox(Model* model) - : model_(model), selected_item_(0), listener_(NULL), content_width_(0) { -} - -ComboBox::~ComboBox() { -} - -void ComboBox::SetListener(Listener* listener) { - listener_ = listener; -} - -gfx::Size ComboBox::GetPreferredSize() { - HWND hwnd = GetNativeControlHWND(); - if (!hwnd) - return gfx::Size(); - - COMBOBOXINFO cbi; - memset(reinterpret_cast(&cbi), 0, sizeof(cbi)); - cbi.cbSize = sizeof(cbi); - // Note: Don't use CB_GETCOMBOBOXINFO since that crashes on WOW64 systems - // when you have a global message hook installed. - GetComboBoxInfo(hwnd, &cbi); - gfx::Rect rect_item(cbi.rcItem); - gfx::Rect rect_button(cbi.rcButton); - gfx::Size border = gfx::NativeTheme::instance()->GetThemeBorderSize( - gfx::NativeTheme::MENULIST); - - // The padding value of '3' is the xy offset from the corner of the control - // to the corner of rcItem. It does not seem to be queryable from the theme. - // It is consistent on all versions of Windows from 2K to Vista, and is - // invariant with respect to the combobox border size. We could conceivably - // get this number from rect_item.x, but it seems fragile to depend on - // position here, inside of the layout code. - const int kItemOffset = 3; - int item_to_button_distance = std::max(kItemOffset - border.width(), 0); - - // The cx computation can be read as measuring from left to right. - int pref_width = std::max(kItemOffset + content_width_ + - kComboboxExtraPaddingX + - item_to_button_distance + rect_button.width() + - border.width(), kMinComboboxWidth); - // The two arguments to ::max below should be typically be equal. - int pref_height = std::max(rect_item.height() + 2 * kItemOffset, - rect_button.height() + 2 * border.height()); - return gfx::Size(pref_width, pref_height); -} - -// VK_ESCAPE should be handled by this view when the drop down list is active. -// In other words, the list should be closed instead of the dialog. -bool ComboBox::OverrideAccelerator(const Accelerator& accelerator) { - if (accelerator != Accelerator(VK_ESCAPE, false, false, false)) - return false; - - HWND hwnd = GetNativeControlHWND(); - if (!hwnd) - return false; - - return ::SendMessage(hwnd, CB_GETDROPPEDSTATE, 0, 0) != 0; -} - -HWND ComboBox::CreateNativeControl(HWND parent_container) { - HWND r = ::CreateWindowEx(GetAdditionalExStyle(), L"COMBOBOX", L"", - WS_CHILD | WS_VSCROLL | CBS_DROPDOWNLIST, - 0, 0, width(), height(), - parent_container, NULL, NULL, NULL); - HFONT font = ResourceBundle::GetSharedInstance(). - GetFont(ResourceBundle::BaseFont).hfont(); - SendMessage(r, WM_SETFONT, reinterpret_cast(font), FALSE); - UpdateComboBoxFromModel(r); - return r; -} - -LRESULT ComboBox::OnCommand(UINT code, int id, HWND source) { - HWND hwnd = GetNativeControlHWND(); - if (!hwnd) - return 0; - - if (code == CBN_SELCHANGE && source == hwnd) { - LRESULT r = ::SendMessage(hwnd, CB_GETCURSEL, 0, 0); - if (r != CB_ERR) { - int prev_selected_item = selected_item_; - selected_item_ = static_cast(r); - if (listener_) - listener_->ItemChanged(this, prev_selected_item, selected_item_); - } - } - return 0; -} - -LRESULT ComboBox::OnNotify(int w_param, LPNMHDR l_param) { - return 0; -} - -void ComboBox::UpdateComboBoxFromModel(HWND hwnd) { - ::SendMessage(hwnd, CB_RESETCONTENT, 0, 0); - gfx::Font font = ResourceBundle::GetSharedInstance().GetFont( - ResourceBundle::BaseFont); - int max_width = 0; - int num_items = model_->GetItemCount(this); - for (int i = 0; i < num_items; ++i) { - const std::wstring& text = model_->GetItemAt(this, i); - - // Inserting the Unicode formatting characters if necessary so that the - // text is displayed correctly in right-to-left UIs. - std::wstring localized_text; - const wchar_t* text_ptr = text.c_str(); - if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text)) - text_ptr = localized_text.c_str(); - - ::SendMessage(hwnd, CB_ADDSTRING, 0, reinterpret_cast(text_ptr)); - max_width = std::max(max_width, font.GetStringWidth(text)); - - } - content_width_ = max_width; - - if (num_items > 0) { - ::SendMessage(hwnd, CB_SETCURSEL, selected_item_, 0); - - // Set the width for the drop down while accounting for the scrollbar and - // borders. - if (num_items > ComboBox_GetMinVisible(hwnd)) - max_width += ::GetSystemMetrics(SM_CXVSCROLL); - // SM_CXEDGE would not be correct here, since the dropdown is flat, not 3D. - int kComboboxDropdownBorderSize = 1; - max_width += 2 * kComboboxDropdownBorderSize + kComboboxExtraPaddingX; - ::SendMessage(hwnd, CB_SETDROPPEDWIDTH, max_width, 0); - } -} - -void ComboBox::ModelChanged() { - HWND hwnd = GetNativeControlHWND(); - if (!hwnd) - return; - selected_item_ = std::min(0, model_->GetItemCount(this)); - UpdateComboBoxFromModel(hwnd); -} - -void ComboBox::SetSelectedItem(int index) { - selected_item_ = index; - HWND hwnd = GetNativeControlHWND(); - if (!hwnd) - return; - - // Note that we use CB_SETCURSEL and not CB_SELECTSTRING because on RTL - // locales the strings we get from our ComboBox::Model might be augmented - // with Unicode directionality marks before we insert them into the combo box - // and therefore we can not assume that the string we get from - // ComboBox::Model can be safely searched for and selected (which is what - // CB_SELECTSTRING does). - ::SendMessage(hwnd, CB_SETCURSEL, selected_item_, 0); -} - -int ComboBox::GetSelectedItem() { - return selected_item_; -} - -} // namespace views diff --git a/views/controls/combo_box.h b/views/controls/combo_box.h deleted file mode 100644 index 3e8eeae..0000000 --- a/views/controls/combo_box.h +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) 2006-2008 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. - -#ifndef VIEWS_CONTROLS_COMBO_BOX_H_ -#define VIEWS_CONTROLS_COMBO_BOX_H_ - -#include "views/controls/native_control.h" - -namespace views { -//////////////////////////////////////////////////////////////////////////////// -// -// ComboBox is a basic non editable combo box. It is initialized from a simple -// model. -// -//////////////////////////////////////////////////////////////////////////////// -class ComboBox : public NativeControl { - public: - class Model { - public: - // Return the number of items in the combo box. - virtual int GetItemCount(ComboBox* source) = 0; - - // Return the string that should be used to represent a given item. - virtual std::wstring GetItemAt(ComboBox* source, int index) = 0; - }; - - class Listener { - public: - // This is invoked once the selected item changed. - virtual void ItemChanged(ComboBox* combo_box, - int prev_index, - int new_index) = 0; - }; - - // |model is not owned by the combo box. - explicit ComboBox(Model* model); - virtual ~ComboBox(); - - // Register |listener| for item change events. - void SetListener(Listener* listener); - - // Overridden from View. - virtual gfx::Size GetPreferredSize(); - virtual bool OverrideAccelerator(const Accelerator& accelerator); - - // Overridden from NativeControl - virtual HWND CreateNativeControl(HWND parent_container); - virtual LRESULT OnCommand(UINT code, int id, HWND source); - virtual LRESULT OnNotify(int w_param, LPNMHDR l_param); - - // Inform the combo box that its model changed. - void ModelChanged(); - - // Set / Get the selected item. - void SetSelectedItem(int index); - int GetSelectedItem(); - - private: - // Update a combo box from our model. - void UpdateComboBoxFromModel(HWND hwnd); - - // Our model. - Model* model_; - - // The current selection. - int selected_item_; - - // Item change listener. - Listener* listener_; - - // The min width, in pixels, for the text content. - int content_width_; - - DISALLOW_EVIL_CONSTRUCTORS(ComboBox); -}; - -} // namespace views - -#endif // VIEWS_CONTROLS_COMBO_BOX_H_ diff --git a/views/controls/combobox/combobox.cc b/views/controls/combobox/combobox.cc new file mode 100644 index 0000000..48adc00 --- /dev/null +++ b/views/controls/combobox/combobox.cc @@ -0,0 +1,104 @@ +// Copyright (c) 2006-2008 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/combobox/combobox.h" + +#include "views/controls/combobox/native_combobox_wrapper.h" + +namespace views { + +// static +const char Combobox::kViewClassName[] = "views/Combobox"; + +//////////////////////////////////////////////////////////////////////////////// +// Combobox, public: + +Combobox::Combobox(Model* model) + : native_wrapper_(NULL), + model_(model), + listener_(NULL), + selected_item_(0) { +} + +Combobox::~Combobox() { +} + +void Combobox::ModelChanged() { + selected_item_ = std::min(0, model_->GetItemCount(this)); + if (native_wrapper_) + native_wrapper_->UpdateFromModel(); +} + +void Combobox::SetSelectedItem(int index) { + selected_item_ = index; + if (native_wrapper_) + native_wrapper_->UpdateSelectedItem(); +} + +void Combobox::SelectionChanged() { + int prev_selected_item = selected_item_; + selected_item_ = native_wrapper_->GetSelectedItem(); + if (listener_) + listener_->ItemChanged(this, prev_selected_item, selected_item_); +} + +//////////////////////////////////////////////////////////////////////////////// +// Combobox, View overrides: + +gfx::Size Combobox::GetPreferredSize() { + if (native_wrapper_) + return native_wrapper_->GetPreferredSize(); + return gfx::Size(); +} + +void Combobox::Layout() { + if (native_wrapper_) { + native_wrapper_->GetView()->SetBounds(0, 0, width(), height()); + native_wrapper_->GetView()->Layout(); + } +} + +void Combobox::SetEnabled(bool flag) { + View::SetEnabled(flag); + if (native_wrapper_) + native_wrapper_->UpdateEnabled(); +} + +// VK_ESCAPE should be handled by this view when the drop down list is active. +// In other words, the list should be closed instead of the dialog. +bool Combobox::OverrideAccelerator(const Accelerator& accelerator) { +#if defined(OS_WIN) + if (accelerator != Accelerator(VK_ESCAPE, false, false, false)) + return false; +#else + NOTIMPLEMENTED(); + // TODO(port): figure out VK_keys +#endif + return native_wrapper_ && native_wrapper_->IsDropdownOpen(); +} + +void Combobox::Focus() { + // Forward the focus to the wrapper. + if (native_wrapper_) + native_wrapper_->SetFocus(); + else + View::Focus(); // Will focus the RootView window (so we still get + // keyboard messages). +} + +void Combobox::ViewHierarchyChanged(bool is_add, View* parent, + View* child) { + if (is_add && !native_wrapper_ && GetWidget()) { + native_wrapper_ = NativeComboboxWrapper::CreateWrapper(this); + native_wrapper_->UpdateFromModel(); + native_wrapper_->UpdateEnabled(); + AddChildView(native_wrapper_->GetView()); + } +} + +std::string Combobox::GetClassName() const { + return kViewClassName; +} + +} // namespace views diff --git a/views/controls/combobox/combobox.h b/views/controls/combobox/combobox.h new file mode 100644 index 0000000..2bb63d2 --- /dev/null +++ b/views/controls/combobox/combobox.h @@ -0,0 +1,89 @@ +// Copyright (c) 2006-2008 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. + +#ifndef VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ +#define VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ + +#include "views/view.h" + +namespace views { + +class NativeComboboxWrapper; + +// A non-editable combo-box. +class Combobox : public View { + public: + // The combobox's class name. + static const char kViewClassName[]; + + class Model { + public: + // Return the number of items in the combo box. + virtual int GetItemCount(Combobox* source) = 0; + + // Return the string that should be used to represent a given item. + virtual std::wstring GetItemAt(Combobox* source, int index) = 0; + }; + + class Listener { + public: + // This is invoked once the selected item changed. + virtual void ItemChanged(Combobox* combo_box, + int prev_index, + int new_index) = 0; + }; + + // |model| is not owned by the combo box. + explicit Combobox(Model* model); + virtual ~Combobox(); + + // Register |listener| for item change events. + void set_listener(Listener* listener) { + listener_ = listener; + } + + // Inform the combo box that its model changed. + void ModelChanged(); + + // Gets/Sets the selected item. + int selected_item() const { return selected_item_; }; + void SetSelectedItem(int index); + + // Called when the combo box's selection is changed by the user. + void SelectionChanged(); + + // Accessor for |model_|. + Model* model() const { return model_; } + + // Overridden from View: + virtual gfx::Size GetPreferredSize(); + virtual void Layout(); + virtual void SetEnabled(bool enabled); + virtual bool OverrideAccelerator(const Accelerator& accelerator); + + protected: + virtual void Focus(); + virtual void ViewHierarchyChanged(bool is_add, View* parent, + View* child); + virtual std::string GetClassName() const; + + private: + // The object that actually implements the native combobox. + NativeComboboxWrapper* native_wrapper_; + + // Our model. + Model* model_; + + // Item change listener. + Listener* listener_; + + // The current selection. + int selected_item_; + + DISALLOW_COPY_AND_ASSIGN(Combobox); +}; + +} // namespace views + +#endif // VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ diff --git a/views/controls/combobox/native_combobox_gtk.cc b/views/controls/combobox/native_combobox_gtk.cc new file mode 100644 index 0000000..b6d2674 --- /dev/null +++ b/views/controls/combobox/native_combobox_gtk.cc @@ -0,0 +1,78 @@ +// 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/combobox/native_combobox_gtk.h" + +#include "views/controls/combobox/combobox.h" + +namespace views { + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxGtk, public: + +NativeComboboxGtk::NativeComboboxGtk(Combobox* combobox) + : combobox_(combobox) { +} + +NativeComboboxGtk::~NativeComboboxGtk() { +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxGtk, NativeComboboxWrapper implementation: + +void NativeComboboxGtk::UpdateFromModel() { + NOTIMPLEMENTED(); +} + +void NativeComboboxGtk::UpdateSelectedItem() { + NOTIMPLEMENTED(); +} + +void NativeComboboxGtk::UpdateEnabled() { + NOTIMPLEMENTED(); +} + +int NativeComboboxGtk::GetSelectedItem() const { + NOTIMPLEMENTED(); + return 0; +} + +bool NativeComboboxGtk::IsDropdownOpen() const { + NOTIMPLEMENTED(); + return false; +} + +gfx::Size NativeComboboxGtk::GetPreferredSize() const { + NOTIMPLEMENTED(); + return gfx::Size(); +} + +View* NativeComboboxGtk::GetView() { + return this; +} + +void NativeComboboxGtk::SetFocus() { + Focus(); +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxGtk, NativeControlGtk overrides: + +void NativeComboboxGtk::CreateNativeControl() { +} + +void NativeComboboxGtk::NativeControlCreated(GtkWidget* native_control) { + NativeControlGtk::NativeControlCreated(native_control); +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxWrapper, public: + +// static +NativeComboboxWrapper* NativeComboboxWrapper::CreateWrapper( + Combobox* combobox) { + return new NativeComboboxGtk(combobox); +} + +} // namespace views diff --git a/views/controls/combobox/native_combobox_gtk.h b/views/controls/combobox/native_combobox_gtk.h new file mode 100644 index 0000000..2405697 --- /dev/null +++ b/views/controls/combobox/native_combobox_gtk.h @@ -0,0 +1,43 @@ +// 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. + +#ifndef VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_GTK_H_ +#define VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_GTK_H_ + +#include "views/controls/combobox/native_combobox_wrapper.h" +#include "views/controls/native_control_gtk.h" + +namespace views { + +class NativeComboboxGtk : public NativeControlGtk, + public NativeComboboxWrapper { + public: + explicit NativeComboboxGtk(Combobox* combobox); + virtual ~NativeComboboxGtk(); + + // Overridden from NativeComboboxWrapper: + virtual void UpdateFromModel(); + virtual void UpdateSelectedItem(); + virtual void UpdateEnabled(); + virtual int GetSelectedItem() const; + virtual bool IsDropdownOpen() const; + virtual gfx::Size GetPreferredSize() const; + virtual View* GetView(); + virtual void SetFocus(); + + protected: + // Overridden from NativeControlGtk: + virtual void CreateNativeControl(); + virtual void NativeControlCreated(GtkWidget* widget); + + private: + // The combobox we are bound to. + Combobox* combobox_; + + DISALLOW_COPY_AND_ASSIGN(NativeComboboxGtk); +}; + +} // namespace views + +#endif // VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_GTK_H_ diff --git a/views/controls/combobox/native_combobox_win.cc b/views/controls/combobox/native_combobox_win.cc new file mode 100644 index 0000000..4562897 --- /dev/null +++ b/views/controls/combobox/native_combobox_win.cc @@ -0,0 +1,184 @@ +// 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/combobox/native_combobox_win.h" + +#include "app/gfx/font.h" +#include "app/l10n_util.h" +#include "app/resource_bundle.h" +#include "base/gfx/native_theme.h" +#include "views/controls/combobox/combobox.h" +#include "views/widget/widget.h" + +namespace views { + +// Limit how small a combobox can be. +static const int kMinComboboxWidth = 148; + +// Add a couple extra pixels to the widths of comboboxes and combobox +// dropdowns so that text isn't too crowded. +static const int kComboboxExtraPaddingX = 6; + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxWin, public: + +NativeComboboxWin::NativeComboboxWin(Combobox* combobox) + : combobox_(combobox), + content_width_(0) { +} + +NativeComboboxWin::~NativeComboboxWin() { +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxWin, NativeComboboxWrapper implementation: + +void NativeComboboxWin::UpdateFromModel() { + SendMessage(native_view(), CB_RESETCONTENT, 0, 0); + gfx::Font font = ResourceBundle::GetSharedInstance().GetFont( + ResourceBundle::BaseFont); + int max_width = 0; + int num_items = combobox_->model()->GetItemCount(combobox_); + for (int i = 0; i < num_items; ++i) { + const std::wstring& text = combobox_->model()->GetItemAt(combobox_, i); + + // Inserting the Unicode formatting characters if necessary so that the + // text is displayed correctly in right-to-left UIs. + std::wstring localized_text; + const wchar_t* text_ptr = text.c_str(); + if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text)) + text_ptr = localized_text.c_str(); + + SendMessage(native_view(), CB_ADDSTRING, 0, + reinterpret_cast(text_ptr)); + max_width = std::max(max_width, font.GetStringWidth(text)); + } + content_width_ = max_width; + + if (num_items > 0) { + SendMessage(native_view(), CB_SETCURSEL, combobox_->selected_item(), 0); + + // Set the width for the drop down while accounting for the scrollbar and + // borders. + if (num_items > ComboBox_GetMinVisible(native_view())) + max_width += GetSystemMetrics(SM_CXVSCROLL); + // SM_CXEDGE would not be correct here, since the dropdown is flat, not 3D. + int kComboboxDropdownBorderSize = 1; + max_width += 2 * kComboboxDropdownBorderSize + kComboboxExtraPaddingX; + SendMessage(native_view(), CB_SETDROPPEDWIDTH, max_width, 0); + } +} + +void NativeComboboxWin::UpdateSelectedItem() { + // Note that we use CB_SETCURSEL and not CB_SELECTSTRING because on RTL + // locales the strings we get from our ComboBox::Model might be augmented + // with Unicode directionality marks before we insert them into the combo box + // and therefore we can not assume that the string we get from + // ComboBox::Model can be safely searched for and selected (which is what + // CB_SELECTSTRING does). + SendMessage(native_view(), CB_SETCURSEL, combobox_->selected_item(), 0); +} + +void NativeComboboxWin::UpdateEnabled() { + SetEnabled(combobox_->IsEnabled()); +} + +int NativeComboboxWin::GetSelectedItem() const { + LRESULT selected_item = SendMessage(native_view(), CB_GETCURSEL, 0, 0); + return selected_item != CB_ERR ? selected_item : -1; +} + +bool NativeComboboxWin::IsDropdownOpen() const { + return SendMessage(native_view(), CB_GETDROPPEDSTATE, 0, 0) != 0; +} + +gfx::Size NativeComboboxWin::GetPreferredSize() const { + COMBOBOXINFO cbi = { 0 }; + cbi.cbSize = sizeof(cbi); + // Note: Don't use CB_GETCOMBOBOXINFO since that crashes on WOW64 systems + // when you have a global message hook installed. + GetComboBoxInfo(native_view(), &cbi); + gfx::Rect rect_item(cbi.rcItem); + gfx::Rect rect_button(cbi.rcButton); + gfx::Size border = gfx::NativeTheme::instance()->GetThemeBorderSize( + gfx::NativeTheme::MENULIST); + + // The padding value of '3' is the xy offset from the corner of the control + // to the corner of rcItem. It does not seem to be queryable from the theme. + // It is consistent on all versions of Windows from 2K to Vista, and is + // invariant with respect to the combobox border size. We could conceivably + // get this number from rect_item.x, but it seems fragile to depend on + // position here, inside of the layout code. + const int kItemOffset = 3; + int item_to_button_distance = std::max(kItemOffset - border.width(), 0); + + // The cx computation can be read as measuring from left to right. + int pref_width = std::max(kItemOffset + content_width_ + + kComboboxExtraPaddingX + + item_to_button_distance + rect_button.width() + + border.width(), kMinComboboxWidth); + // The two arguments to ::max below should be typically be equal. + int pref_height = std::max(rect_item.height() + 2 * kItemOffset, + rect_button.height() + 2 * border.height()); + return gfx::Size(pref_width, pref_height); +} + +View* NativeComboboxWin::GetView() { + return this; +} + +void NativeComboboxWin::SetFocus() { + Focus(); +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxWin, NativeControlWin overrides: + +bool NativeComboboxWin::ProcessMessage(UINT message, + WPARAM w_param, + LPARAM l_param, + LRESULT* result) { + if (message == WM_COMMAND && HIWORD(w_param) == CBN_SELCHANGE) { + combobox_->SelectionChanged(); + *result = 0; + return true; + } + return NativeControlWin::ProcessMessage(message, w_param, l_param, result); +} + +void NativeComboboxWin::CreateNativeControl() { + DWORD flags = WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBS_DROPDOWNLIST; + HWND control_hwnd = ::CreateWindowEx(GetAdditionalExStyle(), L"COMBOBOX", L"", + flags, 0, 0, 100, 20, //width(), height(), + GetWidget()->GetNativeView(), NULL, NULL, + NULL); + NativeControlCreated(control_hwnd); +} + +void NativeComboboxWin::NativeControlCreated(HWND native_control) { + NativeControlWin::NativeControlCreated(native_control); + + UpdateFont(); + UpdateFromModel(); +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxWin, private: + +void NativeComboboxWin::UpdateFont() { + HFONT font = ResourceBundle::GetSharedInstance(). + GetFont(ResourceBundle::BaseFont).hfont(); + SendMessage(native_view(), WM_SETFONT, reinterpret_cast(font), FALSE); +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeComboboxWrapper, public: + +// static +NativeComboboxWrapper* NativeComboboxWrapper::CreateWrapper( + Combobox* combobox) { + return new NativeComboboxWin(combobox); +} + +} // namespace views diff --git a/views/controls/combobox/native_combobox_win.h b/views/controls/combobox/native_combobox_win.h new file mode 100644 index 0000000..627b551 --- /dev/null +++ b/views/controls/combobox/native_combobox_win.h @@ -0,0 +1,52 @@ +// 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. + +#ifndef VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_WIN_H_ +#define VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_WIN_H_ + +#include "views/controls/combobox/native_combobox_wrapper.h" +#include "views/controls/native_control_win.h" + +namespace views { + +class NativeComboboxWin : public NativeControlWin, + public NativeComboboxWrapper { + public: + explicit NativeComboboxWin(Combobox* combobox); + virtual ~NativeComboboxWin(); + + // Overridden from NativeComboboxWrapper: + virtual void UpdateFromModel(); + virtual void UpdateSelectedItem(); + virtual void UpdateEnabled(); + virtual int GetSelectedItem() const; + virtual bool IsDropdownOpen() const; + virtual gfx::Size GetPreferredSize() const; + virtual View* GetView(); + virtual void SetFocus(); + + protected: + // Overridden from NativeControlWin: + virtual bool ProcessMessage(UINT message, + WPARAM w_param, + LPARAM l_param, + LRESULT* result); + virtual void CreateNativeControl(); + virtual void NativeControlCreated(HWND native_control); + + private: + void UpdateFont(); + + // The combobox we are bound to. + Combobox* combobox_; + + // The min width, in pixels, for the text content. + int content_width_; + + DISALLOW_COPY_AND_ASSIGN(NativeComboboxWin); +}; + +} // namespace views + +#endif // VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_WIN_H_ diff --git a/views/controls/combobox/native_combobox_wrapper.h b/views/controls/combobox/native_combobox_wrapper.h new file mode 100644 index 0000000..27e1c8b --- /dev/null +++ b/views/controls/combobox/native_combobox_wrapper.h @@ -0,0 +1,48 @@ +// 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. + +#ifndef VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_WRAPPER_H_ +#define VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_WRAPPER_H_ + +namespace gfx{ +class Size; +} + +namespace views { + +class Combobox; +class View; + +class NativeComboboxWrapper { + public: + // Updates the combobox's content from its model. + virtual void UpdateFromModel() = 0; + + // Updates the displayed selected item from the associated Combobox. + virtual void UpdateSelectedItem() = 0; + + // Updates the enabled state of the combobox from the associated view. + virtual void UpdateEnabled() = 0; + + // Gets the selected index. + virtual int GetSelectedItem() const = 0; + + // Returns true if the Combobox dropdown is open. + virtual bool IsDropdownOpen() const = 0; + + // Returns the preferred size of the combobox. + virtual gfx::Size GetPreferredSize() const = 0; + + // Retrieves the views::View that hosts the native control. + virtual View* GetView() = 0; + + // Sets the focus to the button. + virtual void SetFocus() = 0; + + static NativeComboboxWrapper* CreateWrapper(Combobox* combobox); +}; + +} // namespace views + +#endif // VIEWS_CONTROLS_COMBOBOX_NATIVE_COMBOBOX_WRAPPER_H_ diff --git a/views/controls/native/native_view_host_win.cc b/views/controls/native/native_view_host_win.cc index 0cbaaa7..04c9edc 100644 --- a/views/controls/native/native_view_host_win.cc +++ b/views/controls/native/native_view_host_win.cc @@ -51,6 +51,8 @@ void NativeViewHostWin::NativeViewDetaching() { } void NativeViewHostWin::AddedToWidget() { + if (!IsWindow(host_->native_view())) + return; HWND parent_hwnd = GetParent(host_->native_view()); HWND widget_hwnd = host_->GetWidget()->GetNativeView(); if (parent_hwnd != widget_hwnd) @@ -63,6 +65,8 @@ void NativeViewHostWin::AddedToWidget() { } void NativeViewHostWin::RemovedFromWidget() { + if (!IsWindow(host_->native_view())) + return; ShowWindow(host_->native_view(), SW_HIDE); SetParent(host_->native_view(), NULL); } diff --git a/views/controls/native_control_win.cc b/views/controls/native_control_win.cc index eb188af..2d53f28 100644 --- a/views/controls/native_control_win.cc +++ b/views/controls/native_control_win.cc @@ -114,13 +114,20 @@ void NativeControlWin::ShowContextMenu(const gfx::Point& location) { 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. + // Note that we never unset this property. We don't have to. 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_); + // Subclass the window so we can monitor for key presses. It's important that + // we *only* do this if the derived class wants to intercept keypresses, + // because otherwise the subclass can mysteriously interfere with certain + // other controls, like the combobox, and cause weird effects. + if (NotifyOnKeyDown()) { + original_wndproc_ = + win_util::SetWindowProc(native_control, + &NativeControlWin::NativeControlWndProc); + SetProp(native_control, kNativeControlOriginalWndProcKey, + original_wndproc_); + } Attach(native_control); // native_view() is now valid. @@ -188,7 +195,6 @@ LRESULT NativeControlWin::NativeControlWndProc(HWND window, 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, diff --git a/views/controls/native_control_win.h b/views/controls/native_control_win.h index eee4ded8..4fb5244 100644 --- a/views/controls/native_control_win.h +++ b/views/controls/native_control_win.h @@ -24,9 +24,9 @@ class NativeControlWin : public NativeViewHost { // Returns true if the message was handled, with a valid result in |result|. // Returns false if the message was not handled. virtual bool ProcessMessage(UINT message, - WPARAM w_param, - LPARAM l_param, - LRESULT* result); + WPARAM w_param, + LPARAM l_param, + LRESULT* result); // Called by our subclassed window procedure when a WM_KEYDOWN message is // received by the HWND created by an object derived from NativeControlWin. diff --git a/views/views.gyp b/views/views.gyp index 750588a..97caf78 100644 --- a/views/views.gyp +++ b/views/views.gyp @@ -85,8 +85,13 @@ 'controls/button/radio_button.h', 'controls/button/text_button.cc', 'controls/button/text_button.h', - 'controls/combo_box.cc', - 'controls/combo_box.h', + 'controls/combobox/combobox.cc', + 'controls/combobox/combobox.h', + 'controls/combobox/native_combobox_gtk.cc', + 'controls/combobox/native_combobox_gtk.h', + 'controls/combobox/native_combobox_win.cc', + 'controls/combobox/native_combobox_win.h', + 'controls/combobox/native_combobox_wrapper.h', 'controls/image_view.cc', 'controls/image_view.h', 'controls/label.cc', -- cgit v1.1