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