diff options
author | sadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-26 23:15:37 +0000 |
---|---|---|
committer | sadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-26 23:15:37 +0000 |
commit | 856b972eff8770d5c08ae8ca7dd452817a402e87 (patch) | |
tree | b3224e8ad14bbe3487c4efb9182ff28ef6a82b7c /ui | |
parent | f8560588a7791b90d24aac01112fbfd22a39bc9c (diff) | |
download | chromium_src-856b972eff8770d5c08ae8ca7dd452817a402e87.zip chromium_src-856b972eff8770d5c08ae8ca7dd452817a402e87.tar.gz chromium_src-856b972eff8770d5c08ae8ca7dd452817a402e87.tar.bz2 |
views: Add a slider control.
BUG=110130
TEST=none
Review URL: https://chromiumcodereview.appspot.com/9455082
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@123705 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/views/controls/slider.cc | 105 | ||||
-rw-r--r-- | ui/views/controls/slider.h | 56 | ||||
-rw-r--r-- | ui/views/examples/examples_window.cc | 2 | ||||
-rw-r--r-- | ui/views/examples/slider_example.cc | 44 | ||||
-rw-r--r-- | ui/views/examples/slider_example.h | 42 | ||||
-rw-r--r-- | ui/views/views.gyp | 4 |
6 files changed, 253 insertions, 0 deletions
diff --git a/ui/views/controls/slider.cc b/ui/views/controls/slider.cc new file mode 100644 index 0000000..64a8561 --- /dev/null +++ b/ui/views/controls/slider.cc @@ -0,0 +1,105 @@ +// Copyright (c) 2012 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 "ui/views/controls/slider.h" + +#include "base/logging.h" +#include "third_party/skia/include/core/SkColor.h" +#include "ui/gfx/canvas.h" +#include "ui/gfx/point.h" +#include "ui/gfx/rect.h" + +namespace views { + +Slider::Slider(SliderListener* listener, Orientation orientation) + : listener_(listener), + orientation_(orientation), + value_(0.f) { +} + +Slider::~Slider() { +} + +void Slider::SetValue(float value) { + if (value < 0.0) + value = 0.0; + else if (value > 1.0) + value = 1.0; + if (value_ == value) + return; + float old_value = value_; + value_ = value; + if (listener_) + listener_->SliderValueChanged(this, value_, old_value); + SchedulePaint(); +} + +gfx::Size Slider::GetPreferredSize() { + const int kSizeMajor = 200; + const int kSizeMinor = 40; + + if (orientation_ == HORIZONTAL) + return gfx::Size(kSizeMajor, kSizeMinor); + return gfx::Size(kSizeMinor, kSizeMajor); +} + +void Slider::OnPaint(gfx::Canvas* canvas) { + // TODO(sad): The painting code should use NativeTheme for various platforms. + const int kButtonMajor = 15; + const int kButtonMinor = 4; + const int kLineThickness = 5; + const SkColor kFullColor = SkColorSetARGB(125, 0, 0, 0); + const SkColor kEmptyColor = SkColorSetARGB(50, 0, 0, 0); + const SkColor kButtonColor = SK_ColorBLACK; + + if (orientation_ == HORIZONTAL) { + int w = width() - kButtonMinor; + int full = value_ * w; + int empty = w - full; + int y = height() / 2 - kLineThickness / 2; + canvas->FillRect(gfx::Rect(kButtonMinor / 2, y, + std::max(0, full - kButtonMinor / 2), + kLineThickness), + kFullColor); + canvas->FillRect(gfx::Rect(full + kButtonMinor / 2, y, + std::max(0, empty), kLineThickness), + kEmptyColor); + canvas->FillRect(gfx::Rect(std::max(0, full - kButtonMinor / 2), + y - (kButtonMajor - kLineThickness) / 2, + kButtonMinor, + kButtonMajor), + kButtonColor); + } else { + int h = height() - kButtonMinor; + int full = value_ * h; + int empty = h - full; + int x = width() / 2 - kLineThickness / 2; + canvas->FillRect(gfx::Rect(x, kButtonMinor / 2, + kLineThickness, + std::max(0, empty - kButtonMinor / 2)), + kEmptyColor); + canvas->FillRect(gfx::Rect(x, empty + kButtonMinor / 2, + kLineThickness, full), + kFullColor); + canvas->FillRect(gfx::Rect(x - (kButtonMajor - kLineThickness) / 2, + std::max(0, empty - kButtonMinor / 2), + kButtonMajor, + kButtonMinor), + kButtonColor); + } +} + +bool Slider::OnMousePressed(const views::MouseEvent& event) { + return OnMouseDragged(event); +} + +bool Slider::OnMouseDragged(const views::MouseEvent& event) { + if (orientation_ == HORIZONTAL) + SetValue(static_cast<float>(event.x()) / width()); + else + SetValue(1.0f - static_cast<float>(event.y()) / height()); + return true; +} + +} // namespace views diff --git a/ui/views/controls/slider.h b/ui/views/controls/slider.h new file mode 100644 index 0000000..c70147d --- /dev/null +++ b/ui/views/controls/slider.h @@ -0,0 +1,56 @@ +// Copyright (c) 2012 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 UI_VIEWS_CONTROLS_SLIDER_H_ +#define UI_VIEWS_CONTROLS_SLIDER_H_ +#pragma once + +#include "ui/views/view.h" +#include "ui/views/views_export.h" + +namespace views { + +class Slider; + +class VIEWS_EXPORT SliderListener { + public: + virtual void SliderValueChanged(Slider* sender, + float value, + float old_value) = 0; + + protected: + virtual ~SliderListener() {} +}; + +class VIEWS_EXPORT Slider : public View { + public: + enum Orientation { + HORIZONTAL, + VERTICAL + }; + + Slider(SliderListener* listener, Orientation orientation); + virtual ~Slider(); + + float value() const { return value_; } + void SetValue(float value); + + private: + // views::View overrides: + virtual gfx::Size GetPreferredSize() OVERRIDE; + virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; + virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; + virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE; + + SliderListener* listener_; + Orientation orientation_; + + float value_; + + DISALLOW_COPY_AND_ASSIGN(Slider); +}; + +} // namespace views + +#endif // UI_VIEWS_CONTROLS_SLIDER_H_ diff --git a/ui/views/examples/examples_window.cc b/ui/views/examples/examples_window.cc index ad125ce..f650b10 100644 --- a/ui/views/examples/examples_window.cc +++ b/ui/views/examples/examples_window.cc @@ -29,6 +29,7 @@ #include "ui/views/examples/radio_button_example.h" #include "ui/views/examples/scroll_view_example.h" #include "ui/views/examples/single_split_view_example.h" +#include "ui/views/examples/slider_example.h" #include "ui/views/examples/tabbed_pane_example.h" #include "ui/views/examples/table_example.h" #include "ui/views/examples/text_example.h" @@ -177,6 +178,7 @@ class ExamplesWindowContents : public WidgetDelegateView, combobox_model_.AddExample(new RadioButtonExample); combobox_model_.AddExample(new ScrollViewExample); combobox_model_.AddExample(new SingleSplitViewExample); + combobox_model_.AddExample(new SliderExample); combobox_model_.AddExample(new TabbedPaneExample); combobox_model_.AddExample(new TextExample); combobox_model_.AddExample(new TextfieldExample); diff --git a/ui/views/examples/slider_example.cc b/ui/views/examples/slider_example.cc new file mode 100644 index 0000000..a1ab11e --- /dev/null +++ b/ui/views/examples/slider_example.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2012 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 "ui/views/examples/slider_example.h" + +#include "base/stringprintf.h" +#include "base/utf_string_conversions.h" +#include "ui/views/controls/label.h" +#include "ui/views/layout/box_layout.h" +#include "ui/views/view.h" + +namespace views { +namespace examples { + +SliderExample::SliderExample() + : ExampleBase("Slider"), + slider_(NULL), + label_(NULL) { +} + +SliderExample::~SliderExample() { +} + +void SliderExample::CreateExampleView(View* container) { + label_ = new Label(); + slider_ = new Slider(this, Slider::HORIZONTAL); + + slider_->SetValue(0.5); + + container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 3, 3, 3)); + container->AddChildView(slider_); + container->AddChildView(label_); +} + +void SliderExample::SliderValueChanged(Slider* sender, + float value, + float old_value) { + label_->SetText(ASCIIToUTF16(base::StringPrintf("%.3lf", value))); +} + +} // namespace examples +} // namespace views + diff --git a/ui/views/examples/slider_example.h b/ui/views/examples/slider_example.h new file mode 100644 index 0000000..a94a00c --- /dev/null +++ b/ui/views/examples/slider_example.h @@ -0,0 +1,42 @@ +// Copyright (c) 2012 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 UI_VIEWS_EXAMPLES_SLIDER_EXAMPLE_H_ +#define UI_VIEWS_EXAMPLES_SLIDER_EXAMPLE_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "ui/views/controls/slider.h" +#include "ui/views/examples/example_base.h" + +namespace views { +class Label; + +namespace examples { + +class SliderExample : public ExampleBase, public SliderListener { + public: + SliderExample(); + virtual ~SliderExample(); + + // Overridden from ExampleBase: + virtual void CreateExampleView(View* container) OVERRIDE; + + private: + // Overridden from SliderListener: + virtual void SliderValueChanged(Slider* sender, + float value, + float old_value) OVERRIDE; + + Slider* slider_; + Label* label_; + + DISALLOW_COPY_AND_ASSIGN(SliderExample); +}; + +} // namespace examples +} // namespace views + +#endif // UI_VIEWS_EXAMPLES_SLIDER_EXAMPLE_H_ diff --git a/ui/views/views.gyp b/ui/views/views.gyp index 740b0cd..381838c 100644 --- a/ui/views/views.gyp +++ b/ui/views/views.gyp @@ -198,6 +198,8 @@ 'controls/single_split_view.cc', 'controls/single_split_view.h', 'controls/single_split_view_listener.h', + 'controls/slider.cc', + 'controls/slider.h', 'controls/tabbed_pane/native_tabbed_pane_gtk.cc', 'controls/tabbed_pane/native_tabbed_pane_gtk.h', 'controls/tabbed_pane/native_tabbed_pane_views.cc', @@ -660,6 +662,8 @@ 'examples/scroll_view_example.h', 'examples/single_split_view_example.cc', 'examples/single_split_view_example.h', + 'examples/slider_example.cc', + 'examples/slider_example.h', 'examples/tabbed_pane_example.cc', 'examples/tabbed_pane_example.h', 'examples/table_example.cc', |