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 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | void SkView::setLocalMatrix(const SkMatrix& matrix) |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 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 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 109 | SkRect r; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 110 | r.set(fLoc.fX, fLoc.fY, fLoc.fX + fWidth, fLoc.fY + fHeight); |
| 111 | if (this->isClipToBounds() && |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 112 | canvas->quickReject(r)) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +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 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 121 | |
| 122 | canvas->translate(fLoc.fX, fLoc.fY); |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 123 | canvas->concat(fMatrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 136 | |
| 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); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 144 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +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; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 187 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 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 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 197 | SkView* focus = NULL; |
| 198 | const SkView* view = this; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 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 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 226 | B2FIter iter(this); |
| 227 | SkView* child, *focus; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 228 | while ((child = iter.next()) != NULL) |
| 229 | if ((focus = child->acceptFocus(dir)) != NULL) |
| 230 | return focus; |
| 231 | } |
| 232 | else // prev |
| 233 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 234 | F2BIter iter(this); |
| 235 | SkView* child, *focus; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 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) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 252 | { // start with the root |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 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 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +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 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 282 | else // prevfocus |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 283 | { |
| 284 | parent = focus->fParent; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 285 | if (parent == NULL) // we're the root |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 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 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 367 | size_t len = strlen(type) + 1; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 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 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 375 | SkView::Click* SkView::findClickHandler(SkScalar x, SkScalar y, unsigned modi) { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 376 | if (x < 0 || y < 0 || x >= fWidth || y >= fHeight) { |
| 377 | return NULL; |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 378 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 379 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 380 | if (this->onSendClickToChildren(x, y, modi)) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 381 | F2BIter iter(this); |
| 382 | SkView* child; |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 383 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 384 | while ((child = iter.next()) != NULL) |
| 385 | { |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 386 | SkPoint p; |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 387 | if (!child->globalToLocal(x, y, &p)) { |
| 388 | continue; |
| 389 | } |
| 390 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 391 | Click* click = child->findClickHandler(p.fX, p.fY, modi); |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 392 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 393 | if (click) { |
| 394 | return click; |
| 395 | } |
| 396 | } |
| 397 | } |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 398 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 399 | return this->onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 400 | } |
| 401 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 402 | void SkView::DoClickDown(Click* click, int x, int y, unsigned modi) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 403 | { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 404 | SkASSERT(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 406 | SkView* target = (SkView*)SkEventSink::FindSink(click->fTargetID); |
| 407 | if (NULL == target) { |
| 408 | return; |
| 409 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 410 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 411 | click->fIOrig.set(x, y); |
| 412 | click->fICurr = click->fIPrev = click->fIOrig; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 413 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 414 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 420 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 421 | click->fState = Click::kDown_State; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 422 | click->fModifierKeys = modi; |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 423 | target->onClick(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 424 | } |
| 425 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 426 | void SkView::DoClickMoved(Click* click, int x, int y, unsigned modi) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 446 | click->fModifierKeys = modi; |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 447 | target->onClick(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 448 | } |
| 449 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 450 | void SkView::DoClickUp(Click* click, int x, int y, unsigned modi) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 451 | { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 452 | SkASSERT(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 453 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 454 | SkView* target = (SkView*)SkEventSink::FindSink(click->fTargetID); |
| 455 | if (NULL == target) { |
| 456 | return; |
| 457 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 459 | click->fIPrev = click->fICurr; |
| 460 | click->fICurr.set(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 461 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 462 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 468 | |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 469 | click->fState = Click::kUp_State; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 470 | click->fModifierKeys = modi; |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 471 | target->onClick(click); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | ////////////////////////////////////////////////////////////////////// |
| 475 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 476 | void SkView::invokeLayout() { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 477 | SkView::Layout* layout = this->getLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 478 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 479 | if (layout) { |
| 480 | layout->layoutChildren(this); |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 481 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 482 | } |
| 483 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 484 | void SkView::onDraw(SkCanvas* canvas) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 485 | Artist* artist = this->getArtist(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 486 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 487 | if (artist) { |
| 488 | artist->draw(this, canvas); |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 489 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 490 | } |
| 491 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 492 | void SkView::onSizeChange() {} |
| 493 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 494 | bool SkView::onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) { |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 495 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 496 | } |
| 497 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame^] | 498 | SkView::Click* SkView::onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 499 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 500 | } |
| 501 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 502 | bool SkView::onClick(Click*) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 503 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 504 | } |
| 505 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 506 | bool SkView::handleInval(const SkRect*) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 507 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | ////////////////////////////////////////////////////////////////////// |
| 511 | |
| 512 | void SkView::getLocalBounds(SkRect* bounds) const |
| 513 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 514 | if (bounds) |
| 515 | bounds->set(0, 0, fWidth, fHeight); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | ////////////////////////////////////////////////////////////////////// |
| 519 | ////////////////////////////////////////////////////////////////////// |
| 520 | |
| 521 | void SkView::detachFromParent_NoLayout() |
| 522 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 523 | if (fParent == NULL) |
| 524 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 525 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 526 | if (fContainsFocus) |
| 527 | (void)this->setFocusView(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 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 531 | SkView* next = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 532 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 533 | if (fNextSibling != this) // do we have any siblings |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 534 | { |
| 535 | fNextSibling->fPrevSibling = fPrevSibling; |
| 536 | fPrevSibling->fNextSibling = fNextSibling; |
| 537 | next = fNextSibling; |
| 538 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 539 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 540 | if (fParent->fFirstChild == this) |
| 541 | fParent->fFirstChild = next; |
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 | fParent = fNextSibling = fPrevSibling = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 544 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 545 | this->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void SkView::detachFromParent() |
| 549 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 550 | SkView* parent = fParent; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 551 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 552 | if (parent) |
| 553 | { |
| 554 | this->detachFromParent_NoLayout(); |
| 555 | parent->invokeLayout(); |
| 556 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | SkView* SkView::attachChildToBack(SkView* child) |
| 560 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 561 | SkASSERT(child != this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 562 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 563 | if (child == NULL || fFirstChild == child) |
| 564 | goto DONE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 565 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 566 | child->ref(); |
| 567 | child->detachFromParent_NoLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 568 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 569 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 581 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 582 | fFirstChild = child; |
| 583 | child->fParent = this; |
| 584 | child->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 585 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 586 | this->invokeLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 587 | DONE: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 588 | return child; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | SkView* SkView::attachChildToFront(SkView* child) |
| 592 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 593 | SkASSERT(child != this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 594 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 595 | if (child == NULL || (fFirstChild && fFirstChild->fPrevSibling == child)) |
| 596 | goto DONE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 597 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 598 | child->ref(); |
| 599 | child->detachFromParent_NoLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 600 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 601 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 614 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 615 | child->fParent = this; |
| 616 | child->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 617 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 618 | this->invokeLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 619 | DONE: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 620 | return child; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void SkView::detachAllChildren() |
| 624 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 625 | while (fFirstChild) |
| 626 | fFirstChild->detachFromParent_NoLayout(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 627 | } |
| 628 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 629 | void 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.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 642 | bool SkView::globalToLocal(SkScalar x, SkScalar y, SkPoint* local) const |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 643 | { |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 644 | SkASSERT(this); |
| 645 | |
| 646 | if (NULL != local) { |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 647 | SkMatrix m; |
| 648 | this->localToGlobal(&m); |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 649 | if (!m.invert(&m)) { |
| 650 | return false; |
| 651 | } |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 652 | SkPoint p; |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 653 | m.mapXY(x, y, &p); |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 654 | local->set(p.fX, p.fY); |
| 655 | } |
| 656 | |
| 657 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | ////////////////////////////////////////////////////////////////// |
| 661 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 662 | /* Even if the subclass overrides onInflate, they should always be |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 663 | sure to call the inherited method, so that we get called. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 664 | */ |
| 665 | void SkView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 666 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 667 | SkScalar x, y; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 668 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 669 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 674 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 675 | 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.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 | // inflate the flags |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 682 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 683 | static const char* gFlagNames[] = { |
| 684 | "visible", "enabled", "focusable", "flexH", "flexV" |
| 685 | }; |
| 686 | SkASSERT(SK_ARRAY_COUNT(gFlagNames) == kFlagShiftCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 687 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 688 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void SkView::inflate(const SkDOM& dom, const SkDOM::Node* node) |
| 697 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 698 | this->onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | void SkView::onPostInflate(const SkTDict<SkView*>&) |
| 702 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 703 | // override in subclass as needed |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void SkView::postInflate(const SkTDict<SkView*>& dict) |
| 707 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 708 | this->onPostInflate(dict); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 709 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 710 | B2FIter iter(this); |
| 711 | SkView* child; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 712 | while ((child = iter.next()) != NULL) |
| 713 | child->postInflate(dict); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | ////////////////////////////////////////////////////////////////// |
| 717 | |
| 718 | SkView* SkView::sendEventToParents(const SkEvent& evt) |
| 719 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 720 | SkView* parent = fParent; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 721 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 722 | while (parent) |
| 723 | { |
| 724 | if (parent->doEvent(evt)) |
| 725 | return parent; |
| 726 | parent = parent->fParent; |
| 727 | } |
| 728 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 729 | } |
| 730 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 731 | SkView* SkView::sendQueryToParents(SkEvent* evt) { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 732 | SkView* parent = fParent; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 733 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 734 | while (parent) { |
| 735 | if (parent->doQuery(evt)) { |
| 736 | return parent; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 737 | } |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 738 | parent = parent->fParent; |
| 739 | } |
| 740 | return NULL; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 741 | } |
| 742 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 743 | ////////////////////////////////////////////////////////////////// |
| 744 | ////////////////////////////////////////////////////////////////// |
| 745 | |
| 746 | SkView::F2BIter::F2BIter(const SkView* parent) |
| 747 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 748 | fFirstChild = parent ? parent->fFirstChild : NULL; |
| 749 | fChild = fFirstChild ? fFirstChild->fPrevSibling : NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 750 | } |
| 751 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 752 | SkView* SkView::F2BIter::next() |
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 | SkView* curr = fChild; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 755 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 756 | if (fChild) |
| 757 | { |
| 758 | if (fChild == fFirstChild) |
| 759 | fChild = NULL; |
| 760 | else |
| 761 | fChild = fChild->fPrevSibling; |
| 762 | } |
| 763 | return curr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | SkView::B2FIter::B2FIter(const SkView* parent) |
| 767 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 768 | fFirstChild = parent ? parent->fFirstChild : NULL; |
| 769 | fChild = fFirstChild; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 770 | } |
| 771 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 772 | SkView* SkView::B2FIter::next() |
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 | SkView* curr = fChild; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 775 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 776 | if (fChild) |
| 777 | { |
| 778 | SkView* next = fChild->fNextSibling; |
| 779 | if (next == fFirstChild) |
| 780 | next = NULL; |
| 781 | fChild = next; |
| 782 | } |
| 783 | return curr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | ////////////////////////////////////////////////////////////////// |
| 787 | ////////////////////////////////////////////////////////////////// |
| 788 | |
| 789 | #ifdef SK_DEBUG |
| 790 | |
| 791 | static inline void show_if_nonzero(const char name[], SkScalar value) |
| 792 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 793 | if (value) |
| 794 | SkDebugf("%s=\"%g\"", name, value/65536.); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static void tab(int level) |
| 798 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 799 | for (int i = 0; i < level; i++) |
| 800 | SkDebugf(" "); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | static void dumpview(const SkView* view, int level, bool recurse) |
| 804 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 805 | tab(level); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 806 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 807 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 812 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 813 | if (recurse) |
| 814 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 815 | SkView::B2FIter iter(view); |
| 816 | SkView* child; |
| 817 | bool noChildren = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 818 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 819 | while ((child = iter.next()) != NULL) |
| 820 | { |
| 821 | if (noChildren) |
| 822 | SkDebugf(">\n"); |
| 823 | noChildren = false; |
| 824 | dumpview(child, level + 1, true); |
| 825 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 826 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 827 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | void SkView::dump(bool recurse) const |
| 843 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 844 | dumpview(this, 0, recurse); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | #endif |