1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __C_GUI_EDIT_BOX_H_INCLUDED__
6 #define __C_GUI_EDIT_BOX_H_INCLUDED__
7 
8 #include "IrrCompileConfig.h"
9 #ifdef _IRR_COMPILE_WITH_GUI_
10 
11 #include "IGUIEditBox.h"
12 #include "irrArray.h"
13 #include "IOSOperator.h"
14 
15 namespace irr
16 {
17 namespace gui
18 {
19 	class CGUIEditBox : public IGUIEditBox
20 	{
21 	public:
22 
23 		//! constructor
24 		CGUIEditBox(const wchar_t* text, bool border, IGUIEnvironment* environment,
25 			IGUIElement* parent, s32 id, const core::rect<s32>& rectangle);
26 
27 		//! destructor
28 		virtual ~CGUIEditBox();
29 
30 		//! Sets another skin independent font.
31 		virtual void setOverrideFont(IGUIFont* font=0);
32 
33 		//! Gets the override font (if any)
34 		/** \return The override font (may be 0) */
35 		virtual IGUIFont* getOverrideFont() const;
36 
37 		//! Get the font which is used right now for drawing
38 		/** Currently this is the override font when one is set and the
39 		font of the active skin otherwise */
40 		virtual IGUIFont* getActiveFont() const;
41 
42 		//! Sets another color for the text.
43 		virtual void setOverrideColor(video::SColor color);
44 
45 		//! Gets the override color
46 		virtual video::SColor getOverrideColor() const;
47 
48 		//! Sets if the text should use the overide color or the
49 		//! color in the gui skin.
50 		virtual void enableOverrideColor(bool enable);
51 
52 		//! Checks if an override color is enabled
53 		/** \return true if the override color is enabled, false otherwise */
54 		virtual bool isOverrideColorEnabled(void) const;
55 
56 		//! Sets whether to draw the background
57 		virtual void setDrawBackground(bool draw);
58 
59 		//! Turns the border on or off
60 		virtual void setDrawBorder(bool border);
61 
62 		//! Enables or disables word wrap for using the edit box as multiline text editor.
63 		virtual void setWordWrap(bool enable);
64 
65 		//! Checks if word wrap is enabled
66 		//! \return true if word wrap is enabled, false otherwise
67 		virtual bool isWordWrapEnabled() const;
68 
69 		//! Enables or disables newlines.
70 		/** \param enable: If set to true, the EGET_EDITBOX_ENTER event will not be fired,
71 		instead a newline character will be inserted. */
72 		virtual void setMultiLine(bool enable);
73 
74 		//! Checks if multi line editing is enabled
75 		//! \return true if mult-line is enabled, false otherwise
76 		virtual bool isMultiLineEnabled() const;
77 
78 		//! Enables or disables automatic scrolling with cursor position
79 		//! \param enable: If set to true, the text will move around with the cursor position
80 		virtual void setAutoScroll(bool enable);
81 
82 		//! Checks to see if automatic scrolling is enabled
83 		//! \return true if automatic scrolling is enabled, false if not
84 		virtual bool isAutoScrollEnabled() const;
85 
86 		//! Gets the size area of the text in the edit box
87 		//! \return Returns the size in pixels of the text
88 		virtual core::dimension2du getTextDimension();
89 
90 		//! Sets text justification
91 		virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
92 
93 		//! called if an event happened.
94 		virtual bool OnEvent(const SEvent& event);
95 
96 		//! draws the element and its children
97 		virtual void draw();
98 
99 		//! Sets the new caption of this element.
100 		virtual void setText(const wchar_t* text);
101 
102 		//! Sets the maximum amount of characters which may be entered in the box.
103 		//! \param max: Maximum amount of characters. If 0, the character amount is
104 		//! infinity.
105 		virtual void setMax(u32 max);
106 
107 		//! Returns maximum amount of characters, previously set by setMax();
108 		virtual u32 getMax() const;
109 
110 		//! Sets whether the edit box is a password box. Setting this to true will
111 		/** disable MultiLine, WordWrap and the ability to copy with ctrl+c or ctrl+x
112 		\param passwordBox: true to enable password, false to disable
113 		\param passwordChar: the character that is displayed instead of letters */
114 		virtual void setPasswordBox(bool passwordBox, wchar_t passwordChar = L'*');
115 
116 		//! Returns true if the edit box is currently a password box.
117 		virtual bool isPasswordBox() const;
118 
119 		//! Updates the absolute position, splits text if required
120 		virtual void updateAbsolutePosition();
121 
122 		//! Writes attributes of the element.
123 		virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
124 
125 		//! Reads attributes of the element
126 		virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
127 
128 	protected:
129 		//! Breaks the single text line.
130 		void breakText();
131 		//! sets the area of the given line
132 		void setTextRect(s32 line);
133 		//! returns the line number that the cursor is on
134 		s32 getLineFromPos(s32 pos);
135 		//! adds a letter to the edit box
136 		void inputChar(wchar_t c);
137 		//! calculates the current scroll position
138 		void calculateScrollPos();
139 		//! calculated the FrameRect
140 		void calculateFrameRect();
141 		//! send some gui event to parent
142 		void sendGuiEvent(EGUI_EVENT_TYPE type);
143 		//! set text markers
144 		void setTextMarkers(s32 begin, s32 end);
145 
146 		bool processKey(const SEvent& event);
147 		bool processMouse(const SEvent& event);
148 		s32 getCursorPos(s32 x, s32 y);
149 
150 		bool MouseMarking;
151 		bool Border;
152 		bool Background;
153 		bool OverrideColorEnabled;
154 		s32 MarkBegin;
155 		s32 MarkEnd;
156 
157 		video::SColor OverrideColor;
158 		gui::IGUIFont *OverrideFont, *LastBreakFont;
159 		IOSOperator* Operator;
160 
161 		u32 BlinkStartTime;
162 		s32 CursorPos;
163 		s32 HScrollPos, VScrollPos; // scroll position in characters
164 		u32 Max;
165 
166 		bool WordWrap, MultiLine, AutoScroll, PasswordBox;
167 		wchar_t PasswordChar;
168 		EGUI_ALIGNMENT HAlign, VAlign;
169 
170 		core::array< core::stringw > BrokenText;
171 		core::array< s32 > BrokenTextPositions;
172 
173 		core::rect<s32> CurrentTextRect, FrameRect; // temporary values
174 	};
175 
176 
177 } // end namespace gui
178 } // end namespace irr
179 
180 #endif // _IRR_COMPILE_WITH_GUI_
181 #endif // __C_GUI_EDIT_BOX_H_INCLUDED__
182 
183