Mercurial > ~dholland > hg > ag > index.cgi
comparison examples/dsl/edit.h @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /***** | |
2 | |
3 AnaGram Programming Examples | |
4 | |
5 A Dos Script Language | |
6 Line Edit Class | |
7 The Edit Class is completely defined in this file. | |
8 There is no supporting CPP file. | |
9 | |
10 Copyright 1993 Parsifal Software. All Rights Reserved. | |
11 | |
12 This software is provided 'as-is', without any express or implied | |
13 warranty. In no event will the authors be held liable for any damages | |
14 arising from the use of this software. | |
15 | |
16 Permission is granted to anyone to use this software for any purpose, | |
17 including commercial applications, and to alter it and redistribute it | |
18 freely, subject to the following restrictions: | |
19 | |
20 1. The origin of this software must not be misrepresented; you must not | |
21 claim that you wrote the original software. If you use this software | |
22 in a product, an acknowledgment in the product documentation would be | |
23 appreciated but is not required. | |
24 2. Altered source versions must be plainly marked as such, and must not be | |
25 misrepresented as being the original software. | |
26 3. This notice may not be removed or altered from any source distribution. | |
27 | |
28 *****/ | |
29 | |
30 | |
31 #ifndef EDIT_H | |
32 #define EDIT_H | |
33 | |
34 #include <string.h> | |
35 #include "charsink.h" | |
36 #include "util.h" | |
37 #include "screen.h" | |
38 | |
39 class line_edit_buffer { | |
40 char *text; // text of line being edited | |
41 int x; // position of cursor in line | |
42 int length; // length of line | |
43 int buf_size; // does not include final zero | |
44 public: | |
45 int changes; // if non-zero, line changed | |
46 | |
47 | |
48 // Constructors | |
49 | |
50 line_edit_buffer(int n) { // Init with no initial string | |
51 buf_size = n; | |
52 text = new char[buf_size + 1]; | |
53 memset(text,0, buf_size + 1); | |
54 x = length = changes = 0; | |
55 } | |
56 | |
57 line_edit_buffer(char *s, int n) { // Init with initial string | |
58 if (s != NULL) { | |
59 length = strlen(s); | |
60 if (length > n) n = length; // Make buf at least as long | |
61 } | |
62 else length = 0; | |
63 buf_size = n; | |
64 text = new char[buf_size + 1]; | |
65 memset(text,0, buf_size + 1); | |
66 if (s != NULL) strcpy(text, s); | |
67 x = changes = 0; | |
68 } | |
69 | |
70 | |
71 // Destructor | |
72 | |
73 ~line_edit_buffer() { | |
74 delete [] text; | |
75 } | |
76 | |
77 | |
78 // Insert char in buffer | |
79 | |
80 line_edit_buffer &operator << (const int c) { | |
81 if (length >= buf_size) return *this; | |
82 if (length < x) { | |
83 memset(&text[length], ' ', x - length); | |
84 length = x; | |
85 text[length] = 0; | |
86 } | |
87 memmove(&text[x+1], &text[x], length++ - x); | |
88 text[x++] = (char )c; | |
89 changes++; | |
90 return *this; | |
91 } | |
92 | |
93 | |
94 // Increment cursor position | |
95 | |
96 line_edit_buffer &operator ++() { | |
97 if (x < buf_size) x++; | |
98 return *this; | |
99 } | |
100 | |
101 | |
102 // Move to end of line | |
103 | |
104 friend line_edit_buffer &end(line_edit_buffer &b) { | |
105 b.x = b.length; | |
106 return b; | |
107 } | |
108 line_edit_buffer &operator --() { | |
109 if (x) x--; | |
110 return *this; | |
111 } | |
112 | |
113 // Move to beginning of line | |
114 | |
115 friend line_edit_buffer &home(line_edit_buffer &b) { | |
116 b.x = 0; | |
117 return b; | |
118 } | |
119 | |
120 // Delete a character | |
121 | |
122 line_edit_buffer &operator ~() { | |
123 if (x >= length) return *this; | |
124 memmove(&text[x], &text[x+1], --length - x); | |
125 text[length] = 0; | |
126 changes++; | |
127 return *this; | |
128 } | |
129 | |
130 operator char *() { | |
131 return text; | |
132 } | |
133 | |
134 int index() { // return index in line | |
135 return x; | |
136 } | |
137 | |
138 | |
139 // Display edit buffer in a a screen rectangle | |
140 | |
141 friend screen_rect &operator << ( screen_rect &r | |
142 , const line_edit_buffer &b | |
143 ) { | |
144 int offset = b.x - r.size.x + 1; | |
145 if (offset < 0) offset = 0; | |
146 | |
147 r.set_cursor(b.x - offset) << (b.text + offset); | |
148 return r; | |
149 } | |
150 }; | |
151 | |
152 #endif |