internals.c revision 1.9 1 /* $NetBSD: internals.c,v 1.9 2001/02/03 12:38:47 blymn Exp $ */
2
3 /*-
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
6 * All rights reserved.
7 *
8 * This code has been donated to The NetBSD Foundation by the Author.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software withough specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 *
30 */
31
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <strings.h>
36 #include "internals.h"
37 #include "form.h"
38
39 #ifdef DEBUG
40 /*
41 * file handle to write debug info to, this will be initialised when
42 * the form is first posted.
43 */
44 FILE *dbg = NULL;
45 #endif
46
47 /* define our own min function - this is not generic but will do here
48 * (don't believe me? think about what value you would get
49 * from min(x++, y++)
50 */
51 #define min(a,b) (((a) > (b))? (b) : (a))
52
53 /* for the line joining function... */
54 #define JOIN_NEXT 1
55 #define JOIN_NEXT_NW 2 /* next join, don't wrap the joined line */
56 #define JOIN_PREV 3
57 #define JOIN_PREV_NW 4 /* previous join, don't wrap the joined line */
58
59 static void
60 _formi_do_char_validation(FIELD *field, FIELDTYPE *type, char c, int *ret_val);
61 static void
62 _formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val);
63 static int
64 _formi_join_line(FIELD *field, char *str, unsigned int pos, int direction);
65 void
66 _formi_hscroll_back(FIELD *field, unsigned int amt);
67 void
68 _formi_hscroll_fwd(FIELD *field, unsigned int amt);
69 static void
70 _formi_scroll_back(FIELD *field, unsigned int amt);
71 static void
72 _formi_scroll_fwd(FIELD *field, unsigned int amt);
73 static int
74 find_sow(char *str, unsigned int offset);
75 static int
76 find_cur_line(FIELD *cur);
77
78 /*
79 * Open the debug file if it is not already open....
80 */
81 #ifdef DEBUG
82 int
83 _formi_create_dbg_file(void)
84 {
85 if (dbg == NULL) {
86 dbg = fopen("___form_dbg.out", "w");
87 if (dbg == NULL) {
88 fprintf(stderr, "Cannot open debug file!\n");
89 return E_SYSTEM_ERROR;
90 }
91 }
92
93 return E_OK;
94 }
95 #endif
96
97 /*
98 * Set the form's current field to the first valid field on the page.
99 * Assume the fields have been sorted and stitched.
100 */
101 int
102 _formi_pos_first_field(FORM *form)
103 {
104 FIELD *cur;
105 int old_page;
106
107 old_page = form->page;
108
109 /* scan forward for an active page....*/
110 while (form->page_starts[form->page].in_use == 0) {
111 form->page++;
112 if (form->page > form->max_page) {
113 form->page = old_page;
114 return E_REQUEST_DENIED;
115 }
116 }
117
118 cur = form->fields[form->page_starts[form->page].first];
119 while ((cur->opts & (O_VISIBLE | O_ACTIVE))
120 != (O_VISIBLE | O_ACTIVE)) {
121 cur = CIRCLEQ_NEXT(cur, glue);
122 if (cur == (void *) &form->sorted_fields) {
123 form->page = old_page;
124 return E_REQUEST_DENIED;
125 }
126 }
127
128 form->cur_field = cur->index;
129 return E_OK;
130 }
131
132 /*
133 * Set the field to the next active and visible field, the fields are
134 * traversed in index order in the direction given. If the parameter
135 * use_sorted is TRUE then the sorted field list will be traversed instead
136 * of using the field index.
137 */
138 int
139 _formi_pos_new_field(FORM *form, unsigned direction, unsigned use_sorted)
140 {
141 FIELD *cur;
142 int i;
143
144 i = form->cur_field;
145 cur = form->fields[i];
146
147 do {
148 if (direction == _FORMI_FORWARD) {
149 if (use_sorted == TRUE) {
150 if ((form->wrap == FALSE) &&
151 (cur == CIRCLEQ_LAST(&form->sorted_fields)))
152 return E_REQUEST_DENIED;
153 cur = CIRCLEQ_NEXT(cur, glue);
154 i = cur->index;
155 } else {
156 if ((form->wrap == FALSE) &&
157 ((i + 1) >= form->field_count))
158 return E_REQUEST_DENIED;
159 i++;
160 if (i >= form->field_count)
161 i = 0;
162 }
163 } else {
164 if (use_sorted == TRUE) {
165 if ((form->wrap == FALSE) &&
166 (cur == CIRCLEQ_FIRST(&form->sorted_fields)))
167 return E_REQUEST_DENIED;
168 cur = CIRCLEQ_PREV(cur, glue);
169 i = cur->index;
170 } else {
171 if ((form->wrap == FALSE) && (i <= 0))
172 return E_REQUEST_DENIED;
173 i--;
174 if (i < 0)
175 i = form->field_count - 1;
176 }
177 }
178
179 if ((form->fields[i]->opts & (O_VISIBLE | O_ACTIVE))
180 == (O_VISIBLE | O_ACTIVE)) {
181 form->cur_field = i;
182 return E_OK;
183 }
184 }
185 while (i != form->cur_field);
186
187 return E_REQUEST_DENIED;
188 }
189
190 /*
191 * Find the line in a field that the cursor is currently on.
192 */
193 static int
194 find_cur_line(FIELD *cur)
195 {
196 unsigned start, end, pos, row;
197 const char *str;
198
199 str = cur->buffers[0].string;
200 pos = cur->start_char + cur->hscroll + cur->cursor_xpos;
201
202 start = 0;
203 end = 0;
204
205 for (row = 1; row < cur->row_count; row++) {
206 start = _formi_find_bol(str, start);
207 end = _formi_find_eol(str, end);
208 if ((pos >= start) && (pos <= end))
209 return row;
210 }
211
212 return 0;
213 }
214
215
216 /*
217 * Word wrap the contents of the field's buffer 0 if this is allowed.
218 * If the wrap is successful, that is, the row count nor the buffer
219 * size is exceeded then the function will return E_OK, otherwise it
220 * will return E_REQUEST_DENIED.
221 */
222 int
223 _formi_wrap_field(FIELD *field, unsigned int pos)
224 {
225 char *str, *new;
226 int width, length, allocated, row_count, sol, eol, wrapped;
227 size_t new_size;
228
229 wrapped = FALSE;
230 row_count = 0;
231 allocated = field->buffers[0].allocated;
232 length = field->buffers[0].length;
233 if ((str = (char *) malloc(sizeof(char) * allocated)) == NULL)
234 return E_SYSTEM_ERROR;
235
236 strcpy(str,field->buffers[0].string);
237
238 if ((field->opts & O_STATIC) == O_STATIC)
239 width = field->cols + 1;
240 else
241 width = field->dcols;
242
243 while (str[pos] != '\0') {
244 row_count++;
245 sol = _formi_find_bol(str, pos);
246 eol = _formi_find_eol(str, pos);
247 if ((eol - sol) <= width) {
248 /* line may be too short, try joining some lines */
249 pos = eol;
250 if ((eol - sol) == width) {
251 /* if line is just right then don't wrap */
252 pos++;
253 continue;
254 }
255
256 if (_formi_join_line(field, str, pos, JOIN_NEXT_NW)
257 == E_OK) {
258 row_count--; /* cuz we just joined a line */
259 wrapped = TRUE;
260 } else
261 break;
262 } else {
263 /* line is too long, split it - maybe */
264 /* split on first whitespace before current word */
265 pos = sol + width;
266 if (!isblank(str[pos]))
267 pos = find_sow(str, pos);
268
269 if (pos != sol) {
270 if (length + 1 >= allocated) {
271 new_size = allocated + 64
272 - (allocated % 64);
273
274 if ((new = (char *) realloc(str,
275 sizeof(char) * new_size)
276 ) == NULL) {
277 free(str);
278 return E_SYSTEM_ERROR;
279 }
280 str = new;
281 allocated = new_size;
282 }
283
284 bcopy(&str[pos], &str[pos + 1],
285 (unsigned) length - pos - 1);
286 str[pos] = '\n';
287 pos = pos + 1;
288 length++;
289 wrapped = TRUE;
290 } else
291 break;
292 }
293 }
294
295 if (row_count > field->rows) {
296 free(str);
297 return E_REQUEST_DENIED;
298 }
299
300 if (wrapped == TRUE) {
301 field->buffers[0].length = length;
302 field->buffers[0].allocated = allocated;
303 free(field->buffers[0].string);
304 field->buffers[0].string = str;
305 } else /* all that work was in vain.... */
306 free(str);
307
308 return E_OK;
309 }
310
311 /*
312 * Join the two lines that surround the location pos, the type
313 * variable indicates the direction of the join. Note that pos is
314 * assumed to be at either the end of the line for a JOIN_NEXT or at
315 * the beginning of the line for a JOIN_PREV. We need to check the
316 * field options to ensure the join does not overflow the line limit
317 * (if wrap is off) or wrap the field buffer again. Returns E_OK if
318 * the join was successful or E_REQUEST_DENIED if the join cannot
319 * happen.
320 */
321 static int
322 _formi_join_line(FIELD *field, char *str, unsigned int pos, int direction)
323 {
324 unsigned int len, sol, eol, npos, start, dest;
325
326 npos = pos;
327
328 if ((direction == JOIN_NEXT) || (direction == JOIN_NEXT_NW)) {
329 sol = _formi_find_bol(str, pos);
330 npos++;
331 /* see if there is another line following... */
332 if (str[npos] == '\0')
333 return E_REQUEST_DENIED;
334 eol = _formi_find_eol(str, npos);
335
336 start = npos;
337 dest = pos;
338 len = eol - npos;
339 } else {
340 if (pos == 0)
341 return E_REQUEST_DENIED;
342 eol = _formi_find_eol(str, pos);
343 npos--;
344 sol = _formi_find_bol(str, npos);
345
346 start = pos;
347 dest = npos;
348 len = eol - pos;
349 }
350
351
352 /* if we cannot wrap and the length of the resultant line
353 * is bigger than our field width we shall deny the request.
354 */
355 if (((field->opts & O_WRAP) != O_WRAP) && /* XXXXX check for dynamic field */
356 ((sol + eol - 1) > field->cols))
357 return E_REQUEST_DENIED;
358
359 bcopy(&str[start], &str[dest], (unsigned) len);
360
361 /* wrap the field if required, if this fails undo the change */
362 if ((direction == JOIN_NEXT) || (direction == JOIN_PREV)) {
363 if (_formi_wrap_field(field, (unsigned int) pos) != E_OK) {
364 bcopy(&str[dest], &str[start], (unsigned) len);
365 str[dest] = '\n';
366 return E_REQUEST_DENIED;
367 }
368 }
369
370 return E_OK;
371 }
372
373 /*
374 * skip the blanks in the given string, start at the index start and
375 * continue forward until either the end of the string or a non-blank
376 * character is found. Return the index of either the end of the string or
377 * the first non-blank character.
378 */
379 unsigned
380 _formi_skip_blanks(char *string, unsigned int start)
381 {
382 unsigned int i;
383
384 i = start;
385
386 while ((string[i] != '\0') && isblank(string[i]))
387 i++;
388
389 return i;
390 }
391
392 /*
393 * Return the index of the top left most field of the two given fields.
394 */
395 static int
396 _formi_top_left(FORM *form, int a, int b)
397 {
398 /* lower row numbers always win here.... */
399 if (form->fields[a]->form_row < form->fields[b]->form_row)
400 return a;
401
402 if (form->fields[a]->form_row > form->fields[b]->form_row)
403 return b;
404
405 /* rows must be equal, check columns */
406 if (form->fields[a]->form_col < form->fields[b]->form_col)
407 return a;
408
409 if (form->fields[a]->form_col > form->fields[b]->form_col)
410 return b;
411
412 /* if we get here fields must be in exactly the same place, punt */
413 return a;
414 }
415
416 /*
417 * Return the index to the field that is the bottom-right-most of the
418 * two given fields.
419 */
420 static int
421 _formi_bottom_right(FORM *form, int a, int b)
422 {
423 /* check the rows first, biggest row wins */
424 if (form->fields[a]->form_row > form->fields[b]->form_row)
425 return a;
426 if (form->fields[a]->form_row < form->fields[b]->form_row)
427 return b;
428
429 /* rows must be equal, check cols, biggest wins */
430 if (form->fields[a]->form_col > form->fields[b]->form_col)
431 return a;
432 if (form->fields[a]->form_col < form->fields[b]->form_col)
433 return b;
434
435 /* fields in the same place, punt */
436 return a;
437 }
438
439 /*
440 * Find the next '\n' character in the given string starting at offset
441 * if there are no newlines found then return the index to the end of the
442 * string.
443 */
444 int
445 _formi_find_eol(const char *string, unsigned int offset)
446 {
447 char *location;
448 int eol;
449
450 if ((location = index(&string[offset], '\n')) != NULL)
451 eol = location - string;
452 else
453 eol = strlen(string);
454
455 if (eol > 0)
456 eol--;
457
458 return eol;
459 }
460
461 /*
462 * Find the previous '\n' character in the given string starting at offset
463 * if there are no newlines found then return 0.
464 */
465 int
466 _formi_find_bol(const char *string, unsigned int offset)
467 {
468 int cnt;
469
470 cnt = offset;
471 while ((cnt > 0) && (string[cnt] != '\n'))
472 cnt--;
473
474 /* if we moved and found a newline go forward one to point at the
475 * actual start of the line....
476 */
477 if ((cnt != offset) && (string[cnt] == '\n'))
478 cnt++;
479
480 return cnt;
481 }
482
483 /*
484 * Find the end of the current word in the string str, starting at
485 * offset - the end includes any trailing whitespace. If the end of
486 * the string is found before a new word then just return the offset
487 * to the end of the string.
488 */
489 static int
490 find_eow(char *str, unsigned int offset)
491 {
492 int start;
493
494 start = offset;
495 /* first skip any non-whitespace */
496 while ((str[start] != '\0') && !isblank(str[start]))
497 start++;
498
499 /* see if we hit the end of the string */
500 if (str[start] == '\0')
501 return start;
502
503 /* otherwise skip the whitespace.... */
504 while ((str[start] != '\0') && isblank(str[start]))
505 start++;
506
507 return start;
508 }
509
510 /*
511 * Find the beginning of the current word in the string str, starting
512 * at offset.
513 */
514 static int
515 find_sow(char *str, unsigned int offset)
516 {
517 int start;
518
519 start = offset;
520
521 if (start > 0) {
522 if (isblank(str[start]) || isblank(str[start - 1])) {
523 if (isblank(str[start - 1]))
524 start--;
525 /* skip the whitespace.... */
526 while ((start >= 0) && isblank(str[start]))
527 start--;
528 }
529 }
530
531 /* see if we hit the start of the string */
532 if (start < 0)
533 return 0;
534
535 /* now skip any non-whitespace */
536 while ((start >= 0) && !isblank(str[start]))
537 start--;
538
539 if (start > 0)
540 start++; /* last loop has us pointing at a space, adjust */
541
542 if (start < 0)
543 start = 0;
544
545 return start;
546 }
547
548 /*
549 * Scroll the field forward the given number of lines.
550 */
551 static void
552 _formi_scroll_fwd(FIELD *field, unsigned int amt)
553 {
554 /* check if we have lines to scroll */
555 if (field->row_count < (field->start_line + field->rows))
556 return;
557
558 field->start_line += min(amt,
559 field->row_count - field->start_line
560 - field->rows);
561 }
562
563 /*
564 * Scroll the field backward the given number of lines.
565 */
566 static void
567 _formi_scroll_back(FIELD *field, unsigned int amt)
568 {
569 if (field->start_line == 0)
570 return;
571
572 field->start_line -= min(field->start_line, amt);
573 }
574
575 /*
576 * Scroll the field forward the given number of characters.
577 */
578 void
579 _formi_hscroll_fwd(FIELD *field, int unsigned amt)
580 {
581 int end, scroll_amt;
582
583 end = _formi_find_eol(field->buffers[0].string,
584 field->start_char + field->hscroll
585 + field->cursor_xpos) - field->start_char
586 - field->hscroll - field->cursor_xpos;
587
588 scroll_amt = min(amt, end);
589 if (scroll_amt < 0)
590 scroll_amt = 0;
591
592 field->hscroll += scroll_amt;
593 if (amt > field->cursor_xpos)
594 field->cursor_xpos = 0;
595 else
596 field->cursor_xpos -= scroll_amt;
597 }
598
599 /*
600 * Scroll the field backward the given number of characters.
601 */
602 void
603 _formi_hscroll_back(FIELD *field, unsigned int amt)
604 {
605 int flen, sa;
606
607 sa = min(field->hscroll, amt);
608 field->hscroll -= sa;
609 field->cursor_xpos += sa;
610 flen = field->cols;
611 if (field->start_char > 0)
612 flen--;
613 if (field->cursor_xpos > flen)
614 field->cursor_xpos = flen;
615 }
616
617 /*
618 * Find the different pages in the form fields and assign the form
619 * page_starts array with the information to find them.
620 */
621 int
622 _formi_find_pages(FORM *form)
623 {
624 int i, cur_page = 0;
625
626 if ((form->page_starts = (_FORMI_PAGE_START *)
627 malloc((form->max_page + 1) * sizeof(_FORMI_PAGE_START))) == NULL)
628 return E_SYSTEM_ERROR;
629
630 /* initialise the page starts array */
631 memset(form->page_starts, 0,
632 (form->max_page + 1) * sizeof(_FORMI_PAGE_START));
633
634 for (i =0; i < form->field_count; i++) {
635 if (form->fields[i]->page_break == 1)
636 cur_page++;
637 if (form->page_starts[cur_page].in_use == 0) {
638 form->page_starts[cur_page].in_use = 1;
639 form->page_starts[cur_page].first = i;
640 form->page_starts[cur_page].last = i;
641 form->page_starts[cur_page].top_left = i;
642 form->page_starts[cur_page].bottom_right = i;
643 } else {
644 form->page_starts[cur_page].last = i;
645 form->page_starts[cur_page].top_left =
646 _formi_top_left(form,
647 form->page_starts[cur_page].top_left,
648 i);
649 form->page_starts[cur_page].bottom_right =
650 _formi_bottom_right(form,
651 form->page_starts[cur_page].bottom_right,
652 i);
653 }
654 }
655
656 return E_OK;
657 }
658
659 /*
660 * Completely redraw the field of the given form.
661 */
662 void
663 _formi_redraw_field(FORM *form, int field)
664 {
665 unsigned int pre, post, flen, slen, i, row, start, end, offset;
666 char *str;
667 FIELD *cur;
668 #ifdef DEBUG
669 char buffer[100];
670 #endif
671
672 cur = form->fields[field];
673 str = cur->buffers[0].string;
674 flen = cur->cols;
675 slen = 0;
676 start = 0;
677 end = 0;
678
679 wmove(form->subwin, (int) cur->form_row, (int) cur->form_col);
680 for (row = 1; row <= cur->row_count; row++) {
681 if (str == NULL) {
682 start = end = 0;
683 } else {
684 if ((str[end] == '\0') || (str[end + 1] == '\0')
685 || (row == 1))
686 start = end;
687 else
688 start = end + 1;
689 }
690
691 if (cur->buffers[0].length > 0) {
692 end = _formi_find_eol(str, start);
693 slen = end - start + 1;
694 } else
695 slen = 0;
696
697 switch (cur->justification) {
698 case JUSTIFY_RIGHT:
699 post = 0;
700 if (flen < slen)
701 pre = 0;
702 else
703 pre = flen - slen;
704
705 break;
706
707 case JUSTIFY_CENTER:
708 if (flen < slen) {
709 pre = 0;
710 post = 0;
711 } else {
712 pre = flen - slen;
713 post = pre = pre / 2;
714 /* get padding right if centring is not even */
715 if ((post + pre + slen) < flen)
716 post++;
717 }
718 break;
719
720 case NO_JUSTIFICATION:
721 case JUSTIFY_LEFT:
722 default:
723 pre = 0;
724 if (flen <= slen)
725 post = 0;
726 else {
727 post = flen - slen;
728 if (post > flen)
729 post = flen;
730 }
731 break;
732 }
733
734 if (pre > cur->hscroll - start)
735 pre = pre - cur->hscroll + start;
736 else
737 pre = 0;
738
739 if (slen > cur->hscroll) {
740 slen -= cur->hscroll;
741 post += cur->hscroll;
742 if (post > flen)
743 post = flen;
744 } else {
745 slen = 0;
746 post = flen - pre;
747 }
748
749 if (form->cur_field == field)
750 wattrset(form->subwin, cur->fore);
751 else
752 wattrset(form->subwin, cur->back);
753
754 #ifdef DEBUG
755 fprintf(dbg, "redraw_field: start=%d, pre=%d, slen=%d, flen=%d, post=%d, hscroll=%d\n",
756 start, pre, slen, flen, post, cur->hscroll);
757 if (str != NULL) {
758 strncpy(buffer, &str[cur->start_char], flen);
759 } else {
760 strcpy(buffer, "(null)");
761 }
762 buffer[flen] = '\0';
763 fprintf(dbg, "redraw_field: %s\n", buffer);
764 #endif
765
766 for (i = start + cur->hscroll; i < pre; i++)
767 waddch(form->subwin, cur->pad);
768
769 offset = cur->hscroll;
770 if (cur->start_char > 0)
771 offset += cur->start_char - 1;
772
773 if (flen > cur->hscroll + 1) {
774 if (flen > slen)
775 flen -= cur->hscroll + 1;
776 } else
777 flen = 0;
778
779 #ifdef DEBUG
780 fprintf(dbg, "redraw_field: will add %d chars, offset is %d\n",
781 min(slen, flen), offset);
782 #endif
783 for (i = 0;
784 i < min(slen, flen); i++)
785 {
786 #ifdef DEBUG
787 fprintf(dbg, "adding char str[%d]=%c\n",
788 i + offset, str[i + offset]);
789 #endif
790 waddch(form->subwin,
791 ((cur->opts & O_PUBLIC) == O_PUBLIC)?
792 str[i + offset] : cur->pad);
793 }
794
795 for (i = 0; i < post; i++)
796 waddch(form->subwin, cur->pad);
797 }
798
799 return;
800 }
801
802 /*
803 * Display the fields attached to the form that are on the current page
804 * on the screen.
805 *
806 */
807 int
808 _formi_draw_page(FORM *form)
809 {
810 int i;
811
812 if (form->page_starts[form->page].in_use == 0)
813 return E_BAD_ARGUMENT;
814
815 wclear(form->subwin);
816
817 for (i = form->page_starts[form->page].first;
818 i <= form->page_starts[form->page].last; i++)
819 _formi_redraw_field(form, i);
820
821 return E_OK;
822 }
823
824 /*
825 * Add the character c at the position pos in buffer 0 of the given field
826 */
827 int
828 _formi_add_char(FIELD *field, unsigned int pos, char c)
829 {
830 char *new;
831 unsigned int new_size;
832 int status;
833
834 /*
835 * If buffer has not had a string before, set it to a blank
836 * string. Everything should flow from there....
837 */
838 if (field->buffers[0].string == NULL) {
839 set_field_buffer(field, 0, "");
840 }
841
842 if (_formi_validate_char(field, c) != E_OK) {
843 #ifdef DEBUG
844 fprintf(dbg, "add_char: char %c failed char validation\n", c);
845 #endif
846 return E_INVALID_FIELD;
847 }
848
849 #ifdef DEBUG
850 fprintf(dbg, "add_char: pos=%d, char=%c\n", pos, c);
851 fprintf(dbg,
852 "add_char enter: xpos=%d, start=%d, length=%d(%d), allocated=%d\n",
853 field->cursor_xpos, field->start_char,
854 field->buffers[0].length, strlen(field->buffers[0].string),
855 field->buffers[0].allocated);
856 fprintf(dbg, "add_char enter: %s\n", field->buffers[0].string);
857 #endif
858 if (((field->opts & O_BLANK) == O_BLANK) &&
859 (field->buf0_status == FALSE)) {
860 field->buffers[0].length = 0;
861 field->buffers[0].string[0] = '\0';
862 pos = 0;
863 field->start_char = 0;
864 field->start_line = 0;
865 field->hscroll = 0;
866 field->row_count = 1;
867 field->cursor_xpos = 0;
868 field->cursor_ypos = 0;
869 }
870
871
872 if ((field->overlay == 0)
873 || ((field->overlay == 1) && (pos >= field->buffers[0].length))) {
874 if (field->buffers[0].length + 1
875 >= field->buffers[0].allocated) {
876 new_size = field->buffers[0].allocated + 64
877 - (field->buffers[0].allocated % 64);
878 if ((new = (char *) realloc(field->buffers[0].string,
879 new_size )) == NULL)
880 return E_SYSTEM_ERROR;
881 field->buffers[0].allocated = new_size;
882 field->buffers[0].string = new;
883 }
884 }
885
886 if ((field->overlay == 0) && (field->buffers[0].length > pos)) {
887 bcopy(&field->buffers[0].string[pos],
888 &field->buffers[0].string[pos + 1],
889 field->buffers[0].length - pos + 1);
890 }
891
892 field->buffers[0].string[pos] = c;
893 if (pos >= field->buffers[0].length) {
894 /* make sure the string is terminated if we are at the
895 * end of the string, the terminator would be missing
896 * if we are are at the end of the field.
897 */
898 field->buffers[0].string[pos + 1] = '\0';
899 }
900
901 /* only increment the length if we are inserting characters
902 * OR if we are at the end of the field in overlay mode.
903 */
904 if ((field->overlay == 0)
905 || ((field->overlay == 1) && (pos >= field->buffers[0].length)))
906 field->buffers[0].length++;
907
908 /* wrap the field, if needed */
909 status = _formi_wrap_field(field, pos);
910 if (status != E_OK) {
911 /* wrap failed for some reason, back out the char insert */
912 bcopy(&field->buffers[0].string[pos + 1],
913 &field->buffers[0].string[pos],
914 field->buffers[0].length - pos);
915 field->buffers[0].length--;
916 } else {
917 field->buf0_status = TRUE;
918 field->cursor_xpos++;
919 if (field->cursor_xpos > field->cols - 1) {
920 field->start_char++;
921 field->cursor_xpos = field->cols - 1;
922 }
923 }
924
925 #ifdef DEBUG
926 fprintf(dbg,
927 "add_char exit: xpos=%d, start=%d, length=%d(%d), allocated=%d\n",
928 field->cursor_xpos, field->start_char,
929 field->buffers[0].length, strlen(field->buffers[0].string),
930 field->buffers[0].allocated);
931 fprintf(dbg,"add_char exit: %s\n", field->buffers[0].string);
932 fprintf(dbg, "add_char exit: status = %s\n",
933 (status == E_OK)? "OK" : "FAILED");
934 #endif
935 return (status == E_OK);
936 }
937
938 /*
939 * Manipulate the text in a field, this takes the given form and performs
940 * the passed driver command on the current text field. Returns 1 if the
941 * text field was modified.
942 */
943 int
944 _formi_manipulate_field(FORM *form, int c)
945 {
946 FIELD *cur;
947 char *str;
948 unsigned int i, start, end, pos;
949
950 cur = form->fields[form->cur_field];
951
952 #ifdef DEBUG
953 fprintf(dbg,
954 "entry: xpos=%d, start_char=%d, length=%d, allocated=%d\n",
955 cur->cursor_xpos, cur->start_char, cur->buffers[0].length,
956 cur->buffers[0].allocated);
957 fprintf(dbg, "entry: string=");
958 if (cur->buffers[0].string == NULL)
959 fprintf(dbg, "(null)\n");
960 else
961 fprintf(dbg, "\"%s\"\n", cur->buffers[0].string);
962 #endif
963
964 /* Cannot manipulate a null string! */
965 if (cur->buffers[0].string == NULL)
966 return E_REQUEST_DENIED;
967
968 switch (c) {
969 case REQ_NEXT_CHAR:
970 if ((cur->cursor_xpos + cur->start_char
971 - ((cur->start_char > 0)? 1 : 0) + cur->hscroll + 1)
972 > cur->buffers[0].length) {
973 return E_REQUEST_DENIED;
974 }
975 cur->cursor_xpos++;
976 if (cur->cursor_xpos >= cur->cols - cur->hscroll - 1) {
977 if (cur->cols < (cur->hscroll + 1))
978 cur->cursor_xpos = 0;
979 else
980 cur->cursor_xpos = cur->cols
981 - cur->hscroll - 1;
982 cur->start_char++;
983 }
984 break;
985
986 case REQ_PREV_CHAR:
987 if (cur->cursor_xpos == 0) {
988 if (cur->start_char > 0)
989 cur->start_char--;
990 else if (cur->hscroll > 0)
991 cur->hscroll--;
992 else
993 return E_REQUEST_DENIED;
994 } else
995 cur->cursor_xpos--;
996 break;
997
998 case REQ_NEXT_LINE:
999 cur->cursor_ypos++;
1000 if (cur->cursor_ypos > cur->rows) {
1001 if ((cur->opts & O_STATIC) == O_STATIC) {
1002 if (cur->start_line + cur->cursor_ypos
1003 > cur->drows) {
1004 cur->cursor_ypos--;
1005 return E_REQUEST_DENIED;
1006 }
1007 } else {
1008 if (cur->start_line + cur->cursor_ypos
1009 > cur->nrows + cur->rows) {
1010 cur->cursor_ypos--;
1011 return E_REQUEST_DENIED;
1012 }
1013 }
1014 cur->start_line++;
1015 }
1016 break;
1017
1018 case REQ_PREV_LINE:
1019 if (cur->cursor_ypos == 0) {
1020 if (cur->start_line == 0)
1021 return E_REQUEST_DENIED;
1022 cur->start_line--;
1023 } else
1024 cur->cursor_ypos--;
1025 break;
1026
1027 case REQ_NEXT_WORD:
1028 start = cur->start_char + cur->cursor_xpos;
1029 str = cur->buffers[0].string;
1030
1031 start = find_eow(str, start);
1032
1033 /* check if we hit the end */
1034 if (str[start] == '\0')
1035 return E_REQUEST_DENIED;
1036
1037 /* otherwise we must have found the start of a word...*/
1038 if (start - cur->start_char < cur->cols) {
1039 cur->cursor_xpos = start;
1040 } else {
1041 cur->start_char = start;
1042 cur->cursor_xpos = 0;
1043 }
1044 break;
1045
1046 case REQ_PREV_WORD:
1047 start = cur->start_char + cur->cursor_xpos;
1048 if (cur->start_char > 0)
1049 start--;
1050
1051 if (start == 0)
1052 return E_REQUEST_DENIED;
1053
1054 str = cur->buffers[0].string;
1055
1056 start = find_sow(str, start);
1057
1058 if (start - cur->start_char > 0) {
1059 cur->cursor_xpos = start;
1060 } else {
1061 cur->start_char = start;
1062 cur->cursor_xpos = 0;
1063 }
1064 break;
1065
1066 case REQ_BEG_FIELD:
1067 cur->start_char = 0;
1068 cur->start_line = 0;
1069 cur->cursor_xpos = 0;
1070 cur->cursor_ypos = 0;
1071 break;
1072
1073 case REQ_END_FIELD:
1074 if (cur->row_count > cur->rows) {
1075 cur->start_line = cur->row_count - cur->rows;
1076 cur->cursor_ypos = cur->rows - 1;
1077 } else {
1078 cur->start_line = 0;
1079 cur->cursor_ypos = cur->row_count - 1;
1080 }
1081
1082 if ((str = rindex(cur->buffers[0].string, '\n')) == NULL) {
1083 cur->cursor_xpos = cur->cols - 1;
1084 if (cur->start_char < (cur->buffers[0].length +
1085 cur->cols)) {
1086 cur->start_char = 0;
1087 cur->cursor_xpos = cur->buffers[0].length;
1088 } else {
1089 cur->start_char = cur->buffers[0].length -
1090 cur->cols;
1091 }
1092 } else {
1093 cur->start_char = (str - cur->buffers[0].string);
1094 if (strlen(str) > cur->cols)
1095 cur->cursor_xpos = cur->cols;
1096 else
1097 cur->cursor_xpos = strlen(str);
1098 }
1099 break;
1100
1101 case REQ_BEG_LINE:
1102 start = cur->start_char + cur->cursor_xpos;
1103 if (cur->buffers[0].string[start] == '\n') {
1104 if (start > 0)
1105 start--;
1106 else
1107 return E_REQUEST_DENIED;
1108 }
1109
1110 while ((start > 0)
1111 && (cur->buffers[0].string[start] != '\n'))
1112 start--;
1113
1114 if (start > 0)
1115 start++;
1116
1117 cur->start_char = start;
1118 cur->cursor_xpos = 0;
1119 break;
1120
1121 case REQ_END_LINE:
1122 start = cur->start_char + cur->cursor_xpos;
1123 end = _formi_find_eol(cur->buffers[0].string, start);
1124 start = _formi_find_bol(cur->buffers[0].string, start);
1125
1126 if (end - start > cur->cols - 1) {
1127 cur->cursor_xpos = cur->cols - 1;
1128 cur->start_char = end - cur->cols + 3;
1129 } else {
1130 cur->cursor_xpos = end - start + 1;
1131 cur->start_char = start;
1132 }
1133 break;
1134
1135 case REQ_LEFT_CHAR:
1136 if ((cur->cursor_xpos == 0) && (cur->start_char == 0))
1137 return E_REQUEST_DENIED;
1138
1139 if (cur->cursor_xpos == 0) {
1140 cur->start_char--;
1141 if (cur->buffers[0].string[cur->start_char] == '\n') {
1142 if ((cur->cursor_ypos == 0) &&
1143 (cur->start_line == 0))
1144 {
1145 cur->start_char++;
1146 return E_REQUEST_DENIED;
1147 }
1148
1149 if (cur->cursor_ypos == 0)
1150 cur->start_line--;
1151 else
1152 cur->cursor_ypos--;
1153
1154 end = _formi_find_eol(cur->buffers[0].string,
1155 cur->start_char);
1156 start = _formi_find_bol(cur->buffers[0].string,
1157 cur->start_char);
1158 if (end - start >= cur->cols) {
1159 cur->cursor_xpos = cur->cols - 1;
1160 cur->start_char = end - cur->cols;
1161 } else {
1162 cur->cursor_xpos = end - start;
1163 cur->start_char = start;
1164 }
1165 }
1166 } else
1167 cur->cursor_xpos--;
1168 break;
1169
1170 case REQ_RIGHT_CHAR:
1171 pos = cur->start_char + cur->cursor_xpos;
1172 if (cur->buffers[0].string[pos] == '\0')
1173 return E_REQUEST_DENIED;
1174
1175 #ifdef DEBUG
1176 fprintf(dbg, "req_right_char enter: start=%d, xpos=%d, c=%c\n",
1177 cur->start_char, cur->cursor_xpos,
1178 cur->buffers[0].string[pos]);
1179 #endif
1180
1181 if (cur->buffers[0].string[pos] == '\n') {
1182 start = pos + 1;
1183 if (cur->buffers[0].string[start] == 0)
1184 return E_REQUEST_DENIED;
1185 end = _formi_find_eol(cur->buffers[0].string, start);
1186 if (end - start > cur->cols) {
1187 cur->cursor_xpos = cur->cols - 1;
1188 cur->start_char = end - cur->cols - 1;
1189 } else {
1190 cur->cursor_xpos = end - start;
1191 cur->start_char = start;
1192 }
1193 } else {
1194 if (cur->cursor_xpos == cur->cols - 1)
1195 cur->start_char++;
1196 else
1197 cur->cursor_xpos++;
1198 }
1199 #ifdef DEBUG
1200 fprintf(dbg, "req_right_char exit: start=%d, xpos=%d, c=%c\n",
1201 cur->start_char, cur->cursor_xpos,
1202 cur->buffers[0].string[cur->start_char +
1203 cur->cursor_xpos]);
1204 #endif
1205 break;
1206
1207 case REQ_UP_CHAR:
1208 if (cur->cursor_ypos == 0) {
1209 if (cur->start_line == 0)
1210 return E_REQUEST_DENIED;
1211
1212 cur->start_line--;
1213 } else
1214 cur->cursor_ypos--;
1215
1216 start = find_cur_line(cur);
1217 end = _formi_find_eol(cur->buffers[0].string, start);
1218 cur->start_char = start;
1219 if (cur->cursor_xpos > end - start)
1220 cur->cursor_xpos = end - start;
1221 break;
1222
1223 case REQ_DOWN_CHAR:
1224 if (cur->cursor_ypos == cur->rows - 1) {
1225 if (cur->start_line + cur->rows == cur->row_count)
1226 return E_REQUEST_DENIED;
1227 cur->start_line++;
1228 } else
1229 cur->cursor_ypos++;
1230
1231 start = find_cur_line(cur);
1232 end = _formi_find_eol(cur->buffers[0].string, start);
1233 cur->start_char = start;
1234 if (cur->cursor_xpos > end - start)
1235 cur->cursor_xpos = end - start;
1236 break;
1237
1238 case REQ_NEW_LINE:
1239 _formi_add_char(cur, cur->start_char + cur->cursor_xpos, '\n');
1240 cur->row_count++;
1241 break;
1242
1243 case REQ_INS_CHAR:
1244 _formi_add_char(cur, cur->start_char + cur->cursor_xpos,
1245 cur->pad);
1246 break;
1247
1248 case REQ_INS_LINE:
1249 start = _formi_find_bol(cur->buffers[0].string, cur->start_char);
1250 _formi_add_char(cur, start, '\n');
1251 cur->row_count++;
1252 break;
1253
1254 case REQ_DEL_CHAR:
1255 if (cur->buffers[0].string[cur->start_char + cur->cursor_xpos]
1256 == '\0')
1257 return E_REQUEST_DENIED;
1258
1259 start = cur->start_char + cur->cursor_xpos;
1260 end = cur->buffers[0].length;
1261 if (cur->buffers[0].string[start] == '\n') {
1262 if (cur->row_count > 1) {
1263 cur->row_count--;
1264 _formi_join_line(cur, cur->buffers[0].string,
1265 start, JOIN_NEXT);
1266 } else
1267 cur->buffers[0].string[start] = '\0';
1268 } else {
1269 bcopy(&cur->buffers[0].string[start + 1],
1270 &cur->buffers[0].string[start],
1271 (unsigned) end - start + 1);
1272 }
1273
1274 cur->buffers[0].length--;
1275 break;
1276
1277 case REQ_DEL_PREV:
1278 if ((cur->cursor_xpos == 0) && (cur->start_char == 0))
1279 return E_REQUEST_DENIED;
1280
1281 start = cur->cursor_xpos + cur->start_char;
1282 end = cur->buffers[0].length;
1283
1284 if (cur->buffers[0].string[cur->start_char + cur->cursor_xpos] == '\n') {
1285 _formi_join_line(cur, cur->buffers[0].string,
1286 cur->start_char + cur->cursor_xpos,
1287 JOIN_PREV);
1288 cur->row_count--;
1289 } else {
1290 bcopy(&cur->buffers[0].string[start],
1291 &cur->buffers[0].string[start - 1],
1292 (unsigned) end - start + 1);
1293 }
1294
1295 cur->buffers[0].length--;
1296 if ((cur->cursor_xpos == 0) && (cur->start_char > 0))
1297 cur->start_char--;
1298 else if ((cur->cursor_xpos == cur->cols - 1)
1299 && (cur->start_char > 0))
1300 cur->start_char--;
1301 else if (cur->cursor_xpos > 0)
1302 cur->cursor_xpos--;
1303
1304 break;
1305
1306 case REQ_DEL_LINE:
1307 start = cur->start_char + cur->cursor_xpos;
1308 end = _formi_find_eol(cur->buffers[0].string, start);
1309 start = _formi_find_bol(cur->buffers[0].string, start);
1310 bcopy(&cur->buffers[0].string[end + 1],
1311 &cur->buffers[0].string[start],
1312 (unsigned) cur->buffers[0].length - end + 1);
1313 if (cur->row_count > 1)
1314 cur->row_count--;
1315 break;
1316
1317 case REQ_DEL_WORD:
1318 start = cur->start_char + cur->cursor_xpos;
1319 end = find_eow(cur->buffers[0].string, start);
1320 start = find_sow(cur->buffers[0].string, start);
1321 bcopy(&cur->buffers[0].string[end + 1],
1322 &cur->buffers[0].string[start],
1323 (unsigned) cur->buffers[0].length - end + 1);
1324 cur->buffers[0].length -= end - start;
1325 break;
1326
1327 case REQ_CLR_EOL:
1328 /*XXXX this right or should we just toast the chars? */
1329 start = cur->start_char + cur->cursor_xpos;
1330 end = _formi_find_eol(cur->buffers[0].string, start);
1331 for (i = start; i < end; i++)
1332 cur->buffers[0].string[i] = cur->pad;
1333 break;
1334
1335 case REQ_CLR_EOF:
1336 for (i = cur->start_char + cur->cursor_xpos;
1337 i < cur->buffers[0].length; i++)
1338 cur->buffers[0].string[i] = cur->pad;
1339 break;
1340
1341 case REQ_CLR_FIELD:
1342 for (i = 0; i < cur->buffers[0].length; i++)
1343 cur->buffers[0].string[i] = cur->pad;
1344 break;
1345
1346 case REQ_OVL_MODE:
1347 cur->overlay = 1;
1348 break;
1349
1350 case REQ_INS_MODE:
1351 cur->overlay = 0;
1352 break;
1353
1354 case REQ_SCR_FLINE:
1355 _formi_scroll_fwd(cur, 1);
1356 break;
1357
1358 case REQ_SCR_BLINE:
1359 _formi_scroll_back(cur, 1);
1360 break;
1361
1362 case REQ_SCR_FPAGE:
1363 _formi_scroll_fwd(cur, cur->rows);
1364 break;
1365
1366 case REQ_SCR_BPAGE:
1367 _formi_scroll_back(cur, cur->rows);
1368 break;
1369
1370 case REQ_SCR_FHPAGE:
1371 _formi_scroll_fwd(cur, cur->rows / 2);
1372 break;
1373
1374 case REQ_SCR_BHPAGE:
1375 _formi_scroll_back(cur, cur->rows / 2);
1376 break;
1377
1378 case REQ_SCR_FCHAR:
1379 _formi_hscroll_fwd(cur, 1);
1380 break;
1381
1382 case REQ_SCR_BCHAR:
1383 _formi_hscroll_back(cur, 1);
1384 break;
1385
1386 case REQ_SCR_HFLINE:
1387 _formi_hscroll_fwd(cur, cur->cols);
1388 break;
1389
1390 case REQ_SCR_HBLINE:
1391 _formi_hscroll_back(cur, cur->cols);
1392 break;
1393
1394 case REQ_SCR_HFHALF:
1395 _formi_hscroll_fwd(cur, cur->cols / 2);
1396 break;
1397
1398 case REQ_SCR_HBHALF:
1399 _formi_hscroll_back(cur, cur->cols / 2);
1400 break;
1401
1402 default:
1403 return 0;
1404 }
1405
1406 #ifdef DEBUG
1407 fprintf(dbg, "exit: xpos=%d, start_char=%d, length=%d, allocated=%d\n",
1408 cur->cursor_xpos, cur->start_char, cur->buffers[0].length,
1409 cur->buffers[0].allocated);
1410 fprintf(dbg, "exit: string=\"%s\"\n", cur->buffers[0].string);
1411 #endif
1412 return 1;
1413 }
1414
1415 /*
1416 * Validate the give character by passing it to any type character
1417 * checking routines, if they exist.
1418 */
1419 int
1420 _formi_validate_char(FIELD *field, char c)
1421 {
1422 int ret_val;
1423
1424 if (field->type == NULL)
1425 return E_OK;
1426
1427 ret_val = E_INVALID_FIELD;
1428 _formi_do_char_validation(field, field->type, c, &ret_val);
1429
1430 return ret_val;
1431 }
1432
1433
1434 /*
1435 * Perform the validation of the character, invoke all field_type validation
1436 * routines. If the field is ok then update ret_val to E_OK otherwise
1437 * ret_val is not changed.
1438 */
1439 static void
1440 _formi_do_char_validation(FIELD *field, FIELDTYPE *type, char c, int *ret_val)
1441 {
1442 if ((type->flags & _TYPE_IS_LINKED) == _TYPE_IS_LINKED) {
1443 _formi_do_char_validation(field, type->link->next, c, ret_val);
1444 _formi_do_char_validation(field, type->link->prev, c, ret_val);
1445 } else {
1446 if (type->char_check == NULL)
1447 *ret_val = E_OK;
1448 else {
1449 if (type->char_check((int)(unsigned char) c,
1450 field->args) == TRUE)
1451 *ret_val = E_OK;
1452 }
1453 }
1454 }
1455
1456 /*
1457 * Validate the current field. If the field validation returns success then
1458 * return E_OK otherwise return E_INVALID_FIELD.
1459 *
1460 */
1461 int
1462 _formi_validate_field(FORM *form)
1463 {
1464 FIELD *cur;
1465 char *bp;
1466 int ret_val, count;
1467
1468
1469 if ((form == NULL) || (form->fields == NULL) ||
1470 (form->fields[0] == NULL))
1471 return E_INVALID_FIELD;
1472
1473 cur = form->fields[form->cur_field];
1474
1475 if (((cur->opts & O_PASSOK) == O_PASSOK) && (cur->buf0_status = 0))
1476 return E_OK;
1477
1478 bp = cur->buffers[0].string;
1479 count = _formi_skip_blanks(bp, 0);
1480
1481 /* check if we have a null field, depending on the nullok flag
1482 * this may be acceptable or not....
1483 */
1484 if (cur->buffers[0].string[count] == '\0') {
1485 if ((cur->opts & O_NULLOK) == O_NULLOK)
1486 return E_OK;
1487 else
1488 return E_INVALID_FIELD;
1489 }
1490
1491 /* if there is no type then just accept the field */
1492 if (cur->type == NULL)
1493 return E_OK;
1494
1495 ret_val = E_INVALID_FIELD;
1496 _formi_do_validation(cur, cur->type, &ret_val);
1497
1498 return ret_val;
1499 }
1500
1501 /*
1502 * Perform the validation of the field, invoke all field_type validation
1503 * routines. If the field is ok then update ret_val to E_OK otherwise
1504 * ret_val is not changed.
1505 */
1506 static void
1507 _formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val)
1508 {
1509 if ((type->flags & _TYPE_IS_LINKED) == _TYPE_IS_LINKED) {
1510 _formi_do_validation(field, type->link->next, ret_val);
1511 _formi_do_validation(field, type->link->prev, ret_val);
1512 } else {
1513 if (type->field_check == NULL)
1514 *ret_val = E_OK;
1515 else {
1516 if (type->field_check(field, field_buffer(field, 0))
1517 == TRUE)
1518 *ret_val = E_OK;
1519 }
1520 }
1521 }
1522
1523 /*
1524 * Select the next/previous choice for the field, the driver command
1525 * selecting the direction will be passed in c. Return 1 if a choice
1526 * selection succeeded, 0 otherwise.
1527 */
1528 int
1529 _formi_field_choice(FORM *form, int c)
1530 {
1531 FIELDTYPE *type;
1532 FIELD *field;
1533
1534 if ((form == NULL) || (form->fields == NULL) ||
1535 (form->fields[0] == NULL) ||
1536 (form->fields[form->cur_field]->type == NULL))
1537 return 0;
1538
1539 field = form->fields[form->cur_field];
1540 type = field->type;
1541
1542 switch (c) {
1543 case REQ_NEXT_CHOICE:
1544 if (type->next_choice == NULL)
1545 return 0;
1546 else
1547 return type->next_choice(field,
1548 field_buffer(field, 0));
1549
1550 case REQ_PREV_CHOICE:
1551 if (type->prev_choice == NULL)
1552 return 0;
1553 else
1554 return type->prev_choice(field,
1555 field_buffer(field, 0));
1556
1557 default: /* should never happen! */
1558 return 0;
1559 }
1560 }
1561
1562 /*
1563 * Update the fields if they have changed. The parameter old has the
1564 * previous current field as the current field may have been updated by
1565 * the driver. Return 1 if the form page needs updating.
1566 *
1567 */
1568 int
1569 _formi_update_field(FORM *form, int old_field)
1570 {
1571 int cur, i;
1572
1573 cur = form->cur_field;
1574
1575 if (old_field != cur) {
1576 if (!((cur >= form->page_starts[form->page].first) &&
1577 (cur <= form->page_starts[form->page].last))) {
1578 /* not on same page any more */
1579 for (i = 0; i < form->max_page; i++) {
1580 if ((form->page_starts[i].in_use == 1) &&
1581 (form->page_starts[i].first <= cur) &&
1582 (form->page_starts[i].last >= cur)) {
1583 form->page = i;
1584 return 1;
1585 }
1586 }
1587 }
1588 }
1589
1590 _formi_redraw_field(form, old_field);
1591 _formi_redraw_field(form, form->cur_field);
1592 return 0;
1593 }
1594
1595 /*
1596 * Compare function for the field sorting
1597 *
1598 */
1599 static int
1600 field_sort_compare(const void *one, const void *two)
1601 {
1602 const FIELD *a, *b;
1603 int tl;
1604
1605 /* LINTED const castaway; we don't modify these! */
1606 a = (const FIELD *) *((const FIELD **) one);
1607 b = (const FIELD *) *((const FIELD **) two);
1608
1609 if (a == NULL)
1610 return 1;
1611
1612 if (b == NULL)
1613 return -1;
1614
1615 /*
1616 * First check the page, we want the fields sorted by page.
1617 *
1618 */
1619 if (a->page != b->page)
1620 return ((a->page > b->page)? 1 : -1);
1621
1622 tl = _formi_top_left(a->parent, a->index, b->index);
1623
1624 /*
1625 * sort fields left to right, top to bottom so the top left is
1626 * the less than value....
1627 */
1628 return ((tl == a->index)? -1 : 1);
1629 }
1630
1631 /*
1632 * Sort the fields in a form ready for driver traversal.
1633 */
1634 void
1635 _formi_sort_fields(FORM *form)
1636 {
1637 FIELD **sort_area;
1638 int i;
1639
1640 CIRCLEQ_INIT(&form->sorted_fields);
1641
1642 if ((sort_area = (FIELD **) malloc(sizeof(FIELD *) * form->field_count))
1643 == NULL)
1644 return;
1645
1646 bcopy(form->fields, sort_area, sizeof(FIELD *) * form->field_count);
1647 qsort(sort_area, (unsigned) form->field_count, sizeof(FIELD *),
1648 field_sort_compare);
1649
1650 for (i = 0; i < form->field_count; i++)
1651 CIRCLEQ_INSERT_TAIL(&form->sorted_fields, sort_area[i], glue);
1652
1653 free(sort_area);
1654 }
1655
1656 /*
1657 * Set the neighbours for all the fields in the given form.
1658 */
1659 void
1660 _formi_stitch_fields(FORM *form)
1661 {
1662 int above_row, below_row, end_above, end_below, cur_row, real_end;
1663 FIELD *cur, *above, *below;
1664
1665 /*
1666 * check if the sorted fields circle queue is empty, just
1667 * return if it is.
1668 */
1669 if (CIRCLEQ_EMPTY(&form->sorted_fields))
1670 return;
1671
1672 /* initially nothing is above..... */
1673 above_row = -1;
1674 end_above = TRUE;
1675 above = NULL;
1676
1677 /* set up the first field as the current... */
1678 cur = CIRCLEQ_FIRST(&form->sorted_fields);
1679 cur_row = cur->form_row;
1680
1681 /* find the first field on the next row if any */
1682 below = CIRCLEQ_NEXT(cur, glue);
1683 below_row = -1;
1684 end_below = TRUE;
1685 real_end = TRUE;
1686 while (below != (void *)&form->sorted_fields) {
1687 if (below->form_row != cur_row) {
1688 below_row = below->form_row;
1689 end_below = FALSE;
1690 real_end = FALSE;
1691 break;
1692 }
1693 below = CIRCLEQ_NEXT(below, glue);
1694 }
1695
1696 /* walk the sorted fields, setting the neighbour pointers */
1697 while (cur != (void *) &form->sorted_fields) {
1698 if (cur == CIRCLEQ_FIRST(&form->sorted_fields))
1699 cur->left = NULL;
1700 else
1701 cur->left = CIRCLEQ_PREV(cur, glue);
1702
1703 if (cur == CIRCLEQ_LAST(&form->sorted_fields))
1704 cur->right = NULL;
1705 else
1706 cur->right = CIRCLEQ_NEXT(cur, glue);
1707
1708 if (end_above == TRUE)
1709 cur->up = NULL;
1710 else {
1711 cur->up = above;
1712 above = CIRCLEQ_NEXT(above, glue);
1713 if (above_row != above->form_row) {
1714 end_above = TRUE;
1715 above_row = above->form_row;
1716 }
1717 }
1718
1719 if (end_below == TRUE)
1720 cur->down = NULL;
1721 else {
1722 cur->down = below;
1723 below = CIRCLEQ_NEXT(below, glue);
1724 if (below == (void *) &form->sorted_fields) {
1725 end_below = TRUE;
1726 real_end = TRUE;
1727 } else if (below_row != below->form_row) {
1728 end_below = TRUE;
1729 below_row = below->form_row;
1730 }
1731 }
1732
1733 cur = CIRCLEQ_NEXT(cur, glue);
1734 if ((cur != (void *) &form->sorted_fields)
1735 && (cur_row != cur->form_row)) {
1736 cur_row = cur->form_row;
1737 if (end_above == FALSE) {
1738 for (; above != CIRCLEQ_FIRST(&form->sorted_fields);
1739 above = CIRCLEQ_NEXT(above, glue)) {
1740 if (above->form_row != above_row) {
1741 above_row = above->form_row;
1742 break;
1743 }
1744 }
1745 } else if (above == NULL) {
1746 above = CIRCLEQ_FIRST(&form->sorted_fields);
1747 end_above = FALSE;
1748 above_row = above->form_row;
1749 } else
1750 end_above = FALSE;
1751
1752 if (end_below == FALSE) {
1753 while (below_row == below->form_row) {
1754 below = CIRCLEQ_NEXT(below,
1755 glue);
1756 if (below ==
1757 (void *)&form->sorted_fields) {
1758 real_end = TRUE;
1759 end_below = TRUE;
1760 break;
1761 }
1762 }
1763
1764 if (below != (void *)&form->sorted_fields)
1765 below_row = below->form_row;
1766 } else if (real_end == FALSE)
1767 end_below = FALSE;
1768
1769 }
1770 }
1771 }
1772