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
7 #ifndef __C_GUI_STATIC_TEXT_H_INCLUDED__
8 #define __C_GUI_STATIC_TEXT_H_INCLUDED__
10 #include "IrrCompileConfig.h"
11 #ifdef _IRR_COMPILE_WITH_GUI_
13 #include "IGUIStaticText.h"
20 #include "util/enriched_string.h"
22 #include <IGUIEnvironment.h>
32 const EGUI_ELEMENT_TYPE EGUIET_ENRICHED_STATIC_TEXT = (EGUI_ELEMENT_TYPE)(0x1000);
34 class StaticText : public IGUIStaticText
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 //! draws the element and its children
49 //! Sets another skin independent font.
50 virtual void setOverrideFont(IGUIFont* font=0);
52 //! Gets the override font (if any)
53 virtual IGUIFont* getOverrideFont() const;
55 //! Get the font which is used right now for drawing
56 virtual IGUIFont* getActiveFont() const;
58 //! Sets another color for the text.
59 virtual void setOverrideColor(video::SColor color);
61 //! Sets another color for the background.
62 virtual void setBackgroundColor(video::SColor color);
64 //! Sets whether to draw the background
65 virtual void setDrawBackground(bool draw);
67 //! Gets the background color
68 virtual video::SColor getBackgroundColor() const;
70 //! Checks if background drawing is enabled
71 virtual bool isDrawBackgroundEnabled() const;
73 //! Sets whether to draw the border
74 virtual void setDrawBorder(bool draw);
76 //! Checks if border drawing is enabled
77 virtual bool isDrawBorderEnabled() const;
79 //! Sets alignment mode for text
80 virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
82 //! Gets the override color
83 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
84 virtual const video::SColor& getOverrideColor() const;
86 virtual video::SColor getOverrideColor() const;
89 //! Sets if the static text should use the overide color or the
90 //! color in the gui skin.
91 virtual void enableOverrideColor(bool enable);
93 //! Checks if an override color is enabled
94 virtual bool isOverrideColorEnabled() const;
96 //! Set whether the text in this label should be clipped if it goes outside bounds
97 virtual void setTextRestrainedInside(bool restrainedInside);
99 //! Checks if the text in this label should be clipped if it goes outside bounds
100 virtual bool isTextRestrainedInside() const;
102 //! Enables or disables word wrap for using the static text as
103 //! multiline text control.
104 virtual void setWordWrap(bool enable);
106 //! Checks if word wrap is enabled
107 virtual bool isWordWrapEnabled() const;
109 //! Sets the new caption of this element.
110 virtual void setText(const wchar_t* text);
112 //! Returns the height of the text in pixels when it is drawn.
113 virtual s32 getTextHeight() const;
115 //! Returns the width of the current text, in the current font
116 virtual s32 getTextWidth() const;
118 //! Updates the absolute position, splits text if word wrap is enabled
119 virtual void updateAbsolutePosition();
121 //! Set whether the string should be interpreted as right-to-left (RTL) text
122 /** \note This component does not implement the Unicode bidi standard, the
123 text of the component should be already RTL if you call this. The
124 main difference when RTL is enabled is that the linebreaks for multiline
125 elements are performed starting from the end.
127 virtual void setRightToLeft(bool rtl);
129 //! Checks if the text should be interpreted as right-to-left text
130 virtual bool isRightToLeft() const;
132 //! Writes attributes of the element.
133 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
135 //! Reads attributes of the element
136 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
138 virtual bool hasType(EGUI_ELEMENT_TYPE t) const {
139 return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
142 virtual bool hasType(EGUI_ELEMENT_TYPE t) {
143 return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
146 void setText(const EnrichedString &text);
150 //! Breaks the single text line.
153 EGUI_ALIGNMENT HAlign, VAlign;
155 bool OverrideColorEnabled;
156 bool OverrideBGColorEnabled;
159 bool RestrainTextInside;
162 video::SColor OverrideColor, BGColor;
163 gui::IGUIFont* OverrideFont;
164 gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated.
166 EnrichedString cText;
167 core::array< EnrichedString > BrokenText;
171 } // end namespace gui
173 } // end namespace irr
175 inline irr::gui::IGUIStaticText *addStaticText(
176 irr::gui::IGUIEnvironment *guienv,
177 const EnrichedString &text,
178 const core::rect< s32 > &rectangle,
180 bool wordWrap = true,
181 irr::gui::IGUIElement *parent = NULL,
183 bool fillBackground = false)
185 if (parent == NULL) {
186 // parent is NULL, so we must find one, or we need not to drop
187 // result, but then there will be a memory leak.
189 // What Irrlicht does is to use guienv as a parent, but the problem
190 // is that guienv is here only an IGUIEnvironment, while it is a
191 // CGUIEnvironment in Irrlicht, which inherits from both IGUIElement
192 // and IGUIEnvironment.
194 // A solution would be to dynamic_cast guienv to a
195 // IGUIElement*, but Irrlicht is shipped without rtti support
196 // in some distributions, causing the dymanic_cast to segfault.
198 // Thus, to find the parent, we create a dummy StaticText and ask
199 // for its parent, and then remove it.
200 irr::gui::IGUIStaticText *dummy_text =
201 guienv->addStaticText(L"", rectangle, border, wordWrap,
202 parent, id, fillBackground);
203 parent = dummy_text->getParent();
204 dummy_text->remove();
206 irr::gui::IGUIStaticText *result = new irr::gui::StaticText(
207 text, border, guienv, parent,
208 id, rectangle, fillBackground);
210 result->setWordWrap(wordWrap);
215 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
217 // dynamic_cast not possible due to some distributions shipped
218 // without rtti support in irrlicht
219 if (static_text->hasType(irr::gui::EGUIET_ENRICHED_STATIC_TEXT)) {
220 irr::gui::StaticText* stext = static_cast<irr::gui::StaticText*>(static_text);
221 stext->setText(text);
223 static_text->setText(text.c_str());
227 #else // USE_FREETYPE
229 inline irr::gui::IGUIStaticText *addStaticText(
230 irr::gui::IGUIEnvironment *guienv,
231 const EnrichedString &text,
232 const core::rect< s32 > &rectangle,
234 bool wordWrap = true,
235 irr::gui::IGUIElement *parent = NULL,
237 bool fillBackground = false)
239 return guienv->addStaticText(text.c_str(), rectangle, border, wordWrap, parent, id, fillBackground);
242 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
244 static_text->setText(text.c_str());
249 inline irr::gui::IGUIStaticText *addStaticText(
250 irr::gui::IGUIEnvironment *guienv,
252 const core::rect< s32 > &rectangle,
254 bool wordWrap = true,
255 irr::gui::IGUIElement *parent = NULL,
257 bool fillBackground = false) {
258 return addStaticText(guienv, EnrichedString(text), rectangle, border, wordWrap, parent, id, fillBackground);
261 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const wchar_t *text)
263 setStaticText(static_text, EnrichedString(text));
266 #endif // _IRR_COMPILE_WITH_GUI_
268 #endif // C_GUI_STATIC_TEXT_H_INCLUDED