~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CGUIEditBox.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 
 
5
#include "CGUIEditBox.h"
 
6
#ifdef _IRR_COMPILE_WITH_GUI_
 
7
 
 
8
#include "IGUISkin.h"
 
9
#include "IGUIEnvironment.h"
 
10
#include "IGUIFont.h"
 
11
#include "IVideoDriver.h"
 
12
#include "rect.h"
 
13
#include "os.h"
 
14
#include "Keycodes.h"
 
15
 
 
16
/*
 
17
        todo:
 
18
        optional scrollbars
 
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
 
22
        numerical
 
23
*/
 
24
 
 
25
namespace irr
 
26
{
 
27
namespace gui
 
28
{
 
29
 
 
30
//! constructor
 
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)
 
41
{
 
42
        #ifdef _DEBUG
 
43
        setDebugName("CGUIEditBox");
 
44
        #endif
 
45
 
 
46
        Text = text;
 
47
 
 
48
        if (Environment)
 
49
                Operator = Environment->getOSOperator();
 
50
 
 
51
        if (Operator)
 
52
                Operator->grab();
 
53
 
 
54
        // this element can be tabbed to
 
55
        setTabStop(true);
 
56
        setTabOrder(-1);
 
57
 
 
58
        calculateFrameRect();
 
59
        breakText();
 
60
 
 
61
        calculateScrollPos();
 
62
}
 
63
 
 
64
 
 
65
//! destructor
 
66
CGUIEditBox::~CGUIEditBox()
 
67
{
 
68
        if (OverrideFont)
 
69
                OverrideFont->drop();
 
70
 
 
71
        if (Operator)
 
72
                Operator->drop();
 
73
}
 
74
 
 
75
 
 
76
//! Sets another skin independent font.
 
77
void CGUIEditBox::setOverrideFont(IGUIFont* font)
 
78
{
 
79
        if (OverrideFont == font)
 
80
                return;
 
81
 
 
82
        if (OverrideFont)
 
83
                OverrideFont->drop();
 
84
 
 
85
        OverrideFont = font;
 
86
 
 
87
        if (OverrideFont)
 
88
                OverrideFont->grab();
 
89
 
 
90
        breakText();
 
91
}
 
92
 
 
93
//! Gets the override font (if any)
 
94
IGUIFont * CGUIEditBox::getOverrideFont() const
 
95
{
 
96
        return OverrideFont;
 
97
}
 
98
 
 
99
//! Get the font which is used right now for drawing
 
100
IGUIFont* CGUIEditBox::getActiveFont() const
 
101
{
 
102
        if ( OverrideFont )
 
103
                return OverrideFont;
 
104
        IGUISkin* skin = Environment->getSkin();
 
105
        if (skin)
 
106
                return skin->getFont();
 
107
        return 0;
 
108
}
 
109
 
 
110
//! Sets another color for the text.
 
111
void CGUIEditBox::setOverrideColor(video::SColor color)
 
112
{
 
113
        OverrideColor = color;
 
114
        OverrideColorEnabled = true;
 
115
}
 
116
 
 
117
 
 
118
video::SColor const& CGUIEditBox::getOverrideColor() const
 
119
{
 
120
        return OverrideColor;
 
121
}
 
122
 
 
123
 
 
124
//! Turns the border on or off
 
125
void CGUIEditBox::setDrawBorder(bool border)
 
126
{
 
127
        Border = border;
 
128
}
 
129
 
 
130
//! Sets whether to draw the background
 
131
void CGUIEditBox::setDrawBackground(bool draw)
 
132
{
 
133
        Background = draw;
 
134
}
 
135
 
 
136
//! Sets if the text should use the overide color or the color in the gui skin.
 
137
void CGUIEditBox::enableOverrideColor(bool enable)
 
138
{
 
139
        OverrideColorEnabled = enable;
 
140
}
 
141
 
 
142
bool CGUIEditBox::isOverrideColorEnabled() const
 
143
{
 
144
        _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
 
145
        return OverrideColorEnabled;
 
146
}
 
147
 
 
148
//! Enables or disables word wrap
 
149
void CGUIEditBox::setWordWrap(bool enable)
 
150
{
 
151
        WordWrap = enable;
 
152
        breakText();
 
153
}
 
154
 
 
155
 
 
156
void CGUIEditBox::updateAbsolutePosition()
 
157
{
 
158
    core::rect<s32> oldAbsoluteRect(AbsoluteRect);
 
159
        IGUIElement::updateAbsolutePosition();
 
160
        if ( oldAbsoluteRect != AbsoluteRect )
 
161
        {
 
162
                calculateFrameRect();
 
163
        breakText();
 
164
        calculateScrollPos();
 
165
        }
 
166
}
 
167
 
 
168
 
 
169
//! Checks if word wrap is enabled
 
170
bool CGUIEditBox::isWordWrapEnabled() const
 
171
{
 
172
        _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
 
173
        return WordWrap;
 
174
}
 
175
 
 
176
 
 
177
//! Enables or disables newlines.
 
178
void CGUIEditBox::setMultiLine(bool enable)
 
179
{
 
180
        MultiLine = enable;
 
181
}
 
182
 
 
183
 
 
184
//! Checks if multi line editing is enabled
 
185
bool CGUIEditBox::isMultiLineEnabled() const
 
186
{
 
187
        _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
 
188
        return MultiLine;
 
189
}
 
190
 
 
191
 
 
192
void CGUIEditBox::setPasswordBox(bool passwordBox, wchar_t passwordChar)
 
193
{
 
194
        PasswordBox = passwordBox;
 
195
        if (PasswordBox)
 
196
        {
 
197
                PasswordChar = passwordChar;
 
198
                setMultiLine(false);
 
199
                setWordWrap(false);
 
200
                BrokenText.clear();
 
201
        }
 
202
}
 
203
 
 
204
 
 
205
bool CGUIEditBox::isPasswordBox() const
 
206
{
 
207
        _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
 
208
        return PasswordBox;
 
209
}
 
210
 
 
211
 
 
212
//! Sets text justification
 
213
void CGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
 
214
{
 
215
        HAlign = horizontal;
 
216
        VAlign = vertical;
 
217
}
 
218
 
 
219
 
 
220
//! called if an event happened.
 
221
bool CGUIEditBox::OnEvent(const SEvent& event)
 
222
{
 
223
        if (isEnabled())
 
224
        {
 
225
 
 
226
                switch(event.EventType)
 
227
                {
 
228
                case EET_GUI_EVENT:
 
229
                        if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
 
230
                        {
 
231
                                if (event.GUIEvent.Caller == this)
 
232
                                {
 
233
                                        MouseMarking = false;
 
234
                                        setTextMarkers(0,0);
 
235
                                }
 
236
                        }
 
237
                        break;
 
238
                case EET_KEY_INPUT_EVENT:
 
239
                        if (processKey(event))
 
240
                                return true;
 
241
                        break;
 
242
                case EET_MOUSE_INPUT_EVENT:
 
243
                        if (processMouse(event))
 
244
                                return true;
 
245
                        break;
 
246
                default:
 
247
                        break;
 
248
                }
 
249
        }
 
250
 
 
251
        return IGUIElement::OnEvent(event);
 
252
}
 
253
 
 
254
 
 
255
bool CGUIEditBox::processKey(const SEvent& event)
 
256
{
 
257
        if (!event.KeyInput.PressedDown)
 
258
                return false;
 
259
 
 
260
        bool textChanged = false;
 
261
        s32 newMarkBegin = MarkBegin;
 
262
        s32 newMarkEnd = MarkEnd;
 
263
 
 
264
        // control shortcut handling
 
265
 
 
266
        if (event.KeyInput.Control)
 
267
        {
 
268
                // german backlash '\' entered with control + '?'
 
269
                if ( event.KeyInput.Char == '\\' )
 
270
                {
 
271
                        inputChar(event.KeyInput.Char);
 
272
                        return true;
 
273
                }
 
274
 
 
275
                switch(event.KeyInput.Key)
 
276
                {
 
277
                case KEY_KEY_A:
 
278
                        // select all
 
279
                        newMarkBegin = 0;
 
280
                        newMarkEnd = Text.size();
 
281
                        break;
 
282
                case KEY_KEY_C:
 
283
                        // copy to clipboard
 
284
                        if (!PasswordBox && Operator && MarkBegin != MarkEnd)
 
285
                        {
 
286
                                const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
 
287
                                const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
 
288
 
 
289
                                core::stringc s;
 
290
                                s = Text.subString(realmbgn, realmend - realmbgn).c_str();
 
291
                                Operator->copyToClipboard(s.c_str());
 
292
                        }
 
293
                        break;
 
294
                case KEY_KEY_X:
 
295
                        // cut to the clipboard
 
296
                        if (!PasswordBox && Operator && MarkBegin != MarkEnd)
 
297
                        {
 
298
                                const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
 
299
                                const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
 
300
 
 
301
                                // copy
 
302
                                core::stringc sc;
 
303
                                sc = Text.subString(realmbgn, realmend - realmbgn).c_str();
 
304
                                Operator->copyToClipboard(sc.c_str());
 
305
 
 
306
                                if (isEnabled())
 
307
                                {
 
308
                                        // delete
 
309
                                        core::stringw s;
 
310
                                        s = Text.subString(0, realmbgn);
 
311
                                        s.append( Text.subString(realmend, Text.size()-realmend) );
 
312
                                        Text = s;
 
313
 
 
314
                                        CursorPos = realmbgn;
 
315
                                        newMarkBegin = 0;
 
316
                                        newMarkEnd = 0;
 
317
                                        textChanged = true;
 
318
                                }
 
319
                        }
 
320
                        break;
 
321
                case KEY_KEY_V:
 
322
                        if ( !isEnabled() )
 
323
                                break;
 
324
 
 
325
                        // paste from the clipboard
 
326
                        if (Operator)
 
327
                        {
 
328
                                const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
 
329
                                const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
 
330
 
 
331
                                // add new character
 
332
                                const c8* p = Operator->getTextFromClipboard();
 
333
                                if (p)
 
334
                                {
 
335
                                        if (MarkBegin == MarkEnd)
 
336
                                        {
 
337
                                                // insert text
 
338
                                                core::stringw s = Text.subString(0, CursorPos);
 
339
                                                s.append(p);
 
340
                                                s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
 
341
 
 
342
                                                if (!Max || s.size()<=Max) // thx to Fish FH for fix
 
343
                                                {
 
344
                                                        Text = s;
 
345
                                                        s = p;
 
346
                                                        CursorPos += s.size();
 
347
                                                }
 
348
                                        }
 
349
                                        else
 
350
                                        {
 
351
                                                // replace text
 
352
 
 
353
                                                core::stringw s = Text.subString(0, realmbgn);
 
354
                                                s.append(p);
 
355
                                                s.append( Text.subString(realmend, Text.size()-realmend) );
 
356
 
 
357
                                                if (!Max || s.size()<=Max)  // thx to Fish FH for fix
 
358
                                                {
 
359
                                                        Text = s;
 
360
                                                        s = p;
 
361
                                                        CursorPos = realmbgn + s.size();
 
362
                                                }
 
363
                                        }
 
364
                                }
 
365
 
 
366
                                newMarkBegin = 0;
 
367
                                newMarkEnd = 0;
 
368
                                textChanged = true;
 
369
                        }
 
370
                        break;
 
371
                case KEY_HOME:
 
372
                        // move/highlight to start of text
 
373
                        if (event.KeyInput.Shift)
 
374
                        {
 
375
                                newMarkEnd = CursorPos;
 
376
                                newMarkBegin = 0;
 
377
                                CursorPos = 0;
 
378
                        }
 
379
                        else
 
380
                        {
 
381
                                CursorPos = 0;
 
382
                                newMarkBegin = 0;
 
383
                                newMarkEnd = 0;
 
384
                        }
 
385
                        break;
 
386
                case KEY_END:
 
387
                        // move/highlight to end of text
 
388
                        if (event.KeyInput.Shift)
 
389
                        {
 
390
                                newMarkBegin = CursorPos;
 
391
                                newMarkEnd = Text.size();
 
392
                                CursorPos = 0;
 
393
                        }
 
394
                        else
 
395
                        {
 
396
                                CursorPos = Text.size();
 
397
                                newMarkBegin = 0;
 
398
                                newMarkEnd = 0;
 
399
                        }
 
400
                        break;
 
401
                default:
 
402
                        return false;
 
403
                }
 
404
        }
 
405
        // default keyboard handling
 
406
        else
 
407
        switch(event.KeyInput.Key)
 
408
        {
 
409
        case KEY_END:
 
410
                {
 
411
                        s32 p = Text.size();
 
412
                        if (WordWrap || MultiLine)
 
413
                        {
 
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' ))
 
417
                                        p-=1;
 
418
                        }
 
419
 
 
420
                        if (event.KeyInput.Shift)
 
421
                        {
 
422
                                if (MarkBegin == MarkEnd)
 
423
                                        newMarkBegin = CursorPos;
 
424
 
 
425
                                newMarkEnd = p;
 
426
                        }
 
427
                        else
 
428
                        {
 
429
                                newMarkBegin = 0;
 
430
                                newMarkEnd = 0;
 
431
                        }
 
432
                        CursorPos = p;
 
433
                        BlinkStartTime = os::Timer::getTime();
 
434
                }
 
435
                break;
 
436
        case KEY_HOME:
 
437
                {
 
438
 
 
439
                        s32 p = 0;
 
440
                        if (WordWrap || MultiLine)
 
441
                        {
 
442
                                p = getLineFromPos(CursorPos);
 
443
                                p = BrokenTextPositions[p];
 
444
                        }
 
445
 
 
446
                        if (event.KeyInput.Shift)
 
447
                        {
 
448
                                if (MarkBegin == MarkEnd)
 
449
                                        newMarkBegin = CursorPos;
 
450
                                newMarkEnd = p;
 
451
                        }
 
452
                        else
 
453
                        {
 
454
                                newMarkBegin = 0;
 
455
                                newMarkEnd = 0;
 
456
                        }
 
457
                        CursorPos = p;
 
458
                        BlinkStartTime = os::Timer::getTime();
 
459
                }
 
460
                break;
 
461
        case KEY_RETURN:
 
462
                if (MultiLine)
 
463
                {
 
464
                        inputChar(L'\n');
 
465
                        return true;
 
466
                }
 
467
                else
 
468
                {
 
469
                    sendGuiEvent( EGET_EDITBOX_ENTER );
 
470
                }
 
471
                break;
 
472
        case KEY_LEFT:
 
473
 
 
474
                if (event.KeyInput.Shift)
 
475
                {
 
476
                        if (CursorPos > 0)
 
477
                        {
 
478
                                if (MarkBegin == MarkEnd)
 
479
                                        newMarkBegin = CursorPos;
 
480
 
 
481
                                newMarkEnd = CursorPos-1;
 
482
                        }
 
483
                }
 
484
                else
 
485
                {
 
486
                        newMarkBegin = 0;
 
487
                        newMarkEnd = 0;
 
488
                }
 
489
 
 
490
                if (CursorPos > 0) CursorPos--;
 
491
                BlinkStartTime = os::Timer::getTime();
 
492
                break;
 
493
 
 
494
        case KEY_RIGHT:
 
495
                if (event.KeyInput.Shift)
 
496
                {
 
497
                        if (Text.size() > (u32)CursorPos)
 
498
                        {
 
499
                                if (MarkBegin == MarkEnd)
 
500
                                        newMarkBegin = CursorPos;
 
501
 
 
502
                                newMarkEnd = CursorPos+1;
 
503
                        }
 
504
                }
 
505
                else
 
506
                {
 
507
                        newMarkBegin = 0;
 
508
                        newMarkEnd = 0;
 
509
                }
 
510
 
 
511
                if (Text.size() > (u32)CursorPos) CursorPos++;
 
512
                BlinkStartTime = os::Timer::getTime();
 
513
                break;
 
514
        case KEY_UP:
 
515
                if (MultiLine || (WordWrap && BrokenText.size() > 1) )
 
516
                {
 
517
                        s32 lineNo = getLineFromPos(CursorPos);
 
518
                        s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin > MarkEnd ? MarkBegin : MarkEnd);
 
519
                        if (lineNo > 0)
 
520
                        {
 
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;
 
524
                                else
 
525
                                        CursorPos = BrokenTextPositions[lineNo-1] + cp;
 
526
                        }
 
527
 
 
528
                        if (event.KeyInput.Shift)
 
529
                        {
 
530
                                newMarkBegin = mb;
 
531
                                newMarkEnd = CursorPos;
 
532
                        }
 
533
                        else
 
534
                        {
 
535
                                newMarkBegin = 0;
 
536
                                newMarkEnd = 0;
 
537
                        }
 
538
 
 
539
                }
 
540
                else
 
541
                {
 
542
                        return false;
 
543
                }
 
544
                break;
 
545
        case KEY_DOWN:
 
546
                if (MultiLine || (WordWrap && BrokenText.size() > 1) )
 
547
                {
 
548
                        s32 lineNo = getLineFromPos(CursorPos);
 
549
                        s32 mb = (MarkBegin == MarkEnd) ? CursorPos : (MarkBegin < MarkEnd ? MarkBegin : MarkEnd);
 
550
                        if (lineNo < (s32)BrokenText.size()-1)
 
551
                        {
 
552
                                s32 cp = CursorPos - BrokenTextPositions[lineNo];
 
553
                                if ((s32)BrokenText[lineNo+1].size() < cp)
 
554
                                        CursorPos = BrokenTextPositions[lineNo+1] + BrokenText[lineNo+1].size()-1;
 
555
                                else
 
556
                                        CursorPos = BrokenTextPositions[lineNo+1] + cp;
 
557
                        }
 
558
 
 
559
                        if (event.KeyInput.Shift)
 
560
                        {
 
561
                                newMarkBegin = mb;
 
562
                                newMarkEnd = CursorPos;
 
563
                        }
 
564
                        else
 
565
                        {
 
566
                                newMarkBegin = 0;
 
567
                                newMarkEnd = 0;
 
568
                        }
 
569
 
 
570
                }
 
571
                else
 
572
                {
 
573
                        return false;
 
574
                }
 
575
                break;
 
576
 
 
577
        case KEY_BACK:
 
578
                if ( !isEnabled() )
 
579
                        break;
 
580
 
 
581
                if (Text.size())
 
582
                {
 
583
                        core::stringw s;
 
584
 
 
585
                        if (MarkBegin != MarkEnd)
 
586
                        {
 
587
                                // delete marked text
 
588
                                const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
 
589
                                const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
 
590
 
 
591
                                s = Text.subString(0, realmbgn);
 
592
                                s.append( Text.subString(realmend, Text.size()-realmend) );
 
593
                                Text = s;
 
594
 
 
595
                                CursorPos = realmbgn;
 
596
                        }
 
597
                        else
 
598
                        {
 
599
                                // delete text behind cursor
 
600
                                if (CursorPos>0)
 
601
                                        s = Text.subString(0, CursorPos-1);
 
602
                                else
 
603
                                        s = L"";
 
604
                                s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
 
605
                                Text = s;
 
606
                                --CursorPos;
 
607
                        }
 
608
 
 
609
                        if (CursorPos < 0)
 
610
                                CursorPos = 0;
 
611
                        BlinkStartTime = os::Timer::getTime();
 
612
                        newMarkBegin = 0;
 
613
                        newMarkEnd = 0;
 
614
                        textChanged = true;
 
615
                }
 
616
                break;
 
617
        case KEY_DELETE:
 
618
                if ( !isEnabled() )
 
619
                        break;
 
620
 
 
621
                if (Text.size() != 0)
 
622
                {
 
623
                        core::stringw s;
 
624
 
 
625
                        if (MarkBegin != MarkEnd)
 
626
                        {
 
627
                                // delete marked text
 
628
                                const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
 
629
                                const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
 
630
 
 
631
                                s = Text.subString(0, realmbgn);
 
632
                                s.append( Text.subString(realmend, Text.size()-realmend) );
 
633
                                Text = s;
 
634
 
 
635
                                CursorPos = realmbgn;
 
636
                        }
 
637
                        else
 
638
                        {
 
639
                                // delete text before cursor
 
640
                                s = Text.subString(0, CursorPos);
 
641
                                s.append( Text.subString(CursorPos+1, Text.size()-CursorPos-1) );
 
642
                                Text = s;
 
643
                        }
 
644
 
 
645
                        if (CursorPos > (s32)Text.size())
 
646
                                CursorPos = (s32)Text.size();
 
647
 
 
648
                        BlinkStartTime = os::Timer::getTime();
 
649
                        newMarkBegin = 0;
 
650
                        newMarkEnd = 0;
 
651
                        textChanged = true;
 
652
                }
 
653
                break;
 
654
 
 
655
        case KEY_ESCAPE:
 
656
        case KEY_TAB:
 
657
        case KEY_SHIFT:
 
658
        case KEY_F1:
 
659
        case KEY_F2:
 
660
        case KEY_F3:
 
661
        case KEY_F4:
 
662
        case KEY_F5:
 
663
        case KEY_F6:
 
664
        case KEY_F7:
 
665
        case KEY_F8:
 
666
        case KEY_F9:
 
667
        case KEY_F10:
 
668
        case KEY_F11:
 
669
        case KEY_F12:
 
670
        case KEY_F13:
 
671
        case KEY_F14:
 
672
        case KEY_F15:
 
673
        case KEY_F16:
 
674
        case KEY_F17:
 
675
        case KEY_F18:
 
676
        case KEY_F19:
 
677
        case KEY_F20:
 
678
        case KEY_F21:
 
679
        case KEY_F22:
 
680
        case KEY_F23:
 
681
        case KEY_F24:
 
682
                // ignore these keys
 
683
                return false;
 
684
 
 
685
        default:
 
686
                inputChar(event.KeyInput.Char);
 
687
                return true;
 
688
        }
 
689
 
 
690
    // Set new text markers
 
691
    setTextMarkers( newMarkBegin, newMarkEnd );
 
692
 
 
693
        // break the text if it has changed
 
694
        if (textChanged)
 
695
        {
 
696
                breakText();
 
697
                sendGuiEvent(EGET_EDITBOX_CHANGED);
 
698
        }
 
699
 
 
700
        calculateScrollPos();
 
701
 
 
702
        return true;
 
703
}
 
704
 
 
705
 
 
706
//! draws the element and its children
 
707
void CGUIEditBox::draw()
 
708
{
 
709
        if (!IsVisible)
 
710
                return;
 
711
 
 
712
        const bool focus = Environment->hasFocus(this);
 
713
 
 
714
        IGUISkin* skin = Environment->getSkin();
 
715
        if (!skin)
 
716
                return;
 
717
 
 
718
        EGUI_DEFAULT_COLOR bgCol = EGDC_GRAY_EDITABLE;
 
719
        if ( isEnabled() )
 
720
                bgCol = focus ? EGDC_FOCUSED_EDITABLE : EGDC_EDITABLE;
 
721
 
 
722
        if (!Border && Background)
 
723
        {
 
724
                skin->draw2DRectangle(this, skin->getColor(bgCol), AbsoluteRect, &AbsoluteClippingRect);
 
725
        }
 
726
 
 
727
        if (Border)
 
728
        {
 
729
                // draw the border
 
730
                skin->draw3DSunkenPane(this, skin->getColor(bgCol), false, Background, AbsoluteRect, &AbsoluteClippingRect);
 
731
 
 
732
                calculateFrameRect();
 
733
        }
 
734
 
 
735
        core::rect<s32> localClipRect = FrameRect;
 
736
        localClipRect.clipAgainst(AbsoluteClippingRect);
 
737
 
 
738
        // draw the text
 
739
 
 
740
        IGUIFont* font = getActiveFont();
 
741
 
 
742
        s32 cursorLine = 0;
 
743
        s32 charcursorpos = 0;
 
744
 
 
745
        if (font)
 
746
        {
 
747
                if (LastBreakFont != font)
 
748
                {
 
749
                        breakText();
 
750
                }
 
751
 
 
752
                // calculate cursor pos
 
753
 
 
754
                core::stringw *txtLine = &Text;
 
755
                s32 startPos = 0;
 
756
 
 
757
                core::stringw s, s2;
 
758
 
 
759
                // get mark position
 
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;
 
766
 
 
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;
 
771
 
 
772
                if (Text.size())
 
773
                {
 
774
                        if (!isEnabled() && !OverrideColorEnabled)
 
775
                        {
 
776
                                OverrideColorEnabled = true;
 
777
                                OverrideColor = skin->getColor(EGDC_GRAY_TEXT);
 
778
                        }
 
779
 
 
780
                        for (s32 i=0; i < lineCount; ++i)
 
781
                        {
 
782
                                setTextRect(i);
 
783
 
 
784
                                // clipping test - don't draw anything outside the visible area
 
785
                                core::rect<s32> c = localClipRect;
 
786
                                c.clipAgainst(CurrentTextRect);
 
787
                                if (!c.isValid())
 
788
                                        continue;
 
789
 
 
790
                                // get current line
 
791
                                if (PasswordBox)
 
792
                                {
 
793
                                        if (BrokenText.size() != 1)
 
794
                                        {
 
795
                                                BrokenText.clear();
 
796
                                                BrokenText.push_back(core::stringw());
 
797
                                        }
 
798
                                        if (BrokenText[0].size() != Text.size())
 
799
                                        {
 
800
                                                BrokenText[0] = Text;
 
801
                                                for (u32 q = 0; q < Text.size(); ++q)
 
802
                                                {
 
803
                                                        BrokenText[0] [q] = PasswordChar;
 
804
                                                }
 
805
                                        }
 
806
                                        txtLine = &BrokenText[0];
 
807
                                        startPos = 0;
 
808
                                }
 
809
                                else
 
810
                                {
 
811
                                        txtLine = ml ? &BrokenText[i] : &Text;
 
812
                                        startPos = ml ? BrokenTextPositions[i] : 0;
 
813
                                }
 
814
 
 
815
 
 
816
                                // draw normal text
 
817
                                font->draw(txtLine->c_str(), CurrentTextRect,
 
818
                                        OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
 
819
                                        false, true, &localClipRect);
 
820
 
 
821
                                // draw mark and marked text
 
822
                                if (focus && MarkBegin != MarkEnd && i >= hlineStart && i < hlineStart + hlineCount)
 
823
                                {
 
824
 
 
825
                                        s32 mbegin = 0, mend = 0;
 
826
                                        s32 lineStartPos = 0, lineEndPos = txtLine->size();
 
827
 
 
828
                                        if (i == hlineStart)
 
829
                                        {
 
830
                                                // highlight start is on this line
 
831
                                                s = txtLine->subString(0, realmbgn - startPos);
 
832
                                                mbegin = font->getDimension(s.c_str()).Width;
 
833
 
 
834
                                                // deal with kerning
 
835
                                                mbegin += font->getKerningWidth(
 
836
                                                        &((*txtLine)[realmbgn - startPos]),
 
837
                                                        realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
 
838
 
 
839
                                                lineStartPos = realmbgn - startPos;
 
840
                                        }
 
841
                                        if (i == hlineStart + hlineCount - 1)
 
842
                                        {
 
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();
 
847
                                        }
 
848
                                        else
 
849
                                                mend = font->getDimension(txtLine->c_str()).Width;
 
850
 
 
851
                                        CurrentTextRect.UpperLeftCorner.X += mbegin;
 
852
                                        CurrentTextRect.LowerRightCorner.X = CurrentTextRect.UpperLeftCorner.X + mend - mbegin;
 
853
 
 
854
                                        // draw mark
 
855
                                        skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), CurrentTextRect, &localClipRect);
 
856
 
 
857
                                        // draw marked text
 
858
                                        s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
 
859
 
 
860
                                        if (s.size())
 
861
                                                font->draw(s.c_str(), CurrentTextRect,
 
862
                                                        OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
 
863
                                                        false, true, &localClipRect);
 
864
 
 
865
                                }
 
866
                        }
 
867
 
 
868
                        // Return the override color information to its previous settings.
 
869
                        OverrideColorEnabled = prevOver;
 
870
                        OverrideColor = prevColor;
 
871
                }
 
872
 
 
873
                // draw cursor
 
874
 
 
875
                if (WordWrap || MultiLine)
 
876
                {
 
877
                        cursorLine = getLineFromPos(CursorPos);
 
878
                        txtLine = &BrokenText[cursorLine];
 
879
                        startPos = BrokenTextPositions[cursorLine];
 
880
                }
 
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);
 
884
 
 
885
                if (focus && (os::Timer::getTime() - BlinkStartTime) % 700 < 350)
 
886
                {
 
887
                        setTextRect(cursorLine);
 
888
                        CurrentTextRect.UpperLeftCorner.X += charcursorpos;
 
889
 
 
890
                        font->draw(L"_", CurrentTextRect,
 
891
                                OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_BUTTON_TEXT),
 
892
                                false, true, &localClipRect);
 
893
                }
 
894
        }
 
895
 
 
896
        // draw children
 
897
        IGUIElement::draw();
 
898
}
 
899
 
 
900
 
 
901
//! Sets the new caption of this element.
 
902
void CGUIEditBox::setText(const wchar_t* text)
 
903
{
 
904
        Text = text;
 
905
        if (u32(CursorPos) > Text.size())
 
906
                CursorPos = Text.size();
 
907
        HScrollPos = 0;
 
908
        breakText();
 
909
}
 
910
 
 
911
 
 
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)
 
915
{
 
916
        AutoScroll = enable;
 
917
}
 
918
 
 
919
 
 
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
 
923
{
 
924
        _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
 
925
        return AutoScroll;
 
926
}
 
927
 
 
928
 
 
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()
 
932
{
 
933
        core::rect<s32> ret;
 
934
 
 
935
        setTextRect(0);
 
936
        ret = CurrentTextRect;
 
937
 
 
938
        for (u32 i=1; i < BrokenText.size(); ++i)
 
939
        {
 
940
                setTextRect(i);
 
941
                ret.addInternalPoint(CurrentTextRect.UpperLeftCorner);
 
942
                ret.addInternalPoint(CurrentTextRect.LowerRightCorner);
 
943
        }
 
944
 
 
945
        return core::dimension2du(ret.getSize());
 
946
}
 
947
 
 
948
 
 
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
 
951
//! infinity.
 
952
void CGUIEditBox::setMax(u32 max)
 
953
{
 
954
        Max = max;
 
955
 
 
956
        if (Text.size() > Max && Max != 0)
 
957
                Text = Text.subString(0, Max);
 
958
}
 
959
 
 
960
 
 
961
//! Returns maximum amount of characters, previously set by setMax();
 
962
u32 CGUIEditBox::getMax() const
 
963
{
 
964
        return Max;
 
965
}
 
966
 
 
967
 
 
968
bool CGUIEditBox::processMouse(const SEvent& event)
 
969
{
 
970
        switch(event.MouseInput.Event)
 
971
        {
 
972
        case irr::EMIE_LMOUSE_LEFT_UP:
 
973
                if (Environment->hasFocus(this))
 
974
                {
 
975
                        CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
 
976
                        if (MouseMarking)
 
977
                        {
 
978
                            setTextMarkers( MarkBegin, CursorPos );
 
979
                        }
 
980
                        MouseMarking = false;
 
981
                        calculateScrollPos();
 
982
                        return true;
 
983
                }
 
984
                break;
 
985
        case irr::EMIE_MOUSE_MOVED:
 
986
                {
 
987
                        if (MouseMarking)
 
988
                        {
 
989
                                CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
 
990
                                setTextMarkers( MarkBegin, CursorPos );
 
991
                                calculateScrollPos();
 
992
                                return true;
 
993
                        }
 
994
                }
 
995
                break;
 
996
        case EMIE_LMOUSE_PRESSED_DOWN:
 
997
                if (!Environment->hasFocus(this))
 
998
                {
 
999
                        BlinkStartTime = os::Timer::getTime();
 
1000
                        MouseMarking = true;
 
1001
                        CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
 
1002
                        setTextMarkers(CursorPos, CursorPos );
 
1003
                        calculateScrollPos();
 
1004
                        return true;
 
1005
                }
 
1006
                else
 
1007
                {
 
1008
                        if (!AbsoluteClippingRect.isPointInside(
 
1009
                                core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
 
1010
                        {
 
1011
                                return false;
 
1012
                        }
 
1013
                        else
 
1014
                        {
 
1015
                                // move cursor
 
1016
                                CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
 
1017
 
 
1018
                s32 newMarkBegin = MarkBegin;
 
1019
                                if (!MouseMarking)
 
1020
                                        newMarkBegin = CursorPos;
 
1021
 
 
1022
                                MouseMarking = true;
 
1023
                                setTextMarkers( newMarkBegin, CursorPos);
 
1024
                                calculateScrollPos();
 
1025
                                return true;
 
1026
                        }
 
1027
                }
 
1028
        default:
 
1029
                break;
 
1030
        }
 
1031
 
 
1032
        return false;
 
1033
}
 
1034
 
 
1035
 
 
1036
s32 CGUIEditBox::getCursorPos(s32 x, s32 y)
 
1037
{
 
1038
        IGUIFont* font = getActiveFont();
 
1039
 
 
1040
        const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
 
1041
 
 
1042
        core::stringw *txtLine=0;
 
1043
        s32 startPos=0;
 
1044
        x+=3;
 
1045
 
 
1046
        for (u32 i=0; i < lineCount; ++i)
 
1047
        {
 
1048
                setTextRect(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;
 
1053
 
 
1054
                // is it inside this region?
 
1055
                if (y >= CurrentTextRect.UpperLeftCorner.Y && y <= CurrentTextRect.LowerRightCorner.Y)
 
1056
                {
 
1057
                        // we've found the clicked line
 
1058
                        txtLine = (WordWrap || MultiLine) ? &BrokenText[i] : &Text;
 
1059
                        startPos = (WordWrap || MultiLine) ? BrokenTextPositions[i] : 0;
 
1060
                        break;
 
1061
                }
 
1062
        }
 
1063
 
 
1064
        if (x < CurrentTextRect.UpperLeftCorner.X)
 
1065
                x = CurrentTextRect.UpperLeftCorner.X;
 
1066
 
 
1067
        if ( !txtLine )
 
1068
                return 0;
 
1069
 
 
1070
        s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - CurrentTextRect.UpperLeftCorner.X);
 
1071
 
 
1072
        // click was on or left of the line
 
1073
        if (idx != -1)
 
1074
                return idx + startPos;
 
1075
 
 
1076
        // click was off the right edge of the line, go to end.
 
1077
        return txtLine->size() + startPos;
 
1078
}
 
1079
 
 
1080
 
 
1081
//! Breaks the single text line.
 
1082
void CGUIEditBox::breakText()
 
1083
{
 
1084
        if ((!WordWrap && !MultiLine))
 
1085
                return;
 
1086
 
 
1087
        BrokenText.clear(); // need to reallocate :/
 
1088
        BrokenTextPositions.set_used(0);
 
1089
 
 
1090
        IGUIFont* font = getActiveFont();
 
1091
        if (!font)
 
1092
                return;
 
1093
 
 
1094
        LastBreakFont = font;
 
1095
 
 
1096
        core::stringw line;
 
1097
        core::stringw word;
 
1098
        core::stringw whitespace;
 
1099
        s32 lastLineStart = 0;
 
1100
        s32 size = Text.size();
 
1101
        s32 length = 0;
 
1102
        s32 elWidth = RelativeRect.getWidth() - 6;
 
1103
        wchar_t c;
 
1104
 
 
1105
        for (s32 i=0; i<size; ++i)
 
1106
        {
 
1107
                c = Text[i];
 
1108
                bool lineBreak = false;
 
1109
 
 
1110
                if (c == L'\r') // Mac or Windows breaks
 
1111
                {
 
1112
                        lineBreak = true;
 
1113
                        c = 0;
 
1114
                        if (Text[i+1] == L'\n') // Windows breaks
 
1115
                        {
 
1116
                                Text.erase(i+1);
 
1117
                                --size;
 
1118
                        }
 
1119
                }
 
1120
                else if (c == L'\n') // Unix breaks
 
1121
                {
 
1122
                        lineBreak = true;
 
1123
                        c = 0;
 
1124
                }
 
1125
 
 
1126
                // don't break if we're not a multi-line edit box
 
1127
                if (!MultiLine)
 
1128
                        lineBreak = false;
 
1129
 
 
1130
                if (c == L' ' || c == 0 || i == (size-1))
 
1131
                {
 
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;
 
1137
 
 
1138
                        if (WordWrap && length + worldlgth + whitelgth > elWidth)
 
1139
                        {
 
1140
                                // break to next line
 
1141
                                length = worldlgth;
 
1142
                                BrokenText.push_back(line);
 
1143
                                BrokenTextPositions.push_back(lastLineStart);
 
1144
                                lastLineStart = i - (s32)word.size();
 
1145
                                line = word;
 
1146
                        }
 
1147
                        else
 
1148
                        {
 
1149
                                // add word to line
 
1150
                                line += whitespace;
 
1151
                                line += word;
 
1152
                                length += whitelgth + worldlgth;
 
1153
                        }
 
1154
 
 
1155
                        word = L"";
 
1156
                        whitespace = L"";
 
1157
 
 
1158
 
 
1159
                        if ( c )
 
1160
                                whitespace += c;
 
1161
 
 
1162
                        // compute line break
 
1163
                        if (lineBreak)
 
1164
                        {
 
1165
                                line += whitespace;
 
1166
                                line += word;
 
1167
                                BrokenText.push_back(line);
 
1168
                                BrokenTextPositions.push_back(lastLineStart);
 
1169
                                lastLineStart = i+1;
 
1170
                                line = L"";
 
1171
                                word = L"";
 
1172
                                whitespace = L"";
 
1173
                                length = 0;
 
1174
                        }
 
1175
                }
 
1176
                else
 
1177
                {
 
1178
                        // yippee this is a word..
 
1179
                        word += c;
 
1180
                }
 
1181
        }
 
1182
 
 
1183
        line += whitespace;
 
1184
        line += word;
 
1185
        BrokenText.push_back(line);
 
1186
        BrokenTextPositions.push_back(lastLineStart);
 
1187
}
 
1188
 
 
1189
 
 
1190
void CGUIEditBox::setTextRect(s32 line)
 
1191
{
 
1192
        if ( line < 0 )
 
1193
                return;
 
1194
 
 
1195
        IGUIFont* font = getActiveFont();
 
1196
        if (!font)
 
1197
                return;
 
1198
 
 
1199
        core::dimension2du d;
 
1200
 
 
1201
        // get text dimension
 
1202
        const u32 lineCount = (WordWrap || MultiLine) ? BrokenText.size() : 1;
 
1203
        if (WordWrap || MultiLine)
 
1204
        {
 
1205
                d = font->getDimension(BrokenText[line].c_str());
 
1206
        }
 
1207
        else
 
1208
        {
 
1209
                d = font->getDimension(Text.c_str());
 
1210
                d.Height = AbsoluteRect.getHeight();
 
1211
        }
 
1212
        d.Height += font->getKerningHeight();
 
1213
 
 
1214
        // justification
 
1215
        switch (HAlign)
 
1216
        {
 
1217
        case EGUIA_CENTER:
 
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);
 
1221
                break;
 
1222
        case EGUIA_LOWERRIGHT:
 
1223
                // align to right edge
 
1224
                CurrentTextRect.UpperLeftCorner.X = FrameRect.getWidth() - d.Width;
 
1225
                CurrentTextRect.LowerRightCorner.X = FrameRect.getWidth();
 
1226
                break;
 
1227
        default:
 
1228
                // align to left edge
 
1229
                CurrentTextRect.UpperLeftCorner.X = 0;
 
1230
                CurrentTextRect.LowerRightCorner.X = d.Width;
 
1231
 
 
1232
        }
 
1233
 
 
1234
        switch (VAlign)
 
1235
        {
 
1236
        case EGUIA_CENTER:
 
1237
                // align to v centre
 
1238
                CurrentTextRect.UpperLeftCorner.Y =
 
1239
                        (FrameRect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
 
1240
                break;
 
1241
        case EGUIA_LOWERRIGHT:
 
1242
                // align to bottom edge
 
1243
                CurrentTextRect.UpperLeftCorner.Y =
 
1244
                        FrameRect.getHeight() - lineCount*d.Height + d.Height*line;
 
1245
                break;
 
1246
        default:
 
1247
                // align to top edge
 
1248
                CurrentTextRect.UpperLeftCorner.Y = d.Height*line;
 
1249
                break;
 
1250
        }
 
1251
 
 
1252
        CurrentTextRect.UpperLeftCorner.X  -= HScrollPos;
 
1253
        CurrentTextRect.LowerRightCorner.X -= HScrollPos;
 
1254
        CurrentTextRect.UpperLeftCorner.Y  -= VScrollPos;
 
1255
        CurrentTextRect.LowerRightCorner.Y = CurrentTextRect.UpperLeftCorner.Y + d.Height;
 
1256
 
 
1257
        CurrentTextRect += FrameRect.UpperLeftCorner;
 
1258
 
 
1259
}
 
1260
 
 
1261
 
 
1262
s32 CGUIEditBox::getLineFromPos(s32 pos)
 
1263
{
 
1264
        if (!WordWrap && !MultiLine)
 
1265
                return 0;
 
1266
 
 
1267
        s32 i=0;
 
1268
        while (i < (s32)BrokenTextPositions.size())
 
1269
        {
 
1270
                if (BrokenTextPositions[i] > pos)
 
1271
                        return i-1;
 
1272
                ++i;
 
1273
        }
 
1274
        return (s32)BrokenTextPositions.size() - 1;
 
1275
}
 
1276
 
 
1277
 
 
1278
void CGUIEditBox::inputChar(wchar_t c)
 
1279
{
 
1280
        if (!isEnabled())
 
1281
                return;
 
1282
 
 
1283
        if (c != 0)
 
1284
        {
 
1285
                if (Text.size() < Max || Max == 0)
 
1286
                {
 
1287
                        core::stringw s;
 
1288
 
 
1289
                        if (MarkBegin != MarkEnd)
 
1290
                        {
 
1291
                                // replace marked text
 
1292
                                const s32 realmbgn = MarkBegin < MarkEnd ? MarkBegin : MarkEnd;
 
1293
                                const s32 realmend = MarkBegin < MarkEnd ? MarkEnd : MarkBegin;
 
1294
 
 
1295
                                s = Text.subString(0, realmbgn);
 
1296
                                s.append(c);
 
1297
                                s.append( Text.subString(realmend, Text.size()-realmend) );
 
1298
                                Text = s;
 
1299
                                CursorPos = realmbgn+1;
 
1300
                        }
 
1301
                        else
 
1302
                        {
 
1303
                                // add new character
 
1304
                                s = Text.subString(0, CursorPos);
 
1305
                                s.append(c);
 
1306
                                s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
 
1307
                                Text = s;
 
1308
                                ++CursorPos;
 
1309
                        }
 
1310
 
 
1311
                        BlinkStartTime = os::Timer::getTime();
 
1312
                        setTextMarkers(0, 0);
 
1313
                }
 
1314
        }
 
1315
        breakText();
 
1316
        sendGuiEvent(EGET_EDITBOX_CHANGED);
 
1317
        calculateScrollPos();
 
1318
}
 
1319
 
 
1320
 
 
1321
void CGUIEditBox::calculateScrollPos()
 
1322
{
 
1323
        if (!AutoScroll)
 
1324
                return;
 
1325
 
 
1326
        // calculate horizontal scroll position
 
1327
        s32 cursLine = getLineFromPos(CursorPos);
 
1328
        if ( cursLine < 0 )
 
1329
                return;
 
1330
        setTextRect(cursLine);
 
1331
 
 
1332
        // don't do horizontal scrolling when wordwrap is enabled.
 
1333
        if (!WordWrap)
 
1334
        {
 
1335
                // get cursor position
 
1336
                IGUIFont* font = getActiveFont();
 
1337
                if (!font)
 
1338
                        return;
 
1339
 
 
1340
                core::stringw *txtLine = MultiLine ? &BrokenText[cursLine] : &Text;
 
1341
                s32 cPos = MultiLine ? CursorPos - BrokenTextPositions[cursLine] : CursorPos;
 
1342
 
 
1343
                s32 cStart = CurrentTextRect.UpperLeftCorner.X + HScrollPos +
 
1344
                        font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
 
1345
 
 
1346
                s32 cEnd = cStart + font->getDimension(L"_ ").Width;
 
1347
 
 
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;
 
1352
                else
 
1353
                        HScrollPos = 0;
 
1354
 
 
1355
                // todo: adjust scrollbar
 
1356
        }
 
1357
 
 
1358
        // vertical scroll position
 
1359
        if (FrameRect.LowerRightCorner.Y < CurrentTextRect.LowerRightCorner.Y + VScrollPos)
 
1360
                VScrollPos = CurrentTextRect.LowerRightCorner.Y - FrameRect.LowerRightCorner.Y + VScrollPos;
 
1361
 
 
1362
        else if (FrameRect.UpperLeftCorner.Y > CurrentTextRect.UpperLeftCorner.Y + VScrollPos)
 
1363
                VScrollPos = CurrentTextRect.UpperLeftCorner.Y - FrameRect.UpperLeftCorner.Y + VScrollPos;
 
1364
        else
 
1365
                VScrollPos = 0;
 
1366
 
 
1367
        // todo: adjust scrollbar
 
1368
}
 
1369
 
 
1370
void CGUIEditBox::calculateFrameRect()
 
1371
{
 
1372
        FrameRect = AbsoluteRect;
 
1373
        IGUISkin *skin = 0;
 
1374
        if (Environment)
 
1375
                skin = Environment->getSkin();
 
1376
        if (Border && skin)
 
1377
        {
 
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;
 
1382
        }
 
1383
}
 
1384
 
 
1385
//! set text markers
 
1386
void CGUIEditBox::setTextMarkers(s32 begin, s32 end)
 
1387
{
 
1388
    if ( begin != MarkBegin || end != MarkEnd )
 
1389
    {
 
1390
        MarkBegin = begin;
 
1391
        MarkEnd = end;
 
1392
        sendGuiEvent(EGET_EDITBOX_MARKING_CHANGED);
 
1393
    }
 
1394
}
 
1395
 
 
1396
//! send some gui event to parent
 
1397
void CGUIEditBox::sendGuiEvent(EGUI_EVENT_TYPE type)
 
1398
{
 
1399
        if ( Parent )
 
1400
        {
 
1401
        SEvent e;
 
1402
        e.EventType = EET_GUI_EVENT;
 
1403
        e.GUIEvent.Caller = this;
 
1404
        e.GUIEvent.Element = 0;
 
1405
        e.GUIEvent.EventType = type;
 
1406
 
 
1407
        Parent->OnEvent(e);
 
1408
        }
 
1409
}
 
1410
 
 
1411
//! Writes attributes of the element.
 
1412
void CGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
 
1413
{
 
1414
        // IGUIEditBox::serializeAttributes(out,options);
 
1415
 
 
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);
 
1431
 
 
1432
        IGUIEditBox::serializeAttributes(out,options);
 
1433
}
 
1434
 
 
1435
 
 
1436
//! Reads attributes of the element
 
1437
void CGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
 
1438
{
 
1439
        IGUIEditBox::deserializeAttributes(in,options);
 
1440
 
 
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");
 
1450
 
 
1451
        if (!ch.size())
 
1452
                setPasswordBox(in->getAttributeAsBool("PasswordBox"));
 
1453
        else
 
1454
                setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
 
1455
 
 
1456
        setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
 
1457
                        (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
 
1458
 
 
1459
        // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
 
1460
}
 
1461
 
 
1462
 
 
1463
} // end namespace gui
 
1464
} // end namespace irr
 
1465
 
 
1466
#endif // _IRR_COMPILE_WITH_GUI_
 
1467