Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
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 */
8 #ifndef SkWidget_DEFINED
9 #define SkWidget_DEFINED
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"
19 class SkEvent;
20 class SkInterpolator;
21 class SkShader;
23 ////////////////////////////////////////////////////////////////////////////////
25 class SkWidget : public SkView {
26 public:
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();
32 static void Init();
33 static void Term();
34 protected:
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);
42 private:
43 SkEvent fEvent;
44 typedef SkView INHERITED;
45 };
47 ////////////////////////////////////////////////////////////////////////////////
49 class SkHasLabelWidget : public SkWidget {
50 public:
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);
59 protected:
60 // called when the label changes
61 virtual void onLabelChange();
63 // overrides
64 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
66 private:
67 SkString fLabel;
68 typedef SkWidget INHERITED;
69 };
71 ////////////////////////////////////////////////////////////////////////////////
73 class SkButtonWidget : public SkHasLabelWidget {
74 public:
75 SkButtonWidget(uint32_t flags = 0) : SkHasLabelWidget(flags), fState(kOff_State) {}
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);
85 protected:
86 /** called when the label changes. default behavior is to inval the widget */
87 virtual void onButtonStateChange();
89 // overrides
90 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
92 private:
93 State fState;
94 typedef SkHasLabelWidget INHERITED;
95 };
97 ////////////////////////////////////////////////////////////////////////////////
99 class SkPushButtonWidget : public SkButtonWidget {
100 public:
101 SkPushButtonWidget(uint32_t flags = 0) : SkButtonWidget(flags) {}
103 protected:
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);
109 private:
110 typedef SkButtonWidget INHERITED;
111 };
113 ////////////////////////////////////////////////////////////////////////////////
115 class SkCheckBoxWidget : public SkButtonWidget {
116 public:
117 SkCheckBoxWidget(uint32_t flags = 0);
119 protected:
120 virtual bool onEvent(const SkEvent&);
121 virtual void onDraw(SkCanvas*);
122 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
124 private:
125 typedef SkButtonWidget INHERITED;
126 };
128 ////////////////////////////////////////////////////////////////////////////////
130 class SkStaticTextView : public SkView {
131 public:
132 SkStaticTextView(uint32_t flags = 0);
133 virtual ~SkStaticTextView();
135 enum Mode {
136 kFixedSize_Mode,
137 kAutoWidth_Mode,
138 kAutoHeight_Mode,
140 kModeCount
141 };
142 Mode getMode() const { return (Mode)fMode; }
143 void setMode(Mode);
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&);
160 protected:
161 // overrides
162 virtual void onDraw(SkCanvas*);
163 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
165 private:
166 SkPoint fMargin;
167 SkString fText;
168 SkPaint fPaint;
169 uint8_t fMode;
170 uint8_t fSpacingAlign;
172 void computeSize();
174 typedef SkView INHERITED;
175 };
177 ////////////////////////////////////////////////////////////////////////////////
179 class SkBitmapView : public SkView {
180 public:
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[]);
188 protected:
189 virtual void onDraw(SkCanvas*);
190 virtual void onInflate(const SkDOM&, const SkDOM::Node*);
192 private:
193 SkBitmap fBitmap;
194 typedef SkView INHERITED;
195 };
197 ////////////////////////////////////////////////////////////////////////////////
199 class SkHasLabelView : public SkView {
200 public:
201 void getLabel(SkString*) const;
202 void setLabel(const SkString&);
203 void setLabel(const char label[]);
205 protected:
206 SkString fLabel;
208 // called when the label changes
209 virtual void onLabelChange();
211 // overrides
212 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
213 };
215 ////////////////////////////////////////////////////////////////////////////////
217 class SkPushButtonView : public SkHasLabelView {
218 public:
219 SkPushButtonView(uint32_t flags = 0);
221 protected:
222 virtual void onDraw(SkCanvas*);
223 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
224 };
226 ////////////////////////////////////////////////////////////////////////////////
228 class SkCheckBoxView : public SkHasLabelView {
229 public:
230 SkCheckBoxView(uint32_t flags = 0);
232 enum State {
233 kOff_State,
234 kOn_State,
235 kMaybe_State
236 };
237 State getState() const { return fState; }
238 void setState(State);
240 protected:
241 virtual void onDraw(SkCanvas*);
242 virtual void onInflate(const SkDOM& dom, const SkDOM::Node*);
244 private:
245 State fState;
246 };
248 ////////////////////////////////////////////////////////////////////////////////
250 class SkProgressView : public SkView {
251 public:
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);
261 protected:
262 virtual void onDraw(SkCanvas*);
263 virtual void onInflate(const SkDOM& dom, const SkDOM::Node* node);
265 private:
266 uint16_t fValue, fMax;
267 SkShader* fOnShader, *fOffShader;
268 SkInterpolator* fInterp;
269 bool fDoInterp;
271 typedef SkView INHERITED;
272 };
274 ////////////////////////////////////////////////////////////////////////////////
276 class SkListSource : public SkEventSink {
277 public:
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);
285 };
287 ////////////////////////////////////////////////////////////////////////////////
289 class SkListView : public SkView {
290 public:
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
298 */
299 int getSelection() const { return fCurrIndex; }
300 /** Set the index of the selected row, or -1 for none
301 */
302 void setSelection(int);
304 void moveSelectionUp();
305 void moveSelectionDown();
307 enum Attr {
308 kBG_Attr,
309 kNormalText_Attr,
310 kHiliteText_Attr,
311 kHiliteCell_Attr,
312 kAttrCount
313 };
314 SkPaint& paint(Attr);
316 SkListSource* getListSource() const { return fSource; }
317 SkListSource* setListSource(SkListSource*);
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
333 protected:
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);
339 private:
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;
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;
357 };
359 ////////////////////////////////////////////////////////////////////////////////
361 class SkGridView : public SkView {
362 public:
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
370 */
371 int getSelection() const { return fCurrIndex; }
372 /** Set the index of the selected row, or -1 for none
373 */
374 void setSelection(int);
376 void moveSelectionUp();
377 void moveSelectionDown();
379 enum Attr {
380 kBG_Attr,
381 kHiliteCell_Attr,
382 kAttrCount
383 };
384 SkPaint& paint(Attr);
386 SkListSource* getListSource() const { return fSource; }
387 SkListSource* setListSource(SkListSource*);
389 protected:
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);
395 private:
396 SkView* fScrollBar;
397 SkPaint fPaint[kAttrCount];
398 SkListSource* fSource;
399 int fCurrIndex; // logical index
401 SkPoint fCellSize;
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;
410 };
412 #endif