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 = getActiveFont();
323 const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
325 core::stringw *txtLine = NULL;
327 u32 curr_line_idx = 0;
330 for (; curr_line_idx < lineCount; ++curr_line_idx) {
331 setTextRect(curr_line_idx);
332 if (curr_line_idx == 0 && y < m_current_text_rect.UpperLeftCorner.Y)
333 y = m_current_text_rect.UpperLeftCorner.Y;
334 if (curr_line_idx == lineCount - 1 && y > m_current_text_rect.LowerRightCorner.Y)
335 y = m_current_text_rect.LowerRightCorner.Y;
337 // is it inside this region?
338 if (y >= m_current_text_rect.UpperLeftCorner.Y && y <= m_current_text_rect.LowerRightCorner.Y) {
339 // we've found the clicked line
340 txtLine = (m_word_wrap || m_multiline) ? &m_broken_text[curr_line_idx] : &Text;
341 startPos = (m_word_wrap || m_multiline) ? m_broken_text_positions[curr_line_idx] : 0;
346 if (x < m_current_text_rect.UpperLeftCorner.X)
347 x = m_current_text_rect.UpperLeftCorner.X;
348 else if (x > m_current_text_rect.LowerRightCorner.X)
349 x = m_current_text_rect.LowerRightCorner.X;
351 s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - m_current_text_rect.UpperLeftCorner.X);
352 // Special handling for last line, if we are on limits, add 1 extra shift because idx
353 // will be the last char, not null char of the wstring
354 if (curr_line_idx == lineCount - 1 && x == m_current_text_rect.LowerRightCorner.X)
357 return rangelim(idx + startPos, 0, S32_MAX);
361 //! Breaks the single text line.
362 void intlGUIEditBox::breakText()
364 IGUISkin* skin = Environment->getSkin();
366 if ((!m_word_wrap && !m_multiline) || !skin)
369 m_broken_text.clear(); // need to reallocate :/
370 m_broken_text_positions.clear();
372 IGUIFont* font = m_override_font;
373 if (!m_override_font)
374 font = skin->getFont();
379 m_last_break_font = font;
383 core::stringw whitespace;
384 s32 lastLineStart = 0;
385 s32 size = Text.size();
387 s32 elWidth = RelativeRect.getWidth() - m_scrollbar_width - 10;
390 for (s32 i=0; i<size; ++i)
393 bool lineBreak = false;
395 if (c == L'\r') // Mac or Windows breaks
399 if (Text[i+1] == L'\n') // Windows breaks
405 else if (c == L'\n') // Unix breaks
411 // don't break if we're not a multi-line edit box
415 if (c == L' ' || c == 0 || i == (size-1))
418 // here comes the next whitespace, look if
419 // we can break the last word to the next line.
420 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
421 s32 worldlgth = font->getDimension(word.c_str()).Width;
423 if (m_word_wrap && length + worldlgth + whitelgth > elWidth)
425 // break to next line
427 m_broken_text.push_back(line);
428 m_broken_text_positions.push_back(lastLineStart);
429 lastLineStart = i - (s32)word.size();
437 length += whitelgth + worldlgth;
446 // compute line break
451 m_broken_text.push_back(line);
452 m_broken_text_positions.push_back(lastLineStart);
462 // yippee this is a word..
469 m_broken_text.push_back(line);
470 m_broken_text_positions.push_back(lastLineStart);
474 void intlGUIEditBox::setTextRect(s32 line)
476 core::dimension2du d;
478 IGUISkin* skin = Environment->getSkin();
482 IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
487 // get text dimension
488 const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
489 if (m_word_wrap || m_multiline)
491 d = font->getDimension(m_broken_text[line].c_str());
495 d = font->getDimension(Text.c_str());
496 d.Height = AbsoluteRect.getHeight();
498 d.Height += font->getKerningHeight();
505 m_current_text_rect.UpperLeftCorner.X = (m_frame_rect.getWidth()/2) - (d.Width/2);
506 m_current_text_rect.LowerRightCorner.X = (m_frame_rect.getWidth()/2) + (d.Width/2);
508 case EGUIA_LOWERRIGHT:
509 // align to right edge
510 m_current_text_rect.UpperLeftCorner.X = m_frame_rect.getWidth() - d.Width;
511 m_current_text_rect.LowerRightCorner.X = m_frame_rect.getWidth();
514 // align to left edge
515 m_current_text_rect.UpperLeftCorner.X = 0;
516 m_current_text_rect.LowerRightCorner.X = d.Width;
524 m_current_text_rect.UpperLeftCorner.Y =
525 (m_frame_rect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
527 case EGUIA_LOWERRIGHT:
528 // align to bottom edge
529 m_current_text_rect.UpperLeftCorner.Y =
530 m_frame_rect.getHeight() - lineCount*d.Height + d.Height*line;
534 m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
538 m_current_text_rect.UpperLeftCorner.X -= m_hscroll_pos;
539 m_current_text_rect.LowerRightCorner.X -= m_hscroll_pos;
540 m_current_text_rect.UpperLeftCorner.Y -= m_vscroll_pos;
541 m_current_text_rect.LowerRightCorner.Y = m_current_text_rect.UpperLeftCorner.Y + d.Height;
543 m_current_text_rect += m_frame_rect.UpperLeftCorner;
547 void intlGUIEditBox::calculateScrollPos()
552 // calculate horizontal scroll position
553 s32 cursLine = getLineFromPos(m_cursor_pos);
554 setTextRect(cursLine);
556 // don't do horizontal scrolling when wordwrap is enabled.
559 // get cursor position
560 IGUISkin* skin = Environment->getSkin();
563 IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
567 core::stringw *txtLine = m_multiline ? &m_broken_text[cursLine] : &Text;
568 s32 cPos = m_multiline ? m_cursor_pos - m_broken_text_positions[cursLine] : m_cursor_pos;
570 s32 cStart = m_current_text_rect.UpperLeftCorner.X + m_hscroll_pos +
571 font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
573 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
575 if (m_frame_rect.LowerRightCorner.X < cEnd)
576 m_hscroll_pos = cEnd - m_frame_rect.LowerRightCorner.X;
577 else if (m_frame_rect.UpperLeftCorner.X > cStart)
578 m_hscroll_pos = cStart - m_frame_rect.UpperLeftCorner.X;
582 // todo: adjust scrollbar
585 if (!m_word_wrap && !m_multiline)
588 // vertical scroll position
589 if (m_frame_rect.LowerRightCorner.Y < m_current_text_rect.LowerRightCorner.Y)
590 m_vscroll_pos += m_current_text_rect.LowerRightCorner.Y - m_frame_rect.LowerRightCorner.Y; // scrolling downwards
591 else if (m_frame_rect.UpperLeftCorner.Y > m_current_text_rect.UpperLeftCorner.Y)
592 m_vscroll_pos += m_current_text_rect.UpperLeftCorner.Y - m_frame_rect.UpperLeftCorner.Y; // scrolling upwards
594 // todo: adjust scrollbar
596 m_vscrollbar->setPos(m_vscroll_pos);
600 //! Create a vertical scrollbar
601 void intlGUIEditBox::createVScrollBar()
605 if (m_override_font) {
606 fontHeight = m_override_font->getDimension(L"").Height;
608 if (IGUISkin* skin = Environment->getSkin()) {
609 if (IGUIFont* font = skin->getFont()) {
610 fontHeight = font->getDimension(L"").Height;
615 irr::core::rect<s32> scrollbarrect = m_frame_rect;
616 scrollbarrect.UpperLeftCorner.X += m_frame_rect.getWidth() - m_scrollbar_width;
617 m_vscrollbar = new GUIScrollBar(Environment, getParent(), -1,
618 scrollbarrect, false, true);
620 m_vscrollbar->setVisible(false);
621 m_vscrollbar->setSmallStep(3 * fontHeight);
622 m_vscrollbar->setLargeStep(10 * fontHeight);
625 } // end namespace gui
626 } // end namespace irr