cedffd82ffc3fce5a07e3ee8e8a107c8863900c8
[oweals/minetest.git] / src / gui / guiEditBoxWithScrollbar.h
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt, Modified by Mustapha Tachouct
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 GUIEDITBOXWITHSCROLLBAR_HEADER
6 #define GUIEDITBOXWITHSCROLLBAR_HEADER
7
8 #include "IGUIEditBox.h"
9 #include "IOSOperator.h"
10 #include "IGUIScrollBar.h"
11 #include <vector>
12
13 using namespace irr;
14 using namespace irr::gui;
15
16 class GUIEditBoxWithScrollBar : public IGUIEditBox
17 {
18 public:
19
20         //! constructor
21         GUIEditBoxWithScrollBar(const wchar_t* text, bool border, IGUIEnvironment* environment,
22                 IGUIElement* parent, s32 id, const core::rect<s32>& rectangle,
23                 bool writable = true, bool has_vscrollbar = true);
24
25         //! destructor
26         virtual ~GUIEditBoxWithScrollBar();
27
28         //! Sets another skin independent font.
29         virtual void setOverrideFont(IGUIFont* font = 0);
30
31         //! Gets the override font (if any)
32         /** \return The override font (may be 0) */
33         virtual IGUIFont* getOverrideFont() const;
34
35         //! Get the font which is used right now for drawing
36         /** Currently this is the override font when one is set and the
37         font of the active skin otherwise */
38         virtual IGUIFont* getActiveFont() const;
39
40         //! Sets another color for the text.
41         virtual void setOverrideColor(video::SColor color);
42
43         //! Gets the override color
44         virtual video::SColor getOverrideColor() const;
45
46         //! Sets if the text should use the overide color or the
47         //! color in the gui skin.
48         virtual void enableOverrideColor(bool enable);
49
50         //! Checks if an override color is enabled
51         /** \return true if the override color is enabled, false otherwise */
52         virtual bool isOverrideColorEnabled(void) const;
53
54         //! Sets whether to draw the background
55         virtual void setDrawBackground(bool draw);
56
57         //! Turns the border on or off
58         virtual void setDrawBorder(bool border);
59
60         //! Enables or disables word wrap for using the edit box as multiline text editor.
61         virtual void setWordWrap(bool enable);
62
63         //! Checks if word wrap is enabled
64         //! \return true if word wrap is enabled, false otherwise
65         virtual bool isWordWrapEnabled() const;
66
67         //! Enables or disables newlines.
68         /** \param enable: If set to true, the EGET_EDITBOX_ENTER event will not be fired,
69         instead a newline character will be inserted. */
70         virtual void setMultiLine(bool enable);
71
72         //! Checks if multi line editing is enabled
73         //! \return true if mult-line is enabled, false otherwise
74         virtual bool isMultiLineEnabled() const;
75
76         //! Enables or disables automatic scrolling with cursor position
77         //! \param enable: If set to true, the text will move around with the cursor position
78         virtual void setAutoScroll(bool enable);
79
80         //! Checks to see if automatic scrolling is enabled
81         //! \return true if automatic scrolling is enabled, false if not
82         virtual bool isAutoScrollEnabled() const;
83
84         //! Gets the size area of the text in the edit box
85         //! \return Returns the size in pixels of the text
86         virtual core::dimension2du getTextDimension();
87
88         //! Sets text justification
89         virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
90
91         //! called if an event happened.
92         virtual bool OnEvent(const SEvent& event);
93
94         //! draws the element and its children
95         virtual void draw();
96
97         //! Sets the new caption of this element.
98         virtual void setText(const wchar_t* text);
99
100         //! Sets the maximum amount of characters which may be entered in the box.
101         //! \param max: Maximum amount of characters. If 0, the character amount is
102         //! infinity.
103         virtual void setMax(u32 max);
104
105         //! Returns maximum amount of characters, previously set by setMax();
106         virtual u32 getMax() const;
107
108         //! Sets whether the edit box is a password box. Setting this to true will
109         /** disable MultiLine, WordWrap and the ability to copy with ctrl+c or ctrl+x
110         \param passwordBox: true to enable password, false to disable
111         \param passwordChar: the character that is displayed instead of letters */
112         virtual void setPasswordBox(bool passwordBox, wchar_t passwordChar = L'*');
113
114         //! Returns true if the edit box is currently a password box.
115         virtual bool isPasswordBox() const;
116
117         //! Updates the absolute position, splits text if required
118         virtual void updateAbsolutePosition();
119         
120         virtual void setWritable(bool writable);
121
122         //! Change the background color
123         virtual void setBackgroundColor(const video::SColor &bg_color);
124
125         //! Writes attributes of the element.
126         virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
127
128         //! Reads attributes of the element
129         virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
130
131         virtual bool isDrawBackgroundEnabled() const;
132         virtual bool isDrawBorderEnabled() const;
133         virtual void setCursorChar(const wchar_t cursorChar);
134         virtual wchar_t getCursorChar() const;
135         virtual void setCursorBlinkTime(irr::u32 timeMs);
136         virtual irr::u32 getCursorBlinkTime() const;
137
138 protected:
139         //! Breaks the single text line.
140         void breakText();
141         //! sets the area of the given line
142         void setTextRect(s32 line);
143         //! returns the line number that the cursor is on
144         s32 getLineFromPos(s32 pos);
145         //! adds a letter to the edit box
146         void inputChar(wchar_t c);
147         //! calculates the current scroll position
148         void calculateScrollPos();
149         //! calculated the FrameRect
150         void calculateFrameRect();
151         //! send some gui event to parent
152         void sendGuiEvent(EGUI_EVENT_TYPE type);
153         //! set text markers
154         void setTextMarkers(s32 begin, s32 end);
155         //! create a Vertical ScrollBar
156         void createVScrollBar();
157         //! update the vertical scrollBar (visibilty & position)
158         void updateVScrollBar();
159
160         bool processKey(const SEvent& event);
161         bool processMouse(const SEvent& event);
162         s32 getCursorPos(s32 x, s32 y);
163
164         bool m_mouse_marking;
165         bool m_border;
166         bool m_background;
167         bool m_override_color_enabled;
168         s32 m_mark_begin;
169         s32 m_mark_end;
170
171         video::SColor m_override_color;
172         gui::IGUIFont *m_override_font, *m_last_break_font;
173         IOSOperator* m_operator;
174
175         u32 m_blink_start_time;
176         s32 m_cursor_pos;
177         s32 m_hscroll_pos, m_vscroll_pos; // scroll position in characters
178         u32 m_max;
179
180         bool m_word_wrap, m_multiline, m_autoscroll, m_passwordbox;
181         wchar_t m_passwordchar;
182         EGUI_ALIGNMENT m_halign, m_valign;
183
184         std::vector<core::stringw> m_broken_text;
185         std::vector<s32> m_broken_text_positions;
186
187         core::rect<s32> m_current_text_rect, m_frame_rect; // temporary values
188
189         u32 m_scrollbar_width;
190         IGUIScrollBar *m_vscrollbar;
191         bool m_writable;
192
193         bool m_bg_color_used;
194         video::SColor m_bg_color;
195 };
196
197
198 #endif // GUIEDITBOXWITHSCROLLBAR_HEADER
199