field.c revision 1.12 1 /* $NetBSD: field.c,v 1.12 2001/06/13 10:45:58 wiz Exp $ */
2 /*-
3 * Copyright (c) 1998-1999 Brett Lymn
4 * (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
5 * All rights reserved.
6 *
7 * This code has been donated to The NetBSD Foundation by the Author.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *
29 */
30
31 #include <stdlib.h>
32 #include <strings.h>
33 #include <form.h>
34 #include "internals.h"
35
36 extern FORM _formi_default_form;
37
38 FIELD _formi_default_field = {
39 0, /* rows in the field */
40 0, /* columns in the field */
41 0, /* dynamic rows */
42 0, /* dynamic columns */
43 0, /* maximum growth */
44 0, /* starting row in the form subwindow */
45 0, /* starting column in the form subwindow */
46 0, /* number of off screen rows */
47 0, /* index of this field in form fields array. */
48 0, /* number of buffers associated with this field */
49 FALSE, /* set to true if buffer 0 has changed. */
50 NO_JUSTIFICATION, /* justification style of the field */
51 FALSE, /* set to true if field is in overlay mode */
52 0, /* starting char in string (horiz scroll) */
53 0, /* starting line in field (vert scroll) */
54 0, /* number of rows actually used in field */
55 0, /* x pos of cursor in field */
56 0, /* y pos of cursor in field */
57 0, /* start of a new page on the form if 1 */
58 0, /* number of the page this field is on */
59 A_NORMAL, /* character attributes for the foreground */
60 A_NORMAL, /* character attributes for the background */
61 ' ', /* padding character */
62 DEFAULT_FORM_OPTS, /* options for the field */
63 NULL, /* the form this field is bound to, if any */
64 NULL, /* field above this one */
65 NULL, /* field below this one */
66 NULL, /* field to the left of this one */
67 NULL, /* field to the right of this one */
68 NULL, /* user defined pointer. */
69 NULL, /* used if fields are linked */
70 NULL, /* type struct for the field */
71 {NULL, NULL}, /* circle queue glue for sorting fields */
72 NULL, /* args for field type. */
73 0, /* number of allocated slots in lines array */
74 NULL, /* pointer to the array of lines structures. */
75 NULL, /* array of buffers for the field */
76 };
77
78 /* internal function prototypes */
79 static FIELD *
80 _formi_create_field(FIELD *, int, int, int, int, int, int);
81
82
83 /*
84 * Set the userptr for the field
85 */
86 int
87 set_field_userptr(FIELD *field, void *ptr)
88 {
89 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
90
91 fp->userptr = ptr;
92
93 return E_OK;
94 }
95
96 /*
97 * Return the userptr for the field.
98 */
99
100 void *
101 field_userptr(FIELD *field)
102 {
103 if (field == NULL)
104 return _formi_default_field.userptr;
105 else
106 return field->userptr;
107 }
108
109 /*
110 * Set the options for the designated field.
111 */
112 int
113 set_field_opts(FIELD *field, Form_Options options)
114 {
115 int i;
116
117 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
118
119 /* not allowed to set opts if the field is the current one */
120 if ((field != NULL) && (field->parent != NULL) &&
121 (field->parent->cur_field == field->index))
122 return E_CURRENT;
123
124 if ((options & O_STATIC) == O_STATIC) {
125 for (i = 0; i < field->nbuf; i++) {
126 if (field->buffers[i].length > field->cols)
127 field->buffers[i].string[field->cols] = '\0';
128 }
129 }
130
131 fp->opts = options;
132
133 return E_OK;
134 }
135
136 /*
137 * Turn on the passed field options.
138 */
139 int
140 field_opts_on(FIELD *field, Form_Options options)
141 {
142 int i;
143
144 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
145
146 /* not allowed to set opts if the field is the current one */
147 if ((field != NULL) && (field->parent != NULL) &&
148 (field->parent->cur_field == field->index))
149 return E_CURRENT;
150
151 if ((options & O_STATIC) == O_STATIC) {
152 for (i = 0; i < field->nbuf; i++) {
153 if (field->buffers[i].length > field->cols)
154 field->buffers[i].string[field->cols] = '\0';
155 }
156 }
157
158 fp->opts |= options;
159
160 return E_OK;
161 }
162
163 /*
164 * Turn off the passed field options.
165 */
166 int
167 field_opts_off(FIELD *field, Form_Options options)
168 {
169 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
170
171 /* not allowed to set opts if the field is the current one */
172 if ((field != NULL) && (field->parent != NULL) &&
173 (field->parent->cur_field == field->index))
174 return E_CURRENT;
175
176 fp->opts &= ~options;
177 return E_OK;
178 }
179
180 /*
181 * Return the field options associated with the passed field.
182 */
183 Form_Options
184 field_opts(FIELD *field)
185 {
186 if (field == NULL)
187 return _formi_default_field.opts;
188 else
189 return field->opts;
190 }
191
192 /*
193 * Set the justification for the passed field.
194 */
195 int
196 set_field_just(FIELD *field, int justification)
197 {
198 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
199
200 /*
201 * not allowed to set justification if the field is
202 * the current one
203 */
204 if ((field != NULL) && (field->parent != NULL) &&
205 (field->parent->cur_field == field->index))
206 return E_CURRENT;
207
208 if ((justification < MIN_JUST_STYLE) /* check justification valid */
209 || (justification > MAX_JUST_STYLE))
210 return E_BAD_ARGUMENT;
211
212 fp->justification = justification;
213 return E_OK;
214 }
215
216 /*
217 * Return the justification style of the field passed.
218 */
219 int
220 field_just(FIELD *field)
221 {
222 if (field == NULL)
223 return _formi_default_field.justification;
224 else
225 return field->justification;
226 }
227
228 /*
229 * Return information about the field passed.
230 */
231 int
232 field_info(FIELD *field, int *rows, int *cols, int *frow, int *fcol,
233 int *nrow, int *nbuf)
234 {
235 if (field == NULL)
236 return E_BAD_ARGUMENT;
237
238 *rows = field->rows;
239 *cols = field->cols;
240 *frow = field->form_row;
241 *fcol = field->form_col;
242 *nrow = field->nrows;
243 *nbuf = field->nbuf;
244
245 return E_OK;
246 }
247
248 /*
249 * Report the dynamic field information.
250 */
251 int
252 dynamic_field_info(FIELD *field, int *drows, int *dcols, int *max)
253 {
254 if (field == NULL)
255 return E_BAD_ARGUMENT;
256
257 if ((field->opts & O_STATIC) == O_STATIC) {
258 *drows = field->rows;
259 *dcols = field->cols;
260 } else {
261 *drows = field->drows;
262 *dcols = field->dcols;
263 }
264
265 *max = field->max;
266
267 return E_OK;
268 }
269
270 /*
271 * Set the value of the field buffer to the value given.
272 */
273
274 int
275 set_field_buffer(FIELD *field, int buffer, char *value)
276 {
277 unsigned len;
278 int status;
279
280 if (field == NULL)
281 return E_BAD_ARGUMENT;
282
283 if (buffer >= field->nbuf) /* make sure buffer is valid */
284 return E_BAD_ARGUMENT;
285
286 len = strlen(value);
287 if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
288 && ((field->rows + field->nrows) == 1))
289 len = field->cols;
290
291 #ifdef DEBUG
292 if (_formi_create_dbg_file() != E_OK)
293 return E_SYSTEM_ERROR;
294
295 fprintf(dbg,
296 "set_field_buffer: entry: len = %d, value = %s, buffer=%d\n",
297 len, value, buffer);
298 fprintf(dbg, "set_field_buffer: entry: string = ");
299 if (field->buffers[buffer].string != NULL)
300 fprintf(dbg, "%s, len = %d\n", field->buffers[buffer].string,
301 field->buffers[buffer].length);
302 else
303 fprintf(dbg, "(null), len = 0\n");
304 fprintf(dbg, "set_field_buffer: entry: lines.len = %d\n",
305 field->lines[0].length);
306 #endif
307
308 if ((field->buffers[buffer].string =
309 (char *) realloc(field->buffers[buffer].string, len + 1)) == NULL)
310 return E_SYSTEM_ERROR;
311
312 strlcpy(field->buffers[buffer].string, value, len + 1);
313 field->buffers[buffer].length = len;
314 field->buffers[buffer].allocated = len + 1;
315
316 if (buffer == 0) {
317 field->row_count = 1; /* must be at least one row */
318 field->lines[0].start = 0;
319 field->lines[0].end = (len > 0)? (len - 1) : 0;
320 field->lines[0].length = len;
321
322 /* we have to hope the wrap works - if it does not then the
323 buffer is pretty much borked */
324 status = _formi_wrap_field(field, 0);
325 if (status != E_OK)
326 return status;
327
328 /* redraw the field to reflect the new contents. If the field
329 * is attached....
330 */
331 if (field->parent != NULL)
332 _formi_redraw_field(field->parent, field->index);
333 }
334
335 #ifdef DEBUG
336 fprintf(dbg, "set_field_buffer: exit: len = %d, value = %s\n",
337 len, value);
338 fprintf(dbg, "set_field_buffer: exit: string = %s, len = %d\n",
339 field->buffers[buffer].string, field->buffers[buffer].length);
340 fprintf(dbg, "set_field_buffer: exit: lines.len = %d\n",
341 field->lines[0].length);
342 #endif
343
344 return E_OK;
345 }
346
347 /*
348 * Return the requested field buffer to the caller.
349 */
350 char *
351 field_buffer(FIELD *field, int buffer)
352 {
353
354 if (field == NULL)
355 return NULL;
356
357 if (buffer >= field->nbuf)
358 return NULL;
359
360 return field->buffers[buffer].string;
361 }
362
363 /*
364 * Set the buffer 0 field status.
365 */
366 int
367 set_field_status(FIELD *field, int status)
368 {
369
370 if (field == NULL)
371 return E_BAD_ARGUMENT;
372
373 if (status != FALSE)
374 field->buf0_status = TRUE;
375 else
376 field->buf0_status = FALSE;
377
378 return E_OK;
379 }
380
381 /*
382 * Return the buffer 0 status flag for the given field.
383 */
384 int
385 field_status(FIELD *field)
386 {
387
388 if (field == NULL) /* the default buffer 0 never changes :-) */
389 return FALSE;
390
391 return field->buf0_status;
392 }
393
394 /*
395 * Set the maximum growth for a dynamic field.
396 */
397 int
398 set_max_field(FIELD *fptr, int max)
399 {
400 FIELD *field = (field == NULL)? &_formi_default_field : fptr;
401
402 if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
403 return E_BAD_ARGUMENT;
404
405 if (max < 0) /* negative numbers are bad.... */
406 return E_BAD_ARGUMENT;
407
408 field->max = max;
409 return E_OK;
410 }
411
412 /*
413 * Set the field foreground character attributes.
414 */
415 int
416 set_field_fore(FIELD *fptr, chtype attribute)
417 {
418 FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
419
420 field->fore = attribute;
421 return E_OK;
422 }
423
424 /*
425 * Return the foreground character attribute for the given field.
426 */
427 chtype
428 field_fore(FIELD *field)
429 {
430 if (field == NULL)
431 return _formi_default_field.fore;
432 else
433 return field->fore;
434 }
435
436 /*
437 * Set the background character attribute for the given field.
438 */
439 int
440 set_field_back(FIELD *field, chtype attribute)
441 {
442 if (field == NULL)
443 _formi_default_field.back = attribute;
444 else
445 field->back = attribute;
446
447 return E_OK;
448 }
449
450 /*
451 * Set the pad character for the given field.
452 */
453 int
454 set_field_pad(FIELD *field, int pad)
455 {
456 if (field == NULL)
457 _formi_default_field.pad = pad;
458 else
459 field->pad = pad;
460
461 return E_OK;
462 }
463
464 /*
465 * Return the padding character for the given field.
466 */
467 int
468 field_pad(FIELD *field)
469 {
470 if (field == NULL)
471 return _formi_default_field.pad;
472 else
473 return field->pad;
474 }
475
476 /*
477 * Set the field initialisation function hook.
478 */
479 int
480 set_field_init(FORM *form, Form_Hook function)
481 {
482 if (form == NULL)
483 _formi_default_form.field_init = function;
484 else
485 form->field_init = function;
486
487 return E_OK;
488 }
489
490 /*
491 * Return the function hook for the field initialisation.
492 */
493 Form_Hook
494 field_init(FORM *form)
495 {
496 if (form == NULL)
497 return _formi_default_form.field_init;
498 else
499 return form->field_init;
500 }
501
502 /*
503 * Set the field termination function hook.
504 */
505 int
506 set_field_term(FORM *form, Form_Hook function)
507 {
508 if (form == NULL)
509 _formi_default_form.field_term = function;
510 else
511 form->field_term = function;
512
513 return E_OK;
514 }
515
516 /*
517 * Return the function hook defined for the field termination.
518 */
519 Form_Hook
520 field_term(FORM *form)
521 {
522 if (form == NULL)
523 return _formi_default_form.field_term;
524 else
525 return form->field_term;
526 }
527
528 /*
529 * Set the page flag on the given field to indicate it is the start of a
530 * new page.
531 */
532 int
533 set_new_page(FIELD *fptr, int page)
534 {
535 FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
536
537 if (field->parent != NULL) /* check if field is connected to a form */
538 return E_CONNECTED;
539
540 field->page_break = (page != FALSE);
541 return E_OK;
542 }
543
544 /*
545 * Return the page status for the given field. TRUE is returned if the
546 * field is the start of a new page.
547 */
548 int
549 new_page(FIELD *field)
550 {
551 if (field == NULL)
552 return _formi_default_field.page_break;
553 else
554 return field->page_break;
555 }
556
557 /*
558 * Return the index of the field in the form fields array.
559 */
560 int
561 field_index(FIELD *field)
562 {
563 if (field == NULL)
564 return -1;
565
566 if (field->parent == NULL)
567 return -1;
568
569 return field->index;
570 }
571
572 /*
573 * Internal function that does most of the work to create a new field.
574 * The new field is initialised from the information in the prototype
575 * field passed.
576 * Returns NULL on error.
577 */
578 static FIELD *
579 _formi_create_field(FIELD *prototype, int rows, int cols, int frow,
580 int fcol, int nrows, int nbuf)
581 {
582 FIELD *new;
583
584 if ((rows <= 0) || (cols <= 0) || (frow < 0) || (fcol < 0) ||
585 (nrows < 0) || (nbuf < 0))
586 return NULL;
587
588 if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) {
589 return NULL;
590 }
591
592 /* copy in the default field info */
593 bcopy(prototype, new, sizeof(FIELD));
594
595 new->nbuf = nbuf + 1;
596 new->rows = rows;
597 new->cols = cols;
598 new->form_row = frow;
599 new->form_col = fcol;
600 new->nrows = nrows;
601 new->link = new;
602 return new;
603 }
604
605 /*
606 * Create a new field structure.
607 */
608 FIELD *
609 new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf)
610 {
611 FIELD *new;
612 size_t buf_len;
613 int i;
614
615
616 if ((new = _formi_create_field(&_formi_default_field, rows, cols,
617 frow, fcol, nrows, nbuf)) == NULL)
618 return NULL;
619
620 buf_len = (nbuf + 1) * sizeof(FORM_STR);
621
622 if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
623 free(new);
624 return NULL;
625 }
626
627 /* Initialise the strings to a zero length string */
628 for (i = 0; i < nbuf + 1; i++) {
629 if ((new->buffers[i].string =
630 (char *) malloc(sizeof(char))) == NULL) {
631 free(new->buffers);
632 free(new);
633 return NULL;
634 }
635 new->buffers[i].string[0] = '\0';
636 new->buffers[i].length = 0;
637 new->buffers[i].allocated = 1;
638 }
639
640 if ((new->lines = (_FORMI_FIELD_LINES *)
641 malloc(sizeof(struct _formi_field_lines))) == NULL) {
642 free(new->buffers);
643 free(new);
644 return NULL;
645 }
646
647 new->lines_alloced = 1;
648 new->lines[0].length = 0;
649 new->lines[0].start = 0;
650 new->lines[0].end = 0;
651
652 return new;
653 }
654
655 /*
656 * Duplicate the given field, including it's buffers.
657 */
658 FIELD *
659 dup_field(FIELD *field, int frow, int fcol)
660 {
661 FIELD *new;
662 size_t row_len, buf_len;
663
664 if (field == NULL)
665 return NULL;
666
667 if ((new = _formi_create_field(field, (int) field->rows,
668 (int ) field->cols,
669 frow, fcol, (int) field->nrows,
670 field->nbuf - 1)) == NULL)
671 return NULL;
672
673 row_len = (field->rows + field->nrows + 1) * field->cols;
674 buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR);
675
676 if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
677 free(new);
678 return NULL;
679 }
680
681 /* copy the buffers from the source field into the new copy */
682 bcopy(field->buffers, new->buffers, buf_len);
683
684 return new;
685 }
686
687 /*
688 * Create a new field at the specified location by duplicating the given
689 * field. The buffers are shared with the parent field.
690 */
691 FIELD *
692 link_field(FIELD *field, int frow, int fcol)
693 {
694 FIELD *new;
695
696 if (field == NULL)
697 return NULL;
698
699 if ((new = _formi_create_field(field, (int) field->rows,
700 (int) field->cols,
701 frow, fcol, (int) field->nrows,
702 field->nbuf - 1)) == NULL)
703 return NULL;
704
705 new->link = field->link;
706 field->link = new;
707
708 /* we are done. The buffer pointer was copied during the field
709 creation. */
710 return new;
711 }
712
713 /*
714 * Release all storage allocated to the field
715 */
716 int
717 free_field(FIELD *field)
718 {
719 FIELD *flink;
720
721 if (field == NULL)
722 return E_BAD_ARGUMENT;
723
724 if (field->parent != NULL)
725 return E_CONNECTED;
726
727 if (field->link == field) { /* check if field linked */
728 /* no it is not - release the buffers */
729 free(field->buffers);
730 } else {
731 /* is linked, traverse the links to find the field referring
732 * to the one to be freed.
733 */
734 for (flink = field->link; flink != field; flink = flink->link);
735 flink->link = field->link;
736 }
737
738 free(field);
739 return E_OK;
740 }
741
742
743