Subversion Repositories wimsdev

Rev

Rev 7673 | Rev 9007 | 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.  */
  31. void _form_menus(char *p,int kind)
  32. {
  33.     char *n, *li, *pp, *val, *p0, *p1, *p2, *pc, *s, *pfre="";
  34.     char *vlist[MAX_MENU_ITEMS], *plist[MAX_MENU_ITEMS];
  35.     char nbuf[MAX_LINELEN+1],vbuf[MAX_LINELEN+1],pbuf[MAX_LINELEN+1];
  36.     char buf[256], pfrb[256];
  37.     int i,i1=0,i2,itemcnt,type;
  38.     if(!outputing) return;
  39.     n=find_word_start(p); if(*n==0) return;
  40.     pp=find_word_end(n);
  41.     if(*pp!=0) *(pp++)=0;
  42.     else {
  43.       syntax: module_error("syntax_error"); return;
  44.     }
  45.     li=find_word_start(pp); pp=find_word_end(li);
  46.     if(*pp!=0) *(pp++)=0; else goto syntax;
  47.     itemcnt=0;
  48.     if(strcmp(li,"from")==0) {
  49.       p1=find_word_start(pp);
  50.       p2=wordchr(p1,"to"); if(p2==NULL || p2<=p1) goto syntax;
  51.       *(p2-1)=0; p2=find_word_start(p2+strlen("to"));
  52.       pp=wordchr(p2,"prompt"); if(pp!=NULL) *(pp-1)=0;
  53.       i1=evalue(p1); i2=evalue(p2);
  54.       if(i1<-100000000 || i1>100000000 || i2<-100000000 || i2>100000000)
  55.         goto syntax;
  56.       if(i2<i1) return;
  57.       if(i2>i1+MAX_MENU_ITEMS-1) i2=i1+MAX_MENU_ITEMS-1;
  58.       itemcnt=i2-i1+1;
  59.       type=0;
  60.     }
  61.     else {
  62.       if(strcmp(li,"list")==0) {
  63.          p1=find_word_start(pp);
  64.          pp=wordchr(p1,"prompt"); if(pp!=NULL && pp>p1) *(pp-1)=0;
  65.          ovlstrcpy(vbuf,p1);substit(vbuf);
  66.          itemcnt=cutitems(vbuf,vlist,MAX_MENU_ITEMS);
  67.          type=1;
  68.       }
  69.       else goto syntax;
  70.     }
  71.     for(i=0;i<itemcnt;i++) plist[i]=NULL;
  72.     if(pp!=NULL) {
  73.       p1=find_word_start(pp+strlen("prompt"));
  74.       ovlstrcpy(pbuf,p1);substit(pbuf);
  75.       cutitems(pbuf,plist,MAX_MENU_ITEMS);
  76.     }
  77.     ovlstrcpy(nbuf,n);substit(nbuf);
  78.     if(kind==FORM_SELECT) {
  79.       char *pp;
  80.       pp=getvar("wims_formselect_switch"); if(pp==NULL) pp="";
  81.       output("<select %s name=\"%s\" id=\"%s\">\n",pp,nbuf,nbuf);
  82.     }
  83.     if(kind==FORM_BAR) {
  84.       s=getvar("wims_ref_class");
  85.       if(s!=NULL && *s!=0 && !isspace(*s)) {
  86.        snprintf(pfrb,sizeof(pfrb)," <span class=\"%s\">",s); pfre="</span>";
  87.       } else { pfrb[0]=0; pfre=""; }
  88.       output("%s<span style=\"font-weight:bold;\">-</span>",pfrb);
  89.     }
  90.     val=getvar(nbuf);if(val==NULL) val="";
  91.     for(i=0;i<itemcnt;i++) {
  92.       if(type==0) {
  93.          snprintf(buf,sizeof(buf),"%d",i+i1);
  94.          p0=buf;
  95.       }
  96.       else p0=vlist[i];
  97.       if(*find_word_start(p0)==0) continue;
  98.       if(plist[i]==NULL) plist[i]=p0;
  99.       if(*val!=0 &&
  100.         (strcmp(p0,val)==0 ||
  101.          ( (kind==FORM_SELECT || kind==FORM_CHECKBOX)
  102.           && itemchr(val,p0)!=NULL))) {
  103.          if(kind==FORM_SELECT) pc=" selected=\"selected\"";
  104.          else pc=" checked=\"checked\"";
  105.       }
  106.       else pc="";
  107.       s=getvar("wims_ref_class");
  108.      if(s!=NULL && *s!=0 && !isspace(*s)) {
  109.        snprintf(pfrb,sizeof(pfrb)," <span class=\"%s\">",s); pfre="</span>";
  110.      }
  111.      else { pfrb[0]=0; pfre=""; }
  112.       switch(kind) {
  113.          case FORM_SELECT:
  114.          output("<option value=\"%s\"%s>%s</option>\n",p0,pc,plist[i]);
  115.          break;
  116.  
  117.          case FORM_RADIO:
  118.          output("%s<input type=\"radio\" name=\"%s\" id=\"%s%d\" value=\"%s\"%s/><label for=\"%s%d\">%s</label>%s",
  119.              pfrb,nbuf,nbuf,i,p0,pc,nbuf,i,plist[i],pfre);
  120.          if(i<itemcnt-1 && itemcnt>2) _output_(",");
  121.          _output_("\n");
  122.          break;
  123.  
  124.          case FORM_CHECKBOX:
  125.          output("%s<input type=\"checkbox\" name=\"%s\" id=\"%s%d\" value=\"%s\"%s/><label for=\"%s%d\">%s</label>%s",
  126.              pfrb,nbuf,nbuf,i,p0,pc,nbuf,i,plist[i],pfre);
  127.          if(i<itemcnt-1 && itemcnt>2) _output_(",");
  128.          _output_("\n");
  129.          break;
  130.  
  131.          case FORM_BAR:
  132.          output("<input type=\"radio\" name=\"%s\" id=\"%s%d\" value=\"%s\"%s/>",
  133.              nbuf,nbuf,i,p0,pc);
  134.          break;
  135.  
  136.       }
  137.     }
  138.     setvar("wims_formradio_class","");
  139.     if(kind==FORM_SELECT) _output_("</select>");
  140.     if(kind==FORM_BAR) output("<span style=\"font-weight:bold;\">+</span>%s",pfre);
  141. }
  142.  
  143. void exec_formselect(char *p)
  144. {
  145.     _form_menus(p,FORM_SELECT);
  146. }
  147.  
  148. void exec_formradio(char *p)
  149. {
  150.     _form_menus(p,FORM_RADIO);
  151. }
  152.  
  153. void exec_formcheckbox(char *p)
  154. {
  155.     _form_menus(p,FORM_CHECKBOX);
  156. }
  157.  
  158. void exec_formbar(char *p)
  159. {
  160.     _form_menus(p,FORM_BAR);
  161. }
  162.  
  163.