frm_def.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /****************************************************************************
  2. * Copyright (c) 1998-2007,2008 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. * Author: Juergen Pfeifer, 1995,1997 *
  30. ****************************************************************************/
  31. #include "form.priv.h"
  32. MODULE_ID("$Id: frm_def.c,v 1.23 2008/08/04 00:07:55 tom Exp $")
  33. /* this can't be readonly */
  34. static FORM default_form =
  35. {
  36. 0, /* status */
  37. 0, /* rows */
  38. 0, /* cols */
  39. 0, /* currow */
  40. 0, /* curcol */
  41. 0, /* toprow */
  42. 0, /* begincol */
  43. -1, /* maxfield */
  44. -1, /* maxpage */
  45. -1, /* curpage */
  46. ALL_FORM_OPTS, /* opts */
  47. (WINDOW *)0, /* win */
  48. (WINDOW *)0, /* sub */
  49. (WINDOW *)0, /* w */
  50. (FIELD **)0, /* field */
  51. (FIELD *)0, /* current */
  52. (_PAGE *) 0, /* page */
  53. (char *)0, /* usrptr */
  54. NULL, /* forminit */
  55. NULL, /* formterm */
  56. NULL, /* fieldinit */
  57. NULL /* fieldterm */
  58. };
  59. NCURSES_EXPORT_VAR(FORM *) _nc_Default_Form = &default_form;
  60. /*---------------------------------------------------------------------------
  61. | Facility : libnform
  62. | Function : static FIELD *Insert_Field_By_Position(
  63. | FIELD *new_field,
  64. | FIELD *head )
  65. |
  66. | Description : Insert new_field into sorted fieldlist with head "head"
  67. | and return new head of sorted fieldlist. Sorting
  68. | criteria is (row,column). This is a circular list.
  69. |
  70. | Return Values : New head of sorted fieldlist
  71. +--------------------------------------------------------------------------*/
  72. static FIELD *
  73. Insert_Field_By_Position(FIELD *newfield, FIELD *head)
  74. {
  75. FIELD *current, *newhead;
  76. assert(newfield);
  77. if (!head)
  78. { /* empty list is trivial */
  79. newhead = newfield->snext = newfield->sprev = newfield;
  80. }
  81. else
  82. {
  83. newhead = current = head;
  84. while ((current->frow < newfield->frow) ||
  85. ((current->frow == newfield->frow) &&
  86. (current->fcol < newfield->fcol)))
  87. {
  88. current = current->snext;
  89. if (current == head)
  90. { /* We cycled through. Reset head to indicate that */
  91. head = (FIELD *)0;
  92. break;
  93. }
  94. }
  95. /* we leave the loop with current pointing to the field after newfield */
  96. newfield->snext = current;
  97. newfield->sprev = current->sprev;
  98. newfield->snext->sprev = newfield;
  99. newfield->sprev->snext = newfield;
  100. if (current == head)
  101. newhead = newfield;
  102. }
  103. return (newhead);
  104. }
  105. /*---------------------------------------------------------------------------
  106. | Facility : libnform
  107. | Function : static void Disconnect_Fields(FORM *form)
  108. |
  109. | Description : Break association between form and array of fields.
  110. |
  111. | Return Values : -
  112. +--------------------------------------------------------------------------*/
  113. static void
  114. Disconnect_Fields(FORM *form)
  115. {
  116. if (form->field)
  117. {
  118. FIELD **fields;
  119. for (fields = form->field; *fields; fields++)
  120. {
  121. if (form == (*fields)->form)
  122. (*fields)->form = (FORM *)0;
  123. }
  124. form->rows = form->cols = 0;
  125. form->maxfield = form->maxpage = -1;
  126. form->field = (FIELD **)0;
  127. if (form->page)
  128. free(form->page);
  129. form->page = (_PAGE *) 0;
  130. }
  131. }
  132. /*---------------------------------------------------------------------------
  133. | Facility : libnform
  134. | Function : static int Connect_Fields(FORM *form, FIELD **fields)
  135. |
  136. | Description : Set association between form and array of fields.
  137. |
  138. | Return Values : E_OK - no error
  139. | E_CONNECTED - a field is already connected
  140. | E_BAD_ARGUMENT - Invalid form pointer or field array
  141. | E_SYSTEM_ERROR - not enough memory
  142. +--------------------------------------------------------------------------*/
  143. static int
  144. Connect_Fields(FORM *form, FIELD **fields)
  145. {
  146. int field_cnt, j;
  147. int page_nr;
  148. int maximum_row_in_field, maximum_col_in_field;
  149. _PAGE *pg;
  150. T((T_CALLED("Connect_Fields(%p,%p)"), form, fields));
  151. assert(form);
  152. form->field = fields;
  153. form->maxfield = 0;
  154. form->maxpage = 0;
  155. if (!fields)
  156. RETURN(E_OK);
  157. page_nr = 0;
  158. /* store formpointer in fields and count pages */
  159. for (field_cnt = 0; fields[field_cnt]; field_cnt++)
  160. {
  161. if (fields[field_cnt]->form)
  162. RETURN(E_CONNECTED);
  163. if (field_cnt == 0 ||
  164. (fields[field_cnt]->status & _NEWPAGE))
  165. page_nr++;
  166. fields[field_cnt]->form = form;
  167. }
  168. if (field_cnt == 0 || (short)field_cnt < 0)
  169. RETURN(E_BAD_ARGUMENT);
  170. /* allocate page structures */
  171. if ((pg = typeMalloc(_PAGE, page_nr)) != (_PAGE *) 0)
  172. {
  173. T((T_CREATE("_PAGE %p"), pg));
  174. form->page = pg;
  175. }
  176. else
  177. RETURN(E_SYSTEM_ERROR);
  178. /* Cycle through fields and calculate page boundaries as well as
  179. size of the form */
  180. for (j = 0; j < field_cnt; j++)
  181. {
  182. if (j == 0)
  183. pg->pmin = j;
  184. else
  185. {
  186. if (fields[j]->status & _NEWPAGE)
  187. {
  188. pg->pmax = j - 1;
  189. pg++;
  190. pg->pmin = j;
  191. }
  192. }
  193. maximum_row_in_field = fields[j]->frow + fields[j]->rows;
  194. maximum_col_in_field = fields[j]->fcol + fields[j]->cols;
  195. if (form->rows < maximum_row_in_field)
  196. form->rows = maximum_row_in_field;
  197. if (form->cols < maximum_col_in_field)
  198. form->cols = maximum_col_in_field;
  199. }
  200. pg->pmax = field_cnt - 1;
  201. form->maxfield = field_cnt;
  202. form->maxpage = page_nr;
  203. /* Sort fields on form pages */
  204. for (page_nr = 0; page_nr < form->maxpage; page_nr++)
  205. {
  206. FIELD *fld = (FIELD *)0;
  207. for (j = form->page[page_nr].pmin; j <= form->page[page_nr].pmax; j++)
  208. {
  209. fields[j]->index = j;
  210. fields[j]->page = page_nr;
  211. fld = Insert_Field_By_Position(fields[j], fld);
  212. }
  213. if (fld)
  214. {
  215. form->page[page_nr].smin = fld->index;
  216. form->page[page_nr].smax = fld->sprev->index;
  217. }
  218. else
  219. {
  220. form->page[page_nr].smin = 0;
  221. form->page[page_nr].smax = 0;
  222. }
  223. }
  224. RETURN(E_OK);
  225. }
  226. /*---------------------------------------------------------------------------
  227. | Facility : libnform
  228. | Function : static int Associate_Fields(FORM *form, FIELD **fields)
  229. |
  230. | Description : Set association between form and array of fields.
  231. | If there are fields, position to first active field.
  232. |
  233. | Return Values : E_OK - success
  234. | E_BAD_ARGUMENT - Invalid form pointer or field array
  235. | E_CONNECTED - a field is already connected
  236. | E_SYSTEM_ERROR - not enough memory
  237. +--------------------------------------------------------------------------*/
  238. NCURSES_INLINE static int
  239. Associate_Fields(FORM *form, FIELD **fields)
  240. {
  241. int res = Connect_Fields(form, fields);
  242. if (res == E_OK)
  243. {
  244. if (form->maxpage > 0)
  245. {
  246. form->curpage = 0;
  247. form_driver(form, FIRST_ACTIVE_MAGIC);
  248. }
  249. else
  250. {
  251. form->curpage = -1;
  252. form->current = (FIELD *)0;
  253. }
  254. }
  255. return (res);
  256. }
  257. /*---------------------------------------------------------------------------
  258. | Facility : libnform
  259. | Function : FORM *new_form( FIELD **fields )
  260. |
  261. | Description : Create new form with given array of fields.
  262. |
  263. | Return Values : Pointer to form. NULL if error occurred.
  264. ! Set errno:
  265. | E_OK - success
  266. | E_BAD_ARGUMENT - Invalid form pointer or field array
  267. | E_CONNECTED - a field is already connected
  268. | E_SYSTEM_ERROR - not enough memory
  269. +--------------------------------------------------------------------------*/
  270. NCURSES_EXPORT(FORM *)
  271. new_form(FIELD **fields)
  272. {
  273. int err = E_SYSTEM_ERROR;
  274. FORM *form = typeMalloc(FORM, 1);
  275. T((T_CALLED("new_form(%p)"), fields));
  276. if (form)
  277. {
  278. T((T_CREATE("form %p"), form));
  279. *form = *_nc_Default_Form;
  280. if ((err = Associate_Fields(form, fields)) != E_OK)
  281. {
  282. free_form(form);
  283. form = (FORM *)0;
  284. }
  285. }
  286. if (!form)
  287. SET_ERROR(err);
  288. returnForm(form);
  289. }
  290. /*---------------------------------------------------------------------------
  291. | Facility : libnform
  292. | Function : int free_form( FORM *form )
  293. |
  294. | Description : Release internal memory associated with form.
  295. |
  296. | Return Values : E_OK - no error
  297. | E_BAD_ARGUMENT - invalid form pointer
  298. | E_POSTED - form is posted
  299. +--------------------------------------------------------------------------*/
  300. NCURSES_EXPORT(int)
  301. free_form(FORM *form)
  302. {
  303. T((T_CALLED("free_form(%p)"), form));
  304. if (!form)
  305. RETURN(E_BAD_ARGUMENT);
  306. if (form->status & _POSTED)
  307. RETURN(E_POSTED);
  308. Disconnect_Fields(form);
  309. if (form->page)
  310. free(form->page);
  311. free(form);
  312. RETURN(E_OK);
  313. }
  314. /*---------------------------------------------------------------------------
  315. | Facility : libnform
  316. | Function : int set_form_fields( FORM *form, FIELD **fields )
  317. |
  318. | Description : Set a new association of an array of fields to a form
  319. |
  320. | Return Values : E_OK - no error
  321. | E_BAD_ARGUMENT - Invalid form pointer or field array
  322. | E_CONNECTED - a field is already connected
  323. | E_POSTED - form is posted
  324. | E_SYSTEM_ERROR - not enough memory
  325. +--------------------------------------------------------------------------*/
  326. NCURSES_EXPORT(int)
  327. set_form_fields(FORM *form, FIELD **fields)
  328. {
  329. FIELD **old;
  330. int res;
  331. T((T_CALLED("set_form_fields(%p,%p)"), form, fields));
  332. if (!form)
  333. RETURN(E_BAD_ARGUMENT);
  334. if (form->status & _POSTED)
  335. RETURN(E_POSTED);
  336. old = form->field;
  337. Disconnect_Fields(form);
  338. if ((res = Associate_Fields(form, fields)) != E_OK)
  339. Connect_Fields(form, old);
  340. RETURN(res);
  341. }
  342. /*---------------------------------------------------------------------------
  343. | Facility : libnform
  344. | Function : FIELD **form_fields( const FORM *form )
  345. |
  346. | Description : Retrieve array of fields
  347. |
  348. | Return Values : Pointer to field array
  349. +--------------------------------------------------------------------------*/
  350. NCURSES_EXPORT(FIELD **)
  351. form_fields(const FORM *form)
  352. {
  353. T((T_CALLED("form_field(%p)"), form));
  354. returnFieldPtr(Normalize_Form(form)->field);
  355. }
  356. /*---------------------------------------------------------------------------
  357. | Facility : libnform
  358. | Function : int field_count( const FORM *form )
  359. |
  360. | Description : Retrieve number of fields
  361. |
  362. | Return Values : Number of fields, -1 if none are defined
  363. +--------------------------------------------------------------------------*/
  364. NCURSES_EXPORT(int)
  365. field_count(const FORM *form)
  366. {
  367. T((T_CALLED("field_count(%p)"), form));
  368. returnCode(Normalize_Form(form)->maxfield);
  369. }
  370. /* frm_def.c ends here */