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 "SkWidgetViews.h" |
| 9 | #include "SkAnimator.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkStream.h" |
| 13 | #include "SkSystemEventTypes.h" |
| 14 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | /* |
| 16 | I have moved this to SkWidgetViews.h |
| 17 | enum SkinEnum { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 18 | kButton_SkinEnum, |
| 19 | kProgress_SkinEnum, |
| 20 | kScroll_SkinEnum, |
| 21 | kStaticText_SkinEnum, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 22 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 23 | kSkinEnumCount |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | }; |
| 25 | */ |
| 26 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 27 | SK_DEFINE_INST_COUNT(SkListSource) |
| 28 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | const char* get_skin_enum_path(SkinEnum se) |
| 30 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 31 | SkASSERT((unsigned)se < kSkinEnumCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 33 | static const char* gSkinPaths[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | "common/default/default/skins/border3.xml", |
| 35 | "common/default/default/skins/button.xml", |
| 36 | "common/default/default/skins/progressBar.xml", |
| 37 | "common/default/default/skins/scrollBar.xml", |
| 38 | "common/default/default/skins/statictextpaint.xml" |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 39 | }; |
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 | return gSkinPaths[se]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void init_skin_anim(const char path[], SkAnimator* anim) |
| 45 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 46 | SkASSERT(path && anim); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 48 | SkFILEStream stream(path); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 50 | if (!stream.isValid()) |
| 51 | { |
| 52 | SkDEBUGF(("init_skin_anim: loading skin failed <%s>\n", path)); |
| 53 | sk_throw(); |
| 54 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 56 | if (!anim->decodeStream(&stream)) |
| 57 | { |
| 58 | SkDEBUGF(("init_skin_anim: decoding skin failed <%s>\n", path)); |
| 59 | sk_throw(); |
| 60 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void init_skin_anim(SkinEnum se, SkAnimator* anim) |
| 64 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 65 | init_skin_anim(get_skin_enum_path(se), anim); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void init_skin_paint(SkinEnum se, SkPaint* paint) |
| 69 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 70 | SkASSERT(paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 72 | SkAnimator anim; |
| 73 | SkCanvas canvas; |
| 74 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 75 | init_skin_anim(se, &anim); |
| 76 | anim.draw(&canvas, paint, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void inflate_paint(const SkDOM& dom, const SkDOM::Node* node, SkPaint* paint) |
| 80 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 81 | SkASSERT(paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 83 | SkAnimator anim; |
| 84 | SkCanvas canvas; |
| 85 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 86 | if (!anim.decodeDOM(dom, node)) |
| 87 | { |
| 88 | SkDEBUGF(("inflate_paint: decoding dom failed\n")); |
| 89 | SkDEBUGCODE(dom.dump(node);) |
| 90 | sk_throw(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 91 | } |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 92 | anim.draw(&canvas, paint, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | //////////////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | SkWidgetView::SkWidgetView() : SkView(SkView::kFocusable_Mask | SkView::kEnabled_Mask) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | const char* SkWidgetView::getLabel() const |
| 102 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 103 | return fLabel.c_str(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | void SkWidgetView::getLabel(SkString* label) const |
| 107 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 108 | if (label) |
| 109 | *label = fLabel; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void SkWidgetView::setLabel(const char label[]) |
| 113 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 114 | this->setLabel(label, label ? strlen(label) : 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void SkWidgetView::setLabel(const char label[], size_t len) |
| 118 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 119 | if ((label == NULL && fLabel.size() != 0) || !fLabel.equals(label, len)) |
| 120 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 121 | SkString tmp(label, len); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 123 | this->onLabelChange(fLabel.c_str(), tmp.c_str()); |
| 124 | fLabel.swap(tmp); |
| 125 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void SkWidgetView::setLabel(const SkString& label) |
| 129 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 130 | if (fLabel != label) |
| 131 | { |
| 132 | this->onLabelChange(fLabel.c_str(), label.c_str()); |
| 133 | fLabel = label; |
| 134 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool SkWidgetView::postWidgetEvent() |
| 138 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 139 | if (!fEvent.isType("")) |
| 140 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 141 | SkEvent evt(fEvent); // make a copy since onPrepareWidgetEvent may edit the event |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 143 | if (this->onPrepareWidgetEvent(&evt)) |
| 144 | { |
| 145 | SkDEBUGCODE(evt.dump("SkWidgetView::postWidgetEvent");) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 147 | this->postToListeners(evt); // wonder if this should return true if there are > 0 listeners... |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /*virtual*/ void SkWidgetView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 155 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 156 | this->INHERITED::onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 158 | const char* label = dom.findAttr(node, "label"); |
| 159 | if (label) |
| 160 | this->setLabel(label); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 161 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 162 | if ((node = dom.getFirstChild(node, "event")) != NULL) |
| 163 | fEvent.inflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /*virtual*/ void SkWidgetView::onLabelChange(const char oldLabel[], const char newLabel[]) |
| 167 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 168 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static const char gWidgetEventSinkIDSlotName[] = "sk-widget-sinkid-slot"; |
| 172 | |
| 173 | /*virtual*/ bool SkWidgetView::onPrepareWidgetEvent(SkEvent* evt) |
| 174 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 175 | evt->setS32(gWidgetEventSinkIDSlotName, this->getSinkID()); |
| 176 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | SkEventSinkID SkWidgetView::GetWidgetEventSinkID(const SkEvent& evt) |
| 180 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 181 | int32_t sinkID; |
| 182 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 183 | return evt.findS32(gWidgetEventSinkIDSlotName, &sinkID) ? (SkEventSinkID)sinkID : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 187 | |
| 188 | /*virtual*/ bool SkButtonView::onEvent(const SkEvent& evt) |
| 189 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 190 | if (evt.isType(SK_EventType_Key) && evt.getFast32() == kOK_SkKey) |
| 191 | { |
| 192 | this->postWidgetEvent(); |
| 193 | return true; |
| 194 | } |
| 195 | return this->INHERITED::onEvent(evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 199 | |
| 200 | SkCheckButtonView::SkCheckButtonView() : fCheckState(kOff_CheckState) |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | void SkCheckButtonView::setCheckState(CheckState state) |
| 205 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 206 | SkASSERT((unsigned)state <= kUnknown_CheckState); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 207 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 208 | if (fCheckState != state) |
| 209 | { |
| 210 | this->onCheckStateChange(this->getCheckState(), state); |
| 211 | fCheckState = SkToU8(state); |
| 212 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 213 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 214 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | /*virtual*/ void SkCheckButtonView::onCheckStateChange(CheckState oldState, CheckState newState) |
| 216 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 217 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /*virtual*/ void SkCheckButtonView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 221 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 222 | this->INHERITED::onInflate(dom, node); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 223 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 224 | int index = dom.findList(node, "check-state", "off,on,unknown"); |
| 225 | if (index >= 0) |
| 226 | this->setCheckState((CheckState)index); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static const char gCheckStateSlotName[] = "sk-checkbutton-check-slot"; |
| 230 | |
| 231 | /*virtual*/ bool SkCheckButtonView::onPrepareWidgetEvent(SkEvent* evt) |
| 232 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 233 | // could check if we're "disabled", and return false... |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 235 | evt->setS32(gCheckStateSlotName, this->getCheckState()); |
| 236 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | bool SkCheckButtonView::GetWidgetEventCheckState(const SkEvent& evt, CheckState* state) |
| 240 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 241 | int32_t state32; |
| 242 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 243 | if (evt.findS32(gCheckStateSlotName, &state32)) |
| 244 | { |
| 245 | if (state) |
| 246 | *state = (CheckState)state32; |
| 247 | return true; |
| 248 | } |
| 249 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 253 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 254 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 255 | |
| 256 | #include "SkTime.h" |
| 257 | #include <stdio.h> |
| 258 | |
| 259 | class SkAnimButtonView : public SkButtonView { |
| 260 | public: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 261 | SkAnimButtonView() |
| 262 | { |
| 263 | fAnim.setHostEventSink(this); |
| 264 | init_skin_anim(kButton_SkinEnum, &fAnim); |
| 265 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 266 | |
| 267 | protected: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 268 | virtual void onLabelChange(const char oldLabel[], const char newLabel[]) |
| 269 | { |
| 270 | this->INHERITED::onLabelChange(oldLabel, newLabel); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 272 | SkEvent evt("user"); |
| 273 | evt.setString("id", "setLabel"); |
| 274 | evt.setString("LABEL", newLabel); |
| 275 | fAnim.doUserEvent(evt); |
| 276 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 277 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 278 | virtual void onFocusChange(bool gainFocus) |
| 279 | { |
| 280 | this->INHERITED::onFocusChange(gainFocus); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 282 | SkEvent evt("user"); |
| 283 | evt.setString("id", "setFocus"); |
| 284 | evt.setS32("FOCUS", gainFocus); |
| 285 | fAnim.doUserEvent(evt); |
| 286 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 287 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 288 | virtual void onSizeChange() |
| 289 | { |
| 290 | this->INHERITED::onSizeChange(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 292 | SkEvent evt("user"); |
| 293 | evt.setString("id", "setDim"); |
| 294 | evt.setScalar("dimX", this->width()); |
| 295 | evt.setScalar("dimY", this->height()); |
| 296 | fAnim.doUserEvent(evt); |
| 297 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 299 | virtual void onDraw(SkCanvas* canvas) |
| 300 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 301 | SkPaint paint; |
| 302 | SkAnimator::DifferenceType diff = fAnim.draw(canvas, &paint, SkTime::GetMSecs()); |
| 303 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 304 | if (diff == SkAnimator::kDifferent) |
| 305 | this->inval(NULL); |
| 306 | else if (diff == SkAnimator::kPartiallyDifferent) |
| 307 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 308 | SkRect bounds; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 309 | fAnim.getInvalBounds(&bounds); |
| 310 | this->inval(&bounds); |
| 311 | } |
| 312 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 313 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 314 | virtual bool onEvent(const SkEvent& evt) |
| 315 | { |
| 316 | if (evt.isType(SK_EventType_Inval)) |
| 317 | { |
| 318 | this->inval(NULL); |
| 319 | return true; |
| 320 | } |
| 321 | if (evt.isType("recommendDim")) |
| 322 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 323 | SkScalar height; |
| 324 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 325 | if (evt.findScalar("y", &height)) |
| 326 | this->setHeight(height); |
| 327 | return true; |
| 328 | } |
| 329 | return this->INHERITED::onEvent(evt); |
| 330 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 331 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 332 | virtual bool onPrepareWidgetEvent(SkEvent* evt) |
| 333 | { |
| 334 | if (this->INHERITED::onPrepareWidgetEvent(evt)) |
| 335 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 336 | SkEvent e("user"); |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 337 | e.setString("id", "handlePress"); |
| 338 | (void)fAnim.doUserEvent(e); |
| 339 | return true; |
| 340 | } |
| 341 | return false; |
| 342 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | |
| 344 | private: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 345 | SkAnimator fAnim; |
| 346 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 347 | typedef SkButtonView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | //////////////////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 351 | //////////////////////////////////////////////////////////////////////////////////////////// |
| 352 | |
| 353 | SkView* SkWidgetFactory(const char name[]) |
| 354 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 355 | if (name == NULL) |
| 356 | return NULL; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 357 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 358 | // must be in the same order as the SkSkinWidgetEnum is declared |
| 359 | static const char* gNames[] = { |
| 360 | "sk-border", |
| 361 | "sk-button", |
| 362 | "sk-image", |
| 363 | "sk-list", |
| 364 | "sk-progress", |
| 365 | "sk-scroll", |
| 366 | "sk-text" |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 367 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 368 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 370 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); i++) |
| 371 | if (!strcmp(gNames[i], name)) |
| 372 | return SkWidgetFactory((SkWidgetEnum)i); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 374 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | #include "SkImageView.h" |
| 378 | #include "SkProgressBarView.h" |
| 379 | #include "SkScrollBarView.h" |
| 380 | #include "SkBorderView.h" |
| 381 | |
| 382 | SkView* SkWidgetFactory(SkWidgetEnum sw) |
| 383 | { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 384 | switch (sw) { |
| 385 | case kBorder_WidgetEnum: |
| 386 | return new SkBorderView; |
| 387 | case kButton_WidgetEnum: |
| 388 | return new SkAnimButtonView; |
| 389 | case kImage_WidgetEnum: |
| 390 | return new SkImageView; |
| 391 | case kList_WidgetEnum: |
| 392 | return new SkListView; |
| 393 | case kProgress_WidgetEnum: |
| 394 | return new SkProgressBarView; |
| 395 | case kScroll_WidgetEnum: |
| 396 | return new SkScrollBarView; |
| 397 | case kText_WidgetEnum: |
| 398 | return new SkStaticTextView; |
| 399 | default: |
| 400 | SkDEBUGFAIL("unknown enum passed to SkWidgetFactory"); |
| 401 | break; |
| 402 | } |
| 403 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 404 | } |