Subversion Repositories wimsdev

Rev

Rev 3522 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18.         /* html form simplification routines */
  19.  
  20. #define MAX_MENU_ITEMS 256
  21.  
  22. enum {
  23.     FORM_SELECT, FORM_RADIO, FORM_BAR, FORM_CHECKBOX
  24. };
  25.  
  26.         /* Produces <select> and <option>s.
  27.          * Parameter string: name from n1 to n2 [prompt plist]
  28.          * or name, list slist [prompt plist]
  29.          * Using script to do this is too slow. */
  30. void _form_menus(char *p,int kind)
  31. {
  32.     char *n, *li, *pp, *val, *p0, *p1, *p2, *pc;
  33.     char *vlist[MAX_MENU_ITEMS], *plist[MAX_MENU_ITEMS];
  34.     char nbuf[MAX_LINELEN+1],vbuf[MAX_LINELEN+1],pbuf[MAX_LINELEN+1];
  35.     char buf[256];
  36.     int i,i1=0,i2,itemcnt,type;
  37.     if(!outputing) return;
  38.     n=find_word_start(p); if(*n==0) return;
  39.     pp=find_word_end(n);
  40.     if(*pp!=0) *(pp++)=0;
  41.     else {
  42.         syntax: module_error("syntax_error"); return;
  43.     }
  44.     li=find_word_start(pp); pp=find_word_end(li);
  45.     if(*pp!=0) *(pp++)=0; else goto syntax;
  46.     itemcnt=0;
  47.     if(strcmp(li,"from")==0) {
  48.         p1=find_word_start(pp);
  49.         p2=wordchr(p1,"to"); if(p2==NULL || p2<=p1) goto syntax;
  50.         *(p2-1)=0; p2=find_word_start(p2+strlen("to"));
  51.         pp=wordchr(p2,"prompt"); if(pp!=NULL) *(pp-1)=0;
  52.         i1=evalue(p1); i2=evalue(p2);
  53.         if(i1<-100000000 || i1>100000000 || i2<-100000000 || i2>100000000)
  54.           goto syntax;
  55.         if(i2<i1) return;
  56.         if(i2>i1+MAX_MENU_ITEMS-1) i2=i1+MAX_MENU_ITEMS-1;
  57.         itemcnt=i2-i1+1;
  58.         type=0;
  59.     }
  60.     else {
  61.         if(strcmp(li,"list")==0) {
  62.             p1=find_word_start(pp);
  63.             pp=wordchr(p1,"prompt"); if(pp!=NULL && pp>p1) *(pp-1)=0;
  64.             strcpy(vbuf,p1);substit(vbuf);
  65.             itemcnt=cutitems(vbuf,vlist,MAX_MENU_ITEMS);
  66.             type=1;
  67.         }
  68.         else goto syntax;
  69.     }
  70.     for(i=0;i<itemcnt;i++) plist[i]=NULL;
  71.     if(pp!=NULL) {
  72.         p1=find_word_start(pp+strlen("prompt"));
  73.         strcpy(pbuf,p1);substit(pbuf);
  74.         cutitems(pbuf,plist,MAX_MENU_ITEMS);
  75.     }
  76.     strcpy(nbuf,n);substit(nbuf);
  77.     if(kind==FORM_SELECT) {
  78.         char *pp;
  79.         pp=getvar("wims_formselect_switch"); if(pp==NULL) pp="";
  80.         output("<select %s name=\"%s\">\n",pp,nbuf);
  81.     }
  82.     if(kind==FORM_BAR) _output_("<b>-</b>");
  83.     val=getvar(nbuf);if(val==NULL) val="";
  84.     for(i=0;i<itemcnt;i++) {
  85.         if(type==0) {
  86.             snprintf(buf,sizeof(buf),"%d",i+i1);
  87.             p0=buf;
  88.         }
  89.         else p0=vlist[i];
  90.         if(*find_word_start(p0)==0) continue;
  91.         if(plist[i]==NULL) plist[i]=p0;
  92.         if(*val!=0 &&
  93.            (strcmp(p0,val)==0 ||
  94.             ( (kind==FORM_SELECT || kind==FORM_CHECKBOX)
  95.              && itemchr(val,p0)!=NULL))) {
  96.             if(kind==FORM_SELECT) pc=" selected=\"selected\"";
  97.             else pc=" checked=\"checked\"";
  98.         }
  99.         else pc="";
  100.         switch(kind) {
  101.             case FORM_SELECT:
  102.             output("<option value=\"%s\"%s>%s\n",p0,pc,plist[i]);
  103.             break;
  104.            
  105.             case FORM_RADIO:
  106.             output("<input type=\"radio\" name=\"%s\" value=\"%s\"%s>&nbsp;%s",
  107.                    nbuf,p0,pc,plist[i]);
  108.             if(i<itemcnt-1 && itemcnt>2) _output_(",");
  109.             _output_("\n");
  110.             break;
  111.            
  112.             case FORM_CHECKBOX:
  113.             output("<input type=\"checkbox\" name=\"%s\" value=\"%s\"%s>&nbsp;%s",
  114.                    nbuf,p0,pc,plist[i]);
  115.             if(i<itemcnt-1 && itemcnt>2) _output_(",");
  116.             _output_("\n");
  117.             break;
  118.            
  119.             case FORM_BAR:
  120.             output("<input type=\"radio\" name=\"%s\" value=\"%s\"%s>",
  121.                    nbuf,p0,pc);
  122.             break;
  123.            
  124.         }
  125.     }
  126.     if(kind==FORM_SELECT) _output_("</select>");
  127.     if(kind==FORM_BAR) _output_("<b>+</b>");
  128. }
  129.  
  130. void exec_formselect(char *p)
  131. {
  132.     _form_menus(p,FORM_SELECT);
  133. }
  134.  
  135. void exec_formradio(char *p)
  136. {
  137.     _form_menus(p,FORM_RADIO);
  138. }
  139.  
  140. void exec_formcheckbox(char *p)
  141. {
  142.     _form_menus(p,FORM_CHECKBOX);
  143. }
  144.  
  145. void exec_formbar(char *p)
  146. {
  147.     _form_menus(p,FORM_BAR);
  148. }
  149.  
  150.