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 "SkWidget.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkKey.h" |
| 11 | #include "SkParsePaint.h" |
| 12 | #include "SkSystemEventTypes.h" |
| 13 | #include "SkTextBox.h" |
| 14 | |
| 15 | #if 0 |
| 16 | |
| 17 | #ifdef SK_DEBUG |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 18 | static void assert_no_attr(const SkDOM& dom, const SkDOM::Node* node, const char attr[]) |
| 19 | { |
| 20 | const char* value = dom.findAttr(node, attr); |
| 21 | if (value) |
| 22 | SkDebugf("unknown attribute %s=\"%s\"\n", attr, value); |
| 23 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #else |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 25 | #define assert_no_attr(dom, node, attr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #include "SkAnimator.h" |
| 29 | #include "SkTime.h" |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | enum SkinType { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 34 | kPushButton_SkinType, |
| 35 | kStaticText_SkinType, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 37 | kSkinTypeCount |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct SkinSuite { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 41 | SkinSuite(); |
| 42 | ~SkinSuite() |
| 43 | { |
| 44 | for (int i = 0; i < kSkinTypeCount; i++) |
| 45 | delete fAnimators[i]; |
| 46 | } |
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 | SkAnimator* get(SkinType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | |
| 50 | private: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 51 | SkAnimator* fAnimators[kSkinTypeCount]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | SkinSuite::SkinSuite() |
| 55 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 56 | static const char kSkinPath[] = "skins/"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | static const char* gSkinNames[] = { |
| 59 | "pushbutton_skin.xml", |
| 60 | "statictext_skin.xml" |
| 61 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | for (unsigned i = 0; i < SK_ARRAY_COUNT(gSkinNames); i++) |
| 64 | { |
| 65 | size_t len = strlen(gSkinNames[i]); |
| 66 | SkString path(sizeof(kSkinPath) - 1 + len); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | memcpy(path.writable_str(), kSkinPath, sizeof(kSkinPath) - 1); |
| 69 | memcpy(path.writable_str() + sizeof(kSkinPath) - 1, gSkinNames[i], len); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | fAnimators[i] = new SkAnimator; |
| 72 | if (!fAnimators[i]->decodeURI(path.c_str())) |
| 73 | { |
| 74 | delete fAnimators[i]; |
| 75 | fAnimators[i] = NULL; |
| 76 | } |
| 77 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | SkAnimator* SkinSuite::get(SkinType st) |
| 81 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 82 | SkASSERT((unsigned)st < kSkinTypeCount); |
| 83 | return fAnimators[st]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static SkinSuite* gSkinSuite; |
| 87 | |
| 88 | static SkAnimator* get_skin_animator(SkinType st) |
| 89 | { |
| 90 | #if 0 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 91 | if (gSkinSuite == NULL) |
| 92 | gSkinSuite = new SkinSuite; |
| 93 | return gSkinSuite->get(st); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | #else |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | #endif |
| 97 | } |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | void SkWidget::Init() |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | void SkWidget::Term() |
| 106 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 107 | delete gSkinSuite; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void SkWidget::onEnabledChange() |
| 111 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 112 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void SkWidget::postWidgetEvent() |
| 116 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 117 | if (!fEvent.isType("") && this->hasListeners()) |
| 118 | { |
| 119 | this->prepareWidgetEvent(&fEvent); |
| 120 | this->postToListeners(fEvent); |
| 121 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void SkWidget::prepareWidgetEvent(SkEvent*) |
| 125 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 126 | // override in subclass to add any additional fields before posting |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void SkWidget::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 130 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 131 | this->INHERITED::onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 133 | if ((node = dom.getFirstChild(node, "event")) != NULL) |
| 134 | fEvent.inflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /////////////////////////////////////////////////////////////////////////////// |
| 138 | |
| 139 | size_t SkHasLabelWidget::getLabel(SkString* str) const |
| 140 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 141 | if (str) |
| 142 | *str = fLabel; |
| 143 | return fLabel.size(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | size_t SkHasLabelWidget::getLabel(char buffer[]) const |
| 147 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 148 | if (buffer) |
| 149 | memcpy(buffer, fLabel.c_str(), fLabel.size()); |
| 150 | return fLabel.size(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void SkHasLabelWidget::setLabel(const SkString& str) |
| 154 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 155 | this->setLabel(str.c_str(), str.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void SkHasLabelWidget::setLabel(const char label[]) |
| 159 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 160 | this->setLabel(label, strlen(label)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void SkHasLabelWidget::setLabel(const char label[], size_t len) |
| 164 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 165 | if (!fLabel.equals(label, len)) |
| 166 | { |
| 167 | fLabel.set(label, len); |
| 168 | this->onLabelChange(); |
| 169 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void SkHasLabelWidget::onLabelChange() |
| 173 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 174 | // override in subclass |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void SkHasLabelWidget::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 178 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 179 | this->INHERITED::onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 181 | const char* text = dom.findAttr(node, "label"); |
| 182 | if (text) |
| 183 | this->setLabel(text); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | ///////////////////////////////////////////////////////////////////////////////////// |
| 187 | |
| 188 | void SkButtonWidget::setButtonState(State state) |
| 189 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 190 | if (fState != state) |
| 191 | { |
| 192 | fState = state; |
| 193 | this->onButtonStateChange(); |
| 194 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void SkButtonWidget::onButtonStateChange() |
| 198 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 199 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void SkButtonWidget::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 203 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 204 | this->INHERITED::onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 206 | int index; |
| 207 | if ((index = dom.findList(node, "buttonState", "off,on,unknown")) >= 0) |
| 208 | this->setButtonState((State)index); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | ///////////////////////////////////////////////////////////////////////////////////// |
| 212 | |
| 213 | bool SkPushButtonWidget::onEvent(const SkEvent& evt) |
| 214 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 215 | if (evt.isType(SK_EventType_Key) && evt.getFast32() == kOK_SkKey) |
| 216 | { |
| 217 | this->postWidgetEvent(); |
| 218 | return true; |
| 219 | } |
| 220 | return this->INHERITED::onEvent(evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static const char* computeAnimatorState(int enabled, int focused, SkButtonWidget::State state) |
| 224 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 225 | if (!enabled) |
| 226 | return "disabled"; |
| 227 | if (state == SkButtonWidget::kOn_State) |
| 228 | { |
| 229 | SkASSERT(focused); |
| 230 | return "enabled-pressed"; |
| 231 | } |
| 232 | if (focused) |
| 233 | return "enabled-focused"; |
| 234 | return "enabled"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | #include "SkBlurMaskFilter.h" |
| 238 | #include "SkEmbossMaskFilter.h" |
| 239 | |
| 240 | static void create_emboss(SkPaint* paint, SkScalar radius, bool focus, bool pressed) |
| 241 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 242 | SkEmbossMaskFilter::Light light; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 244 | light.fDirection[0] = SK_Scalar1/2; |
| 245 | light.fDirection[1] = SK_Scalar1/2; |
| 246 | light.fDirection[2] = SK_Scalar1/3; |
| 247 | light.fAmbient = 0x48; |
| 248 | light.fSpecular = 0x80; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 249 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 250 | if (pressed) |
| 251 | { |
| 252 | light.fDirection[0] = -light.fDirection[0]; |
| 253 | light.fDirection[1] = -light.fDirection[1]; |
| 254 | } |
| 255 | if (focus) |
| 256 | light.fDirection[2] += SK_Scalar1/4; |
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 | paint->setMaskFilter(new SkEmbossMaskFilter(light, radius))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void SkPushButtonWidget::onDraw(SkCanvas* canvas) |
| 262 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 263 | this->INHERITED::onDraw(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 265 | SkString label; |
| 266 | this->getLabel(&label); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 267 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 268 | SkAnimator* anim = get_skin_animator(kPushButton_SkinType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 270 | if (anim) |
| 271 | { |
| 272 | SkEvent evt("user"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 273 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 274 | evt.setString("id", "prime"); |
| 275 | evt.setScalar("prime-width", this->width()); |
| 276 | evt.setScalar("prime-height", this->height()); |
| 277 | evt.setString("prime-text", label); |
| 278 | evt.setString("prime-state", computeAnimatorState(this->isEnabled(), this->hasFocus(), this->getButtonState())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 280 | (void)anim->doUserEvent(evt); |
| 281 | SkPaint paint; |
| 282 | anim->draw(canvas, &paint, SkTime::GetMSecs()); |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | SkRect r; |
| 287 | SkPaint p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 288 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 289 | r.set(0, 0, this->width(), this->height()); |
| 290 | p.setAntiAliasOn(true); |
| 291 | p.setColor(SK_ColorBLUE); |
| 292 | create_emboss(&p, SkIntToScalar(12)/5, this->hasFocus(), this->getButtonState() == kOn_State); |
| 293 | canvas->drawRoundRect(r, SkScalarHalf(this->height()), SkScalarHalf(this->height()), p); |
| 294 | p.setMaskFilter(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 296 | p.setTextAlign(SkPaint::kCenter_Align); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 298 | SkTextBox box; |
| 299 | box.setMode(SkTextBox::kOneLine_Mode); |
| 300 | box.setSpacingAlign(SkTextBox::kCenter_SpacingAlign); |
| 301 | box.setBox(0, 0, this->width(), this->height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 302 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 303 | // if (this->getButtonState() == kOn_State) |
| 304 | // p.setColor(SK_ColorRED); |
| 305 | // else |
| 306 | p.setColor(SK_ColorWHITE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 307 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 308 | box.draw(canvas, label.c_str(), label.size(), p); |
| 309 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 310 | } |
| 311 | |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 312 | SkView::Click* SkPushButtonWidget::onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 313 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 314 | this->acceptFocus(); |
| 315 | return new Click(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | bool SkPushButtonWidget::onClick(Click* click) |
| 319 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 320 | SkRect r; |
| 321 | State state = kOff_State; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 322 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 323 | this->getLocalBounds(&r); |
| 324 | if (r.contains(click->fCurr)) |
| 325 | { |
| 326 | if (click->fState == Click::kUp_State) |
| 327 | this->postWidgetEvent(); |
| 328 | else |
| 329 | state = kOn_State; |
| 330 | } |
| 331 | this->setButtonState(state); |
| 332 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 336 | |
| 337 | SkStaticTextView::SkStaticTextView(U32 flags) : SkView(flags) |
| 338 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 339 | fMargin.set(0, 0); |
| 340 | fMode = kFixedSize_Mode; |
| 341 | fSpacingAlign = SkTextBox::kStart_SpacingAlign; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | SkStaticTextView::~SkStaticTextView() |
| 345 | { |
| 346 | } |
| 347 | |
| 348 | void SkStaticTextView::computeSize() |
| 349 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 350 | if (fMode == kAutoWidth_Mode) |
| 351 | { |
| 352 | SkScalar width = fPaint.measureText(fText.c_str(), fText.size(), NULL, NULL); |
| 353 | this->setWidth(width + fMargin.fX * 2); |
| 354 | } |
| 355 | else if (fMode == kAutoHeight_Mode) |
| 356 | { |
| 357 | SkScalar width = this->width() - fMargin.fX * 2; |
| 358 | int lines = width > 0 ? SkTextLineBreaker::CountLines(fText.c_str(), fText.size(), fPaint, width) : 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 359 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 360 | SkScalar before, after; |
| 361 | (void)fPaint.measureText(0, NULL, &before, &after); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 363 | this->setHeight(lines * (after - before) + fMargin.fY * 2); |
| 364 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void SkStaticTextView::setMode(Mode mode) |
| 368 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 369 | SkASSERT((unsigned)mode < kModeCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 370 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 371 | if (fMode != mode) |
| 372 | { |
| 373 | fMode = SkToU8(mode); |
| 374 | this->computeSize(); |
| 375 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void SkStaticTextView::setSpacingAlign(SkTextBox::SpacingAlign align) |
| 379 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 380 | fSpacingAlign = SkToU8(align); |
| 381 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void SkStaticTextView::getMargin(SkPoint* margin) const |
| 385 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 386 | if (margin) |
| 387 | *margin = fMargin; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void SkStaticTextView::setMargin(SkScalar dx, SkScalar dy) |
| 391 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 392 | if (fMargin.fX != dx || fMargin.fY != dy) |
| 393 | { |
| 394 | fMargin.set(dx, dy); |
| 395 | this->computeSize(); |
| 396 | this->inval(NULL); |
| 397 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | size_t SkStaticTextView::getText(SkString* text) const |
| 401 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 402 | if (text) |
| 403 | *text = fText; |
| 404 | return fText.size(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | size_t SkStaticTextView::getText(char text[]) const |
| 408 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 409 | if (text) |
| 410 | memcpy(text, fText.c_str(), fText.size()); |
| 411 | return fText.size(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void SkStaticTextView::setText(const SkString& text) |
| 415 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 416 | this->setText(text.c_str(), text.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void SkStaticTextView::setText(const char text[]) |
| 420 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 421 | this->setText(text, strlen(text)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void SkStaticTextView::setText(const char text[], size_t len) |
| 425 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 426 | if (!fText.equals(text, len)) |
| 427 | { |
| 428 | fText.set(text, len); |
| 429 | this->computeSize(); |
| 430 | this->inval(NULL); |
| 431 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void SkStaticTextView::getPaint(SkPaint* paint) const |
| 435 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 436 | if (paint) |
| 437 | *paint = fPaint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void SkStaticTextView::setPaint(const SkPaint& paint) |
| 441 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 442 | if (fPaint != paint) |
| 443 | { |
| 444 | fPaint = paint; |
| 445 | this->computeSize(); |
| 446 | this->inval(NULL); |
| 447 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void SkStaticTextView::onDraw(SkCanvas* canvas) |
| 451 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 452 | this->INHERITED::onDraw(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 453 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 454 | if (fText.isEmpty()) |
| 455 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 456 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 457 | SkTextBox box; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 459 | box.setMode(fMode == kAutoWidth_Mode ? SkTextBox::kOneLine_Mode : SkTextBox::kLineBreak_Mode); |
| 460 | box.setSpacingAlign(this->getSpacingAlign()); |
| 461 | box.setBox(fMargin.fX, fMargin.fY, this->width() - fMargin.fX, this->height() - fMargin.fY); |
| 462 | box.draw(canvas, fText.c_str(), fText.size(), fPaint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void SkStaticTextView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 466 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 467 | this->INHERITED::onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 468 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 469 | int index; |
| 470 | if ((index = dom.findList(node, "mode", "fixed,auto-width,auto-height")) >= 0) |
| 471 | this->setMode((Mode)index); |
| 472 | else |
| 473 | assert_no_attr(dom, node, "mode"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 474 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 475 | if ((index = dom.findList(node, "spacing-align", "start,center,end")) >= 0) |
| 476 | this->setSpacingAlign((SkTextBox::SpacingAlign)index); |
| 477 | else |
| 478 | assert_no_attr(dom, node, "mode"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 479 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 480 | SkScalar s[2]; |
| 481 | if (dom.findScalars(node, "margin", s, 2)) |
| 482 | this->setMargin(s[0], s[1]); |
| 483 | else |
| 484 | assert_no_attr(dom, node, "margin"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 485 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 486 | const char* text = dom.findAttr(node, "text"); |
| 487 | if (text) |
| 488 | this->setText(text); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 489 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 490 | if ((node = dom.getFirstChild(node, "paint")) != NULL) |
| 491 | SkPaint_Inflate(&fPaint, dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 495 | |
| 496 | #include "SkImageDecoder.h" |
| 497 | |
| 498 | SkBitmapView::SkBitmapView(U32 flags) : SkView(flags) |
| 499 | { |
| 500 | } |
| 501 | |
| 502 | SkBitmapView::~SkBitmapView() |
| 503 | { |
| 504 | } |
| 505 | |
| 506 | bool SkBitmapView::getBitmap(SkBitmap* bitmap) const |
| 507 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 508 | if (bitmap) |
| 509 | *bitmap = fBitmap; |
| 510 | return fBitmap.getConfig() != SkBitmap::kNo_Config; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | void SkBitmapView::setBitmap(const SkBitmap* bitmap, bool viewOwnsPixels) |
| 514 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 515 | if (bitmap) |
| 516 | { |
| 517 | fBitmap = *bitmap; |
| 518 | fBitmap.setOwnsPixels(viewOwnsPixels); |
| 519 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | bool SkBitmapView::loadBitmapFromFile(const char path[]) |
| 523 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 524 | SkBitmap bitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 525 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 526 | if (SkImageDecoder::DecodeFile(path, &bitmap)) |
| 527 | { |
| 528 | this->setBitmap(&bitmap, true); |
| 529 | bitmap.setOwnsPixels(false); |
| 530 | return true; |
| 531 | } |
| 532 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | void SkBitmapView::onDraw(SkCanvas* canvas) |
| 536 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 537 | if (fBitmap.getConfig() != SkBitmap::kNo_Config && |
| 538 | fBitmap.width() && fBitmap.height()) |
| 539 | { |
| 540 | SkAutoCanvasRestore restore(canvas, true); |
| 541 | SkPaint p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 542 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 543 | p.setFilterType(SkPaint::kBilinear_FilterType); |
| 544 | canvas->scale( this->width() / fBitmap.width(), |
| 545 | this->height() / fBitmap.height(), |
| 546 | 0, 0); |
| 547 | canvas->drawBitmap(fBitmap, 0, 0, p); |
| 548 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | void SkBitmapView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 552 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 553 | this->INHERITED::onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 554 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 555 | const char* src = dom.findAttr(node, "src"); |
| 556 | if (src) |
| 557 | (void)this->loadBitmapFromFile(src); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | #endif |