-
Notifications
You must be signed in to change notification settings - Fork 5
/
scroll_view.cc
178 lines (157 loc) · 5.12 KB
/
scroll_view.cc
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
// Copyright...
#include <stdio.h>
#include "scroll_view.h"
namespace pdfsketch {
ScrollView::ScrollView() {
h_scroller_.SetResizeParams(false, false, true, false);
h_scroller_.SetDelegate(this);
v_scroller_.SetResizeParams(true, false, false, false);
v_scroller_.SetDelegate(this);
AddSubview(&clip_view_);
}
void ScrollView::SetDocumentView(View* document) {
if (document_) {
printf("already have document!\n");
return;
}
document_ = document;
document_->SetDelegate(this);
clip_view_.AddSubview(document);
document->SetOrigin(Point());
h_scroller_.SetDocSize(0.0, document_->size().width_);
v_scroller_.SetDocSize(0.0, document_->size().height_);
}
void ScrollView::RepositionSubviews() {
if (!document_) {
// nothing to really do
return;
}
// todo: update doc size w/ event, not every call here
h_scroller_.SetDocSize(0.0, document_->size().width_);
v_scroller_.SetDocSize(0.0, document_->size().height_);
bool use_h = false;
bool use_v = false;
// Which scroll bars are needed?
if (document_->size().height_ <= size_.height_ &&
document_->size().width_ <= size_.width_) {
// no scroll bars needed
} else if (document_->size().height_ > size_.height_ &&
document_->size().width_ <=
size_.width_ - ScrollBarView::kThickness) {
use_v = true;
} else if (document_->size().width_ > size_.width_ &&
document_->size().height_ <=
size_.height_ - ScrollBarView::kThickness) {
use_h = true;
} else {
use_v = use_h = true;
}
if (use_h != h_visible_) {
if (use_h) {
AddSubview(&h_scroller_);
} else {
RemoveSubview(&h_scroller_);
}
}
if (use_v != v_visible_) {
if (use_v) {
AddSubview(&v_scroller_);
} else {
RemoveSubview(&v_scroller_);
}
}
h_scroller_.SetShowSize(size_.width_ -
(use_v ? ScrollBarView::kThickness : 0.0));
v_scroller_.SetShowSize(size_.height_ -
(use_h ? ScrollBarView::kThickness : 0.0));
Rect h_scroller_frame =
Rect(0.0, size_.height_ - ScrollBarView::kThickness,
size_.width_ - (use_v ? ScrollBarView::kThickness : 0.0),
ScrollBarView::kThickness);
h_scroller_.SetFrame(h_scroller_frame);
Rect v_scroller_frame =
Rect(size_.width_ - ScrollBarView::kThickness, 0.0,
ScrollBarView::kThickness,
size_.height_ - (use_h ? ScrollBarView::kThickness : 0.0));
v_scroller_.SetFrame(v_scroller_frame);
h_visible_ = use_h;
v_visible_ = use_v;
Rect clip_frame =
Rect(0.0,
0.0,
size_.width_ - (use_v ? ScrollBarView::kThickness : 0.0),
size_.height_ - (use_h ? ScrollBarView::kThickness : 0.0));
clip_view_.SetFrame(clip_frame);
if (!h_visible_) {
// Center doc
Point doc_origin = document_->origin();
doc_origin.x_ = (clip_view_.size().width_ -
document_->size().width_) / 2.0;
document_->SetOrigin(doc_origin);
}
if (!v_visible_) {
// Center doc
Point doc_origin = document_->origin();
doc_origin.y_ = (clip_view_.size().height_ -
document_->size().height_) / 2.0;
document_->SetOrigin(doc_origin);
}
SetNeedsDisplay();
}
void ScrollView::MoveDocPointToVisibleCenter(const Point& center) {
if (h_visible_) {
h_scroller_.CenterDocValue(center.x_);
}
if (v_visible_) {
v_scroller_.CenterDocValue(center.y_);
}
}
void ScrollView::Resize(const Size& size) {
SetSize(size);
RepositionSubviews();
}
void ScrollView::ScrollBarMovedTo(ScrollBarView* scroll_bar, double show_min) {
if (!document_)
return;
show_min = static_cast<int>(show_min);
Rect doc_frame = document_->Frame();
if (scroll_bar == &h_scroller_) {
doc_frame.origin_.x_ = -show_min;
} else {
doc_frame.origin_.y_ = -show_min;
}
document_->SetFrame(doc_frame);
}
void ScrollView::ViewFrameChanged(View* view, const Rect& frame,
const Rect& old_frame) {
// Make a copy of the Rects, as we are modifying the view that
// calls into this.
Rect frame_copy = frame;
Rect old_frame_copy = old_frame;
if (view && view == document_) {
if (handling_doc_frame_changed_)
return;
handling_doc_frame_changed_ = true;
RepositionSubviews();
if (old_frame_copy.size_ != frame_copy.size_ &&
doc_size_.width_ && doc_size_.height_) {
// Zoom event
double x_ratio = doc_visible_center_.x_ / doc_size_.width_;
double y_ratio = doc_visible_center_.y_ / doc_size_.height_;
// Try to keep the visible center of the doc in the same place
Point new_center(x_ratio * document_->size().width_,
y_ratio * document_->size().height_);
MoveDocPointToVisibleCenter(new_center);
}
doc_visible_center_ = document_->VisibleSubrect().Center();
doc_size_ = document_->size();
handling_doc_frame_changed_ = false;
}
}
void ScrollView::OnScrollEvent(const ScrollInputEvent& event) {
if (h_visible_ && event.dx())
h_scroller_.ScrollBy(-event.dx());
if (v_visible_ && event.dy())
v_scroller_.ScrollBy(-event.dy());
}
} // namespace pdfsketch