1 // 11.11.2011 11:11 ValkaTR
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
7 // Characters like: ä ö ü õ ы й ю я ъ № € ° ...
9 // This fix is only needed for linux, because of a bug
10 // in the CIrrDeviceLinux.cpp:1014-1015 of the irrlicht
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
17 // From the "man setlocale":
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:
23 // setlocale(LC_ALL, "");
25 // after program initialization....
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
32 #include <util/numeric.h>
33 #include "intlGUIEditBox.h"
36 #include "IGUIEnvironment.h"
38 #include "IVideoDriver.h"
39 //#include "irrlicht/os.cpp"
41 //#include "Keycodes.h"
47 ctrl+left/right to select word
48 double click/ctrl click: word select + drag to select whole words, triple click to select line
49 optional? dragging selected text
59 intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
60 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
61 const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
62 : GUIEditBox(environment, parent, id, rectangle, border, writable)
65 setDebugName("intlintlGUIEditBox");
71 m_operator = Environment->getOSOperator();
76 // this element can be tabbed to
82 skin = Environment->getSkin();
85 m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
86 m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
87 m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
88 m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
91 if (skin && has_vscrollbar) {
92 m_scrollbar_width = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
94 if (m_scrollbar_width > 0) {
101 calculateScrollPos();
102 setWritable(writable);
105 //! Sets whether to draw the background
106 void intlGUIEditBox::setDrawBackground(bool draw)
110 void intlGUIEditBox::updateAbsolutePosition()
112 core::rect<s32> oldAbsoluteRect(AbsoluteRect);
113 IGUIElement::updateAbsolutePosition();
114 if ( oldAbsoluteRect != AbsoluteRect )
121 //! draws the element and its children
122 void intlGUIEditBox::draw()
127 const bool focus = Environment->hasFocus(this);
129 IGUISkin* skin = Environment->getSkin();
133 m_frame_rect = AbsoluteRect;
140 skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
141 false, true, m_frame_rect, &AbsoluteClippingRect);
144 m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
145 m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
146 m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
147 m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
151 core::rect<s32> localClipRect = m_frame_rect;
152 localClipRect.clipAgainst(AbsoluteClippingRect);
156 IGUIFont* font = m_override_font;
157 if (!m_override_font)
158 font = skin->getFont();
161 s32 charcursorpos = 0;
165 if (m_last_break_font != font)
170 // calculate cursor pos
172 core::stringw *txtLine = &Text;
178 const bool ml = (!m_passwordbox && (m_word_wrap || m_multiline));
179 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
180 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
181 const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
182 const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
183 const s32 lineCount = ml ? m_broken_text.size() : 1;
185 // Save the override color information.
186 // Then, alter it if the edit box is disabled.
187 const bool prevOver = m_override_color_enabled;
188 const video::SColor prevColor = m_override_color;
191 if (!IsEnabled && !m_override_color_enabled)
193 m_override_color_enabled = true;
194 m_override_color = skin->getColor(EGDC_GRAY_TEXT);
197 for (s32 i=0; i < lineCount; ++i)
201 // clipping test - don't draw anything outside the visible area
202 core::rect<s32> c = localClipRect;
203 c.clipAgainst(m_current_text_rect);
210 if (m_broken_text.size() != 1)
212 m_broken_text.clear();
213 m_broken_text.emplace_back();
215 if (m_broken_text[0].size() != Text.size())
217 m_broken_text[0] = Text;
218 for (u32 q = 0; q < Text.size(); ++q)
220 m_broken_text[0] [q] = m_passwordchar;
223 txtLine = &m_broken_text[0];
228 txtLine = ml ? &m_broken_text[i] : &Text;
229 startPos = ml ? m_broken_text_positions[i] : 0;
234 font->draw(txtLine->c_str(), m_current_text_rect,
235 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
236 false, true, &localClipRect);
238 // draw mark and marked text
239 if (focus && m_mark_begin != m_mark_end && i >= hlineStart && i < hlineStart + hlineCount)
242 s32 mbegin = 0, mend = 0;
243 s32 lineStartPos = 0, lineEndPos = txtLine->size();
247 // highlight start is on this line
248 s = txtLine->subString(0, realmbgn - startPos);
249 mbegin = font->getDimension(s.c_str()).Width;
252 mbegin += font->getKerningWidth(
253 &((*txtLine)[realmbgn - startPos]),
254 realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
256 lineStartPos = realmbgn - startPos;
258 if (i == hlineStart + hlineCount - 1)
260 // highlight end is on this line
261 s2 = txtLine->subString(0, realmend - startPos);
262 mend = font->getDimension(s2.c_str()).Width;
263 lineEndPos = (s32)s2.size();
266 mend = font->getDimension(txtLine->c_str()).Width;
268 m_current_text_rect.UpperLeftCorner.X += mbegin;
269 m_current_text_rect.LowerRightCorner.X = m_current_text_rect.UpperLeftCorner.X + mend - mbegin;
272 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), m_current_text_rect, &localClipRect);
275 s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
278 font->draw(s.c_str(), m_current_text_rect,
279 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
280 false, true, &localClipRect);
285 // Return the override color information to its previous settings.
286 m_override_color_enabled = prevOver;
287 m_override_color = prevColor;
292 if (m_word_wrap || m_multiline)
294 cursorLine = getLineFromPos(m_cursor_pos);
295 txtLine = &m_broken_text[cursorLine];
296 startPos = m_broken_text_positions[cursorLine];
298 s = txtLine->subString(0,m_cursor_pos-startPos);
299 charcursorpos = font->getDimension(s.c_str()).Width +
300 font->getKerningWidth(L"_", m_cursor_pos-startPos > 0 ? &((*txtLine)[m_cursor_pos-startPos-1]) : 0);
303 if (focus && (porting::getTimeMs() - m_blink_start_time) % 700 < 350) {
304 setTextRect(cursorLine);
305 m_current_text_rect.UpperLeftCorner.X += charcursorpos;
307 font->draw(L"_", m_current_text_rect,
308 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
309 false, true, &localClipRect);
319 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
321 IGUIFont* font = m_override_font;
322 IGUISkin* skin = Environment->getSkin();
323 if (!m_override_font)
324 font = skin->getFont();
326 const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
328 core::stringw *txtLine = NULL;
330 u32 curr_line_idx = 0;
333 for (; curr_line_idx < lineCount; ++curr_line_idx) {
334 setTextRect(curr_line_idx);
335 if (curr_line_idx == 0 && y < m_current_text_rect.UpperLeftCorner.Y)
336 y = m_current_text_rect.UpperLeftCorner.Y;
337 if (curr_line_idx == lineCount - 1 && y > m_current_text_rect.LowerRightCorner.Y)
338 y = m_current_text_rect.LowerRightCorner.Y;
340 // is it inside this region?
341 if (y >= m_current_text_rect.UpperLeftCorner.Y && y <= m_current_text_rect.LowerRightCorner.Y) {
342 // we've found the clicked line
343 txtLine = (m_word_wrap || m_multiline) ? &m_broken_text[curr_line_idx] : &Text;
344 startPos = (m_word_wrap || m_multiline) ? m_broken_text_positions[curr_line_idx] : 0;
349 if (x < m_current_text_rect.UpperLeftCorner.X)
350 x = m_current_text_rect.UpperLeftCorner.X;
351 else if (x > m_current_text_rect.LowerRightCorner.X)
352 x = m_current_text_rect.LowerRightCorner.X;
354 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - m_current_text_rect.UpperLeftCorner.X);
355 // Special handling for last line, if we are on limits, add 1 extra shift because idx
356 // will be the last char, not null char of the wstring
357 if (curr_line_idx == lineCount - 1 && x == m_current_text_rect.LowerRightCorner.X)
360 return rangelim(idx + startPos, 0, S32_MAX);
364 //! Breaks the single text line.
365 void intlGUIEditBox::breakText()
367 IGUISkin* skin = Environment->getSkin();
369 if ((!m_word_wrap && !m_multiline) || !skin)
372 m_broken_text.clear(); // need to reallocate :/
373 m_broken_text_positions.clear();
375 IGUIFont* font = m_override_font;
376 if (!m_override_font)
377 font = skin->getFont();
382 m_last_break_font = font;
386 core::stringw whitespace;
387 s32 lastLineStart = 0;
388 s32 size = Text.size();
390 s32 elWidth = RelativeRect.getWidth() - m_scrollbar_width - 10;
393 for (s32 i=0; i<size; ++i)
396 bool lineBreak = false;
398 if (c == L'\r') // Mac or Windows breaks
402 if (Text[i+1] == L'\n') // Windows breaks
408 else if (c == L'\n') // Unix breaks
414 // don't break if we're not a multi-line edit box
418 if (c == L' ' || c == 0 || i == (size-1))
421 // here comes the next whitespace, look if
422 // we can break the last word to the next line.
423 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
424 s32 worldlgth = font->getDimension(word.c_str()).Width;
426 if (m_word_wrap && length + worldlgth + whitelgth > elWidth)
428 // break to next line
430 m_broken_text.push_back(line);
431 m_broken_text_positions.push_back(lastLineStart);
432 lastLineStart = i - (s32)word.size();
440 length += whitelgth + worldlgth;
449 // compute line break
454 m_broken_text.push_back(line);
455 m_broken_text_positions.push_back(lastLineStart);
465 // yippee this is a word..
472 m_broken_text.push_back(line);
473 m_broken_text_positions.push_back(lastLineStart);
477 void intlGUIEditBox::setTextRect(s32 line)
479 core::dimension2du d;
481 IGUISkin* skin = Environment->getSkin();
485 IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
490 // get text dimension
491 const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
492 if (m_word_wrap || m_multiline)
494 d = font->getDimension(m_broken_text[line].c_str());
498 d = font->getDimension(Text.c_str());
499 d.Height = AbsoluteRect.getHeight();
501 d.Height += font->getKerningHeight();
508 m_current_text_rect.UpperLeftCorner.X = (m_frame_rect.getWidth()/2) - (d.Width/2);
509 m_current_text_rect.LowerRightCorner.X = (m_frame_rect.getWidth()/2) + (d.Width/2);
511 case EGUIA_LOWERRIGHT:
512 // align to right edge
513 m_current_text_rect.UpperLeftCorner.X = m_frame_rect.getWidth() - d.Width;
514 m_current_text_rect.LowerRightCorner.X = m_frame_rect.getWidth();
517 // align to left edge
518 m_current_text_rect.UpperLeftCorner.X = 0;
519 m_current_text_rect.LowerRightCorner.X = d.Width;
527 m_current_text_rect.UpperLeftCorner.Y =
528 (m_frame_rect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
530 case EGUIA_LOWERRIGHT:
531 // align to bottom edge
532 m_current_text_rect.UpperLeftCorner.Y =
533 m_frame_rect.getHeight() - lineCount*d.Height + d.Height*line;
537 m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
541 m_current_text_rect.UpperLeftCorner.X -= m_hscroll_pos;
542 m_current_text_rect.LowerRightCorner.X -= m_hscroll_pos;
543 m_current_text_rect.UpperLeftCorner.Y -= m_vscroll_pos;
544 m_current_text_rect.LowerRightCorner.Y = m_current_text_rect.UpperLeftCorner.Y + d.Height;
546 m_current_text_rect += m_frame_rect.UpperLeftCorner;
550 void intlGUIEditBox::inputChar(wchar_t c)
552 if (!isEnabled() || !m_writable)
557 if (Text.size() < m_max || m_max == 0)
561 if (m_mark_begin != m_mark_end)
563 // replace marked text
564 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
565 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
567 s = Text.subString(0, realmbgn);
569 s.append( Text.subString(realmend, Text.size()-realmend) );
571 m_cursor_pos = realmbgn+1;
576 s = Text.subString(0, m_cursor_pos);
578 s.append( Text.subString(m_cursor_pos, Text.size()-m_cursor_pos) );
583 m_blink_start_time = porting::getTimeMs();
584 setTextMarkers(0, 0);
588 sendGuiEvent(EGET_EDITBOX_CHANGED);
589 calculateScrollPos();
593 void intlGUIEditBox::calculateScrollPos()
598 // calculate horizontal scroll position
599 s32 cursLine = getLineFromPos(m_cursor_pos);
600 setTextRect(cursLine);
602 // don't do horizontal scrolling when wordwrap is enabled.
605 // get cursor position
606 IGUISkin* skin = Environment->getSkin();
609 IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
613 core::stringw *txtLine = m_multiline ? &m_broken_text[cursLine] : &Text;
614 s32 cPos = m_multiline ? m_cursor_pos - m_broken_text_positions[cursLine] : m_cursor_pos;
616 s32 cStart = m_current_text_rect.UpperLeftCorner.X + m_hscroll_pos +
617 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
619 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
621 if (m_frame_rect.LowerRightCorner.X < cEnd)
622 m_hscroll_pos = cEnd - m_frame_rect.LowerRightCorner.X;
623 else if (m_frame_rect.UpperLeftCorner.X > cStart)
624 m_hscroll_pos = cStart - m_frame_rect.UpperLeftCorner.X;
628 // todo: adjust scrollbar
631 if (!m_word_wrap && !m_multiline)
634 // vertical scroll position
635 if (m_frame_rect.LowerRightCorner.Y < m_current_text_rect.LowerRightCorner.Y)
636 m_vscroll_pos += m_current_text_rect.LowerRightCorner.Y - m_frame_rect.LowerRightCorner.Y; // scrolling downwards
637 else if (m_frame_rect.UpperLeftCorner.Y > m_current_text_rect.UpperLeftCorner.Y)
638 m_vscroll_pos += m_current_text_rect.UpperLeftCorner.Y - m_frame_rect.UpperLeftCorner.Y; // scrolling upwards
640 // todo: adjust scrollbar
642 m_vscrollbar->setPos(m_vscroll_pos);
646 //! Create a vertical scrollbar
647 void intlGUIEditBox::createVScrollBar()
651 if (m_override_font) {
652 fontHeight = m_override_font->getDimension(L"").Height;
654 if (IGUISkin* skin = Environment->getSkin()) {
655 if (IGUIFont* font = skin->getFont()) {
656 fontHeight = font->getDimension(L"").Height;
661 irr::core::rect<s32> scrollbarrect = m_frame_rect;
662 scrollbarrect.UpperLeftCorner.X += m_frame_rect.getWidth() - m_scrollbar_width;
663 m_vscrollbar = new GUIScrollBar(Environment, getParent(), -1,
664 scrollbarrect, false, true);
666 m_vscrollbar->setVisible(false);
667 m_vscrollbar->setSmallStep(3 * fontHeight);
668 m_vscrollbar->setLargeStep(10 * fontHeight);
672 //! Writes attributes of the element.
673 void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
675 // IGUIEditBox::serializeAttributes(out,options);
677 out->addBool ("OverrideColorEnabled", m_override_color_enabled );
678 out->addColor ("OverrideColor", m_override_color);
679 // out->addFont("OverrideFont",m_override_font);
680 out->addInt ("MaxChars", m_max);
681 out->addBool ("WordWrap", m_word_wrap);
682 out->addBool ("MultiLine", m_multiline);
683 out->addBool ("AutoScroll", m_autoscroll);
684 out->addBool ("PasswordBox", m_passwordbox);
685 core::stringw ch = L" ";
686 ch[0] = m_passwordchar;
687 out->addString("PasswordChar", ch.c_str());
688 out->addEnum ("HTextAlign", m_halign, GUIAlignmentNames);
689 out->addEnum ("VTextAlign", m_valign, GUIAlignmentNames);
690 out->addBool ("Writable", m_writable);
692 IGUIEditBox::serializeAttributes(out,options);
696 //! Reads attributes of the element
697 void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
699 IGUIEditBox::deserializeAttributes(in,options);
701 setOverrideColor(in->getAttributeAsColor("OverrideColor"));
702 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
703 setMax(in->getAttributeAsInt("MaxChars"));
704 setWordWrap(in->getAttributeAsBool("WordWrap"));
705 setMultiLine(in->getAttributeAsBool("MultiLine"));
706 setAutoScroll(in->getAttributeAsBool("AutoScroll"));
707 core::stringw ch = in->getAttributeAsStringW("PasswordChar");
710 setPasswordBox(in->getAttributeAsBool("PasswordBox"));
712 setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
714 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
715 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
717 setWritable(in->getAttributeAsBool("Writable"));
718 // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
722 } // end namespace gui
723 } // end namespace irr