2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkWidget_DEFINED
9 #define SkWidget_DEFINED
15 #include "SkTDArray.h"
16 #include "SkTextBox.h"
23 ////////////////////////////////////////////////////////////////////////////////
25 class SkWidget : public SkView {
27 SkWidget(uint32_t flags = 0) : SkView(flags | kFocusable_Mask | kEnabled_Mask) {}
29 /** Call this to post the widget's event to its listeners */
30 void postWidgetEvent();
35 // override to add slots to an event before posting
36 virtual void prepareWidgetEvent(SkEvent*);
37 virtual void onEnabledChange();
39 // <event ...> to initialize the event from XML
40 virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node);
44 typedef SkView INHERITED;
47 ////////////////////////////////////////////////////////////////////////////////
49 class SkHasLabelWidget : public SkWidget {
51 SkHasLabelWidget(uint32_t flags = 0) : SkWidget(flags) {}
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);
60 // called when the label changes
61 virtual void onLabelChange();
64 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
68 typedef SkWidget INHERITED;
71 ////////////////////////////////////////////////////////////////////////////////
73 class SkButtonWidget : public SkHasLabelWidget {
75 SkButtonWidget(uint32_t flags = 0) : SkHasLabelWidget(flags), fState(kOff_State) {}
78 kOff_State, //!< XML: buttonState="off"
79 kOn_State, //!< XML: buttonState="on"
80 kUnknown_State //!< XML: buttonState="unknown"
82 State getButtonState() const { return fState; }
83 void setButtonState(State);
86 /** called when the label changes. default behavior is to inval the widget */
87 virtual void onButtonStateChange();
90 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
94 typedef SkHasLabelWidget INHERITED;
97 ////////////////////////////////////////////////////////////////////////////////
99 class SkPushButtonWidget : public SkButtonWidget {
101 SkPushButtonWidget(uint32_t flags = 0) : SkButtonWidget(flags) {}
104 bool onEvent(const SkEvent&) override;
105 void onDraw(SkCanvas*) override;
106 Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override;
107 bool onClick(Click* click) override;
110 typedef SkButtonWidget INHERITED;
113 ////////////////////////////////////////////////////////////////////////////////
115 class SkCheckBoxWidget : public SkButtonWidget {
117 SkCheckBoxWidget(uint32_t flags = 0);
120 virtual bool onEvent(const SkEvent&);
121 virtual void onDraw(SkCanvas*);
122 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
125 typedef SkButtonWidget INHERITED;
128 ////////////////////////////////////////////////////////////////////////////////
130 class SkStaticTextView : public SkView {
132 SkStaticTextView(uint32_t flags = 0);
133 virtual ~SkStaticTextView();
142 Mode getMode() const { return (Mode)fMode; }
145 SkTextBox::SpacingAlign getSpacingAlign() const { return (SkTextBox::SpacingAlign)fSpacingAlign; }
146 void setSpacingAlign(SkTextBox::SpacingAlign);
148 void getMargin(SkPoint* margin) const;
149 void setMargin(SkScalar dx, SkScalar dy);
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);
157 void getPaint(SkPaint*) const;
158 void setPaint(const SkPaint&);
162 virtual void onDraw(SkCanvas*);
163 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
170 uint8_t fSpacingAlign;
174 typedef SkView INHERITED;
177 ////////////////////////////////////////////////////////////////////////////////
179 class SkBitmapView : public SkView {
181 SkBitmapView(uint32_t flags = 0);
182 virtual ~SkBitmapView();
184 bool getBitmap(SkBitmap*) const;
185 void setBitmap(const SkBitmap*, bool viewOwnsPixels);
186 bool loadBitmapFromFile(const char path[]);
189 virtual void onDraw(SkCanvas*);
190 virtual void onInflate(const SkDOM&, const SkDOM::Node*);
194 typedef SkView INHERITED;
197 ////////////////////////////////////////////////////////////////////////////////
199 class SkHasLabelView : public SkView {
201 void getLabel(SkString*) const;
202 void setLabel(const SkString&);
203 void setLabel(const char label[]);
208 // called when the label changes
209 virtual void onLabelChange();
212 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
215 ////////////////////////////////////////////////////////////////////////////////
217 class SkPushButtonView : public SkHasLabelView {
219 SkPushButtonView(uint32_t flags = 0);
222 virtual void onDraw(SkCanvas*);
223 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
226 ////////////////////////////////////////////////////////////////////////////////
228 class SkCheckBoxView : public SkHasLabelView {
230 SkCheckBoxView(uint32_t flags = 0);
237 State getState() const { return fState; }
238 void setState(State);
241 virtual void onDraw(SkCanvas*);
242 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
248 ////////////////////////////////////////////////////////////////////////////////
250 class SkProgressView : public SkView {
252 SkProgressView(uint32_t flags = 0);
253 virtual ~SkProgressView();
255 uint16_t getValue() const { return fValue; }
256 uint16_t getMax() const { return fMax; }
258 void setMax(U16CPU max);
259 void setValue(U16CPU value);
262 virtual void onDraw(SkCanvas*);
263 virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node);
266 uint16_t fValue, fMax;
267 SkShader* fOnShader, *fOffShader;
268 SkInterpolator* fInterp;
271 typedef SkView INHERITED;
274 ////////////////////////////////////////////////////////////////////////////////
276 class SkListSource : public SkEventSink {
278 virtual int countRows() = 0;
279 virtual void getRow(int index, SkString* left, SkString* right) = 0;
280 virtual SkEvent* getEvent(int index);
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);
287 ////////////////////////////////////////////////////////////////////////////////
289 class SkListView : public SkView {
291 SkListView(uint32_t flags = 0);
292 virtual ~SkListView();
294 SkScalar getRowHeight() const { return fRowHeight; }
295 void setRowHeight(SkScalar);
297 /** Return the index of the selected row, or -1 if none
299 int getSelection() const { return fCurrIndex; }
300 /** Set the index of the selected row, or -1 for none
302 void setSelection(int);
304 void moveSelectionUp();
305 void moveSelectionDown();
314 SkPaint& paint(Attr);
316 SkListSource* getListSource() const { return fSource; }
317 SkListSource* setListSource(SkListSource*);
321 kSelectionChange_Action,
322 kSelectionPicked_Action,
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.
330 void setActionEvent(Action, SkEvent* event);
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);
340 SkPaint fPaint[kAttrCount];
341 SkListSource* fSource;
343 int fCurrIndex; // logical index
344 int fScrollIndex; // logical index of top-most visible row
345 int fVisibleRowCount;
348 void dirtyStrCache();
349 void ensureStrCache(int visibleCount);
351 int logicalToVisualIndex(int index) const { return index - fScrollIndex; }
352 void invalSelection();
353 bool getRowRect(int index, SkRect*) const;
354 void ensureSelectionIsVisible();
356 typedef SkView INHERITED;
359 ////////////////////////////////////////////////////////////////////////////////
361 class SkGridView : public SkView {
363 SkGridView(uint32_t flags = 0);
364 virtual ~SkGridView();
366 void getCellSize(SkPoint*) const;
367 void setCellSize(SkScalar x, SkScalar y);
369 /** Return the index of the selected item, or -1 if none
371 int getSelection() const { return fCurrIndex; }
372 /** Set the index of the selected row, or -1 for none
374 void setSelection(int);
376 void moveSelectionUp();
377 void moveSelectionDown();
384 SkPaint& paint(Attr);
386 SkListSource* getListSource() const { return fSource; }
387 SkListSource* setListSource(SkListSource*);
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);
397 SkPaint fPaint[kAttrCount];
398 SkListSource* fSource;
399 int fCurrIndex; // logical index
402 SkIPoint fVisibleCount;
404 int logicalToVisualIndex(int index) const { return index; }
405 void invalSelection();
406 bool getCellRect(int index, SkRect*) const;
407 void ensureSelectionIsVisible();
409 typedef SkView INHERITED;