michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkWidgetViews.h" michael@0: #include "SkTextBox.h" michael@0: michael@0: #ifdef SK_DEBUG michael@0: static void assert_no_attr(const SkDOM& dom, const SkDOM::Node* node, const char attr[]) michael@0: { michael@0: const char* value = dom.findAttr(node, attr); michael@0: if (value) michael@0: SkDebugf("unknown attribute %s=\"%s\"\n", attr, value); michael@0: } michael@0: #else michael@0: #define assert_no_attr(dom, node, attr) michael@0: #endif michael@0: michael@0: SkStaticTextView::SkStaticTextView() michael@0: { michael@0: fMargin.set(0, 0); michael@0: fMode = kFixedSize_Mode; michael@0: fSpacingAlign = SkTextBox::kStart_SpacingAlign; michael@0: michael@0: // init_skin_paint(kStaticText_SkinEnum, &fPaint); michael@0: } michael@0: michael@0: SkStaticTextView::~SkStaticTextView() michael@0: { michael@0: } michael@0: michael@0: void SkStaticTextView::computeSize() michael@0: { michael@0: if (fMode == kAutoWidth_Mode) michael@0: { michael@0: SkScalar width = fPaint.measureText(fText.c_str(), fText.size()); michael@0: this->setWidth(width + fMargin.fX * 2); michael@0: } michael@0: else if (fMode == kAutoHeight_Mode) michael@0: { michael@0: SkScalar width = this->width() - fMargin.fX * 2; michael@0: int lines = width > 0 ? SkTextLineBreaker::CountLines(fText.c_str(), fText.size(), fPaint, width) : 0; michael@0: michael@0: this->setHeight(lines * fPaint.getFontSpacing() + fMargin.fY * 2); michael@0: } michael@0: } michael@0: michael@0: void SkStaticTextView::setMode(Mode mode) michael@0: { michael@0: SkASSERT((unsigned)mode < kModeCount); michael@0: michael@0: if (fMode != mode) michael@0: { michael@0: fMode = SkToU8(mode); michael@0: this->computeSize(); michael@0: } michael@0: } michael@0: michael@0: void SkStaticTextView::setSpacingAlign(SkTextBox::SpacingAlign align) michael@0: { michael@0: fSpacingAlign = SkToU8(align); michael@0: this->inval(NULL); michael@0: } michael@0: michael@0: void SkStaticTextView::getMargin(SkPoint* margin) const michael@0: { michael@0: if (margin) michael@0: *margin = fMargin; michael@0: } michael@0: michael@0: void SkStaticTextView::setMargin(SkScalar dx, SkScalar dy) michael@0: { michael@0: if (fMargin.fX != dx || fMargin.fY != dy) michael@0: { michael@0: fMargin.set(dx, dy); michael@0: this->computeSize(); michael@0: this->inval(NULL); michael@0: } michael@0: } michael@0: michael@0: size_t SkStaticTextView::getText(SkString* text) const michael@0: { michael@0: if (text) michael@0: *text = fText; michael@0: return fText.size(); michael@0: } michael@0: michael@0: size_t SkStaticTextView::getText(char text[]) const michael@0: { michael@0: if (text) michael@0: memcpy(text, fText.c_str(), fText.size()); michael@0: return fText.size(); michael@0: } michael@0: michael@0: void SkStaticTextView::setText(const SkString& text) michael@0: { michael@0: this->setText(text.c_str(), text.size()); michael@0: } michael@0: michael@0: void SkStaticTextView::setText(const char text[]) michael@0: { michael@0: if (text == NULL) michael@0: text = ""; michael@0: this->setText(text, strlen(text)); michael@0: } michael@0: michael@0: void SkStaticTextView::setText(const char text[], size_t len) michael@0: { michael@0: if (!fText.equals(text, len)) michael@0: { michael@0: fText.set(text, len); michael@0: this->computeSize(); michael@0: this->inval(NULL); michael@0: } michael@0: } michael@0: michael@0: void SkStaticTextView::getPaint(SkPaint* paint) const michael@0: { michael@0: if (paint) michael@0: *paint = fPaint; michael@0: } michael@0: michael@0: void SkStaticTextView::setPaint(const SkPaint& paint) michael@0: { michael@0: if (fPaint != paint) michael@0: { michael@0: fPaint = paint; michael@0: this->computeSize(); michael@0: this->inval(NULL); michael@0: } michael@0: } michael@0: michael@0: void SkStaticTextView::onDraw(SkCanvas* canvas) michael@0: { michael@0: this->INHERITED::onDraw(canvas); michael@0: michael@0: if (fText.isEmpty()) michael@0: return; michael@0: michael@0: SkTextBox box; michael@0: michael@0: box.setMode(fMode == kAutoWidth_Mode ? SkTextBox::kOneLine_Mode : SkTextBox::kLineBreak_Mode); michael@0: box.setSpacingAlign(this->getSpacingAlign()); michael@0: box.setBox(fMargin.fX, fMargin.fY, this->width() - fMargin.fX, this->height() - fMargin.fY); michael@0: box.draw(canvas, fText.c_str(), fText.size(), fPaint); michael@0: } michael@0: michael@0: void SkStaticTextView::onInflate(const SkDOM& dom, const SkDOM::Node* node) michael@0: { michael@0: if (false) { // avoid bit rot, suppress warning michael@0: this->INHERITED::onInflate(dom, node); michael@0: michael@0: int index; michael@0: if ((index = dom.findList(node, "mode", "fixed,auto-width,auto-height")) >= 0) { michael@0: this->setMode((Mode)index); michael@0: } else { michael@0: assert_no_attr(dom, node, "mode"); michael@0: } michael@0: michael@0: if ((index = dom.findList(node, "spacing-align", "start,center,end")) >= 0) { michael@0: this->setSpacingAlign((SkTextBox::SpacingAlign)index); michael@0: } else { michael@0: assert_no_attr(dom, node, "spacing-align"); michael@0: } michael@0: michael@0: SkScalar s[2]; michael@0: if (dom.findScalars(node, "margin", s, 2)) { michael@0: this->setMargin(s[0], s[1]); michael@0: } else { michael@0: assert_no_attr(dom, node, "margin"); michael@0: } michael@0: michael@0: const char* text = dom.findAttr(node, "text"); michael@0: if (text) { michael@0: this->setText(text); michael@0: } michael@0: michael@0: if ((node = dom.getFirstChild(node, "paint")) != NULL && michael@0: (node = dom.getFirstChild(node, "screenplay")) != NULL) michael@0: { michael@0: // FIXME: Including inflate_paint causes Windows build to fail -- it complains michael@0: // that SKListView::SkListView is undefined. michael@0: #if 0 michael@0: inflate_paint(dom, node, &fPaint); michael@0: #endif michael@0: } michael@0: } michael@0: }