-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathview.cc
More file actions
301 lines (272 loc) · 8.59 KB
/
view.cc
File metadata and controls
301 lines (272 loc) · 8.59 KB
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
// Copyright
#include "view.h"
#include <algorithm>
#include <stdio.h>
using std::max;
using std::min;
using std::string;
namespace pdfsketch {
Rect Rect::Intersect(const Rect& that) const {
Rect ret = Rect(Point(max(Left(), that.Left()),
max(Top(), that.Top())),
Point(min(Right(), that.Right()),
min(Bottom(), that.Bottom())));
ret.size_.width_ = max(ret.size_.width_, 0.0);
ret.size_.height_ = max(ret.size_.height_, 0.0);
return ret;
}
bool Rect::Contains(const Point& point) const {
return origin_.x_ <= point.x_ && point.x_ < (origin_.x_ + size_.width_) &&
origin_.y_ <= point.y_ && point.y_ < (origin_.y_ + size_.height_);
}
// Algorithm from https://github.com/adlr/formulatepro/blob/master/FPGraphic.m
bool Rect::SetBottomAbs(double bottom) {
bool flip = bottom < origin_.y_;
if (flip) {
size_.height_ = origin_.y_ - bottom;
origin_.y_ = bottom;
} else {
size_.height_ = bottom - origin_.y_;
}
return flip;
}
bool Rect::SetRightAbs(double right) {
bool flip = right < origin_.x_;
if (flip) {
size_.width_ = origin_.x_ - right;
origin_.x_ = right;
} else {
size_.width_ = right - origin_.x_;
}
return flip;
}
bool Rect::SetTopAbs(double top) {
bool flip = top > (origin_.y_ + size_.height_);
if (flip) {
origin_.y_ += size_.height_;
size_.height_ = top - origin_.y_;
} else {
size_.height_ += (origin_.y_ - top);
origin_.y_ = top;
}
return flip;
}
bool Rect::SetLeftAbs(double left) {
bool flip = left > (origin_.x_ + size_.width_);
if (flip) {
origin_.x_ += size_.width_;
size_.width_ = left - origin_.x_;
} else {
size_.width_ += (origin_.x_ - left);
origin_.x_ = left;
}
return flip;
}
void MouseInputEvent::UpdateToSubview(View* subview,
View* from_superview) {
position_ =
from_superview->ConvertPointToSubview(*subview, position_);
}
void MouseInputEvent::UpdateFromSubview(View* subview) {
position_ = subview->Superview()->ConvertPointFromSubview(*subview, position_);
}
void ScrollInputEvent::UpdateToSubview(View* subview, View* from_superview) {
Size size(dx(), dy());
size = from_superview->ConvertSizeToSubview(*subview, size);
dx_ = size.width_;
dy_ = size.height_;
}
void View::AddSubview(View* subview) {
if (subview->parent_) {
printf("%s: Subview has parent already!\n", __func__);
return;
}
subview->parent_ = this;
subview->upper_sibling_ = NULL;
subview->lower_sibling_ = top_child_;
if (top_child_)
top_child_->upper_sibling_ = subview;
top_child_ = subview;
if (!bottom_child_)
bottom_child_ = top_child_;
}
void View::RemoveSubview(View* subview) {
if (this != subview->parent_) {
printf("Removing view from non-parent!\n");
return;
}
View* prev_upper = subview->upper_sibling_;
View* prev_lower = subview->lower_sibling_;
subview->parent_ = NULL;
if (prev_upper)
prev_upper->lower_sibling_ = prev_lower;
if (prev_lower)
prev_lower->upper_sibling_ = prev_upper;
if (top_child_ == subview)
top_child_ = prev_lower;
if (bottom_child_ == subview)
bottom_child_ = prev_upper;
subview->upper_sibling_ = subview->lower_sibling_ = NULL;
}
void View::SetNeedsDisplayInRect(const Rect& rect) {
if (!parent_) {
printf("%s: Missing parent!\n", __func__);
return;
}
parent_->SetNeedsDisplayInRect(parent_->ConvertRectFromSubview(*this, rect));
}
void View::DrawRect(cairo_t* ctx, const Rect& rect) {
// Draw each child
for (View* child = bottom_child_; child; child = child->upper_sibling_) {
Rect intersect_parent = child->Frame().Intersect(rect);
if (!intersect_parent.size_.width_ || !intersect_parent.size_.height_) {
continue; // No intersection
}
cairo_save(ctx);
intersect_parent.CairoRectangle(ctx);
cairo_clip(ctx);
cairo_translate(ctx, child->origin_.x_, child->origin_.y_);
cairo_scale(ctx, child->scale_, child->scale_);
Rect intersect_child =
intersect_parent.TranslatedBy(-child->origin_.x_,
-child->origin_.y_).
ScaledBy(1 / child->scale_);
child->DrawRect(ctx, intersect_child);
cairo_restore(ctx);
}
}
Rect View::VisibleSubrect() const {
if (!parent_)
return Rect(size_);
// TODO(adlr): fill this in
Rect parent_visible = parent_->ConvertRectToSubview(*this,
parent_->VisibleSubrect());
return parent_visible.Intersect(Rect(size_));
}
void View::Resize(const Size& size) {
double x_delta = size.width_ - size_.width_;
double y_delta = size.height_ - size_.height_;
SetSize(size);
// Update subview frames
for (View* child = top_child_; child; child = child->lower_sibling_) {
Rect frame = child->Frame();
if (!child->top_fixed_to_top_) {
// Set top of rect w/o changing bottom
frame.origin_.y_ += y_delta;
frame.size_.height_ -= y_delta;
}
if (!child->bot_fixed_to_top_) {
frame.size_.height_ += y_delta;
}
if (!child->left_fixed_to_left_) {
// Set left of rect w/o changing right
frame.origin_.x_ += x_delta;
frame.size_.width_ -= x_delta;
}
if (!child->right_fixed_to_left_) {
frame.size_.width_ += x_delta;
}
child->SetFrame(frame);
}
}
void View::SetFrame(const Rect& frame) {
Rect old_frame = Frame();
origin_ = frame.origin_;
Size new_size = frame.size_.ScaledBy(1 / scale_);
if (new_size != size_)
Resize(new_size);
if (delegate_)
delegate_->ViewFrameChanged(this, frame, old_frame);
}
void View::SetSize(const Size& size) {
Rect old_frame = Frame();
size_ = size;
if (delegate_) {
delegate_->ViewFrameChanged(this, Frame(), old_frame);
}
}
void View::SetScale(double scale) {
Rect old_frame = Frame();
scale_ = scale;
if (delegate_)
delegate_->ViewFrameChanged(this, Frame(), old_frame);
}
View* View::OnMouseDown(const MouseInputEvent& event) {
// send to subviews
for (View* child = top_child_; child; child = child->lower_sibling_) {
View* ret = NULL;
if (child->Frame().Contains(event.position())) {
MouseInputEvent child_evt(event);
child_evt.UpdateToSubview(child, this);
ret = child->OnMouseDown(child_evt);
}
if (ret)
return ret;
}
return NULL;
}
bool View::OnKeyText(const KeyboardInputEvent& event) {
for (View* child = top_child_; child; child = child->lower_sibling_)
if (child->OnKeyText(event))
return true;
return false;
}
bool View::OnKeyDown(const KeyboardInputEvent& event) {
for (View* child = top_child_; child; child = child->lower_sibling_)
if (child->OnKeyDown(event))
return true;
return false;
}
bool View::OnKeyUp(const KeyboardInputEvent& event) {
for (View* child = top_child_; child; child = child->lower_sibling_)
if (child->OnKeyUp(event))
return true;
return false;
}
void View::OnScrollEvent(const ScrollInputEvent& event) {
// TODO(adlr): Only pass where mouse pointer is
for (View* child = top_child_; child; child = child->lower_sibling_)
child->OnScrollEvent(event);
}
string View::OnCopy() {
string ret;
for (View* child = top_child_; child; child = child->lower_sibling_) {
ret = child->OnCopy();
if (!ret.empty())
break;
}
return ret;
}
bool View::OnPaste(const string& str) {
for (View* child = top_child_; child; child = child->lower_sibling_)
if (child->OnPaste(str))
return true;
return false;
}
Point View::ConvertPointFromSubview(const View& subview, const Point& point) const {
return Point(point.x_ * subview.scale_, point.y_ * subview.scale_).
TranslatedBy(subview.origin_.x_, subview.origin_.y_);
}
Size View::ConvertSizeFromSubview(const View& subview, const Size& size) const {
return size.ScaledBy(subview.scale_);
}
Point View::ConvertPointToSubview(const View& subview, Point point) const {
if (subview.Superview() != this) {
if (!subview.Superview()) {
printf("Missing superview\n");
return Point();
}
point = ConvertPointToSubview(*subview.Superview(), point);
return subview.Superview()->ConvertPointToSubview(subview, point);
}
Point temp = point.TranslatedBy(-subview.origin_.x_, -subview.origin_.y_);
return Point(temp.x_ / subview.scale_, temp.y_ / subview.scale_);
}
Rect View::ConvertRectToSubview(const View& subview, const Rect& rect) const {
return Rect(ConvertPointToSubview(subview, rect.UpperLeft()),
ConvertPointToSubview(subview, rect.LowerRight()));
}
Size View::ConvertSizeToSubview(const View& subview, const Size& size) const {
return ConvertRectToSubview(subview, Rect(size)).size_;
}
} // namespace pdfsketch