blob: 0c6c0deb2cd8f0b98e995a88c48717e867758d8f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkView.h"
9#include "SkCanvas.h"
10
robertphillips@google.coma22e2112012-08-16 14:58:06 +000011SK_DEFINE_INST_COUNT(SkView::Artist)
12SK_DEFINE_INST_COUNT(SkView::Layout)
13
reed@android.com8a1c16f2008-12-17 15:59:43 +000014////////////////////////////////////////////////////////////////////////
15
16SkView::SkView(uint32_t flags) : fFlags(SkToU8(flags))
17{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000018 fWidth = fHeight = 0;
19 fLoc.set(0, 0);
20 fParent = fFirstChild = fNextSibling = fPrevSibling = NULL;
reed@google.comf03bb562011-11-11 21:42:12 +000021 fMatrix.setIdentity();
robertphillips@google.coma22e2112012-08-16 14:58:06 +000022 fContainsFocus = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023}
24
25SkView::~SkView()
26{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000027 this->detachAllChildren();
reed@android.com8a1c16f2008-12-17 15:59:43 +000028}
29
30void SkView::setFlags(uint32_t flags)
31{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000032 SkASSERT((flags & ~kAllFlagMasks) == 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
robertphillips@google.coma22e2112012-08-16 14:58:06 +000034 uint32_t diff = fFlags ^ flags;
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
robertphillips@google.coma22e2112012-08-16 14:58:06 +000036 if (diff & kVisible_Mask)
37 this->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
robertphillips@google.coma22e2112012-08-16 14:58:06 +000039 fFlags = SkToU8(flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
robertphillips@google.coma22e2112012-08-16 14:58:06 +000041 if (diff & kVisible_Mask)
42 {
43 this->inval(NULL);
44 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000045}
46
47void SkView::setVisibleP(bool pred)
48{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000049 this->setFlags(SkSetClearShift(fFlags, pred, kVisible_Shift));
reed@android.com8a1c16f2008-12-17 15:59:43 +000050}
51
52void SkView::setEnabledP(bool pred)
53{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000054 this->setFlags(SkSetClearShift(fFlags, pred, kEnabled_Shift));
reed@android.com8a1c16f2008-12-17 15:59:43 +000055}
56
57void SkView::setFocusableP(bool pred)
58{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000059 this->setFlags(SkSetClearShift(fFlags, pred, kFocusable_Shift));
reed@android.com8a1c16f2008-12-17 15:59:43 +000060}
61
reed@android.comf2b98d62010-12-20 18:26:13 +000062void SkView::setClipToBounds(bool pred) {
63 this->setFlags(SkSetClearShift(fFlags, !pred, kNoClip_Shift));
64}
65
reed@android.com8a1c16f2008-12-17 15:59:43 +000066void SkView::setSize(SkScalar width, SkScalar height)
67{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000068 width = SkMaxScalar(0, width);
69 height = SkMaxScalar(0, height);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
robertphillips@google.coma22e2112012-08-16 14:58:06 +000071 if (fWidth != width || fHeight != height)
72 {
73 this->inval(NULL);
74 fWidth = width;
75 fHeight = height;
76 this->inval(NULL);
77 this->onSizeChange();
78 this->invokeLayout();
79 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000080}
81
82void SkView::setLoc(SkScalar x, SkScalar y)
83{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000084 if (fLoc.fX != x || fLoc.fY != y)
85 {
reed@google.comf03bb562011-11-11 21:42:12 +000086 this->inval(NULL);
robertphillips@google.coma22e2112012-08-16 14:58:06 +000087 fLoc.set(x, y);
88 this->inval(NULL);
89 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000090}
91
92void SkView::offset(SkScalar dx, SkScalar dy)
93{
robertphillips@google.coma22e2112012-08-16 14:58:06 +000094 if (dx || dy)
95 this->setLoc(fLoc.fX + dx, fLoc.fY + dy);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096}
97
rmistry@google.comd6176b02012-08-23 18:14:13 +000098void SkView::setLocalMatrix(const SkMatrix& matrix)
reed@google.comf03bb562011-11-11 21:42:12 +000099{
100 this->inval(NULL);
101 fMatrix = matrix;
102 this->inval(NULL);
103}
104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105void SkView::draw(SkCanvas* canvas)
106{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000107 if (fWidth && fHeight && this->isVisible())
108 {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109 SkRect r;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000110 r.set(fLoc.fX, fLoc.fY, fLoc.fX + fWidth, fLoc.fY + fHeight);
111 if (this->isClipToBounds() &&
reed@google.com3b3e8952012-08-16 20:53:31 +0000112 canvas->quickReject(r)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000113 return;
114 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116 SkAutoCanvasRestore as(canvas, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117
reed@android.comf2b98d62010-12-20 18:26:13 +0000118 if (this->isClipToBounds()) {
119 canvas->clipRect(r);
120 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121
122 canvas->translate(fLoc.fX, fLoc.fY);
reed@google.comf03bb562011-11-11 21:42:12 +0000123 canvas->concat(fMatrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124
reed@android.com6c5f6f22009-08-14 16:08:38 +0000125 if (fParent) {
126 fParent->beforeChild(this, canvas);
127 }
reed@android.com562ea922010-02-08 21:45:03 +0000128
129 int sc = canvas->save();
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000130 this->onDraw(canvas);
reed@android.com562ea922010-02-08 21:45:03 +0000131 canvas->restoreToCount(sc);
132
reed@android.com6c5f6f22009-08-14 16:08:38 +0000133 if (fParent) {
134 fParent->afterChild(this, canvas);
135 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000136
137 B2FIter iter(this);
138 SkView* child;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139
140 SkCanvas* childCanvas = this->beforeChildren(canvas);
141
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000142 while ((child = iter.next()) != NULL)
143 child->draw(childCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 this->afterChildren(canvas);
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000146 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147}
148
reed@android.comf2b98d62010-12-20 18:26:13 +0000149void SkView::inval(SkRect* rect) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150 SkView* view = this;
reed@android.comf2b98d62010-12-20 18:26:13 +0000151 SkRect storage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000153 for (;;) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000154 if (!view->isVisible()) {
155 return;
156 }
157 if (view->isClipToBounds()) {
158 SkRect bounds;
159 view->getLocalBounds(&bounds);
160 if (rect && !bounds.intersect(*rect)) {
161 return;
162 }
163 storage = bounds;
164 rect = &storage;
165 }
166 if (view->handleInval(rect)) {
167 return;
168 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000170 SkView* parent = view->fParent;
reed@android.comf2b98d62010-12-20 18:26:13 +0000171 if (parent == NULL) {
172 return;
173 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174
reed@android.comf2b98d62010-12-20 18:26:13 +0000175 if (rect) {
176 rect->offset(view->fLoc.fX, view->fLoc.fY);
177 }
178 view = parent;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000179 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180}
181
182////////////////////////////////////////////////////////////////////////////
183
184bool SkView::setFocusView(SkView* fv)
185{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000186 SkView* view = this;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000188 do {
189 if (view->onSetFocusView(fv))
190 return true;
191 } while ((view = view->fParent) != NULL);
192 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193}
194
195SkView* SkView::getFocusView() const
196{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000197 SkView* focus = NULL;
198 const SkView* view = this;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000199 do {
200 if (view->onGetFocusView(&focus))
201 break;
202 } while ((view = view->fParent) != NULL);
203 return focus;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204}
205
206bool SkView::hasFocus() const
207{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000208 return this == this->getFocusView();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209}
210
211bool SkView::acceptFocus()
212{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000213 return this->isFocusable() && this->setFocusView(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214}
215
216/*
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000217 Try to give focus to this view, or its children
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218*/
219SkView* SkView::acceptFocus(FocusDirection dir)
220{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000221 if (dir == kNext_FocusDirection)
222 {
223 if (this->acceptFocus())
224 return this;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225
rmistry@google.comd6176b02012-08-23 18:14:13 +0000226 B2FIter iter(this);
227 SkView* child, *focus;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000228 while ((child = iter.next()) != NULL)
229 if ((focus = child->acceptFocus(dir)) != NULL)
230 return focus;
231 }
232 else // prev
233 {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000234 F2BIter iter(this);
235 SkView* child, *focus;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000236 while ((child = iter.next()) != NULL)
237 if ((focus = child->acceptFocus(dir)) != NULL)
238 return focus;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000240 if (this->acceptFocus())
241 return this;
242 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000244 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245}
246
247SkView* SkView::moveFocus(FocusDirection dir)
248{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000249 SkView* focus = this->getFocusView();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000251 if (focus == NULL)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000252 { // start with the root
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000253 focus = this;
254 while (focus->fParent)
255 focus = focus->fParent;
256 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258 SkView* child, *parent;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000260 if (dir == kNext_FocusDirection)
261 {
262 parent = focus;
263 child = focus->fFirstChild;
264 if (child)
265 goto FIRST_CHILD;
266 else
267 goto NEXT_SIB;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000269 do {
270 while (child != parent->fFirstChild)
271 {
272 FIRST_CHILD:
273 if ((focus = child->acceptFocus(dir)) != NULL)
274 return focus;
275 child = child->fNextSibling;
276 }
277 NEXT_SIB:
278 child = parent->fNextSibling;
279 parent = parent->fParent;
280 } while (parent != NULL);
281 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000282 else // prevfocus
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000283 {
284 parent = focus->fParent;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000285 if (parent == NULL) // we're the root
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000286 return focus->acceptFocus(dir);
287 else
288 {
289 child = focus;
290 while (parent)
291 {
292 while (child != parent->fFirstChild)
293 {
294 child = child->fPrevSibling;
295 if ((focus = child->acceptFocus(dir)) != NULL)
296 return focus;
297 }
298 if (parent->acceptFocus())
299 return parent;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000301 child = parent;
302 parent = parent->fParent;
303 }
304 }
305 }
306 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307}
308
309void SkView::onFocusChange(bool gainFocusP)
310{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000311 this->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312}
313
314////////////////////////////////////////////////////////////////////////////
315
316SkView::Click::Click(SkView* target)
317{
Scroggod3aed392011-06-22 13:26:56 +0000318 SkASSERT(target);
319 fTargetID = target->getSinkID();
320 fType = NULL;
321 fWeOwnTheType = false;
322 fOwner = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323}
324
325SkView::Click::~Click()
326{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000327 this->resetType();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328}
329
330void SkView::Click::resetType()
331{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000332 if (fWeOwnTheType)
333 {
334 sk_free(fType);
335 fWeOwnTheType = false;
336 }
337 fType = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338}
339
340bool SkView::Click::isType(const char type[]) const
341{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000342 const char* t = fType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000344 if (type == t)
345 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000347 if (type == NULL)
348 type = "";
349 if (t == NULL)
350 t = "";
351 return !strcmp(t, type);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352}
353
354void SkView::Click::setType(const char type[])
355{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000356 this->resetType();
357 fType = (char*)type;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358}
359
360void SkView::Click::copyType(const char type[])
361{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000362 if (fType != type)
363 {
364 this->resetType();
365 if (type)
366 {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000367 size_t len = strlen(type) + 1;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000368 fType = (char*)sk_malloc_throw(len);
369 memcpy(fType, type, len);
370 fWeOwnTheType = true;
371 }
372 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373}
374
reed@google.com4d5c26d2013-01-08 16:17:50 +0000375SkView::Click* SkView::findClickHandler(SkScalar x, SkScalar y, unsigned modi) {
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000376 if (x < 0 || y < 0 || x >= fWidth || y >= fHeight) {
377 return NULL;
reed@android.come72fee52009-11-16 14:52:01 +0000378 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379
reed@google.com4d5c26d2013-01-08 16:17:50 +0000380 if (this->onSendClickToChildren(x, y, modi)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000381 F2BIter iter(this);
382 SkView* child;
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000383
reed@android.come72fee52009-11-16 14:52:01 +0000384 while ((child = iter.next()) != NULL)
385 {
reed@google.comf03bb562011-11-11 21:42:12 +0000386 SkPoint p;
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000387 if (!child->globalToLocal(x, y, &p)) {
388 continue;
389 }
390
reed@google.com4d5c26d2013-01-08 16:17:50 +0000391 Click* click = child->findClickHandler(p.fX, p.fY, modi);
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000392
reed@android.come72fee52009-11-16 14:52:01 +0000393 if (click) {
394 return click;
395 }
396 }
397 }
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000398
reed@google.com4d5c26d2013-01-08 16:17:50 +0000399 return this->onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400}
401
reed@google.com4d5c26d2013-01-08 16:17:50 +0000402void SkView::DoClickDown(Click* click, int x, int y, unsigned modi)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403{
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000404 SkASSERT(click);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000406 SkView* target = (SkView*)SkEventSink::FindSink(click->fTargetID);
407 if (NULL == target) {
408 return;
409 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000411 click->fIOrig.set(x, y);
412 click->fICurr = click->fIPrev = click->fIOrig;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000414 click->fOrig.iset(x, y);
415 if (!target->globalToLocal(&click->fOrig)) {
416 // no history to let us recover from this failure
417 return;
418 }
419 click->fPrev = click->fCurr = click->fOrig;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000421 click->fState = Click::kDown_State;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000422 click->fModifierKeys = modi;
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000423 target->onClick(click);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424}
425
reed@google.com4d5c26d2013-01-08 16:17:50 +0000426void SkView::DoClickMoved(Click* click, int x, int y, unsigned modi)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427{
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000428 SkASSERT(click);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000430 SkView* target = (SkView*)SkEventSink::FindSink(click->fTargetID);
431 if (NULL == target) {
432 return;
433 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000435 click->fIPrev = click->fICurr;
436 click->fICurr.set(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000438 click->fPrev = click->fCurr;
439 click->fCurr.iset(x, y);
440 if (!target->globalToLocal(&click->fCurr)) {
441 // on failure pretend the mouse didn't move
442 click->fCurr = click->fPrev;
443 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000445 click->fState = Click::kMoved_State;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000446 click->fModifierKeys = modi;
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000447 target->onClick(click);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448}
449
reed@google.com4d5c26d2013-01-08 16:17:50 +0000450void SkView::DoClickUp(Click* click, int x, int y, unsigned modi)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451{
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000452 SkASSERT(click);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000454 SkView* target = (SkView*)SkEventSink::FindSink(click->fTargetID);
455 if (NULL == target) {
456 return;
457 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000459 click->fIPrev = click->fICurr;
460 click->fICurr.set(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000462 click->fPrev = click->fCurr;
463 click->fCurr.iset(x, y);
464 if (!target->globalToLocal(&click->fCurr)) {
465 // on failure pretend the mouse didn't move
466 click->fCurr = click->fPrev;
467 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000469 click->fState = Click::kUp_State;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000470 click->fModifierKeys = modi;
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000471 target->onClick(click);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000472}
473
474//////////////////////////////////////////////////////////////////////
475
reed@android.come72fee52009-11-16 14:52:01 +0000476void SkView::invokeLayout() {
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000477 SkView::Layout* layout = this->getLayout();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000479 if (layout) {
480 layout->layoutChildren(this);
reed@android.come72fee52009-11-16 14:52:01 +0000481 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482}
483
reed@android.come72fee52009-11-16 14:52:01 +0000484void SkView::onDraw(SkCanvas* canvas) {
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000485 Artist* artist = this->getArtist();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000487 if (artist) {
488 artist->draw(this, canvas);
reed@android.come72fee52009-11-16 14:52:01 +0000489 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000490}
491
reed@android.come72fee52009-11-16 14:52:01 +0000492void SkView::onSizeChange() {}
493
reed@google.com4d5c26d2013-01-08 16:17:50 +0000494bool SkView::onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) {
reed@android.come72fee52009-11-16 14:52:01 +0000495 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496}
497
reed@google.com4d5c26d2013-01-08 16:17:50 +0000498SkView::Click* SkView::onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) {
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000499 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000500}
501
reed@android.come72fee52009-11-16 14:52:01 +0000502bool SkView::onClick(Click*) {
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000503 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504}
505
reed@android.comf2b98d62010-12-20 18:26:13 +0000506bool SkView::handleInval(const SkRect*) {
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000507 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508}
509
510//////////////////////////////////////////////////////////////////////
511
512void SkView::getLocalBounds(SkRect* bounds) const
513{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000514 if (bounds)
515 bounds->set(0, 0, fWidth, fHeight);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000516}
517
518//////////////////////////////////////////////////////////////////////
519//////////////////////////////////////////////////////////////////////
520
521void SkView::detachFromParent_NoLayout()
522{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000523 if (fParent == NULL)
524 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000525
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000526 if (fContainsFocus)
527 (void)this->setFocusView(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000529 this->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000530
rmistry@google.comd6176b02012-08-23 18:14:13 +0000531 SkView* next = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532
rmistry@google.comd6176b02012-08-23 18:14:13 +0000533 if (fNextSibling != this) // do we have any siblings
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000534 {
535 fNextSibling->fPrevSibling = fPrevSibling;
536 fPrevSibling->fNextSibling = fNextSibling;
537 next = fNextSibling;
538 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000540 if (fParent->fFirstChild == this)
541 fParent->fFirstChild = next;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000543 fParent = fNextSibling = fPrevSibling = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000545 this->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000546}
547
548void SkView::detachFromParent()
549{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000550 SkView* parent = fParent;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000552 if (parent)
553 {
554 this->detachFromParent_NoLayout();
555 parent->invokeLayout();
556 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557}
558
559SkView* SkView::attachChildToBack(SkView* child)
560{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000561 SkASSERT(child != this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000563 if (child == NULL || fFirstChild == child)
564 goto DONE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000566 child->ref();
567 child->detachFromParent_NoLayout();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000569 if (fFirstChild == NULL)
570 {
571 child->fNextSibling = child;
572 child->fPrevSibling = child;
573 }
574 else
575 {
576 child->fNextSibling = fFirstChild;
577 child->fPrevSibling = fFirstChild->fPrevSibling;
578 fFirstChild->fPrevSibling->fNextSibling = child;
579 fFirstChild->fPrevSibling = child;
580 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000581
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000582 fFirstChild = child;
583 child->fParent = this;
584 child->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000585
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000586 this->invokeLayout();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000587DONE:
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000588 return child;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000589}
590
591SkView* SkView::attachChildToFront(SkView* child)
592{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000593 SkASSERT(child != this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000594
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000595 if (child == NULL || (fFirstChild && fFirstChild->fPrevSibling == child))
596 goto DONE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000597
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000598 child->ref();
599 child->detachFromParent_NoLayout();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000600
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000601 if (fFirstChild == NULL)
602 {
603 fFirstChild = child;
604 child->fNextSibling = child;
605 child->fPrevSibling = child;
606 }
607 else
608 {
609 child->fNextSibling = fFirstChild;
610 child->fPrevSibling = fFirstChild->fPrevSibling;
611 fFirstChild->fPrevSibling->fNextSibling = child;
612 fFirstChild->fPrevSibling = child;
613 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000614
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000615 child->fParent = this;
616 child->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000617
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000618 this->invokeLayout();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000619DONE:
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000620 return child;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000621}
622
623void SkView::detachAllChildren()
624{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000625 while (fFirstChild)
626 fFirstChild->detachFromParent_NoLayout();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627}
628
reed@google.comf03bb562011-11-11 21:42:12 +0000629void SkView::localToGlobal(SkMatrix* matrix) const
630{
631 if (matrix) {
632 matrix->reset();
633 const SkView* view = this;
634 while (view)
635 {
636 matrix->preConcat(view->getLocalMatrix());
637 matrix->preTranslate(-view->fLoc.fX, -view->fLoc.fY);
638 view = view->fParent;
639 }
640 }
641}
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000642bool SkView::globalToLocal(SkScalar x, SkScalar y, SkPoint* local) const
reed@android.com8a1c16f2008-12-17 15:59:43 +0000643{
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000644 SkASSERT(this);
645
646 if (NULL != local) {
reed@google.comf03bb562011-11-11 21:42:12 +0000647 SkMatrix m;
648 this->localToGlobal(&m);
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000649 if (!m.invert(&m)) {
650 return false;
651 }
reed@google.comf03bb562011-11-11 21:42:12 +0000652 SkPoint p;
reed@google.comf03bb562011-11-11 21:42:12 +0000653 m.mapXY(x, y, &p);
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000654 local->set(p.fX, p.fY);
655 }
656
657 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658}
659
660//////////////////////////////////////////////////////////////////
661
rmistry@google.comd6176b02012-08-23 18:14:13 +0000662/* Even if the subclass overrides onInflate, they should always be
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000663 sure to call the inherited method, so that we get called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000664*/
665void SkView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
666{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000667 SkScalar x, y;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000668
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000669 x = this->locX();
670 y = this->locY();
671 (void)dom.findScalar(node, "x", &x);
672 (void)dom.findScalar(node, "y", &y);
673 this->setLoc(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000675 x = this->width();
676 y = this->height();
677 (void)dom.findScalar(node, "width", &x);
678 (void)dom.findScalar(node, "height", &y);
679 this->setSize(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000681 // inflate the flags
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000683 static const char* gFlagNames[] = {
684 "visible", "enabled", "focusable", "flexH", "flexV"
685 };
686 SkASSERT(SK_ARRAY_COUNT(gFlagNames) == kFlagShiftCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000688 bool b;
689 uint32_t flags = this->getFlags();
690 for (unsigned i = 0; i < SK_ARRAY_COUNT(gFlagNames); i++)
691 if (dom.findBool(node, gFlagNames[i], &b))
692 flags = SkSetClearShift(flags, b, i);
693 this->setFlags(flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000694}
695
696void SkView::inflate(const SkDOM& dom, const SkDOM::Node* node)
697{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000698 this->onInflate(dom, node);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000699}
700
701void SkView::onPostInflate(const SkTDict<SkView*>&)
702{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000703 // override in subclass as needed
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704}
705
706void SkView::postInflate(const SkTDict<SkView*>& dict)
707{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000708 this->onPostInflate(dict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000709
rmistry@google.comd6176b02012-08-23 18:14:13 +0000710 B2FIter iter(this);
711 SkView* child;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000712 while ((child = iter.next()) != NULL)
713 child->postInflate(dict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000714}
715
716//////////////////////////////////////////////////////////////////
717
718SkView* SkView::sendEventToParents(const SkEvent& evt)
719{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000720 SkView* parent = fParent;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000721
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000722 while (parent)
723 {
724 if (parent->doEvent(evt))
725 return parent;
726 parent = parent->fParent;
727 }
728 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000729}
730
reed@android.com34245c72009-11-03 04:00:48 +0000731SkView* SkView::sendQueryToParents(SkEvent* evt) {
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000732 SkView* parent = fParent;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000733
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000734 while (parent) {
735 if (parent->doQuery(evt)) {
736 return parent;
reed@android.com34245c72009-11-03 04:00:48 +0000737 }
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000738 parent = parent->fParent;
739 }
740 return NULL;
reed@android.com34245c72009-11-03 04:00:48 +0000741}
742
reed@android.com8a1c16f2008-12-17 15:59:43 +0000743//////////////////////////////////////////////////////////////////
744//////////////////////////////////////////////////////////////////
745
746SkView::F2BIter::F2BIter(const SkView* parent)
747{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000748 fFirstChild = parent ? parent->fFirstChild : NULL;
749 fChild = fFirstChild ? fFirstChild->fPrevSibling : NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750}
751
rmistry@google.comd6176b02012-08-23 18:14:13 +0000752SkView* SkView::F2BIter::next()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000753{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000754 SkView* curr = fChild;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000755
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000756 if (fChild)
757 {
758 if (fChild == fFirstChild)
759 fChild = NULL;
760 else
761 fChild = fChild->fPrevSibling;
762 }
763 return curr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764}
765
766SkView::B2FIter::B2FIter(const SkView* parent)
767{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000768 fFirstChild = parent ? parent->fFirstChild : NULL;
769 fChild = fFirstChild;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770}
771
rmistry@google.comd6176b02012-08-23 18:14:13 +0000772SkView* SkView::B2FIter::next()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000773{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000774 SkView* curr = fChild;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000775
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000776 if (fChild)
777 {
778 SkView* next = fChild->fNextSibling;
779 if (next == fFirstChild)
780 next = NULL;
781 fChild = next;
782 }
783 return curr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000784}
785
786//////////////////////////////////////////////////////////////////
787//////////////////////////////////////////////////////////////////
788
789#ifdef SK_DEBUG
790
791static inline void show_if_nonzero(const char name[], SkScalar value)
792{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000793 if (value)
794 SkDebugf("%s=\"%g\"", name, value/65536.);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000795}
796
797static void tab(int level)
798{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000799 for (int i = 0; i < level; i++)
800 SkDebugf(" ");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801}
802
803static void dumpview(const SkView* view, int level, bool recurse)
804{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000805 tab(level);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000806
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000807 SkDebugf("<view");
808 show_if_nonzero(" x", view->locX());
809 show_if_nonzero(" y", view->locY());
810 show_if_nonzero(" width", view->width());
811 show_if_nonzero(" height", view->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000812
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000813 if (recurse)
814 {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000815 SkView::B2FIter iter(view);
816 SkView* child;
817 bool noChildren = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000819 while ((child = iter.next()) != NULL)
820 {
821 if (noChildren)
822 SkDebugf(">\n");
823 noChildren = false;
824 dumpview(child, level + 1, true);
825 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000826
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000827 if (!noChildren)
828 {
829 tab(level);
830 SkDebugf("</view>\n");
831 }
832 else
833 goto ONELINER;
834 }
835 else
836 {
837 ONELINER:
838 SkDebugf(" />\n");
839 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000840}
841
842void SkView::dump(bool recurse) const
843{
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000844 dumpview(this, 0, recurse);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845}
846
847#endif