xref: /openbsd-src/lib/libform/fld_current.c (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1 /*	$OpenBSD: fld_current.c,v 1.7 2023/10/17 09:52:10 nicm Exp $	*/
2 /****************************************************************************
3  * Copyright 2020 Thomas E. Dickey                                          *
4  * Copyright 1998-2010,2016 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: fld_current.c,v 1.7 2023/10/17 09:52:10 nicm Exp $")
38 
39 /*---------------------------------------------------------------------------
40 |   Facility      :  libnform
41 |   Function      :  int set_current_field(FORM  * form,FIELD * field)
42 |
43 |   Description   :  Set the current field of the form to the specified one.
44 |
45 |   Return Values :  E_OK              - success
46 |                    E_BAD_ARGUMENT    - invalid form or field pointer
47 |                    E_REQUEST_DENIED  - field not selectable
48 |                    E_BAD_STATE       - called from a hook routine
49 |                    E_INVALID_FIELD   - current field can't be left
50 |                    E_SYSTEM_ERROR    - system error
51 +--------------------------------------------------------------------------*/
FORM_EXPORT(int)52 FORM_EXPORT(int)
53 set_current_field(FORM *form, FIELD *field)
54 {
55   int err = E_OK;
56 
57   T((T_CALLED("set_current_field(%p,%p)"), (void *)form, (void *)field));
58   if (form == 0 || field == 0)
59     {
60       RETURN(E_BAD_ARGUMENT);
61     }
62   else if ((form != field->form) || Field_Is_Not_Selectable(field))
63     {
64       RETURN(E_REQUEST_DENIED);
65     }
66   else if ((form->status & _POSTED) == 0)
67     {
68       form->current = field;
69       form->curpage = field->page;
70     }
71   else
72     {
73       if ((form->status & _IN_DRIVER) != 0)
74 	{
75 	  err = E_BAD_STATE;
76 	}
77       else
78 	{
79 	  if (form->current != field)
80 	    {
81 	      if (form->current && !_nc_Internal_Validation(form))
82 		{
83 		  err = E_INVALID_FIELD;
84 		}
85 	      else
86 		{
87 		  Call_Hook(form, fieldterm);
88 		  if (field->page != form->curpage)
89 		    {
90 		      Call_Hook(form, formterm);
91 		      err = _nc_Set_Form_Page(form, (int)field->page, field);
92 		      Call_Hook(form, forminit);
93 		    }
94 		  else
95 		    {
96 		      err = _nc_Set_Current_Field(form, field);
97 		    }
98 		  Call_Hook(form, fieldinit);
99 		  (void)_nc_Refresh_Current_Field(form);
100 		}
101 	    }
102 	}
103     }
104   RETURN(err);
105 }
106 
107 /*---------------------------------------------------------------------------
108 |   Facility      :  libnform
109 |   Function      :  int unfocus_current_field(FORM * form)
110 |
111 |   Description   :  Removes focus from the current field.
112 |
113 |   Return Values :  E_OK              - success
114 |                    E_BAD_ARGUMENT    - invalid form pointer
115 |                    E_REQUEST_DENIED  - there is no current field to unfocus
116 +--------------------------------------------------------------------------*/
117 FORM_EXPORT(int)
unfocus_current_field(FORM * const form)118 unfocus_current_field(FORM *const form)
119 {
120   T((T_CALLED("unfocus_current_field(%p)"), (const void *)form));
121   if (form == 0)
122     {
123       RETURN(E_BAD_ARGUMENT);
124     }
125   else if (form->current == 0)
126     {
127       RETURN(E_REQUEST_DENIED);
128     }
129   _nc_Unset_Current_Field(form);
130   RETURN(E_OK);
131 }
132 
133 /*---------------------------------------------------------------------------
134 |   Facility      :  libnform
135 |   Function      :  FIELD *current_field(const FORM * form)
136 |
137 |   Description   :  Return the current field.
138 |
139 |   Return Values :  Pointer to the current field.
140 +--------------------------------------------------------------------------*/
141 FORM_EXPORT(FIELD *)
current_field(const FORM * form)142 current_field(const FORM *form)
143 {
144   T((T_CALLED("current_field(%p)"), (const void *)form));
145   returnField(Normalize_Form(form)->current);
146 }
147 
148 /*---------------------------------------------------------------------------
149 |   Facility      :  libnform
150 |   Function      :  int field_index(const FIELD * field)
151 |
152 |   Description   :  Return the index of the field in the field-array of
153 |                    the form.
154 |
155 |   Return Values :  >= 0   : field index
156 |                    -1     : fieldpointer invalid or field not connected
157 +--------------------------------------------------------------------------*/
158 FORM_EXPORT(int)
field_index(const FIELD * field)159 field_index(const FIELD *field)
160 {
161   T((T_CALLED("field_index(%p)"), (const void *)field));
162   returnCode((field != 0 && field->form != 0) ? (int)field->index : -1);
163 }
164 
165 /* fld_current.c ends here */
166