xref: /freebsd/contrib/ncurses/form/frm_data.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
1 /****************************************************************************
2  * Copyright (c) 1998-2010,2013 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, 1995,1997                                    *
31  ****************************************************************************/
32 
33 #include "form.priv.h"
34 
35 MODULE_ID("$Id: frm_data.c,v 1.16 2013/08/24 22:44:05 tom 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 |                    because the beginning 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 NCURSES_EXPORT(bool)
48 data_behind(const FORM *form)
49 {
50   bool result = FALSE;
51 
52   T((T_CALLED("data_behind(%p)"), (const void *)form));
53 
54   if (form && (form->status & _POSTED) && form->current)
55     {
56       FIELD *field;
57 
58       field = form->current;
59       if (!Single_Line_Field(field))
60 	{
61 	  result = (form->toprow == 0) ? FALSE : TRUE;
62 	}
63       else
64 	{
65 	  result = (form->begincol == 0) ? FALSE : TRUE;
66 	}
67     }
68   returnBool(result);
69 }
70 
71 /*---------------------------------------------------------------------------
72 |   Facility      :  libnform
73 |   Function      :  static char * Only_Padding(
74 |                                    WINDOW *w,
75 |                                    int len,
76 |                                    int pad)
77 |
78 |   Description   :  Test if 'length' cells starting at the current position
79 |                    contain a padding character.
80 |
81 |   Return Values :  true if only padding cells are found
82 +--------------------------------------------------------------------------*/
83 NCURSES_INLINE static bool
84 Only_Padding(WINDOW *w, int len, int pad)
85 {
86   bool result = TRUE;
87   int y, x, j;
88   FIELD_CELL cell;
89 
90   getyx(w, y, x);
91   for (j = 0; j < len; ++j)
92     {
93       if (wmove(w, y, x + j) != ERR)
94 	{
95 #if USE_WIDEC_SUPPORT
96 	  if (win_wch(w, &cell) != ERR)
97 	    {
98 	      if ((chtype)CharOf(cell) != ChCharOf(pad)
99 		  || cell.chars[1] != 0)
100 		{
101 		  result = FALSE;
102 		  break;
103 		}
104 	    }
105 #else
106 	  cell = (FIELD_CELL) winch(w);
107 	  if (ChCharOf(cell) != ChCharOf(pad))
108 	    {
109 	      result = FALSE;
110 	      break;
111 	    }
112 #endif
113 	}
114       else
115 	{
116 	  /* if an error, return true: no non-padding text found */
117 	  break;
118 	}
119     }
120   /* no need to reset the cursor position; caller does this */
121   return result;
122 }
123 
124 /*---------------------------------------------------------------------------
125 |   Facility      :  libnform
126 |   Function      :  bool data_ahead(const FORM *form)
127 |
128 |   Description   :  Check for off-screen data ahead. This is more difficult
129 |                    because a dynamic field has a variable end.
130 |
131 |   Return Values :  TRUE   - there are off-screen data ahead
132 |                    FALSE  - there are no off-screen data ahead
133 +--------------------------------------------------------------------------*/
134 NCURSES_EXPORT(bool)
135 data_ahead(const FORM *form)
136 {
137   bool result = FALSE;
138 
139   T((T_CALLED("data_ahead(%p)"), (const void *)form));
140 
141   if (form && (form->status & _POSTED) && form->current)
142     {
143       FIELD *field;
144       bool cursor_moved = FALSE;
145       int pos;
146 
147       field = form->current;
148       assert(form->w);
149 
150       if (Single_Line_Field(field))
151 	{
152 	  int check_len;
153 
154 	  pos = form->begincol + field->cols;
155 	  while (pos < field->dcols)
156 	    {
157 	      check_len = field->dcols - pos;
158 	      if (check_len >= field->cols)
159 		check_len = field->cols;
160 	      cursor_moved = TRUE;
161 	      wmove(form->w, 0, pos);
162 	      if (Only_Padding(form->w, check_len, field->pad))
163 		pos += field->cols;
164 	      else
165 		{
166 		  result = TRUE;
167 		  break;
168 		}
169 	    }
170 	}
171       else
172 	{
173 	  pos = form->toprow + field->rows;
174 	  while (pos < field->drows)
175 	    {
176 	      cursor_moved = TRUE;
177 	      wmove(form->w, pos, 0);
178 	      pos++;
179 	      if (!Only_Padding(form->w, field->cols, field->pad))
180 		{
181 		  result = TRUE;
182 		  break;
183 		}
184 	    }
185 	}
186 
187       if (cursor_moved)
188 	wmove(form->w, form->currow, form->curcol);
189     }
190   returnBool(result);
191 }
192 
193 /* frm_data.c ends here */
194