summaryrefslogtreecommitdiffstats
path: root/views/controls/combobox
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-02 04:47:36 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-02 04:47:36 +0000
commita0d0d38b5d45d7be151bad91a2231d3679432d13 (patch)
tree65866dc75a0ca59d3bd51804db05560e41b9ebf2 /views/controls/combobox
parent73c8cb87ca07481b24f7823c1f2f99f0b530ef11 (diff)
downloadchromium_src-a0d0d38b5d45d7be151bad91a2231d3679432d13.zip
chromium_src-a0d0d38b5d45d7be151bad91a2231d3679432d13.tar.gz
chromium_src-a0d0d38b5d45d7be151bad91a2231d3679432d13.tar.bz2
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
Diffstat (limited to 'views/controls/combobox')
-rw-r--r--views/controls/combobox/combobox.cc104
-rw-r--r--views/controls/combobox/combobox.h89
-rw-r--r--views/controls/combobox/native_combobox_gtk.cc78
-rw-r--r--views/controls/combobox/native_combobox_gtk.h43
-rw-r--r--views/controls/combobox/native_combobox_win.cc184
-rw-r--r--views/controls/combobox/native_combobox_win.h52
-rw-r--r--views/controls/combobox/native_combobox_wrapper.h48
7 files changed, 598 insertions, 0 deletions
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<LPARAM>(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<WPARAM>(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_