Add warning when disabling secure.enable_security (#9943)
[oweals/minetest.git] / src / gui / guiEditBoxWithScrollbar.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // Modified by Mustapha T.
3 // This file is part of the "Irrlicht Engine".
4 // For conditions of distribution and use, see copyright notice in irrlicht.h
5
6 #include "guiEditBoxWithScrollbar.h"
7
8 #include "IGUISkin.h"
9 #include "IGUIEnvironment.h"
10 #include "IGUIFont.h"
11 #include "IVideoDriver.h"
12 #include "rect.h"
13 #include "porting.h"
14 #include "Keycodes.h"
15
16 /*
17 todo:
18 optional scrollbars [done]
19 ctrl+left/right to select word
20 double click/ctrl click: word select + drag to select whole words, triple click to select line
21 optional? dragging selected text
22 numerical
23 */
24
25
26 //! constructor
27 GUIEditBoxWithScrollBar::GUIEditBoxWithScrollBar(const wchar_t* text, bool border,
28         IGUIEnvironment* environment, IGUIElement* parent, s32 id,
29         const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
30         : IGUIEditBox(environment, parent, id, rectangle), m_mouse_marking(false),
31         m_border(border), m_background(true), m_override_color_enabled(false), m_mark_begin(0), m_mark_end(0),
32         m_override_color(video::SColor(101, 255, 255, 255)), m_override_font(0), m_last_break_font(0),
33         m_operator(0), m_blink_start_time(0), m_cursor_pos(0), m_hscroll_pos(0), m_vscroll_pos(0), m_max(0),
34         m_word_wrap(false), m_multiline(false), m_autoscroll(true), m_passwordbox(false),
35         m_passwordchar(L'*'), m_halign(EGUIA_UPPERLEFT), m_valign(EGUIA_CENTER),
36         m_current_text_rect(0, 0, 1, 1), m_frame_rect(rectangle),
37         m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable),
38         m_bg_color_used(false)
39 {
40 #ifdef _DEBUG
41         setDebugName("GUIEditBoxWithScrollBar");
42 #endif
43
44
45         Text = text;
46
47         if (Environment)
48                 m_operator = Environment->getOSOperator();
49
50         if (m_operator)
51                 m_operator->grab();
52
53         // this element can be tabbed to
54         setTabStop(true);
55         setTabOrder(-1);
56
57         if (has_vscrollbar) {
58                 createVScrollBar();
59         }
60
61         calculateFrameRect();
62         breakText();
63
64         calculateScrollPos();
65         setWritable(writable);
66 }
67
68
69 //! destructor
70 GUIEditBoxWithScrollBar::~GUIEditBoxWithScrollBar()
71 {
72         if (m_override_font)
73                 m_override_font->drop();
74
75         if (m_operator)
76                 m_operator->drop();
77
78         if (m_vscrollbar)
79                 m_vscrollbar->drop();
80 }
81
82
83 //! Sets another skin independent font.
84 void GUIEditBoxWithScrollBar::setOverrideFont(IGUIFont* font)
85 {
86         if (m_override_font == font)
87                 return;
88
89         if (m_override_font)
90                 m_override_font->drop();
91
92         m_override_font = font;
93
94         if (m_override_font)
95                 m_override_font->grab();
96
97         breakText();
98 }
99
100 //! Gets the override font (if any)
101 IGUIFont * GUIEditBoxWithScrollBar::getOverrideFont() const
102 {
103         return m_override_font;
104 }
105
106 //! Get the font which is used right now for drawing
107 IGUIFont* GUIEditBoxWithScrollBar::getActiveFont() const
108 {
109         if (m_override_font)
110                 return m_override_font;
111         IGUISkin* skin = Environment->getSkin();
112         if (skin)
113                 return skin->getFont();
114         return 0;
115 }
116
117 //! Sets another color for the text.
118 void GUIEditBoxWithScrollBar::setOverrideColor(video::SColor color)
119 {
120         m_override_color = color;
121         m_override_color_enabled = true;
122 }
123
124
125 video::SColor GUIEditBoxWithScrollBar::getOverrideColor() const
126 {
127         return m_override_color;
128 }
129
130
131 //! Turns the border on or off
132 void GUIEditBoxWithScrollBar::setDrawBorder(bool border)
133 {
134         m_border = border;
135 }
136
137 //! Sets whether to draw the background
138 void GUIEditBoxWithScrollBar::setDrawBackground(bool draw)
139 {
140         m_background = draw;
141 }
142
143 //! Sets if the text should use the overide color or the color in the gui skin.
144 void GUIEditBoxWithScrollBar::enableOverrideColor(bool enable)
145 {
146         m_override_color_enabled = enable;
147 }
148
149 bool GUIEditBoxWithScrollBar::isOverrideColorEnabled() const
150 {
151         return m_override_color_enabled;
152 }
153
154 //! Enables or disables word wrap
155 void GUIEditBoxWithScrollBar::setWordWrap(bool enable)
156 {
157         m_word_wrap = enable;
158         breakText();
159 }
160
161
162 void GUIEditBoxWithScrollBar::updateAbsolutePosition()
163 {
164         core::rect<s32> old_absolute_rect(AbsoluteRect);
165         IGUIElement::updateAbsolutePosition();
166         if (old_absolute_rect != AbsoluteRect) {
167                 calculateFrameRect();
168                 breakText();
169                 calculateScrollPos();
170         }
171 }
172
173 //! Checks if word wrap is enabled
174 bool GUIEditBoxWithScrollBar::isWordWrapEnabled() const
175 {
176         return m_word_wrap;
177 }
178
179
180 //! Enables or disables newlines.
181 void GUIEditBoxWithScrollBar::setMultiLine(bool enable)
182 {
183         m_multiline = enable;
184 }
185
186
187 //! Checks if multi line editing is enabled
188 bool GUIEditBoxWithScrollBar::isMultiLineEnabled() const
189 {
190         return m_multiline;
191 }
192
193
194 void GUIEditBoxWithScrollBar::setPasswordBox(bool password_box, wchar_t password_char)
195 {
196         m_passwordbox = password_box;
197         if (m_passwordbox) {
198                 m_passwordchar = password_char;
199                 setMultiLine(false);
200                 setWordWrap(false);
201                 m_broken_text.clear();
202         }
203 }
204
205
206 bool GUIEditBoxWithScrollBar::isPasswordBox() const
207 {
208         return m_passwordbox;
209 }
210
211
212 //! Sets text justification
213 void GUIEditBoxWithScrollBar::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
214 {
215         m_halign = horizontal;
216         m_valign = vertical;
217 }
218
219
220 //! called if an event happened.
221 bool GUIEditBoxWithScrollBar::OnEvent(const SEvent& event)
222 {
223         if (isEnabled()) {
224                 switch (event.EventType)
225                 {
226                 case EET_GUI_EVENT:
227                         if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) {
228                                 if (event.GUIEvent.Caller == this) {
229                                         m_mouse_marking = false;
230                                         setTextMarkers(0, 0);
231                                 }
232                         }
233                         break;
234                 case EET_KEY_INPUT_EVENT:
235                         if (processKey(event))
236                                 return true;
237                         break;
238                 case EET_MOUSE_INPUT_EVENT:
239                         if (processMouse(event))
240                                 return true;
241                         break;
242                 default:
243                         break;
244                 }
245         }
246
247         return IGUIElement::OnEvent(event);
248 }
249
250
251 bool GUIEditBoxWithScrollBar::processKey(const SEvent& event)
252 {
253         if (!m_writable) {
254                 return false;
255         }
256
257         if (!event.KeyInput.PressedDown)
258                 return false;
259
260         bool text_changed = false;
261         s32 new_mark_begin = m_mark_begin;
262         s32 new_mark_end = m_mark_end;
263
264         // control shortcut handling
265
266         if (event.KeyInput.Control) {
267
268                 // german backlash '\' entered with control + '?'
269                 if (event.KeyInput.Char == '\\') {
270                         inputChar(event.KeyInput.Char);
271                         return true;
272                 }
273
274                 switch (event.KeyInput.Key) {
275                 case KEY_KEY_A:
276                         // select all
277                         new_mark_begin = 0;
278                         new_mark_end = Text.size();
279                         break;
280                 case KEY_KEY_C:
281                         // copy to clipboard
282                         if (!m_passwordbox && m_operator && m_mark_begin != m_mark_end)
283                         {
284                                 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
285                                 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
286
287                                 core::stringc s;
288                                 s = Text.subString(realmbgn, realmend - realmbgn).c_str();
289                                 m_operator->copyToClipboard(s.c_str());
290                         }
291                         break;
292                 case KEY_KEY_X:
293                         // cut to the clipboard
294                         if (!m_passwordbox && m_operator && m_mark_begin != m_mark_end) {
295                                 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
296                                 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
297
298                                 // copy
299                                 core::stringc sc;
300                                 sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
301                                 m_operator->copyToClipboard(sc.c_str());
302
303                                 if (isEnabled())
304                                 {
305                                         // delete
306                                         core::stringw s;
307                                         s = Text.subString(0, realmbgn);
308                                         s.append(Text.subString(realmend, Text.size() - realmend));
309                                         Text = s;
310
311                                         m_cursor_pos = realmbgn;
312                                         new_mark_begin = 0;
313                                         new_mark_end = 0;
314                                         text_changed = true;
315                                 }
316                         }
317                         break;
318                 case KEY_KEY_V:
319                         if (!isEnabled())
320                                 break;
321
322                         // paste from the clipboard
323                         if (m_operator) {
324                                 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
325                                 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
326
327                                 // add new character
328                                 const c8* p = m_operator->getTextFromClipboard();
329                                 if (p) {
330                                         if (m_mark_begin == m_mark_end) {
331                                                 // insert text
332                                                 core::stringw s = Text.subString(0, m_cursor_pos);
333                                                 s.append(p);
334                                                 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
335
336                                                 if (!m_max || s.size() <= m_max) // thx to Fish FH for fix
337                                                 {
338                                                         Text = s;
339                                                         s = p;
340                                                         m_cursor_pos += s.size();
341                                                 }
342                                         } else {
343                                                 // replace text
344
345                                                 core::stringw s = Text.subString(0, realmbgn);
346                                                 s.append(p);
347                                                 s.append(Text.subString(realmend, Text.size() - realmend));
348
349                                                 if (!m_max || s.size() <= m_max)  // thx to Fish FH for fix
350                                                 {
351                                                         Text = s;
352                                                         s = p;
353                                                         m_cursor_pos = realmbgn + s.size();
354                                                 }
355                                         }
356                                 }
357
358                                 new_mark_begin = 0;
359                                 new_mark_end = 0;
360                                 text_changed = true;
361                         }
362                         break;
363                 case KEY_HOME:
364                         // move/highlight to start of text
365                         if (event.KeyInput.Shift) {
366                                 new_mark_end = m_cursor_pos;
367                                 new_mark_begin = 0;
368                                 m_cursor_pos = 0;
369                         } else {
370                                 m_cursor_pos = 0;
371                                 new_mark_begin = 0;
372                                 new_mark_end = 0;
373                         }
374                         break;
375                 case KEY_END:
376                         // move/highlight to end of text
377                         if (event.KeyInput.Shift) {
378                                 new_mark_begin = m_cursor_pos;
379                                 new_mark_end = Text.size();
380                                 m_cursor_pos = 0;
381                         } else {
382                                 m_cursor_pos = Text.size();
383                                 new_mark_begin = 0;
384                                 new_mark_end = 0;
385                         }
386                         break;
387                 default:
388                         return false;
389                 }
390         }
391         // default keyboard handling
392         else
393                 switch (event.KeyInput.Key)     {
394                 case KEY_END:
395                 {
396                         s32 p = Text.size();
397                         if (m_word_wrap || m_multiline) {
398                                 p = getLineFromPos(m_cursor_pos);
399                                 p = m_broken_text_positions[p] + (s32)m_broken_text[p].size();
400                                 if (p > 0 && (Text[p - 1] == L'\r' || Text[p - 1] == L'\n'))
401                                         p -= 1;
402                         }
403
404                         if (event.KeyInput.Shift) {
405                                 if (m_mark_begin == m_mark_end)
406                                         new_mark_begin = m_cursor_pos;
407
408                                 new_mark_end = p;
409                         } else {
410                                 new_mark_begin = 0;
411                                 new_mark_end = 0;
412                         }
413                         m_cursor_pos = p;
414                         m_blink_start_time = porting::getTimeMs();
415                 }
416                 break;
417                 case KEY_HOME:
418                 {
419
420                         s32 p = 0;
421                         if (m_word_wrap || m_multiline) {
422                                 p = getLineFromPos(m_cursor_pos);
423                                 p = m_broken_text_positions[p];
424                         }
425
426                         if (event.KeyInput.Shift) {
427                                 if (m_mark_begin == m_mark_end)
428                                         new_mark_begin = m_cursor_pos;
429                                 new_mark_end = p;
430                         } else {
431                                 new_mark_begin = 0;
432                                 new_mark_end = 0;
433                         }
434                         m_cursor_pos = p;
435                         m_blink_start_time = porting::getTimeMs();
436                 }
437                 break;
438                 case KEY_RETURN:
439                         if (m_multiline) {
440                                 inputChar(L'\n');
441                         } else {
442                                 calculateScrollPos();
443                                 sendGuiEvent(EGET_EDITBOX_ENTER);
444                         }
445                         return true;
446                 case KEY_LEFT:
447
448                         if (event.KeyInput.Shift) {
449                                 if (m_cursor_pos > 0) {
450                                         if (m_mark_begin == m_mark_end)
451                                                 new_mark_begin = m_cursor_pos;
452
453                                         new_mark_end = m_cursor_pos - 1;
454                                 }
455                         } else {
456                                 new_mark_begin = 0;
457                                 new_mark_end = 0;
458                         }
459
460                         if (m_cursor_pos > 0)
461                                 m_cursor_pos--;
462                         m_blink_start_time = porting::getTimeMs();
463                         break;
464
465                 case KEY_RIGHT:
466                         if (event.KeyInput.Shift) {
467                                 if (Text.size() > (u32)m_cursor_pos) {
468                                         if (m_mark_begin == m_mark_end)
469                                                 new_mark_begin = m_cursor_pos;
470
471                                         new_mark_end = m_cursor_pos + 1;
472                                 }
473                         } else {
474                                 new_mark_begin = 0;
475                                 new_mark_end = 0;
476                         }
477
478                         if (Text.size() > (u32)m_cursor_pos)
479                                 m_cursor_pos++;
480                         m_blink_start_time = porting::getTimeMs();
481                         break;
482                 case KEY_UP:
483                         if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
484                                 s32 lineNo = getLineFromPos(m_cursor_pos);
485                                 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos : (m_mark_begin > m_mark_end ? m_mark_begin : m_mark_end);
486                                 if (lineNo > 0) {
487                                         s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
488                                         if ((s32)m_broken_text[lineNo - 1].size() < cp)
489                                                 m_cursor_pos = m_broken_text_positions[lineNo - 1] + core::max_((u32)1, m_broken_text[lineNo - 1].size()) - 1;
490                                         else
491                                                 m_cursor_pos = m_broken_text_positions[lineNo - 1] + cp;
492                                 }
493
494                                 if (event.KeyInput.Shift) {
495                                         new_mark_begin = mb;
496                                         new_mark_end = m_cursor_pos;
497                                 } else {
498                                         new_mark_begin = 0;
499                                         new_mark_end = 0;
500                                 }
501                         } else {
502                                 return false;
503                         }
504                         break;
505                 case KEY_DOWN:
506                         if (m_multiline || (m_word_wrap && m_broken_text.size() > 1)) {
507                                 s32 lineNo = getLineFromPos(m_cursor_pos);
508                                 s32 mb = (m_mark_begin == m_mark_end) ? m_cursor_pos : (m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end);
509                                 if (lineNo < (s32)m_broken_text.size() - 1)
510                                 {
511                                         s32 cp = m_cursor_pos - m_broken_text_positions[lineNo];
512                                         if ((s32)m_broken_text[lineNo + 1].size() < cp)
513                                                 m_cursor_pos = m_broken_text_positions[lineNo + 1] + core::max_((u32)1, m_broken_text[lineNo + 1].size()) - 1;
514                                         else
515                                                 m_cursor_pos = m_broken_text_positions[lineNo + 1] + cp;
516                                 }
517
518                                 if (event.KeyInput.Shift) {
519                                         new_mark_begin = mb;
520                                         new_mark_end = m_cursor_pos;
521                                 } else {
522                                         new_mark_begin = 0;
523                                         new_mark_end = 0;
524                                 }
525
526                         } else {
527                                 return false;
528                         }
529                         break;
530
531                 case KEY_BACK:
532                         if (!isEnabled())
533                                 break;
534
535                         if (Text.size()) {
536                                 core::stringw s;
537
538                                 if (m_mark_begin != m_mark_end) {
539                                         // delete marked text
540                                         const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
541                                         const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
542
543                                         s = Text.subString(0, realmbgn);
544                                         s.append(Text.subString(realmend, Text.size() - realmend));
545                                         Text = s;
546
547                                         m_cursor_pos = realmbgn;
548                                 } else {
549                                         // delete text behind cursor
550                                         if (m_cursor_pos > 0)
551                                                 s = Text.subString(0, m_cursor_pos - 1);
552                                         else
553                                                 s = L"";
554                                         s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
555                                         Text = s;
556                                         --m_cursor_pos;
557                                 }
558
559                                 if (m_cursor_pos < 0)
560                                         m_cursor_pos = 0;
561                                 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
562                                 new_mark_begin = 0;
563                                 new_mark_end = 0;
564                                 text_changed = true;
565                         }
566                         break;
567                 case KEY_DELETE:
568                         if (!isEnabled())
569                                 break;
570
571                         if (Text.size() != 0) {
572                                 core::stringw s;
573
574                                 if (m_mark_begin != m_mark_end) {
575                                         // delete marked text
576                                         const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
577                                         const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
578
579                                         s = Text.subString(0, realmbgn);
580                                         s.append(Text.subString(realmend, Text.size() - realmend));
581                                         Text = s;
582
583                                         m_cursor_pos = realmbgn;
584                                 } else {
585                                         // delete text before cursor
586                                         s = Text.subString(0, m_cursor_pos);
587                                         s.append(Text.subString(m_cursor_pos + 1, Text.size() - m_cursor_pos - 1));
588                                         Text = s;
589                                 }
590
591                                 if (m_cursor_pos > (s32)Text.size())
592                                         m_cursor_pos = (s32)Text.size();
593
594                                 m_blink_start_time = porting::getTimeMs(); // os::Timer::getTime();
595                                 new_mark_begin = 0;
596                                 new_mark_end = 0;
597                                 text_changed = true;
598                         }
599                         break;
600
601                 case KEY_ESCAPE:
602                 case KEY_TAB:
603                 case KEY_SHIFT:
604                 case KEY_F1:
605                 case KEY_F2:
606                 case KEY_F3:
607                 case KEY_F4:
608                 case KEY_F5:
609                 case KEY_F6:
610                 case KEY_F7:
611                 case KEY_F8:
612                 case KEY_F9:
613                 case KEY_F10:
614                 case KEY_F11:
615                 case KEY_F12:
616                 case KEY_F13:
617                 case KEY_F14:
618                 case KEY_F15:
619                 case KEY_F16:
620                 case KEY_F17:
621                 case KEY_F18:
622                 case KEY_F19:
623                 case KEY_F20:
624                 case KEY_F21:
625                 case KEY_F22:
626                 case KEY_F23:
627                 case KEY_F24:
628                         // ignore these keys
629                         return false;
630
631                 default:
632                         inputChar(event.KeyInput.Char);
633                         return true;
634                 }
635
636         // Set new text markers
637         setTextMarkers(new_mark_begin, new_mark_end);
638
639         // break the text if it has changed
640         if (text_changed) {
641                 breakText();
642                 calculateScrollPos();
643                 sendGuiEvent(EGET_EDITBOX_CHANGED);
644         }
645         else
646         {
647                 calculateScrollPos();
648         }
649
650         return true;
651 }
652
653
654 //! draws the element and its children
655 void GUIEditBoxWithScrollBar::draw()
656 {
657         if (!IsVisible)
658                 return;
659
660         const bool focus = Environment->hasFocus(this);
661
662         IGUISkin* skin = Environment->getSkin();
663         if (!skin)
664                 return;
665
666         video::SColor default_bg_color;
667         video::SColor bg_color;
668
669         default_bg_color = m_writable ? skin->getColor(EGDC_WINDOW) : video::SColor(0);
670         bg_color = m_bg_color_used ? m_bg_color : default_bg_color;
671
672         if (!m_border && m_background) {
673                 skin->draw2DRectangle(this, bg_color, AbsoluteRect, &AbsoluteClippingRect);
674         }
675
676         // draw the border
677
678         if (m_border) {
679
680                 if (m_writable) {
681                         skin->draw3DSunkenPane(this, bg_color, false, m_background,
682                                 AbsoluteRect, &AbsoluteClippingRect);
683                 }
684
685                 calculateFrameRect();
686         }
687
688         core::rect<s32> local_clip_rect = m_frame_rect;
689         local_clip_rect.clipAgainst(AbsoluteClippingRect);
690
691         // draw the text
692
693         IGUIFont* font = getActiveFont();
694
695         s32 cursor_line = 0;
696         s32 charcursorpos = 0;
697
698         if (font) {
699                 if (m_last_break_font != font) {
700                         breakText();
701                 }
702
703                 // calculate cursor pos
704
705                 core::stringw *txt_line = &Text;
706                 s32 start_pos = 0;
707
708                 core::stringw s, s2;
709
710                 // get mark position
711                 const bool ml = (!m_passwordbox && (m_word_wrap || m_multiline));
712                 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
713                 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
714                 const s32 hline_start = ml ? getLineFromPos(realmbgn) : 0;
715                 const s32 hline_count = ml ? getLineFromPos(realmend) - hline_start + 1 : 1;
716                 const s32 line_count = ml ? m_broken_text.size() : 1;
717
718                 // Save the override color information.
719                 // Then, alter it if the edit box is disabled.
720                 const bool prevOver = m_override_color_enabled;
721                 const video::SColor prevColor = m_override_color;
722
723                 if (Text.size()) {
724                         if (!isEnabled() && !m_override_color_enabled) {
725                                 m_override_color_enabled = true;
726                                 m_override_color = skin->getColor(EGDC_GRAY_TEXT);
727                         }
728
729                         for (s32 i = 0; i < line_count; ++i) {
730                                 setTextRect(i);
731
732                                 // clipping test - don't draw anything outside the visible area
733                                 core::rect<s32> c = local_clip_rect;
734                                 c.clipAgainst(m_current_text_rect);
735                                 if (!c.isValid())
736                                         continue;
737
738                                 // get current line
739                                 if (m_passwordbox) {
740                                         if (m_broken_text.size() != 1) {
741                                                 m_broken_text.clear();
742                                                 m_broken_text.emplace_back();
743                                         }
744                                         if (m_broken_text[0].size() != Text.size()){
745                                                 m_broken_text[0] = Text;
746                                                 for (u32 q = 0; q < Text.size(); ++q)
747                                                 {
748                                                         m_broken_text[0][q] = m_passwordchar;
749                                                 }
750                                         }
751                                         txt_line = &m_broken_text[0];
752                                         start_pos = 0;
753                                 } else {
754                                         txt_line = ml ? &m_broken_text[i] : &Text;
755                                         start_pos = ml ? m_broken_text_positions[i] : 0;
756                                 }
757
758
759                                 // draw normal text
760                                 font->draw(txt_line->c_str(), m_current_text_rect,
761                                         m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
762                                         false, true, &local_clip_rect);
763
764                                 // draw mark and marked text
765                                 if (focus && m_mark_begin != m_mark_end && i >= hline_start && i < hline_start + hline_count) {
766
767                                         s32 mbegin = 0, mend = 0;
768                                         s32 lineStartPos = 0, lineEndPos = txt_line->size();
769
770                                         if (i == hline_start) {
771                                                 // highlight start is on this line
772                                                 s = txt_line->subString(0, realmbgn - start_pos);
773                                                 mbegin = font->getDimension(s.c_str()).Width;
774
775                                                 // deal with kerning
776                                                 mbegin += font->getKerningWidth(
777                                                         &((*txt_line)[realmbgn - start_pos]),
778                                                         realmbgn - start_pos > 0 ? &((*txt_line)[realmbgn - start_pos - 1]) : 0);
779
780                                                 lineStartPos = realmbgn - start_pos;
781                                         }
782                                         if (i == hline_start + hline_count - 1) {
783                                                 // highlight end is on this line
784                                                 s2 = txt_line->subString(0, realmend - start_pos);
785                                                 mend = font->getDimension(s2.c_str()).Width;
786                                                 lineEndPos = (s32)s2.size();
787                                         } else {
788                                                 mend = font->getDimension(txt_line->c_str()).Width;
789                                         }
790
791
792                                         m_current_text_rect.UpperLeftCorner.X += mbegin;
793                                         m_current_text_rect.LowerRightCorner.X = m_current_text_rect.UpperLeftCorner.X + mend - mbegin;
794
795
796                                         // draw mark
797                                         skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), m_current_text_rect, &local_clip_rect);
798
799                                         // draw marked text
800                                         s = txt_line->subString(lineStartPos, lineEndPos - lineStartPos);
801
802                                         if (s.size())
803                                                 font->draw(s.c_str(), m_current_text_rect,
804                                                         m_override_color_enabled ? m_override_color : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
805                                                         false, true, &local_clip_rect);
806
807                                 }
808                         }
809
810                         // Return the override color information to its previous settings.
811                         m_override_color_enabled = prevOver;
812                         m_override_color = prevColor;
813                 }
814
815                 // draw cursor
816                 if (IsEnabled && m_writable) {
817                         if (m_word_wrap || m_multiline) {
818                                 cursor_line = getLineFromPos(m_cursor_pos);
819                                 txt_line = &m_broken_text[cursor_line];
820                                 start_pos = m_broken_text_positions[cursor_line];
821                         }
822                         s = txt_line->subString(0, m_cursor_pos - start_pos);
823                         charcursorpos = font->getDimension(s.c_str()).Width +
824                                 font->getKerningWidth(L"_", m_cursor_pos - start_pos > 0 ? &((*txt_line)[m_cursor_pos - start_pos - 1]) : 0);
825
826                         if (focus && (porting::getTimeMs() - m_blink_start_time) % 700 < 350) {
827                                 setTextRect(cursor_line);
828                                 m_current_text_rect.UpperLeftCorner.X += charcursorpos;
829
830                                 font->draw(L"_", m_current_text_rect,
831                                         m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
832                                         false, true, &local_clip_rect);
833                         }
834                 }
835         }
836
837         // draw children
838         IGUIElement::draw();
839 }
840
841
842 //! Sets the new caption of this element.
843 void GUIEditBoxWithScrollBar::setText(const wchar_t* text)
844 {
845         Text = text;
846         if (u32(m_cursor_pos) > Text.size())
847                 m_cursor_pos = Text.size();
848         m_hscroll_pos = 0;
849         breakText();
850 }
851
852
853 //! Enables or disables automatic scrolling with cursor position
854 //! \param enable: If set to true, the text will move around with the cursor position
855 void GUIEditBoxWithScrollBar::setAutoScroll(bool enable)
856 {
857         m_autoscroll = enable;
858 }
859
860
861 //! Checks to see if automatic scrolling is enabled
862 //! \return true if automatic scrolling is enabled, false if not
863 bool GUIEditBoxWithScrollBar::isAutoScrollEnabled() const
864 {
865         return m_autoscroll;
866 }
867
868
869 //! Gets the area of the text in the edit box
870 //! \return Returns the size in pixels of the text
871 core::dimension2du GUIEditBoxWithScrollBar::getTextDimension()
872 {
873         core::rect<s32> ret;
874
875         setTextRect(0);
876         ret = m_current_text_rect;
877
878         for (u32 i = 1; i < m_broken_text.size(); ++i) {
879                 setTextRect(i);
880                 ret.addInternalPoint(m_current_text_rect.UpperLeftCorner);
881                 ret.addInternalPoint(m_current_text_rect.LowerRightCorner);
882         }
883
884         return core::dimension2du(ret.getSize());
885 }
886
887
888 //! Sets the maximum amount of characters which may be entered in the box.
889 //! \param max: Maximum amount of characters. If 0, the character amount is
890 //! infinity.
891 void GUIEditBoxWithScrollBar::setMax(u32 max)
892 {
893         m_max = max;
894
895         if (Text.size() > m_max && m_max != 0)
896                 Text = Text.subString(0, m_max);
897 }
898
899
900 //! Returns maximum amount of characters, previously set by setMax();
901 u32 GUIEditBoxWithScrollBar::getMax() const
902 {
903         return m_max;
904 }
905
906
907 bool GUIEditBoxWithScrollBar::processMouse(const SEvent& event)
908 {
909         switch (event.MouseInput.Event)
910         {
911         case irr::EMIE_LMOUSE_LEFT_UP:
912                 if (Environment->hasFocus(this)) {
913                         m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
914                         if (m_mouse_marking) {
915                                 setTextMarkers(m_mark_begin, m_cursor_pos);
916                         }
917                         m_mouse_marking = false;
918                         calculateScrollPos();
919                         return true;
920                 }
921                 break;
922         case irr::EMIE_MOUSE_MOVED:
923         {
924                 if (m_mouse_marking) {
925                         m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
926                         setTextMarkers(m_mark_begin, m_cursor_pos);
927                         calculateScrollPos();
928                         return true;
929                 }
930         }
931         break;
932         case EMIE_LMOUSE_PRESSED_DOWN:
933
934                 if (!Environment->hasFocus(this)) {
935                         m_blink_start_time = porting::getTimeMs();
936                         m_mouse_marking = true;
937                         m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
938                         setTextMarkers(m_cursor_pos, m_cursor_pos);
939                         calculateScrollPos();
940                         return true;
941                 } else {
942                         if (!AbsoluteClippingRect.isPointInside(
943                                 core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
944                                 return false;
945                         } else {
946                                 // move cursor
947                                 m_cursor_pos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
948
949                                 s32 newMarkBegin = m_mark_begin;
950                                 if (!m_mouse_marking)
951                                         newMarkBegin = m_cursor_pos;
952
953                                 m_mouse_marking = true;
954                                 setTextMarkers(newMarkBegin, m_cursor_pos);
955                                 calculateScrollPos();
956                                 return true;
957                         }
958                 }
959         default:
960                 break;
961         }
962
963         return false;
964 }
965
966
967 s32 GUIEditBoxWithScrollBar::getCursorPos(s32 x, s32 y)
968 {
969         IGUIFont* font = getActiveFont();
970
971         const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
972
973         core::stringw *txt_line = 0;
974         s32 start_pos = 0;
975         x += 3;
976
977         for (u32 i = 0; i < line_count; ++i) {
978                 setTextRect(i);
979                 if (i == 0 && y < m_current_text_rect.UpperLeftCorner.Y)
980                         y = m_current_text_rect.UpperLeftCorner.Y;
981                 if (i == line_count - 1 && y > m_current_text_rect.LowerRightCorner.Y)
982                         y = m_current_text_rect.LowerRightCorner.Y;
983
984                 // is it inside this region?
985                 if (y >= m_current_text_rect.UpperLeftCorner.Y && y <= m_current_text_rect.LowerRightCorner.Y) {
986                         // we've found the clicked line
987                         txt_line = (m_word_wrap || m_multiline) ? &m_broken_text[i] : &Text;
988                         start_pos = (m_word_wrap || m_multiline) ? m_broken_text_positions[i] : 0;
989                         break;
990                 }
991         }
992
993         if (x < m_current_text_rect.UpperLeftCorner.X)
994                 x = m_current_text_rect.UpperLeftCorner.X;
995
996         if (!txt_line)
997                 return 0;
998
999         s32 idx = font->getCharacterFromPos(txt_line->c_str(), x - m_current_text_rect.UpperLeftCorner.X);
1000
1001         // click was on or left of the line
1002         if (idx != -1)
1003                 return idx + start_pos;
1004
1005         // click was off the right edge of the line, go to end.
1006         return txt_line->size() + start_pos;
1007 }
1008
1009
1010 //! Breaks the single text line.
1011 void GUIEditBoxWithScrollBar::breakText()
1012 {
1013         if ((!m_word_wrap && !m_multiline))
1014                 return;
1015
1016         m_broken_text.clear(); // need to reallocate :/
1017         m_broken_text_positions.clear();
1018
1019         IGUIFont* font = getActiveFont();
1020         if (!font)
1021                 return;
1022
1023         m_last_break_font = font;
1024
1025         core::stringw line;
1026         core::stringw word;
1027         core::stringw whitespace;
1028         s32 last_line_start = 0;
1029         s32 size = Text.size();
1030         s32 length = 0;
1031         s32 el_width = RelativeRect.getWidth() - 6;
1032         wchar_t c;
1033
1034         for (s32 i = 0; i < size; ++i) {
1035                 c = Text[i];
1036                 bool line_break = false;
1037
1038                 if (c == L'\r') { // Mac or Windows breaks
1039
1040                         line_break = true;
1041                         c = 0;
1042                         if (Text[i + 1] == L'\n') { // Windows breaks
1043                                 // TODO: I (Michael) think that we shouldn't change the text given by the user for whatever reason.
1044                                 // Instead rework the cursor positioning to be able to handle this (but not in stable release
1045                                 // branch as users might already expect this behavior).
1046                                 Text.erase(i + 1);
1047                                 --size;
1048                                 if (m_cursor_pos > i)
1049                                         --m_cursor_pos;
1050                         }
1051                 } else if (c == L'\n') { // Unix breaks
1052                         line_break = true;
1053                         c = 0;
1054                 }
1055
1056                 // don't break if we're not a multi-line edit box
1057                 if (!m_multiline)
1058                         line_break = false;
1059
1060                 if (c == L' ' || c == 0 || i == (size - 1)) {
1061                         // here comes the next whitespace, look if
1062                         // we can break the last word to the next line
1063                         // We also break whitespace, otherwise cursor would vanish beside the right border.
1064                         s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1065                         s32 worldlgth = font->getDimension(word.c_str()).Width;
1066
1067                         if (m_word_wrap && length + worldlgth + whitelgth > el_width && line.size() > 0) {
1068                                 // break to next line
1069                                 length = worldlgth;
1070                                 m_broken_text.push_back(line);
1071                                 m_broken_text_positions.push_back(last_line_start);
1072                                 last_line_start = i - (s32)word.size();
1073                                 line = word;
1074                         } else {
1075                                 // add word to line
1076                                 line += whitespace;
1077                                 line += word;
1078                                 length += whitelgth + worldlgth;
1079                         }
1080
1081                         word = L"";
1082                         whitespace = L"";
1083
1084
1085                         if (c)
1086                                 whitespace += c;
1087
1088                         // compute line break
1089                         if (line_break) {
1090                                 line += whitespace;
1091                                 line += word;
1092                                 m_broken_text.push_back(line);
1093                                 m_broken_text_positions.push_back(last_line_start);
1094                                 last_line_start = i + 1;
1095                                 line = L"";
1096                                 word = L"";
1097                                 whitespace = L"";
1098                                 length = 0;
1099                         }
1100                 } else {
1101                         // yippee this is a word..
1102                         word += c;
1103                 }
1104         }
1105
1106         line += whitespace;
1107         line += word;
1108         m_broken_text.push_back(line);
1109         m_broken_text_positions.push_back(last_line_start);
1110 }
1111
1112 // TODO: that function does interpret VAlign according to line-index (indexed
1113 // line is placed on top-center-bottom) but HAlign according to line-width
1114 // (pixels) and not by row.
1115 // Intuitively I suppose HAlign handling is better as VScrollPos should handle
1116 // the line-scrolling.
1117 // But please no one change this without also rewriting (and this time
1118 // testing!!!) autoscrolling (I noticed this when fixing the old autoscrolling).
1119 void GUIEditBoxWithScrollBar::setTextRect(s32 line)
1120 {
1121         if (line < 0)
1122                 return;
1123
1124         IGUIFont* font = getActiveFont();
1125         if (!font)
1126                 return;
1127
1128         core::dimension2du d;
1129
1130         // get text dimension
1131         const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
1132         if (m_word_wrap || m_multiline) {
1133                 d = font->getDimension(m_broken_text[line].c_str());
1134         } else {
1135                 d = font->getDimension(Text.c_str());
1136                 d.Height = AbsoluteRect.getHeight();
1137         }
1138         d.Height += font->getKerningHeight();
1139
1140         // justification
1141         switch (m_halign) {
1142         case EGUIA_CENTER:
1143                 // align to h centre
1144                 m_current_text_rect.UpperLeftCorner.X = (m_frame_rect.getWidth() / 2) - (d.Width / 2);
1145                 m_current_text_rect.LowerRightCorner.X = (m_frame_rect.getWidth() / 2) + (d.Width / 2);
1146                 break;
1147         case EGUIA_LOWERRIGHT:
1148                 // align to right edge
1149                 m_current_text_rect.UpperLeftCorner.X = m_frame_rect.getWidth() - d.Width;
1150                 m_current_text_rect.LowerRightCorner.X = m_frame_rect.getWidth();
1151                 break;
1152         default:
1153                 // align to left edge
1154                 m_current_text_rect.UpperLeftCorner.X = 0;
1155                 m_current_text_rect.LowerRightCorner.X = d.Width;
1156
1157         }
1158
1159         switch (m_valign) {
1160         case EGUIA_CENTER:
1161                 // align to v centre
1162                 m_current_text_rect.UpperLeftCorner.Y =
1163                         (m_frame_rect.getHeight() / 2) - (line_count*d.Height) / 2 + d.Height*line;
1164                 break;
1165         case EGUIA_LOWERRIGHT:
1166                 // align to bottom edge
1167                 m_current_text_rect.UpperLeftCorner.Y =
1168                         m_frame_rect.getHeight() - line_count*d.Height + d.Height*line;
1169                 break;
1170         default:
1171                 // align to top edge
1172                 m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
1173                 break;
1174         }
1175
1176         m_current_text_rect.UpperLeftCorner.X -= m_hscroll_pos;
1177         m_current_text_rect.LowerRightCorner.X -= m_hscroll_pos;
1178         m_current_text_rect.UpperLeftCorner.Y -= m_vscroll_pos;
1179         m_current_text_rect.LowerRightCorner.Y = m_current_text_rect.UpperLeftCorner.Y + d.Height;
1180
1181         m_current_text_rect += m_frame_rect.UpperLeftCorner;
1182 }
1183
1184
1185 s32 GUIEditBoxWithScrollBar::getLineFromPos(s32 pos)
1186 {
1187         if (!m_word_wrap && !m_multiline)
1188                 return 0;
1189
1190         s32 i = 0;
1191         while (i < (s32)m_broken_text_positions.size()) {
1192                 if (m_broken_text_positions[i] > pos)
1193                         return i - 1;
1194                 ++i;
1195         }
1196         return (s32)m_broken_text_positions.size() - 1;
1197 }
1198
1199
1200 void GUIEditBoxWithScrollBar::inputChar(wchar_t c)
1201 {
1202         if (!isEnabled())
1203                 return;
1204
1205         if (c != 0)     {
1206                 if (Text.size() < m_max || m_max == 0) {
1207                         core::stringw s;
1208
1209                         if (m_mark_begin != m_mark_end) {
1210                                 // replace marked text
1211                                 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
1212                                 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
1213
1214                                 s = Text.subString(0, realmbgn);
1215                                 s.append(c);
1216                                 s.append(Text.subString(realmend, Text.size() - realmend));
1217                                 Text = s;
1218                                 m_cursor_pos = realmbgn + 1;
1219                         } else {
1220                                 // add new character
1221                                 s = Text.subString(0, m_cursor_pos);
1222                                 s.append(c);
1223                                 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
1224                                 Text = s;
1225                                 ++m_cursor_pos;
1226                         }
1227
1228                         m_blink_start_time = porting::getTimeMs();
1229                         setTextMarkers(0, 0);
1230                 }
1231         }
1232         breakText();
1233         calculateScrollPos();
1234         sendGuiEvent(EGET_EDITBOX_CHANGED);
1235 }
1236
1237 // calculate autoscroll
1238 void GUIEditBoxWithScrollBar::calculateScrollPos()
1239 {
1240         if (!m_autoscroll)
1241                 return;
1242
1243         IGUISkin* skin = Environment->getSkin();
1244         if (!skin)
1245                 return;
1246         IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
1247         if (!font)
1248                 return;
1249
1250         s32 curs_line = getLineFromPos(m_cursor_pos);
1251         if (curs_line < 0)
1252                 return;
1253         setTextRect(curs_line);
1254         const bool has_broken_text = m_multiline || m_word_wrap;
1255
1256         // Check horizonal scrolling
1257         // NOTE: Calculations different to vertical scrolling because setTextRect interprets VAlign relative to line but HAlign not relative to row
1258         {
1259                 // get cursor position
1260                 IGUIFont* font = getActiveFont();
1261                 if (!font)
1262                         return;
1263
1264                 // get cursor area
1265                 irr::u32 cursor_width = font->getDimension(L"_").Width;
1266                 core::stringw *txt_line = has_broken_text ? &m_broken_text[curs_line] : &Text;
1267                 s32 cpos = has_broken_text ? m_cursor_pos - m_broken_text_positions[curs_line] : m_cursor_pos;  // column
1268                 s32 cstart = font->getDimension(txt_line->subString(0, cpos).c_str()).Width;            // pixels from text-start
1269                 s32 cend = cstart + cursor_width;
1270                 s32 txt_width = font->getDimension(txt_line->c_str()).Width;
1271
1272                 if (txt_width < m_frame_rect.getWidth()) {
1273                         // TODO: Needs a clean left and right gap removal depending on HAlign, similar to vertical scrolling tests for top/bottom.
1274                         // This check just fixes the case where it was most noticable (text smaller than clipping area).
1275
1276                         m_hscroll_pos = 0;
1277                         setTextRect(curs_line);
1278                 }
1279
1280                 if (m_current_text_rect.UpperLeftCorner.X + cstart < m_frame_rect.UpperLeftCorner.X) {
1281                         // cursor to the left of the clipping area
1282                         m_hscroll_pos -= m_frame_rect.UpperLeftCorner.X - (m_current_text_rect.UpperLeftCorner.X + cstart);
1283                         setTextRect(curs_line);
1284
1285                         // TODO: should show more characters to the left when we're scrolling left
1286                         //      and the cursor reaches the border.
1287                 } else if (m_current_text_rect.UpperLeftCorner.X + cend > m_frame_rect.LowerRightCorner.X)      {
1288                         // cursor to the right of the clipping area
1289                         m_hscroll_pos += (m_current_text_rect.UpperLeftCorner.X + cend) - m_frame_rect.LowerRightCorner.X;
1290                         setTextRect(curs_line);
1291                 }
1292         }
1293
1294         // calculate vertical scrolling
1295         if (has_broken_text) {
1296                 irr::u32 line_height = font->getDimension(L"A").Height + font->getKerningHeight();
1297                 // only up to 1 line fits?
1298                 if (line_height >= (irr::u32)m_frame_rect.getHeight()) {
1299                         m_vscroll_pos = 0;
1300                         setTextRect(curs_line);
1301                         s32 unscrolledPos = m_current_text_rect.UpperLeftCorner.Y;
1302                         s32 pivot = m_frame_rect.UpperLeftCorner.Y;
1303                         switch (m_valign) {
1304                         case EGUIA_CENTER:
1305                                 pivot += m_frame_rect.getHeight() / 2;
1306                                 unscrolledPos += line_height / 2;
1307                                 break;
1308                         case EGUIA_LOWERRIGHT:
1309                                 pivot += m_frame_rect.getHeight();
1310                                 unscrolledPos += line_height;
1311                                 break;
1312                         default:
1313                                 break;
1314                         }
1315                         m_vscroll_pos = unscrolledPos - pivot;
1316                         setTextRect(curs_line);
1317                 } else {
1318                         // First 2 checks are necessary when people delete lines
1319                         setTextRect(0);
1320                         if (m_current_text_rect.UpperLeftCorner.Y > m_frame_rect.UpperLeftCorner.Y && m_valign != EGUIA_LOWERRIGHT) {
1321                                 // first line is leaving a gap on top
1322                                 m_vscroll_pos = 0;
1323                         } else if (m_valign != EGUIA_UPPERLEFT) {
1324                                 u32 lastLine = m_broken_text_positions.empty() ? 0 : m_broken_text_positions.size() - 1;
1325                                 setTextRect(lastLine);
1326                                 if (m_current_text_rect.LowerRightCorner.Y < m_frame_rect.LowerRightCorner.Y)
1327                                 {
1328                                         // last line is leaving a gap on bottom
1329                                         m_vscroll_pos -= m_frame_rect.LowerRightCorner.Y - m_current_text_rect.LowerRightCorner.Y;
1330                                 }
1331                         }
1332
1333                         setTextRect(curs_line);
1334                         if (m_current_text_rect.UpperLeftCorner.Y < m_frame_rect.UpperLeftCorner.Y) {
1335                                 // text above valid area
1336                                 m_vscroll_pos -= m_frame_rect.UpperLeftCorner.Y - m_current_text_rect.UpperLeftCorner.Y;
1337                                 setTextRect(curs_line);
1338                         } else if (m_current_text_rect.LowerRightCorner.Y > m_frame_rect.LowerRightCorner.Y){
1339                                 // text below valid area
1340                                 m_vscroll_pos += m_current_text_rect.LowerRightCorner.Y - m_frame_rect.LowerRightCorner.Y;
1341                                 setTextRect(curs_line);
1342                         }
1343                 }
1344         }
1345
1346         if (m_vscrollbar) {
1347                 m_vscrollbar->setPos(m_vscroll_pos);
1348         }
1349 }
1350
1351 void GUIEditBoxWithScrollBar::calculateFrameRect()
1352 {
1353         m_frame_rect = AbsoluteRect;
1354
1355
1356         IGUISkin *skin = 0;
1357         if (Environment)
1358                 skin = Environment->getSkin();
1359         if (m_border && skin) {
1360                 m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
1361                 m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
1362                 m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
1363                 m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
1364         }
1365
1366         updateVScrollBar();
1367 }
1368
1369 //! set text markers
1370 void GUIEditBoxWithScrollBar::setTextMarkers(s32 begin, s32 end)
1371 {
1372         if (begin != m_mark_begin || end != m_mark_end) {
1373                 m_mark_begin = begin;
1374                 m_mark_end = end;
1375                 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1376         }
1377 }
1378
1379 //! send some gui event to parent
1380 void GUIEditBoxWithScrollBar::sendGuiEvent(EGUI_EVENT_TYPE type)
1381 {
1382         if (Parent) {
1383                 SEvent e;
1384                 e.EventType = EET_GUI_EVENT;
1385                 e.GUIEvent.Caller = this;
1386                 e.GUIEvent.Element = 0;
1387                 e.GUIEvent.EventType = type;
1388
1389                 Parent->OnEvent(e);
1390         }
1391 }
1392
1393 //! create a vertical scroll bar
1394 void GUIEditBoxWithScrollBar::createVScrollBar()
1395 {
1396         IGUISkin *skin = 0;
1397         if (Environment)
1398                 skin = Environment->getSkin();
1399
1400         m_scrollbar_width = skin ? skin->getSize(gui::EGDS_SCROLLBAR_SIZE) : 16;
1401
1402         RelativeRect.LowerRightCorner.X -= m_scrollbar_width + 4;
1403
1404         irr::core::rect<s32> scrollbarrect = m_frame_rect;
1405         scrollbarrect.UpperLeftCorner.X += m_frame_rect.getWidth() - m_scrollbar_width;
1406         m_vscrollbar = new GUIScrollBar(Environment, getParent(), -1,
1407                         scrollbarrect, false, true);
1408
1409         m_vscrollbar->setVisible(false);
1410         m_vscrollbar->setSmallStep(1);
1411         m_vscrollbar->setLargeStep(1);
1412 }
1413
1414 void GUIEditBoxWithScrollBar::updateVScrollBar()
1415 {
1416         if (!m_vscrollbar) {
1417                 return;
1418         }
1419
1420         // OnScrollBarChanged(...)
1421         if (m_vscrollbar->getPos() != m_vscroll_pos) {
1422                 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
1423                 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
1424                 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
1425
1426                 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
1427                 if (scrollymax != m_vscrollbar->getMax()) {
1428                         // manage a newline or a deleted line
1429                         m_vscrollbar->setMax(scrollymax);
1430                         m_vscrollbar->setPageSize(s32(getTextDimension().Height));
1431                         calculateScrollPos();
1432                 } else {
1433                         // manage a newline or a deleted line
1434                         m_vscroll_pos = m_vscrollbar->getPos();
1435                 }
1436         }
1437
1438         // check if a vertical scrollbar is needed ?
1439         if (getTextDimension().Height > (u32) m_frame_rect.getHeight()) {
1440                 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
1441
1442                 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
1443                 if (scrollymax != m_vscrollbar->getMax()) {
1444                         m_vscrollbar->setMax(scrollymax);
1445                         m_vscrollbar->setPageSize(s32(getTextDimension().Height));
1446                 }
1447
1448                 if (!m_vscrollbar->isVisible()) {
1449                         m_vscrollbar->setVisible(true);
1450                 }
1451         } else {
1452                 if (m_vscrollbar->isVisible())
1453                 {
1454                         m_vscrollbar->setVisible(false);
1455                         m_vscroll_pos = 0;
1456                         m_vscrollbar->setPos(0);
1457                         m_vscrollbar->setMax(1);
1458                         m_vscrollbar->setPageSize(s32(getTextDimension().Height));
1459                 }
1460         }
1461
1462 }
1463
1464 //! set true if this editbox is writable
1465 void GUIEditBoxWithScrollBar::setWritable(bool writable)
1466 {
1467         m_writable = writable;
1468 }
1469
1470 //! Change the background color
1471 void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color)
1472 {
1473         m_bg_color = bg_color;
1474         m_bg_color_used = true;
1475 }
1476
1477 //! Writes attributes of the element.
1478 void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const
1479 {
1480         // IGUIEditBox::serializeAttributes(out,options);
1481
1482         out->addBool("Border", m_border);
1483         out->addBool("Background", m_background);
1484         out->addBool("OverrideColorEnabled", m_override_color_enabled);
1485         out->addColor("OverrideColor", m_override_color);
1486         // out->addFont("OverrideFont", OverrideFont);
1487         out->addInt("MaxChars", m_max);
1488         out->addBool("WordWrap", m_word_wrap);
1489         out->addBool("MultiLine", m_multiline);
1490         out->addBool("AutoScroll", m_autoscroll);
1491         out->addBool("PasswordBox", m_passwordbox);
1492         core::stringw ch = L" ";
1493         ch[0] = m_passwordchar;
1494         out->addString("PasswordChar", ch.c_str());
1495         out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
1496         out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
1497         out->addBool("Writable", m_writable);
1498
1499         IGUIEditBox::serializeAttributes(out, options);
1500 }
1501
1502
1503 //! Reads attributes of the element
1504 void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0)
1505 {
1506         IGUIEditBox::deserializeAttributes(in, options);
1507
1508         setDrawBorder(in->getAttributeAsBool("Border"));
1509         setDrawBackground(in->getAttributeAsBool("Background"));
1510         setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1511         enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1512         setMax(in->getAttributeAsInt("MaxChars"));
1513         setWordWrap(in->getAttributeAsBool("WordWrap"));
1514         setMultiLine(in->getAttributeAsBool("MultiLine"));
1515         setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1516         core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1517
1518         if (!ch.size())
1519                 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1520         else
1521                 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1522
1523         setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1524                 (EGUI_ALIGNMENT)in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1525
1526         // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1527         setWritable(in->getAttributeAsBool("Writable"));
1528 }
1529
1530 bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; }
1531 bool GUIEditBoxWithScrollBar::isDrawBorderEnabled() const { return false; }
1532 void GUIEditBoxWithScrollBar::setCursorChar(const wchar_t cursorChar) { }
1533 wchar_t GUIEditBoxWithScrollBar::getCursorChar() const { return '|'; }
1534 void GUIEditBoxWithScrollBar::setCursorBlinkTime(irr::u32 timeMs) { }
1535 irr::u32 GUIEditBoxWithScrollBar::getCursorBlinkTime() const { return 500; }