1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/views/animated/SkWidgetViews.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,309 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#ifndef SkWidgetViews_DEFINED 1.14 +#define SkWidgetViews_DEFINED 1.15 + 1.16 +#include "SkView.h" 1.17 + 1.18 + 1.19 +enum SkWidgetEnum { 1.20 + kBorder_WidgetEnum, //!< <sk-border> 1.21 + kButton_WidgetEnum, //!< <sk-button> 1.22 + kImage_WidgetEnum, //!< <sk-image> 1.23 + kList_WidgetEnum, //!< <sk-list> 1.24 + kProgress_WidgetEnum, //!< <sk-progress> 1.25 + kScroll_WidgetEnum, //!< <sk-scroll> 1.26 + kText_WidgetEnum, //!< <sk-text> 1.27 + 1.28 + kWidgetEnumCount 1.29 +}; 1.30 + 1.31 +//determines which skin to use 1.32 +enum SkinEnum { 1.33 + kBorder_SkinEnum, 1.34 + kButton_SkinEnum, 1.35 + kProgress_SkinEnum, 1.36 + kScroll_SkinEnum, 1.37 + kStaticText_SkinEnum, 1.38 + 1.39 + kSkinEnumCount 1.40 +}; 1.41 + 1.42 +#include "SkAnimator.h" 1.43 +//used for inflates 1.44 +const char* get_skin_enum_path(SkinEnum se); 1.45 +void init_skin_anim(const char path[], SkAnimator* anim); 1.46 +void init_skin_anim(SkinEnum se, SkAnimator* anim); 1.47 +void init_skin_paint(SkinEnum se, SkPaint* paint); 1.48 +void inflate_paint(const SkDOM& dom, const SkDOM::Node* node, SkPaint* paint); 1.49 + 1.50 +/** Given an enum value, return an instance of the specified widget. 1.51 + If the enum is out of range, returns null 1.52 +*/ 1.53 +SkView* SkWidgetFactory(SkWidgetEnum); 1.54 +/** Given the inflate/element name of a widget, return an instance of 1.55 + the specified widget, or null if name does not match any known 1.56 + widget type. 1.57 +*/ 1.58 +SkView* SkWidgetFactory(const char name[]); 1.59 + 1.60 +//////////////////////////////////////////////////////////////////////////////////////////////// 1.61 + 1.62 +class SkWidgetView : public SkView { 1.63 +public: 1.64 + SkWidgetView(); 1.65 + 1.66 + const char* getLabel() const; 1.67 + void getLabel(SkString* label) const; 1.68 + 1.69 + void setLabel(const char[]); 1.70 + void setLabel(const char[], size_t len); 1.71 + void setLabel(const SkString&); 1.72 + 1.73 + SkEvent& event() { return fEvent; } 1.74 + const SkEvent& event() const { return fEvent; } 1.75 + 1.76 + /** Returns true if the widget can post its event to its listeners. 1.77 + */ 1.78 + bool postWidgetEvent(); 1.79 + 1.80 + /** Returns the sinkID of the widgetview that posted the event, or 0 1.81 + */ 1.82 + static SkEventSinkID GetWidgetEventSinkID(const SkEvent&); 1.83 + 1.84 +protected: 1.85 + /** called when the label changes. override in subclasses. default action invals the view's bounds. 1.86 + called with the old and new labels, before the label has actually changed. 1.87 + */ 1.88 + virtual void onLabelChange(const char oldLabel[], const char newLabel[]); 1.89 + /** called before posting the event to our listeners. Override to add slots to the event 1.90 + before posting. Return true to proceed with posting, or false to not post the event to any 1.91 + listener. Note: the event passed in may not be the same as calling this->event(). 1.92 + Be sure to call your INHERITED method as well, so that all classes in the hierarchy get a shot 1.93 + at modifying the event (and possibly returning false to abort). 1.94 + */ 1.95 + virtual bool onPrepareWidgetEvent(SkEvent* evt); 1.96 + 1.97 + // overrides 1.98 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.99 + 1.100 +private: 1.101 + SkString fLabel; 1.102 + SkEvent fEvent; 1.103 + 1.104 + typedef SkView INHERITED; 1.105 +}; 1.106 + 1.107 +//////////////////////////////////////////////////////////////////////////////////////////////// 1.108 + 1.109 +class SkButtonView : public SkWidgetView { 1.110 +public: 1.111 + // inflate: "sk-button" 1.112 + 1.113 +protected: 1.114 + // overrides 1.115 + virtual bool onEvent(const SkEvent&); 1.116 +private: 1.117 + typedef SkWidgetView INHERITED; 1.118 +}; 1.119 + 1.120 +//////////////////////////////////////////////////////////////////////////////////////////////// 1.121 + 1.122 +class SkCheckButtonView : public SkWidgetView { 1.123 +public: 1.124 + SkCheckButtonView(); 1.125 + 1.126 + // inflate: "sk-checkbutton" 1.127 + 1.128 + enum CheckState { 1.129 + kOff_CheckState, //!< inflate: check-state="off" 1.130 + kOn_CheckState, //!< inflate: check-state="on" 1.131 + kUnknown_CheckState //!< inflate: check-state="unknown" 1.132 + }; 1.133 + CheckState getCheckState() const { return (CheckState)fCheckState; } 1.134 + void setCheckState(CheckState); 1.135 + 1.136 + /** use this to extract the CheckState from an event (i.e. one that as posted 1.137 + by a SkCheckButtonView). Returns true if the proper slot was present in the event, 1.138 + and sets state to that value. If no proper slot is found, returns false and does not 1.139 + modify state. 1.140 + */ 1.141 + static bool GetWidgetEventCheckState(const SkEvent&, CheckState* state); 1.142 + 1.143 +protected: 1.144 + // called when the check-state is about to change, but before it actually has 1.145 + virtual void onCheckStateChange(CheckState oldState, CheckState newState); 1.146 + 1.147 + // overrides 1.148 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.149 + virtual bool onPrepareWidgetEvent(SkEvent* evt); 1.150 + 1.151 +private: 1.152 + uint8_t fCheckState; 1.153 + 1.154 + typedef SkWidgetView INHERITED; 1.155 +}; 1.156 + 1.157 +//////////////////////////////////////////////////////////////////////////////////////////////// 1.158 +#include "SkTextBox.h" 1.159 + 1.160 +class SkStaticTextView : public SkView { 1.161 +public: 1.162 + SkStaticTextView(); 1.163 + virtual ~SkStaticTextView(); 1.164 + 1.165 + enum Mode { 1.166 + kFixedSize_Mode, 1.167 + kAutoWidth_Mode, 1.168 + kAutoHeight_Mode, 1.169 + 1.170 + kModeCount 1.171 + }; 1.172 + Mode getMode() const { return (Mode)fMode; } 1.173 + void setMode(Mode); 1.174 + 1.175 + SkTextBox::SpacingAlign getSpacingAlign() const { return (SkTextBox::SpacingAlign)fSpacingAlign; } 1.176 + void setSpacingAlign(SkTextBox::SpacingAlign); 1.177 + 1.178 + void getMargin(SkPoint* margin) const; 1.179 + void setMargin(SkScalar dx, SkScalar dy); 1.180 + 1.181 + size_t getText(SkString* text = NULL) const; 1.182 + size_t getText(char text[] = NULL) const; 1.183 + void setText(const SkString&); 1.184 + void setText(const char text[]); 1.185 + void setText(const char text[], size_t len); 1.186 + 1.187 + void getPaint(SkPaint*) const; 1.188 + void setPaint(const SkPaint&); 1.189 + 1.190 +protected: 1.191 + // overrides 1.192 + virtual void onDraw(SkCanvas*); 1.193 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 1.194 + 1.195 +private: 1.196 + SkPoint fMargin; 1.197 + SkString fText; 1.198 + SkPaint fPaint; 1.199 + uint8_t fMode; 1.200 + uint8_t fSpacingAlign; 1.201 + 1.202 + void computeSize(); 1.203 + 1.204 + typedef SkView INHERITED; 1.205 +}; 1.206 + 1.207 +//////////////////////////////////////////////////////////////////////////////////////////////// 1.208 + 1.209 +class SkAnimator; 1.210 +class SkListSource; 1.211 +class SkScrollBarView; 1.212 + 1.213 +class SkListView : public SkWidgetView { 1.214 +public: 1.215 + SkListView(); 1.216 + virtual ~SkListView(); 1.217 + 1.218 + bool hasScrollBar() const { return fScrollBar != NULL; } 1.219 + void setHasScrollBar(bool); 1.220 + 1.221 + /** Return the number of visible rows 1.222 + */ 1.223 + int getVisibleRowCount() const { return fVisibleRowCount; } 1.224 + /** Return the index of the selected row, or -1 if none 1.225 + */ 1.226 + int getSelection() const { return fCurrIndex; } 1.227 + /** Set the index of the selected row, or -1 for none 1.228 + */ 1.229 + void setSelection(int); 1.230 + /** If possible, move the selection up and return true, 1.231 + else do nothing and return false 1.232 + If nothing is selected, select the last item (unless there are no items). 1.233 + */ 1.234 + bool moveSelectionUp(); 1.235 + /** If possible, move the selection down and return true, 1.236 + else do nothing and return false. 1.237 + If nothing is selected, select the first item (unless there are no items). 1.238 + */ 1.239 + bool moveSelectionDown(); 1.240 + 1.241 + SkListSource* getListSource() const { return fSource; } 1.242 + SkListSource* setListSource(SkListSource*); 1.243 + 1.244 + /** Call this in your event handler. If the specified event is from a SkListView, 1.245 + then it returns the index of the selected item in this list, otherwise it 1.246 + returns -1 1.247 + */ 1.248 + static int GetWidgetEventListIndex(const SkEvent&); 1.249 + 1.250 +protected: 1.251 + // overrides 1.252 + virtual void onDraw(SkCanvas*); 1.253 + virtual void onSizeChange(); 1.254 + virtual bool onEvent(const SkEvent&); 1.255 + virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); 1.256 + virtual bool onPrepareWidgetEvent(SkEvent*); 1.257 + 1.258 +private: 1.259 + enum DirtyFlags { 1.260 + kAnimCount_DirtyFlag = 0x01, 1.261 + kAnimContent_DirtyFlag = 0x02 1.262 + }; 1.263 + void dirtyCache(unsigned dirtyFlags); 1.264 + bool ensureCache(); 1.265 + 1.266 + int logicalToVisualIndex(int index) const { return index - fScrollIndex; } 1.267 + void invalSelection(); 1.268 + SkScalar getContentWidth() const; 1.269 + bool getRowRect(int index, SkRect*) const; 1.270 + void ensureSelectionIsVisible(); 1.271 + void ensureVisibleRowCount(); 1.272 + 1.273 + struct BindingRec; 1.274 + 1.275 + enum Heights { 1.276 + kNormal_Height, 1.277 + kSelected_Height 1.278 + }; 1.279 + SkListSource* fSource; 1.280 + SkScrollBarView* fScrollBar; 1.281 + SkAnimator* fAnims; 1.282 + BindingRec* fBindings; 1.283 + SkString fSkinName; 1.284 + SkScalar fHeights[2]; 1.285 + int16_t fScrollIndex, fCurrIndex; 1.286 + uint16_t fVisibleRowCount, fBindingCount; 1.287 + SkBool8 fAnimContentDirty; 1.288 + SkBool8 fAnimFocusDirty; 1.289 + 1.290 + typedef SkWidgetView INHERITED; 1.291 +}; 1.292 + 1.293 +class SkListSource : public SkRefCnt { 1.294 +public: 1.295 + SK_DECLARE_INST_COUNT(SkListSource) 1.296 + 1.297 + virtual int countFields(); 1.298 + virtual void getFieldName(int index, SkString* field); 1.299 + /** Return the index of the named field, or -1 if not found */ 1.300 + virtual int findFieldIndex(const char field[]); 1.301 + 1.302 + virtual int countRecords(); 1.303 + virtual void getRecord(int rowIndex, int fieldIndex, SkString* data); 1.304 + 1.305 + virtual bool prepareWidgetEvent(SkEvent*, int rowIndex); 1.306 + 1.307 + static SkListSource* Factory(const char name[]); 1.308 +private: 1.309 + typedef SkRefCnt INHERITED; 1.310 +}; 1.311 + 1.312 +#endif