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