xref: /freebsd/contrib/ncurses/form/fld_current.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2010,2016 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *   Author:  Juergen Pfeifer, 1995,1997                                    *
32  ****************************************************************************/
33 
34 #include "form.priv.h"
35 
36 MODULE_ID("$Id: fld_current.c,v 1.16 2020/05/24 01:40:20 anonymous.maarten Exp $")
37 
38 /*---------------------------------------------------------------------------
39 |   Facility      :  libnform
40 |   Function      :  int set_current_field(FORM  * form,FIELD * field)
41 |
42 |   Description   :  Set the current field of the form to the specified one.
43 |
44 |   Return Values :  E_OK              - success
45 |                    E_BAD_ARGUMENT    - invalid form or field pointer
46 |                    E_REQUEST_DENIED  - field not selectable
47 |                    E_BAD_STATE       - called from a hook routine
48 |                    E_INVALID_FIELD   - current field can't be left
49 |                    E_SYSTEM_ERROR    - system error
50 +--------------------------------------------------------------------------*/
51 FORM_EXPORT(int)
52 set_current_field(FORM *form, FIELD *field)
53 {
54   int err = E_OK;
55 
56   T((T_CALLED("set_current_field(%p,%p)"), (void *)form, (void *)field));
57   if (form == 0 || field == 0)
58     {
59       RETURN(E_BAD_ARGUMENT);
60     }
61   else if ((form != field->form) || Field_Is_Not_Selectable(field))
62     {
63       RETURN(E_REQUEST_DENIED);
64     }
65   else if ((form->status & _POSTED) == 0)
66     {
67       form->current = field;
68       form->curpage = field->page;
69     }
70   else
71     {
72       if ((form->status & _IN_DRIVER) != 0)
73 	{
74 	  err = E_BAD_STATE;
75 	}
76       else
77 	{
78 	  if (form->current != field)
79 	    {
80 	      if (form->current && !_nc_Internal_Validation(form))
81 		{
82 		  err = E_INVALID_FIELD;
83 		}
84 	      else
85 		{
86 		  Call_Hook(form, fieldterm);
87 		  if (field->page != form->curpage)
88 		    {
89 		      Call_Hook(form, formterm);
90 		      err = _nc_Set_Form_Page(form, (int)field->page, field);
91 		      Call_Hook(form, forminit);
92 		    }
93 		  else
94 		    {
95 		      err = _nc_Set_Current_Field(form, field);
96 		    }
97 		  Call_Hook(form, fieldinit);
98 		  (void)_nc_Refresh_Current_Field(form);
99 		}
100 	    }
101 	}
102     }
103   RETURN(err);
104 }
105 
106 /*---------------------------------------------------------------------------
107 |   Facility      :  libnform
108 |   Function      :  int unfocus_current_field(FORM * form)
109 |
110 |   Description   :  Removes focus from the current field.
111 |
112 |   Return Values :  E_OK              - success
113 |                    E_BAD_ARGUMENT    - invalid form pointer
114 |                    E_REQUEST_DENIED  - there is no current field to unfocus
115 +--------------------------------------------------------------------------*/
116 FORM_EXPORT(int)
117 unfocus_current_field(FORM *const form)
118 {
119   T((T_CALLED("unfocus_current_field(%p)"), (const void *)form));
120   if (form == 0)
121     {
122       RETURN(E_BAD_ARGUMENT);
123     }
124   else if (form->current == 0)
125     {
126       RETURN(E_REQUEST_DENIED);
127     }
128   _nc_Unset_Current_Field(form);
129   RETURN(E_OK);
130 }
131 
132 /*---------------------------------------------------------------------------
133 |   Facility      :  libnform
134 |   Function      :  FIELD *current_field(const FORM * form)
135 |
136 |   Description   :  Return the current field.
137 |
138 |   Return Values :  Pointer to the current field.
139 +--------------------------------------------------------------------------*/
140 FORM_EXPORT(FIELD *)
141 current_field(const FORM *form)
142 {
143   T((T_CALLED("current_field(%p)"), (const void *)form));
144   returnField(Normalize_Form(form)->current);
145 }
146 
147 /*---------------------------------------------------------------------------
148 |   Facility      :  libnform
149 |   Function      :  int field_index(const FIELD * field)
150 |
151 |   Description   :  Return the index of the field in the field-array of
152 |                    the form.
153 |
154 |   Return Values :  >= 0   : field index
155 |                    -1     : fieldpointer invalid or field not connected
156 +--------------------------------------------------------------------------*/
157 FORM_EXPORT(int)
158 field_index(const FIELD *field)
159 {
160   T((T_CALLED("field_index(%p)"), (const void *)field));
161   returnCode((field != 0 && field->form != 0) ? (int)field->index : -1);
162 }
163 
164 /* fld_current.c ends here */
165