summaryrefslogtreecommitdiffstats
path: root/views
diff options
context:
space:
mode:
authorsaintlou@chromium.org <saintlou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-03 03:09:24 +0000
committersaintlou@chromium.org <saintlou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-03 03:09:24 +0000
commitcc643fd82eceeae19797daa0e09bb8b9bde54c81 (patch)
tree017775620e75b8a4c22619a2bbeacb237b23a064 /views
parentf3086a40d46f8e20577c7db6e1d3ffb2801be8ba (diff)
downloadchromium_src-cc643fd82eceeae19797daa0e09bb8b9bde54c81.zip
chromium_src-cc643fd82eceeae19797daa0e09bb8b9bde54c81.tar.gz
chromium_src-cc643fd82eceeae19797daa0e09bb8b9bde54c81.tar.bz2
Factored code drawing the focusable border used in some Views controls
BUG=none TEST=none Review URL: http://codereview.chromium.org/6910009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@83849 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r--views/controls/combobox/native_combobox_views.cc69
-rw-r--r--views/controls/combobox/native_combobox_views.h29
-rw-r--r--views/controls/focusable_border.cc74
-rw-r--r--views/controls/focusable_border.h47
-rw-r--r--views/controls/textfield/native_textfield_views.cc61
-rw-r--r--views/controls/textfield/native_textfield_views.h27
-rw-r--r--views/views.gyp2
7 files changed, 137 insertions, 172 deletions
diff --git a/views/controls/combobox/native_combobox_views.cc b/views/controls/combobox/native_combobox_views.cc
index 492b1a7..4a77f7a 100644
--- a/views/controls/combobox/native_combobox_views.cc
+++ b/views/controls/combobox/native_combobox_views.cc
@@ -15,8 +15,9 @@
#include "ui/gfx/path.h"
#include "views/background.h"
#include "views/border.h"
-#include "views/controls/menu/menu_2.h"
#include "views/controls/combobox/combobox.h"
+#include "views/controls/focusable_border.h"
+#include "views/controls/menu/menu_2.h"
#if defined(OS_LINUX)
#include "ui/gfx/gtk_util.h"
@@ -41,16 +42,11 @@ const int kComboboxArrowSize = 9;
const int kComboboxArrowOffset = 7;
const int kComboboxArrowMargin = 12;
-// Color settings for text, border, backgrounds and cursor.
+// Color settings for text and border.
// These are tentative, and should be derived from theme, system
// settings and current settings.
-const SkColor kSelectedTextColor = SK_ColorWHITE;
-const SkColor kReadonlyTextColor = SK_ColorDKGRAY;
-const SkColor kFocusedSelectionColor = SK_ColorBLUE;
-const SkColor kUnfocusedSelectionColor = SK_ColorLTGRAY;
-const SkColor kFocusedBorderColor = SK_ColorCYAN;
const SkColor kDefaultBorderColor = SK_ColorGRAY;
-const SkColor kCursorColor = SK_ColorBLACK;
+const SkColor kTextColor = SK_ColorBLACK;
// A switch to enable NativeTextfieldViews;
const char kEnableComboboxViewsSwitch[] = "enable-combobox-views";
@@ -63,7 +59,7 @@ const char NativeComboboxViews::kViewClassName[] =
NativeComboboxViews::NativeComboboxViews(Combobox* parent)
: combobox_(parent),
- text_border_(new ComboboxBorder()),
+ text_border_(new FocusableBorder()),
dropdown_open_(false),
selected_item_(-1),
content_width_(0),
@@ -247,7 +243,7 @@ void NativeComboboxViews::DrawArrow(gfx::Canvas* canvas,
int shift_y) const {
SkPaint paint;
paint.setStyle(SkPaint::kStrokeAndFill_Style);
- paint.setColor(kCursorColor);
+ paint.setColor(kTextColor);
paint.setAntiAlias(true);
gfx::Path path;
path.incReserve(4);
@@ -269,7 +265,7 @@ void NativeComboboxViews::PaintText(gfx::Canvas* canvas) {
int x = insets.left();
int y = insets.top();
int text_height = height() - insets.height();
- SkColor text_color = kCursorColor;
+ SkColor text_color = kTextColor;
int index = GetSelectedItem();
if (index < 0 || index > combobox_->model()->GetItemCount())
@@ -334,55 +330,4 @@ void NativeComboboxViews::ShowDropDownMenu() {
}
}
-///////////////////////////////////////////////////////////////////////////////
-//
-// ComboboxBorder
-//
-///////////////////////////////////////////////////////////////////////////////
-
-NativeComboboxViews::ComboboxBorder::ComboboxBorder()
- : has_focus_(false),
- insets_(kTopInsetSize, kLeftInsetSize, kBottomInsetSize, kRightInsetSize)
-{
-}
-
-void NativeComboboxViews::ComboboxBorder::Paint(
- const View& view, gfx::Canvas* canvas) const {
- SkRect rect;
- rect.set(SkIntToScalar(0), SkIntToScalar(0),
- SkIntToScalar(view.width()), SkIntToScalar(view.height()));
- SkScalar corners[8] = {
- // top-left
- SkIntToScalar(insets_.left()),
- SkIntToScalar(insets_.top()),
- // top-right
- SkIntToScalar(insets_.right()),
- SkIntToScalar(insets_.top()),
- // bottom-right
- SkIntToScalar(insets_.right()),
- SkIntToScalar(insets_.bottom()),
- // bottom-left
- SkIntToScalar(insets_.left()),
- SkIntToScalar(insets_.bottom()),
- };
- SkPath path;
- path.addRoundRect(rect, corners);
- SkPaint paint;
- paint.setStyle(SkPaint::kStroke_Style);
- paint.setFlags(SkPaint::kAntiAlias_Flag);
- paint.setColor(has_focus_ ? kFocusedBorderColor : kDefaultBorderColor);
- paint.setStrokeWidth(SkIntToScalar(has_focus_ ? 2 : 1));
- canvas->AsCanvasSkia()->drawPath(path, paint);
-}
-
-void NativeComboboxViews::ComboboxBorder::GetInsets(gfx::Insets* insets) const
-{
- *insets = insets_;
-}
-
-void NativeComboboxViews::ComboboxBorder::SetInsets(int top, int left,
- int bottom, int right) {
- insets_.Set(top, left, bottom, right);
-}
-
} // namespace views
diff --git a/views/controls/combobox/native_combobox_views.h b/views/controls/combobox/native_combobox_views.h
index f74adc4..7891731 100644
--- a/views/controls/combobox/native_combobox_views.h
+++ b/views/controls/combobox/native_combobox_views.h
@@ -19,6 +19,7 @@ namespace views {
class KeyEvent;
class Menu2;
+class FocusableBorder;
// A views/skia only implementation of NativeComboboxWrapper.
// No platform specific code is used.
@@ -69,32 +70,6 @@ class NativeComboboxViews : public views::View,
static void SetEnableComboboxViews(bool enabled);
private:
-
- // A Border class to draw focus border for the text field.
- // TODO(saintlou): refactor with NativeTextfieldViews
- class ComboboxBorder : public Border {
- public:
- ComboboxBorder();
-
- // Border implementation.
- virtual void Paint(const View& view, gfx::Canvas* canvas) const OVERRIDE;
- virtual void GetInsets(gfx::Insets* insets) const OVERRIDE;
-
- // Sets the insets of the border.
- void SetInsets(int top, int left, int bottom, int right);
-
- // Sets the focus state.
- void set_has_focus(bool has_focus) {
- has_focus_ = has_focus;
- }
-
- private:
- bool has_focus_;
- gfx::Insets insets_;
-
- DISALLOW_COPY_AND_ASSIGN(ComboboxBorder);
- };
-
// Returns the Combobox's font.
const gfx::Font& GetFont() const;
@@ -112,7 +87,7 @@ class NativeComboboxViews : public views::View,
Combobox* combobox_;
// The reference to the border class. The object is owned by View::border_.
- ComboboxBorder* text_border_;
+ FocusableBorder* text_border_;
// Context menu and its content list for the combobox.
scoped_ptr<ui::SimpleMenuModel> dropdown_list_model_;
diff --git a/views/controls/focusable_border.cc b/views/controls/focusable_border.cc
new file mode 100644
index 0000000..8c5e6b6
--- /dev/null
+++ b/views/controls/focusable_border.cc
@@ -0,0 +1,74 @@
+// Copyright (c) 2011 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/focusable_border.h"
+
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/canvas_skia.h"
+#include "ui/gfx/insets.h"
+
+namespace {
+
+// Define the size of the insets
+const int kTopInsetSize = 4;
+const int kLeftInsetSize = 4;
+const int kBottomInsetSize = 4;
+const int kRightInsetSize = 4;
+
+// Color settings for border.
+// These are tentative, and should be derived from theme, system
+// settings and current settings.
+// TODO(saintlou): understand why this is not factored/shared somewhere
+const SkColor kFocusedBorderColor = SK_ColorCYAN;
+const SkColor kDefaultBorderColor = SK_ColorGRAY;
+
+} // namespace
+
+namespace views {
+
+FocusableBorder::FocusableBorder()
+ : has_focus_(false),
+ insets_(kTopInsetSize, kLeftInsetSize,
+ kBottomInsetSize, kRightInsetSize) {
+}
+
+void FocusableBorder::Paint(const View& view, gfx::Canvas* canvas) const {
+ SkRect rect;
+ rect.set(SkIntToScalar(0), SkIntToScalar(0),
+ SkIntToScalar(view.width()), SkIntToScalar(view.height()));
+ SkScalar corners[8] = {
+ // top-left
+ SkIntToScalar(insets_.left()),
+ SkIntToScalar(insets_.top()),
+ // top-right
+ SkIntToScalar(insets_.right()),
+ SkIntToScalar(insets_.top()),
+ // bottom-right
+ SkIntToScalar(insets_.right()),
+ SkIntToScalar(insets_.bottom()),
+ // bottom-left
+ SkIntToScalar(insets_.left()),
+ SkIntToScalar(insets_.bottom()),
+ };
+ SkPath path;
+ path.addRoundRect(rect, corners);
+ SkPaint paint;
+ paint.setStyle(SkPaint::kStroke_Style);
+ paint.setFlags(SkPaint::kAntiAlias_Flag);
+ // TODO(oshima): Copy what WebKit does for focused border.
+ paint.setColor(has_focus_ ? kFocusedBorderColor : kDefaultBorderColor);
+ paint.setStrokeWidth(SkIntToScalar(has_focus_ ? 2 : 1));
+
+ canvas->AsCanvasSkia()->drawPath(path, paint);
+}
+
+void FocusableBorder::GetInsets(gfx::Insets* insets) const {
+ *insets = insets_;
+}
+
+void FocusableBorder::SetInsets(int top, int left, int bottom, int right) {
+ insets_.Set(top, left, bottom, right);
+}
+
+} // namespace views
diff --git a/views/controls/focusable_border.h b/views/controls/focusable_border.h
new file mode 100644
index 0000000..59f9e45
--- /dev/null
+++ b/views/controls/focusable_border.h
@@ -0,0 +1,47 @@
+// Copyright (c) 2011 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_FOCUSABLE_BORDER_H_
+#define VIEWS_CONTROLS_FOCUSABLE_BORDER_H_
+#pragma once
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "views/border.h"
+#include "views/view.h"
+
+namespace gfx {
+class Canvas;
+class Insets;
+}
+
+namespace views {
+
+// A Border class to draw a focused border around a field (e.g textfield).
+class FocusableBorder : public Border {
+ public:
+ FocusableBorder();
+
+ // Sets the insets of the border.
+ void SetInsets(int top, int left, int bottom, int right);
+
+ // Sets the focus state.
+ void set_has_focus(bool has_focus) {
+ has_focus_ = has_focus;
+ }
+
+ // Overridden from Border:
+ virtual void Paint(const View& view, gfx::Canvas* canvas) const OVERRIDE;
+ virtual void GetInsets(gfx::Insets* insets) const OVERRIDE;
+
+ private:
+ bool has_focus_;
+ gfx::Insets insets_;
+
+ DISALLOW_COPY_AND_ASSIGN(FocusableBorder);
+};
+
+} // namespace views
+
+#endif // VIEWS_CONTROLS_FOCUSABLE_BORDER_H_
diff --git a/views/controls/textfield/native_textfield_views.cc b/views/controls/textfield/native_textfield_views.cc
index cf3e14a..546b188 100644
--- a/views/controls/textfield/native_textfield_views.cc
+++ b/views/controls/textfield/native_textfield_views.cc
@@ -18,6 +18,7 @@
#include "ui/gfx/insets.h"
#include "views/background.h"
#include "views/border.h"
+#include "views/controls/focusable_border.h"
#include "views/controls/menu/menu_2.h"
#include "views/controls/textfield/textfield.h"
#include "views/controls/textfield/textfield_controller.h"
@@ -36,15 +37,13 @@ namespace {
// A global flag to switch the Textfield wrapper to TextfieldViews.
bool textfield_view_enabled = false;
-// Color setttings for text, border, backgrounds and cursor.
+// Color setttings for text, backgrounds and cursor.
// These are tentative, and should be derived from theme, system
// settings and current settings.
const SkColor kSelectedTextColor = SK_ColorWHITE;
const SkColor kReadonlyTextColor = SK_ColorDKGRAY;
const SkColor kFocusedSelectionColor = SK_ColorBLUE;
const SkColor kUnfocusedSelectionColor = SK_ColorLTGRAY;
-const SkColor kFocusedBorderColor = SK_ColorCYAN;
-const SkColor kDefaultBorderColor = SK_ColorGRAY;
const SkColor kCursorColor = SK_ColorBLACK;
// Parameters to control cursor blinking.
@@ -63,7 +62,7 @@ const char NativeTextfieldViews::kViewClassName[] =
NativeTextfieldViews::NativeTextfieldViews(Textfield* parent)
: textfield_(parent),
ALLOW_THIS_IN_INITIALIZER_LIST(model_(new TextfieldViewsModel(this))),
- text_border_(new TextfieldBorder()),
+ text_border_(new FocusableBorder()),
text_offset_(0),
insert_(true),
is_cursor_visible_(false),
@@ -965,58 +964,4 @@ bool NativeTextfieldViews::ShouldInsertChar(char16 ch, int flags) {
(flags & ~(ui::EF_SHIFT_DOWN | ui::EF_CAPS_LOCK_DOWN)) != ui::EF_ALT_DOWN;
}
-///////////////////////////////////////////////////////////////////////////////
-//
-// TextifieldBorder
-//
-///////////////////////////////////////////////////////////////////////////////
-
-NativeTextfieldViews::TextfieldBorder::TextfieldBorder()
- : has_focus_(false),
- insets_(4, 4, 4, 4) {
-}
-
-void NativeTextfieldViews::TextfieldBorder::Paint(
- const View& view, gfx::Canvas* canvas) const {
- SkRect rect;
- rect.set(SkIntToScalar(0), SkIntToScalar(0),
- SkIntToScalar(view.width()), SkIntToScalar(view.height()));
- SkScalar corners[8] = {
- // top-left
- SkIntToScalar(insets_.left()),
- SkIntToScalar(insets_.top()),
- // top-right
- SkIntToScalar(insets_.right()),
- SkIntToScalar(insets_.top()),
- // bottom-right
- SkIntToScalar(insets_.right()),
- SkIntToScalar(insets_.bottom()),
- // bottom-left
- SkIntToScalar(insets_.left()),
- SkIntToScalar(insets_.bottom()),
- };
- SkPath path;
- path.addRoundRect(rect, corners);
- SkPaint paint;
- paint.setStyle(SkPaint::kStroke_Style);
- paint.setFlags(SkPaint::kAntiAlias_Flag);
- // TODO(oshima): Copy what WebKit does for focused border.
- paint.setColor(has_focus_ ? kFocusedBorderColor : kDefaultBorderColor);
- paint.setStrokeWidth(SkIntToScalar(has_focus_ ? 2 : 1));
-
- canvas->AsCanvasSkia()->drawPath(path, paint);
-}
-
-void NativeTextfieldViews::TextfieldBorder::GetInsets(gfx::Insets* insets) const
-{
- *insets = insets_;
-}
-
-void NativeTextfieldViews::TextfieldBorder::SetInsets(int top,
- int left,
- int bottom,
- int right) {
- insets_.Set(top, left, bottom, right);
-}
-
} // namespace views
diff --git a/views/controls/textfield/native_textfield_views.h b/views/controls/textfield/native_textfield_views.h
index 1803e21..69385049c 100644
--- a/views/controls/textfield/native_textfield_views.h
+++ b/views/controls/textfield/native_textfield_views.h
@@ -26,6 +26,7 @@ class Canvas;
namespace views {
+class FocusableBorder;
class KeyEvent;
class Menu2;
@@ -127,30 +128,6 @@ class NativeTextfieldViews : public views::View,
private:
friend class NativeTextfieldViewsTest;
- // A Border class to draw focus border for the text field.
- class TextfieldBorder : public Border {
- public:
- TextfieldBorder();
-
- // Border implementation.
- virtual void Paint(const View& view, gfx::Canvas* canvas) const;
- virtual void GetInsets(gfx::Insets* insets) const;
-
- // Sets the insets of the border.
- void SetInsets(int top, int left, int bottom, int right);
-
- // Sets the focus state.
- void set_has_focus(bool has_focus) {
- has_focus_ = has_focus;
- }
-
- private:
- bool has_focus_;
- gfx::Insets insets_;
-
- DISALLOW_COPY_AND_ASSIGN(TextfieldBorder);
- };
-
// Overridden from TextInputClient:
virtual void SetCompositionText(
const ui::CompositionText& composition) OVERRIDE;
@@ -245,7 +222,7 @@ class NativeTextfieldViews : public views::View,
scoped_ptr<TextfieldViewsModel> model_;
// The reference to the border class. The object is owned by View::border_.
- TextfieldBorder* text_border_;
+ FocusableBorder* text_border_;
// The x offset for the text to be drawn, without insets;
int text_offset_;
diff --git a/views/views.gyp b/views/views.gyp
index 64cffe1..58d366d 100644
--- a/views/views.gyp
+++ b/views/views.gyp
@@ -116,6 +116,8 @@
'controls/combobox/native_combobox_win.cc',
'controls/combobox/native_combobox_win.h',
'controls/combobox/native_combobox_wrapper.h',
+ 'controls/focusable_border.cc',
+ 'controls/focusable_border.h',
'controls/image_view.cc',
'controls/image_view.h',
'controls/label.cc',