OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [mwin/] [winlib/] [edit.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
 * Copyright (C) 1999, 2000, Wei Yongming.
3
 * Portions Copyright (c) 2000 Greg Haerr <greg@censoft.com>
4
 *
5
 * Edit control for Microwindows win32 api.
6
 */
7
 
8
/*
9
**  This library is free software; you can redistribute it and/or
10
**  modify it under the terms of the GNU Library General Public
11
**  License as published by the Free Software Foundation; either
12
**  version 2 of the License, or (at your option) any later version.
13
**
14
**  This library is distributed in the hope that it will be useful,
15
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
**  Library General Public License for more details.
18
**
19
**  You should have received a copy of the GNU Library General Public
20
**  License along with this library; if not, write to the Free
21
**  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22
**  MA 02111-1307, USA
23
*/
24
 
25
/*
26
**  Alternatively, the contents of this file may be used under the terms
27
**  of the Mozilla Public License (the "MPL License") in which case the
28
**  provisions of the MPL License are applicable instead of those above.
29
*/
30
 
31
/* Note:
32
**  Although there was a version by Zhao Jianghua, this version of
33
**  EDIT control is written by Wei Yongming from scratch.
34
**
35
** Create date: 1999/8/26
36
**
37
** Modify records:
38
**
39
**  Who             When        Where       For What                Status
40
**-----------------------------------------------------------------------------
41
**  WEI Yongming    2000/02/24  Tsinghua    Add MPL License         Finished
42
**  Kevin Tseng     2000/05/30  gv          port to microwin        ported
43
**  Greg Haerr      2000/06/16  Utah        3d look, bug fixes      Finished
44
**  Kevin Tseng     2000/06/22  gv          port to mw-nanox        ported
45
**
46
** TODO:
47
**    * Selection.
48
**    * Undo.
49
*/
50
 
51
#include <stdio.h>
52
#include <stdlib.h>
53
#include <string.h>
54
#define MWINCLUDECOLORS
55
#include "windows.h"    /* windef.h, winuser.h */
56
#include "wintools.h"
57
#include "device.h"     /* GdGetTextSize */
58
 
59
#define USE_BIG5
60
 
61
#if 0
62
#define DEFAULT_FONT    DEFAULT_GUI_FONT
63
#endif
64
#define DEFAULT_FONT    SYSTEM_FIXED_FONT
65
 
66
#define WIDTH_EDIT_BORDER       2
67
#define MARGIN_EDIT_LEFT        1
68
#define MARGIN_EDIT_TOP         1
69
#define MARGIN_EDIT_RIGHT       2
70
#define MARGIN_EDIT_BOTTOM      1
71
 
72
#define LEN_SLEDIT_BUFFER       3000
73
#define LEN_SLEDIT_UNDOBUFFER   1024
74
 
75
#define EST_FOCUSED     0x00000001L
76
#define EST_MODIFY      0x00000002L
77
#define EST_READONLY    0x00000004L
78
#define EST_REPLACE     0x00000008L
79
 
80
#define EDIT_OP_NONE    0x00
81
#define EDIT_OP_DELETE  0x01
82
#define EDIT_OP_INSERT  0x02
83
#define EDIT_OP_REPLACE 0x03
84
 
85
typedef struct tagSLEDITDATA {
86
    HFONT   hFont;          /* hFont used */
87
    int     bufferLen;      /* length of buffer */
88
 
89
    int     dataEnd;        /* data end position */
90
    int     editPos;        /* current edit position */
91
    int     caretOff;       /* caret offset in box */
92
    int     startPos;       /* start display position */
93
 
94
    int     selStart;       /* selection start position */
95
    int     selEnd;         /* selection end position */
96
 
97
    int     passwdChar;     /* password character */
98
 
99
    int     leftMargin;     /* left margin */
100
    int     topMargin;      /* top margin */
101
    int     rightMargin;    /* right margin */
102
    int     bottomMargin;   /* bottom margin */
103
 
104
    int     hardLimit;      /* hard limit */
105
 
106
    int     lastOp;         /* last operation */
107
    int     lastPos;        /* last operation position */
108
    int     affectedLen;    /* affected len of last operation */
109
    int     undoBufferLen;  /* undo buffer len */
110
    char    undoBuffer [LEN_SLEDIT_UNDOBUFFER]; /* Undo buffer; */
111
    char    buffer [LEN_SLEDIT_BUFFER];         /* buffer */
112
} SLEDITDATA, *PSLEDITDATA;
113
 
114
static LRESULT CALLBACK
115
SLEditCtrlProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
116
 
117
static int GetSysCharHeight (HWND hwnd)
118
{
119
#ifndef USE_BIG5            
120
        HDC             hdc;
121
        int xw, xh, xb;
122
 
123
        hdc = GetDC(hwnd);
124
        SelectObject(hdc, GetStockObject(DEFAULT_FONT));
125
        GdSetFont(hdc->font->pfont);
126
        GdGetTextSize(hdc->font->pfont,"X",1, &xw,&xh,&xb,MWTF_ASCII);
127
        ReleaseDC(hwnd,hdc);
128
 
129
        return xh;
130
#else
131
        return 12;
132
#endif
133
}
134
 
135
static int GetSysCharWidth (HWND hwnd)
136
{
137
#ifndef USE_BIG5            
138
        HDC             hdc;
139
        int xw, xh, xb;
140
 
141
        hdc = GetDC(hwnd);
142
        SelectObject(hdc, GetStockObject(DEFAULT_FONT));
143
        GdSetFont(hdc->font->pfont);
144
        GdGetTextSize(hdc->font->pfont,"X",1, &xw,&xh,&xb,MWTF_ASCII);
145
        ReleaseDC(hwnd,hdc);
146
 
147
        return xw;
148
#else
149
        return 6;
150
#endif
151
}
152
 
153
static int GetSysCCharWidth (HWND hwnd)
154
{
155
        return (2*GetSysCharWidth(hwnd));
156
}
157
 
158
int WINAPI MwRegisterEditControl(HINSTANCE hInstance)
159
{
160
        WNDCLASS        wc;
161
 
162
        wc.style        = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
163
        wc.lpfnWndProc  = (WNDPROC)SLEditCtrlProc;
164
        wc.cbClsExtra   = 0;
165
        wc.cbWndExtra   = 0;
166
        wc.hInstance    = hInstance;
167
        wc.hIcon        = NULL;
168
        wc.hCursor      = 0; /*LoadCursor(NULL, IDC_ARROW);*/
169
        wc.hbrBackground= GetStockObject(WHITE_BRUSH);
170
        wc.lpszMenuName = NULL;
171
        wc.lpszClassName= "EDIT";
172
 
173
        return RegisterClass(&wc);
174
}
175
 
176
static int edtGetOutWidth (const HWND pCtrl)
177
{
178
    return pCtrl->clirect.right - pCtrl->clirect.left
179
            - ((PSLEDITDATA)(pCtrl->userdata2))->leftMargin
180
            - ((PSLEDITDATA)(pCtrl->userdata2))->rightMargin;
181
}
182
 
183
static int edtGetStartDispPosAtEnd (const HWND pCtrl, PSLEDITDATA pSLEditData)
184
{
185
    int         nOutWidth = edtGetOutWidth (pCtrl);
186
    int         endPos  = pSLEditData->dataEnd;
187
    int         newStartPos = pSLEditData->startPos;
188
    const char* buffer = pSLEditData->buffer;
189
 
190
    while (TRUE) {
191
        if ((endPos - newStartPos) * GetSysCharWidth (pCtrl) < nOutWidth)
192
            break;
193
 
194
        /* FIXME: #ifdef GB2312?*/
195
        if ((BYTE)buffer [newStartPos] > 0xA0)  /* 1st:gb:a1-f7,big5:a1-f9 */
196
        {
197
            newStartPos ++;
198
            if (newStartPos < pSLEditData->dataEnd)
199
            {
200
#ifndef USE_BIG5
201
                if ((BYTE)buffer [newStartPos] > 0xA0)
202
#else   /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
203
                if ( ((BYTE)buffer [newStartPos] >= 0x40 && (BYTE)buffer[newStartPos] <= 0x7e) ||
204
                     ((BYTE)buffer [newStartPos] >= 0xa1 && (BYTE)buffer[newStartPos] <= 0xfe))
205
#endif
206
                    newStartPos ++;
207
            }
208
        }
209
        else
210
            newStartPos ++;
211
    }
212
 
213
    return newStartPos;
214
}
215
 
216
static int edtGetDispLen (const HWND pCtrl)
217
{
218
    int i, n = 0;
219
    int nOutWidth = edtGetOutWidth (pCtrl);
220
    int nTextWidth = 0;
221
    PSLEDITDATA pSLEditData = (PSLEDITDATA)(pCtrl->userdata2);
222
    const char* buffer = pSLEditData->buffer;
223
 
224
    for (i = pSLEditData->startPos; i < pSLEditData->dataEnd; i++) {
225
        /* FIXME #ifdef GB2312?*/
226
        if ((BYTE)buffer [i] > 0xA0)    /* st:gb:a1-f7,big5:a1-f9 */
227
        {
228
            i++;
229
            if (i < pSLEditData->dataEnd)
230
            {
231
#ifndef USE_BIG5
232
                if ((BYTE)buffer [i] > 0xA0)    /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
233
#else   /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
234
                if ( ((BYTE)buffer [i] >= 0x40 && (BYTE)buffer[i] <= 0x7e) ||
235
                     ((BYTE)buffer [i] >= 0xa1 && (BYTE)buffer[i] <= 0xfe))
236
#endif
237
                {
238
                    nTextWidth += GetSysCCharWidth (pCtrl);
239
                    n += 2;
240
                }
241
                else
242
                    i--;
243
            }
244
            else
245
            {
246
                nTextWidth += GetSysCharWidth (pCtrl);
247
                n++;
248
            }
249
        }
250
        else
251
        {
252
            nTextWidth += GetSysCharWidth (pCtrl);
253
            n++;
254
        }
255
 
256
        if (nTextWidth > nOutWidth)
257
            break;
258
    }
259
 
260
    return n;
261
}
262
 
263
static int edtGetOffset (HWND hwnd,const SLEDITDATA* pSLEditData, int x)
264
{
265
    int i;
266
    int newOff = 0;
267
    int nTextWidth = 0;
268
    const char* buffer = pSLEditData->buffer;
269
 
270
    x -= pSLEditData->leftMargin;
271
    for (i = pSLEditData->startPos; i < pSLEditData->dataEnd; i++) {
272
        if ((nTextWidth + (GetSysCharWidth(hwnd) >> 1)) >= x)
273
            break;
274
 
275
        /* FIXME #ifdef GB2312?*/
276
        if ((BYTE)buffer [i] > 0xA0)    /* 1st:gb:a1-f7,big5:a1-f9 */
277
        {
278
            i++;
279
            if (i < pSLEditData->dataEnd)
280
            {
281
#ifndef USE_BIG5
282
                if ((BYTE)buffer [i] > 0xA0)    /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
283
#else   /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
284
                if ( ((BYTE)buffer [i] >= 0x40 && (BYTE)buffer[i] <= 0x7e) ||
285
                     ((BYTE)buffer [i] >= 0xa1 && (BYTE)buffer[i] <= 0xfe))
286
#endif
287
                {
288
                    nTextWidth += GetSysCCharWidth (hwnd);
289
                    newOff += 2;
290
                }
291
                else
292
                    i --;
293
            }
294
            else
295
            {
296
                nTextWidth += GetSysCharWidth (hwnd);
297
                newOff ++;
298
            }
299
        }
300
        else
301
        {
302
            nTextWidth += GetSysCharWidth (hwnd);
303
            newOff ++;
304
        }
305
 
306
    }
307
 
308
    return newOff;
309
}
310
 
311
static BOOL edtIsACCharBeforePosition (const char* string, int pos)
312
{
313
    if (pos < 2)
314
        return FALSE;
315
 
316
/* 1st:gb:a1-f7,big5:a1-f9  2nd:gb:a1-fe,big5:40-7e,a1-fe */
317
#ifndef USE_BIG5
318
    /* FIXME #ifdef GB2312?*/
319
    if ((BYTE)string [pos - 2] > 0xA0 && (BYTE)string [pos - 1] > 0xA0)
320
        return TRUE;
321
#else
322
    if ((BYTE)string [pos - 2] > 0xA0)
323
    {
324
        if ( ((BYTE)string [pos - 1] >= 0x40 && (BYTE)string[pos - 1] <= 0x7e) ||
325
             ((BYTE)string [pos - 1] >= 0xa1 && (BYTE)string[pos - 1] <= 0xfe))
326
            return TRUE;
327
    }
328
#endif
329
 
330
    return FALSE;
331
}
332
 
333
static BOOL edtIsACCharAtPosition (const char* string, int len, int pos)
334
{
335
    if (pos > (len - 2))
336
        return FALSE;
337
 
338
/* 1st:gb:a1-f7,big5:a1-f9  2nd:gb:a1-fe,big5:40-7e,a1-fe */
339
#ifndef USE_BIG5
340
    if ((BYTE)string [pos] > 0xA0 && (BYTE)string [pos + 1] > 0xA0)
341
        return TRUE;
342
#else
343
    if ((BYTE)string [pos] > 0xA0)
344
    {
345
        if ( ((BYTE)string [pos + 1] >= 0x40 && (BYTE)string [pos + 1] <= 0x7e) ||
346
             ((BYTE)string [pos + 1] >= 0xa1 && (BYTE)string [pos + 1] <= 0xfe)) {
347
            /*fprintf(stderr,"true\n");
348
            fflush(stderr);*/
349
            return TRUE;
350
        }
351
    }
352
#endif
353
 
354
    return FALSE;
355
}
356
 
357
LRESULT CALLBACK
358
SLEditCtrlProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
359
{
360
    HWND        pCtrl;
361
    DWORD       dwStyle;
362
    HDC         hdc;
363
    PSLEDITDATA pSLEditData;
364
    RECT        rc;
365
 
366
    pCtrl       = hWnd;
367
    dwStyle     = pCtrl->style;
368
 
369
    switch (message)
370
    {
371
        case WM_CREATE:
372
            if (!(pSLEditData = malloc (sizeof (SLEDITDATA)))) {
373
                fprintf (stderr, "EDIT: malloc error!\n");
374
                return -1;
375
            }
376
 
377
            pSLEditData->hFont          = GetStockObject(DEFAULT_FONT);
378
 
379
            pSLEditData->bufferLen      = LEN_SLEDIT_BUFFER;
380
            pSLEditData->editPos        = 0;
381
            pSLEditData->caretOff       = 0;
382
            pSLEditData->startPos       = 0;
383
 
384
            pSLEditData->selStart       = 0;
385
            pSLEditData->selEnd         = 0;
386
            pSLEditData->passwdChar     = '*';
387
            pSLEditData->leftMargin     = MARGIN_EDIT_LEFT;
388
            pSLEditData->topMargin      = MARGIN_EDIT_TOP;
389
            pSLEditData->rightMargin    = MARGIN_EDIT_RIGHT;
390
            pSLEditData->bottomMargin   = MARGIN_EDIT_BOTTOM;
391
 
392
            pSLEditData->hardLimit      = -1;
393
 
394
            /* undo information */
395
            pSLEditData->lastOp         = EDIT_OP_NONE;
396
            pSLEditData->lastPos        = 0;
397
            pSLEditData->affectedLen    = 0;
398
            pSLEditData->undoBufferLen  = LEN_SLEDIT_UNDOBUFFER;
399
            pSLEditData->undoBuffer [0] = '\0';
400
 
401
            pSLEditData->dataEnd        = strlen (pCtrl->szTitle);
402
            memcpy (pSLEditData->buffer, pCtrl->szTitle,
403
                    min (LEN_SLEDIT_BUFFER, pSLEditData->dataEnd));
404
 
405
            pCtrl->userdata2 = (DWORD) pSLEditData;
406
 
407
            pCtrl->userdata  = 0;
408
        break;
409
 
410
        case WM_DESTROY:
411
            DestroyCaret ();
412
 
413
            free ((void*)pCtrl->userdata2);
414
        break;
415
#if 0        
416
        case WM_CHANGESIZE:
417
        {
418
            pCtrl->cl = pCtrl->left   + WIDTH_EDIT_BORDER;
419
            pCtrl->ct = pCtrl->top    + WIDTH_EDIT_BORDER;
420
            pCtrl->cr = pCtrl->right  - WIDTH_EDIT_BORDER;
421
            pCtrl->cb = pCtrl->bottom - WIDTH_EDIT_BORDER;
422
        }
423
        break;
424
#endif
425
 
426
#if 1   /* jmt: for edit: chinese support */
427
        case WM_SETFONT:
428
        {
429
                pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
430
                pSLEditData->hFont = (HFONT)wParam;
431
 
432
                ShowWindow(hWnd, SW_HIDE);
433
                ShowWindow(hWnd, SW_SHOWNA);
434
 
435
                if(LOWORD(lParam))
436
                        InvalidateRect(hWnd,NULL,TRUE);
437
        }
438
        return (LRESULT)0;
439
 
440
        case WM_GETFONT:
441
                pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
442
                return (LRESULT)pSLEditData->hFont;
443
#endif    
444
 
445
#if 0    
446
        case WM_SETCURSOR:
447
            if (dwStyle & WS_DISABLED)
448
            {
449
                SetCursor (GetSystemCursor (IDC_ARROW));
450
                return 0;
451
            }
452
        break;
453
#endif
454
        case WM_KILLFOCUS:
455
            pCtrl->userdata &= ~EST_FOCUSED;
456
 
457
            HideCaret (hWnd);
458
            DestroyCaret ();
459
 
460
            SendMessage (GetParent (hWnd), WM_COMMAND,
461
                 (WPARAM) MAKELONG (pCtrl->id, EN_KILLFOCUS), (LPARAM)hWnd);
462
        break;
463
 
464
        case WM_SETFOCUS:
465
            if (pCtrl->userdata & EST_FOCUSED)
466
                return 0;
467
 
468
            pCtrl->userdata |= EST_FOCUSED;
469
 
470
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
471
            /* only implemented for ES_LEFT align format. */
472
 
473
            CreateCaret (hWnd, NULL, 1 /*+ GetSysCharWidth(hWnd)*/,
474
                    hWnd->clirect.bottom-hWnd->clirect.top-2);
475
            SetCaretPos (pSLEditData->caretOff * GetSysCharWidth (hWnd)
476
                    + pSLEditData->leftMargin, pSLEditData->topMargin);
477
            ShowCaret (hWnd);
478
 
479
            SendMessage (GetParent (hWnd), WM_COMMAND,
480
               (WPARAM) MAKELONG (pCtrl->id, EN_SETFOCUS), (LPARAM) hWnd);
481
        break;
482
 
483
        case WM_ENABLE:
484
            if ( (!(dwStyle & WS_DISABLED) && !wParam)
485
                    || ((dwStyle & WS_DISABLED) && wParam) ) {
486
                if (wParam)
487
                    pCtrl->style &= ~WS_DISABLED;
488
                else
489
                    pCtrl->style |=  WS_DISABLED;
490
 
491
                InvalidateRect (hWnd, NULL, FALSE);
492
            }
493
        break;
494
 
495
        case WM_NCCALCSIZE:
496
        {
497
                LPNCCALCSIZE_PARAMS lpnc;
498
 
499
                /* calculate client rect from passed window rect in rgrc[0]*/
500
                lpnc = (LPNCCALCSIZE_PARAMS)lParam;
501
                if(GetWindowLong(hWnd, GWL_STYLE) & WS_BORDER)
502
                        InflateRect(&lpnc->rgrc[0], -2, -2);
503
        }
504
                break;
505
 
506
        case WM_NCPAINT:
507
            hdc = wParam? (HDC)wParam: GetWindowDC (hWnd);
508
            GetWindowRect(hWnd, &rc);
509
 
510
            if (dwStyle & WS_BORDER)
511
                Draw3dInset(hdc, rc.left, rc.top,
512
                        rc.right-rc.left, rc.bottom-rc.top);
513
 
514
            if (!wParam)
515
                ReleaseDC (hWnd, hdc);
516
            break;
517
 
518
        case WM_PAINT:
519
        {
520
            int     dispLen;
521
            char*   dispBuffer;
522
            RECT    rect,rc;
523
            PAINTSTRUCT ps;
524
 
525
            HGDIOBJ oldfont;
526
            oldfont=NULL;
527
 
528
            hdc = BeginPaint (hWnd,&ps);
529
            GetClientRect (hWnd, &rect);
530
 
531
            if (dwStyle & WS_DISABLED)
532
            {
533
#if 0
534
                SetBrushColor (hdc, LTGRAY/*COLOR_lightgray*/);
535
                FillBox (hdc, 0, 0, rect.right, rect.bottom);
536
#else
537
                rc.left=0; rc.top=0; rc.bottom=rect.bottom; rc.right=rect.right;
538
                FillRect(hdc,&rc,GetStockObject(LTGRAY_BRUSH));
539
#endif
540
                SetBkColor (hdc, LTGRAY/*COLOR_lightgray*/);
541
            }
542
            else
543
            {
544
#if 0
545
                SetBrushColor (hdc, WHITE/*COLOR_lightwhite*/);
546
                FillBox (hdc, 0, 0, rect.right, rect.bottom);
547
#else
548
                rc.left=0; rc.top=0; rc.bottom=rect.bottom; rc.right=rect.right;
549
                FillRect(hdc,&rc,GetStockObject(WHITE_BRUSH));
550
#endif
551
                SetBkColor (hdc, WHITE/*COLOR_lightwhite*/);
552
            }
553
 
554
            SetTextColor (hdc, BLACK/*COLOR_black*/);
555
            dispLen = edtGetDispLen (pCtrl);
556
            if (dispLen == 0)
557
            {
558
                EndPaint (hWnd, &ps);
559
                break;
560
            }
561
 
562
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
563
 
564
#ifdef _DEBUG
565
            if (pSLEditData->startPos > pSLEditData->dataEnd)
566
                fprintf (stderr, "ASSERT failure: %s.\n", "Edit Paint");
567
#endif
568
 
569
            dispBuffer = ALLOCA(dispLen + 1);
570
 
571
            if (dwStyle & ES_PASSWORD)
572
                memset (dispBuffer, '*', dispLen);
573
            else
574
                memcpy (dispBuffer,
575
                    pSLEditData->buffer + pSLEditData->startPos,
576
                    dispLen);
577
 
578
            dispBuffer [dispLen] = '\0';
579
 
580
            /* only implemented ES_LEFT align format for single line edit. */
581
            rect.left += pSLEditData->leftMargin;
582
            rect.top += pSLEditData->topMargin;
583
            rect.right -= pSLEditData->rightMargin;
584
            rect.bottom -= pSLEditData->bottomMargin;
585
 
586
#if 0   /* FIXME no ClipRectIntersect() */
587
#if 0            
588
            ClipRectIntersect (hdc, &rect);
589
#else
590
            GdSetClipRects(hdc->psd,1,&rect);   /*??==ClipRectIntersect??*/
591
#endif
592
#endif
593
 
594
#ifdef USE_BIG5     
595
            oldfont=SelectObject(hdc,CreateFont(12,
596
                        0,0,0,0,0,0,0,0,0,0,0,
597
                        FF_DONTCARE|DEFAULT_PITCH,
598
                        "HZXFONT"));
599
#else
600
            SelectObject(hdc, pSLEditData->hFont);
601
#endif
602
            TextOut (hdc, pSLEditData->leftMargin, pSLEditData->topMargin,
603
                dispBuffer,-1);
604
 
605
#ifdef USE_BIG5     
606
            DeleteObject(SelectObject(hdc,oldfont));
607
#endif
608
 
609
            EndPaint (hWnd, &ps);
610
 
611
            FREEA(dispBuffer);
612
        }
613
        break;
614
#if 1   /* jmt+ */
615
        case WM_KEYDOWN:
616
        {
617
            BOOL    bChange = FALSE;
618
            int     i;
619
            RECT    InvRect;
620
            int     deleted;
621
 
622
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
623
 
624
            switch ((int)(wParam))      /* (LOWORD (wParam)) */
625
            {
626
#if 0
627
                case SCANCODE_ENTER:
628
                    SendMessage (GetParent (hWnd), WM_COMMAND,
629
                        (WPARAM) MAKELONG (pCtrl->id, EN_ENTER), (LPARAM) hWnd);
630
                return 0;
631
 
632
                case SCANCODE_HOME:
633
                    if (pSLEditData->editPos == 0)
634
                        return 0;
635
 
636
                    pSLEditData->editPos  = 0;
637
                    pSLEditData->caretOff = 0;
638
 
639
                    SetCaretPos (pSLEditData->caretOff * GetSysCharWidth (hWnd)
640
                        + pSLEditData->leftMargin, pSLEditData->topMargin);
641
                    if (pSLEditData->startPos != 0)
642
                        InvalidateRect (hWnd, NULL, FALSE);
643
 
644
                    pSLEditData->startPos = 0;
645
                return 0;
646
 
647
                case SCANCODE_END:
648
                {
649
                    int newStartPos;
650
 
651
                    if (pSLEditData->editPos == pSLEditData->dataEnd)
652
                        return 0;
653
 
654
                    newStartPos = edtGetStartDispPosAtEnd (pCtrl, pSLEditData);
655
 
656
                    pSLEditData->editPos = pSLEditData->dataEnd;
657
                    pSLEditData->caretOff = pSLEditData->editPos - newStartPos;
658
 
659
                   SetCaretPos (pSLEditData->caretOff * GetSysCharWidth (hWnd)
660
                        + pSLEditData->leftMargin, pSLEditData->topMargin);
661
                   if (pSLEditData->startPos != newStartPos)
662
                        InvalidateRect (hWnd, NULL, FALSE);
663
 
664
                    pSLEditData->startPos = newStartPos;
665
                }
666
                return 0;
667
#endif
668
 
669
                case VK_LEFT: /* SCANCODE_CURSORBLOCKLEFT: */
670
                {
671
                    BOOL bScroll = FALSE;
672
                    int  scrollStep;
673
 
674
                    if (pSLEditData->editPos == 0)
675
                        return 0;
676
 
677
                    if (edtIsACCharBeforePosition (pSLEditData->buffer,
678
                            pSLEditData->editPos))
679
                    {
680
                        scrollStep = 2;
681
                        pSLEditData->editPos -= 2;
682
                    }
683
                    else {
684
                        scrollStep = 1;
685
                        pSLEditData->editPos --;
686
                    }
687
 
688
                    pSLEditData->caretOff -= scrollStep;
689
                    if (pSLEditData->caretOff == 0
690
                            && pSLEditData->editPos != 0)
691
                    {
692
                        bScroll = TRUE;
693
 
694
                        if (edtIsACCharBeforePosition (pSLEditData->buffer,
695
                                pSLEditData->editPos))
696
                        {
697
                            pSLEditData->startPos -= 2;
698
                            pSLEditData->caretOff = 2;
699
                        }
700
                        else
701
                        {
702
                            pSLEditData->startPos --;
703
                            pSLEditData->caretOff = 1;
704
                        }
705
                    }
706
                    else if (pSLEditData->caretOff < 0)
707
                    {
708
                        pSLEditData->startPos = 0;
709
                        pSLEditData->caretOff = 0;
710
                    }
711
 
712
                    SetCaretPos (pSLEditData->caretOff * GetSysCharWidth (hWnd)
713
                            + pSLEditData->leftMargin, pSLEditData->topMargin);
714
 
715
                    if (bScroll)
716
                        InvalidateRect (hWnd, NULL, FALSE);
717
                }
718
                return 0;
719
 
720
                case VK_RIGHT: /* SCANCODE_CURSORBLOCKRIGHT: */
721
                {
722
                    BOOL bScroll = FALSE;
723
                    int  scrollStep, moveStep;
724
 
725
                    if (pSLEditData->editPos == pSLEditData->dataEnd)
726
                        return 0;
727
 
728
                    if (edtIsACCharAtPosition (pSLEditData->buffer,
729
                                pSLEditData->dataEnd,
730
                                pSLEditData->startPos))
731
                    {
732
                        if (edtIsACCharAtPosition (pSLEditData->buffer,
733
                                    pSLEditData->dataEnd,
734
                                    pSLEditData->editPos))
735
                        {
736
                            scrollStep = 2;
737
                            moveStep = 2;
738
                            pSLEditData->editPos  += 2;
739
                        }
740
                        else
741
                        {
742
                            scrollStep = 2;
743
                            moveStep = 1;
744
                            pSLEditData->editPos ++;
745
                        }
746
                    }
747
                    else
748
                    {
749
                        if (edtIsACCharAtPosition (pSLEditData->buffer,
750
                                    pSLEditData->dataEnd,
751
                                    pSLEditData->editPos))
752
                        {
753
                            if (edtIsACCharAtPosition (pSLEditData->buffer,
754
                                    pSLEditData->dataEnd,
755
                                    pSLEditData->startPos + 1))
756
                                scrollStep = 3;
757
                            else
758
                                scrollStep = 2;
759
 
760
                            moveStep = 2;
761
                            pSLEditData->editPos += 2;
762
                        }
763
                        else
764
                        {
765
                            scrollStep = 1;
766
                            moveStep = 1;
767
                            pSLEditData->editPos ++;
768
                        }
769
                    }
770
 
771
                    pSLEditData->caretOff += moveStep;
772
                    if (pSLEditData->caretOff * GetSysCharWidth (hWnd)
773
                            > edtGetOutWidth (pCtrl))
774
                    {
775
                        bScroll = TRUE;
776
                        pSLEditData->startPos += scrollStep;
777
 
778
                        pSLEditData->caretOff =
779
                            pSLEditData->editPos - pSLEditData->startPos;
780
                    }
781
 
782
                    SetCaretPos (pSLEditData->caretOff * GetSysCharWidth (hWnd)
783
                            + pSLEditData->leftMargin, pSLEditData->topMargin);
784
                    if (bScroll)
785
                        InvalidateRect (hWnd, NULL, FALSE);
786
                }
787
                return 0;
788
#if 0                
789
                case SCANCODE_INSERT:
790
                    pCtrl->userdata ^= EST_REPLACE;
791
                break;
792
#endif
793
                case VK_DELETE: /* SCANCODE_REMOVE: */
794
                    if ((pCtrl->userdata & EST_READONLY)
795
                            || (pSLEditData->editPos == pSLEditData->dataEnd)){
796
        #if 0   /* fix: no ping() */
797
                        Ping ();
798
        #endif
799
                        return 0;
800
                    }
801
 
802
                    if (edtIsACCharAtPosition (pSLEditData->buffer,
803
                            pSLEditData->dataEnd, pSLEditData->editPos))
804
                        deleted = 2;
805
                    else
806
                        deleted = 1;
807
 
808
                    for (i = pSLEditData->editPos;
809
                            i < pSLEditData->dataEnd - deleted; i++)
810
                        pSLEditData->buffer [i]
811
                            = pSLEditData->buffer [i + deleted];
812
 
813
                    pSLEditData->dataEnd -= deleted;
814
                    bChange = TRUE;
815
 
816
                    InvRect.left = pSLEditData->leftMargin
817
                            + pSLEditData->caretOff * GetSysCharWidth (hWnd);
818
                    InvRect.top = pSLEditData->topMargin;
819
                    InvRect.right = pCtrl->clirect.right - pCtrl->clirect.left;
820
                    InvRect.bottom = pCtrl->clirect.bottom - pCtrl->clirect.top;
821
 
822
                    InvalidateRect (hWnd, &InvRect, FALSE);
823
                break;
824
 
825
                case VK_BACK: /* SCANCODE_BACKSPACE: */
826
                    if ((pCtrl->userdata & EST_READONLY)
827
                            || (pSLEditData->editPos == 0)) {
828
#if 0   /* fix: no ping */
829
                        Ping ();
830
#endif
831
                        return 0;
832
                    }
833
 
834
                    if (edtIsACCharBeforePosition (pSLEditData->buffer,
835
                                    pSLEditData->editPos))
836
                        deleted = 2;
837
                    else
838
                        deleted = 1;
839
 
840
                    for (i = pSLEditData->editPos;
841
                            i < pSLEditData->dataEnd;
842
                            i++)
843
                        pSLEditData->buffer [i - deleted]
844
                            = pSLEditData->buffer [i];
845
 
846
                    pSLEditData->dataEnd -= deleted;
847
                    pSLEditData->editPos -= deleted;
848
                    bChange = TRUE;
849
 
850
                    pSLEditData->caretOff -= deleted;
851
                    if (pSLEditData->caretOff == 0
852
                            && pSLEditData->editPos != 0) {
853
                        if (edtIsACCharBeforePosition (pSLEditData->buffer,
854
                                pSLEditData->editPos)) {
855
                            pSLEditData->startPos -= 2;
856
                            pSLEditData->caretOff = 2;
857
                        }
858
                        else {
859
                            pSLEditData->startPos --;
860
                            pSLEditData->caretOff = 1;
861
                        }
862
 
863
                        InvRect.left = pSLEditData->leftMargin;
864
                        InvRect.top = pSLEditData->topMargin;
865
                        InvRect.right = pCtrl->clirect.right -
866
                                pCtrl->clirect.left;
867
                        InvRect.bottom = pCtrl->clirect.bottom -
868
                                pCtrl->clirect.top;
869
                    }
870
                    else {
871
                        InvRect.left = pSLEditData->leftMargin
872
                            + pSLEditData->caretOff * GetSysCharWidth (hWnd);
873
                        InvRect.top = pSLEditData->topMargin;
874
                        InvRect.right = pCtrl->clirect.right -
875
                                pCtrl->clirect.left;
876
                        InvRect.bottom = pCtrl->clirect.bottom -
877
                                pCtrl->clirect.top;
878
                    }
879
 
880
                    SetCaretPos (pSLEditData->caretOff * GetSysCharWidth (hWnd)
881
                            + pSLEditData->leftMargin, pSLEditData->topMargin);
882
                    InvalidateRect (hWnd, &InvRect, FALSE);
883
                break;
884
 
885
                default:
886
                break;
887
            }
888
 
889
            if (bChange)
890
                SendMessage (GetParent (hWnd), WM_COMMAND,
891
                    (WPARAM) MAKELONG (pCtrl->id, EN_CHANGE), (LPARAM) hWnd);
892
        }
893
        break;
894
#endif
895
        case WM_CHAR:
896
        {
897
            char charBuffer [2];
898
            int  i, chars, scrollStep, inserting;
899
            RECT InvRect;
900
 
901
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
902
 
903
            if (dwStyle & ES_READONLY) {
904
 
905
#if 0   /* fix: no ping() */
906
                Ping();
907
#endif
908
                return 0;
909
            }
910
 
911
            if (HIBYTE (wParam)) {
912
                charBuffer [0] = LOBYTE (wParam);
913
                charBuffer [1] = HIBYTE (wParam);
914
                chars = 2;
915
            }
916
            else {
917
                charBuffer [0] = LOBYTE (wParam);
918
                chars = 1;
919
            }
920
 
921
            if (chars == 1) {
922
                switch (charBuffer [0])
923
                {
924
                    case 0x00:  /* NULL */
925
                    case 0x07:  /* BEL */
926
                    case 0x08:  /* BS */
927
                    case 0x09:  /* HT */
928
                    case 0x0A:  /* LF */
929
                    case 0x0B:  /* VT */
930
                    case 0x0C:  /* FF */
931
                    case 0x0D:  /* CR */
932
                    case 0x1B:  /* Escape */
933
                    return 0;
934
                }
935
            }
936
 
937
            if (pCtrl->userdata & EST_REPLACE) {
938
                if (pSLEditData->dataEnd == pSLEditData->editPos)
939
                    inserting = chars;
940
                else if (edtIsACCharAtPosition (pSLEditData->buffer,
941
                                pSLEditData->dataEnd,
942
                                pSLEditData->editPos)) {
943
                    if (chars == 2)
944
                        inserting = 0;
945
                    else
946
                        inserting = -1;
947
                }
948
                else {
949
                    if (chars == 2)
950
                        inserting = 1;
951
                    else
952
                        inserting = 0;
953
                }
954
            }
955
            else
956
                inserting = chars;
957
 
958
            /* check space */
959
            if (pSLEditData->dataEnd + inserting > pSLEditData->bufferLen) {
960
 
961
#if 0   /* fix: no ping() */
962
                Ping ();
963
#endif
964
                SendMessage (GetParent (hWnd), WM_COMMAND,
965
                    (WPARAM) MAKELONG (pCtrl->id, EN_MAXTEXT), (LPARAM) hWnd);
966
                return 0;
967
            }
968
            else if ((pSLEditData->hardLimit >= 0)
969
                        && ((pSLEditData->dataEnd + inserting)
970
                            > pSLEditData->hardLimit)) {
971
#if 0   /* fix: no ping() */
972
                Ping ();
973
#endif
974
                SendMessage (GetParent (hWnd), WM_COMMAND,
975
                    (WPARAM) MAKELONG (pCtrl->id, EN_MAXTEXT), (LPARAM) hWnd);
976
                return 0;
977
            }
978
 
979
            if (inserting == -1) {
980
                for (i = pSLEditData->editPos; i < pSLEditData->dataEnd-1; i++)
981
                    pSLEditData->buffer [i] = pSLEditData->buffer [i + 1];
982
            }
983
            else if (inserting > 0) {
984
                for (i = pSLEditData->dataEnd + inserting - 1;
985
                        i > pSLEditData->editPos + inserting - 1;
986
                        i--)
987
                    pSLEditData->buffer [i]
988
                            = pSLEditData->buffer [i - inserting];
989
            }
990
 
991
            for (i = 0; i < chars; i++)
992
                    pSLEditData->buffer [pSLEditData->editPos + i]
993
                        = charBuffer [i];
994
 
995
            pSLEditData->editPos += chars;
996
            pSLEditData->caretOff += chars;
997
            pSLEditData->dataEnd += inserting;
998
 
999
            if (pSLEditData->caretOff * GetSysCharWidth (hWnd)
1000
                            > edtGetOutWidth (pCtrl))
1001
            {
1002
                if (edtIsACCharAtPosition (pSLEditData->buffer,
1003
                                pSLEditData->dataEnd,
1004
                                pSLEditData->startPos))
1005
                    scrollStep = 2;
1006
                else {
1007
                    if (chars == 2) {
1008
                        if (edtIsACCharAtPosition (pSLEditData->buffer,
1009
                                pSLEditData->dataEnd,
1010
                                pSLEditData->startPos + 1))
1011
                            scrollStep = 3;
1012
                        else
1013
                            scrollStep = 2;
1014
                    }
1015
                    else
1016
                        scrollStep = 1;
1017
                }
1018
 
1019
                pSLEditData->startPos += scrollStep;
1020
 
1021
                pSLEditData->caretOff =
1022
                            pSLEditData->editPos - pSLEditData->startPos;
1023
 
1024
                InvRect.left = pSLEditData->leftMargin;
1025
                InvRect.top = pSLEditData->topMargin;
1026
                InvRect.right = pCtrl->clirect.right - pCtrl->clirect.left;
1027
                InvRect.bottom = pCtrl->clirect.bottom - pCtrl->clirect.top;
1028
            }
1029
            else {
1030
                InvRect.left = pSLEditData->leftMargin
1031
                                    + (pSLEditData->caretOff - chars)
1032
                                        * GetSysCharWidth (hWnd);
1033
                InvRect.top = pSLEditData->topMargin;
1034
                InvRect.right = pCtrl->clirect.right - pCtrl->clirect.left;
1035
                InvRect.bottom = pCtrl->clirect.bottom - pCtrl->clirect.top;
1036
            }
1037
 
1038
            SetCaretPos (pSLEditData->caretOff * GetSysCharWidth (hWnd)
1039
                            + pSLEditData->leftMargin, pSLEditData->topMargin);
1040
            InvalidateRect (hWnd, &InvRect, FALSE);
1041
 
1042
            SendMessage (GetParent (hWnd), WM_COMMAND,
1043
                (WPARAM) MAKELONG (pCtrl->id, EN_CHANGE), (LPARAM) hWnd);
1044
        }
1045
        break;
1046
 
1047
        case WM_GETTEXTLENGTH:
1048
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
1049
            return pSLEditData->dataEnd;
1050
 
1051
        case WM_GETTEXT:
1052
        {
1053
            char*   buffer = (char*)lParam;
1054
            int     len;
1055
 
1056
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
1057
 
1058
            len = min ((int)wParam, pSLEditData->dataEnd);
1059
 
1060
            memcpy (buffer, pSLEditData->buffer, len);
1061
            buffer [len] = '\0';
1062
 
1063
            return len;
1064
        }
1065
        break;
1066
 
1067
        case WM_SETTEXT:
1068
        {
1069
            int len;
1070
 
1071
            if (dwStyle & ES_READONLY)
1072
                return 0;
1073
 
1074
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
1075
 
1076
 
1077
            len = strlen ((char*)lParam);
1078
            len = min (len, pSLEditData->bufferLen);
1079
 
1080
            if (pSLEditData->hardLimit >= 0)
1081
                len = min (len, pSLEditData->hardLimit);
1082
 
1083
            pSLEditData->dataEnd = len;
1084
            memcpy (pSLEditData->buffer, (char*)lParam, len);
1085
 
1086
            pSLEditData->editPos        = 0;
1087
            pSLEditData->caretOff       = 0;
1088
            pSLEditData->startPos       = 0;
1089
 
1090
            InvalidateRect (hWnd, NULL, FALSE);
1091
        }
1092
        break;
1093
 
1094
        case WM_LBUTTONDBLCLK:
1095
        break;
1096
 
1097
        case WM_LBUTTONDOWN:
1098
        {
1099
            int newOff;
1100
 
1101
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
1102
            newOff = edtGetOffset (hWnd,pSLEditData, LOWORD (lParam));
1103
 
1104
            if (newOff != pSLEditData->caretOff)
1105
            {
1106
                pSLEditData->editPos += newOff - pSLEditData->caretOff;
1107
                pSLEditData->caretOff = newOff;
1108
 
1109
                SetCaretPos (pSLEditData->caretOff * GetSysCharWidth (hWnd)
1110
                        + pSLEditData->leftMargin, pSLEditData->topMargin);
1111
            }
1112
        }
1113
        break;
1114
 
1115
        case WM_LBUTTONUP:
1116
        break;
1117
 
1118
        case WM_MOUSEMOVE:
1119
        break;
1120
 
1121
        case WM_GETDLGCODE:
1122
        return DLGC_WANTCHARS | DLGC_HASSETSEL | DLGC_WANTARROWS;
1123
 
1124
        case EM_SETREADONLY:
1125
            if (wParam)
1126
                pCtrl->style/*dwStyle*/ |= ES_READONLY;
1127
            else
1128
                pCtrl->style/*dwStyle*/ &= ~ES_READONLY;
1129
        return 0;
1130
 
1131
        case EM_SETPASSWORDCHAR:
1132
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
1133
 
1134
            if (pSLEditData->passwdChar != (int)wParam) {
1135
                if (dwStyle & ES_PASSWORD) {
1136
                    pSLEditData->passwdChar = (int)wParam;
1137
                    InvalidateRect (hWnd, NULL, TRUE);
1138
                }
1139
            }
1140
        return 0;
1141
 
1142
        case EM_GETPASSWORDCHAR:
1143
        {
1144
            int* passwdchar;
1145
 
1146
            pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
1147
            passwdchar = (int*) lParam;
1148
 
1149
            *passwdchar = pSLEditData->passwdChar;
1150
        }
1151
        return 0;
1152
 
1153
        case EM_LIMITTEXT:
1154
        {
1155
            int newLimit = (int)wParam;
1156
 
1157
            if (newLimit >= 0) {
1158
                pSLEditData = (PSLEDITDATA) (pCtrl->userdata2);
1159
                if (pSLEditData->bufferLen < newLimit)
1160
                    pSLEditData->hardLimit = -1;
1161
                else
1162
                    pSLEditData->hardLimit = newLimit;
1163
            }
1164
        }
1165
        return 0;
1166
 
1167
        default:
1168
                return DefWindowProc (hWnd, message, wParam, lParam);
1169
        break;
1170
    }
1171
    return 0;
1172
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.