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

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [mwin/] [winlib/] [medit.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Copyright (C) 1999, 2000, Wei Yongming.
3
 * Portions Copyright (c) 2000 Greg Haerr <greg@censoft.com>
4
 *
5
 * Multi Line 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/08/30  gv          port to microwin        ported
43
**
44
**
45
** TODO:
46
**    * Selection.
47
**    * Undo.
48
*/
49
 
50
#include <stdio.h>
51
#include <stdlib.h>
52
#include <string.h>
53
#include <sys/time.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
#define WIDTH_MEDIT_BORDER       2
62
#define MARGIN_MEDIT_LEFT        1
63
#define MARGIN_MEDIT_TOP         1
64
#define MARGIN_MEDIT_RIGHT       2
65
#define MARGIN_MEDIT_BOTTOM      1
66
 
67
#define LEN_MLEDIT_BUFFER       3000
68
#define LEN_MLEDIT_UNDOBUFFER   1024
69
 
70
#define EST_FOCUSED     0x00000001L
71
#define EST_MODIFY      0x00000002L
72
#define EST_READONLY    0x00000004L
73
#define EST_REPLACE     0x00000008L
74
 
75
#define MEDIT_OP_NONE    0x00
76
#define MEDIT_OP_DELETE  0x01
77
#define MEDIT_OP_INSERT  0x02
78
#define MEDIT_OP_REPLACE 0x03
79
 
80
typedef struct tagLINEDATA {
81
        int     lineNO;                   /* ÐкŠ*/
82
        int     dataEnd;
83
        struct  tagLINEDATA *previous;    /* Ç°Ò»ÐÐ */
84
        struct  tagLINEDATA *next;        /* ºóÒ»ÐÐ */
85
        char    buffer[LEN_MLEDIT_BUFFER+1];
86
}LINEDATA;
87
typedef    LINEDATA*     PLINEDATA;
88
 
89
#define ATTENG 0        /* english */
90
#define ATTCHL 1        /* chinese left(1st) byte */
91
#define ATTCHR 2        /* chinese right(2nd) byte */
92
static char attr[LEN_MLEDIT_BUFFER];
93
 
94
typedef struct tagMLEDITDATA {
95
    int     totalLen;      /* length of buffer,¿ÉÄÜûÓÐÓà */
96
 
97
    int     editPos;        /* current edit position */
98
    int     caretPos;       /* caret offset in box */
99
    int     editLine;           /* current eidt line */
100
    int     dispPos;        /* ¿ªÊ¼ÏÔʾµÄλÖà */
101
    int     StartlineDisp;  /* start line displayed */
102
    int     EndlineDisp;    /* end line displayed */
103
    int     linesDisp;      /* ÐèÒªÏÔʾµÄÐÐÊý */
104
    int     lines;                  /* ×ܵÄÐÐÊý` */
105
    int     MaxlinesDisp;    /* ×î´óÏÔʾµÄÐÐÊý. */
106
 
107
    int     selStartPos;    /* selection start position */
108
    int     selStartLine;   /* selection start line */
109
    int     selEndPos;      /* selection end position */
110
    int     selEndLine;     /* selection end line */
111
 
112
    int     passwdChar;     /* password character */
113
 
114
    int     leftMargin;     /* left margin */
115
    int     topMargin;      /* top margin */
116
    int     rightMargin;    /* right margin */
117
    int     bottomMargin;   /* bottom margin */
118
 
119
    int     hardLimit;      /* hard limit */
120
 
121
    int     lastOp;         /* last operation */
122
    int     lastPos;        /* last operation position */
123
    int     lastLine;       /* last operation line */
124
    int     affectedLen;    /* affected len of last operation */
125
    int     undoBufferLen;  /* undo buffer len */
126
    char    undoBuffer [LEN_MLEDIT_UNDOBUFFER];
127
                            /* Undo buffer; */
128
    PLINEDATA   head;       /* buffer */
129
    PLINEDATA   tail;       /* ¿ÉÄܲ»ÐèÒª */
130
}MLEDITDATA;
131
typedef MLEDITDATA* PMLEDITDATA;
132
 
133
BOOL RegisterMLEditControl (void);
134
 
135
int MLEditCtrlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam);
136
 
137
#define PIXEL_invalid (-1)
138
extern HWND sg_hCaretWnd;
139
extern HWND rootwp;
140
 
141
static int GetSysCharHeight (HWND hwnd)
142
{
143
#ifndef USE_BIG5            
144
        HDC             hdc;
145
        int xw, xh, xb;
146
 
147
        hdc = GetDC(hwnd);
148
        SelectObject(hdc, GetStockObject(DEFAULT_FONT));
149
        GdSetFont(hdc->font->pfont);
150
        GdGetTextSize(hdc->font->pfont,"X",1, &xw,&xh,&xb,MWTF_ASCII);
151
        ReleaseDC(hwnd,hdc);
152
 
153
        return xh;
154
#else
155
        return 12;
156
#endif
157
}
158
 
159
static int GetSysCharWidth (HWND hwnd)
160
{
161
#ifndef USE_BIG5            
162
        HDC             hdc;
163
        int xw, xh, xb;
164
 
165
        hdc = GetDC(hwnd);
166
        SelectObject(hdc, GetStockObject(DEFAULT_FONT));
167
        GdSetFont(hdc->font->pfont);
168
        GdGetTextSize(hdc->font->pfont,"X",1, &xw,&xh,&xb,MWTF_ASCII);
169
        ReleaseDC(hwnd,hdc);
170
 
171
        return xw;
172
#else
173
        return 6;
174
#endif
175
}
176
 
177
static int GetSysCCharWidth (HWND hwnd)
178
{
179
        return (2*GetSysCharWidth(hwnd));
180
}
181
char* GetWindowCaption (HWND hWnd)
182
{
183
    return hWnd->szTitle;
184
}
185
 
186
DWORD GetWindowAdditionalData (HWND hWnd)
187
{
188
        return hWnd->userdata;
189
}
190
 
191
DWORD SetWindowAdditionalData (HWND hWnd, DWORD newData)
192
{
193
    DWORD    oldOne = 0L;
194
 
195
    oldOne = hWnd->userdata;
196
    hWnd->userdata = newData;
197
 
198
    return oldOne;
199
}
200
 
201
DWORD GetWindowAdditionalData2 (HWND hWnd)
202
{
203
        return hWnd->userdata2;
204
}
205
 
206
DWORD SetWindowAdditionalData2 (HWND hWnd, DWORD newData)
207
{
208
    DWORD    oldOne = 0L;
209
 
210
    oldOne = hWnd->userdata2;
211
    hWnd->userdata2 = newData;
212
 
213
    return oldOne;
214
}
215
 
216
DWORD GetWindowStyle (HWND hWnd)
217
{
218
        return hWnd->style;
219
}
220
 
221
BOOL ExcludeWindowStyle (HWND hWnd, DWORD dwStyle)
222
{
223
        if (hWnd == rootwp/*HWND_DESKTOP*/)
224
                return FALSE;
225
 
226
        hWnd->style &= ~dwStyle;
227
        return TRUE;
228
}
229
 
230
BOOL IncludeWindowStyle (HWND hWnd, DWORD dwStyle)
231
{
232
 
233
        if (hWnd == rootwp/*HWND_DESKTOP*/)
234
                return FALSE;
235
 
236
        hWnd->style |= dwStyle;
237
        return TRUE;
238
}
239
 
240
int WINAPI MwRegisterMEditControl(HINSTANCE hInstance)
241
{
242
        WNDCLASS        wc;
243
 
244
        wc.style        = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
245
        wc.lpfnWndProc  = (WNDPROC)MLEditCtrlProc;
246
        wc.cbClsExtra   = 0;
247
        wc.cbWndExtra   = 0;
248
        wc.hInstance    = hInstance;
249
        wc.hIcon        = NULL;
250
        wc.hCursor      = 0;
251
        wc.hbrBackground= GetStockObject(WHITE_BRUSH);
252
        wc.lpszMenuName = NULL;
253
        wc.lpszClassName= "MEDIT";
254
 
255
        return RegisterClass(&wc);
256
}
257
 
258
static inline int edtGetOutWidth (HWND hWnd)
259
{
260
        PMLEDITDATA pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
261
        RECT rc;
262
        GetClientRect(hWnd,&rc);
263
    return rc.right - rc.left
264
            - pMLEditData->leftMargin
265
            - pMLEditData->rightMargin;
266
}
267
 
268
static int edtGetStartDispPosAtEnd (HWND hWnd,
269
            PLINEDATA pLineData)
270
{
271
    int         nOutWidth = edtGetOutWidth (hWnd);
272
    int         endPos  = pLineData->dataEnd;
273
    PMLEDITDATA pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
274
    int         newStartPos = pMLEditData->dispPos;
275
    const char* buffer = pLineData->buffer;
276
 
277
    if(endPos < newStartPos)
278
                return 0;
279
    while (TRUE)
280
    {
281
        if ((endPos - newStartPos) * GetSysCharWidth (hWnd) < nOutWidth)
282
            break;
283
 
284
        /* 1st:gb:a1-f7,big5:a1-f9 */
285
        if ((BYTE)buffer [newStartPos] > 0xA0)
286
        {
287
            newStartPos ++;
288
            if (newStartPos < pLineData->dataEnd)
289
            {
290
#ifndef USE_BIG5
291
                if ((BYTE)buffer [newStartPos] > 0xA0)
292
#else   /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
293
                if ( ((BYTE)buffer [newStartPos] >= 0x40 && (BYTE)buffer[newStartPos] <= 0x7e) ||
294
                     ((BYTE)buffer [newStartPos] >= 0xa1 && (BYTE)buffer[newStartPos] <= 0xfe))
295
#endif
296
                    newStartPos ++;
297
            }
298
        }
299
        else
300
            newStartPos ++;
301
    }
302
    return newStartPos;
303
}
304
 
305
static int edtGetDispLen (HWND hWnd,PLINEDATA pLineData)
306
{
307
    int i, n = 0;
308
    int nOutWidth = edtGetOutWidth (hWnd);
309
    int nTextWidth = 0;
310
    PMLEDITDATA pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
311
    const char* buffer = pLineData->buffer;
312
 
313
    if(buffer[0]==0||pLineData->dataEnd<pMLEditData->dispPos)
314
                return 0;
315
 
316
    for (i = pMLEditData->dispPos; i < pLineData->dataEnd; i++)
317
    {
318
        /* 1st:gb:a1-f7,big5:a1-f9 */
319
        if ((BYTE)buffer [i] > 0xA0)
320
        {
321
            i++;
322
            if (i < pLineData->dataEnd)
323
            {
324
#ifndef USE_BIG5
325
                if ((BYTE)buffer [i] > 0xA0)    /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
326
#else   /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
327
                if ( ((BYTE)buffer [i] >= 0x40 && (BYTE)buffer[i] <= 0x7e) ||
328
                     ((BYTE)buffer [i] >= 0xa1 && (BYTE)buffer[i] <= 0xfe))
329
#endif
330
                {
331
                    nTextWidth += GetSysCCharWidth (hWnd);
332
                    n += 2;
333
                }
334
                else
335
                    i--;
336
            }
337
            else
338
            {
339
                nTextWidth += GetSysCharWidth (hWnd);
340
                n++;
341
            }
342
        }
343
        else
344
        {
345
            nTextWidth += GetSysCharWidth (hWnd);
346
            n++;
347
        }
348
 
349
        if (nTextWidth > nOutWidth)
350
            break;
351
    }
352
 
353
    return n;
354
}
355
 
356
static int edtGetOffset (HWND hwnd,const MLEDITDATA* pMLEditData, PLINEDATA pLineData, int x)
357
{
358
    int i;
359
    int newOff = 0;
360
    int nTextWidth = 0;
361
    const char* buffer = pLineData->buffer;
362
 
363
    if(pLineData->dataEnd<pMLEditData->dispPos)
364
                return pLineData->dataEnd;
365
 
366
    x -= pMLEditData->leftMargin;
367
    for (i = pMLEditData->dispPos; i < pLineData->dataEnd; i++) {
368
        if ((nTextWidth + (GetSysCharWidth(hwnd) >> 1)) >= x)
369
            break;
370
 
371
        /* 1st:gb:a1-f7,big5:a1-f9 */
372
        if ((BYTE)buffer [i] > 0xA0)
373
        {
374
            i++;
375
 
376
            if (nTextWidth + GetSysCCharWidth(hwnd)/2 >= x)
377
                break;
378
 
379
            if (i < pLineData->dataEnd)
380
            {
381
#ifndef USE_BIG5
382
                if ((BYTE)buffer [i] > 0xA0)    /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
383
#else   /* 2nd:gb:a1-fe,big5:40-7e,a1-fe */
384
                if ( ((BYTE)buffer [i] >= 0x40 && (BYTE)buffer[i] <= 0x7e) ||
385
                     ((BYTE)buffer [i] >= 0xa1 && (BYTE)buffer[i] <= 0xfe))
386
#endif
387
                {
388
                    nTextWidth += GetSysCCharWidth (hwnd);
389
                    newOff += 2;
390
                }
391
                else
392
                    i --;
393
            }
394
            else
395
            {
396
                nTextWidth += GetSysCharWidth (hwnd);
397
                newOff ++;
398
            }
399
        }
400
        else
401
        {
402
            nTextWidth += GetSysCharWidth (hwnd);
403
            newOff ++;
404
        }
405
    }
406
    return newOff;
407
}
408
 
409
static int edtGetLineNO (HWND hwnd,const MLEDITDATA* pMLEditData, int x)
410
{
411
    int nline = 0;
412
        if(x>=0)
413
        {
414
            nline = x / GetSysCharHeight(hwnd);
415
                if (nline <= pMLEditData->linesDisp)
416
                        return nline;
417
        }
418
        return -1;
419
}
420
 
421
static BOOL edtIsACCharAtPosition (const char* string, int len, int pos)
422
{
423
    if (pos > (len - 2))
424
        return FALSE;
425
 
426
/* 1st:gb:a1-f7,big5:a1-f9  2nd:gb:a1-fe,big5:40-7e,a1-fe */
427
#ifndef USE_BIG5
428
    if ((BYTE)string [pos] > 0xA0 && (BYTE)string [pos + 1] > 0xA0)
429
        return TRUE;
430
#else
431
    if ((BYTE)string [pos] > 0xA0)
432
    {
433
        if ( ((BYTE)string [pos + 1] >= 0x40 && (BYTE)string [pos + 1] <= 0x7e) ||
434
             ((BYTE)string [pos + 1] >= 0xa1 && (BYTE)string [pos + 1] <= 0xfe)) {
435
            /*fprintf(stderr,"true\n");
436
            fflush(stderr);*/
437
            return TRUE;
438
        }
439
    }
440
#endif
441
 
442
    return FALSE;
443
}
444
 
445
static void str2attr(const char* str,int len)
446
{
447
    int i=0;
448
    do
449
    {
450
        if (edtIsACCharAtPosition(str,len,i))
451
        {
452
                attr[i]=ATTCHL;
453
                attr[i+1]=ATTCHR;
454
                i+=2;
455
        }
456
        else
457
        {
458
                attr[i]=ATTENG;
459
                i++;
460
        }
461
    }while(i<len);
462
}
463
 
464
static BOOL edtIsACCharBeforePosition (const char* string,int len, int pos)
465
{
466
    if (pos < 2)
467
        return FALSE;
468
 
469
/* 1st:gb:a1-f7,big5:a1-f9  2nd:gb:a1-fe,big5:40-7e,a1-fe */
470
#ifndef USE_BIG5
471
    /* FIXME #ifdef GB2312?*/
472
    if ((BYTE)string [pos - 2] > 0xA0 && (BYTE)string [pos - 1] > 0xA0)
473
        return TRUE;
474
#else
475
#if 0
476
    if ((BYTE)string [pos - 2] > 0xA0)
477
    {
478
        if ( ((BYTE)string [pos - 1] >= 0x40 && (BYTE)string[pos - 1] <= 0x7e) ||
479
             ((BYTE)string [pos - 1] >= 0xa1 && (BYTE)string[pos - 1] <= 0xfe))
480
            return TRUE;
481
    }
482
#else
483
    str2attr(string,len);
484
    if (attr[pos-1]==ATTENG) return FALSE;
485
    else return TRUE;
486
#endif
487
#endif
488
 
489
    return FALSE;
490
}
491
 
492
 
493
static BOOL edtIsACCharFromBegin(const char* string,int len,int pos)
494
{
495
        int i;
496
        if(pos == 0)
497
                return TRUE;
498
        if(len == 0)
499
                return FALSE;
500
        for(i=0;i<len;)
501
        {
502
                if( edtIsACCharAtPosition(string,len,i) )
503
                        i += 2;
504
                else
505
                        i++;
506
                if(i==pos)
507
                        return TRUE;
508
        }
509
        return FALSE;
510
}
511
 
512
int GetRETURNPos(char *str)
513
{
514
        int i;
515
        for(i=0;i<strlen(str);i++)
516
        {
517
                if(str[i]==10)
518
                        return i;
519
        }
520
        return -1;
521
}
522
 
523
void MLEditInitBuffer (PMLEDITDATA pMLEditData,char *spcaption)
524
{
525
        char *caption=spcaption;
526
    int off1;
527
        int lineNO=0;
528
        PLINEDATA  pLineData;
529
        if (!(pMLEditData->head = malloc (sizeof (LINEDATA)))) {
530
                fprintf (stderr, "EDITLINE: malloc error!\n");
531
                return ;
532
        }
533
        pMLEditData->head->previous = NULL;
534
        pLineData=pMLEditData->head;
535
        while ( (off1 = GetRETURNPos(caption)) != -1)
536
        {
537
                off1 = min(off1, LEN_MLEDIT_BUFFER);
538
                memcpy(pLineData->buffer,caption,off1);
539
                pLineData->buffer[off1] = '\0';
540
                caption+=min(off1,LEN_MLEDIT_BUFFER)+1;
541
                pLineData->lineNO  = lineNO;
542
                pMLEditData->dispPos = 0;
543
                pLineData->dataEnd = strlen(pLineData->buffer);
544
                pLineData->next    = malloc (sizeof (LINEDATA));
545
                pLineData->next->previous = pLineData;
546
                pLineData = pLineData->next;
547
                lineNO++;
548
        }
549
        off1 = min(strlen(caption),LEN_MLEDIT_BUFFER);
550
        memcpy(pLineData->buffer,caption,off1);
551
        pLineData->buffer[off1] = '\0';
552
        pLineData->lineNO  = lineNO++;
553
        pMLEditData->dispPos = 0;
554
        pLineData->dataEnd = strlen(pLineData->buffer);
555
        pLineData->next    = NULL;
556
        pMLEditData->lines      = lineNO ;
557
}
558
 
559
PLINEDATA GetLineData(PMLEDITDATA pMLEditData,int lineNO)
560
{
561
        PLINEDATA pLineData=pMLEditData->head;
562
        while(pLineData)
563
        {
564
                if(pLineData->lineNO==lineNO)
565
                        return pLineData;
566
                pLineData = pLineData->next;
567
        }
568
        return NULL;
569
}
570
 
571
int MLEditCtrlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
572
{
573
    DWORD       dwStyle;
574
        DWORD           dw;
575
    HDC         hdc;
576
        PLINEDATA  pLineData;
577
        RECT            clientRect;
578
    PMLEDITDATA pMLEditData;
579
    dwStyle     = GetWindowStyle(hWnd);
580
 
581
    switch (message)
582
    {
583
        case WM_CREATE:
584
        {
585
            if (!(pMLEditData = malloc (sizeof (MLEDITDATA)))) {
586
                fprintf (stderr, "EDIT: malloc error!\n");
587
                return -1;
588
            }
589
 
590
            pMLEditData->totalLen      = LEN_MLEDIT_BUFFER;
591
            pMLEditData->editPos        = 0;
592
            pMLEditData->editLine       = 0;
593
            pMLEditData->caretPos       = 0;
594
 
595
            MLEditInitBuffer(pMLEditData,GetWindowCaption(hWnd));
596
 
597
            GetClientRect(hWnd,&clientRect);
598
            pMLEditData->MaxlinesDisp   = (clientRect.bottom-clientRect.top)/GetSysCharHeight(hWnd);
599
            pMLEditData->linesDisp              = min(pMLEditData->MaxlinesDisp,pMLEditData->lines);
600
            pMLEditData->StartlineDisp  = 0;
601
            pMLEditData->EndlineDisp    = pMLEditData->StartlineDisp + pMLEditData->linesDisp - 1;
602
 
603
            pMLEditData->selStartPos       = 0;
604
            pMLEditData->selEndPos         = 0;
605
            pMLEditData->passwdChar     = '*';
606
            pMLEditData->leftMargin     = MARGIN_MEDIT_LEFT;
607
            pMLEditData->topMargin      = MARGIN_MEDIT_TOP;
608
            pMLEditData->rightMargin    = MARGIN_MEDIT_RIGHT;
609
            pMLEditData->bottomMargin   = MARGIN_MEDIT_BOTTOM;
610
 
611
            pMLEditData->hardLimit      = -1;
612
 
613
            /* undo information */
614
            pMLEditData->lastOp         = MEDIT_OP_NONE;
615
            pMLEditData->lastPos        = 0;
616
            pMLEditData->affectedLen    = 0;
617
            pMLEditData->undoBufferLen  = LEN_MLEDIT_UNDOBUFFER;
618
            pMLEditData->undoBuffer [0] = '\0';
619
                        SetWindowAdditionalData2(hWnd,(DWORD)pMLEditData);
620
                        SetWindowAdditionalData(hWnd,(DWORD)0);
621
            break;
622
        }
623
        case WM_DESTROY:
624
        {
625
                PLINEDATA temp;
626
                pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
627
                DestroyCaret ();
628
                pLineData = pMLEditData->head;
629
                while(pLineData)
630
                {
631
                        /*printf("delete lineNO = %d,buffer=%s\n",pLineData->lineNO,pLineData->buffer);*/
632
                        temp = pLineData->next;
633
                        free(pLineData);
634
                        pLineData = temp;
635
                }
636
                free(pMLEditData);
637
        }
638
        break;
639
 
640
        case WM_SETFONT:
641
        break;
642
 
643
        case WM_GETFONT:
644
        break;
645
 
646
#if 0   /* fix: no WM_SETCURSOR */
647
        case WM_SETCURSOR:
648
            if (dwStyle & WS_DISABLED) {
649
                SetCursor (GetSystemCursor (IDC_ARROW));
650
                return 0;
651
            }
652
        break;
653
 
654
        case WM_SIZECHANGED:
655
        {
656
        }
657
        return 0;
658
#endif
659
        case WM_KILLFOCUS:
660
        {
661
            dw= GetWindowAdditionalData(hWnd);
662
            dw&= ~EST_FOCUSED;
663
            SetWindowAdditionalData(hWnd,dw);
664
 
665
            HideCaret (hWnd);
666
            DestroyCaret ();
667
 
668
            SendMessage (GetParent (hWnd),
669
                         WM_COMMAND,
670
                         (WPARAM) MAKELONG (GetDlgCtrlID(hWnd), EN_KILLFOCUS),
671
                         (LPARAM)hWnd);
672
        }
673
        break;
674
 
675
        case WM_SETFOCUS:
676
                {
677
                        dw= GetWindowAdditionalData(hWnd);
678
            if (dw & EST_FOCUSED)
679
                return 0;
680
 
681
            dw |= EST_FOCUSED;
682
                        SetWindowAdditionalData(hWnd,dw);
683
 
684
            pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
685
 
686
            /* only implemented for ES_LEFT align format. */
687
 
688
            CreateCaret (hWnd, NULL, 1, /*GetSysCharWidth(hWnd)+1,*/
689
                    hWnd->clirect.bottom-hWnd->clirect.top-2);
690
            SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
691
                    + pMLEditData->leftMargin, pMLEditData->topMargin);
692
            ShowCaret(hWnd);
693
 
694
            SendMessage (GetParent (hWnd),
695
                         WM_COMMAND,
696
                         (WPARAM) MAKELONG (GetDlgCtrlID(hWnd), EN_SETFOCUS),
697
                         (LPARAM) hWnd);
698
                }
699
        break;
700
 
701
        case WM_ENABLE:
702
            if ( (!(dwStyle & WS_DISABLED) && !wParam)
703
                    || ((dwStyle & WS_DISABLED) && wParam) ) {
704
                if (wParam)
705
                        ExcludeWindowStyle(hWnd,WS_DISABLED);
706
                else
707
                        IncludeWindowStyle(hWnd,WS_DISABLED);
708
 
709
                InvalidateRect (hWnd, NULL, FALSE);
710
            }
711
        return 0;
712
 
713
        case WM_NCPAINT:
714
        {
715
            RECT rc;
716
#if 0
717
            if (wParam)
718
                hdc = (HDC)wParam;
719
            else
720
                hdc = GetDC (hWnd);
721
#if 0   /* fix: no ClipRectIntersect() */
722
            if (lParam)
723
            ClipRectIntersect (hdc, (RECT*)lParam);
724
#endif
725
#else
726
            hdc = wParam? (HDC)wParam: GetWindowDC (hWnd);
727
            GetWindowRect(hWnd, &rc);
728
#endif
729
            if (dwStyle & WS_BORDER)
730
            {
731
#if 0
732
                RECT rc;
733
                GetWindowRect(hWnd,&rc);
734
                Draw3DDownFrame (hdc, 0, 0,
735
                                      rc.right - rc.left - 1,
736
                                      rc.bottom - rc.top - 1,
737
                                      PIXEL_invalid);
738
#else
739
                Draw3dInset(hdc, rc.left, rc.top,
740
                        rc.right-rc.left, rc.bottom-rc.top);
741
#endif
742
            }
743
            if (!wParam) {
744
                ReleaseDC(hWnd,hdc);
745
                }
746
        }
747
        return 0;
748
 
749
        case WM_PAINT:
750
        {
751
            int     dispLen,i;
752
            char*   dispBuffer;
753
            RECT    rect,rc;
754
            PAINTSTRUCT ps;
755
            HGDIOBJ oldfont=NULL;
756
 
757
            hdc = BeginPaint (hWnd,&ps);
758
            GetClientRect (hWnd, &rect);
759
 
760
            if (dwStyle & WS_DISABLED)
761
            {
762
                rc.left=0; rc.top=0; rc.bottom=rect.bottom; rc.right=rect.right;
763
                FillRect(hdc,&rc,GetStockObject(LTGRAY_BRUSH));
764
                SetBkColor (hdc, LTGRAY/*PIXEL_lightgray*/);
765
            }
766
            else {
767
                rc.left=0; rc.top=0; rc.bottom=rect.bottom; rc.right=rect.right;
768
                FillRect(hdc,&rc,GetStockObject(WHITE_BRUSH));
769
                SetBkColor (hdc, WHITE/*PIXEL_lightwhite*/);
770
            }
771
 
772
            SetTextColor (hdc, BLACK/*PIXEL_black*/);
773
 
774
            pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
775
                        for(i = pMLEditData->StartlineDisp; i <= pMLEditData->EndlineDisp; i++)
776
                        {
777
                                pLineData= GetLineData(pMLEditData,i);
778
                dispLen = edtGetDispLen (hWnd,pLineData);
779
                    if (dispLen == 0 && pMLEditData->EndlineDisp >= pMLEditData->lines) {
780
                        continue;
781
                         }
782
 
783
#ifdef _DEBUG
784
                if (pMLEditData->dispPos > pLineData->dataEnd)
785
                        fprintf (stderr, "ASSERT failure: %s.\n", "Edit Paint");
786
#endif
787
 
788
                dispBuffer = alloca (LEN_MLEDIT_BUFFER+1);
789
 
790
                if (dwStyle & ES_PASSWORD)
791
                    memset (dispBuffer, '*', pLineData->dataEnd);
792
                                    memcpy (dispBuffer,
793
                            pLineData->buffer,  /* +pMLEditData->dispPos, */
794
                                                pLineData->dataEnd);    /* - pMLEditData->dispPos); */
795
                        dispBuffer[pLineData->dataEnd] = '\0';
796
 
797
            /* only implemented ES_LEFT align format for single line edit. */
798
                rect.left = pMLEditData->leftMargin;
799
                rect.top = pMLEditData->topMargin ;
800
                rect.right = pMLEditData->rightMargin;
801
                rect.bottom = pMLEditData->bottomMargin;
802
#if 0
803
                printf("lineNO=%d,lines=%d,editLine=%d\n",pLineData->lineNO,pMLEditData->lines,
804
                        pMLEditData->editLine);
805
                printf("--dispBuffer=%s--\n",dispBuffer);
806
                ClipRectIntersect (hdc, &rect); /* fix: no ClipRectIntersect() */
807
#endif
808
 
809
#ifdef USE_BIG5     
810
            oldfont=SelectObject(hdc,CreateFont(12,
811
                        0,0,0,0,0,0,0,0,0,0,0,
812
                        FF_DONTCARE|DEFAULT_PITCH,
813
                        "HZXFONT"));
814
#endif
815
                TextOut (hdc,
816
                                pMLEditData->leftMargin - pMLEditData->dispPos * GetSysCharWidth(hWnd) ,
817
                                GetSysCharHeight(hWnd)*(pLineData->lineNO - pMLEditData->StartlineDisp)
818
                                        + pMLEditData->topMargin,
819
                                dispBuffer,-1);
820
                        }
821
#ifdef USE_BIG5     
822
            DeleteObject(SelectObject(hdc,oldfont));
823
#endif
824
            EndPaint (hWnd, &ps);
825
        }
826
        break;
827
 
828
        case WM_KEYDOWN:
829
        {
830
            BOOL    bChange = FALSE;
831
            int     i;
832
            int     deleted;
833
                        PLINEDATA temp = NULL;
834
                        char *  tempP = NULL;
835
 
836
            pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
837
 
838
            switch (LOWORD (wParam))
839
            {
840
 
841
                case VK_RETURN:         /* SCANCODE_ENTER: */
842
                                {
843
                                        pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
844
                                        if (pMLEditData->editPos < pLineData->dataEnd)
845
                                                tempP = pLineData->buffer + pMLEditData->editPos;
846
                                    temp = pLineData->next;
847
                                        pLineData->next = malloc( sizeof(LINEDATA) );
848
                                        pLineData->next->previous = pLineData;
849
                                        pLineData->next->next = temp;
850
                                        if(temp)
851
                                        {
852
                                                temp->previous = pLineData->next;
853
                                        }
854
                                        temp = pLineData->next;
855
                                        temp->lineNO  = pMLEditData->editLine + 1;
856
                                        if(tempP)
857
                                        {
858
                                                memcpy(temp->buffer,tempP,strlen(tempP));
859
                                                temp->dataEnd = strlen(tempP);
860
                                        }
861
                                        else
862
                                                temp->dataEnd = 0;
863
                                        temp->buffer[temp->dataEnd] = '\0';
864
                                        pLineData->dataEnd = pMLEditData->editPos;
865
                                        pLineData->buffer[pLineData->dataEnd]='\0';
866
                                        temp = temp->next;
867
                                        while (temp)
868
                                        {
869
                                                temp->lineNO++;
870
                                                temp = temp->next;
871
                                        }
872
                                        pMLEditData->editPos = 0;
873
                                        pMLEditData->caretPos= 0;
874
                                        pMLEditData->dispPos = 0;
875
                                        if(pMLEditData->linesDisp < pMLEditData->MaxlinesDisp)
876
                                        {
877
                                                pMLEditData->EndlineDisp++;
878
                                                pMLEditData->linesDisp++;
879
                                        }
880
                                        else if(pMLEditData->editLine == pMLEditData->EndlineDisp)
881
                                        {
882
                                                pMLEditData->StartlineDisp++;
883
                                                pMLEditData->EndlineDisp++;
884
                                        }
885
                                        pMLEditData->editLine++;
886
                                        pMLEditData->lines++;
887
                    SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
888
                            + pMLEditData->leftMargin,
889
                        (pMLEditData->editLine - pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
890
                                                        +pMLEditData->topMargin);
891
                    InvalidateRect (hWnd, NULL, FALSE);
892
                        return 0;
893
                                }
894
                case VK_HOME:   /* SCANCODE_HOME: */
895
                                {
896
                                        PLINEDATA temp;
897
                    if (pMLEditData->editPos == 0)
898
                        return 0;
899
 
900
                    pMLEditData->editPos  = 0;
901
                    pMLEditData->caretPos = 0;
902
 
903
                    SetCaretPos (pMLEditData->leftMargin,
904
                        (pMLEditData->editLine-pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
905
                                                        +pMLEditData->topMargin);
906
                                        temp = GetLineData(pMLEditData,pMLEditData->editLine);
907
                    if (pMLEditData->dispPos != 0)
908
                                        {
909
                                                pMLEditData->dispPos = 0;
910
                                            InvalidateRect (hWnd, NULL, FALSE);
911
                                        }
912
                        return 0;
913
                }
914
                case VK_END: /* SCANCODE_END: */
915
                {
916
                    int newStartPos;
917
                    pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
918
                    if (pMLEditData->editPos == pLineData->dataEnd)
919
                        return 0;
920
                    newStartPos = edtGetStartDispPosAtEnd (hWnd, pLineData);
921
 
922
                    pMLEditData->editPos = pLineData->dataEnd;
923
                    pMLEditData->caretPos = pMLEditData->editPos - newStartPos;
924
 
925
                    SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
926
                            + pMLEditData->leftMargin,
927
                                                (pMLEditData->editLine-pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
928
                                + pMLEditData->topMargin);
929
 
930
                    if (pMLEditData->dispPos != newStartPos)
931
                        InvalidateRect (hWnd, NULL, FALSE);
932
                                        pMLEditData->dispPos = newStartPos;
933
                }
934
                return 0;
935
 
936
                case VK_LEFT: /* SCANCODE_CURSORBLOCKLEFT: */
937
                {
938
                    BOOL bScroll = FALSE;
939
                    int  scrollStep,newStartPos;
940
                                        PLINEDATA temp;
941
                                        pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
942
                    if (pMLEditData->editPos == 0 )
943
                                        {
944
                                                temp = pLineData->previous;
945
                                                if(temp && pMLEditData->editLine > pMLEditData->StartlineDisp)
946
                                                {
947
                                                        pMLEditData->editLine --;
948
                                                        pMLEditData->editPos = temp->dataEnd;
949
                                    newStartPos = edtGetStartDispPosAtEnd (hWnd, temp);
950
                                pMLEditData->caretPos = pMLEditData->editPos - newStartPos;
951
                                if (pMLEditData->dispPos != newStartPos)
952
                                                        {
953
                                                                pMLEditData->dispPos = newStartPos;
954
                                                                bScroll = TRUE;
955
                                                        }
956
                                                }
957
                                                else
958
                                return 0;
959
                                        }
960
                                        else
961
                    {   if (edtIsACCharBeforePosition (pLineData->buffer,
962
                                    pLineData->dataEnd,
963
                                    pMLEditData->editPos)) {
964
                                scrollStep = 2;
965
                                pMLEditData->editPos -= 2;
966
                        }
967
                        else {
968
                                scrollStep = 1;
969
                                pMLEditData->editPos --;
970
                        }
971
 
972
                        pMLEditData->caretPos -= scrollStep;
973
                        if (pMLEditData->caretPos == 0
974
                                    && pMLEditData->editPos != 0) {
975
 
976
                                bScroll = TRUE;
977
 
978
                                if (edtIsACCharBeforePosition (pLineData->buffer,
979
                                    pLineData->dataEnd,
980
                                    pMLEditData->editPos)) {
981
                                        pMLEditData->dispPos -= 2;
982
                                pMLEditData->caretPos = 2;
983
                                }
984
                                else {
985
                                                                pMLEditData->dispPos--;
986
                                    pMLEditData->caretPos = 1;
987
                            }
988
                            }
989
                        else if (pMLEditData->caretPos < 0) {
990
                                                        pMLEditData->dispPos = 0;
991
                            pMLEditData->caretPos = 0;
992
                        }
993
                    }
994
                    SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
995
                            + pMLEditData->leftMargin,
996
                                                (pMLEditData->editLine - pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
997
                                + pMLEditData->topMargin);
998
 
999
                    if (bScroll)
1000
                        InvalidateRect (hWnd, NULL, FALSE);
1001
                }
1002
                return 0;
1003
 
1004
                case VK_RIGHT: /* SCANCODE_CURSORBLOCKRIGHT: */
1005
                {
1006
                    BOOL bScroll = FALSE;
1007
                    int  scrollStep, moveStep;
1008
                                        PLINEDATA temp;
1009
 
1010
                                        pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
1011
                    if (pMLEditData->editPos == pLineData->dataEnd)
1012
                                        {
1013
                                                temp = pLineData->next;
1014
                                                if(temp)
1015
                                                {
1016
                                                        pMLEditData->editLine++;
1017
                                                        pMLEditData->editPos  = 0;
1018
                                                        pMLEditData->caretPos = 0;
1019
                                                        if(pMLEditData->dispPos !=0)
1020
                                                        {
1021
                                                                pMLEditData->dispPos  = 0;
1022
                                                                bScroll = TRUE;
1023
                                                        }
1024
                                                }
1025
                                                else
1026
                                return 0;
1027
                                        }
1028
                                        else
1029
                    {
1030
                                                if (edtIsACCharAtPosition (pLineData->buffer,
1031
                                        pLineData->dataEnd,
1032
                                    pMLEditData->dispPos)) {
1033
                            if (edtIsACCharAtPosition (pLineData->buffer,
1034
                                    pLineData->dataEnd,
1035
                                    pMLEditData->editPos)) {
1036
                                scrollStep = 2;
1037
                                    moveStep = 2;
1038
                                pMLEditData->editPos  += 2;
1039
                                }
1040
                                else {
1041
                                scrollStep = 2;
1042
                                    moveStep = 1;
1043
                                pMLEditData->editPos ++;
1044
                                }
1045
                        }
1046
                            else {
1047
                            if (edtIsACCharAtPosition (pLineData->buffer,
1048
                                            pLineData->dataEnd,
1049
                                        pMLEditData->editPos)) {
1050
 
1051
                                    if (edtIsACCharAtPosition (pLineData->buffer,
1052
                                        pLineData->dataEnd,
1053
                                            pMLEditData->dispPos + 1))
1054
                                    scrollStep = 3;
1055
                                    else
1056
                                    scrollStep = 2;
1057
 
1058
                                moveStep = 2;
1059
                                    pMLEditData->editPos += 2;
1060
                            }
1061
                                else {
1062
                                scrollStep = 1;
1063
                                    moveStep = 1;
1064
                                pMLEditData->editPos ++;
1065
                                }
1066
                        }
1067
 
1068
                            pMLEditData->caretPos += moveStep;
1069
                        if (pMLEditData->caretPos * GetSysCharWidth (hWnd)
1070
                                    > edtGetOutWidth (hWnd)) {
1071
                            bScroll = TRUE;
1072
                                pMLEditData->dispPos += scrollStep;
1073
                                pMLEditData->caretPos =
1074
                                pMLEditData->editPos - pMLEditData->dispPos;
1075
                            }
1076
                                        }
1077
                    SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
1078
                            + pMLEditData->leftMargin,
1079
                        (pMLEditData->editLine - pMLEditData->StartlineDisp) * GetSysCharHeight (hWnd)
1080
                                + pMLEditData->topMargin);
1081
 
1082
                    if (bScroll)
1083
                        InvalidateRect (hWnd, NULL, FALSE);
1084
                }
1085
                return 0;
1086
 
1087
                case VK_UP: /* SCANCODE_CURSORBLOCKUP: */
1088
                {
1089
                    BOOL bScroll = FALSE;
1090
                    int  newStartPos;
1091
                                        PLINEDATA temp;
1092
                                        pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
1093
                                        temp = pLineData->previous;
1094
                                        if(pMLEditData->editLine == 0)
1095
                                                return 0;
1096
                                        else if (pMLEditData->editLine == pMLEditData->StartlineDisp)
1097
                                        {
1098
                                                bScroll = TRUE;
1099
                                                pMLEditData->StartlineDisp--;
1100
                                                pMLEditData->EndlineDisp--;
1101
                                        }
1102
                                        pMLEditData->editLine--;
1103
 
1104
                                        if( pMLEditData->editPos >= temp->dataEnd )
1105
                                        {
1106
                                                pMLEditData->editPos = temp->dataEnd;
1107
                                                pMLEditData->dispPos = 0;
1108
                                newStartPos = edtGetStartDispPosAtEnd (hWnd, temp);
1109
                                pMLEditData->dispPos =  newStartPos;
1110
                                                pMLEditData->caretPos = pMLEditData->editPos - newStartPos;
1111
                                                bScroll = TRUE;
1112
                                        }
1113
                                        else
1114
                                        {
1115
                                        newStartPos = edtGetOffset(hWnd, pMLEditData,temp,
1116
                                                pMLEditData->caretPos * GetSysCharWidth (hWnd)
1117
                                        + pMLEditData->leftMargin);
1118
                                                if(!edtIsACCharFromBegin(temp->buffer,temp->dataEnd,
1119
                                                                pMLEditData->dispPos))
1120
                                                {
1121
                                                        bScroll = TRUE;
1122
                                                        pMLEditData->dispPos--;
1123
                                                newStartPos = edtGetOffset(hWnd, pMLEditData,temp,
1124
                                                pMLEditData->caretPos * GetSysCharWidth (hWnd)
1125
                                                + pMLEditData->leftMargin+GetSysCharWidth(hWnd)/2);
1126
                                                }
1127
                                    pMLEditData->editPos = newStartPos + pMLEditData->dispPos;
1128
                                pMLEditData->caretPos = newStartPos;
1129
                                        }
1130
                    SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
1131
                            + pMLEditData->leftMargin,
1132
                                            (pMLEditData->editLine - pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
1133
                            + pMLEditData->topMargin);
1134
                                        if(bScroll)
1135
                                                InvalidateRect(hWnd,NULL,FALSE);
1136
                                }
1137
                                break;
1138
                case VK_DOWN: /* SCANCODE_CURSORBLOCKDOWN: */
1139
                {
1140
                    BOOL bScroll = FALSE;
1141
                    int  newStartPos;
1142
                                        PLINEDATA temp;
1143
                                        pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
1144
                                        temp = pLineData->next;
1145
                                        if(pMLEditData->editLine == pMLEditData->lines-1)
1146
                                                return 0;
1147
                                        else if (pMLEditData->editLine == pMLEditData->EndlineDisp)
1148
                                        {
1149
                                                bScroll = TRUE;
1150
                                                pMLEditData->StartlineDisp++;
1151
                                                pMLEditData->EndlineDisp++;
1152
                                        }
1153
                                        pMLEditData->editLine++;
1154
 
1155
                                        if( pMLEditData->editPos >= temp->dataEnd )
1156
                                        {
1157
                                                pMLEditData->editPos = temp->dataEnd;
1158
                                                pMLEditData->dispPos = 0;
1159
                                newStartPos = edtGetStartDispPosAtEnd (hWnd, temp);
1160
                                pMLEditData->dispPos =  newStartPos;
1161
                                                pMLEditData->caretPos = pMLEditData->editPos - newStartPos;
1162
                                                bScroll = TRUE;
1163
                                        }
1164
                                        else
1165
                                        {
1166
                                        newStartPos = edtGetOffset(hWnd, pMLEditData,temp,
1167
                                                pMLEditData->caretPos * GetSysCharWidth (hWnd)
1168
                                        + pMLEditData->leftMargin);
1169
                                                if(!edtIsACCharFromBegin(temp->buffer,temp->dataEnd,
1170
                                                                pMLEditData->dispPos))
1171
                                                {
1172
                                                        bScroll = TRUE;
1173
                                                        pMLEditData->dispPos--;
1174
                                                newStartPos = edtGetOffset(hWnd, pMLEditData,temp,
1175
                                                pMLEditData->caretPos * GetSysCharWidth (hWnd)
1176
                                                + pMLEditData->leftMargin+GetSysCharWidth(hWnd)/2);
1177
                                                }
1178
                                    pMLEditData->editPos = newStartPos + pMLEditData->dispPos;
1179
                                pMLEditData->caretPos = newStartPos;
1180
                                        }
1181
                    SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
1182
                            + pMLEditData->leftMargin,
1183
                                            (pMLEditData->editLine - pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
1184
                            + pMLEditData->topMargin);
1185
                                        if(bScroll)
1186
                                                InvalidateRect(hWnd,NULL,FALSE);
1187
 
1188
                                }
1189
                                break;
1190
                case VK_INSERT: /* SCANCODE_INSERT: */
1191
                                        dw = GetWindowAdditionalData(hWnd);
1192
                    dw ^= EST_REPLACE;
1193
                                        SetWindowAdditionalData(hWnd,dw);
1194
                break;
1195
 
1196
                case VK_DELETE: /* SCANCODE_REMOVE: */
1197
                                {
1198
                                        PLINEDATA temp;
1199
                                        int leftLen;
1200
                                        pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
1201
                    if ((GetWindowAdditionalData(hWnd) & EST_READONLY) ){
1202
#if 0   /* fix: no ping() */
1203
                        ping ();
1204
#endif
1205
                        return 0;
1206
                    }
1207
                        temp = pLineData->next;
1208
                                        if (pLineData->dataEnd == pMLEditData->editPos && temp)
1209
                                        {
1210
                                                if(pLineData->dataEnd + temp->dataEnd <= LEN_MLEDIT_BUFFER)
1211
                                                {
1212
                                                        memcpy(pLineData->buffer+pLineData->dataEnd,temp->buffer,temp->dataEnd);
1213
                                                        pLineData->dataEnd += temp->dataEnd;
1214
                                                        pLineData->buffer[pLineData->dataEnd] = '\0';
1215
                                                        if(temp->next)
1216
                                                        {
1217
                                                            pLineData->next = temp->next;
1218
                                                                temp->next->previous = pLineData;
1219
                                                        }
1220
                                                        else
1221
                                                                pLineData->next = NULL;
1222
                                                        free(temp);
1223
                                                        temp = pLineData->next;
1224
                                                        while (temp)
1225
                                                        {
1226
                                                                temp->lineNO--;
1227
                                                                temp = temp->next;
1228
                                                        }
1229
                                                        if(pMLEditData->lines <= pMLEditData->MaxlinesDisp)
1230
                                                        {
1231
                                                                pMLEditData->EndlineDisp--;
1232
                                                                pMLEditData->linesDisp--;
1233
                                                        }
1234
                                                        if(pMLEditData->EndlineDisp >= pMLEditData->lines-1)
1235
                                                        {
1236
                                                                pMLEditData->EndlineDisp--;
1237
                                                                if(pMLEditData->StartlineDisp !=0)
1238
                                                                        pMLEditData->StartlineDisp--;
1239
                                                                else
1240
                                                                        pMLEditData->linesDisp--;
1241
                                                        }
1242
                                                        pMLEditData->lines--;
1243
                                                }
1244
                                                else if (temp->dataEnd > 0)
1245
                                                {
1246
                                                        leftLen = LEN_MLEDIT_BUFFER - pLineData->dataEnd;
1247
                                                        memcpy(pLineData->buffer+pLineData->dataEnd,temp->buffer,leftLen);
1248
                                                        pLineData->dataEnd +=leftLen;
1249
                                                        pLineData->buffer[pLineData->dataEnd] = '\0';
1250
                                                        memcpy(temp->buffer,temp->buffer+leftLen,temp->dataEnd-leftLen);
1251
                                                        temp->dataEnd -=leftLen;
1252
                                                        temp->buffer[temp->dataEnd] = '\0';
1253
                                                }
1254
                                        }
1255
                                        else if (pMLEditData->editPos != pLineData->dataEnd)
1256
                                        {
1257
                            if (edtIsACCharAtPosition (pLineData->buffer,
1258
                                        pLineData->dataEnd,
1259
                                            pMLEditData->editPos))
1260
                                deleted = 2;
1261
                        else
1262
                                deleted = 1;
1263
 
1264
                        for (i = pMLEditData->editPos;
1265
                                    i < pLineData->dataEnd - deleted;
1266
                                i++)
1267
                               pLineData->buffer [i]
1268
                                = pLineData->buffer [i + deleted];
1269
 
1270
                            pLineData->dataEnd -= deleted;
1271
                                                pLineData->buffer[pLineData->dataEnd] = '\0';
1272
                                        }
1273
                        bChange = TRUE;
1274
                    InvalidateRect (hWnd, NULL,FALSE);
1275
                                }
1276
                break;
1277
 
1278
                case VK_BACK: /* SCANCODE_BACKSPACE: */
1279
                                {
1280
                                        PLINEDATA temp;
1281
                                        int leftLen,tempEnd;
1282
                    if ((GetWindowAdditionalData(hWnd) & EST_READONLY) ){
1283
#if 0    /* fix: no Ping() */
1284
                        Ping ();
1285
#endif
1286
                        return 0;
1287
                    }
1288
                                        pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
1289
                                        temp = pLineData->previous;
1290
                                        if (pMLEditData->editPos == 0 && temp)
1291
                                        {
1292
                                                tempEnd = temp->dataEnd;
1293
                                                if(pLineData->dataEnd + temp->dataEnd <= LEN_MLEDIT_BUFFER)
1294
                                                {
1295
                                                        memcpy(temp->buffer+temp->dataEnd,pLineData->buffer,pLineData->dataEnd);
1296
                                                        temp->dataEnd +=pLineData->dataEnd;
1297
                                                        temp->buffer[temp->dataEnd] = '\0';
1298
                                                        if(pLineData->next)
1299
                                                        {
1300
                                                            temp->next = pLineData->next;
1301
                                                                pLineData->next->previous = temp;
1302
                                                        }
1303
                                                        else
1304
                                                                temp->next = NULL;
1305
                                                        free(pLineData);
1306
                                                        pLineData = temp;
1307
                                                        temp = temp->next;
1308
                                                        while(temp)
1309
                                                        {
1310
                                                                temp->lineNO--;
1311
                                                                temp = temp->next;
1312
                                                        }
1313
                                                        if(pMLEditData->StartlineDisp == pMLEditData->editLine
1314
                                                                        && pMLEditData->StartlineDisp != 0)
1315
                                                        {
1316
                                                                pMLEditData->StartlineDisp--;
1317
                                                                if(pMLEditData->EndlineDisp == pMLEditData->lines)
1318
                                                                        pMLEditData->EndlineDisp--;
1319
                                                        }
1320
                                                        if(pMLEditData->lines <= pMLEditData->MaxlinesDisp)
1321
                                                        {
1322
                                                                pMLEditData->linesDisp--;
1323
                                                                pMLEditData->EndlineDisp--;
1324
                                                        }
1325
                                                        pMLEditData->lines--;
1326
                                                }
1327
                                                else if (pLineData->dataEnd > 0)
1328
                                                {
1329
                                                        leftLen = LEN_MLEDIT_BUFFER - temp->dataEnd;
1330
                                                        memcpy(temp->buffer+temp->dataEnd,pLineData->buffer,leftLen);
1331
                                                        temp->dataEnd +=leftLen;
1332
                                                        temp->buffer[temp->dataEnd] = '\0';
1333
                                                        memcpy(pLineData->buffer,pLineData->buffer+leftLen,pLineData->dataEnd-leftLen);
1334
                                                        pLineData->dataEnd -=leftLen;
1335
                                                        pLineData->buffer[pLineData->dataEnd] = '\0';
1336
                                                }
1337
                                                pMLEditData->editLine--;
1338
                                                pMLEditData->editPos = tempEnd;
1339
                                                pMLEditData->dispPos = tempEnd;
1340
                                                /* µ±±à¼­Î»Öò»Îª0,caretλÖÃΪ0µÄʱºò,Òƶ¯caretλÖÃ. */
1341
                                if (pMLEditData->caretPos == 0
1342
                                    && pMLEditData->editPos != 0) {
1343
                                if (edtIsACCharBeforePosition (pLineData->buffer,
1344
                                    pLineData->dataEnd,
1345
                                    pMLEditData->editPos)) {
1346
                                        pMLEditData->dispPos -= 2;
1347
                                pMLEditData->caretPos = 2;
1348
                                    }
1349
                                else {
1350
                                                                pMLEditData->dispPos--;
1351
                                        pMLEditData->caretPos = 1;
1352
                            }
1353
                                }
1354
                        else if (pMLEditData->caretPos < 0) {
1355
                                                        pMLEditData->dispPos = 0;
1356
                                pMLEditData->caretPos = 0;
1357
                        }
1358
                                        }
1359
                                        else if (pMLEditData->editPos != 0 )
1360
                                        {
1361
                            if (edtIsACCharBeforePosition (pLineData->buffer,
1362
                                        pLineData->dataEnd,
1363
                                        pMLEditData->editPos))
1364
                                deleted = 2;
1365
                        else
1366
                                deleted = 1;
1367
 
1368
                        for (i = pMLEditData->editPos;
1369
                                    i < pLineData->dataEnd;
1370
                                i++)
1371
                                pLineData->buffer [i - deleted]
1372
                                = pLineData->buffer [i];
1373
 
1374
                            pLineData->dataEnd -= deleted;
1375
                        pMLEditData->editPos -= deleted;
1376
                            pMLEditData->caretPos -= deleted;
1377
                            if (pMLEditData->caretPos == 0
1378
                                && pMLEditData->editPos != 0) {
1379
                                if (edtIsACCharBeforePosition (pLineData->buffer,
1380
                                    pLineData->dataEnd,
1381
                                    pMLEditData->editPos)) {
1382
                                    pMLEditData->dispPos -= 2;
1383
                                pMLEditData->caretPos = 2;
1384
                                 }
1385
                            else {
1386
                                    pMLEditData->dispPos --;
1387
                                pMLEditData->caretPos = 1;
1388
                            }
1389
 
1390
                            }
1391
                                        }
1392
                    bChange = TRUE;
1393
                    SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
1394
                            + pMLEditData->leftMargin,
1395
                                            (pMLEditData->editLine - pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
1396
                            + pMLEditData->topMargin);
1397
                    InvalidateRect (hWnd, NULL, FALSE);
1398
                                }
1399
                break;
1400
 
1401
                default:
1402
                break;
1403
            }
1404
 
1405
            if (bChange)
1406
                SendMessage (GetParent (hWnd), WM_COMMAND,
1407
                        (WPARAM) MAKELONG (GetDlgCtrlID(hWnd), EN_CHANGE),
1408
                        (LPARAM) hWnd);
1409
            return 0;
1410
        }
1411
 
1412
        case WM_CHAR:
1413
        {
1414
            char charBuffer [2];
1415
            int  i, chars, scrollStep, inserting;
1416
                        UINT format;
1417
 
1418
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1419
 
1420
                        pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
1421
 
1422
            if (dwStyle & ES_READONLY) {
1423
#if 0    /* fix: no Ping() */
1424
                Ping ();
1425
#endif
1426
                return 0;
1427
            }
1428
            if (HIBYTE (wParam)) {
1429
                charBuffer [0] = LOBYTE (wParam);
1430
                charBuffer [1] = HIBYTE (wParam);
1431
                chars = 2;
1432
            }
1433
            else {
1434
                charBuffer [0] = LOBYTE (wParam);
1435
                chars = 1;
1436
            }
1437
 
1438
            if (chars == 1) {
1439
                switch (charBuffer [0])
1440
                {
1441
                    case 0x00:  /* NULL */
1442
                    case 0x07:  /* BEL */
1443
                    case 0x08:  /* BS */
1444
                    case 0x09:  /* HT */
1445
                    case 0x0A:  /* LF */
1446
                    case 0x0B:  /* VT */
1447
                    case 0x0C:  /* FF */
1448
                    case 0x0D:  /* CR */
1449
                    case 0x1B:  /* Escape */
1450
                    return 0;
1451
                }
1452
            }
1453
            if (GetWindowAdditionalData(hWnd) & EST_REPLACE) {
1454
                if (pLineData->dataEnd == pMLEditData->editPos)
1455
                    inserting = chars;
1456
                else if (edtIsACCharAtPosition (pLineData->buffer,
1457
                                pLineData->dataEnd,
1458
                                pMLEditData->editPos)) {
1459
                    if (chars == 2)
1460
                        inserting = 0;
1461
                    else
1462
                        inserting = -1;
1463
                }
1464
                else {
1465
                    if (chars == 2)
1466
                        inserting = 1;
1467
                    else
1468
                        inserting = 0;
1469
                }
1470
            }
1471
            else
1472
                inserting = chars;
1473
 
1474
            /* check space */
1475
            if (pLineData->dataEnd + inserting > pMLEditData->totalLen) {
1476
#if 0   /* fix no ping */
1477
                Ping ();
1478
#endif
1479
                SendMessage (GetParent (hWnd), WM_COMMAND,
1480
                            (WPARAM) MAKELONG (GetDlgCtrlID(hWnd), EN_MAXTEXT),
1481
                            (LPARAM) hWnd);
1482
                return 0;
1483
            }
1484
            else if ((pMLEditData->hardLimit >= 0)
1485
                        && ((pLineData->dataEnd + inserting)
1486
                            > pMLEditData->hardLimit)) {
1487
#if 0   /* fix no ping */
1488
                Ping ();
1489
#endif
1490
                SendMessage (GetParent (hWnd), WM_COMMAND,
1491
                            (WPARAM) MAKELONG (GetDlgCtrlID(hWnd), EN_MAXTEXT),
1492
                            (LPARAM) hWnd);
1493
                return 0;
1494
            }
1495
            if (inserting == -1) {
1496
                for (i = pMLEditData->editPos; i < pLineData->dataEnd-1; i++)
1497
                    pLineData->buffer [i] = pLineData->buffer [i + 1];
1498
            }
1499
            else if (inserting > 0) {
1500
                for (i = pLineData->dataEnd + inserting - 1;
1501
                        i > pMLEditData->editPos + inserting - 1;
1502
                        i--)
1503
                    pLineData->buffer [i]
1504
                            = pLineData->buffer [i - inserting];
1505
            }
1506
            for (i = 0; i < chars; i++)
1507
                    pLineData->buffer [pMLEditData->editPos + i]
1508
                        = charBuffer [i];
1509
 
1510
            pMLEditData->editPos += chars;
1511
            pMLEditData->caretPos += chars;
1512
            pLineData->dataEnd += inserting;
1513
                        pLineData->buffer[pLineData->dataEnd] = '\0';
1514
            if (pMLEditData->caretPos * GetSysCharWidth (hWnd)
1515
                            > edtGetOutWidth (hWnd))
1516
            {
1517
                if (edtIsACCharAtPosition (pLineData->buffer,
1518
                                pLineData->dataEnd,
1519
                                pMLEditData->dispPos))
1520
                    scrollStep = 2;
1521
                else {
1522
                    if (chars == 2) {
1523
                        if (edtIsACCharAtPosition (pLineData->buffer,
1524
                                pLineData->dataEnd,
1525
                                pMLEditData->dispPos + 1))
1526
                            scrollStep = 3;
1527
                        else
1528
                            scrollStep = 2;
1529
                    }
1530
                    else
1531
                        scrollStep = 1;
1532
                }
1533
 
1534
                pMLEditData->dispPos += scrollStep;
1535
 
1536
                pMLEditData->caretPos =
1537
                            pMLEditData->editPos - pMLEditData->dispPos;
1538
 
1539
            }
1540
            SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
1541
                            + pMLEditData->leftMargin,
1542
                                            (pMLEditData->editLine - pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
1543
                            + pMLEditData->topMargin);
1544
            InvalidateRect (hWnd, NULL,FALSE);
1545
                        format = DT_NOPREFIX;
1546
            SendMessage (GetParent (hWnd), WM_COMMAND,
1547
                    (WPARAM) MAKELONG (GetDlgCtrlID(hWnd), EN_CHANGE),
1548
                    (LPARAM) hWnd);
1549
        }
1550
        return 0;
1551
 
1552
        case WM_GETTEXTLENGTH:
1553
                {
1554
                        PLINEDATA temp;
1555
                        int    lineNO = (int)wParam;
1556
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1557
                        temp = pMLEditData->head;
1558
                        while(temp)
1559
                        {
1560
                                if (temp->lineNO == lineNO)
1561
                                        return  temp->dataEnd;
1562
                                temp = temp->next;
1563
                        }
1564
        return -1;
1565
        }
1566
                case WM_GETTEXT:
1567
                {
1568
                        PLINEDATA temp;
1569
                        int len,total = 0,lineNO;
1570
                        char * buffer = (char*)lParam;
1571
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1572
                        len = (int)wParam;
1573
            lineNO = (int)wParam;
1574
                        temp = pMLEditData->head;
1575
                        while (temp && total + temp->dataEnd < len)
1576
                        {
1577
                                memcpy(buffer+total,temp->buffer,temp->dataEnd);
1578
                                total += temp->dataEnd;
1579
                                temp = temp->next;
1580
                        }
1581
 
1582
                }
1583
                return 0;
1584
/* can i add it to message define ? */
1585
#if 0
1586
        case WM_GETLINETEXT:
1587
        {
1588
                        PLINEDATA temp;
1589
            char*   buffer = (char*)lParam;
1590
            int     lineNO,len;
1591
 
1592
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1593
            lineNO = (int)wParam;
1594
                        temp = GetLineData(pMLEditData,lineNO);
1595
                        if(temp)
1596
                        {
1597
                                len = min ((int)wParam, temp->dataEnd);
1598
                        memcpy (buffer, temp->buffer,len);
1599
                        buffer [len] = '\0';
1600
                                return 0;
1601
                        }
1602
            return -1;
1603
        }
1604
        break;
1605
                case WM_SETTEXT:
1606
                {
1607
                        MLEditInitBuffer(pMLEditData,(char *)lParam);
1608
                }
1609
                return 0;
1610
#endif
1611
/* can i add it to message defined? */
1612
#if 0
1613
        case WM_SETLINETEXT:
1614
        {
1615
            int len,lineNO;
1616
                        PLINEDATA temp;
1617
 
1618
            if (dwStyle & ES_READONLY)
1619
                return 0;
1620
 
1621
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1622
 
1623
            len = strlen ((char*)lParam);
1624
                        lineNO = (int)wParam;
1625
                        temp = pMLEditData->head;
1626
            len = min (len, pMLEditData->totalLen);
1627
 
1628
            if (pMLEditData->hardLimit >= 0)
1629
                len = min (len, pMLEditData->hardLimit);
1630
                while (temp)
1631
                        {
1632
                                if(temp->lineNO == lineNO)
1633
                                {
1634
                        temp->dataEnd = len;
1635
                    memcpy (temp->buffer, (char*)lParam, len);
1636
                                }
1637
                                temp = temp->next;
1638
                        }
1639
            pMLEditData->editPos        = 0;
1640
            pMLEditData->caretPos       = 0;
1641
            pMLEditData->dispPos        = 0;
1642
            InvalidateRect (hWnd, NULL, FALSE);
1643
        }
1644
        return 0;
1645
#endif
1646
        case WM_LBUTTONDBLCLK:
1647
        break;
1648
 
1649
        case WM_LBUTTONDOWN:
1650
        {
1651
            int newOff,lineNO;
1652
                        PLINEDATA temp;
1653
                        BOOL bScroll = FALSE;
1654
 
1655
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1656
            lineNO = edtGetLineNO (hWnd,pMLEditData, HIWORD (lParam));
1657
                        if ( lineNO < 0 )
1658
                                return 0;
1659
                        lineNO += pMLEditData->StartlineDisp;
1660
                        if (lineNO <= pMLEditData->EndlineDisp && lineNO <= pMLEditData->lines-1 )
1661
                        {
1662
                                temp = GetLineData(pMLEditData,lineNO);
1663
                        newOff = edtGetOffset (hWnd,pMLEditData,temp, LOWORD (lParam));
1664
                                if(!edtIsACCharFromBegin(temp->buffer,temp->dataEnd,pMLEditData->dispPos))
1665
                                {
1666
                                        bScroll = TRUE;
1667
                                        pMLEditData->dispPos--;
1668
                                newOff = edtGetOffset (hWnd,pMLEditData,temp, LOWORD(lParam)+GetSysCharWidth(hWnd)/2);
1669
                                }
1670
                if (newOff != pMLEditData->caretPos || lineNO != pMLEditData->editLine) {
1671
                                        pMLEditData->editLine = temp->lineNO;
1672
                            pMLEditData->editPos = newOff +pMLEditData->dispPos;
1673
                        pMLEditData->caretPos = newOff;
1674
                            SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
1675
                            + pMLEditData->leftMargin,
1676
                                        (pMLEditData->editLine - pMLEditData->StartlineDisp) * GetSysCharHeight(hWnd)
1677
                                        + pMLEditData->topMargin);
1678
                        }
1679
                                if(bScroll)
1680
                                        InvalidateRect(hWnd,NULL,FALSE);
1681
                        }
1682
        }
1683
        break;
1684
 
1685
        case WM_LBUTTONUP:
1686
        break;
1687
 
1688
        case WM_MOUSEMOVE:
1689
        break;
1690
 
1691
        case WM_GETDLGCODE:
1692
        return DLGC_WANTCHARS | DLGC_HASSETSEL | DLGC_WANTARROWS;
1693
 
1694
        case EM_SETREADONLY:
1695
            if (wParam)
1696
                                IncludeWindowStyle(hWnd,ES_READONLY);
1697
            else
1698
                                ExcludeWindowStyle(hWnd,ES_READONLY);
1699
        return 0;
1700
 
1701
        case EM_SETPASSWORDCHAR:
1702
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1703
 
1704
            if (pMLEditData->passwdChar != (int)wParam) {
1705
                if (dwStyle & ES_PASSWORD) {
1706
                    pMLEditData->passwdChar = (int)wParam;
1707
                    InvalidateRect (hWnd, NULL, TRUE);
1708
                }
1709
            }
1710
        return 0;
1711
 
1712
        case EM_GETPASSWORDCHAR:
1713
        {
1714
            int* passwdchar;
1715
 
1716
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1717
            passwdchar = (int*) lParam;
1718
 
1719
            *passwdchar = pMLEditData->passwdChar;
1720
        }
1721
        return 0;
1722
 
1723
        case EM_LIMITTEXT:
1724
        {
1725
            int newLimit = (int)wParam;
1726
 
1727
            if (newLimit >= 0) {
1728
            pMLEditData = (PMLEDITDATA)GetWindowAdditionalData2(hWnd);
1729
                if (pMLEditData->totalLen < newLimit)
1730
                    pMLEditData->hardLimit = -1;
1731
                else
1732
                    pMLEditData->hardLimit = newLimit;
1733
            }
1734
        }
1735
        return 0;
1736
 
1737
        default:
1738
                return DefWindowProc (hWnd, message, wParam, lParam);
1739
        break;
1740
    }
1741
 
1742
    return 0;    /* !DefaultControlProc (hWnd, message, wParam, lParam); */
1743
}
1744
 

powered by: WebSVN 2.1.0

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