epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 8 | #include "SkWidgetViews.h" |
| 9 | #include "SkTextBox.h" |
| 10 | |
| 11 | #ifdef SK_DEBUG |
| 12 | static 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 | |
| 22 | SkStaticTextView::SkStaticTextView() |
| 23 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | fMargin.set(0, 0); |
| 25 | fMode = kFixedSize_Mode; |
| 26 | fSpacingAlign = SkTextBox::kStart_SpacingAlign; |
| 27 | |
| 28 | // init_skin_paint(kStaticText_SkinEnum, &fPaint); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | SkStaticTextView::~SkStaticTextView() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | void SkStaticTextView::computeSize() |
| 36 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 37 | 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.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 46 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 47 | this->setHeight(lines * fPaint.getFontSpacing() + fMargin.fY * 2); |
| 48 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void SkStaticTextView::setMode(Mode mode) |
| 52 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 53 | SkASSERT((unsigned)mode < kModeCount); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 54 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | if (fMode != mode) |
| 56 | { |
| 57 | fMode = SkToU8(mode); |
| 58 | this->computeSize(); |
| 59 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void SkStaticTextView::setSpacingAlign(SkTextBox::SpacingAlign align) |
| 63 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 64 | fSpacingAlign = SkToU8(align); |
| 65 | this->inval(NULL); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void SkStaticTextView::getMargin(SkPoint* margin) const |
| 69 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 70 | if (margin) |
| 71 | *margin = fMargin; |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void SkStaticTextView::setMargin(SkScalar dx, SkScalar dy) |
| 75 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 76 | if (fMargin.fX != dx || fMargin.fY != dy) |
| 77 | { |
| 78 | fMargin.set(dx, dy); |
| 79 | this->computeSize(); |
| 80 | this->inval(NULL); |
| 81 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | size_t SkStaticTextView::getText(SkString* text) const |
| 85 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 86 | if (text) |
| 87 | *text = fText; |
| 88 | return fText.size(); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | size_t SkStaticTextView::getText(char text[]) const |
| 92 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 93 | if (text) |
| 94 | memcpy(text, fText.c_str(), fText.size()); |
| 95 | return fText.size(); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void SkStaticTextView::setText(const SkString& text) |
| 99 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 100 | this->setText(text.c_str(), text.size()); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void SkStaticTextView::setText(const char text[]) |
| 104 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 105 | if (text == NULL) |
| 106 | text = ""; |
| 107 | this->setText(text, strlen(text)); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void SkStaticTextView::setText(const char text[], size_t len) |
| 111 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 112 | if (!fText.equals(text, len)) |
| 113 | { |
| 114 | fText.set(text, len); |
| 115 | this->computeSize(); |
| 116 | this->inval(NULL); |
| 117 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void SkStaticTextView::getPaint(SkPaint* paint) const |
| 121 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 122 | if (paint) |
| 123 | *paint = fPaint; |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void SkStaticTextView::setPaint(const SkPaint& paint) |
| 127 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 128 | if (fPaint != paint) |
| 129 | { |
| 130 | fPaint = paint; |
| 131 | this->computeSize(); |
| 132 | this->inval(NULL); |
| 133 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void SkStaticTextView::onDraw(SkCanvas* canvas) |
| 137 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 138 | this->INHERITED::onDraw(canvas); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 139 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 140 | if (fText.isEmpty()) |
| 141 | return; |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 142 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 143 | SkTextBox box; |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 144 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 145 | 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.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void SkStaticTextView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 152 | { |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 153 | if (false) { // avoid bit rot, suppress warning |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 154 | this->INHERITED::onInflate(dom, node); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 155 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 156 | int index; |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 157 | if ((index = dom.findList(node, "mode", "fixed,auto-width,auto-height")) >= 0) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 158 | this->setMode((Mode)index); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 159 | } else { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 160 | assert_no_attr(dom, node, "mode"); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 161 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 162 | |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 163 | if ((index = dom.findList(node, "spacing-align", "start,center,end")) >= 0) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 164 | this->setSpacingAlign((SkTextBox::SpacingAlign)index); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 165 | } else { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 166 | assert_no_attr(dom, node, "spacing-align"); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 167 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 168 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 169 | SkScalar s[2]; |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 170 | if (dom.findScalars(node, "margin", s, 2)) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 171 | this->setMargin(s[0], s[1]); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 172 | } else { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 173 | assert_no_attr(dom, node, "margin"); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 174 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 175 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 176 | const char* text = dom.findAttr(node, "text"); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 177 | if (text) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 178 | this->setText(text); |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 179 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 180 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 181 | if ((node = dom.getFirstChild(node, "paint")) != NULL && |
| 182 | (node = dom.getFirstChild(node, "screenplay")) != NULL) |
| 183 | { |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 184 | // FIXME: Including inflate_paint causes Windows build to fail -- it complains |
| 185 | // that SKListView::SkListView is undefined. |
| 186 | #if 0 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 187 | inflate_paint(dom, node, &fPaint); |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 188 | #endif |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 189 | } |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 190 | } |
reed@android.com | 985dfad | 2010-02-09 12:40:30 +0000 | [diff] [blame] | 191 | } |
| 192 | |