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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
// 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/scrollbar/bitmap_scroll_bar.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "build/build_config.h"
#include "grit/ui_strings.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/canvas.h"
#include "ui/views/controls/menu/menu.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h"
#include "ui/views/widget/widget.h"
#if defined(OS_LINUX)
#include "ui/views/screen.h"
#endif
#undef min
#undef max
namespace views {
namespace {
// The distance the mouse can be dragged outside the bounds of the thumb during
// dragging before the scrollbar will snap back to its regular position.
const int kScrollThumbDragOutSnap = 100;
///////////////////////////////////////////////////////////////////////////////
//
// AutorepeatButton
//
// A button that activates on mouse pressed rather than released, and that
// continues to fire the clicked action as the mouse button remains pressed
// down on the button.
//
///////////////////////////////////////////////////////////////////////////////
class AutorepeatButton : public ImageButton {
public:
explicit AutorepeatButton(ButtonListener* listener)
: ImageButton(listener),
ALLOW_THIS_IN_INITIALIZER_LIST(repeater_(
base::Bind(&AutorepeatButton::NotifyClick,
base::Unretained(this)))) {
}
virtual ~AutorepeatButton() {}
protected:
virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE {
Button::NotifyClick(event);
repeater_.Start();
return true;
}
virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE {
OnMouseCaptureLost();
}
virtual void OnMouseCaptureLost() OVERRIDE {
repeater_.Stop();
}
private:
void NotifyClick() {
#if defined(OS_WIN)
gfx::Point cursor_point(GetMessagePos());
#elif defined(OS_LINUX)
gfx::Point cursor_point = gfx::Screen::GetCursorScreenPoint();
#endif
views::MouseEvent event(ui::ET_MOUSE_RELEASED,
cursor_point.x(), cursor_point.y(),
ui::EF_LEFT_MOUSE_BUTTON);
Button::NotifyClick(event);
}
// The repeat controller that we use to repeatedly click the button when the
// mouse button is down.
RepeatController repeater_;
DISALLOW_COPY_AND_ASSIGN(AutorepeatButton);
};
///////////////////////////////////////////////////////////////////////////////
//
// BitmapScrollBarThumb
//
// A view that acts as the thumb in the scroll bar track that the user can
// drag to scroll the associated contents view within the viewport.
//
///////////////////////////////////////////////////////////////////////////////
class BitmapScrollBarThumb : public BaseScrollBarThumb {
public:
explicit BitmapScrollBarThumb(BitmapScrollBar* scroll_bar)
: BaseScrollBarThumb(scroll_bar),
scroll_bar_(scroll_bar) {
}
virtual ~BitmapScrollBarThumb() { }
// View overrides:
virtual gfx::Size GetPreferredSize() OVERRIDE {
return gfx::Size(background_bitmap()->width(),
start_cap_bitmap()->height() +
end_cap_bitmap()->height() +
grippy_bitmap()->height());
}
protected:
// View overrides:
virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
canvas->DrawBitmapInt(*start_cap_bitmap(), 0, 0);
int top_cap_height = start_cap_bitmap()->height();
int bottom_cap_height = end_cap_bitmap()->height();
int thumb_body_height = height() - top_cap_height - bottom_cap_height;
canvas->TileImageInt(*background_bitmap(), 0, top_cap_height,
background_bitmap()->width(), thumb_body_height);
canvas->DrawBitmapInt(*end_cap_bitmap(), 0,
height() - bottom_cap_height);
// Paint the grippy over the track.
int grippy_x = (width() - grippy_bitmap()->width()) / 2;
int grippy_y = (thumb_body_height - grippy_bitmap()->height()) / 2;
canvas->DrawBitmapInt(*grippy_bitmap(), grippy_x, grippy_y);
}
private:
// Returns the image rendered at the start of the thumb.
gfx::ImageSkia* start_cap_bitmap() const {
return scroll_bar_->images_[BitmapScrollBar::THUMB_START_CAP][GetState()];
}
// Returns the image rendered at the end of the thumb.
gfx::ImageSkia* end_cap_bitmap() const {
return scroll_bar_->images_[BitmapScrollBar::THUMB_END_CAP][GetState()];
}
// Returns the image that is tiled in the background of the thumb between
// the start and the end caps.
gfx::ImageSkia* background_bitmap() const {
return scroll_bar_->images_[BitmapScrollBar::THUMB_MIDDLE][GetState()];
}
// Returns the image that is rendered in the middle of the thumb
// transparently over the background bitmap.
gfx::ImageSkia* grippy_bitmap() const {
return scroll_bar_->images_[BitmapScrollBar::THUMB_GRIPPY]
[CustomButton::BS_NORMAL];
}
// The BitmapScrollBar that owns us.
BitmapScrollBar* scroll_bar_;
DISALLOW_COPY_AND_ASSIGN(BitmapScrollBarThumb);
};
} // namespace
///////////////////////////////////////////////////////////////////////////////
// BitmapScrollBar, public:
BitmapScrollBar::BitmapScrollBar(bool horizontal, bool show_scroll_buttons)
: BaseScrollBar(horizontal, new BitmapScrollBarThumb(this)),
ALLOW_THIS_IN_INITIALIZER_LIST(prev_button_(new AutorepeatButton(this))),
ALLOW_THIS_IN_INITIALIZER_LIST(next_button_(new AutorepeatButton(this))),
show_scroll_buttons_(show_scroll_buttons) {
if (!show_scroll_buttons_) {
prev_button_->SetVisible(false);
next_button_->SetVisible(false);
}
AddChildView(prev_button_);
AddChildView(next_button_);
set_context_menu_controller(this);
prev_button_->set_context_menu_controller(this);
next_button_->set_context_menu_controller(this);
}
void BitmapScrollBar::SetImage(ScrollBarPart part,
CustomButton::ButtonState state,
gfx::ImageSkia* image_skia) {
DCHECK(part < PART_COUNT);
DCHECK(state < CustomButton::BS_COUNT);
switch (part) {
case PREV_BUTTON:
prev_button_->SetImage(state, image_skia);
break;
case NEXT_BUTTON:
next_button_->SetImage(state, image_skia);
break;
case THUMB_START_CAP:
case THUMB_MIDDLE:
case THUMB_END_CAP:
case THUMB_GRIPPY:
case THUMB_TRACK:
images_[part][state] = image_skia;
break;
}
}
int BitmapScrollBar::GetLayoutSize() const {
gfx::Size prefsize = prev_button_->GetPreferredSize();
return IsHorizontal() ? prefsize.height() : prefsize.width();
}
gfx::Rect BitmapScrollBar::GetTrackBounds() const {
gfx::Size prefsize = prev_button_->GetPreferredSize();
if (IsHorizontal()) {
if (!show_scroll_buttons_)
prefsize.set_width(0);
int new_width =
std::max(0, width() - (prefsize.width() * 2));
gfx::Rect track_bounds(prefsize.width(), 0, new_width, prefsize.height());
return track_bounds;
}
if (!show_scroll_buttons_)
prefsize.set_height(0);
gfx::Rect track_bounds(0, prefsize.height(), prefsize.width(),
std::max(0, height() - (prefsize.height() * 2)));
return track_bounds;
}
///////////////////////////////////////////////////////////////////////////////
// BitmapScrollBar, View implementation:
gfx::Size BitmapScrollBar::GetPreferredSize() {
// In this case, we're returning the desired width of the scrollbar and its
// minimum allowable height.
gfx::Size button_prefsize = prev_button_->GetPreferredSize();
return gfx::Size(button_prefsize.width(), button_prefsize.height() * 2);
}
void BitmapScrollBar::Layout() {
// Size and place the two scroll buttons.
if (show_scroll_buttons_) {
gfx::Size prefsize = prev_button_->GetPreferredSize();
prev_button_->SetBounds(0, 0, prefsize.width(), prefsize.height());
prefsize = next_button_->GetPreferredSize();
if (IsHorizontal()) {
next_button_->SetBounds(width() - prefsize.width(), 0, prefsize.width(),
prefsize.height());
} else {
next_button_->SetBounds(0, height() - prefsize.height(), prefsize.width(),
prefsize.height());
}
} else {
prev_button_->SetBounds(0, 0, 0, 0);
next_button_->SetBounds(0, 0, 0, 0);
}
BaseScrollBarThumb* thumb = GetThumb();
// Size and place the thumb
gfx::Size thumb_prefsize = thumb->GetPreferredSize();
gfx::Rect track_bounds = GetTrackBounds();
// Preserve the height/width of the thumb (depending on orientation) as set
// by the last call to |Update|, but coerce the width/height to be the
// appropriate value for the bitmaps provided.
if (IsHorizontal()) {
thumb->SetBounds(thumb->x(), thumb->y(), thumb->width(),
thumb_prefsize.height());
} else {
thumb->SetBounds(thumb->x(), thumb->y(), thumb_prefsize.width(),
thumb->height());
}
// Hide the thumb if the track isn't tall enough to display even a tiny
// thumb. The user can only use the mousewheel, scroll buttons or keyboard
// in this scenario.
if ((IsHorizontal() && (track_bounds.width() < thumb_prefsize.width()) ||
(!IsHorizontal() && (track_bounds.height() < thumb_prefsize.height())))) {
thumb->SetVisible(false);
} else if (!thumb->visible()) {
thumb->SetVisible(true);
}
}
///////////////////////////////////////////////////////////////////////////////
// BitmapScrollBar, View implementation:
void BitmapScrollBar::OnPaint(gfx::Canvas* canvas) {
// Paint the track.
gfx::Rect track_bounds = GetTrackBounds();
canvas->TileImageInt(*images_[THUMB_TRACK][GetThumbTrackState()],
track_bounds.x(), track_bounds.y(),
track_bounds.width(), track_bounds.height());
}
///////////////////////////////////////////////////////////////////////////////
// BitmapScrollBar, ButtonListener implementation:
void BitmapScrollBar::ButtonPressed(Button* sender, const views::Event& event) {
if (sender == prev_button_) {
ScrollByAmount(SCROLL_PREV_LINE);
} else if (sender == next_button_) {
ScrollByAmount(SCROLL_NEXT_LINE);
}
}
} // namespace views
|