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
38 StaticText(const EnrichedString &text, bool border, IGUIEnvironment* environment,
39 IGUIElement* parent, s32 id, const core::rect<s32>& rectangle,
40 bool background = false);
43 virtual ~StaticText();
45 static irr::gui::IGUIStaticText *add(
46 irr::gui::IGUIEnvironment *guienv,
47 const EnrichedString &text,
48 const core::rect< s32 > &rectangle,
51 irr::gui::IGUIElement *parent = NULL,
53 bool fillBackground = false)
56 // parent is NULL, so we must find one, or we need not to drop
57 // result, but then there will be a memory leak.
59 // What Irrlicht does is to use guienv as a parent, but the problem
60 // is that guienv is here only an IGUIEnvironment, while it is a
61 // CGUIEnvironment in Irrlicht, which inherits from both IGUIElement
62 // and IGUIEnvironment.
64 // A solution would be to dynamic_cast guienv to a
65 // IGUIElement*, but Irrlicht is shipped without rtti support
66 // in some distributions, causing the dymanic_cast to segfault.
68 // Thus, to find the parent, we create a dummy StaticText and ask
69 // for its parent, and then remove it.
70 irr::gui::IGUIStaticText *dummy_text =
71 guienv->addStaticText(L"", rectangle, border, wordWrap,
72 parent, id, fillBackground);
73 parent = dummy_text->getParent();
76 irr::gui::IGUIStaticText *result = new irr::gui::StaticText(
77 text, border, guienv, parent,
78 id, rectangle, fillBackground);
80 result->setWordWrap(wordWrap);
85 static irr::gui::IGUIStaticText *add(
86 irr::gui::IGUIEnvironment *guienv,
88 const core::rect< s32 > &rectangle,
91 irr::gui::IGUIElement *parent = NULL,
93 bool fillBackground = false)
95 return add(guienv, EnrichedString(text), rectangle, border, wordWrap, parent,
99 //! draws the element and its children
102 //! Sets another skin independent font.
103 virtual void setOverrideFont(IGUIFont* font=0);
105 //! Gets the override font (if any)
106 virtual IGUIFont* getOverrideFont() const;
108 //! Get the font which is used right now for drawing
109 virtual IGUIFont* getActiveFont() const;
111 //! Sets another color for the text.
112 virtual void setOverrideColor(video::SColor color);
114 //! Sets another color for the background.
115 virtual void setBackgroundColor(video::SColor color);
117 //! Sets whether to draw the background
118 virtual void setDrawBackground(bool draw);
120 //! Gets the background color
121 virtual video::SColor getBackgroundColor() const;
123 //! Checks if background drawing is enabled
124 virtual bool isDrawBackgroundEnabled() const;
126 //! Sets whether to draw the border
127 virtual void setDrawBorder(bool draw);
129 //! Checks if border drawing is enabled
130 virtual bool isDrawBorderEnabled() const;
132 //! Sets alignment mode for text
133 virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
135 //! Gets the override color
136 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
137 virtual const video::SColor& getOverrideColor() const;
139 virtual video::SColor getOverrideColor() const;
142 //! Sets if the static text should use the overide color or the
143 //! color in the gui skin.
144 virtual void enableOverrideColor(bool enable);
146 //! Checks if an override color is enabled
147 virtual bool isOverrideColorEnabled() const;
149 //! Set whether the text in this label should be clipped if it goes outside bounds
150 virtual void setTextRestrainedInside(bool restrainedInside);
152 //! Checks if the text in this label should be clipped if it goes outside bounds
153 virtual bool isTextRestrainedInside() const;
155 //! Enables or disables word wrap for using the static text as
156 //! multiline text control.
157 virtual void setWordWrap(bool enable);
159 //! Checks if word wrap is enabled
160 virtual bool isWordWrapEnabled() const;
162 //! Sets the new caption of this element.
163 virtual void setText(const wchar_t* text);
165 //! Returns the height of the text in pixels when it is drawn.
166 virtual s32 getTextHeight() const;
168 //! Returns the width of the current text, in the current font
169 virtual s32 getTextWidth() const;
171 //! Updates the absolute position, splits text if word wrap is enabled
172 virtual void updateAbsolutePosition();
174 //! Set whether the string should be interpreted as right-to-left (RTL) text
175 /** \note This component does not implement the Unicode bidi standard, the
176 text of the component should be already RTL if you call this. The
177 main difference when RTL is enabled is that the linebreaks for multiline
178 elements are performed starting from the end.
180 virtual void setRightToLeft(bool rtl);
182 //! Checks if the text should be interpreted as right-to-left text
183 virtual bool isRightToLeft() const;
185 //! Writes attributes of the element.
186 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
188 //! Reads attributes of the element
189 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
191 virtual bool hasType(EGUI_ELEMENT_TYPE t) const {
192 return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
195 virtual bool hasType(EGUI_ELEMENT_TYPE t) {
196 return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
199 void setText(const EnrichedString &text);
203 //! Breaks the single text line.
206 EGUI_ALIGNMENT HAlign, VAlign;
208 bool OverrideColorEnabled;
209 bool OverrideBGColorEnabled;
212 bool RestrainTextInside;
215 video::SColor OverrideColor, BGColor;
216 gui::IGUIFont* OverrideFont;
217 gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated.
219 EnrichedString cText;
220 core::array< EnrichedString > BrokenText;
224 } // end namespace gui
226 } // end namespace irr
228 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
230 // dynamic_cast not possible due to some distributions shipped
231 // without rtti support in irrlicht
232 if (static_text->hasType(irr::gui::EGUIET_ENRICHED_STATIC_TEXT)) {
233 irr::gui::StaticText* stext = static_cast<irr::gui::StaticText*>(static_text);
234 stext->setText(text);
236 static_text->setText(text.c_str());
240 #else // USE_FREETYPE
242 inline irr::gui::IGUIStaticText *addStaticText(
243 irr::gui::IGUIEnvironment *guienv,
244 const EnrichedString &text,
245 const core::rect< s32 > &rectangle,
247 bool wordWrap = true,
248 irr::gui::IGUIElement *parent = NULL,
250 bool fillBackground = false)
252 return guienv->addStaticText(text.c_str(), rectangle, border, wordWrap, parent, id, fillBackground);
255 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
257 static_text->setText(text.c_str());
262 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const wchar_t *text)
264 setStaticText(static_text, EnrichedString(text));
267 #endif // _IRR_COMPILE_WITH_GUI_