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
6 #ifdef _IRR_COMPILE_WITH_GUI_
\r
8 #include "IGUISkin.h"
\r
9 #include "IGUIEnvironment.h"
\r
10 #include "IGUIFont.h"
\r
11 #include "IVideoDriver.h"
\r
20 CGUIStaticText::CGUIStaticText(const wchar_t* text, bool border,
\r
21 IGUIEnvironment* environment, IGUIElement* parent,
\r
22 s32 id, const core::rect<s32>& rectangle,
\r
24 : IGUIStaticText(environment, parent, id, rectangle),
\r
25 HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_UPPERLEFT),
\r
26 Border(border), OverrideColorEnabled(false), OverrideBGColorEnabled(false), WordWrap(false), Background(background),
\r
27 RestrainTextInside(true), RightToLeft(false),
\r
28 OverrideColor(video::SColor(101,255,255,255)), BGColor(video::SColor(101,210,210,210)),
\r
29 OverrideFont(0), LastBreakFont(0)
\r
32 setDebugName("CGUIStaticText");
\r
36 if (environment && environment->getSkin())
\r
38 BGColor = environment->getSkin()->getColor(gui::EGDC_3D_FACE);
\r
44 CGUIStaticText::~CGUIStaticText()
\r
47 OverrideFont->drop();
\r
51 //! draws the element and its children
\r
52 void CGUIStaticText::draw()
\r
57 IGUISkin* skin = Environment->getSkin();
\r
60 video::IVideoDriver* driver = Environment->getVideoDriver();
\r
62 core::rect<s32> frameRect(AbsoluteRect);
\r
68 if ( !OverrideBGColorEnabled ) // skin-colors can change
\r
69 BGColor = skin->getColor(gui::EGDC_3D_FACE);
\r
71 driver->draw2DRectangle(BGColor, frameRect, &AbsoluteClippingRect);
\r
78 skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
\r
79 frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
\r
85 IGUIFont* font = getActiveFont();
\r
91 if (VAlign == EGUIA_LOWERRIGHT)
\r
93 frameRect.UpperLeftCorner.Y = frameRect.LowerRightCorner.Y -
\r
94 font->getDimension(L"A").Height - font->getKerningHeight();
\r
96 if (HAlign == EGUIA_LOWERRIGHT)
\r
98 frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
\r
99 font->getDimension(Text.c_str()).Width;
\r
102 font->draw(Text.c_str(), frameRect,
\r
104 HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER, (RestrainTextInside ? &AbsoluteClippingRect : NULL));
\r
108 if (font != LastBreakFont)
\r
111 core::rect<s32> r = frameRect;
\r
112 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
\r
113 s32 totalHeight = height * BrokenText.size();
\r
114 if (VAlign == EGUIA_CENTER)
\r
116 r.UpperLeftCorner.Y = r.getCenter().Y - (totalHeight / 2);
\r
118 else if (VAlign == EGUIA_LOWERRIGHT)
\r
120 r.UpperLeftCorner.Y = r.LowerRightCorner.Y - totalHeight;
\r
123 for (u32 i=0; i<BrokenText.size(); ++i)
\r
125 if (HAlign == EGUIA_LOWERRIGHT)
\r
127 r.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
\r
128 font->getDimension(BrokenText[i].c_str()).Width;
\r
131 font->draw(BrokenText[i].c_str(), r,
\r
133 HAlign == EGUIA_CENTER, false, (RestrainTextInside ? &AbsoluteClippingRect : NULL));
\r
135 r.LowerRightCorner.Y += height;
\r
136 r.UpperLeftCorner.Y += height;
\r
142 IGUIElement::draw();
\r
146 //! Sets another skin independent font.
\r
147 void CGUIStaticText::setOverrideFont(IGUIFont* font)
\r
149 if (OverrideFont == font)
\r
153 OverrideFont->drop();
\r
155 OverrideFont = font;
\r
158 OverrideFont->grab();
\r
163 //! Gets the override font (if any)
\r
164 IGUIFont * CGUIStaticText::getOverrideFont() const
\r
166 return OverrideFont;
\r
169 //! Get the font which is used right now for drawing
\r
170 IGUIFont* CGUIStaticText::getActiveFont() const
\r
172 if ( OverrideFont )
\r
173 return OverrideFont;
\r
174 IGUISkin* skin = Environment->getSkin();
\r
176 return skin->getFont();
\r
180 //! Sets another color for the text.
\r
181 void CGUIStaticText::setOverrideColor(video::SColor color)
\r
183 OverrideColor = color;
\r
184 OverrideColorEnabled = true;
\r
188 //! Sets another color for the text.
\r
189 void CGUIStaticText::setBackgroundColor(video::SColor color)
\r
192 OverrideBGColorEnabled = true;
\r
197 //! Sets whether to draw the background
\r
198 void CGUIStaticText::setDrawBackground(bool draw)
\r
204 //! Gets the background color
\r
205 video::SColor CGUIStaticText::getBackgroundColor() const
\r
211 //! Checks if background drawing is enabled
\r
212 bool CGUIStaticText::isDrawBackgroundEnabled() const
\r
218 //! Sets whether to draw the border
\r
219 void CGUIStaticText::setDrawBorder(bool draw)
\r
225 //! Checks if border drawing is enabled
\r
226 bool CGUIStaticText::isDrawBorderEnabled() const
\r
232 void CGUIStaticText::setTextRestrainedInside(bool restrainTextInside)
\r
234 RestrainTextInside = restrainTextInside;
\r
238 bool CGUIStaticText::isTextRestrainedInside() const
\r
240 return RestrainTextInside;
\r
244 void CGUIStaticText::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
\r
246 HAlign = horizontal;
\r
251 video::SColor CGUIStaticText::getOverrideColor() const
\r
253 return OverrideColor;
\r
257 irr::video::SColor CGUIStaticText::getActiveColor() const
\r
259 if ( OverrideColorEnabled )
\r
260 return OverrideColor;
\r
261 IGUISkin* skin = Environment->getSkin();
\r
263 return OverrideColorEnabled ? OverrideColor : skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT);
\r
264 return OverrideColor;
\r
267 //! Sets if the static text should use the override color or the
\r
268 //! color in the gui skin.
\r
269 void CGUIStaticText::enableOverrideColor(bool enable)
\r
271 OverrideColorEnabled = enable;
\r
275 bool CGUIStaticText::isOverrideColorEnabled() const
\r
277 return OverrideColorEnabled;
\r
281 //! Enables or disables word wrap for using the static text as
\r
282 //! multiline text control.
\r
283 void CGUIStaticText::setWordWrap(bool enable)
\r
290 bool CGUIStaticText::isWordWrapEnabled() const
\r
296 void CGUIStaticText::setRightToLeft(bool rtl)
\r
298 if (RightToLeft != rtl)
\r
306 bool CGUIStaticText::isRightToLeft() const
\r
308 return RightToLeft;
\r
312 //! Breaks the single text line.
\r
313 void CGUIStaticText::breakText()
\r
318 BrokenText.clear();
\r
320 IGUISkin* skin = Environment->getSkin();
\r
321 IGUIFont* font = getActiveFont();
\r
325 LastBreakFont = font;
\r
327 core::stringw line;
\r
328 core::stringw word;
\r
329 core::stringw whitespace;
\r
330 s32 size = Text.size();
\r
332 s32 elWidth = RelativeRect.getWidth();
\r
334 elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X);
\r
337 // We have to deal with right-to-left and left-to-right differently
\r
338 // However, most parts of the following code is the same, it's just
\r
339 // some order and boundaries which change.
\r
342 // regular (left-to-right)
\r
343 for (s32 i=0; i<size; ++i)
\r
346 bool lineBreak = false;
\r
348 if (c == L'\r') // Mac or Windows breaks
\r
351 if (Text[i+1] == L'\n') // Windows breaks
\r
358 else if (c == L'\n') // Unix breaks
\r
364 bool isWhitespace = (c == L' ' || c == 0);
\r
365 if ( !isWhitespace )
\r
371 if ( isWhitespace || i == (size-1))
\r
375 // here comes the next whitespace, look if
\r
376 // we must break the last word to the next line.
\r
377 const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
\r
378 const s32 wordlgth = font->getDimension(word.c_str()).Width;
\r
380 if (wordlgth > elWidth)
\r
382 // This word is too long to fit in the available space, look for
\r
383 // the Unicode Soft HYphen (SHY / 00AD) character for a place to
\r
384 // break the word at
\r
385 int where = word.findFirst( wchar_t(0x00AD) );
\r
388 core::stringw first = word.subString(0, where);
\r
389 core::stringw second = word.subString(where, word.size() - where);
\r
390 BrokenText.push_back(line + first + L"-");
\r
391 const s32 secondLength = font->getDimension(second.c_str()).Width;
\r
393 length = secondLength;
\r
398 // No soft hyphen found, so there's nothing more we can do
\r
399 // break to next line
\r
401 BrokenText.push_back(line);
\r
406 else if (length && (length + wordlgth + whitelgth > elWidth))
\r
408 // break to next line
\r
409 BrokenText.push_back(line);
\r
415 // add word to line
\r
416 line += whitespace;
\r
418 length += whitelgth + wordlgth;
\r
425 if ( isWhitespace )
\r
430 // compute line break
\r
433 line += whitespace;
\r
435 BrokenText.push_back(line);
\r
444 line += whitespace;
\r
446 BrokenText.push_back(line);
\r
451 for (s32 i=size; i>=0; --i)
\r
454 bool lineBreak = false;
\r
456 if (c == L'\r') // Mac or Windows breaks
\r
459 if ((i>0) && Text[i-1] == L'\n') // Windows breaks
\r
466 else if (c == L'\n') // Unix breaks
\r
472 if (c==L' ' || c==0 || i==0)
\r
476 // here comes the next whitespace, look if
\r
477 // we must break the last word to the next line.
\r
478 const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
\r
479 const s32 wordlgth = font->getDimension(word.c_str()).Width;
\r
481 if (length && (length + wordlgth + whitelgth > elWidth))
\r
483 // break to next line
\r
484 BrokenText.push_back(line);
\r
490 // add word to line
\r
491 line = whitespace + line;
\r
492 line = word + line;
\r
493 length += whitelgth + wordlgth;
\r
501 whitespace = core::stringw(&c, 1) + whitespace;
\r
503 // compute line break
\r
506 line = whitespace + line;
\r
507 line = word + line;
\r
508 BrokenText.push_back(line);
\r
517 // yippee this is a word..
\r
518 word = core::stringw(&c, 1) + word;
\r
522 line = whitespace + line;
\r
523 line = word + line;
\r
524 BrokenText.push_back(line);
\r
529 //! Sets the new caption of this element.
\r
530 void CGUIStaticText::setText(const wchar_t* text)
\r
532 IGUIElement::setText(text);
\r
537 void CGUIStaticText::updateAbsolutePosition()
\r
539 IGUIElement::updateAbsolutePosition();
\r
544 //! Returns the height of the text in pixels when it is drawn.
\r
545 s32 CGUIStaticText::getTextHeight() const
\r
547 IGUIFont* font = getActiveFont();
\r
553 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
\r
554 return height* BrokenText.size();
\r
558 // TODO: Text can have multiple lines which are not in BrokenText
\r
559 // This is likely not correct. But as I have no time for further
\r
560 // investigation I just fix it for now by return the true height here.
\r
561 return font->getDimension(Text.c_str()).Height;
\r
566 s32 CGUIStaticText::getTextWidth() const
\r
568 IGUIFont * font = getActiveFont();
\r
576 for(u32 line = 0; line < BrokenText.size(); ++line)
\r
578 s32 width = font->getDimension(BrokenText[line].c_str()).Width;
\r
588 return font->getDimension(Text.c_str()).Width;
\r
593 } // end namespace gui
\r
594 } // end namespace irr
\r
596 #endif // _IRR_COMPILE_WITH_GUI_
\r