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