/* * A simple form-fillout function that lets you pass an array of * Tag's that describe a form and it will pop up a window and let * a user fill out a form that describes the values you'd like. * * The main function is GetValues(). See the demo program in this * same directory (main.c, in the function edit()) for a more * detailed example. * * Written by Allen Martin, (amartin@cs.wpi.edu). */ #include #include #include #include "libsx.h" #include "multireq.h" #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) typedef struct { Widget window; int cancelled; Widget *w; TagList *tags; } MReqData; /* protos */ static void mreq_ok(Widget w, MReqData *mdata); static void mreq_cancel(Widget w, MReqData *mdata); int GetValues(TagList *tags) { MReqData mdata; int num_tags=0, num_widg=0, num_labels=0; int i, w, l; Widget *label_w, ok_w, cancel_w; char string[256], window_name[256]="Requestor"; int maxwidth=0, widest_label=-1; /* first count the number of tag items and required widgets */ for(; tags[num_tags].tag != TAG_DONE; num_tags++) { if(tags[num_tags].tag == TAG_LABEL) { num_labels++; continue; } else if(tags[num_tags].tag == TAG_WINDOW_LABEL) { if(!tags[num_tags].label) { fprintf(stderr, "Invalid window name passed to GetValues()\n"); return(TRUE); } strcpy(window_name, tags[num_tags].label); continue; } /* determine the widest label */ if(strlen(tags[num_tags].label) > maxwidth) { maxwidth = strlen(tags[num_tags].label); widest_label = num_labels; } num_labels++; num_widg++; } /* allocate mem for the widgets */ if(!(mdata.w = (Widget *)malloc(num_widg * sizeof(Widget)))) return(TRUE); if(!(label_w = (Widget *)malloc(num_labels * sizeof(Widget)))) { free(mdata.w); return(TRUE); } mdata.window = MakeWindow(window_name, SAME_DISPLAY, EXCLUSIVE_WINDOW); mdata.tags = tags; /* create the label widgets first */ for(i=0, l=0; itags; tagptr->tag != TAG_DONE; tagptr++) { switch(tagptr->tag) { case TAG_STRING: cptr = GetStringEntry(mdata->w[widg_num++]); strcpy(tagptr->data, cptr); break; case TAG_INT: cptr = GetStringEntry(mdata->w[widg_num++]); *((int *)tagptr->data) = atoi(cptr); break; case TAG_FLOAT: cptr = GetStringEntry(mdata->w[widg_num++]); *((float *)tagptr->data) = atof(cptr); break; case TAG_WINDOW_LABEL: case TAG_LABEL: break; default: fprintf(stderr, "GetValues() : Invalid tag item %d\n", tagptr->tag); } } mdata->cancelled = FALSE; SetCurrentWindow(mdata->window); CloseWindow(); } static void mreq_cancel(Widget w, MReqData *mdata) { mdata->cancelled = TRUE; SetCurrentWindow(mdata->window); CloseWindow(); }