1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // Modified by Mustapha T.
3 // This file is part of the "Irrlicht Engine".
4 // For conditions of distribution and use, see copyright notice in irrlicht.h
6 #include "guiEditBoxWithScrollbar.h"
9 #include "IGUIEnvironment.h"
11 #include "IVideoDriver.h"
18 optional scrollbars [done]
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
26 GUIEditBoxWithScrollBar::GUIEditBoxWithScrollBar(const wchar_t* text, bool border,
27 IGUIEnvironment* environment, IGUIElement* parent, s32 id,
28 const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
29 : GUIEditBox(environment, parent, id, rectangle, border, writable),
30 m_background(true), m_bg_color_used(false)
33 setDebugName("GUIEditBoxWithScrollBar");
40 m_operator = Environment->getOSOperator();
45 // this element can be tabbed to
57 setWritable(writable);
60 //! Sets whether to draw the background
61 void GUIEditBoxWithScrollBar::setDrawBackground(bool draw)
67 void GUIEditBoxWithScrollBar::updateAbsolutePosition()
69 core::rect<s32> old_absolute_rect(AbsoluteRect);
70 IGUIElement::updateAbsolutePosition();
71 if (old_absolute_rect != AbsoluteRect) {
79 //! draws the element and its children
80 void GUIEditBoxWithScrollBar::draw()
85 const bool focus = Environment->hasFocus(this);
87 IGUISkin* skin = Environment->getSkin();
91 video::SColor default_bg_color;
92 video::SColor bg_color;
94 default_bg_color = m_writable ? skin->getColor(EGDC_WINDOW) : video::SColor(0);
95 bg_color = m_bg_color_used ? m_bg_color : default_bg_color;
97 if (!m_border && m_background) {
98 skin->draw2DRectangle(this, bg_color, AbsoluteRect, &AbsoluteClippingRect);
106 skin->draw3DSunkenPane(this, bg_color, false, m_background,
107 AbsoluteRect, &AbsoluteClippingRect);
110 calculateFrameRect();
113 core::rect<s32> local_clip_rect = m_frame_rect;
114 local_clip_rect.clipAgainst(AbsoluteClippingRect);
118 IGUIFont* font = getActiveFont();
121 s32 charcursorpos = 0;
124 if (m_last_break_font != font) {
128 // calculate cursor pos
130 core::stringw *txt_line = &Text;
136 const bool ml = (!m_passwordbox && (m_word_wrap || m_multiline));
137 const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
138 const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
139 const s32 hline_start = ml ? getLineFromPos(realmbgn) : 0;
140 const s32 hline_count = ml ? getLineFromPos(realmend) - hline_start + 1 : 1;
141 const s32 line_count = ml ? m_broken_text.size() : 1;
143 // Save the override color information.
144 // Then, alter it if the edit box is disabled.
145 const bool prevOver = m_override_color_enabled;
146 const video::SColor prevColor = m_override_color;
149 if (!isEnabled() && !m_override_color_enabled) {
150 m_override_color_enabled = true;
151 m_override_color = skin->getColor(EGDC_GRAY_TEXT);
154 for (s32 i = 0; i < line_count; ++i) {
157 // clipping test - don't draw anything outside the visible area
158 core::rect<s32> c = local_clip_rect;
159 c.clipAgainst(m_current_text_rect);
165 if (m_broken_text.size() != 1) {
166 m_broken_text.clear();
167 m_broken_text.emplace_back();
169 if (m_broken_text[0].size() != Text.size()){
170 m_broken_text[0] = Text;
171 for (u32 q = 0; q < Text.size(); ++q)
173 m_broken_text[0][q] = m_passwordchar;
176 txt_line = &m_broken_text[0];
179 txt_line = ml ? &m_broken_text[i] : &Text;
180 start_pos = ml ? m_broken_text_positions[i] : 0;
185 font->draw(txt_line->c_str(), m_current_text_rect,
186 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
187 false, true, &local_clip_rect);
189 // draw mark and marked text
190 if (focus && m_mark_begin != m_mark_end && i >= hline_start && i < hline_start + hline_count) {
192 s32 mbegin = 0, mend = 0;
193 s32 lineStartPos = 0, lineEndPos = txt_line->size();
195 if (i == hline_start) {
196 // highlight start is on this line
197 s = txt_line->subString(0, realmbgn - start_pos);
198 mbegin = font->getDimension(s.c_str()).Width;
201 mbegin += font->getKerningWidth(
202 &((*txt_line)[realmbgn - start_pos]),
203 realmbgn - start_pos > 0 ? &((*txt_line)[realmbgn - start_pos - 1]) : 0);
205 lineStartPos = realmbgn - start_pos;
207 if (i == hline_start + hline_count - 1) {
208 // highlight end is on this line
209 s2 = txt_line->subString(0, realmend - start_pos);
210 mend = font->getDimension(s2.c_str()).Width;
211 lineEndPos = (s32)s2.size();
213 mend = font->getDimension(txt_line->c_str()).Width;
217 m_current_text_rect.UpperLeftCorner.X += mbegin;
218 m_current_text_rect.LowerRightCorner.X = m_current_text_rect.UpperLeftCorner.X + mend - mbegin;
222 skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), m_current_text_rect, &local_clip_rect);
225 s = txt_line->subString(lineStartPos, lineEndPos - lineStartPos);
228 font->draw(s.c_str(), m_current_text_rect,
229 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
230 false, true, &local_clip_rect);
235 // Return the override color information to its previous settings.
236 m_override_color_enabled = prevOver;
237 m_override_color = prevColor;
241 if (IsEnabled && m_writable) {
242 if (m_word_wrap || m_multiline) {
243 cursor_line = getLineFromPos(m_cursor_pos);
244 txt_line = &m_broken_text[cursor_line];
245 start_pos = m_broken_text_positions[cursor_line];
247 s = txt_line->subString(0, m_cursor_pos - start_pos);
248 charcursorpos = font->getDimension(s.c_str()).Width +
249 font->getKerningWidth(L"_", m_cursor_pos - start_pos > 0 ? &((*txt_line)[m_cursor_pos - start_pos - 1]) : 0);
251 if (focus && (porting::getTimeMs() - m_blink_start_time) % 700 < 350) {
252 setTextRect(cursor_line);
253 m_current_text_rect.UpperLeftCorner.X += charcursorpos;
255 font->draw(L"_", m_current_text_rect,
256 m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
257 false, true, &local_clip_rect);
267 s32 GUIEditBoxWithScrollBar::getCursorPos(s32 x, s32 y)
269 IGUIFont* font = getActiveFont();
271 const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
273 core::stringw *txt_line = 0;
277 for (u32 i = 0; i < line_count; ++i) {
279 if (i == 0 && y < m_current_text_rect.UpperLeftCorner.Y)
280 y = m_current_text_rect.UpperLeftCorner.Y;
281 if (i == line_count - 1 && y > m_current_text_rect.LowerRightCorner.Y)
282 y = m_current_text_rect.LowerRightCorner.Y;
284 // is it inside this region?
285 if (y >= m_current_text_rect.UpperLeftCorner.Y && y <= m_current_text_rect.LowerRightCorner.Y) {
286 // we've found the clicked line
287 txt_line = (m_word_wrap || m_multiline) ? &m_broken_text[i] : &Text;
288 start_pos = (m_word_wrap || m_multiline) ? m_broken_text_positions[i] : 0;
293 if (x < m_current_text_rect.UpperLeftCorner.X)
294 x = m_current_text_rect.UpperLeftCorner.X;
299 s32 idx = font->getCharacterFromPos(txt_line->c_str(), x - m_current_text_rect.UpperLeftCorner.X);
301 // click was on or left of the line
303 return idx + start_pos;
305 // click was off the right edge of the line, go to end.
306 return txt_line->size() + start_pos;
310 //! Breaks the single text line.
311 void GUIEditBoxWithScrollBar::breakText()
313 if ((!m_word_wrap && !m_multiline))
316 m_broken_text.clear(); // need to reallocate :/
317 m_broken_text_positions.clear();
319 IGUIFont* font = getActiveFont();
323 m_last_break_font = font;
327 core::stringw whitespace;
328 s32 last_line_start = 0;
329 s32 size = Text.size();
331 s32 el_width = RelativeRect.getWidth() - m_scrollbar_width - 10;
334 for (s32 i = 0; i < size; ++i) {
336 bool line_break = false;
338 if (c == L'\r') { // Mac or Windows breaks
342 if (Text[i + 1] == L'\n') { // Windows breaks
343 // TODO: I (Michael) think that we shouldn't change the text given by the user for whatever reason.
344 // Instead rework the cursor positioning to be able to handle this (but not in stable release
345 // branch as users might already expect this behavior).
348 if (m_cursor_pos > i)
351 } else if (c == L'\n') { // Unix breaks
356 // don't break if we're not a multi-line edit box
360 if (c == L' ' || c == 0 || i == (size - 1)) {
361 // here comes the next whitespace, look if
362 // we can break the last word to the next line
363 // We also break whitespace, otherwise cursor would vanish beside the right border.
364 s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
365 s32 worldlgth = font->getDimension(word.c_str()).Width;
367 if (m_word_wrap && length + worldlgth + whitelgth > el_width && line.size() > 0) {
368 // break to next line
370 m_broken_text.push_back(line);
371 m_broken_text_positions.push_back(last_line_start);
372 last_line_start = i - (s32)word.size();
378 length += whitelgth + worldlgth;
388 // compute line break
392 m_broken_text.push_back(line);
393 m_broken_text_positions.push_back(last_line_start);
394 last_line_start = i + 1;
401 // yippee this is a word..
408 m_broken_text.push_back(line);
409 m_broken_text_positions.push_back(last_line_start);
412 // TODO: that function does interpret VAlign according to line-index (indexed
413 // line is placed on top-center-bottom) but HAlign according to line-width
414 // (pixels) and not by row.
415 // Intuitively I suppose HAlign handling is better as VScrollPos should handle
416 // the line-scrolling.
417 // But please no one change this without also rewriting (and this time
418 // testing!!!) autoscrolling (I noticed this when fixing the old autoscrolling).
419 void GUIEditBoxWithScrollBar::setTextRect(s32 line)
424 IGUIFont* font = getActiveFont();
428 core::dimension2du d;
430 // get text dimension
431 const u32 line_count = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
432 if (m_word_wrap || m_multiline) {
433 d = font->getDimension(m_broken_text[line].c_str());
435 d = font->getDimension(Text.c_str());
436 d.Height = AbsoluteRect.getHeight();
438 d.Height += font->getKerningHeight();
444 m_current_text_rect.UpperLeftCorner.X = (m_frame_rect.getWidth() / 2) - (d.Width / 2);
445 m_current_text_rect.LowerRightCorner.X = (m_frame_rect.getWidth() / 2) + (d.Width / 2);
447 case EGUIA_LOWERRIGHT:
448 // align to right edge
449 m_current_text_rect.UpperLeftCorner.X = m_frame_rect.getWidth() - d.Width;
450 m_current_text_rect.LowerRightCorner.X = m_frame_rect.getWidth();
453 // align to left edge
454 m_current_text_rect.UpperLeftCorner.X = 0;
455 m_current_text_rect.LowerRightCorner.X = d.Width;
462 m_current_text_rect.UpperLeftCorner.Y =
463 (m_frame_rect.getHeight() / 2) - (line_count*d.Height) / 2 + d.Height*line;
465 case EGUIA_LOWERRIGHT:
466 // align to bottom edge
467 m_current_text_rect.UpperLeftCorner.Y =
468 m_frame_rect.getHeight() - line_count*d.Height + d.Height*line;
472 m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
476 m_current_text_rect.UpperLeftCorner.X -= m_hscroll_pos;
477 m_current_text_rect.LowerRightCorner.X -= m_hscroll_pos;
478 m_current_text_rect.UpperLeftCorner.Y -= m_vscroll_pos;
479 m_current_text_rect.LowerRightCorner.Y = m_current_text_rect.UpperLeftCorner.Y + d.Height;
481 m_current_text_rect += m_frame_rect.UpperLeftCorner;
484 // calculate autoscroll
485 void GUIEditBoxWithScrollBar::calculateScrollPos()
490 IGUISkin* skin = Environment->getSkin();
493 IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
497 s32 curs_line = getLineFromPos(m_cursor_pos);
500 setTextRect(curs_line);
501 const bool has_broken_text = m_multiline || m_word_wrap;
503 // Check horizonal scrolling
504 // NOTE: Calculations different to vertical scrolling because setTextRect interprets VAlign relative to line but HAlign not relative to row
506 // get cursor position
507 IGUIFont* font = getActiveFont();
512 irr::u32 cursor_width = font->getDimension(L"_").Width;
513 core::stringw *txt_line = has_broken_text ? &m_broken_text[curs_line] : &Text;
514 s32 cpos = has_broken_text ? m_cursor_pos - m_broken_text_positions[curs_line] : m_cursor_pos; // column
515 s32 cstart = font->getDimension(txt_line->subString(0, cpos).c_str()).Width; // pixels from text-start
516 s32 cend = cstart + cursor_width;
517 s32 txt_width = font->getDimension(txt_line->c_str()).Width;
519 if (txt_width < m_frame_rect.getWidth()) {
520 // TODO: Needs a clean left and right gap removal depending on HAlign, similar to vertical scrolling tests for top/bottom.
521 // This check just fixes the case where it was most noticable (text smaller than clipping area).
524 setTextRect(curs_line);
527 if (m_current_text_rect.UpperLeftCorner.X + cstart < m_frame_rect.UpperLeftCorner.X) {
528 // cursor to the left of the clipping area
529 m_hscroll_pos -= m_frame_rect.UpperLeftCorner.X - (m_current_text_rect.UpperLeftCorner.X + cstart);
530 setTextRect(curs_line);
532 // TODO: should show more characters to the left when we're scrolling left
533 // and the cursor reaches the border.
534 } else if (m_current_text_rect.UpperLeftCorner.X + cend > m_frame_rect.LowerRightCorner.X) {
535 // cursor to the right of the clipping area
536 m_hscroll_pos += (m_current_text_rect.UpperLeftCorner.X + cend) - m_frame_rect.LowerRightCorner.X;
537 setTextRect(curs_line);
541 // calculate vertical scrolling
542 if (has_broken_text) {
543 irr::u32 line_height = font->getDimension(L"A").Height + font->getKerningHeight();
544 // only up to 1 line fits?
545 if (line_height >= (irr::u32)m_frame_rect.getHeight()) {
547 setTextRect(curs_line);
548 s32 unscrolledPos = m_current_text_rect.UpperLeftCorner.Y;
549 s32 pivot = m_frame_rect.UpperLeftCorner.Y;
552 pivot += m_frame_rect.getHeight() / 2;
553 unscrolledPos += line_height / 2;
555 case EGUIA_LOWERRIGHT:
556 pivot += m_frame_rect.getHeight();
557 unscrolledPos += line_height;
562 m_vscroll_pos = unscrolledPos - pivot;
563 setTextRect(curs_line);
565 // First 2 checks are necessary when people delete lines
567 if (m_current_text_rect.UpperLeftCorner.Y > m_frame_rect.UpperLeftCorner.Y && m_valign != EGUIA_LOWERRIGHT) {
568 // first line is leaving a gap on top
570 } else if (m_valign != EGUIA_UPPERLEFT) {
571 u32 lastLine = m_broken_text_positions.empty() ? 0 : m_broken_text_positions.size() - 1;
572 setTextRect(lastLine);
573 if (m_current_text_rect.LowerRightCorner.Y < m_frame_rect.LowerRightCorner.Y)
575 // last line is leaving a gap on bottom
576 m_vscroll_pos -= m_frame_rect.LowerRightCorner.Y - m_current_text_rect.LowerRightCorner.Y;
580 setTextRect(curs_line);
581 if (m_current_text_rect.UpperLeftCorner.Y < m_frame_rect.UpperLeftCorner.Y) {
582 // text above valid area
583 m_vscroll_pos -= m_frame_rect.UpperLeftCorner.Y - m_current_text_rect.UpperLeftCorner.Y;
584 setTextRect(curs_line);
585 } else if (m_current_text_rect.LowerRightCorner.Y > m_frame_rect.LowerRightCorner.Y){
586 // text below valid area
587 m_vscroll_pos += m_current_text_rect.LowerRightCorner.Y - m_frame_rect.LowerRightCorner.Y;
588 setTextRect(curs_line);
594 m_vscrollbar->setPos(m_vscroll_pos);
598 void GUIEditBoxWithScrollBar::calculateFrameRect()
600 m_frame_rect = AbsoluteRect;
605 skin = Environment->getSkin();
606 if (m_border && skin) {
607 m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
608 m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
609 m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X) + 1;
610 m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y) + 1;
616 //! create a vertical scroll bar
617 void GUIEditBoxWithScrollBar::createVScrollBar()
621 skin = Environment->getSkin();
623 m_scrollbar_width = skin ? skin->getSize(gui::EGDS_SCROLLBAR_SIZE) : 16;
625 irr::core::rect<s32> scrollbarrect = m_frame_rect;
626 scrollbarrect.UpperLeftCorner.X += m_frame_rect.getWidth() - m_scrollbar_width;
627 m_vscrollbar = new GUIScrollBar(Environment, getParent(), -1,
628 scrollbarrect, false, true);
630 m_vscrollbar->setVisible(false);
631 m_vscrollbar->setSmallStep(1);
632 m_vscrollbar->setLargeStep(1);
637 //! Change the background color
638 void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color)
640 m_bg_color = bg_color;
641 m_bg_color_used = true;
644 //! Writes attributes of the element.
645 void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const
647 out->addBool("Border", m_border);
648 out->addBool("Background", m_background);
649 // out->addFont("OverrideFont", OverrideFont);
651 GUIEditBox::serializeAttributes(out, options);
655 //! Reads attributes of the element
656 void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0)
658 GUIEditBox::deserializeAttributes(in, options);
660 setDrawBorder(in->getAttributeAsBool("Border"));
661 setDrawBackground(in->getAttributeAsBool("Background"));
664 bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; }
665 bool GUIEditBoxWithScrollBar::isDrawBorderEnabled() const { return false; }
666 void GUIEditBoxWithScrollBar::setCursorChar(const wchar_t cursorChar) { }
667 wchar_t GUIEditBoxWithScrollBar::getCursorChar() const { return '|'; }
668 void GUIEditBoxWithScrollBar::setCursorBlinkTime(irr::u32 timeMs) { }
669 irr::u32 GUIEditBoxWithScrollBar::getCursorBlinkTime() const { return 500; }