diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-21 00:50:26 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-21 00:50:26 +0000 |
commit | 443a4b92574e123034450906da6fac6bbb7d9dd3 (patch) | |
tree | 28120062400ef4deaeae2f328677f2cda719ac20 /chrome/views | |
parent | 986b7694534e767af2656a41f756c8d4ed864243 (diff) | |
download | chromium_src-443a4b92574e123034450906da6fac6bbb7d9dd3.zip chromium_src-443a4b92574e123034450906da6fac6bbb7d9dd3.tar.gz chromium_src-443a4b92574e123034450906da6fac6bbb7d9dd3.tar.bz2 |
Add support for radio buttons. The code implementing checked/group was copied from the old radio button.
Review URL: http://codereview.chromium.org/50080
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12235 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/views')
-rw-r--r-- | chrome/views/controls/button/checkbox2.h | 5 | ||||
-rw-r--r-- | chrome/views/controls/button/native_button_win.cc | 1 | ||||
-rw-r--r-- | chrome/views/controls/button/radio_button2.cc | 103 | ||||
-rw-r--r-- | chrome/views/controls/button/radio_button2.h | 45 |
4 files changed, 149 insertions, 5 deletions
diff --git a/chrome/views/controls/button/checkbox2.h b/chrome/views/controls/button/checkbox2.h index a10c993..d2c1e0f 100644 --- a/chrome/views/controls/button/checkbox2.h +++ b/chrome/views/controls/button/checkbox2.h @@ -70,11 +70,6 @@ class Checkbox2 : public NativeButton2 { DISALLOW_COPY_AND_ASSIGN(Checkbox2); }; -// TODO(beng): move to own file and un-stub: -class RadioButton2 : public Checkbox2 { - -}; - } // namespace views #endif // #ifndef CHROME_VIEWS_CONTROLS_BUTTON_CHECKBOX2_H_ diff --git a/chrome/views/controls/button/native_button_win.cc b/chrome/views/controls/button/native_button_win.cc index 6114259..8bffd96 100644 --- a/chrome/views/controls/button/native_button_win.cc +++ b/chrome/views/controls/button/native_button_win.cc @@ -7,6 +7,7 @@ #include "base/logging.h" #include "chrome/views/controls/button/checkbox2.h" #include "chrome/views/controls/button/native_button2.h" +#include "chrome/views/controls/button/radio_button2.h" #include "chrome/views/widget/widget.h" namespace views { diff --git a/chrome/views/controls/button/radio_button2.cc b/chrome/views/controls/button/radio_button2.cc new file mode 100644 index 0000000..72bd927 --- /dev/null +++ b/chrome/views/controls/button/radio_button2.cc @@ -0,0 +1,103 @@ +// 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 "chrome/views/controls/button/radio_button2.h" + +#include "chrome/views/widget/root_view.h" + +namespace views { + +// static +const char RadioButton2::kViewClassName[] = "chrome/views/RadioButton"; + +//////////////////////////////////////////////////////////////////////////////// +// RadioButton, public: + +RadioButton2::RadioButton2() { +} + +RadioButton2::RadioButton2(ButtonListener* listener) : Checkbox2(listener) { +} + +RadioButton2::RadioButton2(ButtonListener* listener, const std::wstring& label) + : Checkbox2(listener, label) { +} + +RadioButton2::RadioButton2(ButtonListener* listener, + const std::wstring& label, + int group_id) + : Checkbox2(listener, label) { + SetGroup(group_id); +} + +RadioButton2::~RadioButton2() { +} + +//////////////////////////////////////////////////////////////////////////////// +// RadioButton, Checkbox overrides: + +void RadioButton2::SetChecked(bool checked) { + if (checked == RadioButton2::checked()) + return; + if (checked) { + // We can't just get the root view here because sometimes the radio + // button isn't attached to a root view (e.g., if it's part of a tab page + // that is currently not active). + View* container = GetParent(); + while (container && container->GetParent()) + container = container->GetParent(); + if (container) { + std::vector<View*> other; + container->GetViewsWithGroup(GetGroup(), &other); + std::vector<View*>::iterator i; + for (i = other.begin(); i != other.end(); ++i) { + if (*i != this) { + RadioButton2* peer = static_cast<RadioButton2*>(*i); + peer->SetChecked(false); + } + } + } + } + Checkbox2::SetChecked(checked); + +} + +//////////////////////////////////////////////////////////////////////////////// +// RadioButton, View overrides: + +View* RadioButton2::GetSelectedViewForGroup(int group_id) { + std::vector<View*> views; + GetRootView()->GetViewsWithGroup(group_id, &views); + if (views.empty()) + return NULL; + + for (std::vector<View*>::const_iterator iter = views.begin(); + iter != views.end(); ++iter) { + RadioButton2* radio_button = static_cast<RadioButton2*>(*iter); + if (radio_button->checked()) + return radio_button; + } + return NULL; +} + +bool RadioButton2::IsGroupFocusTraversable() const { + // When focusing a radio button with tab/shift+tab, only the selected button + // from the group should be focused. + return false; +} + +std::string RadioButton2::GetClassName() const { + return kViewClassName; +} + +//////////////////////////////////////////////////////////////////////////////// +// RadioButton, NativeButton overrides: + +void RadioButton2::CreateWrapper() { + native_wrapper_ = NativeButtonWrapper::CreateRadioButtonWrapper(this); + native_wrapper_->UpdateLabel(); + native_wrapper_->UpdateChecked(); +} + +} // namespace views diff --git a/chrome/views/controls/button/radio_button2.h b/chrome/views/controls/button/radio_button2.h new file mode 100644 index 0000000..ce198d1 --- /dev/null +++ b/chrome/views/controls/button/radio_button2.h @@ -0,0 +1,45 @@ +// 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 CHROME_VIEWS_CONTROLS_BUTTON_RADIO_BUTTON2_H_ +#define CHROME_VIEWS_CONTROLS_BUTTON_RADIO_BUTTON2_H_ + +#include "chrome/views/controls/button/checkbox2.h" + +namespace views { + +// A Checkbox subclass representing a radio button. +class RadioButton2 : public Checkbox2 { + public: + // The button's class name. + static const char kViewClassName[]; + + RadioButton2(); + explicit RadioButton2(ButtonListener* listener); + RadioButton2(ButtonListener* listener, const std::wstring& label); + RadioButton2(ButtonListener* listener, + const std::wstring& label, + int group_id); + virtual ~RadioButton2(); + + // Overridden from Checkbox: + virtual void SetChecked(bool checked); + + // Overridden from View: + virtual View* GetSelectedViewForGroup(int group_id); + virtual bool IsGroupFocusTraversable() const; + + protected: + virtual std::string GetClassName() const; + + // Overridden from NativeButton2: + virtual void CreateWrapper(); + + private: + DISALLOW_COPY_AND_ASSIGN(RadioButton2); +}; + +} // namespace views + +#endif // #ifndef CHROME_VIEWS_CONTROLS_BUTTON_CHECKBOX2_H_ |