1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// 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_BUTTON_TEXT_BUTTON_H_
#define VIEWS_CONTROLS_BUTTON_TEXT_BUTTON_H_
#pragma once
#include "gfx/font.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "views/border.h"
#include "views/controls/button/custom_button.h"
namespace views {
////////////////////////////////////////////////////////////////////////////////
//
// TextButtonBorder
//
// A Border subclass that paints a TextButton's background layer -
// basically the button frame in the hot/pushed states.
//
// Note that this type of button is not focusable by default and will not be
// part of the focus chain. Call SetFocusable(true) to make it part of the
// focus chain.
//
////////////////////////////////////////////////////////////////////////////////
class TextButtonBorder : public Border {
public:
TextButtonBorder();
virtual ~TextButtonBorder();
// Render the background for the provided view
virtual void Paint(const View& view, gfx::Canvas* canvas) const;
// Returns the insets for the border.
virtual void GetInsets(gfx::Insets* insets) const;
protected:
// Images
struct MBBImageSet {
SkBitmap* top_left;
SkBitmap* top;
SkBitmap* top_right;
SkBitmap* left;
SkBitmap* center;
SkBitmap* right;
SkBitmap* bottom_left;
SkBitmap* bottom;
SkBitmap* bottom_right;
};
MBBImageSet hot_set_;
MBBImageSet pushed_set_;
virtual void Paint(const View& view, gfx::Canvas* canvas,
const MBBImageSet& set) const;
private:
DISALLOW_COPY_AND_ASSIGN(TextButtonBorder);
};
////////////////////////////////////////////////////////////////////////////////
//
// TextButton
//
// A button which displays text and/or and icon that can be changed in
// response to actions. TextButton reserves space for the largest string
// passed to SetText. To reset the cached max size invoke ClearMaxTextSize.
//
////////////////////////////////////////////////////////////////////////////////
class TextButton : public CustomButton {
public:
// Enumeration of how the prefix ('&') character is processed. The default
// is |PREFIX_NONE|.
enum PrefixType {
// No special processing is done.
PREFIX_NONE,
// The character following the prefix character is not rendered specially.
PREFIX_HIDE,
// The character following the prefix character is underlined.
PREFIX_SHOW
};
TextButton(ButtonListener* listener, const std::wstring& text);
virtual ~TextButton();
// Call SetText once per string in your set of possible values at button
// creation time, so that it can contain the largest of them and avoid
// resizing the button when the text changes.
virtual void SetText(const std::wstring& text);
std::wstring text() const { return text_; }
enum TextAlignment {
ALIGN_LEFT,
ALIGN_CENTER,
ALIGN_RIGHT
};
void set_alignment(TextAlignment alignment) { alignment_ = alignment; }
void set_prefix_type(PrefixType type) { prefix_type_ = type; }
// Sets the icon.
void SetIcon(const SkBitmap& icon);
SkBitmap icon() const { return icon_; }
void SetHoverIcon(const SkBitmap& icon);
SkBitmap icon_hover() const { return icon_hover_; }
// Meanings are reversed for right-to-left layouts.
enum IconPlacement {
ICON_ON_LEFT,
ICON_ON_RIGHT
};
IconPlacement icon_placement() { return icon_placement_; }
void set_icon_placement(IconPlacement icon_placement) {
icon_placement_ = icon_placement;
}
// TextButton remembers the maximum display size of the text passed to
// SetText. This method resets the cached maximum display size to the
// current size.
void ClearMaxTextSize();
void set_max_width(int max_width) { max_width_ = max_width; }
void SetFont(const gfx::Font& font);
void SetEnabledColor(SkColor color);
void SetDisabledColor(SkColor color);
void SetHighlightColor(SkColor color);
void SetHoverColor(SkColor color);
void SetNormalHasBorder(bool normal_has_border);
// Sets whether or not to show the highlighed (i.e. hot) state. Default true.
void SetShowHighlighted(bool show_highlighted);
// Paint the button into the specified canvas. If |for_drag| is true, the
// function paints a drag image representation into the canvas.
virtual void Paint(gfx::Canvas* canvas, bool for_drag);
// Overridden from View:
virtual gfx::Size GetPreferredSize();
virtual gfx::Size GetMinimumSize();
virtual void SetEnabled(bool enabled);
// Text colors.
static const SkColor kEnabledColor;
static const SkColor kHighlightColor;
static const SkColor kDisabledColor;
static const SkColor kHoverColor;
protected:
virtual void Paint(gfx::Canvas* canvas);
// Called when enabled or disabled state changes, or the colors for those
// states change.
virtual void UpdateColor();
private:
// Updates text_size_ and max_text_size_ from the current text/font. This is
// invoked when the font or text changes.
void UpdateTextSize();
// The text string that is displayed in the button.
std::wstring text_;
// The size of the text string.
gfx::Size text_size_;
// Track the size of the largest text string seen so far, so that
// changing text_ will not resize the button boundary.
gfx::Size max_text_size_;
// The alignment of the text string within the button.
TextAlignment alignment_;
// The position of the icon.
IconPlacement icon_placement_;
// The font used to paint the text.
gfx::Font font_;
// Text color.
SkColor color_;
// State colors.
SkColor color_enabled_;
SkColor color_disabled_;
SkColor color_highlight_;
SkColor color_hover_;
// An icon displayed with the text.
SkBitmap icon_;
// An optional different version of the icon for hover state.
SkBitmap icon_hover_;
bool has_hover_icon_;
// The width of the button will never be larger than this value. A value <= 0
// indicates the width is not constrained.
int max_width_;
// This is true if normal state has a border frame; default is false.
bool normal_has_border_;
// Whether or not to show the highlighted (i.e. hot) state.
bool show_highlighted_;
PrefixType prefix_type_;
DISALLOW_COPY_AND_ASSIGN(TextButton);
};
} // namespace views
#endif // VIEWS_CONTROLS_BUTTON_TEXT_BUTTON_H_
|