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