1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/views/SkWidget.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,412 @@ 1.4 +/* 1.5 + * Copyright 2006 The Android Open Source Project 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkWidget_DEFINED 1.12 +#define SkWidget_DEFINED 1.13 + 1.14 +#include "SkBitmap.h" 1.15 +#include "SkDOM.h" 1.16 +#include "SkPaint.h" 1.17 +#include "SkString.h" 1.18 +#include "SkTDArray.h" 1.19 +#include "SkTextBox.h" 1.20 +#include "SkView.h" 1.21 + 1.22 +class SkEvent; 1.23 +class SkInterpolator; 1.24 +class SkShader; 1.25 + 1.26 +//////////////////////////////////////////////////////////////////////////////// 1.27 + 1.28 +class SkWidget : public SkView { 1.29 +public: 1.30 + SkWidget(uint32_t flags = 0) : SkView(flags | kFocusable_Mask | kEnabled_Mask) {} 1.31 + 1.32 + /** Call this to post the widget's event to its listeners */ 1.33 + void postWidgetEvent(); 1.34 + 1.35 + static void Init(); 1.36 + static void Term(); 1.37 +protected: 1.38 + // override to add slots to an event before posting 1.39 + virtual void prepareWidgetEvent(SkEvent*); 1.40 + virtual void onEnabledChange(); 1.41 + 1.42 + // <event ...> to initialize the event from XML 1.43 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); 1.44 + 1.45 +private: 1.46 + SkEvent fEvent; 1.47 + typedef SkView INHERITED; 1.48 +}; 1.49 + 1.50 +//////////////////////////////////////////////////////////////////////////////// 1.51 + 1.52 +class SkHasLabelWidget : public SkWidget { 1.53 +public: 1.54 + SkHasLabelWidget(uint32_t flags = 0) : SkWidget(flags) {} 1.55 + 1.56 + size_t getLabel(SkString* label = NULL) const; 1.57 + size_t getLabel(char lable[] = NULL) const; 1.58 + void setLabel(const SkString&); 1.59 + void setLabel(const char label[]); 1.60 + void setLabel(const char label[], size_t len); 1.61 + 1.62 +protected: 1.63 + // called when the label changes 1.64 + virtual void onLabelChange(); 1.65 + 1.66 + // overrides 1.67 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.68 + 1.69 +private: 1.70 + SkString fLabel; 1.71 + typedef SkWidget INHERITED; 1.72 +}; 1.73 + 1.74 +//////////////////////////////////////////////////////////////////////////////// 1.75 + 1.76 +class SkButtonWidget : public SkHasLabelWidget { 1.77 +public: 1.78 + SkButtonWidget(uint32_t flags = 0) : SkHasLabelWidget(flags), fState(kOff_State) {} 1.79 + 1.80 + enum State { 1.81 + kOff_State, //!< XML: buttonState="off" 1.82 + kOn_State, //!< XML: buttonState="on" 1.83 + kUnknown_State //!< XML: buttonState="unknown" 1.84 + }; 1.85 + State getButtonState() const { return fState; } 1.86 + void setButtonState(State); 1.87 + 1.88 +protected: 1.89 + /** called when the label changes. default behavior is to inval the widget */ 1.90 + virtual void onButtonStateChange(); 1.91 + 1.92 + // overrides 1.93 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.94 + 1.95 +private: 1.96 + State fState; 1.97 + typedef SkHasLabelWidget INHERITED; 1.98 +}; 1.99 + 1.100 +//////////////////////////////////////////////////////////////////////////////// 1.101 + 1.102 +class SkPushButtonWidget : public SkButtonWidget { 1.103 +public: 1.104 + SkPushButtonWidget(uint32_t flags = 0) : SkButtonWidget(flags) {} 1.105 + 1.106 +protected: 1.107 + virtual bool onEvent(const SkEvent&); 1.108 + virtual void onDraw(SkCanvas*); 1.109 + virtual Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE; 1.110 + virtual bool onClick(Click* click); 1.111 + 1.112 +private: 1.113 + typedef SkButtonWidget INHERITED; 1.114 +}; 1.115 + 1.116 +//////////////////////////////////////////////////////////////////////////////// 1.117 + 1.118 +class SkCheckBoxWidget : public SkButtonWidget { 1.119 +public: 1.120 + SkCheckBoxWidget(uint32_t flags = 0); 1.121 + 1.122 +protected: 1.123 + virtual bool onEvent(const SkEvent&); 1.124 + virtual void onDraw(SkCanvas*); 1.125 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.126 + 1.127 +private: 1.128 + typedef SkButtonWidget INHERITED; 1.129 +}; 1.130 + 1.131 +//////////////////////////////////////////////////////////////////////////////// 1.132 + 1.133 +class SkStaticTextView : public SkView { 1.134 +public: 1.135 + SkStaticTextView(uint32_t flags = 0); 1.136 + virtual ~SkStaticTextView(); 1.137 + 1.138 + enum Mode { 1.139 + kFixedSize_Mode, 1.140 + kAutoWidth_Mode, 1.141 + kAutoHeight_Mode, 1.142 + 1.143 + kModeCount 1.144 + }; 1.145 + Mode getMode() const { return (Mode)fMode; } 1.146 + void setMode(Mode); 1.147 + 1.148 + SkTextBox::SpacingAlign getSpacingAlign() const { return (SkTextBox::SpacingAlign)fSpacingAlign; } 1.149 + void setSpacingAlign(SkTextBox::SpacingAlign); 1.150 + 1.151 + void getMargin(SkPoint* margin) const; 1.152 + void setMargin(SkScalar dx, SkScalar dy); 1.153 + 1.154 + size_t getText(SkString* text = NULL) const; 1.155 + size_t getText(char text[] = NULL) const; 1.156 + void setText(const SkString&); 1.157 + void setText(const char text[]); 1.158 + void setText(const char text[], size_t len); 1.159 + 1.160 + void getPaint(SkPaint*) const; 1.161 + void setPaint(const SkPaint&); 1.162 + 1.163 +protected: 1.164 + // overrides 1.165 + virtual void onDraw(SkCanvas*); 1.166 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.167 + 1.168 +private: 1.169 + SkPoint fMargin; 1.170 + SkString fText; 1.171 + SkPaint fPaint; 1.172 + uint8_t fMode; 1.173 + uint8_t fSpacingAlign; 1.174 + 1.175 + void computeSize(); 1.176 + 1.177 + typedef SkView INHERITED; 1.178 +}; 1.179 + 1.180 +//////////////////////////////////////////////////////////////////////////////// 1.181 + 1.182 +class SkBitmapView : public SkView { 1.183 +public: 1.184 + SkBitmapView(uint32_t flags = 0); 1.185 + virtual ~SkBitmapView(); 1.186 + 1.187 + bool getBitmap(SkBitmap*) const; 1.188 + void setBitmap(const SkBitmap*, bool viewOwnsPixels); 1.189 + bool loadBitmapFromFile(const char path[]); 1.190 + 1.191 +protected: 1.192 + virtual void onDraw(SkCanvas*); 1.193 + virtual void onInflate(const SkDOM&, const SkDOM::Node*); 1.194 + 1.195 +private: 1.196 + SkBitmap fBitmap; 1.197 + typedef SkView INHERITED; 1.198 +}; 1.199 + 1.200 +//////////////////////////////////////////////////////////////////////////////// 1.201 + 1.202 +class SkHasLabelView : public SkView { 1.203 +public: 1.204 + void getLabel(SkString*) const; 1.205 + void setLabel(const SkString&); 1.206 + void setLabel(const char label[]); 1.207 + 1.208 +protected: 1.209 + SkString fLabel; 1.210 + 1.211 + // called when the label changes 1.212 + virtual void onLabelChange(); 1.213 + 1.214 + // overrides 1.215 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.216 +}; 1.217 + 1.218 +//////////////////////////////////////////////////////////////////////////////// 1.219 + 1.220 +class SkPushButtonView : public SkHasLabelView { 1.221 +public: 1.222 + SkPushButtonView(uint32_t flags = 0); 1.223 + 1.224 +protected: 1.225 + virtual void onDraw(SkCanvas*); 1.226 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.227 +}; 1.228 + 1.229 +//////////////////////////////////////////////////////////////////////////////// 1.230 + 1.231 +class SkCheckBoxView : public SkHasLabelView { 1.232 +public: 1.233 + SkCheckBoxView(uint32_t flags = 0); 1.234 + 1.235 + enum State { 1.236 + kOff_State, 1.237 + kOn_State, 1.238 + kMaybe_State 1.239 + }; 1.240 + State getState() const { return fState; } 1.241 + void setState(State); 1.242 + 1.243 +protected: 1.244 + virtual void onDraw(SkCanvas*); 1.245 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.246 + 1.247 +private: 1.248 + State fState; 1.249 +}; 1.250 + 1.251 +//////////////////////////////////////////////////////////////////////////////// 1.252 + 1.253 +class SkProgressView : public SkView { 1.254 +public: 1.255 + SkProgressView(uint32_t flags = 0); 1.256 + virtual ~SkProgressView(); 1.257 + 1.258 + uint16_t getValue() const { return fValue; } 1.259 + uint16_t getMax() const { return fMax; } 1.260 + 1.261 + void setMax(U16CPU max); 1.262 + void setValue(U16CPU value); 1.263 + 1.264 +protected: 1.265 + virtual void onDraw(SkCanvas*); 1.266 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); 1.267 + 1.268 +private: 1.269 + uint16_t fValue, fMax; 1.270 + SkShader* fOnShader, *fOffShader; 1.271 + SkInterpolator* fInterp; 1.272 + bool fDoInterp; 1.273 + 1.274 + typedef SkView INHERITED; 1.275 +}; 1.276 + 1.277 +//////////////////////////////////////////////////////////////////////////////// 1.278 + 1.279 +class SkListSource : public SkEventSink { 1.280 +public: 1.281 + virtual int countRows() = 0; 1.282 + virtual void getRow(int index, SkString* left, SkString* right) = 0; 1.283 + virtual SkEvent* getEvent(int index); 1.284 + 1.285 + static SkListSource* CreateFromDir(const char path[], const char suffix[], 1.286 + const char targetPrefix[]); 1.287 + static SkListSource* CreateFromDOM(const SkDOM& dom, const SkDOM::Node* node); 1.288 +}; 1.289 + 1.290 +//////////////////////////////////////////////////////////////////////////////// 1.291 + 1.292 +class SkListView : public SkView { 1.293 +public: 1.294 + SkListView(uint32_t flags = 0); 1.295 + virtual ~SkListView(); 1.296 + 1.297 + SkScalar getRowHeight() const { return fRowHeight; } 1.298 + void setRowHeight(SkScalar); 1.299 + 1.300 + /** Return the index of the selected row, or -1 if none 1.301 + */ 1.302 + int getSelection() const { return fCurrIndex; } 1.303 + /** Set the index of the selected row, or -1 for none 1.304 + */ 1.305 + void setSelection(int); 1.306 + 1.307 + void moveSelectionUp(); 1.308 + void moveSelectionDown(); 1.309 + 1.310 + enum Attr { 1.311 + kBG_Attr, 1.312 + kNormalText_Attr, 1.313 + kHiliteText_Attr, 1.314 + kHiliteCell_Attr, 1.315 + kAttrCount 1.316 + }; 1.317 + SkPaint& paint(Attr); 1.318 + 1.319 + SkListSource* getListSource() const { return fSource; } 1.320 + SkListSource* setListSource(SkListSource*); 1.321 + 1.322 +#if 0 1.323 + enum Action { 1.324 + kSelectionChange_Action, 1.325 + kSelectionPicked_Action, 1.326 + kActionCount 1.327 + }; 1.328 + /** If event is not null, it is retained by the view, and a copy 1.329 + of the event will be posted to its listeners when the specified 1.330 + action occurs. If event is null, then no event will be posted for 1.331 + the specified action. 1.332 + */ 1.333 + void setActionEvent(Action, SkEvent* event); 1.334 +#endif 1.335 + 1.336 +protected: 1.337 + virtual void onDraw(SkCanvas*); 1.338 + virtual void onSizeChange(); 1.339 + virtual bool onEvent(const SkEvent&); 1.340 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); 1.341 + 1.342 +private: 1.343 + SkPaint fPaint[kAttrCount]; 1.344 + SkListSource* fSource; 1.345 + SkScalar fRowHeight; 1.346 + int fCurrIndex; // logical index 1.347 + int fScrollIndex; // logical index of top-most visible row 1.348 + int fVisibleRowCount; 1.349 + SkString* fStrCache; 1.350 + 1.351 + void dirtyStrCache(); 1.352 + void ensureStrCache(int visibleCount); 1.353 + 1.354 + int logicalToVisualIndex(int index) const { return index - fScrollIndex; } 1.355 + void invalSelection(); 1.356 + bool getRowRect(int index, SkRect*) const; 1.357 + void ensureSelectionIsVisible(); 1.358 + 1.359 + typedef SkView INHERITED; 1.360 +}; 1.361 + 1.362 +//////////////////////////////////////////////////////////////////////////////// 1.363 + 1.364 +class SkGridView : public SkView { 1.365 +public: 1.366 + SkGridView(uint32_t flags = 0); 1.367 + virtual ~SkGridView(); 1.368 + 1.369 + void getCellSize(SkPoint*) const; 1.370 + void setCellSize(SkScalar x, SkScalar y); 1.371 + 1.372 + /** Return the index of the selected item, or -1 if none 1.373 + */ 1.374 + int getSelection() const { return fCurrIndex; } 1.375 + /** Set the index of the selected row, or -1 for none 1.376 + */ 1.377 + void setSelection(int); 1.378 + 1.379 + void moveSelectionUp(); 1.380 + void moveSelectionDown(); 1.381 + 1.382 + enum Attr { 1.383 + kBG_Attr, 1.384 + kHiliteCell_Attr, 1.385 + kAttrCount 1.386 + }; 1.387 + SkPaint& paint(Attr); 1.388 + 1.389 + SkListSource* getListSource() const { return fSource; } 1.390 + SkListSource* setListSource(SkListSource*); 1.391 + 1.392 +protected: 1.393 + virtual void onDraw(SkCanvas*); 1.394 + virtual void onSizeChange(); 1.395 + virtual bool onEvent(const SkEvent&); 1.396 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); 1.397 + 1.398 +private: 1.399 + SkView* fScrollBar; 1.400 + SkPaint fPaint[kAttrCount]; 1.401 + SkListSource* fSource; 1.402 + int fCurrIndex; // logical index 1.403 + 1.404 + SkPoint fCellSize; 1.405 + SkIPoint fVisibleCount; 1.406 + 1.407 + int logicalToVisualIndex(int index) const { return index; } 1.408 + void invalSelection(); 1.409 + bool getCellRect(int index, SkRect*) const; 1.410 + void ensureSelectionIsVisible(); 1.411 + 1.412 + typedef SkView INHERITED; 1.413 +}; 1.414 + 1.415 +#endif