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