blob: 199b2fe8c5c761daab1d364d33f8172e961cb2a2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com985dfad2010-02-09 12:40:30 +00008#include "SkWidgetViews.h"
9#include "SkTextBox.h"
10
11#ifdef SK_DEBUG
12static void assert_no_attr(const SkDOM& dom, const SkDOM::Node* node, const char attr[])
13{
14 const char* value = dom.findAttr(node, attr);
15 if (value)
16 SkDebugf("unknown attribute %s=\"%s\"\n", attr, value);
17}
18#else
19 #define assert_no_attr(dom, node, attr)
20#endif
21
22SkStaticTextView::SkStaticTextView()
23{
rmistry@google.comd6176b02012-08-23 18:14:13 +000024 fMargin.set(0, 0);
25 fMode = kFixedSize_Mode;
26 fSpacingAlign = SkTextBox::kStart_SpacingAlign;
27
28// init_skin_paint(kStaticText_SkinEnum, &fPaint);
reed@android.com985dfad2010-02-09 12:40:30 +000029}
30
31SkStaticTextView::~SkStaticTextView()
32{
33}
34
35void SkStaticTextView::computeSize()
36{
rmistry@google.comd6176b02012-08-23 18:14:13 +000037 if (fMode == kAutoWidth_Mode)
38 {
39 SkScalar width = fPaint.measureText(fText.c_str(), fText.size());
40 this->setWidth(width + fMargin.fX * 2);
41 }
42 else if (fMode == kAutoHeight_Mode)
43 {
44 SkScalar width = this->width() - fMargin.fX * 2;
45 int lines = width > 0 ? SkTextLineBreaker::CountLines(fText.c_str(), fText.size(), fPaint, width) : 0;
reed@android.com985dfad2010-02-09 12:40:30 +000046
rmistry@google.comd6176b02012-08-23 18:14:13 +000047 this->setHeight(lines * fPaint.getFontSpacing() + fMargin.fY * 2);
48 }
reed@android.com985dfad2010-02-09 12:40:30 +000049}
50
51void SkStaticTextView::setMode(Mode mode)
52{
rmistry@google.comd6176b02012-08-23 18:14:13 +000053 SkASSERT((unsigned)mode < kModeCount);
reed@android.com985dfad2010-02-09 12:40:30 +000054
rmistry@google.comd6176b02012-08-23 18:14:13 +000055 if (fMode != mode)
56 {
57 fMode = SkToU8(mode);
58 this->computeSize();
59 }
reed@android.com985dfad2010-02-09 12:40:30 +000060}
61
62void SkStaticTextView::setSpacingAlign(SkTextBox::SpacingAlign align)
63{
rmistry@google.comd6176b02012-08-23 18:14:13 +000064 fSpacingAlign = SkToU8(align);
65 this->inval(NULL);
reed@android.com985dfad2010-02-09 12:40:30 +000066}
67
68void SkStaticTextView::getMargin(SkPoint* margin) const
69{
rmistry@google.comd6176b02012-08-23 18:14:13 +000070 if (margin)
71 *margin = fMargin;
reed@android.com985dfad2010-02-09 12:40:30 +000072}
73
74void SkStaticTextView::setMargin(SkScalar dx, SkScalar dy)
75{
rmistry@google.comd6176b02012-08-23 18:14:13 +000076 if (fMargin.fX != dx || fMargin.fY != dy)
77 {
78 fMargin.set(dx, dy);
79 this->computeSize();
80 this->inval(NULL);
81 }
reed@android.com985dfad2010-02-09 12:40:30 +000082}
83
84size_t SkStaticTextView::getText(SkString* text) const
85{
rmistry@google.comd6176b02012-08-23 18:14:13 +000086 if (text)
87 *text = fText;
88 return fText.size();
reed@android.com985dfad2010-02-09 12:40:30 +000089}
90
91size_t SkStaticTextView::getText(char text[]) const
92{
rmistry@google.comd6176b02012-08-23 18:14:13 +000093 if (text)
94 memcpy(text, fText.c_str(), fText.size());
95 return fText.size();
reed@android.com985dfad2010-02-09 12:40:30 +000096}
97
98void SkStaticTextView::setText(const SkString& text)
99{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000100 this->setText(text.c_str(), text.size());
reed@android.com985dfad2010-02-09 12:40:30 +0000101}
102
103void SkStaticTextView::setText(const char text[])
104{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105 if (text == NULL)
106 text = "";
107 this->setText(text, strlen(text));
reed@android.com985dfad2010-02-09 12:40:30 +0000108}
109
110void SkStaticTextView::setText(const char text[], size_t len)
111{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112 if (!fText.equals(text, len))
113 {
114 fText.set(text, len);
115 this->computeSize();
116 this->inval(NULL);
117 }
reed@android.com985dfad2010-02-09 12:40:30 +0000118}
119
120void SkStaticTextView::getPaint(SkPaint* paint) const
121{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122 if (paint)
123 *paint = fPaint;
reed@android.com985dfad2010-02-09 12:40:30 +0000124}
125
126void SkStaticTextView::setPaint(const SkPaint& paint)
127{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128 if (fPaint != paint)
129 {
130 fPaint = paint;
131 this->computeSize();
132 this->inval(NULL);
133 }
reed@android.com985dfad2010-02-09 12:40:30 +0000134}
135
136void SkStaticTextView::onDraw(SkCanvas* canvas)
137{
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138 this->INHERITED::onDraw(canvas);
reed@android.com985dfad2010-02-09 12:40:30 +0000139
rmistry@google.comd6176b02012-08-23 18:14:13 +0000140 if (fText.isEmpty())
141 return;
reed@android.com985dfad2010-02-09 12:40:30 +0000142
rmistry@google.comd6176b02012-08-23 18:14:13 +0000143 SkTextBox box;
reed@android.com985dfad2010-02-09 12:40:30 +0000144
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145 box.setMode(fMode == kAutoWidth_Mode ? SkTextBox::kOneLine_Mode : SkTextBox::kLineBreak_Mode);
146 box.setSpacingAlign(this->getSpacingAlign());
147 box.setBox(fMargin.fX, fMargin.fY, this->width() - fMargin.fX, this->height() - fMargin.fY);
148 box.draw(canvas, fText.c_str(), fText.size(), fPaint);
reed@android.com985dfad2010-02-09 12:40:30 +0000149}
150
151void SkStaticTextView::onInflate(const SkDOM& dom, const SkDOM::Node* node)
152{
caryclark@google.com679ab312012-06-06 12:04:00 +0000153if (false) { // avoid bit rot, suppress warning
rmistry@google.comd6176b02012-08-23 18:14:13 +0000154 this->INHERITED::onInflate(dom, node);
reed@android.com985dfad2010-02-09 12:40:30 +0000155
rmistry@google.comd6176b02012-08-23 18:14:13 +0000156 int index;
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000157 if ((index = dom.findList(node, "mode", "fixed,auto-width,auto-height")) >= 0) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000158 this->setMode((Mode)index);
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000159 } else {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000160 assert_no_attr(dom, node, "mode");
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000161 }
reed@android.com985dfad2010-02-09 12:40:30 +0000162
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000163 if ((index = dom.findList(node, "spacing-align", "start,center,end")) >= 0) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000164 this->setSpacingAlign((SkTextBox::SpacingAlign)index);
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000165 } else {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166 assert_no_attr(dom, node, "spacing-align");
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000167 }
reed@android.com985dfad2010-02-09 12:40:30 +0000168
rmistry@google.comd6176b02012-08-23 18:14:13 +0000169 SkScalar s[2];
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000170 if (dom.findScalars(node, "margin", s, 2)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000171 this->setMargin(s[0], s[1]);
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000172 } else {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000173 assert_no_attr(dom, node, "margin");
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000174 }
reed@android.com985dfad2010-02-09 12:40:30 +0000175
rmistry@google.comd6176b02012-08-23 18:14:13 +0000176 const char* text = dom.findAttr(node, "text");
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000177 if (text) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178 this->setText(text);
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000179 }
reed@android.com985dfad2010-02-09 12:40:30 +0000180
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181 if ((node = dom.getFirstChild(node, "paint")) != NULL &&
182 (node = dom.getFirstChild(node, "screenplay")) != NULL)
183 {
caryclark@google.com679ab312012-06-06 12:04:00 +0000184// FIXME: Including inflate_paint causes Windows build to fail -- it complains
185// that SKListView::SkListView is undefined.
186#if 0
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187 inflate_paint(dom, node, &fPaint);
reed@android.com985dfad2010-02-09 12:40:30 +0000188#endif
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189 }
caryclark@google.com679ab312012-06-06 12:04:00 +0000190}
reed@android.com985dfad2010-02-09 12:40:30 +0000191}
192