1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // Copyright (C) 2016 Nathanaƫl Courant
3 // Modified this class to work with EnrichedStrings too
4 // This file is part of the "Irrlicht Engine".
5 // For conditions of distribution and use, see copyright notice in irrlicht.h
9 #include "IrrCompileConfig.h"
10 #ifdef _IRR_COMPILE_WITH_GUI_
12 #include "IGUIStaticText.h"
19 #include "util/enriched_string.h"
21 #include <IGUIEnvironment.h>
31 const EGUI_ELEMENT_TYPE EGUIET_ENRICHED_STATIC_TEXT = (EGUI_ELEMENT_TYPE)(0x1000);
33 class StaticText : public IGUIStaticText
37 // StaticText is translated by EnrichedString.
38 // No need to use translate_string()
39 StaticText(const EnrichedString &text, bool border, IGUIEnvironment* environment,
40 IGUIElement* parent, s32 id, const core::rect<s32>& rectangle,
41 bool background = false);
44 virtual ~StaticText();
46 static irr::gui::IGUIStaticText *add(
47 irr::gui::IGUIEnvironment *guienv,
48 const EnrichedString &text,
49 const core::rect< s32 > &rectangle,
52 irr::gui::IGUIElement *parent = NULL,
54 bool fillBackground = false)
57 // parent is NULL, so we must find one, or we need not to drop
58 // result, but then there will be a memory leak.
60 // What Irrlicht does is to use guienv as a parent, but the problem
61 // is that guienv is here only an IGUIEnvironment, while it is a
62 // CGUIEnvironment in Irrlicht, which inherits from both IGUIElement
63 // and IGUIEnvironment.
65 // A solution would be to dynamic_cast guienv to a
66 // IGUIElement*, but Irrlicht is shipped without rtti support
67 // in some distributions, causing the dymanic_cast to segfault.
69 // Thus, to find the parent, we create a dummy StaticText and ask
70 // for its parent, and then remove it.
71 irr::gui::IGUIStaticText *dummy_text =
72 guienv->addStaticText(L"", rectangle, border, wordWrap,
73 parent, id, fillBackground);
74 parent = dummy_text->getParent();
77 irr::gui::IGUIStaticText *result = new irr::gui::StaticText(
78 text, border, guienv, parent,
79 id, rectangle, fillBackground);
81 result->setWordWrap(wordWrap);
86 static irr::gui::IGUIStaticText *add(
87 irr::gui::IGUIEnvironment *guienv,
89 const core::rect< s32 > &rectangle,
92 irr::gui::IGUIElement *parent = NULL,
94 bool fillBackground = false)
96 return add(guienv, EnrichedString(text), rectangle, border, wordWrap, parent,
100 //! draws the element and its children
103 //! Sets another skin independent font.
104 virtual void setOverrideFont(IGUIFont* font=0);
106 //! Gets the override font (if any)
107 virtual IGUIFont* getOverrideFont() const;
109 //! Get the font which is used right now for drawing
110 virtual IGUIFont* getActiveFont() const;
112 //! Sets another color for the text.
113 virtual void setOverrideColor(video::SColor color);
115 //! Sets another color for the background.
116 virtual void setBackgroundColor(video::SColor color);
118 //! Sets whether to draw the background
119 virtual void setDrawBackground(bool draw);
121 //! Gets the background color
122 virtual video::SColor getBackgroundColor() const;
124 //! Checks if background drawing is enabled
125 virtual bool isDrawBackgroundEnabled() const;
127 //! Sets whether to draw the border
128 virtual void setDrawBorder(bool draw);
130 //! Checks if border drawing is enabled
131 virtual bool isDrawBorderEnabled() const;
133 //! Sets alignment mode for text
134 virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
136 //! Gets the override color
137 virtual video::SColor getOverrideColor() const;
139 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
140 //! Gets the currently used text color
141 virtual video::SColor getActiveColor() const;
144 //! Sets if the static text should use the overide color or the
145 //! color in the gui skin.
146 virtual void enableOverrideColor(bool enable);
148 //! Checks if an override color is enabled
149 virtual bool isOverrideColorEnabled() const;
151 //! Set whether the text in this label should be clipped if it goes outside bounds
152 virtual void setTextRestrainedInside(bool restrainedInside);
154 //! Checks if the text in this label should be clipped if it goes outside bounds
155 virtual bool isTextRestrainedInside() const;
157 //! Enables or disables word wrap for using the static text as
158 //! multiline text control.
159 virtual void setWordWrap(bool enable);
161 //! Checks if word wrap is enabled
162 virtual bool isWordWrapEnabled() const;
164 //! Sets the new caption of this element.
165 virtual void setText(const wchar_t* text);
167 //! Returns the height of the text in pixels when it is drawn.
168 virtual s32 getTextHeight() const;
170 //! Returns the width of the current text, in the current font
171 virtual s32 getTextWidth() const;
173 //! Updates the absolute position, splits text if word wrap is enabled
174 virtual void updateAbsolutePosition();
176 //! Set whether the string should be interpreted as right-to-left (RTL) text
177 /** \note This component does not implement the Unicode bidi standard, the
178 text of the component should be already RTL if you call this. The
179 main difference when RTL is enabled is that the linebreaks for multiline
180 elements are performed starting from the end.
182 virtual void setRightToLeft(bool rtl);
184 //! Checks if the text should be interpreted as right-to-left text
185 virtual bool isRightToLeft() const;
187 virtual bool hasType(EGUI_ELEMENT_TYPE t) const {
188 return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
191 virtual bool hasType(EGUI_ELEMENT_TYPE t) {
192 return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
195 void setText(const EnrichedString &text);
199 //! Breaks the single text line.
202 EGUI_ALIGNMENT HAlign, VAlign;
206 bool RestrainTextInside;
209 gui::IGUIFont* OverrideFont;
210 gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated.
212 EnrichedString ColoredText;
213 std::vector<EnrichedString> BrokenText;
217 } // end namespace gui
219 } // end namespace irr
221 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
223 // dynamic_cast not possible due to some distributions shipped
224 // without rtti support in irrlicht
225 if (static_text->hasType(irr::gui::EGUIET_ENRICHED_STATIC_TEXT)) {
226 irr::gui::StaticText* stext = static_cast<irr::gui::StaticText*>(static_text);
227 stext->setText(text);
229 static_text->setText(text.c_str());
233 #else // USE_FREETYPE
243 static irr::gui::IGUIStaticText *add(
244 irr::gui::IGUIEnvironment *guienv,
245 const EnrichedString &text,
246 const core::rect< s32 > &rectangle,
248 bool wordWrap = true,
249 irr::gui::IGUIElement *parent = NULL,
251 bool fillBackground = false)
253 return guienv->addStaticText(text.c_str(), rectangle, border, wordWrap, parent, id, fillBackground);
257 } // end namespace gui
259 } // end namespace irr
261 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
263 static_text->setText(text.c_str());
268 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const wchar_t *text)
270 setStaticText(static_text, EnrichedString(text, static_text->getOverrideColor()));
273 #endif // _IRR_COMPILE_WITH_GUI_