diff options
Diffstat (limited to 'chrome/views/controls/button/radio_button.cc')
-rw-r--r-- | chrome/views/controls/button/radio_button.cc | 142 |
1 files changed, 58 insertions, 84 deletions
diff --git a/chrome/views/controls/button/radio_button.cc b/chrome/views/controls/button/radio_button.cc index 0736189..fb09549 100644 --- a/chrome/views/controls/button/radio_button.cc +++ b/chrome/views/controls/button/radio_button.cc @@ -1,110 +1,65 @@ -// 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. +// 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_button.h" -#include "chrome/views/controls/label.h" -#include "chrome/views/controls/hwnd_view.h" #include "chrome/views/widget/root_view.h" namespace views { -// FIXME(ACW) there got be a better way to find out the check box sizes -static int kRadioWidth = 13; -static int kRadioHeight = 13; -static int kRadioToLabel = 4; - +// static const char RadioButton::kViewClassName[] = "chrome/views/RadioButton"; -RadioButton::RadioButton(const std::wstring& label, int group_id) - : CheckBox(label) { - SetGroup(group_id); -} +//////////////////////////////////////////////////////////////////////////////// +// RadioButton, public: -RadioButton::~RadioButton() { +RadioButton::RadioButton() : Checkbox() { } -HWND RadioButton::CreateNativeControl(HWND parent_container) { - HWND r = ::CreateWindowEx(GetAdditionalExStyle(), - L"BUTTON", - L"", - WS_CHILD | BS_RADIOBUTTON , - 0, 0, width(), height(), - parent_container, NULL, NULL, NULL); - ConfigureNativeButton(r); - return r; +RadioButton::RadioButton(const std::wstring& label) : Checkbox(label) { } -LRESULT RadioButton::OnCommand(UINT code, int id, HWND source) { - // Radio buttons can't be toggled off once selected except by clicking on - // another radio button within the same group, so we override this from - // CheckBox to prevent this from happening. - if (code == BN_CLICKED) { - RequestFocus(); - if (!IsSelected()) { - SetIsSelected(true); - return NativeButton::OnCommand(code, id, source); - } - } - return 0; -} - -// static -int RadioButton::GetTextIndent() { - return kRadioWidth + kRadioToLabel + kFocusPaddingHorizontal; -} - - -std::string RadioButton::GetClassName() const { - return kViewClassName; -} - -gfx::Size RadioButton::GetPreferredSize() { - gfx::Size prefsize = label_->GetPreferredSize(); - prefsize.set_height(std::max(prefsize.height() + kFocusPaddingVertical * 2, - kRadioHeight)); - prefsize.Enlarge(kRadioToLabel + kRadioWidth + kFocusPaddingHorizontal * 2, - 0); - return prefsize; +RadioButton::RadioButton(const std::wstring& label, int group_id) + : Checkbox(label) { + SetGroup(group_id); } -void RadioButton::Layout() { - int label_x = GetTextIndent(); - label_->SetBounds(label_x, 0, width() - label_x, height()); - if (hwnd_view_) { - int first_line_height = label_->GetFont().height(); - hwnd_view_->SetBounds(0, ((first_line_height - kRadioHeight) / 2) + 1, - kRadioWidth, kRadioHeight); - hwnd_view_->Layout(); - } +RadioButton::~RadioButton() { } -void RadioButton::SetIsSelected(bool f) { - if (f != IsSelected()) { - if (f) { - // 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) { - RadioButton* peer = static_cast<RadioButton*>(*i); - peer->SetIsSelected(false); - } +//////////////////////////////////////////////////////////////////////////////// +// RadioButton, Checkbox overrides: + +void RadioButton::SetChecked(bool checked) { + if (checked == RadioButton::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) { + RadioButton* peer = static_cast<RadioButton*>(*i); + peer->SetChecked(false); } } } - CheckBox::SetIsSelected(f); } + Checkbox::SetChecked(checked); + } +//////////////////////////////////////////////////////////////////////////////// +// RadioButton, View overrides: + View* RadioButton::GetSelectedViewForGroup(int group_id) { std::vector<View*> views; GetRootView()->GetViewsWithGroup(group_id, &views); @@ -114,10 +69,29 @@ View* RadioButton::GetSelectedViewForGroup(int group_id) { for (std::vector<View*>::const_iterator iter = views.begin(); iter != views.end(); ++iter) { RadioButton* radio_button = static_cast<RadioButton*>(*iter); - if (radio_button->IsSelected()) + if (radio_button->checked()) return radio_button; } return NULL; } +bool RadioButton::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 RadioButton::GetClassName() const { + return kViewClassName; +} + +//////////////////////////////////////////////////////////////////////////////// +// RadioButton, NativeButton overrides: + +void RadioButton::CreateWrapper() { + native_wrapper_ = NativeButtonWrapper::CreateRadioButtonWrapper(this); + native_wrapper_->UpdateLabel(); + native_wrapper_->UpdateChecked(); +} + } // namespace views |