]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/intlGUIEditBox.cpp
Use "Aux1" key name consistently everywhere
[dragonfireclient.git] / src / gui / 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 #include "IGUISkin.h"
36 #include "IGUIEnvironment.h"
37 #include "IGUIFont.h"
38 #include "IVideoDriver.h"
39 //#include "irrlicht/os.cpp"
40 #include "porting.h"
41 //#include "Keycodes.h"
42 #include "log.h"
43
44 /*
45         todo:
46         optional scrollbars
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
50         numerical
51 */
52
53 namespace irr
54 {
55 namespace gui
56 {
57
58 //! constructor
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)
63 {
64         #ifdef _DEBUG
65         setDebugName("intlintlGUIEditBox");
66         #endif
67
68         Text = text;
69
70         if (Environment)
71                 m_operator = Environment->getOSOperator();
72
73         if (m_operator)
74                 m_operator->grab();
75
76         // this element can be tabbed to
77         setTabStop(true);
78         setTabOrder(-1);
79
80         IGUISkin *skin = 0;
81         if (Environment)
82                 skin = Environment->getSkin();
83         if (m_border && skin)
84         {
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;
89         }
90
91         if (skin && has_vscrollbar) {
92                 m_scrollbar_width = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
93
94                 if (m_scrollbar_width > 0) {
95                         createVScrollBar();
96                 }
97         }
98
99         breakText();
100
101         calculateScrollPos();
102         setWritable(writable);
103 }
104
105 //! Sets whether to draw the background
106 void intlGUIEditBox::setDrawBackground(bool draw)
107 {
108 }
109
110 void intlGUIEditBox::updateAbsolutePosition()
111 {
112     core::rect<s32> oldAbsoluteRect(AbsoluteRect);
113         IGUIElement::updateAbsolutePosition();
114         if ( oldAbsoluteRect != AbsoluteRect )
115         {
116         breakText();
117         }
118 }
119
120
121 //! draws the element and its children
122 void intlGUIEditBox::draw()
123 {
124         if (!IsVisible)
125                 return;
126
127         const bool focus = Environment->hasFocus(this);
128
129         IGUISkin* skin = Environment->getSkin();
130         if (!skin)
131                 return;
132
133         m_frame_rect = AbsoluteRect;
134
135         // draw the border
136
137         if (m_border)
138         {
139                 if (m_writable) {
140                         skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
141                                 false, true, m_frame_rect, &AbsoluteClippingRect);
142                 }
143
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;
148         }
149
150         updateVScrollBar();
151         core::rect<s32> localClipRect = m_frame_rect;
152         localClipRect.clipAgainst(AbsoluteClippingRect);
153
154         // draw the text
155
156         IGUIFont* font = m_override_font;
157         if (!m_override_font)
158                 font = skin->getFont();
159
160         s32 cursorLine = 0;
161         s32 charcursorpos = 0;
162
163         if (font)
164         {
165                 if (m_last_break_font != font)
166                 {
167                         breakText();
168                 }
169
170                 // calculate cursor pos
171
172                 core::stringw *txtLine = &Text;
173                 s32 startPos = 0;
174
175                 core::stringw s, s2;
176
177                 // get mark position
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;
184
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;
189
190                 if (!Text.empty()) {
191                         if (!IsEnabled && !m_override_color_enabled)
192                         {
193                                 m_override_color_enabled = true;
194                                 m_override_color = skin->getColor(EGDC_GRAY_TEXT);
195                         }
196
197                         for (s32 i=0; i < lineCount; ++i)
198                         {
199                                 setTextRect(i);
200
201                                 // clipping test - don't draw anything outside the visible area
202                                 core::rect<s32> c = localClipRect;
203                                 c.clipAgainst(m_current_text_rect);
204                                 if (!c.isValid())
205                                         continue;
206
207                                 // get current line
208                                 if (m_passwordbox)
209                                 {
210                                         if (m_broken_text.size() != 1)
211                                         {
212                                                 m_broken_text.clear();
213                                                 m_broken_text.emplace_back();
214                                         }
215                                         if (m_broken_text[0].size() != Text.size())
216                                         {
217                                                 m_broken_text[0] = Text;
218                                                 for (u32 q = 0; q < Text.size(); ++q)
219                                                 {
220                                                         m_broken_text[0] [q] = m_passwordchar;
221                                                 }
222                                         }
223                                         txtLine = &m_broken_text[0];
224                                         startPos = 0;
225                                 }
226                                 else
227                                 {
228                                         txtLine = ml ? &m_broken_text[i] : &Text;
229                                         startPos = ml ? m_broken_text_positions[i] : 0;
230                                 }
231
232
233                                 // draw normal text
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);
237
238                                 // draw mark and marked text
239                                 if (focus && m_mark_begin != m_mark_end && i >= hlineStart && i < hlineStart + hlineCount)
240                                 {
241
242                                         s32 mbegin = 0, mend = 0;
243                                         s32 lineStartPos = 0, lineEndPos = txtLine->size();
244
245                                         if (i == hlineStart)
246                                         {
247                                                 // highlight start is on this line
248                                                 s = txtLine->subString(0, realmbgn - startPos);
249                                                 mbegin = font->getDimension(s.c_str()).Width;
250
251                                                 // deal with kerning
252                                                 mbegin += font->getKerningWidth(
253                                                         &((*txtLine)[realmbgn - startPos]),
254                                                         realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
255
256                                                 lineStartPos = realmbgn - startPos;
257                                         }
258                                         if (i == hlineStart + hlineCount - 1)
259                                         {
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();
264                                         }
265                                         else
266                                                 mend = font->getDimension(txtLine->c_str()).Width;
267
268                                         m_current_text_rect.UpperLeftCorner.X += mbegin;
269                                         m_current_text_rect.LowerRightCorner.X = m_current_text_rect.UpperLeftCorner.X + mend - mbegin;
270
271                                         // draw mark
272                                         skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), m_current_text_rect, &localClipRect);
273
274                                         // draw marked text
275                                         s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
276
277                                         if (!s.empty())
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);
281
282                                 }
283                         }
284
285                         // Return the override color information to its previous settings.
286                         m_override_color_enabled = prevOver;
287                         m_override_color = prevColor;
288                 }
289
290                 // draw cursor
291
292                 if (m_word_wrap || m_multiline)
293                 {
294                         cursorLine = getLineFromPos(m_cursor_pos);
295                         txtLine = &m_broken_text[cursorLine];
296                         startPos = m_broken_text_positions[cursorLine];
297                 }
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);
301
302                 if (m_writable) {
303                         if (focus && (porting::getTimeMs() - m_blink_start_time) % 700 < 350) {
304                                 setTextRect(cursorLine);
305                                 m_current_text_rect.UpperLeftCorner.X += charcursorpos;
306
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);
310                         }
311                 }
312         }
313
314         // draw children
315         IGUIElement::draw();
316 }
317
318
319 s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
320 {
321         IGUIFont* font = getActiveFont();
322
323         const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
324
325         core::stringw *txtLine = NULL;
326         s32 startPos = 0;
327         u32 curr_line_idx = 0;
328         x += 3;
329
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;
336
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;
342                         break;
343                 }
344         }
345
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;
350
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)
355                 idx++;
356
357         return rangelim(idx + startPos, 0, S32_MAX);
358 }
359
360
361 //! Breaks the single text line.
362 void intlGUIEditBox::breakText()
363 {
364         IGUISkin* skin = Environment->getSkin();
365
366         if ((!m_word_wrap && !m_multiline) || !skin)
367                 return;
368
369         m_broken_text.clear(); // need to reallocate :/
370         m_broken_text_positions.clear();
371
372         IGUIFont* font = m_override_font;
373         if (!m_override_font)
374                 font = skin->getFont();
375
376         if (!font)
377                 return;
378
379         m_last_break_font = font;
380
381         core::stringw line;
382         core::stringw word;
383         core::stringw whitespace;
384         s32 lastLineStart = 0;
385         s32 size = Text.size();
386         s32 length = 0;
387         s32 elWidth = RelativeRect.getWidth() - m_scrollbar_width - 10;
388         wchar_t c;
389
390         for (s32 i=0; i<size; ++i)
391         {
392                 c = Text[i];
393                 bool lineBreak = false;
394
395                 if (c == L'\r') // Mac or Windows breaks
396                 {
397                         lineBreak = true;
398                         c = ' ';
399                         if (Text[i+1] == L'\n') // Windows breaks
400                         {
401                                 Text.erase(i+1);
402                                 --size;
403                         }
404                 }
405                 else if (c == L'\n') // Unix breaks
406                 {
407                         lineBreak = true;
408                         c = ' ';
409                 }
410
411                 // don't break if we're not a multi-line edit box
412                 if (!m_multiline)
413                         lineBreak = false;
414
415                 if (c == L' ' || c == 0 || i == (size-1))
416                 {
417                         if (!word.empty()) {
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;
422
423                                 if (m_word_wrap && length + worldlgth + whitelgth > elWidth)
424                                 {
425                                         // break to next line
426                                         length = worldlgth;
427                                         m_broken_text.push_back(line);
428                                         m_broken_text_positions.push_back(lastLineStart);
429                                         lastLineStart = i - (s32)word.size();
430                                         line = word;
431                                 }
432                                 else
433                                 {
434                                         // add word to line
435                                         line += whitespace;
436                                         line += word;
437                                         length += whitelgth + worldlgth;
438                                 }
439
440                                 word = L"";
441                                 whitespace = L"";
442                         }
443
444                         whitespace += c;
445
446                         // compute line break
447                         if (lineBreak)
448                         {
449                                 line += whitespace;
450                                 line += word;
451                                 m_broken_text.push_back(line);
452                                 m_broken_text_positions.push_back(lastLineStart);
453                                 lastLineStart = i+1;
454                                 line = L"";
455                                 word = L"";
456                                 whitespace = L"";
457                                 length = 0;
458                         }
459                 }
460                 else
461                 {
462                         // yippee this is a word..
463                         word += c;
464                 }
465         }
466
467         line += whitespace;
468         line += word;
469         m_broken_text.push_back(line);
470         m_broken_text_positions.push_back(lastLineStart);
471 }
472
473
474 void intlGUIEditBox::setTextRect(s32 line)
475 {
476         core::dimension2du d;
477
478         IGUISkin* skin = Environment->getSkin();
479         if (!skin)
480                 return;
481
482         IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
483
484         if (!font)
485                 return;
486
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)
490         {
491                 d = font->getDimension(m_broken_text[line].c_str());
492         }
493         else
494         {
495                 d = font->getDimension(Text.c_str());
496                 d.Height = AbsoluteRect.getHeight();
497         }
498         d.Height += font->getKerningHeight();
499
500         // justification
501         switch (m_halign)
502         {
503         case EGUIA_CENTER:
504                 // align to h centre
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);
507                 break;
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();
512                 break;
513         default:
514                 // align to left edge
515                 m_current_text_rect.UpperLeftCorner.X = 0;
516                 m_current_text_rect.LowerRightCorner.X = d.Width;
517
518         }
519
520         switch (m_valign)
521         {
522         case EGUIA_CENTER:
523                 // align to v centre
524                 m_current_text_rect.UpperLeftCorner.Y =
525                         (m_frame_rect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
526                 break;
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;
531                 break;
532         default:
533                 // align to top edge
534                 m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
535                 break;
536         }
537
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;
542
543         m_current_text_rect += m_frame_rect.UpperLeftCorner;
544
545 }
546
547 void intlGUIEditBox::calculateScrollPos()
548 {
549         if (!m_autoscroll)
550                 return;
551
552         // calculate horizontal scroll position
553         s32 cursLine = getLineFromPos(m_cursor_pos);
554         setTextRect(cursLine);
555
556         // don't do horizontal scrolling when wordwrap is enabled.
557         if (!m_word_wrap)
558         {
559                 // get cursor position
560                 IGUISkin* skin = Environment->getSkin();
561                 if (!skin)
562                         return;
563                 IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
564                 if (!font)
565                         return;
566
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;
569
570                 s32 cStart = m_current_text_rect.UpperLeftCorner.X + m_hscroll_pos +
571                         font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
572
573                 s32 cEnd = cStart + font->getDimension(L"_ ").Width;
574
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;
579                 else
580                         m_hscroll_pos = 0;
581
582                 // todo: adjust scrollbar
583         }
584
585         if (!m_word_wrap && !m_multiline)
586                 return;
587
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
593
594         // todo: adjust scrollbar
595         if (m_vscrollbar)
596                 m_vscrollbar->setPos(m_vscroll_pos);
597 }
598
599
600 //! Create a vertical scrollbar
601 void intlGUIEditBox::createVScrollBar()
602 {
603         s32 fontHeight = 1;
604
605         if (m_override_font) {
606                 fontHeight = m_override_font->getDimension(L"").Height;
607         } else {
608                 if (IGUISkin* skin = Environment->getSkin()) {
609                         if (IGUIFont* font = skin->getFont()) {
610                                 fontHeight = font->getDimension(L"").Height;
611                         }
612                 }
613         }
614
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);
619
620         m_vscrollbar->setVisible(false);
621         m_vscrollbar->setSmallStep(3 * fontHeight);
622         m_vscrollbar->setLargeStep(10 * fontHeight);
623 }
624
625 } // end namespace gui
626 } // end namespace irr