epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
| 10 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 11 | SK_DEFINE_INST_COUNT(SkView::Artist) |
| 12 | SK_DEFINE_INST_COUNT(SkView::Layout) |
| 13 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | //////////////////////////////////////////////////////////////////////// |
| 15 | |
| 16 | SkView::SkView(uint32_t flags) : fFlags(SkToU8(flags)) |
| 17 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 18 | fWidth = fHeight = 0; |
| 19 | fLoc.set(0, 0); |
| 20 | fParent = fFirstChild = fNextSibling = fPrevSibling = NULL; |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 21 | fMatrix.setIdentity(); |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 22 | fContainsFocus = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | SkView::~SkView() |
| 26 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 27 | this->detachAllChildren(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void SkView::setFlags(uint32_t flags) |
| 31 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 32 | SkASSERT((flags & ~kAllFlagMasks) == 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 34 | uint32_t diff = fFlags ^ flags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 36 | if (diff & kVisible_Mask) |
| 37 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 39 | fFlags = SkToU8(flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 41 | if (diff & kVisible_Mask) |
| 42 | { |
| 43 | this->inval(NULL); |
| 44 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void SkView::setVisibleP(bool pred) |
| 48 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 49 | this->setFlags(SkSetClearShift(fFlags, pred, kVisible_Shift)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void SkView::setEnabledP(bool pred) |
| 53 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 54 | this->setFlags(SkSetClearShift(fFlags, pred, kEnabled_Shift)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void SkView::setFocusableP(bool pred) |
| 58 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 59 | this->setFlags(SkSetClearShift(fFlags, pred, kFocusable_Shift)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | } |
| 61 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 62 | void SkView::setClipToBounds(bool pred) { |
| 63 | this->setFlags(SkSetClearShift(fFlags, !pred, kNoClip_Shift)); |
| 64 | } |
| 65 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | void SkView::setSize(SkScalar width, SkScalar height) |
| 67 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 68 | width = SkMaxScalar(0, width); |
| 69 | height = SkMaxScalar(0, height); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 71 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void SkView::setLoc(SkScalar x, SkScalar y) |
| 83 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 84 | if (fLoc.fX != x || fLoc.fY != y) |
| 85 | { |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 86 | this->inval(NULL); |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 87 | fLoc.set(x, y); |
| 88 | this->inval(NULL); |
| 89 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void SkView::offset(SkScalar dx, SkScalar dy) |
| 93 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 94 | if (dx || dy) |
| 95 | this->setLoc(fLoc.fX + dx, fLoc.fY + dy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 98 | void SkView::setLocalMatrix(const SkMatrix& matrix) |
| 99 | { |
| 100 | this->inval(NULL); |
| 101 | fMatrix = matrix; |
| 102 | this->inval(NULL); |
| 103 | } |
| 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | void SkView::draw(SkCanvas* canvas) |
| 106 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 107 | if (fWidth && fHeight && this->isVisible()) |
| 108 | { |
| 109 | SkRect r; |
| 110 | r.set(fLoc.fX, fLoc.fY, fLoc.fX + fWidth, fLoc.fY + fHeight); |
| 111 | if (this->isClipToBounds() && |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 112 | canvas->quickReject(r, SkCanvas::kBW_EdgeType)) { |
| 113 | return; |
| 114 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 116 | SkAutoCanvasRestore as(canvas, true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 118 | if (this->isClipToBounds()) { |
| 119 | canvas->clipRect(r); |
| 120 | } |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 121 | |
| 122 | canvas->translate(fLoc.fX, fLoc.fY); |
| 123 | canvas->concat(fMatrix); |
| 124 | |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 125 | if (fParent) { |
| 126 | fParent->beforeChild(this, canvas); |
| 127 | } |
reed@android.com | 562ea92 | 2010-02-08 21:45:03 +0000 | [diff] [blame] | 128 | |
| 129 | int sc = canvas->save(); |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 130 | this->onDraw(canvas); |
reed@android.com | 562ea92 | 2010-02-08 21:45:03 +0000 | [diff] [blame] | 131 | canvas->restoreToCount(sc); |
| 132 | |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 133 | if (fParent) { |
| 134 | fParent->afterChild(this, canvas); |
| 135 | } |
| 136 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 137 | B2FIter iter(this); |
| 138 | SkView* child; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | |
| 140 | SkCanvas* childCanvas = this->beforeChildren(canvas); |
| 141 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 142 | while ((child = iter.next()) != NULL) |
| 143 | child->draw(childCanvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | |
| 145 | this->afterChildren(canvas); |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 146 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | } |
| 148 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 149 | void SkView::inval(SkRect* rect) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 150 | SkView* view = this; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 151 | SkRect storage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 153 | for (;;) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 154 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 170 | SkView* parent = view->fParent; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 171 | if (parent == NULL) { |
| 172 | return; |
| 173 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 175 | if (rect) { |
| 176 | rect->offset(view->fLoc.fX, view->fLoc.fY); |
| 177 | } |
| 178 | view = parent; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 179 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | //////////////////////////////////////////////////////////////////////////// |
| 183 | |
| 184 | bool SkView::setFocusView(SkView* fv) |
| 185 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 186 | SkView* view = this; |
| 187 | |
| 188 | do { |
| 189 | if (view->onSetFocusView(fv)) |
| 190 | return true; |
| 191 | } while ((view = view->fParent) != NULL); |
| 192 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | SkView* SkView::getFocusView() const |
| 196 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 197 | SkView* focus = NULL; |
| 198 | const SkView* view = this; |
| 199 | do { |
| 200 | if (view->onGetFocusView(&focus)) |
| 201 | break; |
| 202 | } while ((view = view->fParent) != NULL); |
| 203 | return focus; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | bool SkView::hasFocus() const |
| 207 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 208 | return this == this->getFocusView(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | bool SkView::acceptFocus() |
| 212 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 213 | return this->isFocusable() && this->setFocusView(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 217 | Try to give focus to this view, or its children |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | */ |
| 219 | SkView* SkView::acceptFocus(FocusDirection dir) |
| 220 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 221 | if (dir == kNext_FocusDirection) |
| 222 | { |
| 223 | if (this->acceptFocus()) |
| 224 | return this; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 226 | B2FIter iter(this); |
| 227 | SkView* child, *focus; |
| 228 | while ((child = iter.next()) != NULL) |
| 229 | if ((focus = child->acceptFocus(dir)) != NULL) |
| 230 | return focus; |
| 231 | } |
| 232 | else // prev |
| 233 | { |
| 234 | F2BIter iter(this); |
| 235 | SkView* child, *focus; |
| 236 | while ((child = iter.next()) != NULL) |
| 237 | if ((focus = child->acceptFocus(dir)) != NULL) |
| 238 | return focus; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 240 | if (this->acceptFocus()) |
| 241 | return this; |
| 242 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 244 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | SkView* SkView::moveFocus(FocusDirection dir) |
| 248 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 249 | SkView* focus = this->getFocusView(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 251 | if (focus == NULL) |
| 252 | { // start with the root |
| 253 | focus = this; |
| 254 | while (focus->fParent) |
| 255 | focus = focus->fParent; |
| 256 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 258 | SkView* child, *parent; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 260 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 269 | 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 | } |
| 282 | else // prevfocus |
| 283 | { |
| 284 | parent = focus->fParent; |
| 285 | if (parent == NULL) // we're the root |
| 286 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 301 | child = parent; |
| 302 | parent = parent->fParent; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void SkView::onFocusChange(bool gainFocusP) |
| 310 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 311 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | //////////////////////////////////////////////////////////////////////////// |
| 315 | |
| 316 | SkView::Click::Click(SkView* target) |
| 317 | { |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 318 | SkASSERT(target); |
| 319 | fTargetID = target->getSinkID(); |
| 320 | fType = NULL; |
| 321 | fWeOwnTheType = false; |
| 322 | fOwner = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | SkView::Click::~Click() |
| 326 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 327 | this->resetType(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void SkView::Click::resetType() |
| 331 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 332 | if (fWeOwnTheType) |
| 333 | { |
| 334 | sk_free(fType); |
| 335 | fWeOwnTheType = false; |
| 336 | } |
| 337 | fType = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | bool SkView::Click::isType(const char type[]) const |
| 341 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 342 | const char* t = fType; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 344 | if (type == t) |
| 345 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 347 | if (type == NULL) |
| 348 | type = ""; |
| 349 | if (t == NULL) |
| 350 | t = ""; |
| 351 | return !strcmp(t, type); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void SkView::Click::setType(const char type[]) |
| 355 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 356 | this->resetType(); |
| 357 | fType = (char*)type; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void SkView::Click::copyType(const char type[]) |
| 361 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 362 | if (fType != type) |
| 363 | { |
| 364 | this->resetType(); |
| 365 | if (type) |
| 366 | { |
| 367 | size_t len = strlen(type) + 1; |
| 368 | fType = (char*)sk_malloc_throw(len); |
| 369 | memcpy(fType, type, len); |
| 370 | fWeOwnTheType = true; |
| 371 | } |
| 372 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | SkView::Click* SkView::findClickHandler(SkScalar x, SkScalar y) |
| 376 | { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 377 | if (x < 0 || y < 0 || x >= fWidth || y >= fHeight) { |
| 378 | return NULL; |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 379 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 380 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 381 | if (this->onSendClickToChildren(x, y)) { |
| 382 | F2BIter iter(this); |
| 383 | SkView* child; |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 384 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 385 | while ((child = iter.next()) != NULL) |
| 386 | { |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 387 | SkPoint p; |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 388 | if (!child->globalToLocal(x, y, &p)) { |
| 389 | continue; |
| 390 | } |
| 391 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 392 | Click* click = child->findClickHandler(p.fX, p.fY); |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 393 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 394 | if (click) { |
| 395 | return click; |
| 396 | } |
| 397 | } |
| 398 | } |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 399 | |
| 400 | return this->onFindClickHandler(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void SkView::DoClickDown(Click* click, int x, int y) |
| 404 | { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 405 | SkASSERT(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 406 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 407 | SkView* target = (SkView*)SkEventSink::FindSink(click->fTargetID); |
| 408 | if (NULL == target) { |
| 409 | return; |
| 410 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 411 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 412 | click->fIOrig.set(x, y); |
| 413 | click->fICurr = click->fIPrev = click->fIOrig; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 414 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 415 | click->fOrig.iset(x, y); |
| 416 | if (!target->globalToLocal(&click->fOrig)) { |
| 417 | // no history to let us recover from this failure |
| 418 | return; |
| 419 | } |
| 420 | click->fPrev = click->fCurr = click->fOrig; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 421 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 422 | click->fState = Click::kDown_State; |
| 423 | target->onClick(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void SkView::DoClickMoved(Click* click, int x, int y) |
| 427 | { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 428 | SkASSERT(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 429 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 430 | SkView* target = (SkView*)SkEventSink::FindSink(click->fTargetID); |
| 431 | if (NULL == target) { |
| 432 | return; |
| 433 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 434 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 435 | click->fIPrev = click->fICurr; |
| 436 | click->fICurr.set(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 438 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 444 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 445 | click->fState = Click::kMoved_State; |
| 446 | target->onClick(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void SkView::DoClickUp(Click* click, int x, int y) |
| 450 | { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 451 | SkASSERT(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 452 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 453 | SkView* target = (SkView*)SkEventSink::FindSink(click->fTargetID); |
| 454 | if (NULL == target) { |
| 455 | return; |
| 456 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 457 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 458 | click->fIPrev = click->fICurr; |
| 459 | click->fICurr.set(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 460 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 461 | click->fPrev = click->fCurr; |
| 462 | click->fCurr.iset(x, y); |
| 463 | if (!target->globalToLocal(&click->fCurr)) { |
| 464 | // on failure pretend the mouse didn't move |
| 465 | click->fCurr = click->fPrev; |
| 466 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 467 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 468 | click->fState = Click::kUp_State; |
| 469 | target->onClick(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | ////////////////////////////////////////////////////////////////////// |
| 473 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 474 | void SkView::invokeLayout() { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 475 | SkView::Layout* layout = this->getLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 476 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 477 | if (layout) { |
| 478 | layout->layoutChildren(this); |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 479 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 480 | } |
| 481 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 482 | void SkView::onDraw(SkCanvas* canvas) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 483 | Artist* artist = this->getArtist(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 484 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 485 | if (artist) { |
| 486 | artist->draw(this, canvas); |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 487 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 488 | } |
| 489 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 490 | void SkView::onSizeChange() {} |
| 491 | |
| 492 | bool SkView::onSendClickToChildren(SkScalar x, SkScalar y) { |
| 493 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 494 | } |
| 495 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 496 | SkView::Click* SkView::onFindClickHandler(SkScalar x, SkScalar y) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 497 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 498 | } |
| 499 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 500 | bool SkView::onClick(Click*) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 501 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 502 | } |
| 503 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 504 | bool SkView::handleInval(const SkRect*) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 505 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | ////////////////////////////////////////////////////////////////////// |
| 509 | |
| 510 | void SkView::getLocalBounds(SkRect* bounds) const |
| 511 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 512 | if (bounds) |
| 513 | bounds->set(0, 0, fWidth, fHeight); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | ////////////////////////////////////////////////////////////////////// |
| 517 | ////////////////////////////////////////////////////////////////////// |
| 518 | |
| 519 | void SkView::detachFromParent_NoLayout() |
| 520 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 521 | if (fParent == NULL) |
| 522 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 523 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 524 | if (fContainsFocus) |
| 525 | (void)this->setFocusView(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 527 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 528 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 529 | SkView* next = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 531 | if (fNextSibling != this) // do we have any siblings |
| 532 | { |
| 533 | fNextSibling->fPrevSibling = fPrevSibling; |
| 534 | fPrevSibling->fNextSibling = fNextSibling; |
| 535 | next = fNextSibling; |
| 536 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 537 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 538 | if (fParent->fFirstChild == this) |
| 539 | fParent->fFirstChild = next; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 540 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 541 | fParent = fNextSibling = fPrevSibling = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 542 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 543 | this->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void SkView::detachFromParent() |
| 547 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 548 | SkView* parent = fParent; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 549 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 550 | if (parent) |
| 551 | { |
| 552 | this->detachFromParent_NoLayout(); |
| 553 | parent->invokeLayout(); |
| 554 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | SkView* SkView::attachChildToBack(SkView* child) |
| 558 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 559 | SkASSERT(child != this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 560 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 561 | if (child == NULL || fFirstChild == child) |
| 562 | goto DONE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 563 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 564 | child->ref(); |
| 565 | child->detachFromParent_NoLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 566 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 567 | if (fFirstChild == NULL) |
| 568 | { |
| 569 | child->fNextSibling = child; |
| 570 | child->fPrevSibling = child; |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | child->fNextSibling = fFirstChild; |
| 575 | child->fPrevSibling = fFirstChild->fPrevSibling; |
| 576 | fFirstChild->fPrevSibling->fNextSibling = child; |
| 577 | fFirstChild->fPrevSibling = child; |
| 578 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 579 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 580 | fFirstChild = child; |
| 581 | child->fParent = this; |
| 582 | child->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 583 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 584 | this->invokeLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 585 | DONE: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 586 | return child; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | SkView* SkView::attachChildToFront(SkView* child) |
| 590 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 591 | SkASSERT(child != this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 592 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 593 | if (child == NULL || (fFirstChild && fFirstChild->fPrevSibling == child)) |
| 594 | goto DONE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 595 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 596 | child->ref(); |
| 597 | child->detachFromParent_NoLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 598 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 599 | if (fFirstChild == NULL) |
| 600 | { |
| 601 | fFirstChild = child; |
| 602 | child->fNextSibling = child; |
| 603 | child->fPrevSibling = child; |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | child->fNextSibling = fFirstChild; |
| 608 | child->fPrevSibling = fFirstChild->fPrevSibling; |
| 609 | fFirstChild->fPrevSibling->fNextSibling = child; |
| 610 | fFirstChild->fPrevSibling = child; |
| 611 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 612 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 613 | child->fParent = this; |
| 614 | child->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 615 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 616 | this->invokeLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 617 | DONE: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 618 | return child; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | void SkView::detachAllChildren() |
| 622 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 623 | while (fFirstChild) |
| 624 | fFirstChild->detachFromParent_NoLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 625 | } |
| 626 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 627 | void SkView::localToGlobal(SkMatrix* matrix) const |
| 628 | { |
| 629 | if (matrix) { |
| 630 | matrix->reset(); |
| 631 | const SkView* view = this; |
| 632 | while (view) |
| 633 | { |
| 634 | matrix->preConcat(view->getLocalMatrix()); |
| 635 | matrix->preTranslate(-view->fLoc.fX, -view->fLoc.fY); |
| 636 | view = view->fParent; |
| 637 | } |
| 638 | } |
| 639 | } |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 640 | bool SkView::globalToLocal(SkScalar x, SkScalar y, SkPoint* local) const |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 641 | { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 642 | SkASSERT(this); |
| 643 | |
| 644 | if (NULL != local) { |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 645 | SkMatrix m; |
| 646 | this->localToGlobal(&m); |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 647 | if (!m.invert(&m)) { |
| 648 | return false; |
| 649 | } |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 650 | SkPoint p; |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 651 | m.mapXY(x, y, &p); |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 652 | local->set(p.fX, p.fY); |
| 653 | } |
| 654 | |
| 655 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | ////////////////////////////////////////////////////////////////// |
| 659 | |
| 660 | /* Even if the subclass overrides onInflate, they should always be |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 661 | sure to call the inherited method, so that we get called. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 662 | */ |
| 663 | void SkView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 664 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 665 | SkScalar x, y; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 666 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 667 | x = this->locX(); |
| 668 | y = this->locY(); |
| 669 | (void)dom.findScalar(node, "x", &x); |
| 670 | (void)dom.findScalar(node, "y", &y); |
| 671 | this->setLoc(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 672 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 673 | x = this->width(); |
| 674 | y = this->height(); |
| 675 | (void)dom.findScalar(node, "width", &x); |
| 676 | (void)dom.findScalar(node, "height", &y); |
| 677 | this->setSize(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 678 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 679 | // inflate the flags |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 680 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 681 | static const char* gFlagNames[] = { |
| 682 | "visible", "enabled", "focusable", "flexH", "flexV" |
| 683 | }; |
| 684 | SkASSERT(SK_ARRAY_COUNT(gFlagNames) == kFlagShiftCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 685 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 686 | bool b; |
| 687 | uint32_t flags = this->getFlags(); |
| 688 | for (unsigned i = 0; i < SK_ARRAY_COUNT(gFlagNames); i++) |
| 689 | if (dom.findBool(node, gFlagNames[i], &b)) |
| 690 | flags = SkSetClearShift(flags, b, i); |
| 691 | this->setFlags(flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | void SkView::inflate(const SkDOM& dom, const SkDOM::Node* node) |
| 695 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 696 | this->onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void SkView::onPostInflate(const SkTDict<SkView*>&) |
| 700 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 701 | // override in subclass as needed |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | void SkView::postInflate(const SkTDict<SkView*>& dict) |
| 705 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 706 | this->onPostInflate(dict); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 707 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 708 | B2FIter iter(this); |
| 709 | SkView* child; |
| 710 | while ((child = iter.next()) != NULL) |
| 711 | child->postInflate(dict); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | ////////////////////////////////////////////////////////////////// |
| 715 | |
| 716 | SkView* SkView::sendEventToParents(const SkEvent& evt) |
| 717 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 718 | SkView* parent = fParent; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 719 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 720 | while (parent) |
| 721 | { |
| 722 | if (parent->doEvent(evt)) |
| 723 | return parent; |
| 724 | parent = parent->fParent; |
| 725 | } |
| 726 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 727 | } |
| 728 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 729 | SkView* SkView::sendQueryToParents(SkEvent* evt) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 730 | SkView* parent = fParent; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 731 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 732 | while (parent) { |
| 733 | if (parent->doQuery(evt)) { |
| 734 | return parent; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 735 | } |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 736 | parent = parent->fParent; |
| 737 | } |
| 738 | return NULL; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 739 | } |
| 740 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 741 | ////////////////////////////////////////////////////////////////// |
| 742 | ////////////////////////////////////////////////////////////////// |
| 743 | |
| 744 | SkView::F2BIter::F2BIter(const SkView* parent) |
| 745 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 746 | fFirstChild = parent ? parent->fFirstChild : NULL; |
| 747 | fChild = fFirstChild ? fFirstChild->fPrevSibling : NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | SkView* SkView::F2BIter::next() |
| 751 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 752 | SkView* curr = fChild; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 753 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 754 | if (fChild) |
| 755 | { |
| 756 | if (fChild == fFirstChild) |
| 757 | fChild = NULL; |
| 758 | else |
| 759 | fChild = fChild->fPrevSibling; |
| 760 | } |
| 761 | return curr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | SkView::B2FIter::B2FIter(const SkView* parent) |
| 765 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 766 | fFirstChild = parent ? parent->fFirstChild : NULL; |
| 767 | fChild = fFirstChild; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | SkView* SkView::B2FIter::next() |
| 771 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 772 | SkView* curr = fChild; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 773 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 774 | if (fChild) |
| 775 | { |
| 776 | SkView* next = fChild->fNextSibling; |
| 777 | if (next == fFirstChild) |
| 778 | next = NULL; |
| 779 | fChild = next; |
| 780 | } |
| 781 | return curr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | ////////////////////////////////////////////////////////////////// |
| 785 | ////////////////////////////////////////////////////////////////// |
| 786 | |
| 787 | #ifdef SK_DEBUG |
| 788 | |
| 789 | static inline void show_if_nonzero(const char name[], SkScalar value) |
| 790 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 791 | if (value) |
| 792 | SkDebugf("%s=\"%g\"", name, value/65536.); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static void tab(int level) |
| 796 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 797 | for (int i = 0; i < level; i++) |
| 798 | SkDebugf(" "); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | static void dumpview(const SkView* view, int level, bool recurse) |
| 802 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 803 | tab(level); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 804 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 805 | SkDebugf("<view"); |
| 806 | show_if_nonzero(" x", view->locX()); |
| 807 | show_if_nonzero(" y", view->locY()); |
| 808 | show_if_nonzero(" width", view->width()); |
| 809 | show_if_nonzero(" height", view->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 810 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 811 | if (recurse) |
| 812 | { |
| 813 | SkView::B2FIter iter(view); |
| 814 | SkView* child; |
| 815 | bool noChildren = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 816 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 817 | while ((child = iter.next()) != NULL) |
| 818 | { |
| 819 | if (noChildren) |
| 820 | SkDebugf(">\n"); |
| 821 | noChildren = false; |
| 822 | dumpview(child, level + 1, true); |
| 823 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 824 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 825 | if (!noChildren) |
| 826 | { |
| 827 | tab(level); |
| 828 | SkDebugf("</view>\n"); |
| 829 | } |
| 830 | else |
| 831 | goto ONELINER; |
| 832 | } |
| 833 | else |
| 834 | { |
| 835 | ONELINER: |
| 836 | SkDebugf(" />\n"); |
| 837 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | void SkView::dump(bool recurse) const |
| 841 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 842 | dumpview(this, 0, recurse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | #endif |