Subversion Repositories wimsdev

Rev

Rev 17170 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 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
 
7673 bpr 18
/* html form simplification routines */
8185 bpr 19
#include "wims.h"
10 reyssat 20
 
21
#define MAX_MENU_ITEMS 256
22
 
23
enum {
12241 bpr 24
  FORM_SELECT, FORM_RADIO, FORM_BAR, FORM_CHECKBOX
10 reyssat 25
};
26
 
7673 bpr 27
/* Produces <select> and <option>s.
28
 * Parameter string: name from n1 to n2 [prompt plist]
29
 * or name, list slist [prompt plist]
8155 bpr 30
 * Using script to do this is too slow.
31
 */
10 reyssat 32
void _form_menus(char *p,int kind)
33
{
12241 bpr 34
  char *n, *li, *pp, *val, *p0, *p1, *p2, *pc, *s, *hmode, *name;
35
  char *vlist[MAX_MENU_ITEMS], *plist[MAX_MENU_ITEMS];
36
  char nbuf[MAX_LINELEN+1],vbuf[MAX_LINELEN+1],pbuf[MAX_LINELEN+1];
37
  char buf[256], pfrb[256], pfre[256], input_id[128];
38
  int i,i1=0,i2,itemcnt,type;
39
  if(!outputing) return;
40
  n=find_word_start(p); if(*n==0) return;
41
  pp=find_word_end(n);
42
  if(*pp!=0) *(pp++)=0;
43
  else {
44
    syntax: module_error("syntax_error"); return;
45
  }
46
  li=find_word_start(pp); pp=find_word_end(li);
47
  if(*pp!=0) *(pp++)=0; else goto syntax;
48
  itemcnt=0;
49
  if(strcmp(li,"from")==0) {
50
    p1=find_word_start(pp);
51
    p2=wordchr(p1,"to"); if(p2==NULL || p2<=p1) goto syntax;
52
    *(p2-1)=0; p2=find_word_start(p2+strlen("to"));
53
    pp=wordchr(p2,"prompt"); if(pp!=NULL) *(pp-1)=0;
54
    i1=evalue(p1); i2=evalue(p2);
55
    if(i1<-100000000 || i1>100000000 || i2<-100000000 || i2>100000000)
56
      goto syntax;
57
    if(i2<i1) return;
58
    if(i2>i1+MAX_MENU_ITEMS-1) i2=i1+MAX_MENU_ITEMS-1;
59
    itemcnt=i2-i1+1;
60
    type=0;
61
  }
62
  else {
63
    if(strcmp(li,"list")==0) {
7673 bpr 64
      p1=find_word_start(pp);
12241 bpr 65
      pp=wordchr(p1,"prompt"); if(pp!=NULL && pp>p1) *(pp-1)=0;
66
      ovlstrcpy(vbuf,p1);substit(vbuf);
67
      itemcnt=cutitems(vbuf,vlist,MAX_MENU_ITEMS);
68
      type=1;
10 reyssat 69
    }
12241 bpr 70
    else goto syntax;
71
  }
72
  for(i=0;i<itemcnt;i++) plist[i]=NULL;
73
  if(pp!=NULL) {
74
    p1=find_word_start(pp+strlen("prompt"));
75
    ovlstrcpy(pbuf,p1);substit(pbuf);
76
    cutitems(pbuf,plist,MAX_MENU_ITEMS);
77
  }
78
  ovlstrcpy(nbuf,n);substit(nbuf);
79
  if(kind==FORM_SELECT) {
80
    char *pp;
18321 obado 81
    s=getvar("wims_ref_class");
82
    if(s!=NULL && *s!=0 && !isspace(*s)) {
83
      snprintf(pfrb,sizeof(pfrb),"class=\"%s\"",s);
84
    } else { pfrb[0]=0;}
12241 bpr 85
    pp=getvar("wims_formselect_switch"); if(pp==NULL) pp="";
18321 obado 86
    output("<select %s %s name=\"%s\" id=\"%s\">\n",pp,pfrb,nbuf,nbuf);
12241 bpr 87
  }
88
  if(kind==FORM_BAR) {
89
    s=getvar("wims_ref_class");
90
    if(s!=NULL && *s!=0 && !isspace(*s)) {
91
      snprintf(pfrb,sizeof(pfrb)," <span class=\"%s\">",s); strcpy(pfre,"</span>");
92
    } else { pfrb[0]=0; pfre[0]=0; }
13285 obado 93
    output("%s<span style=\"font-weight:bold;\">-</span>&nbsp;",pfrb);
12241 bpr 94
  }
95
  val=getvar(nbuf);if(val==NULL) val="";
96
 
97
  s = getvar("wims_ref_id");
98
  if(s!=NULL && *s!=0 && !isspace(*s)) { strcpy(input_id,s); }
99
  else { strcpy(input_id,nbuf);}
100
 
101
  for(i=0;i<itemcnt;i++) {
102
    if(type==0) {
103
      snprintf(buf,sizeof(buf),"%d",i+i1);
104
      p0=buf;
10 reyssat 105
    }
12241 bpr 106
    else p0=vlist[i];
107
    if(*find_word_start(p0)==0) continue;
108
    if(plist[i]==NULL) plist[i]=p0;
109
    if(*val!=0 &&
110
        (strcmp(p0,val)==0 ||
111
        ( (kind==FORM_SELECT || kind==FORM_CHECKBOX)
112
        && itemchr(val,p0)!=NULL))) {
113
      if(kind==FORM_SELECT) pc=" selected=\"selected\"";
114
      else pc=" checked=\"checked\"";
10 reyssat 115
    }
12241 bpr 116
    else pc="";
9007 obado 117
 
12241 bpr 118
    s = getvar("wims_ref_class");
119
    hmode=getvar("wims_html_mode");
120
    pfrb[0]=0; pfre[0]=0;
121
    if(s!=NULL && *s!=0 && !isspace(*s)) {
122
      if(hmode==NULL || *hmode==0 || isspace(*hmode)) {
123
        hmode="span";
7673 bpr 124
      }
12241 bpr 125
      snprintf(pfrb, sizeof(pfrb), "<%s class=\"%s\">", hmode, s);
126
      snprintf(pfre, sizeof(pfre), "</%s>", hmode);
127
    } else {
128
      if(hmode!=NULL && *hmode!=0) {
129
        snprintf(pfrb, sizeof(pfrb), "<%s>", hmode);
130
        snprintf(pfre, sizeof(pfre), "</%s>", hmode);
7673 bpr 131
      }
12241 bpr 132
    }
133
    switch(kind) {
134
      case FORM_RADIO: name="radio"; break;
135
      case FORM_CHECKBOX: name="checkbox"; break;
13029 bpr 136
      case FORM_BAR: name="radio"; break;
12241 bpr 137
      default: name="";
138
    }
139
    switch(kind) {
140
      case FORM_SELECT:
141
        output("<option value=\"%s\"%s>%s</option>\n", p0, pc, plist[i]);
142
        break;
9007 obado 143
 
12241 bpr 144
      case FORM_RADIO:
145
      case FORM_CHECKBOX:
146
        if (itemcnt > 1) {
17170 bpr 147
          output("%s<input type=\"%s\" name=\"%s\" id=\"%s_%d\" value=\"%s\"%s><label for=\"%s_%d\">%s</label>%s",
12241 bpr 148
           pfrb,name,nbuf,input_id,i+1,p0,pc,input_id,i+1,plist[i],pfre);
18321 obado 149
          if (i==itemcnt) {
150
            output("</fieldset>");
151
          }
12241 bpr 152
        } else {
17170 bpr 153
          output("%s<input type=\"%s\" name=\"%s\" id=\"%s\" value=\"%s\"%s><label for=\"%s\">%s</label>%s",
12241 bpr 154
           pfrb,name,nbuf,input_id,p0,pc,input_id,plist[i],pfre);
9007 obado 155
        }
12241 bpr 156
        if(i<itemcnt-1 && itemcnt>2 && (hmode==NULL || *hmode==0)) _output_(",");
157
        _output_("\n");
158
        break;
159
      case FORM_BAR:
17170 bpr 160
        output("<input type=\"%s\" name=\"%s\" id=\"%s_%d\" title=\"%s\" value=\"%s\"%s><label style=\"display:none\" for=\"%s_%d\">%s</label>",
13285 obado 161
           name,nbuf,input_id,i+1,p0,p0,pc,input_id,i+1,p0);
12241 bpr 162
        break;
10 reyssat 163
    }
12241 bpr 164
  }
165
  setvar("wims_ref_class","");
166
  setvar("wims_ref_id","");
167
  setvar("wims_html_mode","");
168
  if(kind==FORM_SELECT) _output_("</select>");
16561 bpr 169
  if(kind==FORM_BAR) output("&nbsp;<span style=\"font-weight:bold;\">+</span>");
10 reyssat 170
}
171
 
172
void exec_formselect(char *p)
173
{
12241 bpr 174
  _form_menus(p,FORM_SELECT);
10 reyssat 175
}
176
 
177
void exec_formradio(char *p)
178
{
12241 bpr 179
  _form_menus(p,FORM_RADIO);
10 reyssat 180
}
181
 
182
void exec_formcheckbox(char *p)
183
{
15371 obado 184
  _form_menus(p,FORM_CHECKBOX);
10 reyssat 185
}
186
 
187
void exec_formbar(char *p)
188
{
12241 bpr 189
  _form_menus(p,FORM_BAR);
10 reyssat 190
}