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