1 #include "SkWidgetViews.h"
2 #include "SkTextBox.h"
3
4 #ifdef SK_DEBUG
assert_no_attr(const SkDOM & dom,const SkDOM::Node * node,const char attr[])5 static void assert_no_attr(const SkDOM& dom, const SkDOM::Node* node, const char attr[])
6 {
7 const char* value = dom.findAttr(node, attr);
8 if (value)
9 SkDebugf("unknown attribute %s=\"%s\"\n", attr, value);
10 }
11 #else
12 #define assert_no_attr(dom, node, attr)
13 #endif
14
SkStaticTextView()15 SkStaticTextView::SkStaticTextView()
16 {
17 fMargin.set(0, 0);
18 fMode = kFixedSize_Mode;
19 fSpacingAlign = SkTextBox::kStart_SpacingAlign;
20
21 // init_skin_paint(kStaticText_SkinEnum, &fPaint);
22 }
23
~SkStaticTextView()24 SkStaticTextView::~SkStaticTextView()
25 {
26 }
27
computeSize()28 void SkStaticTextView::computeSize()
29 {
30 if (fMode == kAutoWidth_Mode)
31 {
32 SkScalar width = fPaint.measureText(fText.c_str(), fText.size());
33 this->setWidth(width + fMargin.fX * 2);
34 }
35 else if (fMode == kAutoHeight_Mode)
36 {
37 SkScalar width = this->width() - fMargin.fX * 2;
38 int lines = width > 0 ? SkTextLineBreaker::CountLines(fText.c_str(), fText.size(), fPaint, width) : 0;
39
40 this->setHeight(lines * fPaint.getFontSpacing() + fMargin.fY * 2);
41 }
42 }
43
setMode(Mode mode)44 void SkStaticTextView::setMode(Mode mode)
45 {
46 SkASSERT((unsigned)mode < kModeCount);
47
48 if (fMode != mode)
49 {
50 fMode = SkToU8(mode);
51 this->computeSize();
52 }
53 }
54
setSpacingAlign(SkTextBox::SpacingAlign align)55 void SkStaticTextView::setSpacingAlign(SkTextBox::SpacingAlign align)
56 {
57 fSpacingAlign = SkToU8(align);
58 this->inval(NULL);
59 }
60
getMargin(SkPoint * margin) const61 void SkStaticTextView::getMargin(SkPoint* margin) const
62 {
63 if (margin)
64 *margin = fMargin;
65 }
66
setMargin(SkScalar dx,SkScalar dy)67 void SkStaticTextView::setMargin(SkScalar dx, SkScalar dy)
68 {
69 if (fMargin.fX != dx || fMargin.fY != dy)
70 {
71 fMargin.set(dx, dy);
72 this->computeSize();
73 this->inval(NULL);
74 }
75 }
76
getText(SkString * text) const77 size_t SkStaticTextView::getText(SkString* text) const
78 {
79 if (text)
80 *text = fText;
81 return fText.size();
82 }
83
getText(char text[]) const84 size_t SkStaticTextView::getText(char text[]) const
85 {
86 if (text)
87 memcpy(text, fText.c_str(), fText.size());
88 return fText.size();
89 }
90
setText(const SkString & text)91 void SkStaticTextView::setText(const SkString& text)
92 {
93 this->setText(text.c_str(), text.size());
94 }
95
setText(const char text[])96 void SkStaticTextView::setText(const char text[])
97 {
98 if (text == NULL)
99 text = "";
100 this->setText(text, strlen(text));
101 }
102
setText(const char text[],size_t len)103 void SkStaticTextView::setText(const char text[], size_t len)
104 {
105 if (!fText.equals(text, len))
106 {
107 fText.set(text, len);
108 this->computeSize();
109 this->inval(NULL);
110 }
111 }
112
getPaint(SkPaint * paint) const113 void SkStaticTextView::getPaint(SkPaint* paint) const
114 {
115 if (paint)
116 *paint = fPaint;
117 }
118
setPaint(const SkPaint & paint)119 void SkStaticTextView::setPaint(const SkPaint& paint)
120 {
121 if (fPaint != paint)
122 {
123 fPaint = paint;
124 this->computeSize();
125 this->inval(NULL);
126 }
127 }
128
onDraw(SkCanvas * canvas)129 void SkStaticTextView::onDraw(SkCanvas* canvas)
130 {
131 this->INHERITED::onDraw(canvas);
132
133 if (fText.isEmpty())
134 return;
135
136 SkTextBox box;
137
138 box.setMode(fMode == kAutoWidth_Mode ? SkTextBox::kOneLine_Mode : SkTextBox::kLineBreak_Mode);
139 box.setSpacingAlign(this->getSpacingAlign());
140 box.setBox(fMargin.fX, fMargin.fY, this->width() - fMargin.fX, this->height() - fMargin.fY);
141 box.draw(canvas, fText.c_str(), fText.size(), fPaint);
142 }
143
onInflate(const SkDOM & dom,const SkDOM::Node * node)144 void SkStaticTextView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
145 {
146 #if 0
147 this->INHERITED::onInflate(dom, node);
148
149 int index;
150 if ((index = dom.findList(node, "mode", "fixed,auto-width,auto-height")) >= 0)
151 this->setMode((Mode)index);
152 else
153 assert_no_attr(dom, node, "mode");
154
155 if ((index = dom.findList(node, "spacing-align", "start,center,end")) >= 0)
156 this->setSpacingAlign((SkTextBox::SpacingAlign)index);
157 else
158 assert_no_attr(dom, node, "spacing-align");
159
160 SkScalar s[2];
161 if (dom.findScalars(node, "margin", s, 2))
162 this->setMargin(s[0], s[1]);
163 else
164 assert_no_attr(dom, node, "margin");
165
166 const char* text = dom.findAttr(node, "text");
167 if (text)
168 this->setText(text);
169
170 if ((node = dom.getFirstChild(node, "paint")) != NULL &&
171 (node = dom.getFirstChild(node, "screenplay")) != NULL)
172 {
173 inflate_paint(dom, node, &fPaint);
174 }
175 #endif
176 }
177
178