summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-20 23:46:40 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-20 23:46:40 +0000
commit37dbc0308b719e59de593e6cfb1a6aafa77d59c9 (patch)
treec0032afae7e637baa3980485eb82f5e5bb488c37
parentdbdbef7ec95b9749cc0eb13823d91a6653294796 (diff)
downloadchromium_src-37dbc0308b719e59de593e6cfb1a6aafa77d59c9.zip
chromium_src-37dbc0308b719e59de593e6cfb1a6aafa77d59c9.tar.gz
chromium_src-37dbc0308b719e59de593e6cfb1a6aafa77d59c9.tar.bz2
More button modifications
Review URL: http://codereview.chromium.org/50047 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12233 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/views/controls/button/checkbox2.cc94
-rw-r--r--chrome/views/controls/button/checkbox2.h19
-rw-r--r--chrome/views/controls/button/native_button2.cc2
-rw-r--r--chrome/views/controls/button/native_button_win.cc31
-rw-r--r--chrome/views/controls/button/native_button_win.h11
-rw-r--r--chrome/views/controls/button/native_button_wrapper.h6
6 files changed, 144 insertions, 19 deletions
diff --git a/chrome/views/controls/button/checkbox2.cc b/chrome/views/controls/button/checkbox2.cc
index d3b4786..426d0d8 100644
--- a/chrome/views/controls/button/checkbox2.cc
+++ b/chrome/views/controls/button/checkbox2.cc
@@ -4,6 +4,7 @@
#include "chrome/views/controls/button/checkbox2.h"
+#include "chrome/common/gfx/chrome_canvas.h"
#include "chrome/views/controls/label.h"
namespace views {
@@ -11,29 +12,33 @@ namespace views {
// static
const char Checkbox2::kViewClassName[] = "chrome/views/Checkbox";
+static const int kCheckboxLabelSpacing = 4;
+static const int kLabelFocusPaddingHorizontal = 2;
+static const int kLabelFocusPaddingVertical = 1;
+
////////////////////////////////////////////////////////////////////////////////
// Checkbox2, public:
Checkbox2::Checkbox2() : NativeButton2(NULL), checked_(false) {
- CreateLabel(std::wstring());
+ Init(std::wstring());
}
Checkbox2::Checkbox2(ButtonListener* listener)
: NativeButton2(listener),
checked_(false) {
- CreateLabel(std::wstring());
+ Init(std::wstring());
}
Checkbox2::Checkbox2(ButtonListener* listener, const std::wstring& label)
: NativeButton2(listener, label),
checked_(false) {
- CreateLabel(label);
+ Init(label);
}
Checkbox2::~Checkbox2() {
}
-void Checkbox2::SetMultiline(bool multiline) {
+void Checkbox2::SetMultiLine(bool multiline) {
label_->SetMultiLine(multiline);
}
@@ -48,10 +53,80 @@ void Checkbox2::SetChecked(bool checked) {
// Checkbox2, View overrides:
gfx::Size Checkbox2::GetPreferredSize() {
- return gfx::Size(120, 30);
+ gfx::Size prefsize = native_wrapper_->GetView()->GetPreferredSize();
+ prefsize.set_width(
+ prefsize.width() + kCheckboxLabelSpacing +
+ kLabelFocusPaddingHorizontal * 2);
+ gfx::Size label_prefsize = label_->GetPreferredSize();
+ prefsize.set_width(prefsize.width() + label_prefsize.width());
+ prefsize.set_height(
+ std::max(prefsize.height(),
+ label_prefsize.height() + kLabelFocusPaddingVertical * 2));
+ return prefsize;
}
void Checkbox2::Layout() {
+ if (!native_wrapper_)
+ return;
+
+ gfx::Size checkmark_prefsize = native_wrapper_->GetView()->GetPreferredSize();
+ int label_x = checkmark_prefsize.width() + kCheckboxLabelSpacing +
+ kLabelFocusPaddingHorizontal;
+ label_->SetBounds(
+ label_x, 0, std::max(0, width() - label_x - kLabelFocusPaddingHorizontal),
+ height());
+ int first_line_height = label_->GetFont().height();
+ native_wrapper_->GetView()->SetBounds(
+ 0, ((first_line_height - checkmark_prefsize.height()) / 2),
+ checkmark_prefsize.width(), checkmark_prefsize.height());
+ native_wrapper_->GetView()->Layout();
+}
+
+void Checkbox2::Paint(ChromeCanvas* canvas) {
+ // Paint the focus border manually since we don't want to send actual focus
+ // in to the inner view.
+ if (HasFocus()) {
+ gfx::Rect label_bounds = label_->bounds();
+ canvas->DrawFocusRect(
+ MirroredLeftPointForRect(label_bounds) - kLabelFocusPaddingHorizontal,
+ 0,
+ label_bounds.width() + kLabelFocusPaddingHorizontal * 2,
+ label_bounds.height() - kLabelFocusPaddingVertical * 2);
+ }
+}
+
+View* Checkbox2::GetViewForPoint(const gfx::Point& point) {
+ return GetViewForPoint(point, false);
+}
+
+View* Checkbox2::GetViewForPoint(const gfx::Point& point,
+ bool can_create_floating) {
+ return GetLocalBounds(true).Contains(point) ? this : NULL;
+}
+
+void Checkbox2::OnMouseEntered(const MouseEvent& e) {
+ native_wrapper_->SetPushed(HitTestLabel(e));
+}
+
+void Checkbox2::OnMouseMoved(const MouseEvent& e) {
+ native_wrapper_->SetPushed(HitTestLabel(e));
+}
+
+void Checkbox2::OnMouseExited(const MouseEvent& e) {
+ native_wrapper_->SetPushed(false);
+}
+
+bool Checkbox2::OnMousePressed(const MouseEvent& e) {
+ native_wrapper_->SetPushed(HitTestLabel(e));
+ return true;
+}
+
+void Checkbox2::OnMouseReleased(const MouseEvent& e, bool canceled) {
+ native_wrapper_->SetPushed(false);
+ if (!canceled & HitTestLabel(e)) {
+ SetChecked(!checked());
+ ButtonPressed();
+ }
}
std::string Checkbox2::GetClassName() const {
@@ -74,11 +149,18 @@ void Checkbox2::InitBorder() {
////////////////////////////////////////////////////////////////////////////////
// Checkbox2, private:
-void Checkbox2::CreateLabel(const std::wstring& label_text) {
+void Checkbox2::Init(const std::wstring& label_text) {
+ SetFocusable(true);
set_minimum_size(gfx::Size(0, 0));
label_ = new Label(label_text);
label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
AddChildView(label_);
}
+bool Checkbox2::HitTestLabel(const MouseEvent& e) {
+ gfx::Point tmp(e.location());
+ ConvertPointToView(this, label_, &tmp);
+ return label_->HitTest(tmp);
+}
+
} // namespace views
diff --git a/chrome/views/controls/button/checkbox2.h b/chrome/views/controls/button/checkbox2.h
index b3ef7cf..a10c993 100644
--- a/chrome/views/controls/button/checkbox2.h
+++ b/chrome/views/controls/button/checkbox2.h
@@ -26,15 +26,24 @@ class Checkbox2 : public NativeButton2 {
// If true, long lines are wrapped, and this is reflected in the preferred
// size returned by GetPreferredSize. If false, text that will not fit within
// the available bounds for the label will be cropped.
- void SetMultiline(bool multiline);
+ void SetMultiLine(bool multiline);
// Sets/Gets whether or not the checkbox is checked.
- void SetChecked(bool checked);
+ virtual void SetChecked(bool checked);
bool checked() const { return checked_; }
// Overridden from View:
virtual gfx::Size GetPreferredSize();
virtual void Layout();
+ virtual void Paint(ChromeCanvas* canvas);
+ virtual View* GetViewForPoint(const gfx::Point& point);
+ virtual View* GetViewForPoint(const gfx::Point& point,
+ bool can_create_floating);
+ virtual void OnMouseEntered(const MouseEvent& e);
+ virtual void OnMouseMoved(const MouseEvent& e);
+ virtual void OnMouseExited(const MouseEvent& e);
+ virtual bool OnMousePressed(const MouseEvent& e);
+ virtual void OnMouseReleased(const MouseEvent& e, bool canceled);
protected:
virtual std::string GetClassName() const;
@@ -45,7 +54,11 @@ class Checkbox2 : public NativeButton2 {
private:
// Called from the constructor to create and configure the checkbox label.
- void CreateLabel(const std::wstring& label_text);
+ void Init(const std::wstring& label_text);
+
+ // Returns true if the event (in Checkbox coordinates) is within the bounds of
+ // the label.
+ bool HitTestLabel(const MouseEvent& e);
// The checkbox's label. We don't use the OS version because of transparency
// and sizing issues.
diff --git a/chrome/views/controls/button/native_button2.cc b/chrome/views/controls/button/native_button2.cc
index 5478cdd..0a12e59 100644
--- a/chrome/views/controls/button/native_button2.cc
+++ b/chrome/views/controls/button/native_button2.cc
@@ -74,6 +74,8 @@ void NativeButton2::SetIsDefault(bool is_default) {
}
void NativeButton2::ButtonPressed() {
+ RequestFocus();
+
// TODO(beng): obtain mouse event flags for native buttons someday.
NotifyClick(mouse_event_flags());
}
diff --git a/chrome/views/controls/button/native_button_win.cc b/chrome/views/controls/button/native_button_win.cc
index 43c9134..6114259 100644
--- a/chrome/views/controls/button/native_button_win.cc
+++ b/chrome/views/controls/button/native_button_win.cc
@@ -5,8 +5,8 @@
#include "chrome/views/controls/button/native_button_win.h"
#include "base/logging.h"
-#include "chrome/views/controls/button/native_button2.h"
#include "chrome/views/controls/button/checkbox2.h"
+#include "chrome/views/controls/button/native_button2.h"
#include "chrome/views/widget/widget.h"
namespace views {
@@ -36,9 +36,11 @@ void NativeButtonWin::UpdateFont() {
}
void NativeButtonWin::UpdateDefault() {
- SendMessage(GetHWND(), BM_SETSTYLE,
- native_button_->is_default() ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON,
- true);
+ if (!IsCheckbox()) {
+ SendMessage(GetHWND(), BM_SETSTYLE,
+ native_button_->is_default() ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON,
+ true);
+ }
}
View* NativeButtonWin::GetView() {
@@ -98,6 +100,10 @@ void NativeButtonWin::NativeControlCreated(HWND control_hwnd) {
UpdateDefault();
}
+// We could obtain this from the theme, but that only works if themes are
+// active.
+static const int kCheckboxSize = 13; // pixels
+
////////////////////////////////////////////////////////////////////////////////
// NativeCheckboxWin, public:
@@ -110,6 +116,13 @@ NativeCheckboxWin::~NativeCheckboxWin() {
}
////////////////////////////////////////////////////////////////////////////////
+// NativeCheckboxWin, View overrides:
+
+gfx::Size NativeCheckboxWin::GetPreferredSize() {
+ return gfx::Size(kCheckboxSize, kCheckboxSize);
+}
+
+////////////////////////////////////////////////////////////////////////////////
// NativeCheckboxWin, NativeButtonWrapper implementation:
void NativeCheckboxWin::UpdateChecked() {
@@ -117,8 +130,8 @@ void NativeCheckboxWin::UpdateChecked() {
checkbox_->checked() ? BST_CHECKED : BST_UNCHECKED, 0);
}
-void NativeCheckboxWin::SetHighlight(bool highlight) {
- SendMessage(GetHWND(), BM_SETSTATE, highlight, 0);
+void NativeCheckboxWin::SetPushed(bool pushed) {
+ SendMessage(GetHWND(), BM_SETSTATE, pushed, 0);
}
////////////////////////////////////////////////////////////////////////////////
@@ -190,5 +203,11 @@ NativeButtonWrapper* NativeButtonWrapper::CreateCheckboxWrapper(
return new NativeCheckboxWin(checkbox);
}
+// static
+NativeButtonWrapper* NativeButtonWrapper::CreateRadioButtonWrapper(
+ RadioButton2* radio_button) {
+ return new NativeRadioButtonWin(radio_button);
+}
+
} // namespace views
diff --git a/chrome/views/controls/button/native_button_win.h b/chrome/views/controls/button/native_button_win.h
index 97eab47..4b34a20 100644
--- a/chrome/views/controls/button/native_button_win.h
+++ b/chrome/views/controls/button/native_button_win.h
@@ -37,6 +37,9 @@ class NativeButtonWin : public NativeControlWin,
virtual void CreateNativeControl();
virtual void NativeControlCreated(HWND control_hwnd);
+ // Returns true if this button is actually a checkbox or radio button.
+ virtual bool IsCheckbox() const { return false; }
+
private:
// The NativeButton we are bound to.
NativeButton2* native_button_;
@@ -50,9 +53,12 @@ class NativeCheckboxWin : public NativeButtonWin {
explicit NativeCheckboxWin(Checkbox2* native_button);
virtual ~NativeCheckboxWin();
+ // Overridden from View:
+ virtual gfx::Size GetPreferredSize();
+
// Overridden from NativeButtonWrapper:
virtual void UpdateChecked();
- virtual void SetHighlight(bool highlight);
+ virtual void SetPushed(bool pushed);
// Overridden from NativeControlWin:
virtual LRESULT ProcessMessage(UINT message,
@@ -62,6 +68,7 @@ class NativeCheckboxWin : public NativeButtonWin {
protected:
virtual void CreateNativeControl();
virtual void NativeControlCreated(HWND control_hwnd);
+ virtual bool IsCheckbox() const { return true; }
private:
// The Checkbox we are bound to.
@@ -83,7 +90,7 @@ class NativeRadioButtonWin : public NativeCheckboxWin {
private:
DISALLOW_COPY_AND_ASSIGN(NativeRadioButtonWin);
};
-
+
} // namespace views
#endif // #ifndef CHROME_VIEWS_CONTROLS_BUTTON_NATIVE_BUTTON_WIN_H_
diff --git a/chrome/views/controls/button/native_button_wrapper.h b/chrome/views/controls/button/native_button_wrapper.h
index 7eaacfe..3d1c932 100644
--- a/chrome/views/controls/button/native_button_wrapper.h
+++ b/chrome/views/controls/button/native_button_wrapper.h
@@ -32,8 +32,8 @@ class NativeButtonWrapper {
// associated NativeCheckbox. Valid only for checkboxes and radio buttons.
virtual void UpdateChecked() {}
- // Shows the hover state for the button if |highlight| is true.
- virtual void SetHighlight(bool highlight) {};
+ // Shows the pushed state for the button if |pushed| is true.
+ virtual void SetPushed(bool pushed) {};
// Retrieves the views::View that hosts the native control.
virtual View* GetView() = 0;
@@ -41,6 +41,8 @@ class NativeButtonWrapper {
// Creates an appropriate NativeButtonWrapper for the platform.
static NativeButtonWrapper* CreateNativeButtonWrapper(NativeButton2* button);
static NativeButtonWrapper* CreateCheckboxWrapper(Checkbox2* checkbox);
+ static NativeButtonWrapper* CreateRadioButtonWrapper(
+ RadioButton2* radio_button);
};