forked from RealDeuce/OpenDoors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathODEdit.c
More file actions
2650 lines (2334 loc) · 97.6 KB
/
Copy pathODEdit.c
File metadata and controls
2650 lines (2334 loc) · 97.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* OpenDoors Online Software Programming Toolkit
* (C) Copyright 1991 - 1999 by Brian Pirie.
*
* Oct-2001 door32.sys/socket modifications by Rob Swindell (www.synchro.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* File: ODEdit.c
*
* Description: Implementation of the OpenDoors multi-line editor, which
* allows the user to edit strings which may span many lines.
* Provides standard text editor features, such as word wrap.
*
* Revisions: Date Ver Who Change
* ---------------------------------------------------------------
* Dec 07, 1995 6.00 BP Created.
* Dec 12, 1995 6.00 BP Added entry, exit and kernel macros.
* Dec 30, 1995 6.00 BP Added ODCALL for calling convention.
* Jan 04, 1996 6.00 BP Use od_get_input().
* Jan 12, 1996 6.00 BP Claim exclusive use of arrow keys.
* Jan 31, 1996 6.00 BP Added timeout for od_get_input().
* Feb 08, 1996 6.00 BP Finished implementation details.
* Feb 13, 1996 6.00 BP Added od_get_input() flags parameter.
* Feb 16, 1996 6.00 BP New trans. size estimation heuristics.
* Feb 19, 1996 6.00 BP Changed version number to 6.00.
* Feb 24, 1996 6.00 BP Fixed garbage on [Enter] after w-wrap.
* Mar 03, 1996 6.10 BP Begin version 6.10.
* Mar 13, 1996 6.10 BP Restore cursor position after menu.
* Mar 19, 1996 6.10 BP MSVC15 source-level compatibility.
* Jun 08, 1996 6.10 BP Added cast in call to alloc function.
* Oct 19, 2001 6.20 RS Eliminated MSVC 6.0 warning.
* Aug 10, 2003 6.23 SH *nix support
*/
#define BUILDING_OPENDOORS
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "OpenDoor.h"
#include "ODTypes.h"
#include "ODGen.h"
#include "ODCore.h"
#include "ODKrnl.h"
#include "ODStat.h"
#include "ODCom.h"
#include "ODScrn.h"
/* ========================================================================= */
/* Misc. definitions. */
/* ========================================================================= */
/* Macros used by this module. */
#define IS_EOL_CHAR(ch) ((ch) == '\n' || (ch) == '\r' || (ch) == '\0')
/* Configurable constants. */
#define LINE_ARRAY_GROW_SIZE 20
#define BUFFER_GROW_SIZE 4096
#define ANSI_SCROLL_DISTANCE 7
#define PRE_DRAIN_TIME 10000
#define MAX_TAB_STOP_SIZE 8
#define DEFAULT_TAB_STOP_SIZE 8
#define DEFAULT_LINE_BREAK "\n"
/* Other manifest constants. */
#define REDRAW_NO_BOUNDARY 0xffff
/* Default editor options. */
static tODEditOptions ODEditOptionsDefault =
{
1, 1, 80, 23,
FORMAT_PARAGRAPH_BREAKS,
NULL,
NULL,
EFLAG_NORMAL,
};
/* ========================================================================= */
/* Multiline editor instance structure. */
/* ========================================================================= */
typedef struct
{
char *pszEditBuffer;
UINT unBufferSize;
tODEditOptions *pUserOptions;
UINT unCurrentLine;
UINT unCurrentColumn;
UINT unLineScrolledToTop;
UINT unAreaWidth;
UINT unAreaHeight;
char **papchStartOfLine;
UINT unLineArraySize;
UINT unLinesInBuffer;
BOOL bInsertMode;
UINT unTabStopSize;
UINT unScrollDistance;
char *pszLineBreak;
char *pszParagraphBreak;
BOOL bWordWrapLongLines;
void *pRememberBuffer;
} tEditInstance;
/* ========================================================================= */
/* Editor function prototypes. */
/* ========================================================================= */
/* High level implementation. */
static BOOL ODEditSetupInstance(tEditInstance *pEditInstance,
char *pszBufferToEdit, UINT unBufferSize, tODEditOptions *pUserOptions);
static void ODEditRedrawArea(tEditInstance *pEditInstance);
static void ODEditDrawAreaLine(tEditInstance *pEditInstance,
UINT unAreaLineToDraw);
static INT ODEditMainLoop(tEditInstance *pEditInstance);
static void ODEditGotoPreviousLine(tEditInstance *pEditInstance);
static void ODEditGotoNextLine(tEditInstance *pEditInstance);
static BOOL ODEditScrollArea(tEditInstance *pEditInstance, INT nDistance);
static BOOL ODEditRecommendFullRedraw(tEditInstance *pEditInstance,
UINT unEstPartialRedrawBytes, BOOL bDefault);
static UINT ODEditEstDrawBytes(tEditInstance *pEditInstance,
UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
UINT unFinishRedrawColumn);
static UINT ODEditGetCurrentLineInArea(tEditInstance *pEditInstance);
static void ODEditUpdateCursorPos(tEditInstance *pEditInstance);
static void ODEditUpdateCursorIfMoved(tEditInstance *pEditInstance);
static tODResult ODEditEnterText(tEditInstance *pEditInstance,
char *pszEntered, BOOL bInsertMode);
static void ODEditSetBreakSequence(tEditInstance *pEditInstance,
char chFirstEOLChar, char chSecondEOLChar);
static BOOL ODEditCursorLeft(tEditInstance *pEditInstance);
static void ODEditDeleteCurrentChar(tEditInstance *pEditInstance);
static void ODEditDeleteCurrentLine(tEditInstance *pEditInstance);
static BOOL ODEditPastEndOfCurLine(tEditInstance *pEditInstance);
static size_t ODEditRememberBufferSize(tEditInstance *pEditInstance);
static void ODEditRememberArea(tEditInstance *pEditInstance,
void *pRememberedArea);
static void ODEditRedrawChanged(tEditInstance *pEditInstance,
void *pRememberedArea, UINT unUpperBoundary, UINT unLowerBoundary);
static BOOL ODEditDetermineChanged(tEditInstance *pEditInstance,
void *pRememberedArea, UINT unUpperBoundary, UINT unLowerBoundary,
UINT *punStartRedrawLine, UINT *punStartRedrawColumn,
UINT *punFinishRedrawLine, UINT *punFinishRedrawColumn);
static void ODEditRedrawSubArea(tEditInstance *pEditInstance,
UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
UINT unFinishRedrawColumn);
static void ODEditGetActualCurPos(tEditInstance *pEditInstance,
UINT *punRow, UINT *punColumn);
static BOOL ODEditIsEOLForMode(tEditInstance *pEditInstance, char chToTest);
/* Low level buffer manipulation functions. */
static BOOL ODEditBufferFormatAndIndex(tEditInstance *pEditInstance);
static UINT ODEditBufferGetLineLength(tEditInstance *pEditInstance,
UINT unBufferLine);
static UINT ODEditBufferGetTotalLines(tEditInstance *pEditInstance);
static char *ODEditBufferGetCharacter(tEditInstance *pEditInstance,
UINT unBufferLine, UINT unBufferColumn);
static tODResult ODEditBufferMakeSpace(tEditInstance *pEditInstance,
UINT unLine, UINT unColumn, UINT unNumChars);
static tODResult ODEditTryToGrow(tEditInstance *pEditInstance,
UINT unSizeNeeded);
/* ========================================================================= */
/* High level editor implementation. */
/* ========================================================================= */
/* ----------------------------------------------------------------------------
* od_multiline_edit()
*
* Multiline editor function, allows the user to enter or change text that
* spans multiple lines.
*
* Parameters: pszBufferToEdit - Pointer to '\0'-terminated buffer of text
* to edit.
*
* unBufferSize - Size of the buffer, in characters.
*
* pEditOptions - Pointer to a tODEditOptions structure, or
* NULL to use default settings.
*
* Return: An od_multiline_edit()-specific result code.
*/
ODAPIDEF INT ODCALL od_multiline_edit(char *pszBufferToEdit, UINT unBufferSize,
tODEditOptions *pEditOptions)
{
tEditInstance EditInstance;
INT nToReturn;
/* Log function entry if running in trace mode */
TRACE(TRACE_API, "od_node_open()");
/* Initialize OpenDoors if not already done. */
if(!bODInitialized) od_init();
OD_API_ENTRY();
/* Validate parameters. */
if(pszBufferToEdit == NULL || unBufferSize == 0)
{
od_control.od_error = ERR_PARAMETER;
OD_API_EXIT();
return(OD_MULTIEDIT_ERROR);
}
/* Check the user's terminal supports the required capabilities. */
if(!(od_control.user_ansi || od_control.user_avatar))
{
od_control.od_error = ERR_NOGRAPHICS;
OD_API_EXIT();
return(OD_MULTIEDIT_ERROR);
}
/* Initialize editor instance information structure. */
if(!ODEditSetupInstance(&EditInstance, pszBufferToEdit, unBufferSize,
pEditOptions))
{
OD_API_EXIT();
return(OD_MULTIEDIT_ERROR);
}
/* Attempt to build the buffer line index and ensure that the buffer */
/* conforms to the format specified by the client application. */
if(!ODEditBufferFormatAndIndex(&EditInstance))
{
od_control.od_error = ERR_MEMORY;
OD_API_EXIT();
return(OD_MULTIEDIT_ERROR);
}
/* Claim exclusive use of arrow keys. */
ODStatStartArrowUse();
/* Ensure that all information in the outbound communications buffer */
/* has been sent before starting. This way, we can safely purge the */
/* outbound buffer at any time without loosing anything that was sent */
/* before od_multiline_edit() was called. */
ODWaitDrain(PRE_DRAIN_TIME);
/* Draw the initial edit area. */
ODEditRedrawArea(&EditInstance);
/* Run the main editor loop. */
nToReturn = ODEditMainLoop(&EditInstance);
/* Release exclusive use of arrow keys. */
ODStatEndArrowUse();
/* Set final information which will be available in the user options */
/* structure for the client application to access. */
EditInstance.pUserOptions->pszFinalBuffer = EditInstance.pszEditBuffer;
EditInstance.pUserOptions->unFinalBufferSize = unBufferSize;
OD_API_EXIT();
return(nToReturn);
}
/* ----------------------------------------------------------------------------
* ODEditSetupInstance() *** PRIVATE FUNCTION ***
*
* Initializes editor instance information structure.
*
* Parameters: pEditInstance - Editor instance information structure.
*
* pszBufferToEdit - Buffer pointer provided by client.
*
* unBufferSize - Initial buffer size, as specified by the
* client.
*
* pUserOptions - Editor options specified by the client.
*
* Return: TRUE on success, FALSE on failure. In the case of failure,
* od_control.od_error is set appropriately.
*/
static BOOL ODEditSetupInstance(tEditInstance *pEditInstance,
char *pszBufferToEdit, UINT unBufferSize, tODEditOptions *pUserOptions)
{
ASSERT(pEditInstance != NULL);
ASSERT(pszBufferToEdit != NULL);
/* Setup editor instance structure. */
pEditInstance->pszEditBuffer = pszBufferToEdit;
pEditInstance->unBufferSize = unBufferSize;
if(pUserOptions == NULL)
{
/* Edit options is just the defaults. */
pEditInstance->pUserOptions = &ODEditOptionsDefault;
}
else
{
/* Edit options are supplied by the user. */
pEditInstance->pUserOptions = pUserOptions;
/* Initialize any edit options that the user did not setup. */
/* Check that edit area has been initialized. */
if(pUserOptions->nAreaLeft == 0)
{
pUserOptions->nAreaLeft = ODEditOptionsDefault.nAreaLeft;
}
if(pUserOptions->nAreaRight == 0)
{
pUserOptions->nAreaRight = ODEditOptionsDefault.nAreaRight;
}
if(pUserOptions->nAreaTop == 0)
{
pUserOptions->nAreaTop = ODEditOptionsDefault.nAreaTop;
}
if(pUserOptions->nAreaBottom == 0)
{
pUserOptions->nAreaBottom = ODEditOptionsDefault.nAreaBottom;
}
}
pEditInstance->unCurrentLine = 0;
pEditInstance->unCurrentColumn = 0;
pEditInstance->unLineScrolledToTop = 0;
pEditInstance->papchStartOfLine = NULL;
pEditInstance->unLineArraySize = 0;
pEditInstance->unLinesInBuffer = 0;
pEditInstance->unAreaWidth = (UINT)pEditInstance->pUserOptions->
nAreaRight - (UINT)pEditInstance->pUserOptions->nAreaLeft + 1;
pEditInstance->unAreaHeight = (UINT)pEditInstance->pUserOptions->
nAreaBottom - (UINT)pEditInstance->pUserOptions->nAreaTop + 1;
pEditInstance->bInsertMode = TRUE;
pEditInstance->unTabStopSize = DEFAULT_TAB_STOP_SIZE;
/* Setup line break and paragraph break sequences, if they can be */
/* determined at this point. If they can't be determined, set them */
/* to NULL. */
switch(pEditInstance->pUserOptions->TextFormat)
{
case FORMAT_FTSC_MESSAGE:
/* FTSC compliant messages use \r as a paragraph break, and do */
/* not have any line break characters. */
pEditInstance->pszLineBreak = "";
pEditInstance->pszParagraphBreak = "\r";
break;
case FORMAT_PARAGRAPH_BREAKS:
/* Paragraph break mode only inserts CR/LF sequences at the end */
/* of a paragrah, and word-wraps the text that forms a paragrah. */
pEditInstance->pszLineBreak = "";
pEditInstance->pszParagraphBreak = NULL;
break;
case FORMAT_LINE_BREAKS:
case FORMAT_NO_WORDWRAP:
/* Line break mode and no word wrap mode both terminate every */
/* line of the file with a CR/LF sequence, and have no paragrah */
/* terminator. In line break mode, word wrap is enabled, whereas */
/* it is not in FORMAT_NO_WORDWRAP mode. */
pEditInstance->pszLineBreak = NULL;
pEditInstance->pszParagraphBreak = "";
break;
default:
/* An invalid text format was specified. */
od_control.od_error = ERR_PARAMETER;
return(FALSE);
}
/* Determine whether long lines sould be word wrapped or character */
/* wrapped. */
pEditInstance->bWordWrapLongLines = (pEditInstance->pUserOptions->TextFormat
!= FORMAT_NO_WORDWRAP);
/* Attempt to allocate abuffer for remembered data. */
pEditInstance->pRememberBuffer =
malloc(ODEditRememberBufferSize(pEditInstance));
if(pEditInstance->pRememberBuffer == NULL)
{
od_control.od_error = ERR_MEMORY;
return(FALSE);
}
/* If AVATAR mode or local mode is active, then scroll up or down one */
/* line at a time. */
if(od_control.user_avatar || od_control.baud == 0)
{
pEditInstance->unScrollDistance = 1;
}
/* In ANSI mode with a remote connection, scroll multiple lines at a */
/* time. This is the minimum of the default scroll distance, and the */
/* current height of the edit area - 1. */
else
{
pEditInstance->unScrollDistance = MIN(ANSI_SCROLL_DISTANCE,
pEditInstance->pUserOptions->nAreaBottom
- pEditInstance->pUserOptions->nAreaTop);
}
/* Return with success. */
return(TRUE);
}
/* ----------------------------------------------------------------------------
* ODEditRedrawArea() *** PRIVATE FUNCTION ***
*
* Redraws the area of the screen used by the editor.
*
* Parameters: pEditInstance - Editor instance information structure.
*
* Return: void
*/
static void ODEditRedrawArea(tEditInstance *pEditInstance)
{
UINT unAreaLine;
ASSERT(pEditInstance != NULL);
ODScrnEnableCaret(FALSE);
/* First, remove anything that is still in the outbound communications */
/* buffer, since whatever it was, it will no longer be visible after */
/* the screen redraw anyhow. */
if(od_control.baud != 0) ODComClearOutbound(hSerialPort);
/* Loop, drawing every line in the edit area. */
for(unAreaLine = 0; unAreaLine < pEditInstance->unAreaHeight; ++unAreaLine)
{
ODEditDrawAreaLine(pEditInstance, unAreaLine);
}
ODScrnEnableCaret(TRUE);
}
/* ----------------------------------------------------------------------------
* ODEditDrawAreaLine() *** PRIVATE FUNCTION ***
*
* Redraws the specified line in the area of the screen used by the editor.
*
* Parameters: pEditInstance - Editor instance information structure.
*
* unAreaLineToDraw - 0-based line number in the edit area to be
* redrawn.
*
* Return: void
*/
static void ODEditDrawAreaLine(tEditInstance *pEditInstance,
UINT unAreaLineToDraw)
{
UINT unBufferLine;
UINT unLineLength;
ASSERT(pEditInstance != NULL);
ASSERT(unAreaLineToDraw >= 0);
ASSERT(unAreaLineToDraw < pEditInstance->unAreaHeight);
/* Determine the buffer line that is displayed on this screen line. */
unBufferLine = unAreaLineToDraw + pEditInstance->unLineScrolledToTop;
/* Position the cursor to the beginning of this line. */
od_set_cursor((UINT)pEditInstance->pUserOptions->nAreaTop
+ unAreaLineToDraw, (UINT)pEditInstance->pUserOptions->nAreaLeft);
/* If this line is not beyond the end of the buffer. */
if(unBufferLine < pEditInstance->unLinesInBuffer)
{
/* Determine the length of this buffer line. */
unLineLength = ODEditBufferGetLineLength(pEditInstance, unBufferLine);
od_disp(ODEditBufferGetCharacter(pEditInstance, unBufferLine, 0),
unLineLength, TRUE);
}
else
{
unLineLength = 0;
}
/* If right edge of edit area aligns with the right edge of the screen. */
if(pEditInstance->pUserOptions->nAreaRight == OD_SCREEN_WIDTH)
{
/* Clear the remainder of this line on the screen. */
od_clr_line();
}
else
{
/* Place spaces after the end of the current line, up to right edge of */
/* the edit area. */
od_repeat(' ', (BYTE)(pEditInstance->unAreaWidth - unLineLength));
}
}
/* ----------------------------------------------------------------------------
* ODEditMainLoop() *** PRIVATE FUNCTION ***
*
* Implements the main editor loop, which repeatedly waits for input from the
* user, until the user chooses to exit.
*
* Parameters: pEditInstance - Editor instance information structure.
*
* Return: Value to be returned by od_multiline_edit.
*/
static INT ODEditMainLoop(tEditInstance *pEditInstance)
{
tODInputEvent InputEvent;
ASSERT(pEditInstance != NULL);
/* Set initial cursor position. */
ODEditUpdateCursorPos(pEditInstance);
/* Loop, obtaining keystrokes until the user chooses to exit. */
for(;;)
{
od_get_input(&InputEvent, OD_NO_TIMEOUT, GETIN_NORMAL);
if(InputEvent.EventType == EVENT_EXTENDED_KEY)
{
switch(InputEvent.chKeyPress)
{
case OD_KEY_UP:
/* If we aren't at the start of the file, then move to the */
/* previous line. */
if(pEditInstance->unCurrentLine > 0)
{
ODEditGotoPreviousLine(pEditInstance);
ODEditUpdateCursorPos(pEditInstance);
}
break;
case OD_KEY_DOWN:
/* If we aren't at the end of the file, then move to the */
/* next line. */
if(pEditInstance->unCurrentLine
< ODEditBufferGetTotalLines(pEditInstance) - 1)
{
ODEditGotoNextLine(pEditInstance);
ODEditUpdateCursorPos(pEditInstance);
}
break;
case OD_KEY_LEFT:
/* Attempt to move the cursor left. */
if(ODEditCursorLeft(pEditInstance))
{
/* If it was possible to move the cursor, then update the */
/* cursor position on the screen. */
ODEditUpdateCursorPos(pEditInstance);
}
break;
case OD_KEY_RIGHT:
/* In word wrap mode, we allow the cursor to move up to the */
/* end of this line, and then wrap around to the next line. */
if(pEditInstance->bWordWrapLongLines)
{
if(pEditInstance->unCurrentColumn < ODEditBufferGetLineLength
(pEditInstance, pEditInstance->unCurrentLine))
{
++pEditInstance->unCurrentColumn;
ODEditUpdateCursorPos(pEditInstance);
}
else if(pEditInstance->unCurrentLine
< ODEditBufferGetTotalLines(pEditInstance) - 1)
{
ODEditGotoNextLine(pEditInstance);
pEditInstance->unCurrentColumn = 0;
ODEditUpdateCursorPos(pEditInstance);
}
}
/* In character wrap mode, we allow the cursor to move up to */
/* the right edge of the edit area. */
else
{
if(pEditInstance->unCurrentColumn
< pEditInstance->unAreaWidth - 1)
{
++pEditInstance->unCurrentColumn;
ODEditUpdateCursorPos(pEditInstance);
}
}
break;
case OD_KEY_HOME:
pEditInstance->unCurrentColumn = 0;
ODEditUpdateCursorPos(pEditInstance);
break;
case OD_KEY_END:
pEditInstance->unCurrentColumn = ODEditBufferGetLineLength(
pEditInstance, pEditInstance->unCurrentLine);
ODEditUpdateCursorPos(pEditInstance);
break;
case OD_KEY_PGUP:
if(pEditInstance->unLineScrolledToTop > 0)
{
UINT unDistance = MIN(pEditInstance->unAreaHeight - 1,
pEditInstance->unLineScrolledToTop);
ODEditScrollArea(pEditInstance, -((INT)unDistance));
pEditInstance->unCurrentLine -= unDistance;
ODEditUpdateCursorPos(pEditInstance);
}
else if(pEditInstance->unCurrentLine != 0)
{
pEditInstance->unCurrentLine = 0;
ODEditUpdateCursorPos(pEditInstance);
}
break;
case OD_KEY_PGDN:
if(pEditInstance->unLineScrolledToTop <
pEditInstance->unLinesInBuffer - 1)
{
UINT unDistance = MIN(pEditInstance->unAreaHeight - 1,
pEditInstance->unLinesInBuffer
- pEditInstance->unLineScrolledToTop - 1);
ODEditScrollArea(pEditInstance, (INT)unDistance);
pEditInstance->unCurrentLine = MIN(
pEditInstance->unCurrentLine + unDistance,
pEditInstance->unLinesInBuffer - 1);
ODEditUpdateCursorPos(pEditInstance);
}
break;
case OD_KEY_INSERT:
/* If the insert key is pressed, then toggle insert mode. */
pEditInstance->bInsertMode = !pEditInstance->bInsertMode;
break;
case OD_KEY_DELETE:
/* Delete the character at the current position. */
/* If we are currently past the end of this line. */
if(ODEditPastEndOfCurLine(pEditInstance))
{
/* Add spaces to this line to fill it up to the current */
/* cursor position. */
switch(ODEditBufferMakeSpace(pEditInstance,
pEditInstance->unCurrentLine,
pEditInstance->unCurrentColumn, 0))
{
case kODRCUnrecoverableFailure:
/* If we encountered an unrecoverable failure, then */
/* exit from the editor with a memory allocation */
/* error. */
od_control.od_error = ERR_MEMORY;
return(OD_MULTIEDIT_ERROR);
case kODRCSuccess:
/* On success, delete the current character. */
ODEditDeleteCurrentChar(pEditInstance);
break;
default:
/* On any other failure, just beep and continue. */
od_putch('\a');
}
}
else
{
/* If we aren't pas the end of the line, then just do a */
/* simple delete character operation. */
ODEditDeleteCurrentChar(pEditInstance);
}
break;
}
}
else if(InputEvent.EventType == EVENT_CHARACTER)
{
if(InputEvent.chKeyPress == 25)
{
/* Control-Y (delete line) has been pressed. */
ODEditDeleteCurrentLine(pEditInstance);
}
else if(InputEvent.chKeyPress == 26
|| InputEvent.chKeyPress == 27)
{
/* Escape or control-Z has been pressed. */
/* If a menu callback function has been provided by */
/* the client, then call it. */
if(pEditInstance->pUserOptions->pfMenuCallback != NULL)
{
/* Call the menu callback function. */
switch((*pEditInstance->pUserOptions->pfMenuCallback)(NULL))
{
case EDIT_MENU_EXIT_EDITOR:
return(OD_MULTIEDIT_SUCCESS);
case EDIT_MENU_DO_NOTHING:
/* Continue in the editor without doing anything. */
break;
default:
ASSERT(FALSE);
}
/* If we are continuing, then restore initial cursor pos. */
ODEditUpdateCursorPos(pEditInstance);
}
else
{
/* If a menu key callback function has not been provided, */
/* then we exit the editor unconditionally. */
return(OD_MULTIEDIT_SUCCESS);
}
}
else if(InputEvent.chKeyPress == '\b')
{
/* Backspace key has been pressed. */
/* If the cursor is past the end of the line, then we just move */
/* the cursor left, without deleting any characters. */
BOOL bDelete = !ODEditPastEndOfCurLine(pEditInstance);
/* Backup the cursor one space. */
if(ODEditCursorLeft(pEditInstance))
{
/* If there was space to move the cursor back to, then */
/* proceed and remove the character at the current position. */
if(bDelete)
{
ODEditDeleteCurrentChar(pEditInstance);
}
else
{
/* In this case, we must still show the new cursor */
/* position. */
ODEditUpdateCursorPos(pEditInstance);
}
}
}
else if(InputEvent.chKeyPress == '\t')
{
char szTextToAdd[MAX_TAB_STOP_SIZE + 1];
UINT unTargetColumn;
UINT unTargetDistance;
/* A tab key has been entered. */
/* Determine the column that this will move the cursor to. */
ASSERT(pEditInstance->unTabStopSize <= MAX_TAB_STOP_SIZE);
unTargetColumn = ((pEditInstance->unCurrentColumn / pEditInstance->
unTabStopSize) + 1) * pEditInstance->unTabStopSize;
/* In insert mode, then insert spaces into the buffer. */
if(pEditInstance->bInsertMode)
{
/* Determine the number of columns that we need to advance in */
/* order to reach this target column. */
unTargetDistance = unTargetColumn -
pEditInstance->unCurrentColumn;
ASSERT(unTargetDistance <= MAX_TAB_STOP_SIZE);
/* Translate this to a string with the appropriate number of */
/* spaces. */
memset(szTextToAdd, ' ', unTargetDistance);
szTextToAdd[unTargetDistance] = '\0';
/* Add this to the buffer. */
if(ODEditEnterText(pEditInstance, szTextToAdd, TRUE) ==
kODRCUnrecoverableFailure)
{
od_control.od_error = ERR_MEMORY;
return(OD_MULTIEDIT_ERROR);
}
}
/* In overwrite mode, then just advance the cursor position. */
else
{
/* Determine the column where the cursor should be wrapped. */
UINT unWrapColumn = pEditInstance->bWordWrapLongLines ?
ODEditBufferGetLineLength(pEditInstance,
pEditInstance->unCurrentLine)
: pEditInstance->unAreaWidth;
if(unTargetColumn < unWrapColumn)
{
pEditInstance->unCurrentColumn = unTargetColumn;
ODEditUpdateCursorPos(pEditInstance);
}
else if(pEditInstance->unCurrentLine
< ODEditBufferGetTotalLines(pEditInstance) - 1)
{
ODEditGotoNextLine(pEditInstance);
pEditInstance->unCurrentColumn = 0;
ODEditUpdateCursorPos(pEditInstance);
}
}
}
else if(InputEvent.chKeyPress == '\r')
{
char *pszTextToAdd;
/* The enter key has been pressed. In insert mode, or at the end */
/* of the buffer in overwrite mode, this adds a new line. */
if(pEditInstance->bInsertMode || pEditInstance->unCurrentLine
>= ODEditBufferGetTotalLines(pEditInstance) - 1)
{
if(!pEditInstance->bInsertMode)
{
/* If we are not in insert mode, begin by positioning the */
/* cursor at the end of this line. */
pEditInstance->unCurrentColumn = ODEditBufferGetLineLength(
pEditInstance, pEditInstance->unCurrentLine);
}
/* Determine the line/paragraph break sequence to use. */
if(pEditInstance->pszLineBreak != NULL
&& strlen(pEditInstance->pszLineBreak) > 0)
{
pszTextToAdd = pEditInstance->pszLineBreak;
}
else if(pEditInstance->pszParagraphBreak != NULL
&& strlen(pEditInstance->pszParagraphBreak) > 0)
{
pszTextToAdd = pEditInstance->pszParagraphBreak;
}
else
{
pszTextToAdd = DEFAULT_LINE_BREAK;
}
/* Insert the sequence into the buffer. */
if(ODEditEnterText(pEditInstance, pszTextToAdd, TRUE) ==
kODRCUnrecoverableFailure)
{
od_control.od_error = ERR_MEMORY;
return(OD_MULTIEDIT_ERROR);
}
}
else
{
/* Pressing the enter key in overwrite mode just moves the */
/* cursor to the beginning of the next line. In other words, */
/* it is equivalent to pressing Down arrow followed by home. */
ODEditGotoNextLine(pEditInstance);
pEditInstance->unCurrentColumn = 0;
ODEditUpdateCursorPos(pEditInstance);
}
}
else if(InputEvent.chKeyPress >= 32)
{
char szTextToAdd[2];
szTextToAdd[0] = InputEvent.chKeyPress;
szTextToAdd[1] = '\0';
/* A valid buffer character has been entered. */
if(ODEditEnterText(pEditInstance, szTextToAdd,
(BOOL)(pEditInstance->bInsertMode
|| ODEditPastEndOfCurLine(pEditInstance)))
== kODRCUnrecoverableFailure)
{
od_control.od_error = ERR_MEMORY;
return(OD_MULTIEDIT_ERROR);
}
}
}
}
}
/* ----------------------------------------------------------------------------
* ODEditGotoPreviousLine() *** PRIVATE FUNCTION ***
*
* Moves the current cursor position to the previous line, scrolling the screen
* if necessary to keep the cursor visible.
*
* Parameters: pEditInstance - Editor instance information structure.
*
* Return: void
*/
static void ODEditGotoPreviousLine(tEditInstance *pEditInstance)
{
ASSERT(pEditInstance != NULL);
/* If we are already at the first line, then return without doing */
/* anything. */
if(pEditInstance->unCurrentLine == 0) return;
/* If cursor is at top of edit area, then scroll area */
/* first. */
if(ODEditGetCurrentLineInArea(pEditInstance) == 0)
{
ODEditScrollArea(pEditInstance,
-(INT)(MIN(pEditInstance->unScrollDistance,
pEditInstance->unCurrentLine)));
}
/* Move cursor to previous line. */
--pEditInstance->unCurrentLine;
}
/* ----------------------------------------------------------------------------
* ODEditGotoNextLine() *** PRIVATE FUNCTION ***
*
* Advances the current cursor position to the next line, scrolling the screen
* if necessary to keep the cursor visible.
*
* Parameters: pEditInstance - Editor instance information structure.
*
* Return: void
*/
static void ODEditGotoNextLine(tEditInstance *pEditInstance)
{
ASSERT(pEditInstance != NULL);
/* If we are already at the end of the file, then return without */
/* doing anything. */
if(pEditInstance->unCurrentLine
>= ODEditBufferGetTotalLines(pEditInstance) - 1)
{
return;
}
/* If cursor is at the bottom of the edit area, then scroll area first. */
if(ODEditGetCurrentLineInArea(pEditInstance)
== pEditInstance->unAreaHeight - 1)
{
ODEditScrollArea(pEditInstance,
(INT)MIN(pEditInstance->unScrollDistance,
ODEditBufferGetTotalLines(pEditInstance)
- pEditInstance->unCurrentLine));
}
/* Move cursor to next line. */
++pEditInstance->unCurrentLine;
}
/* ----------------------------------------------------------------------------
* ODEditScrollArea() *** PRIVATE FUNCTION ***
*
* Scrolls the edit area up or down the specified distance, redrawing newly
* "exposed" lines.
*
* Parameters: pEditInstance - Editor instance information structure.
*
* nDistance - Number of lines to scroll, where a positive
* value moves the text upwards, and a negative
* value moves the text down.
*
* Return: FALSE if a full redraw has been performed, TRUE if an efficient
* scroll command has been used.
*/
static BOOL ODEditScrollArea(tEditInstance *pEditInstance, INT nDistance)
{
BOOL bUseScrollCommand = FALSE;
UINT unAreaLine;
UINT unBufferLine;
UINT unFirstAreaLineToDraw;
UINT unLastAreaLineToDraw;
UINT unPositiveDistance;
ASSERT(pEditInstance);
/* If scroll distance is zero, then we don't need to do anything at all. */
if(nDistance == 0)
{
return(TRUE);
}
/* Otherwise, obtain the absolute value of the distance as an unsigned */
/* integer. */
else if(nDistance < 0)
{
unPositiveDistance = (UINT)-nDistance;
}
else
{
unPositiveDistance = (UINT)nDistance;
}
/* In AVATAR mode, if more than one of the currently visible lines will */
/* still be visible after scrolling, then we will consider using the */
/* scroll operation. */
if(od_control.user_avatar
&& ((INT)pEditInstance->unAreaHeight) - ((INT)unPositiveDistance) > 1)
{
/* Even under this situation, we only want to use the scroll operation */
/* if the amount of data still in the outbound buffer + our estimate */
/* of the amount of data that will be sent to perform the scroll */
/* operation is less than our estimate of the amount of data that */
/* would be sent by a complete screen redraw. */