1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "CGUIStaticText.h"
\r
7 #include "IGUISkin.h"
\r
8 #include "IGUIEnvironment.h"
\r
9 #include "IGUIFont.h"
\r
10 #include "IVideoDriver.h"
\r
19 CGUIStaticText::CGUIStaticText(const wchar_t* text, bool border,
\r
20 IGUIEnvironment* environment, IGUIElement* parent,
\r
21 s32 id, const core::rect<s32>& rectangle,
\r
23 : IGUIStaticText(environment, parent, id, rectangle),
\r
24 HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_UPPERLEFT),
\r
25 Border(border), OverrideColorEnabled(false), OverrideBGColorEnabled(false), WordWrap(false), Background(background),
\r
26 RestrainTextInside(true), RightToLeft(false),
\r
27 OverrideColor(video::SColor(101,255,255,255)), BGColor(video::SColor(101,210,210,210)),
\r
28 OverrideFont(0), LastBreakFont(0)
\r
31 setDebugName("CGUIStaticText");
\r
35 if (environment && environment->getSkin())
\r
37 BGColor = environment->getSkin()->getColor(gui::EGDC_3D_FACE);
\r
43 CGUIStaticText::~CGUIStaticText()
\r
46 OverrideFont->drop();
\r
50 //! draws the element and its children
\r
51 void CGUIStaticText::draw()
\r
56 IGUISkin* skin = Environment->getSkin();
\r
59 video::IVideoDriver* driver = Environment->getVideoDriver();
\r
61 core::rect<s32> frameRect(AbsoluteRect);
\r
67 if ( !OverrideBGColorEnabled ) // skin-colors can change
\r
68 BGColor = skin->getColor(gui::EGDC_3D_FACE);
\r
70 driver->draw2DRectangle(BGColor, frameRect, &AbsoluteClippingRect);
\r
77 skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
\r
78 frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
\r
84 IGUIFont* font = getActiveFont();
\r
90 if (VAlign == EGUIA_LOWERRIGHT)
\r
92 frameRect.UpperLeftCorner.Y = frameRect.LowerRightCorner.Y -
\r
93 font->getDimension(L"A").Height - font->getKerningHeight();
\r
95 if (HAlign == EGUIA_LOWERRIGHT)
\r
97 frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
\r
98 font->getDimension(Text.c_str()).Width;
\r
101 font->draw(Text.c_str(), frameRect,
\r
103 HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER, (RestrainTextInside ? &AbsoluteClippingRect : NULL));
\r
107 if (font != LastBreakFont)
\r
110 core::rect<s32> r = frameRect;
\r
111 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
\r
112 s32 totalHeight = height * BrokenText.size();
\r
113 if (VAlign == EGUIA_CENTER)
\r
115 r.UpperLeftCorner.Y = r.getCenter().Y - (totalHeight / 2);
\r
117 else if (VAlign == EGUIA_LOWERRIGHT)
\r
119 r.UpperLeftCorner.Y = r.LowerRightCorner.Y - totalHeight;
\r
122 for (u32 i=0; i<BrokenText.size(); ++i)
\r
124 if (HAlign == EGUIA_LOWERRIGHT)
\r
126 r.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
\r
127 font->getDimension(BrokenText[i].c_str()).Width;
\r
130 font->draw(BrokenText[i].c_str(), r,
\r
132 HAlign == EGUIA_CENTER, false, (RestrainTextInside ? &AbsoluteClippingRect : NULL));
\r
134 r.LowerRightCorner.Y += height;
\r
135 r.UpperLeftCorner.Y += height;
\r
141 IGUIElement::draw();
\r
145 //! Sets another skin independent font.
\r
146 void CGUIStaticText::setOverrideFont(IGUIFont* font)
\r
148 if (OverrideFont == font)
\r
152 OverrideFont->drop();
\r
154 OverrideFont = font;
\r
157 OverrideFont->grab();
\r
162 //! Gets the override font (if any)
\r
163 IGUIFont * CGUIStaticText::getOverrideFont() const
\r
165 return OverrideFont;
\r
168 //! Get the font which is used right now for drawing
\r
169 IGUIFont* CGUIStaticText::getActiveFont() const
\r
171 if ( OverrideFont )
\r
172 return OverrideFont;
\r
173 IGUISkin* skin = Environment->getSkin();
\r
175 return skin->getFont();
\r
179 //! Sets another color for the text.
\r
180 void CGUIStaticText::setOverrideColor(video::SColor color)
\r
182 OverrideColor = color;
\r
183 OverrideColorEnabled = true;
\r
187 //! Sets another color for the text.
\r
188 void CGUIStaticText::setBackgroundColor(video::SColor color)
\r
191 OverrideBGColorEnabled = true;
\r
196 //! Sets whether to draw the background
\r
197 void CGUIStaticText::setDrawBackground(bool draw)
\r
203 //! Gets the background color
\r
204 video::SColor CGUIStaticText::getBackgroundColor() const
\r
210 //! Checks if background drawing is enabled
\r
211 bool CGUIStaticText::isDrawBackgroundEnabled() const
\r
217 //! Sets whether to draw the border
\r
218 void CGUIStaticText::setDrawBorder(bool draw)
\r
224 //! Checks if border drawing is enabled
\r
225 bool CGUIStaticText::isDrawBorderEnabled() const
\r
231 void CGUIStaticText::setTextRestrainedInside(bool restrainTextInside)
\r
233 RestrainTextInside = restrainTextInside;
\r
237 bool CGUIStaticText::isTextRestrainedInside() const
\r
239 return RestrainTextInside;
\r
243 void CGUIStaticText::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
\r
245 HAlign = horizontal;
\r
250 video::SColor CGUIStaticText::getOverrideColor() const
\r
252 return OverrideColor;
\r
256 irr::video::SColor CGUIStaticText::getActiveColor() const
\r
258 if ( OverrideColorEnabled )
\r
259 return OverrideColor;
\r
260 IGUISkin* skin = Environment->getSkin();
\r
262 return OverrideColorEnabled ? OverrideColor : skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT);
\r
263 return OverrideColor;
\r
266 //! Sets if the static text should use the override color or the
\r
267 //! color in the gui skin.
\r
268 void CGUIStaticText::enableOverrideColor(bool enable)
\r
270 OverrideColorEnabled = enable;
\r
274 bool CGUIStaticText::isOverrideColorEnabled() const
\r
276 return OverrideColorEnabled;
\r
280 //! Enables or disables word wrap for using the static text as
\r
281 //! multiline text control.
\r
282 void CGUIStaticText::setWordWrap(bool enable)
\r
289 bool CGUIStaticText::isWordWrapEnabled() const
\r
295 void CGUIStaticText::setRightToLeft(bool rtl)
\r
297 if (RightToLeft != rtl)
\r
305 bool CGUIStaticText::isRightToLeft() const
\r
307 return RightToLeft;
\r
311 //! Breaks the single text line.
\r
312 void CGUIStaticText::breakText()
\r
317 BrokenText.clear();
\r
319 IGUISkin* skin = Environment->getSkin();
\r
320 IGUIFont* font = getActiveFont();
\r
324 LastBreakFont = font;
\r
326 core::stringw line;
\r
327 core::stringw word;
\r
328 core::stringw whitespace;
\r
329 s32 size = Text.size();
\r
331 s32 elWidth = RelativeRect.getWidth();
\r
333 elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X);
\r
336 // We have to deal with right-to-left and left-to-right differently
\r
337 // However, most parts of the following code is the same, it's just
\r
338 // some order and boundaries which change.
\r
341 // regular (left-to-right)
\r
342 for (s32 i=0; i<size; ++i)
\r
345 bool lineBreak = false;
\r
347 if (c == L'\r') // Mac or Windows breaks
\r
350 if (Text[i+1] == L'\n') // Windows breaks
\r
357 else if (c == L'\n') // Unix breaks
\r
363 bool isWhitespace = (c == L' ' || c == 0);
\r
364 if ( !isWhitespace )
\r
370 if ( isWhitespace || i == (size-1))
\r
374 // here comes the next whitespace, look if
\r
375 // we must break the last word to the next line.
\r
376 const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
\r
377 const s32 wordlgth = font->getDimension(word.c_str()).Width;
\r
379 if (wordlgth > elWidth)
\r
381 // This word is too long to fit in the available space, look for
\r
382 // the Unicode Soft HYphen (SHY / 00AD) character for a place to
\r
383 // break the word at
\r
384 int where = word.findFirst( wchar_t(0x00AD) );
\r
387 core::stringw first = word.subString(0, where);
\r
388 core::stringw second = word.subString(where, word.size() - where);
\r
389 BrokenText.push_back(line + first + L"-");
\r
390 const s32 secondLength = font->getDimension(second.c_str()).Width;
\r
392 length = secondLength;
\r
397 // No soft hyphen found, so there's nothing more we can do
\r
398 // break to next line
\r
400 BrokenText.push_back(line);
\r
405 else if (length && (length + wordlgth + whitelgth > elWidth))
\r
407 // break to next line
\r
408 BrokenText.push_back(line);
\r
414 // add word to line
\r
415 line += whitespace;
\r
417 length += whitelgth + wordlgth;
\r
424 if ( isWhitespace )
\r
429 // compute line break
\r
432 line += whitespace;
\r
434 BrokenText.push_back(line);
\r
443 line += whitespace;
\r
445 BrokenText.push_back(line);
\r
450 for (s32 i=size; i>=0; --i)
\r
453 bool lineBreak = false;
\r
455 if (c == L'\r') // Mac or Windows breaks
\r
458 if ((i>0) && Text[i-1] == L'\n') // Windows breaks
\r
465 else if (c == L'\n') // Unix breaks
\r
471 if (c==L' ' || c==0 || i==0)
\r
475 // here comes the next whitespace, look if
\r
476 // we must break the last word to the next line.
\r
477 const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
\r
478 const s32 wordlgth = font->getDimension(word.c_str()).Width;
\r
480 if (length && (length + wordlgth + whitelgth > elWidth))
\r
482 // break to next line
\r
483 BrokenText.push_back(line);
\r
489 // add word to line
\r
490 line = whitespace + line;
\r
491 line = word + line;
\r
492 length += whitelgth + wordlgth;
\r
500 whitespace = core::stringw(&c, 1) + whitespace;
\r
502 // compute line break
\r
505 line = whitespace + line;
\r
506 line = word + line;
\r
507 BrokenText.push_back(line);
\r
516 // yippee this is a word..
\r
517 word = core::stringw(&c, 1) + word;
\r
521 line = whitespace + line;
\r
522 line = word + line;
\r
523 BrokenText.push_back(line);
\r
528 //! Sets the new caption of this element.
\r
529 void CGUIStaticText::setText(const wchar_t* text)
\r
531 IGUIElement::setText(text);
\r
536 void CGUIStaticText::updateAbsolutePosition()
\r
538 IGUIElement::updateAbsolutePosition();
\r
543 //! Returns the height of the text in pixels when it is drawn.
\r
544 s32 CGUIStaticText::getTextHeight() const
\r
546 IGUIFont* font = getActiveFont();
\r
552 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
\r
553 return height* BrokenText.size();
\r
557 // TODO: Text can have multiple lines which are not in BrokenText
\r
558 // This is likely not correct. But as I have no time for further
\r
559 // investigation I just fix it for now by return the true height here.
\r
560 return font->getDimension(Text.c_str()).Height;
\r
565 s32 CGUIStaticText::getTextWidth() const
\r
567 IGUIFont * font = getActiveFont();
\r
575 for(u32 line = 0; line < BrokenText.size(); ++line)
\r
577 s32 width = font->getDimension(BrokenText[line].c_str()).Width;
\r
587 return font->getDimension(Text.c_str()).Width;
\r
592 } // end namespace gui
\r
593 } // end namespace irr
\r