1 | /* $XConsortium: tsource.c,v 2.23 91/07/05 18:56:14 converse Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * COPYRIGHT 1987 |
---|
5 | * DIGITAL EQUIPMENT CORPORATION |
---|
6 | * MAYNARD, MASSACHUSETTS |
---|
7 | * ALL RIGHTS RESERVED. |
---|
8 | * |
---|
9 | * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND |
---|
10 | * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION. |
---|
11 | * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR |
---|
12 | * ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. |
---|
13 | * |
---|
14 | * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT RIGHTS, |
---|
15 | * APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN ADDITION TO THAT |
---|
16 | * SET FORTH ABOVE. |
---|
17 | * |
---|
18 | * |
---|
19 | * Permission to use, copy, modify, and distribute this software and its |
---|
20 | * documentation for any purpose and without fee is hereby granted, provided |
---|
21 | * that the above copyright notice appear in all copies and that both that |
---|
22 | * copyright notice and this permission notice appear in supporting documentation, |
---|
23 | * and that the name of Digital Equipment Corporation not be used in advertising |
---|
24 | * or publicity pertaining to distribution of the software without specific, |
---|
25 | * written prior permission. |
---|
26 | */ |
---|
27 | |
---|
28 | /* File: tsource.c -- the code for a toc source */ |
---|
29 | |
---|
30 | #include "xmh.h" |
---|
31 | #include "tocintrnl.h" |
---|
32 | #include <X11/Xatom.h> |
---|
33 | #include "tsourceP.h" |
---|
34 | |
---|
35 | /**************************************************************** |
---|
36 | * |
---|
37 | * Full class record constant |
---|
38 | * |
---|
39 | ****************************************************************/ |
---|
40 | |
---|
41 | /* Private Data */ |
---|
42 | |
---|
43 | #define Offset(field) XtOffsetOf(TocSourceRec, toc_source.field) |
---|
44 | |
---|
45 | static XtResource resources[] = { |
---|
46 | {XtNtoc, XtCToc, XtRPointer, sizeof(caddr_t), |
---|
47 | Offset(toc), XtRPointer, NULL}, |
---|
48 | }; |
---|
49 | |
---|
50 | #undef Offset |
---|
51 | |
---|
52 | static void Initialize(); |
---|
53 | static XawTextPosition Read(), Scan(), Search(); |
---|
54 | static int Replace(); |
---|
55 | |
---|
56 | #define SuperClass (&textSrcClassRec) |
---|
57 | TocSourceClassRec tocSourceClassRec = { |
---|
58 | { |
---|
59 | /* core_class fields */ |
---|
60 | /* superclass */ (WidgetClass) SuperClass, |
---|
61 | /* class_name */ "TocSrc", |
---|
62 | /* widget_size */ sizeof(TocSourceRec), |
---|
63 | /* class_initialize */ NULL, |
---|
64 | /* class_part_initialize */ NULL, |
---|
65 | /* class_inited */ FALSE, |
---|
66 | /* initialize */ Initialize, |
---|
67 | /* initialize_hook */ NULL, |
---|
68 | /* realize */ NULL, |
---|
69 | /* actions */ NULL, |
---|
70 | /* num_actions */ 0, |
---|
71 | /* resources */ resources, |
---|
72 | /* num_resources */ XtNumber(resources), |
---|
73 | /* xrm_class */ NULLQUARK, |
---|
74 | /* compress_motion */ FALSE, |
---|
75 | /* compress_exposure */ FALSE, |
---|
76 | /* compress_enterleave */ FALSE, |
---|
77 | /* visible_interest */ FALSE, |
---|
78 | /* destroy */ NULL, |
---|
79 | /* resize */ NULL, |
---|
80 | /* expose */ NULL, |
---|
81 | /* set_values */ NULL, |
---|
82 | /* set_values_hook */ NULL, |
---|
83 | /* set_values_almost */ NULL, |
---|
84 | /* get_values_hook */ NULL, |
---|
85 | /* accept_focus */ NULL, |
---|
86 | /* version */ XtVersion, |
---|
87 | /* callback_private */ NULL, |
---|
88 | /* tm_table */ NULL, |
---|
89 | /* query_geometry */ NULL, |
---|
90 | /* display_accelerator */ NULL, |
---|
91 | /* extension */ NULL |
---|
92 | }, |
---|
93 | /* textSrc_class fields */ |
---|
94 | { |
---|
95 | /* Read */ Read, |
---|
96 | /* Replace */ Replace, |
---|
97 | /* Scan */ Scan, |
---|
98 | /* Search */ Search, |
---|
99 | /* SetSelection */ XtInheritSetSelection, |
---|
100 | /* ConvertSelection */ XtInheritConvertSelection |
---|
101 | }, |
---|
102 | /* toc_source_class fields */ |
---|
103 | { |
---|
104 | /* keeping the compiler happy */ 0 |
---|
105 | } |
---|
106 | }; |
---|
107 | |
---|
108 | WidgetClass tocSourceWidgetClass = (WidgetClass)&tocSourceClassRec; |
---|
109 | |
---|
110 | /************************************************************ |
---|
111 | * |
---|
112 | * Class specific methods. |
---|
113 | * |
---|
114 | ************************************************************/ |
---|
115 | |
---|
116 | Msg MsgFromPosition(toc, position, dir) |
---|
117 | Toc toc; |
---|
118 | XawTextPosition position; |
---|
119 | XawTextScanDirection dir; |
---|
120 | { |
---|
121 | Msg msg; |
---|
122 | int h, l, m; |
---|
123 | if (position > toc->lastPos) position = toc->lastPos; |
---|
124 | if (dir == XawsdLeft) position--; |
---|
125 | l = 0; |
---|
126 | h = toc->nummsgs - 1; |
---|
127 | while (l < h - 1) { |
---|
128 | m = (l + h) / 2; |
---|
129 | if (toc->msgs[m]->position > position) |
---|
130 | h = m; |
---|
131 | else |
---|
132 | l = m; |
---|
133 | } |
---|
134 | msg = toc->msgs[h]; |
---|
135 | if (msg->position > position) |
---|
136 | msg = toc->msgs[h = l]; |
---|
137 | while (!msg->visible) |
---|
138 | msg = toc->msgs[h++]; |
---|
139 | if (position < msg->position || position > msg->position + msg->length) |
---|
140 | Punt("Error in MsgFromPosition!"); |
---|
141 | return msg; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | static XawTextPosition CoerceToLegalPosition(toc, position) |
---|
146 | Toc toc; |
---|
147 | XawTextPosition position; |
---|
148 | { |
---|
149 | return (position < 0) ? 0 : |
---|
150 | ((position > toc->lastPos) ? toc->lastPos : position); |
---|
151 | } |
---|
152 | |
---|
153 | static XawTextPosition |
---|
154 | Read(w, position, block, length) |
---|
155 | Widget w; |
---|
156 | XawTextPosition position; |
---|
157 | XawTextBlock *block; |
---|
158 | int length; |
---|
159 | { |
---|
160 | TocSourceWidget source = (TocSourceWidget) w; |
---|
161 | Toc toc = source->toc_source.toc; |
---|
162 | Msg msg; |
---|
163 | int count; |
---|
164 | |
---|
165 | if (position < toc->lastPos) { |
---|
166 | block->firstPos = position; |
---|
167 | msg = MsgFromPosition(toc, position, XawsdRight); |
---|
168 | block->ptr = msg->buf + (position - msg->position); |
---|
169 | count = msg->length - (position - msg->position); |
---|
170 | block->length = (count < length) ? count : length; |
---|
171 | position += block->length; |
---|
172 | } |
---|
173 | else { |
---|
174 | block->firstPos = 0; |
---|
175 | block->length = 0; |
---|
176 | block->ptr = ""; |
---|
177 | } |
---|
178 | block->format = FMT8BIT; |
---|
179 | return position; |
---|
180 | } |
---|
181 | |
---|
182 | /* Right now, we can only replace a piece with another piece of the same size, |
---|
183 | and it can't cross between lines. */ |
---|
184 | |
---|
185 | static int |
---|
186 | Replace(w, startPos, endPos, block) |
---|
187 | Widget w; |
---|
188 | XawTextPosition startPos, endPos; |
---|
189 | XawTextBlock *block; |
---|
190 | { |
---|
191 | TocSourceWidget source = (TocSourceWidget) w; |
---|
192 | Toc toc = source->toc_source.toc; |
---|
193 | Msg msg; |
---|
194 | int i; |
---|
195 | |
---|
196 | if (block->length != endPos - startPos) |
---|
197 | return XawEditError; |
---|
198 | msg = MsgFromPosition(toc, startPos, XawsdRight); |
---|
199 | for (i = 0; i < block->length; i++) |
---|
200 | msg->buf[startPos - msg->position + i] = block->ptr[i]; |
---|
201 | /* for (i=0 ; i<toc->numwidgets ; i++) |
---|
202 | XawTextInvalidate(toc->widgets[i], startPos, endPos); |
---|
203 | * |
---|
204 | * CDP 9/1/89 |
---|
205 | */ |
---|
206 | return XawEditDone; |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | #define Look(ti, c)\ |
---|
211 | { \ |
---|
212 | if ((dir == XawsdLeft && ti <= 0) || \ |
---|
213 | (dir == XawsdRight && ti >= toc->lastPos)) \ |
---|
214 | c = 0; \ |
---|
215 | else { \ |
---|
216 | if (ti + doff < msg->position || \ |
---|
217 | ti + doff >= msg->position + msg->length) \ |
---|
218 | msg = MsgFromPosition(toc, ti, dir); \ |
---|
219 | c = msg->buf[ti + doff - msg->position]; \ |
---|
220 | } \ |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | static XawTextPosition |
---|
226 | Scan(w, position, sType, dir, count, include) |
---|
227 | Widget w; |
---|
228 | XawTextPosition position; |
---|
229 | XawTextScanType sType; |
---|
230 | XawTextScanDirection dir; |
---|
231 | int count; |
---|
232 | Boolean include; |
---|
233 | { |
---|
234 | TocSourceWidget source = (TocSourceWidget) w; |
---|
235 | Toc toc = source->toc_source.toc; |
---|
236 | XawTextPosition textindex; |
---|
237 | Msg msg; |
---|
238 | char c; |
---|
239 | int ddir, doff, i, whiteSpace; |
---|
240 | ddir = (dir == XawsdRight) ? 1 : -1; |
---|
241 | doff = (dir == XawsdRight) ? 0 : -1; |
---|
242 | |
---|
243 | if (toc->lastPos == 0) return 0; |
---|
244 | textindex = position; |
---|
245 | if (textindex + doff < 0) return 0; |
---|
246 | if (dir == XawsdRight && textindex >= toc->lastPos) return toc->lastPos; |
---|
247 | msg = MsgFromPosition(toc, textindex, dir); |
---|
248 | switch (sType) { |
---|
249 | case XawstPositions: |
---|
250 | if (!include && count > 0) |
---|
251 | count--; |
---|
252 | textindex = CoerceToLegalPosition(toc, textindex + count * ddir); |
---|
253 | break; |
---|
254 | case XawstWhiteSpace: |
---|
255 | for (i = 0; i < count; i++) { |
---|
256 | whiteSpace = -1; |
---|
257 | while (textindex >= 0 && textindex <= toc->lastPos) { |
---|
258 | Look(textindex, c); |
---|
259 | if ((c == ' ') || (c == '\t') || (c == '\n')) { |
---|
260 | if (whiteSpace < 0) whiteSpace = textindex; |
---|
261 | } else if (whiteSpace >= 0) |
---|
262 | break; |
---|
263 | textindex += ddir; |
---|
264 | } |
---|
265 | } |
---|
266 | if (!include) { |
---|
267 | if (whiteSpace < 0 && dir == XawsdRight) |
---|
268 | whiteSpace = toc->lastPos; |
---|
269 | textindex = whiteSpace; |
---|
270 | } |
---|
271 | textindex = CoerceToLegalPosition(toc, textindex); |
---|
272 | break; |
---|
273 | case XawstEOL: |
---|
274 | case XawstParagraph: |
---|
275 | for (i = 0; i < count; i++) { |
---|
276 | while (textindex >= 0 && textindex <= toc->lastPos) { |
---|
277 | Look(textindex, c); |
---|
278 | if (c == '\n') |
---|
279 | break; |
---|
280 | textindex += ddir; |
---|
281 | } |
---|
282 | if (i < count - 1) |
---|
283 | textindex += ddir; |
---|
284 | } |
---|
285 | if (include) |
---|
286 | textindex += ddir; |
---|
287 | textindex = CoerceToLegalPosition(toc, textindex); |
---|
288 | break; |
---|
289 | case XawstAll: |
---|
290 | if (dir == XawsdLeft) |
---|
291 | textindex = 0; |
---|
292 | else |
---|
293 | textindex = toc->lastPos; |
---|
294 | break; |
---|
295 | } |
---|
296 | return textindex; |
---|
297 | } |
---|
298 | /*ARGSUSED*/ |
---|
299 | static XawTextPosition Search(w, position, direction, block) |
---|
300 | Widget w; |
---|
301 | XawTextPosition position; |
---|
302 | XawTextScanDirection direction; |
---|
303 | XawTextBlock *block; |
---|
304 | { |
---|
305 | /* TocSourceWidget source = (TocSourceWidget) w; |
---|
306 | * Toc toc = source->toc_source.toc; |
---|
307 | * not implemented |
---|
308 | */ |
---|
309 | return XawTextSearchError; |
---|
310 | } |
---|
311 | |
---|
312 | /* Public definitions. */ |
---|
313 | |
---|
314 | /* ARGSUSED*/ |
---|
315 | static void Initialize(request, new, args, num_args) |
---|
316 | Widget request, new; |
---|
317 | ArgList args; |
---|
318 | Cardinal *num_args; |
---|
319 | { |
---|
320 | Toc toc; |
---|
321 | TocSourceWidget source = (TocSourceWidget) new; |
---|
322 | |
---|
323 | source->text_source.edit_mode = XawtextRead; /* force read only. */ |
---|
324 | |
---|
325 | toc = source->toc_source.toc; |
---|
326 | |
---|
327 | toc->hasselection = FALSE; |
---|
328 | toc->left = toc->right = 0; |
---|
329 | } |
---|
330 | |
---|
331 | void TSourceInvalid(toc, position, length) |
---|
332 | Toc toc; |
---|
333 | XawTextPosition position; |
---|
334 | int length; |
---|
335 | { |
---|
336 | XawTextInvalidate(XtParent(toc->source), position, |
---|
337 | (XawTextPosition) position+length-1); |
---|
338 | } |
---|