]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiEditBoxWithScrollbar.cpp
Drop .NET-specific workaround: _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX
[dragonfireclient.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 /*
18 todo:
19 optional scrollbars [done]
20 ctrl+left/right to select word
21 double click/ctrl click: word select + drag to select whole words, triple click to select line
22 optional? dragging selected text
23 numerical
24 */
25
26
27 //! constructor
28 GUIEditBoxWithScrollBar::GUIEditBoxWithScrollBar(const wchar_t* text, bool border,
29         IGUIEnvironment* environment, IGUIElement* parent, s32 id,
30         const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
31         : IGUIEditBox(environment, parent, id, rectangle), m_mouse_marking(false),
32         m_border(border), m_background(true), m_override_color_enabled(false), m_mark_begin(0), m_mark_end(0),
33         m_override_color(video::SColor(101, 255, 255, 255)), m_override_font(0), m_last_break_font(0),
34         m_operator(0), m_blink_start_time(0), m_cursor_pos(0), m_hscroll_pos(0), m_vscroll_pos(0), m_max(0),
35         m_word_wrap(false), m_multiline(false), m_autoscroll(true), m_passwordbox(false),
36         m_passwordchar(L'*'), m_halign(EGUIA_UPPERLEFT), m_valign(EGUIA_CENTER),
37         m_current_text_rect(0, 0, 1, 1), m_frame_rect(rectangle),
38         m_scrollbar_width(0), m_vscrollbar(NULL), m_writable(writable),
39         m_bg_color_used(false)
40 {
41 #ifdef _DEBUG
42         setDebugName("GUIEditBoxWithScrollBar");
43 #endif
44
45
46         Text = text;
47
48         if (Environment)
49                 m_operator = Environment->getOSOperator();
50
51         if (m_operator)
52                 m_operator->grab();
53
54         // this element can be tabbed to
55         setTabStop(true);
56         setTabOrder(-1);
57
58         if (has_vscrollbar) {
59                 createVScrollBar();
60         }
61
62         calculateFrameRect();
63         breakText();
64
65         calculateScrollPos();
66         setWritable(writable);
67 }
68
69
70 //! destructor
71 GUIEditBoxWithScrollBar::~GUIEditBoxWithScrollBar()
72 {
73         if (m_override_font)
74                 m_override_font->drop();
75
76         if (m_operator)
77                 m_operator->drop();
78
79         m_vscrollbar->remove();
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 line is placed on top-center-bottom)
1113 // but HAlign according to line-width (pixels) and not by row.
1114 // Intuitively I suppose HAlign handling is better as VScrollPos should handle the line-scrolling.
1115 // But please no one change this without also rewriting (and this time fucking testing!!!) autoscrolling (I noticed this when fixing the old autoscrolling).
1116 void GUIEditBoxWithScrollBar::setTextRect(s32 line)
1117 {
1118         if (line < 0)
1119                 return;
1120
1121         IGUIFont* font = getActiveFont();
1122         if (!font)
1123                 return;
1124
1125         core::dimension2du d;
1126
1127         // get text dimension
1128         const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
1129         if (m_word_wrap || m_multiline) {
1130                 d = font->getDimension(m_broken_text[line].c_str());
1131         } else {
1132                 d = font->getDimension(Text.c_str());
1133                 d.Height = AbsoluteRect.getHeight();
1134         }
1135         d.Height += font->getKerningHeight();
1136
1137         // justification
1138         switch (m_halign) {
1139         case EGUIA_CENTER:
1140                 // align to h centre
1141                 m_current_text_rect.UpperLeftCorner.X = (m_frame_rect.getWidth() / 2) - (d.Width / 2);
1142                 m_current_text_rect.LowerRightCorner.X = (m_frame_rect.getWidth() / 2) + (d.Width / 2);
1143                 break;
1144         case EGUIA_LOWERRIGHT:
1145                 // align to right edge
1146                 m_current_text_rect.UpperLeftCorner.X = m_frame_rect.getWidth() - d.Width;
1147                 m_current_text_rect.LowerRightCorner.X = m_frame_rect.getWidth();
1148                 break;
1149         default:
1150                 // align to left edge
1151                 m_current_text_rect.UpperLeftCorner.X = 0;
1152                 m_current_text_rect.LowerRightCorner.X = d.Width;
1153
1154         }
1155
1156         switch (m_valign) {
1157         case EGUIA_CENTER:
1158                 // align to v centre
1159                 m_current_text_rect.UpperLeftCorner.Y =
1160                         (m_frame_rect.getHeight() / 2) - (line_count*d.Height) / 2 + d.Height*line;
1161                 break;
1162         case EGUIA_LOWERRIGHT:
1163                 // align to bottom edge
1164                 m_current_text_rect.UpperLeftCorner.Y =
1165                         m_frame_rect.getHeight() - line_count*d.Height + d.Height*line;
1166                 break;
1167         default:
1168                 // align to top edge
1169                 m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
1170                 break;
1171         }
1172
1173         m_current_text_rect.UpperLeftCorner.X -= m_hscroll_pos;
1174         m_current_text_rect.LowerRightCorner.X -= m_hscroll_pos;
1175         m_current_text_rect.UpperLeftCorner.Y -= m_vscroll_pos;
1176         m_current_text_rect.LowerRightCorner.Y = m_current_text_rect.UpperLeftCorner.Y + d.Height;
1177
1178         m_current_text_rect += m_frame_rect.UpperLeftCorner;
1179 }
1180
1181
1182 s32 GUIEditBoxWithScrollBar::getLineFromPos(s32 pos)
1183 {
1184         if (!m_word_wrap && !m_multiline)
1185                 return 0;
1186
1187         s32 i = 0;
1188         while (i < (s32)m_broken_text_positions.size()) {
1189                 if (m_broken_text_positions[i] > pos)
1190                         return i - 1;
1191                 ++i;
1192         }
1193         return (s32)m_broken_text_positions.size() - 1;
1194 }
1195
1196
1197 void GUIEditBoxWithScrollBar::inputChar(wchar_t c)
1198 {
1199         if (!isEnabled())
1200                 return;
1201
1202         if (c != 0)     {
1203                 if (Text.size() < m_max || m_max == 0) {
1204                         core::stringw s;
1205
1206                         if (m_mark_begin != m_mark_end) {
1207                                 // replace marked text
1208                                 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
1209                                 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
1210
1211                                 s = Text.subString(0, realmbgn);
1212                                 s.append(c);
1213                                 s.append(Text.subString(realmend, Text.size() - realmend));
1214                                 Text = s;
1215                                 m_cursor_pos = realmbgn + 1;
1216                         } else {
1217                                 // add new character
1218                                 s = Text.subString(0, m_cursor_pos);
1219                                 s.append(c);
1220                                 s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
1221                                 Text = s;
1222                                 ++m_cursor_pos;
1223                         }
1224
1225                         m_blink_start_time = porting::getTimeMs();
1226                         setTextMarkers(0, 0);
1227                 }
1228         }
1229         breakText();
1230         calculateScrollPos();
1231         sendGuiEvent(EGET_EDITBOX_CHANGED);
1232 }
1233
1234 // calculate autoscroll
1235 void GUIEditBoxWithScrollBar::calculateScrollPos()
1236 {
1237         if (!m_autoscroll)
1238                 return;
1239
1240         IGUISkin* skin = Environment->getSkin();
1241         if (!skin)
1242                 return;
1243         IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
1244         if (!font)
1245                 return;
1246
1247         s32 curs_line = getLineFromPos(m_cursor_pos);
1248         if (curs_line < 0)
1249                 return;
1250         setTextRect(curs_line);
1251         const bool has_broken_text = m_multiline || m_word_wrap;
1252
1253         // Check horizonal scrolling
1254         // NOTE: Calculations different to vertical scrolling because setTextRect interprets VAlign relative to line but HAlign not relative to row
1255         {
1256                 // get cursor position
1257                 IGUIFont* font = getActiveFont();
1258                 if (!font)
1259                         return;
1260
1261                 // get cursor area
1262                 irr::u32 cursor_width = font->getDimension(L"_").Width;
1263                 core::stringw *txt_line = has_broken_text ? &m_broken_text[curs_line] : &Text;
1264                 s32 cpos = has_broken_text ? m_cursor_pos - m_broken_text_positions[curs_line] : m_cursor_pos;  // column
1265                 s32 cstart = font->getDimension(txt_line->subString(0, cpos).c_str()).Width;            // pixels from text-start
1266                 s32 cend = cstart + cursor_width;
1267                 s32 txt_width = font->getDimension(txt_line->c_str()).Width;
1268
1269                 if (txt_width < m_frame_rect.getWidth()) {
1270                         // TODO: Needs a clean left and right gap removal depending on HAlign, similar to vertical scrolling tests for top/bottom.
1271                         // This check just fixes the case where it was most noticable (text smaller than clipping area).
1272
1273                         m_hscroll_pos = 0;
1274                         setTextRect(curs_line);
1275                 }
1276
1277                 if (m_current_text_rect.UpperLeftCorner.X + cstart < m_frame_rect.UpperLeftCorner.X) {
1278                         // cursor to the left of the clipping area
1279                         m_hscroll_pos -= m_frame_rect.UpperLeftCorner.X - (m_current_text_rect.UpperLeftCorner.X + cstart);
1280                         setTextRect(curs_line);
1281
1282                         // TODO: should show more characters to the left when we're scrolling left
1283                         //      and the cursor reaches the border.
1284                 } else if (m_current_text_rect.UpperLeftCorner.X + cend > m_frame_rect.LowerRightCorner.X)      {
1285                         // cursor to the right of the clipping area
1286                         m_hscroll_pos += (m_current_text_rect.UpperLeftCorner.X + cend) - m_frame_rect.LowerRightCorner.X;
1287                         setTextRect(curs_line);
1288                 }
1289         }
1290
1291         // calculate vertical scrolling
1292         if (has_broken_text) {
1293                 irr::u32 line_height = font->getDimension(L"A").Height + font->getKerningHeight();
1294                 // only up to 1 line fits?
1295                 if (line_height >= (irr::u32)m_frame_rect.getHeight()) {
1296                         m_vscroll_pos = 0;
1297                         setTextRect(curs_line);
1298                         s32 unscrolledPos = m_current_text_rect.UpperLeftCorner.Y;
1299                         s32 pivot = m_frame_rect.UpperLeftCorner.Y;
1300                         switch (m_valign) {
1301                         case EGUIA_CENTER:
1302                                 pivot += m_frame_rect.getHeight() / 2;
1303                                 unscrolledPos += line_height / 2;
1304                                 break;
1305                         case EGUIA_LOWERRIGHT:
1306                                 pivot += m_frame_rect.getHeight();
1307                                 unscrolledPos += line_height;
1308                                 break;
1309                         default:
1310                                 break;
1311                         }
1312                         m_vscroll_pos = unscrolledPos - pivot;
1313                         setTextRect(curs_line);
1314                 } else {
1315                         // First 2 checks are necessary when people delete lines
1316                         setTextRect(0);
1317                         if (m_current_text_rect.UpperLeftCorner.Y > m_frame_rect.UpperLeftCorner.Y && m_valign != EGUIA_LOWERRIGHT) {
1318                                 // first line is leaving a gap on top
1319                                 m_vscroll_pos = 0;
1320                         } else if (m_valign != EGUIA_UPPERLEFT) {
1321                                 u32 lastLine = m_broken_text_positions.empty() ? 0 : m_broken_text_positions.size() - 1;
1322                                 setTextRect(lastLine);
1323                                 if (m_current_text_rect.LowerRightCorner.Y < m_frame_rect.LowerRightCorner.Y)
1324                                 {
1325                                         // last line is leaving a gap on bottom
1326                                         m_vscroll_pos -= m_frame_rect.LowerRightCorner.Y - m_current_text_rect.LowerRightCorner.Y;
1327                                 }
1328                         }
1329
1330                         setTextRect(curs_line);
1331                         if (m_current_text_rect.UpperLeftCorner.Y < m_frame_rect.UpperLeftCorner.Y) {
1332                                 // text above valid area
1333                                 m_vscroll_pos -= m_frame_rect.UpperLeftCorner.Y - m_current_text_rect.UpperLeftCorner.Y;
1334                                 setTextRect(curs_line);
1335                         } else if (m_current_text_rect.LowerRightCorner.Y > m_frame_rect.LowerRightCorner.Y){
1336                                 // text below valid area
1337                                 m_vscroll_pos += m_current_text_rect.LowerRightCorner.Y - m_frame_rect.LowerRightCorner.Y;
1338                                 setTextRect(curs_line);
1339                         }
1340                 }
1341         }
1342
1343         if (m_vscrollbar) {
1344                 m_vscrollbar->setPos(m_vscroll_pos);
1345         }
1346 }
1347
1348 void GUIEditBoxWithScrollBar::calculateFrameRect()
1349 {
1350         m_frame_rect = AbsoluteRect;
1351
1352
1353         IGUISkin *skin = 0;
1354         if (Environment)
1355                 skin = Environment->getSkin();
1356         if (m_border && skin) {
1357                 m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
1358                 m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
1359                 m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
1360                 m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
1361         }
1362
1363         updateVScrollBar();
1364 }
1365
1366 //! set text markers
1367 void GUIEditBoxWithScrollBar::setTextMarkers(s32 begin, s32 end)
1368 {
1369         if (begin != m_mark_begin || end != m_mark_end) {
1370                 m_mark_begin = begin;
1371                 m_mark_end = end;
1372                 sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1373         }
1374 }
1375
1376 //! send some gui event to parent
1377 void GUIEditBoxWithScrollBar::sendGuiEvent(EGUI_EVENT_TYPE type)
1378 {
1379         if (Parent) {
1380                 SEvent e;
1381                 e.EventType = EET_GUI_EVENT;
1382                 e.GUIEvent.Caller = this;
1383                 e.GUIEvent.Element = 0;
1384                 e.GUIEvent.EventType = type;
1385
1386                 Parent->OnEvent(e);
1387         }
1388 }
1389
1390 //! create a vertical scroll bar
1391 void GUIEditBoxWithScrollBar::createVScrollBar()
1392 {
1393         IGUISkin *skin = 0;
1394         if (Environment)
1395                 skin = Environment->getSkin();
1396
1397         m_scrollbar_width = skin ? skin->getSize(gui::EGDS_SCROLLBAR_SIZE) : 16;
1398
1399         RelativeRect.LowerRightCorner.X -= m_scrollbar_width + 4;
1400
1401         irr::core::rect<s32> scrollbarrect = m_frame_rect;
1402         scrollbarrect.UpperLeftCorner.X += m_frame_rect.getWidth() - m_scrollbar_width;
1403         m_vscrollbar = Environment->addScrollBar(false, scrollbarrect, getParent(), getID());
1404         m_vscrollbar->setVisible(false);
1405         m_vscrollbar->setSmallStep(1);
1406         m_vscrollbar->setLargeStep(1);
1407 }
1408
1409 void GUIEditBoxWithScrollBar::updateVScrollBar()
1410 {
1411         if (!m_vscrollbar) {
1412                 return;
1413         }
1414
1415         // OnScrollBarChanged(...)
1416         if (m_vscrollbar->getPos() != m_vscroll_pos) {
1417                 s32 deltaScrollY = m_vscrollbar->getPos() - m_vscroll_pos;
1418                 m_current_text_rect.UpperLeftCorner.Y -= deltaScrollY;
1419                 m_current_text_rect.LowerRightCorner.Y -= deltaScrollY;
1420
1421                 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
1422                 if (scrollymax != m_vscrollbar->getMax()) {
1423                         // manage a newline or a deleted line
1424                         m_vscrollbar->setMax(scrollymax);
1425                         calculateScrollPos();
1426                 } else {
1427                         // manage a newline or a deleted line
1428                         m_vscroll_pos = m_vscrollbar->getPos();
1429                 }
1430         }
1431
1432         // check if a vertical scrollbar is needed ?
1433         if (getTextDimension().Height > (u32) m_frame_rect.getHeight()) {
1434                 m_frame_rect.LowerRightCorner.X -= m_scrollbar_width;
1435
1436                 s32 scrollymax = getTextDimension().Height - m_frame_rect.getHeight();
1437                 if (scrollymax != m_vscrollbar->getMax()) {
1438                         m_vscrollbar->setMax(scrollymax);
1439                 }
1440
1441                 if (!m_vscrollbar->isVisible()) {
1442                         m_vscrollbar->setVisible(true);
1443                 }
1444         } else {
1445                 if (m_vscrollbar->isVisible())
1446                 {
1447                         m_vscrollbar->setVisible(false);
1448                         m_vscroll_pos = 0;
1449                         m_vscrollbar->setPos(0);
1450                         m_vscrollbar->setMax(1);
1451                 }
1452         }
1453
1454
1455 }
1456
1457 //! set true if this editbox is writable
1458 void GUIEditBoxWithScrollBar::setWritable(bool writable)
1459 {
1460         m_writable = writable;
1461 }
1462
1463 //! Change the background color
1464 void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color)
1465 {
1466         m_bg_color = bg_color;
1467         m_bg_color_used = true;
1468 }
1469
1470 //! Writes attributes of the element.
1471 void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const
1472 {
1473         // IGUIEditBox::serializeAttributes(out,options);
1474
1475         out->addBool("Border", m_border);
1476         out->addBool("Background", m_background);
1477         out->addBool("OverrideColorEnabled", m_override_color_enabled);
1478         out->addColor("OverrideColor", m_override_color);
1479         // out->addFont("OverrideFont", OverrideFont);
1480         out->addInt("MaxChars", m_max);
1481         out->addBool("WordWrap", m_word_wrap);
1482         out->addBool("MultiLine", m_multiline);
1483         out->addBool("AutoScroll", m_autoscroll);
1484         out->addBool("PasswordBox", m_passwordbox);
1485         core::stringw ch = L" ";
1486         ch[0] = m_passwordchar;
1487         out->addString("PasswordChar", ch.c_str());
1488         out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
1489         out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
1490         out->addBool("Writable", m_writable);
1491
1492         IGUIEditBox::serializeAttributes(out, options);
1493 }
1494
1495
1496 //! Reads attributes of the element
1497 void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0)
1498 {
1499         IGUIEditBox::deserializeAttributes(in, options);
1500
1501         setDrawBorder(in->getAttributeAsBool("Border"));
1502         setDrawBackground(in->getAttributeAsBool("Background"));
1503         setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1504         enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1505         setMax(in->getAttributeAsInt("MaxChars"));
1506         setWordWrap(in->getAttributeAsBool("WordWrap"));
1507         setMultiLine(in->getAttributeAsBool("MultiLine"));
1508         setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1509         core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1510
1511         if (!ch.size())
1512                 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1513         else
1514                 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1515
1516         setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1517                 (EGUI_ALIGNMENT)in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1518
1519         // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1520         setWritable(in->getAttributeAsBool("Writable"));
1521 }
1522
1523 bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; }
1524 bool GUIEditBoxWithScrollBar::isDrawBorderEnabled() const { return false; }
1525 void GUIEditBoxWithScrollBar::setCursorChar(const wchar_t cursorChar) { }
1526 wchar_t GUIEditBoxWithScrollBar::getCursorChar() const { return '|'; }
1527 void GUIEditBoxWithScrollBar::setCursorBlinkTime(irr::u32 timeMs) { }
1528 irr::u32 GUIEditBoxWithScrollBar::getCursorBlinkTime() const { return 500; }