| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2006 The Android Open Source Project |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkWidget_DEFINED |
| 11 | #define SkWidget_DEFINED |
| 12 | |
| 13 | #include "SkView.h" |
| 14 | #include "SkBitmap.h" |
| 15 | #include "SkDOM.h" |
| 16 | #include "SkPaint.h" |
| 17 | #include "SkString.h" |
| 18 | #include "SkTDArray.h" |
| 19 | |
| 20 | ////////////////////////////////////////////////////////////////////////////// |
| 21 | |
| 22 | class SkWidget : public SkView { |
| 23 | public: |
| 24 | SkWidget(uint32_t flags = 0) : SkView(flags | kFocusable_Mask | kEnabled_Mask) {} |
| 25 | |
| 26 | /** Call this to post the widget's event to its listeners */ |
| 27 | void postWidgetEvent(); |
| 28 | |
| 29 | static void Init(); |
| 30 | static void Term(); |
| 31 | protected: |
| 32 | // override to add slots to an event before posting |
| 33 | virtual void prepareWidgetEvent(SkEvent*); |
| 34 | virtual void onEnabledChange(); |
| 35 | |
| 36 | // <event ...> to initialize the event from XML |
| 37 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); |
| 38 | |
| 39 | private: |
| 40 | SkEvent fEvent; |
| 41 | typedef SkView INHERITED; |
| 42 | }; |
| 43 | |
| 44 | class SkHasLabelWidget : public SkWidget { |
| 45 | public: |
| 46 | SkHasLabelWidget(uint32_t flags = 0) : SkWidget(flags) {} |
| 47 | |
| 48 | size_t getLabel(SkString* label = NULL) const; |
| 49 | size_t getLabel(char lable[] = NULL) const; |
| 50 | void setLabel(const SkString&); |
| 51 | void setLabel(const char label[]); |
| 52 | void setLabel(const char label[], size_t len); |
| 53 | |
| 54 | protected: |
| 55 | // called when the label changes |
| 56 | virtual void onLabelChange(); |
| 57 | |
| 58 | // overrides |
| 59 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); |
| 60 | |
| 61 | private: |
| 62 | SkString fLabel; |
| 63 | typedef SkWidget INHERITED; |
| 64 | }; |
| 65 | |
| 66 | class SkButtonWidget : public SkHasLabelWidget { |
| 67 | public: |
| 68 | SkButtonWidget(uint32_t flags = 0) : SkHasLabelWidget(flags), fState(kOff_State) {} |
| 69 | |
| 70 | enum State { |
| 71 | kOff_State, //!< XML: buttonState="off" |
| 72 | kOn_State, //!< XML: buttonState="on" |
| 73 | kUnknown_State //!< XML: buttonState="unknown" |
| 74 | }; |
| 75 | State getButtonState() const { return fState; } |
| 76 | void setButtonState(State); |
| 77 | |
| 78 | protected: |
| 79 | /** called when the label changes. default behavior is to inval the widget */ |
| 80 | virtual void onButtonStateChange(); |
| 81 | |
| 82 | // overrides |
| 83 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); |
| 84 | |
| 85 | private: |
| 86 | State fState; |
| 87 | typedef SkHasLabelWidget INHERITED; |
| 88 | }; |
| 89 | |
| 90 | class SkPushButtonWidget : public SkButtonWidget { |
| 91 | public: |
| 92 | SkPushButtonWidget(uint32_t flags = 0) : SkButtonWidget(flags) {} |
| 93 | |
| 94 | protected: |
| 95 | virtual bool onEvent(const SkEvent&); |
| 96 | virtual void onDraw(SkCanvas*); |
| 97 | virtual Click* onFindClickHandler(SkScalar x, SkScalar y); |
| 98 | virtual bool onClick(Click* click); |
| 99 | |
| 100 | private: |
| 101 | typedef SkButtonWidget INHERITED; |
| 102 | }; |
| 103 | |
| 104 | class SkCheckBoxWidget : public SkButtonWidget { |
| 105 | public: |
| 106 | SkCheckBoxWidget(uint32_t flags = 0); |
| 107 | |
| 108 | protected: |
| 109 | virtual bool onEvent(const SkEvent&); |
| 110 | virtual void onDraw(SkCanvas*); |
| 111 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); |
| 112 | |
| 113 | private: |
| 114 | typedef SkButtonWidget INHERITED; |
| 115 | }; |
| 116 | |
| 117 | #include "SkTextBox.h" |
| 118 | |
| 119 | class SkStaticTextView : public SkView { |
| 120 | public: |
| 121 | SkStaticTextView(uint32_t flags = 0); |
| 122 | virtual ~SkStaticTextView(); |
| 123 | |
| 124 | enum Mode { |
| 125 | kFixedSize_Mode, |
| 126 | kAutoWidth_Mode, |
| 127 | kAutoHeight_Mode, |
| 128 | |
| 129 | kModeCount |
| 130 | }; |
| 131 | Mode getMode() const { return (Mode)fMode; } |
| 132 | void setMode(Mode); |
| 133 | |
| 134 | SkTextBox::SpacingAlign getSpacingAlign() const { return (SkTextBox::SpacingAlign)fSpacingAlign; } |
| 135 | void setSpacingAlign(SkTextBox::SpacingAlign); |
| 136 | |
| 137 | void getMargin(SkPoint* margin) const; |
| 138 | void setMargin(SkScalar dx, SkScalar dy); |
| 139 | |
| 140 | size_t getText(SkString* text = NULL) const; |
| 141 | size_t getText(char text[] = NULL) const; |
| 142 | void setText(const SkString&); |
| 143 | void setText(const char text[]); |
| 144 | void setText(const char text[], size_t len); |
| 145 | |
| 146 | void getPaint(SkPaint*) const; |
| 147 | void setPaint(const SkPaint&); |
| 148 | |
| 149 | protected: |
| 150 | // overrides |
| 151 | virtual void onDraw(SkCanvas*); |
| 152 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); |
| 153 | |
| 154 | private: |
| 155 | SkPoint fMargin; |
| 156 | SkString fText; |
| 157 | SkPaint fPaint; |
| 158 | uint8_t fMode; |
| 159 | uint8_t fSpacingAlign; |
| 160 | |
| 161 | void computeSize(); |
| 162 | |
| 163 | typedef SkView INHERITED; |
| 164 | }; |
| 165 | |
| 166 | class SkBitmapView : public SkView { |
| 167 | public: |
| 168 | SkBitmapView(uint32_t flags = 0); |
| 169 | virtual ~SkBitmapView(); |
| 170 | |
| 171 | bool getBitmap(SkBitmap*) const; |
| 172 | void setBitmap(const SkBitmap*, bool viewOwnsPixels); |
| 173 | bool loadBitmapFromFile(const char path[]); |
| 174 | |
| 175 | protected: |
| 176 | virtual void onDraw(SkCanvas*); |
| 177 | virtual void onInflate(const SkDOM&, const SkDOM::Node*); |
| 178 | |
| 179 | private: |
| 180 | SkBitmap fBitmap; |
| 181 | typedef SkView INHERITED; |
| 182 | }; |
| 183 | |
| 184 | ///////////////////////////////////////////////////////////////////////////// |
| 185 | |
| 186 | class SkShader; |
| 187 | class SkInterpolator; |
| 188 | |
| 189 | class SkWidgetView : public SkView { |
| 190 | public: |
| 191 | SkWidgetView(uint32_t flags = 0); |
| 192 | virtual ~SkWidgetView(); |
| 193 | |
| 194 | static const char* GetEventType(); |
| 195 | }; |
| 196 | |
| 197 | class SkSliderView : public SkWidgetView { |
| 198 | public: |
| 199 | SkSliderView(uint32_t flags = 0); |
| 200 | |
| 201 | uint16_t getValue() const { return fValue; } |
| 202 | uint16_t getMax() const { return fMax; } |
| 203 | |
| 204 | void setMax(U16CPU max); |
| 205 | void setValue(U16CPU value); |
| 206 | |
| 207 | protected: |
| 208 | virtual void onDraw(SkCanvas*); |
| 209 | virtual Click* onFindClickHandler(SkScalar x, SkScalar y); |
| 210 | virtual bool onClick(Click*); |
| 211 | |
| 212 | private: |
| 213 | uint16_t fValue, fMax; |
| 214 | |
| 215 | typedef SkWidgetView INHERITED; |
| 216 | }; |
| 217 | |
| 218 | ////////////////////////////////////////////////////////////////////////////// |
| 219 | |
| 220 | class SkHasLabelView : public SkView { |
| 221 | public: |
| 222 | void getLabel(SkString*) const; |
| 223 | void setLabel(const SkString&); |
| 224 | void setLabel(const char label[]); |
| 225 | |
| 226 | protected: |
| 227 | SkString fLabel; |
| 228 | |
| 229 | // called when the label changes |
| 230 | virtual void onLabelChange(); |
| 231 | |
| 232 | // overrides |
| 233 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); |
| 234 | }; |
| 235 | |
| 236 | class SkPushButtonView : public SkHasLabelView { |
| 237 | public: |
| 238 | SkPushButtonView(uint32_t flags = 0); |
| 239 | |
| 240 | protected: |
| 241 | virtual void onDraw(SkCanvas*); |
| 242 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); |
| 243 | }; |
| 244 | |
| 245 | class SkCheckBoxView : public SkHasLabelView { |
| 246 | public: |
| 247 | SkCheckBoxView(uint32_t flags = 0); |
| 248 | |
| 249 | enum State { |
| 250 | kOff_State, |
| 251 | kOn_State, |
| 252 | kMaybe_State |
| 253 | }; |
| 254 | State getState() const { return fState; } |
| 255 | void setState(State); |
| 256 | |
| 257 | protected: |
| 258 | virtual void onDraw(SkCanvas*); |
| 259 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); |
| 260 | |
| 261 | private: |
| 262 | State fState; |
| 263 | }; |
| 264 | |
| 265 | class SkProgressView : public SkView { |
| 266 | public: |
| 267 | SkProgressView(uint32_t flags = 0); |
| 268 | virtual ~SkProgressView(); |
| 269 | |
| 270 | uint16_t getValue() const { return fValue; } |
| 271 | uint16_t getMax() const { return fMax; } |
| 272 | |
| 273 | void setMax(U16CPU max); |
| 274 | void setValue(U16CPU value); |
| 275 | |
| 276 | protected: |
| 277 | virtual void onDraw(SkCanvas*); |
| 278 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); |
| 279 | |
| 280 | private: |
| 281 | uint16_t fValue, fMax; |
| 282 | SkShader* fOnShader, *fOffShader; |
| 283 | SkInterpolator* fInterp; |
| 284 | bool fDoInterp; |
| 285 | |
| 286 | typedef SkView INHERITED; |
| 287 | }; |
| 288 | |
| 289 | class SkTextView : public SkView { |
| 290 | public: |
| 291 | SkTextView(uint32_t flags = 0); |
| 292 | virtual ~SkTextView(); |
| 293 | |
| 294 | enum AnimaDir { |
| 295 | kNeutral_AnimDir, |
| 296 | kForward_AnimDir, |
| 297 | kBackward_AnimDir, |
| 298 | kAnimDirCount |
| 299 | }; |
| 300 | |
| 301 | void getText(SkString*) const; |
| 302 | void setText(const SkString&, AnimaDir dir = kNeutral_AnimDir); |
| 303 | void setText(const char text[], AnimaDir dir = kNeutral_AnimDir); |
| 304 | void setText(const char text[], size_t len, AnimaDir dir = kNeutral_AnimDir); |
| 305 | |
| 306 | void getMargin(SkPoint* margin) const; |
| 307 | void setMargin(const SkPoint&); |
| 308 | |
| 309 | SkPaint& paint() { return fPaint; } |
| 310 | |
| 311 | protected: |
| 312 | virtual void onDraw(SkCanvas*); |
| 313 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); |
| 314 | |
| 315 | private: |
| 316 | SkString fText; |
| 317 | SkPaint fPaint; |
| 318 | SkPoint fMargin; |
| 319 | |
| 320 | class Interp; |
| 321 | Interp* fInterp; |
| 322 | bool fDoInterp; |
| 323 | // called by the other setText methods. This guy does not check for != |
| 324 | // before doing the assign, so the caller must check for us |
| 325 | void privSetText(const SkString&, AnimaDir dir); |
| 326 | |
| 327 | typedef SkView INHERITED; |
| 328 | }; |
| 329 | |
| 330 | ////////////////////////////////////////////////////////// |
| 331 | |
| 332 | class SkEvent; |
| 333 | |
| 334 | class SkListSource : public SkEventSink { |
| 335 | public: |
| 336 | virtual int countRows() = 0; |
| 337 | virtual void getRow(int index, SkString* left, SkString* right) = 0; |
| 338 | virtual SkEvent* getEvent(int index); |
| 339 | |
| 340 | static SkListSource* CreateFromDir(const char path[], const char suffix[], |
| 341 | const char targetPrefix[]); |
| 342 | static SkListSource* CreateFromDOM(const SkDOM& dom, const SkDOM::Node* node); |
| 343 | }; |
| 344 | |
| 345 | class SkListView : public SkWidgetView { |
| 346 | public: |
| 347 | SkListView(uint32_t flags = 0); |
| 348 | virtual ~SkListView(); |
| 349 | |
| 350 | SkScalar getRowHeight() const { return fRowHeight; } |
| 351 | void setRowHeight(SkScalar); |
| 352 | |
| 353 | /** Return the index of the selected row, or -1 if none |
| 354 | */ |
| 355 | int getSelection() const { return fCurrIndex; } |
| 356 | /** Set the index of the selected row, or -1 for none |
| 357 | */ |
| 358 | void setSelection(int); |
| 359 | |
| 360 | void moveSelectionUp(); |
| 361 | void moveSelectionDown(); |
| 362 | |
| 363 | enum Attr { |
| 364 | kBG_Attr, |
| 365 | kNormalText_Attr, |
| 366 | kHiliteText_Attr, |
| 367 | kHiliteCell_Attr, |
| 368 | kAttrCount |
| 369 | }; |
| 370 | SkPaint& paint(Attr); |
| 371 | |
| 372 | SkListSource* getListSource() const { return fSource; } |
| 373 | SkListSource* setListSource(SkListSource*); |
| 374 | |
| 375 | #if 0 |
| 376 | enum Action { |
| 377 | kSelectionChange_Action, |
| 378 | kSelectionPicked_Action, |
| 379 | kActionCount |
| 380 | }; |
| 381 | /** If event is not null, it is retained by the view, and a copy |
| 382 | of the event will be posted to its listeners when the specified |
| 383 | action occurs. If event is null, then no event will be posted for |
| 384 | the specified action. |
| 385 | */ |
| 386 | void setActionEvent(Action, SkEvent* event); |
| 387 | #endif |
| 388 | |
| 389 | protected: |
| 390 | virtual void onDraw(SkCanvas*); |
| 391 | virtual void onSizeChange(); |
| 392 | virtual bool onEvent(const SkEvent&); |
| 393 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); |
| 394 | |
| 395 | private: |
| 396 | SkPaint fPaint[kAttrCount]; |
| 397 | SkListSource* fSource; |
| 398 | SkScalar fRowHeight; |
| 399 | int fCurrIndex; // logical index |
| 400 | int fScrollIndex; // logical index of top-most visible row |
| 401 | int fVisibleRowCount; |
| 402 | SkString* fStrCache; |
| 403 | |
| 404 | void dirtyStrCache(); |
| 405 | void ensureStrCache(int visibleCount); |
| 406 | |
| 407 | int logicalToVisualIndex(int index) const { return index - fScrollIndex; } |
| 408 | void invalSelection(); |
| 409 | bool getRowRect(int index, SkRect*) const; |
| 410 | void ensureSelectionIsVisible(); |
| 411 | |
| 412 | typedef SkWidgetView INHERITED; |
| 413 | }; |
| 414 | |
| 415 | ////////////////////////////////////////////////////////// |
| 416 | |
| 417 | class SkGridView : public SkWidgetView { |
| 418 | public: |
| 419 | SkGridView(uint32_t flags = 0); |
| 420 | virtual ~SkGridView(); |
| 421 | |
| 422 | void getCellSize(SkPoint*) const; |
| 423 | void setCellSize(SkScalar x, SkScalar y); |
| 424 | |
| 425 | /** Return the index of the selected item, or -1 if none |
| 426 | */ |
| 427 | int getSelection() const { return fCurrIndex; } |
| 428 | /** Set the index of the selected row, or -1 for none |
| 429 | */ |
| 430 | void setSelection(int); |
| 431 | |
| 432 | void moveSelectionUp(); |
| 433 | void moveSelectionDown(); |
| 434 | |
| 435 | enum Attr { |
| 436 | kBG_Attr, |
| 437 | kHiliteCell_Attr, |
| 438 | kAttrCount |
| 439 | }; |
| 440 | SkPaint& paint(Attr); |
| 441 | |
| 442 | SkListSource* getListSource() const { return fSource; } |
| 443 | SkListSource* setListSource(SkListSource*); |
| 444 | |
| 445 | protected: |
| 446 | virtual void onDraw(SkCanvas*); |
| 447 | virtual void onSizeChange(); |
| 448 | virtual bool onEvent(const SkEvent&); |
| 449 | virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); |
| 450 | |
| 451 | private: |
| 452 | SkView* fScrollBar; |
| 453 | SkPaint fPaint[kAttrCount]; |
| 454 | SkListSource* fSource; |
| 455 | int fCurrIndex; // logical index |
| 456 | |
| 457 | SkPoint fCellSize; |
| 458 | SkIPoint fVisibleCount; |
| 459 | |
| 460 | int logicalToVisualIndex(int index) const { return index; } |
| 461 | void invalSelection(); |
| 462 | bool getCellRect(int index, SkRect*) const; |
| 463 | void ensureSelectionIsVisible(); |
| 464 | |
| 465 | typedef SkWidgetView INHERITED; |
| 466 | }; |
| 467 | |
| 468 | #endif |
| 469 | |