summaryrefslogtreecommitdiffhomepage
path: root/Stars45/EditBox.cpp
blob: 7102302daf79f13a94fe529d75a2791b6d24cc0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*  Starshatter OpenSource Distribution
    Copyright (c) 1997-2004, Destroyer Studios LLC.
    All Rights Reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name "Destroyer Studios" nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    SUBSYSTEM:    nGenEx.lib
    FILE:         EditBox.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    EditBox ActiveWindow class
*/

#include "MemDebug.h"
#include "EditBox.h"
#include "FormWindow.h"
#include "Video.h"
#include "Font.h"
#include "Keyboard.h"
#include "Mouse.h"

DWORD GetRealTime();

// +--------------------------------------------------------------------+

static int old_cursor;

// +--------------------------------------------------------------------+

EditBox::EditBox(ActiveWindow* p, int ax, int ay, int aw, int ah, DWORD aid)
    : ScrollWindow(p->GetScreen(), ax, ay, aw, ah, aid, 0, p), caret_x(0), caret_y(0)
{
    sel_start         = 0;
    sel_length        = 0;
    h_offset          = 0;
    pass_char         = 0;

    selected_color    = Color::Yellow;

    char buf[32];
    sprintf_s(buf, "EditBox %d", id);
    desc = buf;
}

EditBox::EditBox(Screen* s, int ax, int ay, int aw, int ah, DWORD aid)
    : ScrollWindow(s, ax, ay, aw, ah, aid), caret_x(0), caret_y(0)
{
    sel_start         = 0;
    sel_length        = 0;
    h_offset          = 0;
    pass_char         = 0;

    selected_color    = Color::Yellow;

    char buf[32];
    sprintf_s(buf, "EditBox %d", id);
    desc = buf;
}

EditBox::~EditBox()
{
}

// +--------------------------------------------------------------------+

void
EditBox::DrawContent(const Rect& ctrl_rect)
{
    int h = rect.h;

    if (line_height < 1)
    line_height = GetFont()->Height();
    page_size = h / (line_height + leading);
}

// +--------------------------------------------------------------------+

void
EditBox::DrawTabbedText()
{
    if (font && text.length()) {
        int border_size = 4;

        if (style & WIN_RAISED_FRAME && style & WIN_SUNK_FRAME)
        border_size = 8;

        Rect label_rect = rect;

        label_rect.x  = border_size;
        label_rect.y  = border_size;
        label_rect.w -= border_size * 2;
        label_rect.h -= border_size * 2;

        if (focus)
        font->SetCaretIndex(sel_start);

        // if displaying in password mode, create a display string
        // containing the proper number of password chars:
        Text s = text;
        if (pass_char)
        s = Text(pass_char, text.length());

        // no tabs set:
        if (tab[0] == 0) {
            DWORD text_flags = DT_WORDBREAK | text_align;

            if (single_line)
            text_flags = text_flags | DT_SINGLELINE;

            if (style & WIN_TEXT_SHADOW) {
                label_rect.x++;
                label_rect.y++;

                font->SetColor(back_color);
                DrawText(s.data() + h_offset, 0, label_rect, text_flags);

                label_rect.x--;
                label_rect.y--;
            }

            font->SetColor(fore_color);
            DrawText(s.data() + h_offset, 0, label_rect, text_flags);
        }

        // use tabs:
        else {
        }

        font->SetCaretIndex(-1);
    }
    else {
        caret_x = 2;
        caret_y = 3;
    }
}

// +--------------------------------------------------------------------+

Color EditBox::GetSelectedColor()
{
    return selected_color;
}

void  EditBox::SetSelectedColor(Color c)
{
    if (selected_color != c) {
        selected_color = c;
    }
}

Text  EditBox::GetSelText()
{
    if (sel_start == 0 && sel_length == text.length())
    return text;

    Text selection;
    char* buf = new(__FILE__,__LINE__) char[sel_length+1];

    if (buf) {
        for (int i = 0; i < sel_length; i++)
        buf[i] = text[(int) (sel_start + i)];

        buf[sel_length] = 0;

        selection = buf;
        delete [] buf;
    }

    return selection;
}

void  EditBox::SetSelStart(int n)
{
    if (n >= 0 && n <= text.length())
    sel_start = n;
}

void  EditBox::SetSelLength(int n)
{
    if (n <= text.length() - sel_start)
    sel_length = n;
}

void  EditBox::EnsureCaretVisible()
{
    if (!single_line) {
        h_offset = 0;
        return;
    }

    if (sel_start < 0) {
        sel_start = 0;
        h_offset  = 0;
    }

    else if (sel_start > h_offset) {
        int   x_caret;
        bool  moved;

        do {
            x_caret = 0;
            moved   = false;

            if (pass_char) {
                Text pass = Text(pass_char, sel_start-h_offset);
                x_caret += font->StringWidth(pass.data(), pass.length());
            }
            else {
                Text sub = text.substring(h_offset, sel_start-h_offset);
                x_caret += font->StringWidth(sub.data(), sub.length());
            }

            if (x_caret >= Width()-4) {
                if (h_offset < text.length()) {
                    h_offset++;
                    moved = true;
                }
            }
        }
        while (moved);
    }

    else {
        h_offset = sel_start;
    }
}

bool  EditBox::CanScroll(int dir, int nlines)
{
    return false;
}

void  EditBox::Scroll(int direction, int nlines)
{
    scrolling = SCROLL_NONE;
}

void  EditBox::ScrollTo(int index)
{
}

int   EditBox::GetPageCount()
{
    return 1;
}

int   EditBox::GetPageSize()
{
    return page_size;
}

// +--------------------------------------------------------------------+

void EditBox::SetFocus()
{
    ActiveWindow::SetFocus();

    sel_start  = text.length();
    sel_length = 0;
}

void EditBox::KillFocus()
{
    ActiveWindow::KillFocus();
}

// +--------------------------------------------------------------------+

int EditBox::CaretFromPoint(int x, int y) const
{
    return 0;
}

// +--------------------------------------------------------------------+

int EditBox::OnMouseMove(int x, int y)
{
    return ActiveWindow::OnMouseMove(x,y);
}

// +--------------------------------------------------------------------+

int EditBox::OnLButtonDown(int x, int y)
{
    return ActiveWindow::OnLButtonDown(x,y);
}

// +--------------------------------------------------------------------+

int EditBox::OnLButtonUp(int x, int y)
{
    return ActiveWindow::OnLButtonUp(x,y);
}

// +--------------------------------------------------------------------+

int EditBox::OnClick()
{
    int fire_click = !scrolling;

    if (scrolling == SCROLL_THUMB)
    scrolling = SCROLL_NONE;

    if (fire_click)
    return ActiveWindow::OnClick();

    return 0;
}

// +--------------------------------------------------------------------+

void EditBox::Insert(char c)
{
    if (single_line && c == '\n')
    return;

    if (sel_start < text.length()) {
        if (sel_start == 0) {
            text = Text(c) + text;
            sel_start = 1;
        }
        else {
            Text t1 = text.substring(0, sel_start);
            Text t2 = text.substring(sel_start, text.length()-sel_start);
            text = t1 + Text(c) + t2;
            sel_start++;
        }
    }
    else {
        text += c;
        sel_start = text.length();
    }

    EnsureCaretVisible();
}

void EditBox::Insert(const char* s)
{
}

void EditBox::Delete()
{
    if (sel_start < text.length()) {
        if (sel_start == 0) {
            text = text.substring(1, text.length()-1);
        }
        else {
            Text t1 = text.substring(0, sel_start);
            Text t2 = text.substring(sel_start+1, text.length()-sel_start-1);
            text = t1 + t2;
        }
    }

    EnsureCaretVisible();
}

void EditBox::Backspace()
{
    if (sel_start > 0) {
        if (sel_start == text.length()) {
            text = text.substring(0, sel_start-1);
        }
        else {
            Text t1 = text.substring(0, sel_start-1);
            Text t2 = text.substring(sel_start, text.length()-sel_start);
            text = t1 + t2;
        }

        sel_start--;
        EnsureCaretVisible();
    }
}

int EditBox::OnKeyDown(int vk, int flags)
{
    if (vk >= 'A' && vk <= 'Z') {
        if (flags & 1)
        Insert((char) vk);
        else
        Insert((char) tolower(vk));
    }
    else {
        switch (vk) {
        case VK_LEFT:
            if (sel_start > 0) sel_start--;
            EnsureCaretVisible();
            break;

        case VK_RIGHT:
            if (sel_start < text.length()) sel_start++;
            EnsureCaretVisible();
            break;

        case VK_HOME:
            sel_start = 0;
            EnsureCaretVisible();
            break;

        case VK_END:
            sel_start = text.length();
            EnsureCaretVisible();
            break;

        case VK_DELETE:   Delete();      break;
        case VK_BACK:     Backspace();   break;

        case VK_SPACE:    Insert(' ');   break;
        case VK_RETURN:   Insert('\n');  break;

        case VK_NUMPAD0:  Insert('0');   break;
        case VK_NUMPAD1:  Insert('1');   break;
        case VK_NUMPAD2:  Insert('2');   break;
        case VK_NUMPAD3:  Insert('3');   break;
        case VK_NUMPAD4:  Insert('4');   break;
        case VK_NUMPAD5:  Insert('5');   break;
        case VK_NUMPAD6:  Insert('6');   break;
        case VK_NUMPAD7:  Insert('7');   break;
        case VK_NUMPAD8:  Insert('8');   break;
        case VK_NUMPAD9:  Insert('9');   break;
        case VK_DECIMAL:  Insert('.');   break;
        case VK_ADD:      Insert('+');   break;
        case VK_SUBTRACT: Insert('-');   break;
        case VK_MULTIPLY: Insert('*');   break;
        case VK_DIVIDE:   Insert('/');   break;

        case '0': if (flags & 1) Insert(')'); else Insert('0'); break;
        case '1': if (flags & 1) Insert('!'); else Insert('1'); break;
        case '2': if (flags & 1) Insert('@'); else Insert('2'); break;
        case '3': if (flags & 1) Insert('#'); else Insert('3'); break;
        case '4': if (flags & 1) Insert('$'); else Insert('4'); break;
        case '5': if (flags & 1) Insert('%'); else Insert('5'); break;
        case '6': if (flags & 1) Insert('^'); else Insert('6'); break;
        case '7': if (flags & 1) Insert('&'); else Insert('7'); break;
        case '8': if (flags & 1) Insert('*'); else Insert('8'); break;
        case '9': if (flags & 1) Insert('('); else Insert('9'); break;
        case 186: if (flags & 1) Insert(':'); else Insert(';'); break;
        case 187: if (flags & 1) Insert('+'); else Insert('='); break;
        case 188: if (flags & 1) Insert('<'); else Insert(','); break;
        case 189: if (flags & 1) Insert('_'); else Insert('-'); break;
        case 190: if (flags & 1) Insert('>'); else Insert('.'); break;
        case 191: if (flags & 1) Insert('?'); else Insert('/'); break;
        case 192: if (flags & 1) Insert('~'); else Insert('`'); break;
        case 219: if (flags & 1) Insert('{'); else Insert('['); break;
        case 221: if (flags & 1) Insert('}'); else Insert(']'); break;
        case 220: if (flags & 1) Insert('|'); else Insert('\\'); break;
        case 222: if (flags & 1) Insert('"'); else Insert('\''); break;
        }
    }

    return ActiveWindow::OnKeyDown(vk, flags);
}