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 SkWidgetViews_DEFINED 18 #define SkWidgetViews_DEFINED 19 20 #include "SkView.h" 21 22 23 enum SkWidgetEnum { 24 kBorder_WidgetEnum, //!< <sk-border> 25 kButton_WidgetEnum, //!< <sk-button> 26 kImage_WidgetEnum, //!< <sk-image> 27 kList_WidgetEnum, //!< <sk-list> 28 kProgress_WidgetEnum, //!< <sk-progress> 29 kScroll_WidgetEnum, //!< <sk-scroll> 30 kText_WidgetEnum, //!< <sk-text> 31 32 kWidgetEnumCount 33 }; 34 35 //determines which skin to use 36 enum SkinEnum { 37 kBorder_SkinEnum, 38 kButton_SkinEnum, 39 kProgress_SkinEnum, 40 kScroll_SkinEnum, 41 kStaticText_SkinEnum, 42 43 kSkinEnumCount 44 }; 45 46 #include "SkAnimator.h" 47 //used for inflates 48 const char* get_skin_enum_path(SkinEnum se); 49 void init_skin_anim(const char path[], SkAnimator* anim); 50 void init_skin_anim(SkinEnum se, SkAnimator* anim); 51 void init_skin_paint(SkinEnum se, SkPaint* paint); 52 void inflate_paint(const SkDOM& dom, const SkDOM::Node* node, SkPaint* paint); 53 54 /** Given an enum value, return an instance of the specified widget. 55 If the enum is out of range, returns null 56 */ 57 SkView* SkWidgetFactory(SkWidgetEnum); 58 /** Given the inflate/element name of a widget, return an instance of 59 the specified widget, or null if name does not match any known 60 widget type. 61 */ 62 SkView* SkWidgetFactory(const char name[]); 63 64 //////////////////////////////////////////////////////////////////////////////////////////////// 65 66 class SkWidgetView : public SkView { 67 public: 68 SkWidgetView(); 69 70 const char* getLabel() const; 71 void getLabel(SkString* label) const; 72 73 void setLabel(const char[]); 74 void setLabel(const char[], size_t len); 75 void setLabel(const SkString&); 76 77 SkEvent& event() { return fEvent; } 78 const SkEvent& event() const { return fEvent; } 79 80 /** Returns true if the widget can post its event to its listeners. 81 */ 82 bool postWidgetEvent(); 83 84 /** Returns the sinkID of the widgetview that posted the event, or 0 85 */ 86 static SkEventSinkID GetWidgetEventSinkID(const SkEvent&); 87 88 protected: 89 /** called when the label changes. override in subclasses. default action invals the view's bounds. 90 called with the old and new labels, before the label has actually changed. 91 */ 92 virtual void onLabelChange(const char oldLabel[], const char newLabel[]); 93 /** called before posting the event to our listeners. Override to add slots to the event 94 before posting. Return true to proceed with posting, or false to not post the event to any 95 listener. Note: the event passed in may not be the same as calling this->event(). 96 Be sure to call your INHERITED method as well, so that all classes in the hierarchy get a shot 97 at modifying the event (and possibly returning false to abort). 98 */ 99 virtual bool onPrepareWidgetEvent(SkEvent* evt); 100 101 // overrides 102 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 103 104 private: 105 SkString fLabel; 106 SkEvent fEvent; 107 108 typedef SkView INHERITED; 109 }; 110 111 //////////////////////////////////////////////////////////////////////////////////////////////// 112 113 class SkButtonView : public SkWidgetView { 114 public: 115 // inflate: "sk-button" 116 117 protected: 118 // overrides 119 virtual bool onEvent(const SkEvent&); 120 }; 121 122 //////////////////////////////////////////////////////////////////////////////////////////////// 123 124 class SkCheckButtonView : public SkWidgetView { 125 public: 126 SkCheckButtonView(); 127 128 // inflate: "sk-checkbutton" 129 130 enum CheckState { 131 kOff_CheckState, //!< inflate: check-state="off" 132 kOn_CheckState, //!< inflate: check-state="on" 133 kUnknown_CheckState //!< inflate: check-state="unknown" 134 }; 135 CheckState getCheckState() const { return (CheckState)fCheckState; } 136 void setCheckState(CheckState); 137 138 /** use this to extract the CheckState from an event (i.e. one that as posted 139 by a SkCheckButtonView). Returns true if the proper slot was present in the event, 140 and sets state to that value. If no proper slot is found, returns false and does not 141 modify state. 142 */ 143 static bool GetWidgetEventCheckState(const SkEvent&, CheckState* state); 144 145 protected: 146 // called when the check-state is about to change, but before it actually has 147 virtual void onCheckStateChange(CheckState oldState, CheckState newState); 148 149 // overrides 150 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 151 virtual bool onPrepareWidgetEvent(SkEvent* evt); 152 153 private: 154 uint8_t fCheckState; 155 156 typedef SkWidgetView INHERITED; 157 }; 158 159 //////////////////////////////////////////////////////////////////////////////////////////////// 160 #include "SkTextBox.h" 161 162 class SkStaticTextView : public SkView { 163 public: 164 SkStaticTextView(); 165 virtual ~SkStaticTextView(); 166 167 enum Mode { 168 kFixedSize_Mode, 169 kAutoWidth_Mode, 170 kAutoHeight_Mode, 171 172 kModeCount 173 }; 174 Mode getMode() const { return (Mode)fMode; } 175 void setMode(Mode); 176 177 SkTextBox::SpacingAlign getSpacingAlign() const { return (SkTextBox::SpacingAlign)fSpacingAlign; } 178 void setSpacingAlign(SkTextBox::SpacingAlign); 179 180 void getMargin(SkPoint* margin) const; 181 void setMargin(SkScalar dx, SkScalar dy); 182 183 size_t getText(SkString* text = NULL) const; 184 size_t getText(char text[] = NULL) const; 185 void setText(const SkString&); 186 void setText(const char text[]); 187 void setText(const char text[], size_t len); 188 189 void getPaint(SkPaint*) const; 190 void setPaint(const SkPaint&); 191 192 protected: 193 // overrides 194 virtual void onDraw(SkCanvas*); 195 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*); 196 197 private: 198 SkPoint fMargin; 199 SkString fText; 200 SkPaint fPaint; 201 uint8_t fMode; 202 uint8_t fSpacingAlign; 203 204 void computeSize(); 205 206 typedef SkView INHERITED; 207 }; 208 209 //////////////////////////////////////////////////////////////////////////////////////////////// 210 211 class SkAnimator; 212 class SkListSource; 213 class SkScrollBarView; 214 215 class SkListView : public SkWidgetView { 216 public: 217 SkListView(); 218 virtual ~SkListView(); 219 220 bool hasScrollBar() const { return fScrollBar != NULL; } 221 void setHasScrollBar(bool); 222 223 /** Return the number of visible rows 224 */ 225 int getVisibleRowCount() const { return fVisibleRowCount; } 226 /** Return the index of the selected row, or -1 if none 227 */ 228 int getSelection() const { return fCurrIndex; } 229 /** Set the index of the selected row, or -1 for none 230 */ 231 void setSelection(int); 232 /** If possible, move the selection up and return true, 233 else do nothing and return false 234 If nothing is selected, select the last item (unless there are no items). 235 */ 236 bool moveSelectionUp(); 237 /** If possible, move the selection down and return true, 238 else do nothing and return false. 239 If nothing is selected, select the first item (unless there are no items). 240 */ 241 bool moveSelectionDown(); 242 243 SkListSource* getListSource() const { return fSource; } 244 SkListSource* setListSource(SkListSource*); 245 246 /** Call this in your event handler. If the specified event is from a SkListView, 247 then it returns the index of the selected item in this list, otherwise it 248 returns -1 249 */ 250 static int GetWidgetEventListIndex(const SkEvent&); 251 252 protected: 253 // overrides 254 virtual void onDraw(SkCanvas*); 255 virtual void onSizeChange(); 256 virtual bool onEvent(const SkEvent&); 257 virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node); 258 virtual bool onPrepareWidgetEvent(SkEvent*); 259 260 private: 261 enum DirtyFlags { 262 kAnimCount_DirtyFlag = 0x01, 263 kAnimContent_DirtyFlag = 0x02 264 }; 265 void dirtyCache(unsigned dirtyFlags); 266 bool ensureCache(); 267 268 int logicalToVisualIndex(int index) const { return index - fScrollIndex; } 269 void invalSelection(); 270 SkScalar getContentWidth() const; 271 bool getRowRect(int index, SkRect*) const; 272 void ensureSelectionIsVisible(); 273 void ensureVisibleRowCount(); 274 275 struct BindingRec; 276 277 enum Heights { 278 kNormal_Height, 279 kSelected_Height 280 }; 281 SkListSource* fSource; 282 SkScrollBarView* fScrollBar; 283 SkAnimator* fAnims; 284 BindingRec* fBindings; 285 SkString fSkinName; 286 SkScalar fHeights[2]; 287 int16_t fScrollIndex, fCurrIndex; 288 uint16_t fVisibleRowCount, fBindingCount; 289 SkBool8 fAnimContentDirty; 290 SkBool8 fAnimFocusDirty; 291 292 typedef SkWidgetView INHERITED; 293 }; 294 295 class SkListSource : public SkRefCnt { 296 public: 297 virtual int countFields(); 298 virtual void getFieldName(int index, SkString* field); 299 /** Return the index of the named field, or -1 if not found */ 300 virtual int findFieldIndex(const char field[]); 301 302 virtual int countRecords(); 303 virtual void getRecord(int rowIndex, int fieldIndex, SkString* data); 304 305 virtual bool prepareWidgetEvent(SkEvent*, int rowIndex); 306 307 static SkListSource* Factory(const char name[]); 308 }; 309 310 #endif 311