1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
5
#include "CGUIEditBox.h"
6
#ifdef _IRR_COMPILE_WITH_GUI_
9
#include "IGUIEnvironment.h"
11
#include "IVideoDriver.h"
19
ctrl+left/right to select word
20
double click/ctrl click: word select + drag to select whole words, triple click to select line
21
optional? dragging selected text
31
CGUIEditBox::CGUIEditBox(const wchar_t* text, bool border,
32
IGUIEnvironment* environment, IGUIElement* parent, s32 id,
33
const core::rect<s32>& rectangle)
34
: IGUIEditBox(environment, parent, id, rectangle), MouseMarking(false),
35
Border(border), Background(true), OverrideColorEnabled(false), MarkBegin(0), MarkEnd(0),
36
OverrideColor(video::SColor(101,255,255,255)), OverrideFont(0), LastBreakFont(0),
37
Operator(0), BlinkStartTime(0), CursorPos(0), HScrollPos(0), VScrollPos(0), Max(0),
38
WordWrap(false), MultiLine(false), AutoScroll(true), PasswordBox(false),
39
PasswordChar(L'*'), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER),
40
CurrentTextRect(0,0,1,1), FrameRect(rectangle)
43
setDebugName("CGUIEditBox");
49
Operator = Environment->getOSOperator();
54
// this element can be tabbed to
66
CGUIEditBox::~CGUIEditBox()
76
//! Sets another skin independent font.
77
void CGUIEditBox::setOverrideFont(IGUIFont* font)
79
if (OverrideFont == font)
93
//! Gets the override font (if any)
94
IGUIFont * CGUIEditBox::getOverrideFont() const
99
//! Get the font which is used right now for drawing
100
IGUIFont* CGUIEditBox::getActiveFont() const
104
IGUISkin* skin = Environment->getSkin();
106
return skin->getFont();
110
//! Sets another color for the text.
111
void CGUIEditBox::setOverrideColor(video::SColor color)
113
OverrideColor = color;
114
OverrideColorEnabled = true;
118
video::SColor const& CGUIEditBox::getOverrideColor() const
120
return OverrideColor;
124
//! Turns the border on or off
125
void CGUIEditBox::setDrawBorder(bool border)
130
//! Sets whether to draw the background
131
void CGUIEditBox::setDrawBackground(bool draw)
136
//! Sets if the text should use the overide color or the color in the gui skin.
137
void CGUIEditBox::enableOverrideColor(bool enable)
139
OverrideColorEnabled = enable;
142
bool CGUIEditBox::isOverrideColorEnabled() const
144
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
145
return OverrideColorEnabled;
148
//! Enables or disables word wrap
149
void CGUIEditBox::setWordWrap(bool enable)
156
void CGUIEditBox::updateAbsolutePosition()
158
core::rect<s32> oldAbsoluteRect(AbsoluteRect);
159
IGUIElement::updateAbsolutePosition();
160
if ( oldAbsoluteRect != AbsoluteRect )
162
calculateFrameRect();
164
calculateScrollPos();
169
//! Checks if word wrap is enabled
170
bool CGUIEditBox::isWordWrapEnabled() const
172
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
177
//! Enables or disables newlines.
178
void CGUIEditBox::setMultiLine(bool enable)
184
//! Checks if multi line editing is enabled
185
bool CGUIEditBox::isMultiLineEnabled() const
187
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
192
void CGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
194
PasswordBox = passwordBox;
197
PasswordChar = passwordChar;
205
bool CGUIEditBox::isPasswordBox() const
207
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
212
//! Sets text justification
213
void CGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
220
//! called if an event happened.
221
bool CGUIEditBox::OnEvent(const SEvent& event)
226
switch(event.EventType)
229
if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
231
if (event.GUIEvent.Caller == this)
233
MouseMarking = false;
238
case EET_KEY_INPUT_EVENT:
239
if (processKey(event))
242
case EET_MOUSE_INPUT_EVENT:
243
if (processMouse(event))
251
return IGUIElement::OnEvent(event);
255
bool CGUIEditBox::processKey(const SEvent& event)
257
if (!event.KeyInput.PressedDown)
260
bool textChanged = false;
261
s32 newMarkBegin = MarkBegin;
262
s32 newMarkEnd = MarkEnd;
264
// control shortcut handling
266
if (event.KeyInput.Control)
268
// german backlash '\' entered with control + '?'
269
if ( event.KeyInput.Char == '\\' )
271
inputChar(event.KeyInput.Char);
275
switch(event.KeyInput.Key)
280
newMarkEnd = Text.size();
284
if (!PasswordBox && Operator && MarkBegin != MarkEnd)
286
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
287
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
290
s = Text.subString(realmbgn, realmend - realmbgn).c_str();
291
Operator->copyToClipboard(s.c_str());
295
// cut to the clipboard
296
if (!PasswordBox && Operator && MarkBegin != MarkEnd)
298
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
299
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
303
sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
304
Operator->copyToClipboard(sc.c_str());
310
s = Text.subString(0, realmbgn);
311
s.append( Text.subString(realmend, Text.size()-realmend) );
314
CursorPos = realmbgn;
325
// paste from the clipboard
328
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
329
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
332
const c8* p = Operator->getTextFromClipboard();
335
if (MarkBegin == MarkEnd)
338
core::stringw s = Text.subString(0, CursorPos);
340
s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
342
if (!Max || s.size()<=Max) // thx to Fish FH for fix
346
CursorPos += s.size();
353
core::stringw s = Text.subString(0, realmbgn);
355
s.append( Text.subString(realmend, Text.size()-realmend) );
357
if (!Max || s.size()<=Max) // thx to Fish FH for fix
361
CursorPos = realmbgn + s.size();
372
// move/highlight to start of text
373
if (event.KeyInput.Shift)
375
newMarkEnd = CursorPos;
387
// move/highlight to end of text
388
if (event.KeyInput.Shift)
390
newMarkBegin = CursorPos;
391
newMarkEnd = Text.size();
396
CursorPos = Text.size();
405
// default keyboard handling
407
switch(event.KeyInput.Key)
412
if (WordWrap || MultiLine)
414
p = getLineFromPos(CursorPos);
415
p = BrokenTextPositions[p] + (s32)BrokenText[p].size();
416
if (p > 0 && (Text[p-1] == L'\r' || Text[p-1] == L'\n' ))
420
if (event.KeyInput.Shift)
422
if (MarkBegin == MarkEnd)
423
newMarkBegin = CursorPos;
433
BlinkStartTime = os::Timer::getTime();
440
if (WordWrap || MultiLine)
442
p = getLineFromPos(CursorPos);
443
p = BrokenTextPositions[p];
446
if (event.KeyInput.Shift)
448
if (MarkBegin == MarkEnd)
449
newMarkBegin = CursorPos;
458
BlinkStartTime = os::Timer::getTime();
469
sendGuiEvent( EGET_EDITBOX_ENTER );
474
if (event.KeyInput.Shift)
478
if (MarkBegin == MarkEnd)
479
newMarkBegin = CursorPos;
481
newMarkEnd = CursorPos-1;
490
if (CursorPos > 0) CursorPos--;
491
BlinkStartTime = os::Timer::getTime();
495
if (event.KeyInput.Shift)
497
if (Text.size() > (u32)CursorPos)
499
if (MarkBegin == MarkEnd)
500
newMarkBegin = CursorPos;
502
newMarkEnd = CursorPos+1;
511
if (Text.size() > (u32)CursorPos) CursorPos++;
512
BlinkStartTime = os::Timer::getTime();
515
if (MultiLine || (WordWrap && BrokenText.size() > 1) )
517
s32 lineNo = getLineFromPos(CursorPos);
518
s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
521
s32 cp = CursorPos - BrokenTextPositions[lineNo];
522
if ((s32)BrokenText[lineNo-1].size() < cp)
523
CursorPos = BrokenTextPositions[lineNo-1] + (s32)BrokenText[lineNo-1].size()-1;
525
CursorPos = BrokenTextPositions[lineNo-1] + cp;
528
if (event.KeyInput.Shift)
531
newMarkEnd = CursorPos;
546
if (MultiLine || (WordWrap && BrokenText.size() > 1) )
548
s32 lineNo = getLineFromPos(CursorPos);
549
s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
550
if (lineNo < (s32)BrokenText.size()-1)
552
s32 cp = CursorPos - BrokenTextPositions[lineNo];
553
if ((s32)BrokenText[lineNo+1].size() < cp)
554
CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
556
CursorPos = BrokenTextPositions[lineNo+1] + cp;
559
if (event.KeyInput.Shift)
562
newMarkEnd = CursorPos;
585
if (MarkBegin != MarkEnd)
587
// delete marked text
588
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
589
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
591
s = Text.subString(0, realmbgn);
592
s.append( Text.subString(realmend, Text.size()-realmend) );
595
CursorPos = realmbgn;
599
// delete text behind cursor
601
s = Text.subString(0, CursorPos-1);
604
s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
611
BlinkStartTime = os::Timer::getTime();
621
if (Text.size() != 0)
625
if (MarkBegin != MarkEnd)
627
// delete marked text
628
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
629
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
631
s = Text.subString(0, realmbgn);
632
s.append( Text.subString(realmend, Text.size()-realmend) );
635
CursorPos = realmbgn;
639
// delete text before cursor
640
s = Text.subString(0, CursorPos);
641
s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
645
if (CursorPos > (s32)Text.size())
646
CursorPos = (s32)Text.size();
648
BlinkStartTime = os::Timer::getTime();
686
inputChar(event.KeyInput.Char);
690
// Set new text markers
691
setTextMarkers( newMarkBegin, newMarkEnd );
693
// break the text if it has changed
697
sendGuiEvent(EGET_EDITBOX_CHANGED);
700
calculateScrollPos();
706
//! draws the element and its children
707
void CGUIEditBox::draw()
712
const bool focus = Environment->hasFocus(this);
714
IGUISkin* skin = Environment->getSkin();
718
EGUI_DEFAULT_COLOR bgCol = EGDC_GRAY_EDITABLE;
720
bgCol = focus ? EGDC_FOCUSED_EDITABLE : EGDC_EDITABLE;
722
if (!Border && Background)
724
skin->draw2DRectangle(this, skin->getColor(bgCol), AbsoluteRect, &AbsoluteClippingRect);
730
skin->draw3DSunkenPane(this, skin->getColor(bgCol), false, Background, AbsoluteRect, &AbsoluteClippingRect);
732
calculateFrameRect();
735
core::rect<s32> localClipRect = FrameRect;
736
localClipRect.clipAgainst(AbsoluteClippingRect);
740
IGUIFont* font = getActiveFont();
743
s32 charcursorpos = 0;
747
if (LastBreakFont != font)
752
// calculate cursor pos
754
core::stringw *txtLine = &Text;
760
const bool ml = (!PasswordBox && (WordWrap || MultiLine));
761
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
762
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
763
const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
764
const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
765
const s32 lineCount = ml ? BrokenText.size() : 1;
767
// Save the override color information.
768
// Then, alter it if the edit box is disabled.
769
const bool prevOver = OverrideColorEnabled;
770
const video::SColor prevColor = OverrideColor;
774
if (!isEnabled() && !OverrideColorEnabled)
776
OverrideColorEnabled = true;
777
OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
780
for (s32 i=0; i < lineCount; ++i)
784
// clipping test - don't draw anything outside the visible area
785
core::rect<s32> c = localClipRect;
786
c.clipAgainst(CurrentTextRect);
793
if (BrokenText.size() != 1)
796
BrokenText.push_back(core::stringw());
798
if (BrokenText[0].size() != Text.size())
800
BrokenText[0] = Text;
801
for (u32 q = 0; q < Text.size(); ++q)
803
BrokenText[0] [q] = PasswordChar;
806
txtLine = &BrokenText[0];
811
txtLine = ml ? &BrokenText[i] : &Text;
812
startPos = ml ? BrokenTextPositions[i] : 0;
817
font->draw(txtLine->c_str(), CurrentTextRect,
818
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
819
false, true, &localClipRect);
821
// draw mark and marked text
822
if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
825
s32 mbegin = 0, mend = 0;
826
s32 lineStartPos = 0, lineEndPos = txtLine->size();
830
// highlight start is on this line
831
s = txtLine->subString(0, realmbgn - startPos);
832
mbegin = font->getDimension(s.c_str()).Width;
835
mbegin += font->getKerningWidth(
836
&((*txtLine)[realmbgn - startPos]),
837
realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
839
lineStartPos = realmbgn - startPos;
841
if (i == hlineStart + hlineCount - 1)
843
// highlight end is on this line
844
s2 = txtLine->subString(0, realmend - startPos);
845
mend = font->getDimension(s2.c_str()).Width;
846
lineEndPos = (s32)s2.size();
849
mend = font->getDimension(txtLine->c_str()).Width;
851
CurrentTextRect.UpperLeftCorner.X += mbegin;
852
CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
855
skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
858
s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
861
font->draw(s.c_str(), CurrentTextRect,
862
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
863
false, true, &localClipRect);
868
// Return the override color information to its previous settings.
869
OverrideColorEnabled = prevOver;
870
OverrideColor = prevColor;
875
if (WordWrap || MultiLine)
877
cursorLine = getLineFromPos(CursorPos);
878
txtLine = &BrokenText[cursorLine];
879
startPos = BrokenTextPositions[cursorLine];
881
s = txtLine->subString(0,CursorPos-startPos);
882
charcursorpos = font->getDimension(s.c_str()).Width +
883
font->getKerningWidth(L"_", CursorPos-startPos > 0 ? &((*txtLine)[CursorPos-startPos-1]) : 0);
885
if (focus && (os::Timer::getTime() - BlinkStartTime) % 700 < 350)
887
setTextRect(cursorLine);
888
CurrentTextRect.UpperLeftCorner.X += charcursorpos;
890
font->draw(L"_", CurrentTextRect,
891
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
892
false, true, &localClipRect);
901
//! Sets the new caption of this element.
902
void CGUIEditBox::setText(const wchar_t* text)
905
if (u32(CursorPos) > Text.size())
906
CursorPos = Text.size();
912
//! Enables or disables automatic scrolling with cursor position
913
//! \param enable: If set to true, the text will move around with the cursor position
914
void CGUIEditBox::setAutoScroll(bool enable)
920
//! Checks to see if automatic scrolling is enabled
921
//! \return true if automatic scrolling is enabled, false if not
922
bool CGUIEditBox::isAutoScrollEnabled() const
924
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
929
//! Gets the area of the text in the edit box
930
//! \return Returns the size in pixels of the text
931
core::dimension2du CGUIEditBox::getTextDimension()
936
ret = CurrentTextRect;
938
for (u32 i=1; i < BrokenText.size(); ++i)
941
ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
942
ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
945
return core::dimension2du(ret.getSize());
949
//! Sets the maximum amount of characters which may be entered in the box.
950
//! \param max: Maximum amount of characters. If 0, the character amount is
952
void CGUIEditBox::setMax(u32 max)
956
if (Text.size() > Max && Max != 0)
957
Text = Text.subString(0, Max);
961
//! Returns maximum amount of characters, previously set by setMax();
962
u32 CGUIEditBox::getMax() const
968
bool CGUIEditBox::processMouse(const SEvent& event)
970
switch(event.MouseInput.Event)
972
case irr::EMIE_LMOUSE_LEFT_UP:
973
if (Environment->hasFocus(this))
975
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
978
setTextMarkers( MarkBegin, CursorPos );
980
MouseMarking = false;
981
calculateScrollPos();
985
case irr::EMIE_MOUSE_MOVED:
989
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
990
setTextMarkers( MarkBegin, CursorPos );
991
calculateScrollPos();
996
case EMIE_LMOUSE_PRESSED_DOWN:
997
if (!Environment->hasFocus(this))
999
BlinkStartTime = os::Timer::getTime();
1000
MouseMarking = true;
1001
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1002
setTextMarkers(CursorPos, CursorPos );
1003
calculateScrollPos();
1008
if (!AbsoluteClippingRect.isPointInside(
1009
core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
1016
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1018
s32 newMarkBegin = MarkBegin;
1020
newMarkBegin = CursorPos;
1022
MouseMarking = true;
1023
setTextMarkers( newMarkBegin, CursorPos);
1024
calculateScrollPos();
1036
s32 CGUIEditBox::getCursorPos(s32 x, s32 y)
1038
IGUIFont* font = getActiveFont();
1040
const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1042
core::stringw *txtLine=0;
1046
for (u32 i=0; i < lineCount; ++i)
1049
if (i == 0 && y < CurrentTextRect.UpperLeftCorner.Y)
1050
y = CurrentTextRect.UpperLeftCorner.Y;
1051
if (i == lineCount - 1 && y > CurrentTextRect.LowerRightCorner.Y )
1052
y = CurrentTextRect.LowerRightCorner.Y;
1054
// is it inside this region?
1055
if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
1057
// we've found the clicked line
1058
txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
1059
startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
1064
if (x < CurrentTextRect.UpperLeftCorner.X)
1065
x = CurrentTextRect.UpperLeftCorner.X;
1070
s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
1072
// click was on or left of the line
1074
return idx + startPos;
1076
// click was off the right edge of the line, go to end.
1077
return txtLine->size() + startPos;
1081
//! Breaks the single text line.
1082
void CGUIEditBox::breakText()
1084
if ((!WordWrap && !MultiLine))
1087
BrokenText.clear(); // need to reallocate :/
1088
BrokenTextPositions.set_used(0);
1090
IGUIFont* font = getActiveFont();
1094
LastBreakFont = font;
1098
core::stringw whitespace;
1099
s32 lastLineStart = 0;
1100
s32 size = Text.size();
1102
s32 elWidth = RelativeRect.getWidth() - 6;
1105
for (s32 i=0; i<size; ++i)
1108
bool lineBreak = false;
1110
if (c == L'\r') // Mac or Windows breaks
1114
if (Text[i+1] == L'\n') // Windows breaks
1120
else if (c == L'\n') // Unix breaks
1126
// don't break if we're not a multi-line edit box
1130
if (c == L' ' || c == 0 || i == (size-1))
1132
// here comes the next whitespace, look if
1133
// we can break the last word to the next line
1134
// We also break whitespace, otherwise cursor would vanish beside the right border.
1135
s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
1136
s32 worldlgth = font->getDimension(word.c_str()).Width;
1138
if (WordWrap && length + worldlgth + whitelgth > elWidth)
1140
// break to next line
1142
BrokenText.push_back(line);
1143
BrokenTextPositions.push_back(lastLineStart);
1144
lastLineStart = i - (s32)word.size();
1152
length += whitelgth + worldlgth;
1162
// compute line break
1167
BrokenText.push_back(line);
1168
BrokenTextPositions.push_back(lastLineStart);
1169
lastLineStart = i+1;
1178
// yippee this is a word..
1185
BrokenText.push_back(line);
1186
BrokenTextPositions.push_back(lastLineStart);
1190
void CGUIEditBox::setTextRect(s32 line)
1195
IGUIFont* font = getActiveFont();
1199
core::dimension2du d;
1201
// get text dimension
1202
const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
1203
if (WordWrap || MultiLine)
1205
d = font->getDimension(BrokenText[line].c_str());
1209
d = font->getDimension(Text.c_str());
1210
d.Height = AbsoluteRect.getHeight();
1212
d.Height += font->getKerningHeight();
1218
// align to h centre
1219
CurrentTextRect.UpperLeftCorner.X = (FrameRect.getWidth()/2) - (d.Width/2);
1220
CurrentTextRect.LowerRightCorner.X = (FrameRect.getWidth()/2) + (d.Width/2);
1222
case EGUIA_LOWERRIGHT:
1223
// align to right edge
1224
CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
1225
CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
1228
// align to left edge
1229
CurrentTextRect.UpperLeftCorner.X = 0;
1230
CurrentTextRect.LowerRightCorner.X = d.Width;
1237
// align to v centre
1238
CurrentTextRect.UpperLeftCorner.Y =
1239
(FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
1241
case EGUIA_LOWERRIGHT:
1242
// align to bottom edge
1243
CurrentTextRect.UpperLeftCorner.Y =
1244
FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
1247
// align to top edge
1248
CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
1252
CurrentTextRect.UpperLeftCorner.X -= HScrollPos;
1253
CurrentTextRect.LowerRightCorner.X -= HScrollPos;
1254
CurrentTextRect.UpperLeftCorner.Y -= VScrollPos;
1255
CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
1257
CurrentTextRect += FrameRect.UpperLeftCorner;
1262
s32 CGUIEditBox::getLineFromPos(s32 pos)
1264
if (!WordWrap && !MultiLine)
1268
while (i < (s32)BrokenTextPositions.size())
1270
if (BrokenTextPositions[i] > pos)
1274
return (s32)BrokenTextPositions.size() - 1;
1278
void CGUIEditBox::inputChar(wchar_t c)
1285
if (Text.size() < Max || Max == 0)
1289
if (MarkBegin != MarkEnd)
1291
// replace marked text
1292
const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
1293
const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
1295
s = Text.subString(0, realmbgn);
1297
s.append( Text.subString(realmend, Text.size()-realmend) );
1299
CursorPos = realmbgn+1;
1303
// add new character
1304
s = Text.subString(0, CursorPos);
1306
s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
1311
BlinkStartTime = os::Timer::getTime();
1312
setTextMarkers(0, 0);
1316
sendGuiEvent(EGET_EDITBOX_CHANGED);
1317
calculateScrollPos();
1321
void CGUIEditBox::calculateScrollPos()
1326
// calculate horizontal scroll position
1327
s32 cursLine = getLineFromPos(CursorPos);
1330
setTextRect(cursLine);
1332
// don't do horizontal scrolling when wordwrap is enabled.
1335
// get cursor position
1336
IGUIFont* font = getActiveFont();
1340
core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
1341
s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
1343
s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
1344
font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
1346
s32 cEnd = cStart + font->getDimension(L"_ ").Width;
1348
if (FrameRect.LowerRightCorner.X < cEnd)
1349
HScrollPos = cEnd - FrameRect.LowerRightCorner.X;
1350
else if (FrameRect.UpperLeftCorner.X > cStart)
1351
HScrollPos = cStart - FrameRect.UpperLeftCorner.X;
1355
// todo: adjust scrollbar
1358
// vertical scroll position
1359
if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
1360
VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
1362
else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
1363
VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
1367
// todo: adjust scrollbar
1370
void CGUIEditBox::calculateFrameRect()
1372
FrameRect = AbsoluteRect;
1375
skin = Environment->getSkin();
1378
FrameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
1379
FrameRect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
1380
FrameRect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
1381
FrameRect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
1385
//! set text markers
1386
void CGUIEditBox::setTextMarkers(s32 begin, s32 end)
1388
if ( begin != MarkBegin || end != MarkEnd )
1392
sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
1396
//! send some gui event to parent
1397
void CGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
1402
e.EventType = EET_GUI_EVENT;
1403
e.GUIEvent.Caller = this;
1404
e.GUIEvent.Element = 0;
1405
e.GUIEvent.EventType = type;
1411
//! Writes attributes of the element.
1412
void CGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
1414
// IGUIEditBox::serializeAttributes(out,options);
1416
out->addBool ("Border", Border);
1417
out->addBool ("Background", Background);
1418
out->addBool ("OverrideColorEnabled",OverrideColorEnabled );
1419
out->addColor ("OverrideColor", OverrideColor);
1420
// out->addFont("OverrideFont",OverrideFont);
1421
out->addInt ("MaxChars", Max);
1422
out->addBool ("WordWrap", WordWrap);
1423
out->addBool ("MultiLine", MultiLine);
1424
out->addBool ("AutoScroll", AutoScroll);
1425
out->addBool ("PasswordBox", PasswordBox);
1426
core::stringw ch = L" ";
1427
ch[0] = PasswordChar;
1428
out->addString("PasswordChar", ch.c_str());
1429
out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
1430
out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
1432
IGUIEditBox::serializeAttributes(out,options);
1436
//! Reads attributes of the element
1437
void CGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
1439
IGUIEditBox::deserializeAttributes(in,options);
1441
setDrawBorder( in->getAttributeAsBool("Border") );
1442
setDrawBackground( in->getAttributeAsBool("Background") );
1443
setOverrideColor(in->getAttributeAsColor("OverrideColor"));
1444
enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
1445
setMax(in->getAttributeAsInt("MaxChars"));
1446
setWordWrap(in->getAttributeAsBool("WordWrap"));
1447
setMultiLine(in->getAttributeAsBool("MultiLine"));
1448
setAutoScroll(in->getAttributeAsBool("AutoScroll"));
1449
core::stringw ch = in->getAttributeAsStringW("PasswordChar");
1452
setPasswordBox(in->getAttributeAsBool("PasswordBox"));
1454
setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
1456
setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
1457
(EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
1459
// setOverrideFont(in->getAttributeAsFont("OverrideFont"));
1463
} // end namespace gui
1464
} // end namespace irr
1466
#endif // _IRR_COMPILE_WITH_GUI_