summaryrefslogtreecommitdiffstats
path: root/views
diff options
context:
space:
mode:
authoroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-29 19:20:08 +0000
committeroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-29 19:20:08 +0000
commit36a9ad19d54426021747e1c292d697fa20b12fc4 (patch)
treeed0aafd8f64a0ad02f1ffe485ff1cf46eb1b869d /views
parent554436b584f8e49c7ede4d4a0b3dd0e4b49379bb (diff)
downloadchromium_src-36a9ad19d54426021747e1c292d697fa20b12fc4.zip
chromium_src-36a9ad19d54426021747e1c292d697fa20b12fc4.tar.gz
chromium_src-36a9ad19d54426021747e1c292d697fa20b12fc4.tar.bz2
Refactor RadioButton Image code for linux/gtk
so that DOMUI menu can use the same image. BUG=none TEST=none Review URL: http://codereview.chromium.org/3435033 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60972 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r--views/controls/menu/menu_item_view_gtk.cc84
-rw-r--r--views/controls/menu/radio_button_image_gtk.cc93
-rw-r--r--views/controls/menu/radio_button_image_gtk.h21
-rw-r--r--views/views.gyp2
4 files changed, 124 insertions, 76 deletions
diff --git a/views/controls/menu/menu_item_view_gtk.cc b/views/controls/menu/menu_item_view_gtk.cc
index 9f77390..73599e2 100644
--- a/views/controls/menu/menu_item_view_gtk.cc
+++ b/views/controls/menu/menu_item_view_gtk.cc
@@ -11,6 +11,7 @@
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "views/controls/button/text_button.h"
#include "views/controls/menu/menu_config.h"
+#include "views/controls/menu/radio_button_image_gtk.h"
#include "views/controls/menu/submenu_view.h"
namespace views {
@@ -22,23 +23,6 @@ static const SkColor kSelectedBackgroundColor = SkColorSetRGB(0xDC, 0xE4, 0xFA);
static const SkColor kSelectedBackgroundColor = SkColorSetRGB(246, 249, 253);
#endif
-// Size of the radio button inciator.
-static const int kSelectedIndicatorSize = 5;
-static const int kIndicatorSize = 10;
-
-// Used for the radio indicator. See theme_draw for details.
-static const double kGradientStop = .5;
-static const SkColor kGradient0 = SkColorSetRGB(255, 255, 255);
-static const SkColor kGradient1 = SkColorSetRGB(255, 255, 255);
-static const SkColor kGradient2 = SkColorSetRGB(0xD8, 0xD8, 0xD8);
-static const SkColor kBaseStroke = SkColorSetRGB(0x8F, 0x8F, 0x8F);
-static const SkColor kRadioButtonIndicatorGradient0 =
- SkColorSetRGB(0, 0, 0);
-static const SkColor kRadioButtonIndicatorGradient1 =
- SkColorSetRGB(0x83, 0x83, 0x83);
-
-static const SkColor kIndicatorStroke = SkColorSetRGB(0, 0, 0);
-
gfx::Size MenuItemView::GetPreferredSize() {
const gfx::Font& font = MenuConfig::instance().font;
// TODO(sky): this is a workaround until I figure out why font.height()
@@ -82,65 +66,13 @@ void MenuItemView::Paint(gfx::Canvas* canvas, bool for_drag) {
AdjustBoundsForRTLUI(&check_bounds);
canvas->DrawBitmapInt(*check, check_bounds.x(), check_bounds.y());
} else if (type_ == RADIO) {
- // This code comes from theme_draw.cc. See it for details.
- canvas->TranslateInt(
- icon_x,
- top_margin + (height() - top_margin - bottom_margin -
- kIndicatorSize) / 2);
-
- SkPoint gradient_points[3];
- gradient_points[0].set(SkIntToScalar(0), SkIntToScalar(0));
- gradient_points[1].set(
- SkIntToScalar(0),
- SkIntToScalar(static_cast<int>(kIndicatorSize * kGradientStop)));
- gradient_points[2].set(SkIntToScalar(0), SkIntToScalar(kIndicatorSize));
- SkColor gradient_colors[3] = { kGradient0, kGradient1, kGradient2 };
- SkShader* shader = SkGradientShader::CreateLinear(
- gradient_points, gradient_colors, NULL, arraysize(gradient_points),
- SkShader::kClamp_TileMode, NULL);
- SkPaint paint;
- paint.setStyle(SkPaint::kFill_Style);
- paint.setAntiAlias(true);
- paint.setShader(shader);
- shader->unref();
- int radius = kIndicatorSize / 2;
- canvas->AsCanvasSkia()->drawCircle(radius, radius, radius, paint);
-
- paint.setStrokeWidth(SkIntToScalar(0));
- paint.setShader(NULL);
- paint.setStyle(SkPaint::kStroke_Style);
- paint.setColor(kBaseStroke);
- canvas->AsCanvasSkia()->drawCircle(radius, radius, radius, paint);
-
- if (GetDelegate()->IsItemChecked(GetCommand())) {
- SkPoint selected_gradient_points[2];
- selected_gradient_points[0].set(SkIntToScalar(0), SkIntToScalar(0));
- selected_gradient_points[1].set(
- SkIntToScalar(0),
- SkIntToScalar(kSelectedIndicatorSize));
- SkColor selected_gradient_colors[2] = { kRadioButtonIndicatorGradient0,
- kRadioButtonIndicatorGradient1 };
- shader = SkGradientShader::CreateLinear(
- selected_gradient_points, selected_gradient_colors, NULL,
- arraysize(selected_gradient_points), SkShader::kClamp_TileMode, NULL);
- paint.setShader(shader);
- shader->unref();
- paint.setStyle(SkPaint::kFill_Style);
- canvas->AsCanvasSkia()->drawCircle(radius, radius,
- kSelectedIndicatorSize / 2, paint);
-
- paint.setStrokeWidth(SkIntToScalar(0));
- paint.setShader(NULL);
- paint.setStyle(SkPaint::kStroke_Style);
- paint.setColor(kIndicatorStroke);
- canvas->AsCanvasSkia()->drawCircle(radius, radius,
- kSelectedIndicatorSize / 2, paint);
- }
-
- canvas->TranslateInt(
- -icon_x,
- -(top_margin + (height() - top_margin - bottom_margin -
- kIndicatorSize) / 2));
+ const SkBitmap* image =
+ GetRadioButtonImage(GetDelegate()->IsItemChecked(GetCommand()));
+ canvas->DrawBitmapInt(*image,
+ icon_x,
+ top_margin +
+ (height() - top_margin - bottom_margin -
+ image->height()) / 2);
}
// Render the foreground.
diff --git a/views/controls/menu/radio_button_image_gtk.cc b/views/controls/menu/radio_button_image_gtk.cc
new file mode 100644
index 0000000..9ce9a38
--- /dev/null
+++ b/views/controls/menu/radio_button_image_gtk.cc
@@ -0,0 +1,93 @@
+// Copyright (c) 2010 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/menu/radio_button_image_gtk.h"
+
+#include "gfx/canvas_skia.h"
+#include "third_party/skia/include/effects/SkGradientShader.h"
+
+namespace {
+
+// Size of the radio button inciator.
+const int kSelectedIndicatorSize = 5;
+const int kIndicatorSize = 10;
+
+// Used for the radio indicator. See theme_draw for details.
+const double kGradientStop = .5;
+const SkColor kGradient0 = SkColorSetRGB(255, 255, 255);
+const SkColor kGradient1 = SkColorSetRGB(255, 255, 255);
+const SkColor kGradient2 = SkColorSetRGB(0xD8, 0xD8, 0xD8);
+const SkColor kBaseStroke = SkColorSetRGB(0x8F, 0x8F, 0x8F);
+const SkColor kRadioButtonIndicatorGradient0 = SkColorSetRGB(0, 0, 0);
+const SkColor kRadioButtonIndicatorGradient1 = SkColorSetRGB(0x83, 0x83, 0x83);
+const SkColor kIndicatorStroke = SkColorSetRGB(0, 0, 0);
+
+SkBitmap* CreateRadioButtonImage(bool selected) {
+ // + 2 (1px on each side) to cover rounding error.
+ gfx::CanvasSkia canvas(kIndicatorSize + 2, kIndicatorSize + 2, false);
+ canvas.TranslateInt(1, 1);
+
+ SkPoint gradient_points[3];
+ gradient_points[0].set(SkIntToScalar(0), SkIntToScalar(0));
+ gradient_points[1].set(
+ SkIntToScalar(0),
+ SkIntToScalar(static_cast<int>(kIndicatorSize * kGradientStop)));
+ gradient_points[2].set(SkIntToScalar(0), SkIntToScalar(kIndicatorSize));
+ SkColor gradient_colors[3] = { kGradient0, kGradient1, kGradient2 };
+ SkShader* shader = SkGradientShader::CreateLinear(
+ gradient_points, gradient_colors, NULL, arraysize(gradient_points),
+ SkShader::kClamp_TileMode, NULL);
+ SkPaint paint;
+ paint.setStyle(SkPaint::kFill_Style);
+ paint.setAntiAlias(true);
+ paint.setShader(shader);
+ shader->unref();
+ int radius = kIndicatorSize / 2;
+ canvas.drawCircle(radius, radius, radius, paint);
+
+ paint.setStrokeWidth(SkIntToScalar(0));
+ paint.setShader(NULL);
+ paint.setStyle(SkPaint::kStroke_Style);
+ paint.setColor(kBaseStroke);
+ canvas.drawCircle(radius, radius, radius, paint);
+
+ if (selected) {
+ SkPoint selected_gradient_points[2];
+ selected_gradient_points[0].set(SkIntToScalar(0), SkIntToScalar(0));
+ selected_gradient_points[1].set(
+ SkIntToScalar(0),
+ SkIntToScalar(kSelectedIndicatorSize));
+ SkColor selected_gradient_colors[2] = { kRadioButtonIndicatorGradient0,
+ kRadioButtonIndicatorGradient1 };
+ shader = SkGradientShader::CreateLinear(
+ selected_gradient_points, selected_gradient_colors, NULL,
+ arraysize(selected_gradient_points), SkShader::kClamp_TileMode, NULL);
+ paint.setShader(shader);
+ shader->unref();
+ paint.setStyle(SkPaint::kFill_Style);
+ canvas.drawCircle(radius, radius,
+ kSelectedIndicatorSize / 2, paint);
+
+ paint.setStrokeWidth(SkIntToScalar(0));
+ paint.setShader(NULL);
+ paint.setStyle(SkPaint::kStroke_Style);
+ paint.setColor(kIndicatorStroke);
+ canvas.drawCircle(radius, radius,
+ kSelectedIndicatorSize / 2, paint);
+ }
+ return new SkBitmap(canvas.ExtractBitmap());
+}
+
+} // namespace
+
+namespace views {
+
+const SkBitmap* GetRadioButtonImage(bool selected) {
+ static const SkBitmap* kRadioOn = CreateRadioButtonImage(true);
+ static const SkBitmap* kRadioOff = CreateRadioButtonImage(false);
+
+ return selected ? kRadioOn : kRadioOff;
+}
+
+} // namespace views;
diff --git a/views/controls/menu/radio_button_image_gtk.h b/views/controls/menu/radio_button_image_gtk.h
new file mode 100644
index 0000000..b606c34
--- /dev/null
+++ b/views/controls/menu/radio_button_image_gtk.h
@@ -0,0 +1,21 @@
+// Copyright (c) 2010 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_MENU_RADIO_BUTTON_IMAGE_H_
+#define VIEWS_CONTROLS_MENU_RADIO_BUTTON_IMAGE_H_
+#pragma once
+
+#include "third_party/skia/include/core/SkBitmap.h"
+
+namespace views {
+
+// Return the RadioButton image for given state.
+// It returns the "selected" image when |selected| is
+// true, or the "unselected" image if false.
+// The returned image is global object and should not be freed.
+const SkBitmap* GetRadioButtonImage(bool selected);
+
+} // namespace views
+
+#endif // VIEWS_CONTROLS_MENU_RADIO_BUTTON_IMAGE_H_
diff --git a/views/views.gyp b/views/views.gyp
index 8690022..af6096d 100644
--- a/views/views.gyp
+++ b/views/views.gyp
@@ -146,6 +146,8 @@
'controls/menu/native_menu_gtk.h',
'controls/menu/native_menu_win.cc',
'controls/menu/native_menu_win.h',
+ 'controls/menu/radio_button_image_gtk.cc',
+ 'controls/menu/radio_button_image_gtk.h',
'controls/menu/submenu_view.cc',
'controls/menu/submenu_view.h',
'controls/menu/view_menu_delegate.h',