Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: ui/views/controls/button/label_button_border.cc

Issue 12330002: Add views::Button style enum for LabelButton [native] styling, etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add views::Button style enum for LabelButton [native] styling, etc. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/button/label_button_border.h" 5 #include "ui/views/controls/button/label_button_border.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "grit/ui_resources.h" 8 #include "grit/ui_resources.h"
9 #include "ui/base/animation/animation.h" 9 #include "ui/base/animation/animation.h"
10 #include "ui/gfx/canvas.h" 10 #include "ui/gfx/canvas.h"
(...skipping 11 matching lines...) Expand all
22 static const int kPreferredPaddingVertical = 5; 22 static const int kPreferredPaddingVertical = 5;
23 23
24 // Preferred padding between content and edge for NativeTheme border. 24 // Preferred padding between content and edge for NativeTheme border.
25 static const int kPreferredNativeThemePaddingHorizontal = 12; 25 static const int kPreferredNativeThemePaddingHorizontal = 12;
26 static const int kPreferredNativeThemePaddingVertical = 5; 26 static const int kPreferredNativeThemePaddingVertical = 5;
27 27
28 // The default hot and pushed button image IDs; normal has none by default. 28 // The default hot and pushed button image IDs; normal has none by default.
29 const int kHotImages[] = IMAGE_GRID(IDR_TEXTBUTTON_HOVER); 29 const int kHotImages[] = IMAGE_GRID(IDR_TEXTBUTTON_HOVER);
30 const int kPushedImages[] = IMAGE_GRID(IDR_TEXTBUTTON_PRESSED); 30 const int kPushedImages[] = IMAGE_GRID(IDR_TEXTBUTTON_PRESSED);
31 31
32 CustomButton::ButtonState GetButtonState(ui::NativeTheme::State state) { 32 Button::ButtonState GetButtonState(ui::NativeTheme::State state) {
33 switch(state) { 33 switch(state) {
34 case ui::NativeTheme::kDisabled: return CustomButton::STATE_DISABLED; 34 case ui::NativeTheme::kDisabled: return Button::STATE_DISABLED;
35 case ui::NativeTheme::kHovered: return CustomButton::STATE_HOVERED; 35 case ui::NativeTheme::kHovered: return Button::STATE_HOVERED;
36 case ui::NativeTheme::kNormal: return CustomButton::STATE_NORMAL; 36 case ui::NativeTheme::kNormal: return Button::STATE_NORMAL;
37 case ui::NativeTheme::kPressed: return CustomButton::STATE_PRESSED; 37 case ui::NativeTheme::kPressed: return Button::STATE_PRESSED;
38 case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state; 38 case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state;
39 } 39 }
40 return CustomButton::STATE_NORMAL; 40 return Button::STATE_NORMAL;
41 } 41 }
42 42
43 // A helper function to paint the native theme or images as appropriate. 43 // A helper function to paint the native theme or images as appropriate.
44 void PaintHelper(LabelButtonBorder* border, 44 void PaintHelper(LabelButtonBorder* border,
45 gfx::Canvas* canvas, 45 gfx::Canvas* canvas,
46 const ui::NativeTheme* theme, 46 const ui::NativeTheme* theme,
47 ui::NativeTheme::Part part, 47 ui::NativeTheme::Part part,
48 ui::NativeTheme::State state, 48 ui::NativeTheme::State state,
49 const gfx::Rect& rect, 49 const gfx::Rect& rect,
50 const ui::NativeTheme::ExtraParams& extra) { 50 const ui::NativeTheme::ExtraParams& extra) {
51 if (border->native_theme()) { 51 if (border->style() == Button::STYLE_NATIVE_TEXTBUTTON) {
52 theme->Paint(canvas->sk_canvas(), part, state, rect, extra); 52 theme->Paint(canvas->sk_canvas(), part, state, rect, extra);
53 } else { 53 } else {
54 Painter* painter = border->GetPainter(GetButtonState(state)); 54 Painter* painter = border->GetPainter(GetButtonState(state));
55 if (painter) 55 if (painter)
56 painter->Paint(canvas, rect.size()); 56 painter->Paint(canvas, rect.size());
57 } 57 }
58 } 58 }
59 59
60 } // namespace 60 } // namespace
61 61
62 LabelButtonBorder::LabelButtonBorder() : native_theme_(false) { 62 LabelButtonBorder::LabelButtonBorder(Button::ButtonStyle style)
63 SetPainter(CustomButton::STATE_HOVERED, 63 : style_(style) {
64 Painter::CreateImageGridPainter(kHotImages)); 64 if (style == Button::STYLE_TEXTBUTTON) {
65 SetPainter(CustomButton::STATE_PRESSED, 65 SetPainter(Button::STATE_HOVERED,
66 Painter::CreateImageGridPainter(kPushedImages)); 66 Painter::CreateImageGridPainter(kHotImages));
67 SetPainter(Button::STATE_PRESSED,
68 Painter::CreateImageGridPainter(kPushedImages));
69 }
67 } 70 }
68 71
69 LabelButtonBorder::~LabelButtonBorder() {} 72 LabelButtonBorder::~LabelButtonBorder() {}
70 73
71 void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) { 74 void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) {
75 DCHECK(view.GetClassName() == LabelButton::kViewClassName);
72 const NativeThemeDelegate* native_theme_delegate = 76 const NativeThemeDelegate* native_theme_delegate =
73 static_cast<const LabelButton*>(&view); 77 static_cast<const LabelButton*>(&view);
74 ui::NativeTheme::Part part = native_theme_delegate->GetThemePart(); 78 ui::NativeTheme::Part part = native_theme_delegate->GetThemePart();
75 gfx::Rect rect(native_theme_delegate->GetThemePaintRect()); 79 gfx::Rect rect(native_theme_delegate->GetThemePaintRect());
76 ui::NativeTheme::State state; 80 ui::NativeTheme::State state;
77 ui::NativeTheme::ExtraParams extra; 81 ui::NativeTheme::ExtraParams extra;
78 const ui::NativeTheme* theme = view.GetNativeTheme(); 82 const ui::NativeTheme* theme = view.GetNativeTheme();
79 const ui::Animation* animation = native_theme_delegate->GetThemeAnimation(); 83 const ui::Animation* animation = native_theme_delegate->GetThemeAnimation();
80 84
81 if (animation && animation->is_animating()) { 85 if (animation && animation->is_animating()) {
82 // Paint the background state. 86 // Paint the background state.
83 state = native_theme_delegate->GetBackgroundThemeState(&extra); 87 state = native_theme_delegate->GetBackgroundThemeState(&extra);
84 PaintHelper(this, canvas, theme, part, state, rect, extra); 88 PaintHelper(this, canvas, theme, part, state, rect, extra);
85 89
86 // Composite the foreground state above the background state. 90 // Composite the foreground state above the background state.
87 const int alpha = animation->CurrentValueBetween(0, 255); 91 const int alpha = animation->CurrentValueBetween(0, 255);
88 canvas->SaveLayerAlpha(static_cast<uint8>(alpha)); 92 canvas->SaveLayerAlpha(static_cast<uint8>(alpha));
89 state = native_theme_delegate->GetForegroundThemeState(&extra); 93 state = native_theme_delegate->GetForegroundThemeState(&extra);
90 PaintHelper(this, canvas, theme, part, state, rect, extra); 94 PaintHelper(this, canvas, theme, part, state, rect, extra);
91 canvas->Restore(); 95 canvas->Restore();
92 } else { 96 } else {
93 state = native_theme_delegate->GetThemeState(&extra); 97 state = native_theme_delegate->GetThemeState(&extra);
94 PaintHelper(this, canvas, theme, part, state, rect, extra); 98 PaintHelper(this, canvas, theme, part, state, rect, extra);
95 } 99 }
96 100
97 // Draw the Views focus border for the native theme style. 101 // Draw the Views focus border for the native theme style.
98 if (native_theme() && view.focus_border() && extra.button.is_focused) 102 if (style() == Button::STYLE_NATIVE_TEXTBUTTON &&
103 view.focus_border() && extra.button.is_focused)
99 view.focus_border()->Paint(view, canvas); 104 view.focus_border()->Paint(view, canvas);
100 } 105 }
101 106
102 gfx::Insets LabelButtonBorder::GetInsets() const { 107 gfx::Insets LabelButtonBorder::GetInsets() const {
103 if (native_theme()) { 108 if (style() == Button::STYLE_NATIVE_TEXTBUTTON) {
104 return gfx::Insets(kPreferredNativeThemePaddingVertical, 109 return gfx::Insets(kPreferredNativeThemePaddingVertical,
105 kPreferredNativeThemePaddingHorizontal, 110 kPreferredNativeThemePaddingHorizontal,
106 kPreferredNativeThemePaddingVertical, 111 kPreferredNativeThemePaddingVertical,
107 kPreferredNativeThemePaddingHorizontal); 112 kPreferredNativeThemePaddingHorizontal);
108 } else {
109 return gfx::Insets(kPreferredPaddingVertical, kPreferredPaddingHorizontal,
110 kPreferredPaddingVertical, kPreferredPaddingHorizontal);
111 } 113 }
114 DCHECK_EQ(style(), Button::STYLE_TEXTBUTTON);
115 return gfx::Insets(kPreferredPaddingVertical, kPreferredPaddingHorizontal,
116 kPreferredPaddingVertical, kPreferredPaddingHorizontal);
112 } 117 }
113 118
114 Painter* LabelButtonBorder::GetPainter(CustomButton::ButtonState state) { 119 Painter* LabelButtonBorder::GetPainter(Button::ButtonState state) {
115 return painters_[state].get(); 120 return painters_[state].get();
116 } 121 }
117 122
118 void LabelButtonBorder::SetPainter(CustomButton::ButtonState state, 123 void LabelButtonBorder::SetPainter(Button::ButtonState state,
119 Painter* painter) { 124 Painter* painter) {
120 painters_[state].reset(painter); 125 painters_[state].reset(painter);
121 } 126 }
122 127
123 } // namespace views 128 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698