gfx/skia/trunk/src/views/animated/SkWidgetViews.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial