xref: /freebsd/contrib/ncurses/form/frm_data.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *   Author: Juergen Pfeifer <juergen.pfeifer@gmx.net> 1995,1997            *
31  ****************************************************************************/
32 
33 #include "form.priv.h"
34 
35 MODULE_ID("$Id: frm_data.c,v 1.6 1999/05/16 17:20:29 juergen Exp $")
36 
37 /*---------------------------------------------------------------------------
38 |   Facility      :  libnform
39 |   Function      :  bool data_behind(const FORM *form)
40 |
41 |   Description   :  Check for off-screen data behind. This is nearly trivial
42 |                    becose the begin of a field is fixed.
43 |
44 |   Return Values :  TRUE   - there are off-screen data behind
45 |                    FALSE  - there are no off-screen data behind
46 +--------------------------------------------------------------------------*/
47 bool data_behind(const FORM *form)
48 {
49   bool result = FALSE;
50 
51   if (form && (form->status & _POSTED) && form->current)
52     {
53       FIELD *field;
54 
55       field = form->current;
56       if (!Single_Line_Field(field))
57 	{
58 	  result = (form->toprow==0) ? FALSE : TRUE;
59 	}
60       else
61 	{
62 	  result = (form->begincol==0) ? FALSE : TRUE;
63 	}
64     }
65   return(result);
66 }
67 
68 /*---------------------------------------------------------------------------
69 |   Facility      :  libnform
70 |   Function      :  static char * After_Last_Non_Pad_Position(
71 |                                    char *buffer,
72 |                                    int len,
73 |                                    int pad)
74 |
75 |   Description   :  Find the last position in the buffer that doesn't
76 |                    contain a padding character.
77 |
78 |   Return Values :  The pointer to this position
79 +--------------------------------------------------------------------------*/
80 INLINE
81 static char * After_Last_Non_Pad_Position(char *buffer, int len, int pad)
82 {
83   char *end = buffer + len;
84 
85   assert(buffer && len>=0);
86   while ( (buffer < end) && (*(end-1)==pad) )
87     end--;
88 
89   return end;
90 }
91 
92 #define SMALL_BUFFER_SIZE (80)
93 
94 /*---------------------------------------------------------------------------
95 |   Facility      :  libnform
96 |   Function      :  bool data_ahead(const FORM *form)
97 |
98 |   Description   :  Check for off-screen data ahead. This is more difficult
99 |                    because a dynamic field has a variable end.
100 |
101 |   Return Values :  TRUE   - there are off-screen data ahead
102 |                    FALSE  - there are no off-screen data ahead
103 +--------------------------------------------------------------------------*/
104 bool data_ahead(const FORM *form)
105 {
106   bool result = FALSE;
107 
108   if (form && (form->status & _POSTED) && form->current)
109     {
110       static char buffer[SMALL_BUFFER_SIZE + 1];
111       FIELD *field;
112       bool large_buffer;
113       bool cursor_moved = FALSE;
114       char *bp;
115       char *found_content;
116       int pos;
117 
118       field = form->current;
119       assert(form->w);
120 
121       large_buffer = (field->cols > SMALL_BUFFER_SIZE);
122       if (large_buffer)
123 	bp = (char *)malloc((size_t)(field->cols) + 1);
124       else
125 	bp = buffer;
126 
127       assert(bp);
128 
129       if (Single_Line_Field(field))
130 	{
131 	  int check_len;
132 
133 	  pos = form->begincol + field->cols;
134 	  while (pos < field->dcols)
135 	    {
136 	      check_len = field->dcols - pos;
137 	      if ( check_len >= field->cols )
138 		check_len = field->cols;
139 	      cursor_moved = TRUE;
140 	      wmove(form->w,0,pos);
141 	      winnstr(form->w,bp,check_len);
142 	      found_content =
143 		After_Last_Non_Pad_Position(bp,check_len,field->pad);
144 	      if (found_content==bp)
145 		  pos += field->cols;
146 	      else
147 		{
148 		  result = TRUE;
149 		  break;
150 		}
151 	    }
152 	}
153       else
154 	{
155 	  pos = form->toprow + field->rows;
156 	  while (pos < field->drows)
157 	    {
158 	      cursor_moved = TRUE;
159 	      wmove(form->w,pos,0);
160 	      pos++;
161 	      winnstr(form->w,bp,field->cols);
162 	      found_content =
163 		After_Last_Non_Pad_Position(bp,field->cols,field->pad);
164 	      if (found_content!=bp)
165 		{
166 		  result = TRUE;
167 		  break;
168 		}
169 	    }
170 	}
171 
172       if (large_buffer)
173 	free(bp);
174 
175       if (cursor_moved)
176 	wmove(form->w,form->currow,form->curcol);
177     }
178   return(result);
179 }
180 
181 /* frm_data.c ends here */
182