summaryrefslogtreecommitdiff
path: root/indra/llui/lltextbox.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'indra/llui/lltextbox.cpp')
-rw-r--r--indra/llui/lltextbox.cpp645
1 files changed, 472 insertions, 173 deletions
diff --git a/indra/llui/lltextbox.cpp b/indra/llui/lltextbox.cpp
index 89893bcf8d..132bef0296 100644
--- a/indra/llui/lltextbox.cpp
+++ b/indra/llui/lltextbox.cpp
@@ -35,69 +35,60 @@
#include "lluictrlfactory.h"
#include "llfocusmgr.h"
#include "llwindow.h"
-
-static LLRegisterWidget<LLTextBox> r("text");
-
-LLTextBox::LLTextBox(const std::string& name, const LLRect& rect, const std::string& text,
- const LLFontGL* font, BOOL mouse_opaque)
-: LLUICtrl(name, rect, mouse_opaque, NULL, NULL, FOLLOWS_LEFT | FOLLOWS_TOP ),
- mFontGL(font ? font : LLFontGL::getFontSansSerifSmall())
-{
- initDefaults();
- setText( text );
- setTabStop(FALSE);
-}
-
-LLTextBox::LLTextBox(const std::string& name, const std::string& text, F32 max_width,
- const LLFontGL* font, BOOL mouse_opaque) :
- LLUICtrl(name, LLRect(0, 0, 1, 1), mouse_opaque, NULL, NULL, FOLLOWS_LEFT | FOLLOWS_TOP),
- mFontGL(font ? font : LLFontGL::getFontSansSerifSmall())
-{
- initDefaults();
- setWrappedText(text, max_width);
- reshapeToFitText();
- setTabStop(FALSE);
-}
-
-LLTextBox::LLTextBox(const std::string& name_and_label, const LLRect& rect) :
- LLUICtrl(name_and_label, rect, TRUE, NULL, NULL, FOLLOWS_LEFT | FOLLOWS_TOP),
- mFontGL(LLFontGL::getFontSansSerifSmall())
-{
- initDefaults();
- setText( name_and_label );
- setTabStop(FALSE);
-}
-
-void LLTextBox::initDefaults()
+#include "llurlregistry.h"
+#include "llstyle.h"
+
+static LLDefaultChildRegistry::Register<LLTextBox> r("text");
+
+LLTextBox::Params::Params()
+: text_color("text_color"),
+ length("length"),
+ type("type"),
+ border_visible("border_visible", false),
+ border_drop_shadow_visible("border_drop_shadow_visible", false),
+ bg_visible("bg_visible", false),
+ use_ellipses("use_ellipses"),
+ word_wrap("word_wrap", false),
+ drop_shadow_visible("drop_shadow_visible"),
+ disabled_color("disabled_color"),
+ background_color("background_color"),
+ v_pad("v_pad", 0),
+ h_pad("h_pad", 0),
+ line_spacing("line_spacing", 0),
+ text("text"),
+ font_shadow("font_shadow", LLFontGL::NO_SHADOW)
+{}
+
+LLTextBox::LLTextBox(const LLTextBox::Params& p)
+: LLUICtrl(p),
+ LLTextBase(p),
+ mBackgroundVisible( p.bg_visible ),
+ mBorderVisible( p.border_visible ),
+ mShadowType( p.font_shadow ),
+ mBorderDropShadowVisible( p.border_drop_shadow_visible ),
+ mUseEllipses( p.use_ellipses ),
+ mHPad(p.h_pad),
+ mVPad(p.v_pad),
+ mVAlign( LLFontGL::TOP ),
+ mClickedCallback(NULL),
+ mTextColor(p.text_color()),
+ mDisabledColor(p.disabled_color()),
+ mBackgroundColor(p.background_color()),
+ mHAlign(p.font_halign),
+ mLineSpacing(p.line_spacing),
+ mDidWordWrap(FALSE)
{
- mTextColor = LLUI::sColorsGroup->getColor("LabelTextColor");
- mDisabledColor = LLUI::sColorsGroup->getColor("LabelDisabledColor");
- mBackgroundColor = LLUI::sColorsGroup->getColor("DefaultBackgroundColor");
- mBorderColor = LLUI::sColorsGroup->getColor("DefaultHighlightLight");
- mHoverColor = LLUI::sColorsGroup->getColor( "LabelSelectedColor" );
- mHoverActive = FALSE;
- mHasHover = FALSE;
- mBackgroundVisible = FALSE;
- mBorderVisible = FALSE;
- mFontStyle = LLFontGL::DROP_SHADOW_SOFT;
- mBorderDropShadowVisible = FALSE;
- mUseEllipses = FALSE;
- mLineSpacing = 0;
- mHPad = 0;
- mVPad = 0;
- mHAlign = LLFontGL::LEFT;
- mVAlign = LLFontGL::TOP;
- mClickedCallback = NULL;
- mCallbackUserData = NULL;
+ mWordWrap = p.word_wrap;
+ setText( p.text() );
}
BOOL LLTextBox::handleMouseDown(S32 x, S32 y, MASK mask)
{
BOOL handled = FALSE;
- // HACK: Only do this if there actually is a click callback, so that
+ // HACK: Only do this if there actually is something to click, so that
// overly large text boxes in the older UI won't start eating clicks.
- if (mClickedCallback)
+ if (isClickable())
{
handled = TRUE;
@@ -113,17 +104,15 @@ BOOL LLTextBox::handleMouseDown(S32 x, S32 y, MASK mask)
return handled;
}
-
BOOL LLTextBox::handleMouseUp(S32 x, S32 y, MASK mask)
{
BOOL handled = FALSE;
// We only handle the click if the click both started and ended within us
- // HACK: Only do this if there actually is a click callback, so that
+ // HACK: Only do this if there actually is something to click, so that
// overly large text boxes in the older UI won't start eating clicks.
- if (mClickedCallback
- && hasMouseCapture())
+ if (isClickable() && hasMouseCapture())
{
handled = TRUE;
@@ -135,33 +124,62 @@ BOOL LLTextBox::handleMouseUp(S32 x, S32 y, MASK mask)
make_ui_sound("UISndClickRelease");
}
- // DO THIS AT THE VERY END to allow the button to be destroyed as a result of being clicked.
- // If mouseup in the widget, it's been clicked
- if (mClickedCallback)
+ // handle clicks on Urls in the textbox first
+ if (! handleMouseUpOverUrl(x, y))
{
- (*mClickedCallback)( mCallbackUserData );
+ // DO THIS AT THE VERY END to allow the button to be destroyed
+ // as a result of being clicked. If mouseup in the widget,
+ // it's been clicked
+ if (mClickedCallback && ! handled)
+ {
+ mClickedCallback();
+ }
}
}
return handled;
}
+BOOL LLTextBox::handleRightMouseDown(S32 x, S32 y, MASK mask)
+{
+ // pop up a context menu for any Url under the cursor
+ return handleRightMouseDownOverUrl(this, x, y);
+}
+
BOOL LLTextBox::handleHover(S32 x, S32 y, MASK mask)
{
- BOOL handled = LLView::handleHover(x,y,mask);
- if(mHoverActive)
+ // Check to see if we're over an HTML-style link
+ if (handleHoverOverUrl(x, y))
{
- mHasHover = TRUE; // This should be set every frame during a hover.
- getWindow()->setCursor(UI_CURSOR_ARROW);
+ lldebugst(LLERR_USER_INPUT) << "hover handled by " << getName() << llendl;
+ getWindow()->setCursor(UI_CURSOR_HAND);
+ return TRUE;
}
- return (handled || mHasHover);
+ return LLView::handleHover(x,y,mask);
+}
+
+BOOL LLTextBox::handleToolTip(S32 x, S32 y, std::string& msg, LLRect& sticky_rect_screen)
+{
+ if (handleToolTipForUrl(this, x, y, msg, sticky_rect_screen))
+ {
+ return TRUE;
+ }
+
+ return LLUICtrl::handleToolTip(x, y, msg, sticky_rect_screen);
}
void LLTextBox::setText(const LLStringExplicit& text)
{
- mText.assign(text);
- setLineLengths();
+ if(mWordWrap && !mDidWordWrap)
+ {
+ setWrappedText(text);
+ }
+ else
+ {
+ mText.assign(text);
+ updateDisplayTextAndSegments();
+ }
}
void LLTextBox::setLineLengths()
@@ -169,11 +187,11 @@ void LLTextBox::setLineLengths()
mLineLengthList.clear();
std::string::size_type cur = 0;
- std::string::size_type len = mText.getWString().size();
+ std::string::size_type len = mDisplayText.size();
while (cur < len)
{
- std::string::size_type end = mText.getWString().find('\n', cur);
+ std::string::size_type end = mDisplayText.find('\n', cur);
std::string::size_type runLen;
if (end == std::string::npos)
@@ -191,19 +209,12 @@ void LLTextBox::setLineLengths()
}
}
-void LLTextBox::setWrappedText(const LLStringExplicit& in_text, F32 max_width)
+LLWString LLTextBox::wrapText(const LLWString &wtext, S32 &hoffset, S32 &line_num, F32 max_width)
{
- if (max_width < 0.0)
- {
- max_width = (F32)getRect().getWidth();
- }
-
- LLWString wtext = utf8str_to_wstring(in_text);
LLWString final_wtext;
- LLWString::size_type cur = 0;;
- LLWString::size_type len = wtext.size();
-
+ LLWString::size_type cur = 0;
+ LLWString::size_type len = wtext.size();
while (cur < len)
{
LLWString::size_type end = wtext.find('\n', cur);
@@ -212,29 +223,121 @@ void LLTextBox::setWrappedText(const LLStringExplicit& in_text, F32 max_width)
end = len;
}
+ bool charsRemaining = true;
LLWString::size_type runLen = end - cur;
if (runLen > 0)
{
+ // work out how many chars can fit onto the current line
LLWString run(wtext, cur, runLen);
LLWString::size_type useLen =
- mFontGL->maxDrawableChars(run.c_str(), max_width, runLen, TRUE);
+ mDefaultFont->maxDrawableChars(run.c_str(), max_width-hoffset, runLen, TRUE);
+ charsRemaining = (cur + useLen < len);
+ // try to break lines on word boundaries
+ if (useLen < run.size())
+ {
+ LLWString::size_type prev_use_len = useLen;
+ while (useLen > 0 && ! isspace(run[useLen-1]) && ! ispunct(run[useLen-1]))
+ {
+ --useLen;
+ }
+ if (useLen == 0)
+ {
+ useLen = prev_use_len;
+ }
+ }
+
+ // add the chars that could fit onto one line to our result
final_wtext.append(wtext, cur, useLen);
cur += useLen;
+ hoffset += mDefaultFont->getWidth(run.substr(0, useLen).c_str());
+
+ // abort if not enough room to add any more characters
+ if (useLen == 0)
+ {
+ break;
+ }
}
- if (cur < len)
+ if (charsRemaining)
{
if (wtext[cur] == '\n')
{
cur += 1;
}
final_wtext += '\n';
+ hoffset = 0;
+ line_num += 1;
}
}
- std::string final_text = wstring_to_utf8str(final_wtext);
- setText(final_text);
+ return final_wtext;
+}
+
+void LLTextBox::setWrappedText(const LLStringExplicit& in_text, F32 max_width)
+{
+ mDidWordWrap = TRUE;
+ setText(wstring_to_utf8str(getWrappedText(in_text, max_width)));
+}
+
+LLWString LLTextBox::getWrappedText(const LLStringExplicit& in_text, F32 max_width)
+{
+ //
+ // we don't want to wrap Urls otherwise we won't be able to detect their
+ // presence for hyperlinking. So we look for all Urls, and then word wrap
+ // the text before and after, but never break a Url in the middle. We
+ // also need to consider that the Url will be displayed as a label (not
+ // necessary the actual Url string).
+ //
+
+ if (max_width < 0.0f)
+ {
+ max_width = (F32)getRect().getWidth();
+ }
+
+ LLWString wtext = utf8str_to_wstring(in_text);
+ LLWString final_wtext;
+ S32 line_num = 1;
+ S32 hoffset = 0;
+
+ // find the next Url in the text string
+ LLUrlMatch match;
+ while ( LLUrlRegistry::instance().findUrl(wtext, match))
+ {
+ S32 start = match.getStart();
+ S32 end = match.getEnd() + 1;
+
+ // perform word wrap on the text before the Url
+ final_wtext += wrapText(wtext.substr(0, start), hoffset, line_num, max_width);
+
+ // add the Url (but compute width based on its label)
+ S32 label_width = mDefaultFont->getWidth(match.getLabel());
+ if (hoffset > 0 && hoffset + label_width > max_width)
+ {
+ final_wtext += '\n';
+ line_num++;
+ hoffset = 0;
+ }
+ final_wtext += wtext.substr(start, end-start);
+ hoffset += label_width;
+ if (hoffset > max_width)
+ {
+ final_wtext += '\n';
+ line_num++;
+ hoffset = 0;
+ // eat any leading whitespace on the next line
+ while (isspace(wtext[end]) && end < (S32)wtext.size())
+ {
+ end++;
+ }
+ }
+
+ // move on to the rest of the text after the Url
+ wtext = wtext.substr(end, wtext.size() - end + 1);
+ }
+
+ final_wtext += wrapText(wtext, hoffset, line_num, max_width);
+ return final_wtext;
}
S32 LLTextBox::getTextPixelWidth()
@@ -247,7 +350,7 @@ S32 LLTextBox::getTextPixelWidth()
iter != mLineLengthList.end(); ++iter)
{
S32 line_length = *iter;
- S32 line_width = mFontGL->getWidth( mText.getWString().c_str(), cur_pos, line_length );
+ S32 line_width = mDefaultFont->getWidth( mDisplayText.c_str(), cur_pos, line_length );
if( line_width > max_line_width )
{
max_line_width = line_width;
@@ -257,7 +360,7 @@ S32 LLTextBox::getTextPixelWidth()
}
else
{
- max_line_width = mFontGL->getWidth(mText.getWString().c_str());
+ max_line_width = mDefaultFont->getWidth(mDisplayText.c_str());
}
return max_line_width;
}
@@ -269,19 +372,26 @@ S32 LLTextBox::getTextPixelHeight()
{
num_lines = 1;
}
- return (S32)(num_lines * mFontGL->getLineHeight());
+ return (S32)(num_lines * mDefaultFont->getLineHeight());
}
+void LLTextBox::setValue(const LLSD& value )
+{
+ mDidWordWrap = FALSE;
+ setText(value.asString());
+}
BOOL LLTextBox::setTextArg( const std::string& key, const LLStringExplicit& text )
{
mText.setArg(key, text);
- setLineLengths();
+ updateDisplayTextAndSegments();
return TRUE;
}
void LLTextBox::draw()
{
+ F32 alpha = getDrawContext().mAlpha;
+
if (mBorderVisible)
{
gl_rect_2d_offset_local(getLocalRect(), 2, FALSE);
@@ -289,16 +399,16 @@ void LLTextBox::draw()
if( mBorderDropShadowVisible )
{
- static LLColor4 color_drop_shadow = LLUI::sColorsGroup->getColor("ColorDropShadow");
- static S32 drop_shadow_tooltip = LLUI::sConfigGroup->getS32("DropShadowTooltip");
+ static LLUIColor color_drop_shadow = LLUIColorTable::instance().getColor("ColorDropShadow");
+ static LLUICachedControl<S32> drop_shadow_tooltip ("DropShadowTooltip", 0);
gl_drop_shadow(0, getRect().getHeight(), getRect().getWidth(), 0,
- color_drop_shadow, drop_shadow_tooltip);
+ color_drop_shadow % alpha, drop_shadow_tooltip);
}
if (mBackgroundVisible)
{
LLRect r( 0, getRect().getHeight(), getRect().getWidth(), 0 );
- gl_rect_2d( r, mBackgroundColor );
+ gl_rect_2d( r, mBackgroundColor.get() % alpha );
}
S32 text_x = 0;
@@ -319,18 +429,11 @@ void LLTextBox::draw()
if ( getEnabled() )
{
- if(mHasHover)
- {
- drawText( text_x, text_y, mHoverColor );
- }
- else
- {
- drawText( text_x, text_y, mTextColor );
- }
+ drawText( text_x, text_y, mDisplayText, mTextColor.get() );
}
else
{
- drawText( text_x, text_y, mDisabledColor );
+ drawText( text_x, text_y, mDisplayText, mDisabledColor.get() );
}
if (sDebugRects)
@@ -338,128 +441,324 @@ void LLTextBox::draw()
drawDebugRect();
}
- mHasHover = FALSE; // This is reset every frame.
+ //// *HACK: also draw debug rectangles around currently-being-edited LLView, and any elements that are being highlighted by GUI preview code (see LLFloaterUIPreview)
+ //std::set<LLView*>::iterator iter = std::find(sPreviewHighlightedElements.begin(), sPreviewHighlightedElements.end(), this);
+ //if ((sEditingUI && this == sEditingUIView) || (iter != sPreviewHighlightedElements.end() && sDrawPreviewHighlights))
+ //{
+ // drawDebugRect();
+ //}
}
void LLTextBox::reshape(S32 width, S32 height, BOOL called_from_parent)
{
- // reparse line lengths
+ // reparse line lengths (don't need to recalculate the display text)
setLineLengths();
LLView::reshape(width, height, called_from_parent);
}
-void LLTextBox::drawText( S32 x, S32 y, const LLColor4& color )
+void LLTextBox::drawText( S32 x, S32 y, const LLWString &text, const LLColor4& color )
{
- if( mLineLengthList.empty() )
+ F32 alpha = getDrawContext().mAlpha;
+ if (mSegments.size() > 1)
+ {
+ // we have Urls (or other multi-styled segments)
+ drawTextSegments(x, y, text);
+ }
+ else if( mLineLengthList.empty() )
{
- mFontGL->render(mText.getWString(), 0, (F32)x, (F32)y, color,
- mHAlign, mVAlign,
- mFontStyle,
- S32_MAX, getRect().getWidth(), NULL, TRUE, mUseEllipses);
+ // simple case of 1 line of text in one style
+ mDefaultFont->render(text, 0, (F32)x, (F32)y, color % alpha,
+ mHAlign, mVAlign,
+ 0,
+ mShadowType,
+ S32_MAX, getRect().getWidth(), NULL, mUseEllipses);
}
else
{
+ // simple case of multiple lines of text, all in the same style
S32 cur_pos = 0;
for (std::vector<S32>::iterator iter = mLineLengthList.begin();
iter != mLineLengthList.end(); ++iter)
{
S32 line_length = *iter;
- mFontGL->render(mText.getWString(), cur_pos, (F32)x, (F32)y, color,
- mHAlign, mVAlign,
- mFontStyle,
- line_length, getRect().getWidth(), NULL, TRUE, mUseEllipses );
+ mDefaultFont->render(text, cur_pos, (F32)x, (F32)y, color % alpha,
+ mHAlign, mVAlign,
+ 0,
+ mShadowType,
+ line_length, getRect().getWidth(), NULL, mUseEllipses );
cur_pos += line_length + 1;
- y -= llfloor(mFontGL->getLineHeight()) + mLineSpacing;
+ S32 line_height = llfloor(mDefaultFont->getLineHeight()) + mLineSpacing;
+ y -= line_height;
+ if(y < line_height)
+ break;
}
}
}
void LLTextBox::reshapeToFitText()
{
+ // wrap remaining lines that did not fit on call to setWrappedText()
+ setLineLengths();
+
S32 width = getTextPixelWidth();
S32 height = getTextPixelHeight();
reshape( width + 2 * mHPad, height + 2 * mVPad );
}
-// virtual
-LLXMLNodePtr LLTextBox::getXML(bool save_children) const
+S32 LLTextBox::getDocIndexFromLocalCoord( S32 local_x, S32 local_y, BOOL round ) const
{
- LLXMLNodePtr node = LLUICtrl::getXML();
-
- // Attributes
- node->createChild("font", TRUE)->setStringValue(LLFontGL::nameFromFont(mFontGL));
- node->createChild("halign", TRUE)->setStringValue(LLFontGL::nameFromHAlign(mHAlign));
- addColorXML(node, mTextColor, "text_color", "LabelTextColor");
- addColorXML(node, mDisabledColor, "disabled_color", "LabelDisabledColor");
- addColorXML(node, mBackgroundColor, "bg_color", "DefaultBackgroundColor");
- addColorXML(node, mBorderColor, "border_color", "DefaultHighlightLight");
- node->createChild("bg_visible", TRUE)->setBoolValue(mBackgroundVisible);
- node->createChild("border_visible", TRUE)->setBoolValue(mBorderVisible);
- node->createChild("border_drop_shadow_visible", TRUE)->setBoolValue(mBorderDropShadowVisible);
- node->createChild("h_pad", TRUE)->setIntValue(mHPad);
- node->createChild("v_pad", TRUE)->setIntValue(mVPad);
-
- // Contents
- node->setStringValue(mText);
-
- return node;
+ // Returns the character offset for the character under the local (x, y) coordinate.
+ // When round is true, if the position is on the right half of a character, the cursor
+ // will be put to its right. If round is false, the cursor will always be put to the
+ // character's left.
+
+ LLRect rect = getLocalRect();
+ rect.mLeft += mHPad;
+ rect.mRight -= mHPad;
+ rect.mTop += mVPad;
+ rect.mBottom -= mVPad;
+
+ // Figure out which line we're nearest to.
+ S32 total_lines = getLineCount();
+ S32 line_height = llround( mDefaultFont->getLineHeight() ) + mLineSpacing;
+ S32 line = (rect.mTop - 1 - local_y) / line_height;
+ if (line >= total_lines)
+ {
+ return getLength(); // past the end
+ }
+
+ line = llclamp( line, 0, total_lines );
+ S32 line_start = getLineStart(line);
+ S32 next_start = getLineStart(line+1);
+ S32 line_end = (next_start != line_start) ? next_start - 1 : getLength();
+ if (line_start == -1)
+ {
+ return 0;
+ }
+
+ S32 line_len = line_end - line_start;
+ S32 pos = mDefaultFont->charFromPixelOffset(mDisplayText.c_str(), line_start,
+ (F32)(local_x - rect.mLeft),
+ (F32)rect.getWidth(),
+ line_len, round);
+
+ return line_start + pos;
}
-// static
-LLView* LLTextBox::fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory)
+S32 LLTextBox::getLineStart( S32 line ) const
{
- std::string name("text_box");
- node->getAttributeString("name", name);
- LLFontGL* font = LLView::selectFont(node);
+ line = llclamp(line, 0, getLineCount()-1);
- std::string text = node->getTextContents();
+ S32 result = 0;
+ for (int i = 0; i < line; i++)
+ {
+ result += mLineLengthList[i] + 1 /* add newline */;
+ }
+
+ return result;
+}
+
+void LLTextBox::updateDisplayTextAndSegments()
+{
+ // remove any previous segment list
+ clearSegments();
+
+ // if URL parsing is turned off, then not much to bo
+ if (! mParseHTML)
+ {
+ mDisplayText = mText.getWString();
+ setLineLengths();
+ return;
+ }
- LLTextBox* text_box = new LLTextBox(name,
- LLRect(),
- text,
- font,
- FALSE);
+ // create unique text segments for Urls
+ mDisplayText.clear();
+ S32 end = 0;
+ LLUrlMatch match;
+ LLWString text = mText.getWString();
+ // find the next Url in the text string
+ while ( LLUrlRegistry::instance().findUrl(text, match,
+ boost::bind(&LLTextBox::onUrlLabelUpdated, this, _1, _2)) )
+ {
+ // work out the char offset for the start/end of the url
+ S32 url_start = match.getStart();
+ S32 url_end = match.getEnd();
+
+ // and the char offset for the label in the display text
+ S32 seg_start = mDisplayText.size();
+ S32 start = seg_start + url_start;
+ S32 end = start + match.getLabel().size();
+
+ // create a segment for the text before the Url
+ mSegments.insert(new LLNormalTextSegment(new LLStyle(), seg_start, start, *this));
+ mDisplayText += text.substr(0, url_start);
+
+ // create a segment for the Url text
+ LLStyleSP html(new LLStyle);
+ html->setVisible(true);
+ html->setColor(mLinkColor);
+ html->mUnderline = TRUE;
+ html->setLinkHREF(match.getUrl());
+
+ LLNormalTextSegment *html_seg = new LLNormalTextSegment(html, start, end, *this);
+ html_seg->setToolTip(match.getTooltip());
+
+ mSegments.insert(html_seg);
+ mDisplayText += utf8str_to_wstring(match.getLabel());
+
+ // move on to the rest of the text after the Url
+ text = text.substr(url_end+1, text.size() - url_end);
+ }
+
+ // output a segment for the remaining text
+ if (text.size() > 0)
+ {
+ mSegments.insert(new LLNormalTextSegment(new LLStyle(), end, end + text.size(), *this));
+ mDisplayText += text;
+ }
- LLFontGL::HAlign halign = LLView::selectFontHAlign(node);
- text_box->setHAlign(halign);
+ // strip whitespace from the end of the text
+ while (mDisplayText.size() > 0 && isspace(mDisplayText[mDisplayText.size()-1]))
+ {
+ mDisplayText = mDisplayText.substr(0, mDisplayText.size() - 1);
- text_box->initFromXML(node, parent);
+ segment_set_t::iterator it = getSegIterContaining(mDisplayText.size());
+ if (it != mSegments.end())
+ {
+ LLTextSegmentPtr seg = *it;
+ seg->setEnd(seg->getEnd()-1);
+ }
+ }
- node->getAttributeS32("line_spacing", text_box->mLineSpacing);
+ // we may have changed the line lengths, so recalculate them
+ setLineLengths();
+}
- std::string font_style;
- if (node->getAttributeString("font-style", font_style))
+void LLTextBox::onUrlLabelUpdated(const std::string &url, const std::string &label)
+{
+ if (mDidWordWrap)
{
- text_box->mFontStyle = LLFontGL::getStyleFromString(font_style);
+ // re-word wrap as the url label lengths may have changed
+ setWrappedText(mText.getString());
}
-
- BOOL mouse_opaque = text_box->getMouseOpaque();
- if (node->getAttributeBOOL("mouse_opaque", mouse_opaque))
+ else
{
- text_box->setMouseOpaque(mouse_opaque);
- }
+ // or just update the display text with the latest Url labels
+ updateDisplayTextAndSegments();
+ }
+}
- if(node->hasAttribute("text_color"))
+bool LLTextBox::isClickable() const
+{
+ // return true if we have been given a click callback
+ if (mClickedCallback)
{
- LLColor4 color;
- LLUICtrlFactory::getAttributeColor(node, "text_color", color);
- text_box->setColor(color);
+ return true;
}
- if(node->hasAttribute("hover_color"))
+ // also return true if we have a clickable Url in the text
+ segment_set_t::const_iterator it;
+ for (it = mSegments.begin(); it != mSegments.end(); ++it)
{
- LLColor4 color;
- LLUICtrlFactory::getAttributeColor(node, "hover_color", color);
- text_box->setHoverColor(color);
- text_box->setHoverActive(true);
+ LLTextSegmentPtr segmentp = *it;
+ if (segmentp)
+ {
+ const LLStyleSP style = segmentp->getStyle();
+ if (style && style->isLink())
+ {
+ return true;
+ }
+ }
}
- BOOL hover_active = FALSE;
- if(node->getAttributeBOOL("hover", hover_active))
+ // otherwise there is nothing clickable here
+ return false;
+}
+
+void LLTextBox::drawTextSegments(S32 init_x, S32 init_y, const LLWString &text)
+{
+ F32 alpha = getDrawContext().mAlpha;
+
+ const S32 text_len = text.length();
+ if (text_len <= 0)
{
- text_box->setHoverActive(hover_active);
+ return;
}
- return text_box;
+ S32 cur_line = 0;
+ S32 num_lines = getLineCount();
+ S32 line_start = getLineStart(cur_line);
+ S32 line_height = llround( mDefaultFont->getLineHeight() ) + mLineSpacing;
+ F32 text_y = (F32) init_y;
+ segment_set_t::iterator cur_seg = mSegments.begin();
+
+ // render a line of text at a time
+ const LLRect textRect = getLocalRect();
+ while((textRect.mBottom <= text_y) && (cur_line < num_lines))
+ {
+ S32 next_start = -1;
+ S32 line_end = text_len;
+
+ if ((cur_line + 1) < num_lines)
+ {
+ next_start = getLineStart(cur_line + 1);
+ line_end = next_start;
+ }
+ if ( text[line_end-1] == '\n' )
+ {
+ --line_end;
+ }
+
+ // render all segments on this line
+ F32 text_x = init_x;
+ S32 seg_start = line_start;
+ while (seg_start < line_end && cur_seg != mSegments.end())
+ {
+ // move to the next segment (or continue the previous one)
+ LLTextSegment *cur_segment = *cur_seg;
+ while (cur_segment->getEnd() <= seg_start)
+ {
+ if (++cur_seg == mSegments.end())
+ {
+ return;
+ }
+ cur_segment = *cur_seg;
+ }
+
+ // Draw a segment within the line
+ S32 clipped_end = llmin( line_end, cur_segment->getEnd() );
+ S32 clipped_len = clipped_end - seg_start;
+ if( clipped_len > 0 )
+ {
+ LLStyleSP style = cur_segment->getStyle();
+ if (style && style->isVisible())
+ {
+ // work out the color for the segment
+ LLColor4 color ;
+ if (getEnabled())
+ {
+ color = style->isLink() ? mLinkColor.get() : mTextColor.get();
+ }
+ else
+ {
+ color = mDisabledColor.get();
+ }
+ color = color % alpha;
+
+ // render a single line worth for this segment
+ mDefaultFont->render(text, seg_start, text_x, text_y, color,
+ mHAlign, mVAlign, 0, mShadowType, clipped_len,
+ textRect.getWidth(), &text_x, mUseEllipses);
+ }
+
+ seg_start += clipped_len;
+ }
+ }
+
+ // move down one line
+ text_y -= (F32)line_height;
+ line_start = next_start;
+ cur_line++;
+ }
}