xref: /freebsd/contrib/dialog/mixedform.c (revision c6879c6c14eedbd060ba588a3129a6c60ebbe783)
14c8945a0SNathan Whitehorn /*
2*f4f33ea0SBaptiste Daroussin  *  $Id: mixedform.c,v 1.13 2018/06/15 01:23:33 tom Exp $
34c8945a0SNathan Whitehorn  *
42a3e3873SBaptiste Daroussin  *  mixedform.c -- implements the mixed form (i.e, typed pairs label/editbox)
54c8945a0SNathan Whitehorn  *
6*f4f33ea0SBaptiste Daroussin  *  Copyright 2007-2013,2018	Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  *
234c8945a0SNathan Whitehorn  *  This is inspired by a patch from Kiran Cherupally
244c8945a0SNathan Whitehorn  *  (but different interface design).
254c8945a0SNathan Whitehorn  */
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <dialog.h>
284c8945a0SNathan Whitehorn 
294c8945a0SNathan Whitehorn #define LLEN(n) ((n) * MIXEDFORM_TAGS)
304c8945a0SNathan Whitehorn 
314c8945a0SNathan Whitehorn #define ItemName(i)     items[LLEN(i) + 0]
324c8945a0SNathan Whitehorn #define ItemNameY(i)    items[LLEN(i) + 1]
334c8945a0SNathan Whitehorn #define ItemNameX(i)    items[LLEN(i) + 2]
344c8945a0SNathan Whitehorn #define ItemText(i)     items[LLEN(i) + 3]
354c8945a0SNathan Whitehorn #define ItemTextY(i)    items[LLEN(i) + 4]
364c8945a0SNathan Whitehorn #define ItemTextX(i)    items[LLEN(i) + 5]
374c8945a0SNathan Whitehorn #define ItemTextFLen(i) items[LLEN(i) + 6]
384c8945a0SNathan Whitehorn #define ItemTextILen(i) items[LLEN(i) + 7]
394c8945a0SNathan Whitehorn #define ItemTypep(i)    items[LLEN(i) + 8]
404c8945a0SNathan Whitehorn #define ItemHelp(i)     (dialog_vars.item_help ? items[LLEN(i) + 9] : dlg_strempty())
414c8945a0SNathan Whitehorn 
424c8945a0SNathan Whitehorn int
dialog_mixedform(const char * title,const char * cprompt,int height,int width,int form_height,int item_no,char ** items)434c8945a0SNathan Whitehorn dialog_mixedform(const char *title,
444c8945a0SNathan Whitehorn 		 const char *cprompt,
454c8945a0SNathan Whitehorn 		 int height,
464c8945a0SNathan Whitehorn 		 int width,
474c8945a0SNathan Whitehorn 		 int form_height,
484c8945a0SNathan Whitehorn 		 int item_no,
494c8945a0SNathan Whitehorn 		 char **items)
504c8945a0SNathan Whitehorn {
514c8945a0SNathan Whitehorn     int result;
52*f4f33ea0SBaptiste Daroussin     int choice = 0;
534c8945a0SNathan Whitehorn     int i;
544c8945a0SNathan Whitehorn     DIALOG_FORMITEM *listitems;
554c8945a0SNathan Whitehorn     DIALOG_VARS save_vars;
564c8945a0SNathan Whitehorn     bool show_status = FALSE;
57febdb468SDevin Teske     char *help_result;
584c8945a0SNathan Whitehorn 
594c8945a0SNathan Whitehorn     dlg_save_vars(&save_vars);
604c8945a0SNathan Whitehorn     dialog_vars.separate_output = TRUE;
614c8945a0SNathan Whitehorn 
624c8945a0SNathan Whitehorn     listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
634c8945a0SNathan Whitehorn     assert_ptr(listitems, "dialog_mixedform");
644c8945a0SNathan Whitehorn 
654c8945a0SNathan Whitehorn     for (i = 0; i < item_no; ++i) {
664c8945a0SNathan Whitehorn 	listitems[i].type = dialog_vars.formitem_type;
674c8945a0SNathan Whitehorn 	listitems[i].name = ItemName(i);
684c8945a0SNathan Whitehorn 	listitems[i].name_len = (int) strlen(ItemName(i));
694c8945a0SNathan Whitehorn 	listitems[i].name_y = dlg_ordinate(ItemNameY(i));
704c8945a0SNathan Whitehorn 	listitems[i].name_x = dlg_ordinate(ItemNameX(i));
714c8945a0SNathan Whitehorn 	listitems[i].text = ItemText(i);
724c8945a0SNathan Whitehorn 	listitems[i].text_len = (int) strlen(ItemText(i));
734c8945a0SNathan Whitehorn 	listitems[i].text_y = dlg_ordinate(ItemTextY(i));
744c8945a0SNathan Whitehorn 	listitems[i].text_x = dlg_ordinate(ItemTextX(i));
754c8945a0SNathan Whitehorn 	listitems[i].text_flen = atoi(ItemTextFLen(i));
764c8945a0SNathan Whitehorn 	listitems[i].text_ilen = atoi(ItemTextILen(i));
774c8945a0SNathan Whitehorn 	listitems[i].help = (dialog_vars.item_help ? ItemHelp(i) :
784c8945a0SNathan Whitehorn 			     dlg_strempty());
794c8945a0SNathan Whitehorn 	listitems[i].type = (unsigned) atoi(ItemTypep(i));
804c8945a0SNathan Whitehorn     }
814c8945a0SNathan Whitehorn 
824c8945a0SNathan Whitehorn     result = dlg_form(title,
834c8945a0SNathan Whitehorn 		      cprompt,
844c8945a0SNathan Whitehorn 		      height,
854c8945a0SNathan Whitehorn 		      width,
864c8945a0SNathan Whitehorn 		      form_height,
874c8945a0SNathan Whitehorn 		      item_no,
884c8945a0SNathan Whitehorn 		      listitems,
894c8945a0SNathan Whitehorn 		      &choice);
904c8945a0SNathan Whitehorn 
914c8945a0SNathan Whitehorn     switch (result) {
924c8945a0SNathan Whitehorn     case DLG_EXIT_OK:		/* FALLTHRU */
934c8945a0SNathan Whitehorn     case DLG_EXIT_EXTRA:
944c8945a0SNathan Whitehorn 	show_status = TRUE;
954c8945a0SNathan Whitehorn 	break;
964c8945a0SNathan Whitehorn     case DLG_EXIT_HELP:
97febdb468SDevin Teske 	dlg_add_help_formitem(&result, &help_result, &listitems[choice]);
984c8945a0SNathan Whitehorn 	show_status = dialog_vars.help_status;
99febdb468SDevin Teske 	dlg_add_string(help_result);
1004c8945a0SNathan Whitehorn 	if (show_status)
1014c8945a0SNathan Whitehorn 	    dlg_add_separator();
1024c8945a0SNathan Whitehorn 	break;
1034c8945a0SNathan Whitehorn     }
1044c8945a0SNathan Whitehorn     if (show_status) {
1054c8945a0SNathan Whitehorn 	for (i = 0; i < item_no; i++) {
1064c8945a0SNathan Whitehorn 	    if (listitems[i].text_flen > 0) {
1074c8945a0SNathan Whitehorn 		dlg_add_string(listitems[i].text);
1084c8945a0SNathan Whitehorn 		dlg_add_separator();
1094c8945a0SNathan Whitehorn 	    }
1104c8945a0SNathan Whitehorn 	}
1112a3e3873SBaptiste Daroussin 	dlg_add_last_key(-1);
1124c8945a0SNathan Whitehorn     }
1134c8945a0SNathan Whitehorn 
1144c8945a0SNathan Whitehorn     dlg_free_formitems(listitems);
1154c8945a0SNathan Whitehorn     dlg_restore_vars(&save_vars);
1164c8945a0SNathan Whitehorn 
1174c8945a0SNathan Whitehorn     return result;
1184c8945a0SNathan Whitehorn }
119