xref: /openbsd-src/lib/libform/fld_current.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: fld_current.c,v 1.2 1998/07/24 02:36:36 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998 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 <Juergen.Pfeifer@T-Online.de> 1995,1997        *
33  ****************************************************************************/
34 #include "form.priv.h"
35 
36 MODULE_ID("$From: fld_current.c,v 1.2 1998/02/11 12:13:44 tom 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 int set_current_field(FORM  * form, FIELD * field)
52 {
53   int err = E_OK;
54 
55   if ( !form || !field )
56     RETURN(E_BAD_ARGUMENT);
57 
58   if ( (form != field->form) || Field_Is_Not_Selectable(field) )
59     RETURN(E_REQUEST_DENIED);
60 
61   if (!(form->status & _POSTED))
62     {
63       form->current = field;
64       form->curpage = field->page;
65   }
66   else
67     {
68       if (form->status & _IN_DRIVER)
69 	err = E_BAD_STATE;
70       else
71 	{
72 	  if (form->current != field)
73 	    {
74 	      if (!_nc_Internal_Validation(form))
75 	       err = E_INVALID_FIELD;
76 	      else
77 		{
78 		  Call_Hook(form,fieldterm);
79 		  if (field->page != form->curpage)
80 		    {
81 		      Call_Hook(form,formterm);
82 		      err = _nc_Set_Form_Page(form,field->page,field);
83 		      Call_Hook(form,forminit);
84 		    }
85 		  else
86 		    {
87 		      err = _nc_Set_Current_Field(form,field);
88 		    }
89 		  Call_Hook(form,fieldinit);
90 		  _nc_Refresh_Current_Field(form);
91 		}
92 	    }
93 	}
94     }
95   RETURN(err);
96 }
97 
98 /*---------------------------------------------------------------------------
99 |   Facility      :  libnform
100 |   Function      :  FIELD *current_field(const FORM * form)
101 |
102 |   Description   :  Return the current field.
103 |
104 |   Return Values :  Pointer to the current field.
105 +--------------------------------------------------------------------------*/
106 FIELD *current_field(const FORM * form)
107 {
108   return Normalize_Form(form)->current;
109 }
110 
111 /*---------------------------------------------------------------------------
112 |   Facility      :  libnform
113 |   Function      :  int field_index(const FIELD * field)
114 |
115 |   Description   :  Return the index of the field in the field-array of
116 |                    the form.
117 |
118 |   Return Values :  >= 0   : field index
119 |                    -1     : fieldpointer invalid or field not connected
120 +--------------------------------------------------------------------------*/
121 int field_index(const FIELD * field)
122 {
123   return ( (field && field->form) ? field->index : -1 );
124 }
125 
126 /* fld_current.c ends here */
127