Home | History | Annotate | Download | only in views
      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 private:
    121     typedef SkWidgetView INHERITED;
    122 };
    123 
    124 ////////////////////////////////////////////////////////////////////////////////////////////////
    125 
    126 class SkCheckButtonView : public SkWidgetView {
    127 public:
    128     SkCheckButtonView();
    129 
    130     // inflate: "sk-checkbutton"
    131 
    132     enum CheckState {
    133         kOff_CheckState,        //!< inflate: check-state="off"
    134         kOn_CheckState,         //!< inflate: check-state="on"
    135         kUnknown_CheckState     //!< inflate: check-state="unknown"
    136     };
    137     CheckState  getCheckState() const { return (CheckState)fCheckState; }
    138     void        setCheckState(CheckState);
    139 
    140     /** use this to extract the CheckState from an event (i.e. one that as posted
    141         by a SkCheckButtonView). Returns true if the proper slot was present in the event,
    142         and sets state to that value. If no proper slot is found, returns false and does not
    143         modify state.
    144     */
    145     static bool GetWidgetEventCheckState(const SkEvent&, CheckState* state);
    146 
    147 protected:
    148     // called when the check-state is about to change, but before it actually has
    149     virtual void onCheckStateChange(CheckState oldState, CheckState newState);
    150 
    151     // overrides
    152     virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
    153     virtual bool onPrepareWidgetEvent(SkEvent* evt);
    154 
    155 private:
    156     uint8_t  fCheckState;
    157 
    158     typedef SkWidgetView INHERITED;
    159 };
    160 
    161 ////////////////////////////////////////////////////////////////////////////////////////////////
    162 #include "SkTextBox.h"
    163 
    164 class SkStaticTextView : public SkView {
    165 public:
    166             SkStaticTextView();
    167     virtual ~SkStaticTextView();
    168 
    169     enum Mode {
    170         kFixedSize_Mode,
    171         kAutoWidth_Mode,
    172         kAutoHeight_Mode,
    173 
    174         kModeCount
    175     };
    176     Mode    getMode() const { return (Mode)fMode; }
    177     void    setMode(Mode);
    178 
    179     SkTextBox::SpacingAlign getSpacingAlign() const { return (SkTextBox::SpacingAlign)fSpacingAlign; }
    180     void    setSpacingAlign(SkTextBox::SpacingAlign);
    181 
    182     void    getMargin(SkPoint* margin) const;
    183     void    setMargin(SkScalar dx, SkScalar dy);
    184 
    185     size_t  getText(SkString* text = NULL) const;
    186     size_t  getText(char text[] = NULL) const;
    187     void    setText(const SkString&);
    188     void    setText(const char text[]);
    189     void    setText(const char text[], size_t len);
    190 
    191     void    getPaint(SkPaint*) const;
    192     void    setPaint(const SkPaint&);
    193 
    194 protected:
    195     // overrides
    196     virtual void onDraw(SkCanvas*);
    197     virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
    198 
    199 private:
    200     SkPoint     fMargin;
    201     SkString    fText;
    202     SkPaint     fPaint;
    203     uint8_t     fMode;
    204     uint8_t     fSpacingAlign;
    205 
    206     void computeSize();
    207 
    208     typedef SkView INHERITED;
    209 };
    210 
    211 ////////////////////////////////////////////////////////////////////////////////////////////////
    212 
    213 class SkAnimator;
    214 class SkListSource;
    215 class SkScrollBarView;
    216 
    217 class SkListView : public SkWidgetView {
    218 public:
    219             SkListView();
    220     virtual ~SkListView();
    221 
    222     bool    hasScrollBar() const { return fScrollBar != NULL; }
    223     void    setHasScrollBar(bool);
    224 
    225     /** Return the number of visible rows
    226     */
    227     int     getVisibleRowCount() const { return fVisibleRowCount; }
    228     /** Return the index of the selected row, or -1 if none
    229     */
    230     int     getSelection() const { return fCurrIndex; }
    231     /** Set the index of the selected row, or -1 for none
    232     */
    233     void    setSelection(int);
    234     /** If possible, move the selection up and return true,
    235         else do nothing and return false
    236         If nothing is selected, select the last item (unless there are no items).
    237     */
    238     bool    moveSelectionUp();
    239     /** If possible, move the selection down and return true,
    240         else do nothing and return false.
    241         If nothing is selected, select the first item (unless there are no items).
    242     */
    243     bool    moveSelectionDown();
    244 
    245     SkListSource*   getListSource() const { return fSource; }
    246     SkListSource*   setListSource(SkListSource*);
    247 
    248     /** Call this in your event handler. If the specified event is from a SkListView,
    249         then it returns the index of the selected item in this list, otherwise it
    250         returns -1
    251     */
    252     static int GetWidgetEventListIndex(const SkEvent&);
    253 
    254 protected:
    255     // overrides
    256     virtual void onDraw(SkCanvas*);
    257     virtual void onSizeChange();
    258     virtual bool onEvent(const SkEvent&);
    259     virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node);
    260     virtual bool onPrepareWidgetEvent(SkEvent*);
    261 
    262 private:
    263     enum DirtyFlags {
    264         kAnimCount_DirtyFlag    = 0x01,
    265         kAnimContent_DirtyFlag  = 0x02
    266     };
    267     void    dirtyCache(unsigned dirtyFlags);
    268     bool    ensureCache();
    269 
    270     int     logicalToVisualIndex(int index) const { return index - fScrollIndex; }
    271     void    invalSelection();
    272     SkScalar getContentWidth() const;
    273     bool    getRowRect(int index, SkRect*) const;
    274     void    ensureSelectionIsVisible();
    275     void    ensureVisibleRowCount();
    276 
    277     struct BindingRec;
    278 
    279     enum Heights {
    280         kNormal_Height,
    281         kSelected_Height
    282     };
    283     SkListSource*   fSource;
    284     SkScrollBarView*    fScrollBar;
    285     SkAnimator*     fAnims;
    286     BindingRec*     fBindings;
    287     SkString        fSkinName;
    288     SkScalar        fHeights[2];
    289     int16_t         fScrollIndex, fCurrIndex;
    290     uint16_t        fVisibleRowCount, fBindingCount;
    291     SkBool8         fAnimContentDirty;
    292     SkBool8         fAnimFocusDirty;
    293 
    294     typedef SkWidgetView INHERITED;
    295 };
    296 
    297 class SkListSource : public SkRefCnt {
    298 public:
    299     virtual int countFields();
    300     virtual void getFieldName(int index, SkString* field);
    301     /** Return the index of the named field, or -1 if not found */
    302     virtual int findFieldIndex(const char field[]);
    303 
    304     virtual int countRecords();
    305     virtual void getRecord(int rowIndex, int fieldIndex, SkString* data);
    306 
    307     virtual bool prepareWidgetEvent(SkEvent*, int rowIndex);
    308 
    309     static SkListSource* Factory(const char name[]);
    310 };
    311 
    312 #endif
    313