Selection output to xvgr files is now cleaner.
[gromacs/qmmm-gamess-us.git] / src / ngmx / xdlghi.c
bloba10317a32b7c5b7caed7ffa30aabc3ddc7570809
2 /*
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 3.2.0
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
33 * And Hey:
34 * Gyas ROwers Mature At Cryogenic Speed
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
40 #include <string.h>
42 #include "gmx_fatal.h"
43 #include "string2.h"
44 #include "sysstuff.h"
45 #include "smalloc.h"
46 #include "macros.h"
47 #include "xdlghi.h"
48 #include "fgrid.h"
50 t_dlgitem **CreateRadioButtonGroup(t_x11 *x11, char *szTitle,
51 t_id GroupID, int nrb, t_id rb[],
52 int nSelect,
53 char *szRB[], int x0,int y0)
54 /* This routine creates a radio button group at the
55 * specified position. The return values is a pointer to an
56 * array of dlgitems, the array has length (nrb+1) with the +1
57 * because of the groupbox.
58 * nSelect is the ordinal of the selected button.
61 t_dlgitem **dlgitem;
62 int x,y,w;
63 int i;
65 snew(dlgitem,nrb+1);
66 dlgitem[0]=CreateGroupBox(x11,szTitle,GroupID,nrb,rb,x0,y0,0,0,0);
67 x=x0+2*OFFS_X;
68 y=dlgitem[0]->win.y+dlgitem[0]->win.height;
69 w=0;
70 for (i=0; (i<nrb); i++) {
71 dlgitem[i+1]=CreateRadioButton(x11,szRB[i],(i==nSelect),
72 rb[i],GroupID,x,y,0,0,0);
73 y+=dlgitem[i+1]->win.height+OFFS_Y;
74 w=max(w,dlgitem[i+1]->win.width);
76 for (i=0; (i<nrb); i++)
77 dlgitem[i+1]->win.width=w;
78 dlgitem[0]->win.width=w+4*OFFS_X;
79 dlgitem[0]->win.height=y-y0;
81 return dlgitem;
84 t_dlgitem **CreateDlgitemGroup(t_x11 *x11, const char *szTitle,
85 t_id GroupID, int x0, int y0,
86 int nitem, ...)
87 /* This routine creates a dlgitem group at the
88 * specified position. The return values is a pointer to an
89 * array of dlgitems, the array has length (nitem+1) with the +1
90 * because of the groupbox.
93 va_list ap;
95 t_dlgitem **dlgitem;
96 t_id *ids;
97 edlgitem edlg;
98 char *name;
99 bool bBool;
100 Pixmap pm;
101 int nlines,buflen;
102 char *buf,**lines;
103 int x,y,w,i;
105 va_start(ap,nitem);
107 snew(dlgitem,nitem+1);
108 snew(ids,nitem);
109 x=x0+2*OFFS_X;
110 dlgitem[0]=CreateGroupBox(x11,szTitle,GroupID,nitem,ids,x0,y0,0,0,0);
111 y=dlgitem[0]->win.y+dlgitem[0]->win.height;
112 w=0;
113 for (i=0; (i<nitem); i++) {
114 edlg=(edlgitem)va_arg(ap,int);
115 ids[i]=va_arg(ap,int);
116 switch (edlg) {
117 case edlgBN:
118 name=va_arg(ap,char *);
119 bBool=va_arg(ap,int);
120 dlgitem[i+1]=CreateButton(x11,name,bBool,ids[i],GroupID,x,y,0,0,0);
121 break;
122 case edlgRB:
123 name=va_arg(ap,char *);
124 bBool=va_arg(ap,int);
125 dlgitem[i+1]=CreateRadioButton(x11,name,bBool,ids[i],GroupID,x,y,0,0,0);
126 break;
127 case edlgCB:
128 name=va_arg(ap,char *);
129 bBool=va_arg(ap,int);
130 dlgitem[i+1]=CreateCheckBox(x11,name,bBool,ids[i],GroupID,x,y,0,0,0);
131 break;
132 case edlgPM:
133 pm=va_arg(ap,Pixmap);
134 dlgitem[i+1]=CreatePixmap(x11,pm,ids[i],GroupID,x,y,0,0,0);
135 break;
136 case edlgST:
137 nlines=va_arg(ap,int);
138 lines=va_arg(ap,char **);
139 dlgitem[i+1]=CreateStaticText(x11,nlines,lines,ids[i],GroupID,
140 x,y,0,0,0);
141 break;
142 case edlgET:
143 name=va_arg(ap,char *);
144 buflen=va_arg(ap,int);
145 buf=va_arg(ap,char *);
146 dlgitem[i+1]=CreateEditText(x11,name,buflen,buf,ids[i],
147 GroupID,x,y,0,0,0);
148 break;
149 case edlgGB:
150 default:
151 gmx_fatal(FARGS,"Invalid dlgitem type: %d\n",edlg);
153 y+=dlgitem[i+1]->win.height+OFFS_Y;
154 w=max(w,dlgitem[i+1]->win.width);
156 va_end(ap);
157 sfree(dlgitem[0]->u.groupbox.item);
158 sfree(dlgitem[0]->win.text);
159 dlgitem[0]=CreateGroupBox(x11,szTitle,GroupID,nitem,ids,x0,y0,0,0,0);
160 for (i=0; (i<nitem); i++)
161 dlgitem[i+1]->win.width=w;
162 dlgitem[0]->win.width=w+4*OFFS_X;
163 dlgitem[0]->win.height=y-y0;
164 return dlgitem;
167 static void AddDlgItemGroups(t_dlg *dlg, int gridx, int gridy,
168 t_dlgitemlist **grid, bool bAutoPosition)
170 t_dlgitemlist *item;
171 int x1,y1,w1,h1;
172 int x,y,dw,dh;
173 float w,h;
175 w=h=0;
176 for(x=0; (x<gridx); x++)
177 for(y=0; (y<gridy); y++) {
178 item=&(grid[x][y]);
179 if (item->nitem) {
180 if (!item->list) {
181 printf("Error: empty list with non-empty nitem (%d)\n",item->nitem);
182 printf(" at grid point: %d,%d\n",x,y);
183 printf(" with size: %dx%d\n",item->w,item->h);
184 exit(1);
186 else {
187 AddDlgItems(dlg,item->nitem,item->list);
188 dw=item->w;
189 dh=item->h;
190 w=max(w,((float) QueryDlgItemW(dlg,item->list[0]->ID))/dw);
191 h=max(h,((float) QueryDlgItemH(dlg,item->list[0]->ID))/dh);
195 w1=gridx*w;
196 h1=gridy*h;
197 SetDlgSize(dlg,w1,h1,bAutoPosition);
198 #ifdef DEBUG
199 printf("Dimensions of grid cell: %8.3f x %8.3f\n",w,h);
200 printf("Dimensions of window: %d x %d\n",w1,h1);
201 #endif
203 for(x=0; (x<gridx); x++)
204 for(y=0; (y<gridy); y++) {
205 item=&(grid[x][y]);
206 if (item->nitem) {
207 x1=x*w;
208 y1=y*h;
209 w1=item->w*w;
210 h1=item->h*h;
211 #ifdef DEBUG
212 printf("New size: %d x %d at %d, %d\n",w1,h1,x1,y1);
213 #endif
214 SetDlgItemSize(dlg,item->list[0]->ID,w1,h1);
215 SetDlgItemPos(dlg,item->list[0]->ID,x1,y1);
220 static t_dlgitemlist **NewDlgitemList(int w, int h)
222 int i,j;
223 t_dlgitemlist **grid;
225 snew(grid,w);
226 for(i=0; (i<w); i++) {
227 snew(grid[i],h);
228 for (j=0; (j<h); j++) {
229 grid[i][j].nitem=0;
230 grid[i][j].list=NULL;
233 return grid;
236 static void AddListItem(t_dlgitemlist *list, t_dlgitem *item)
238 srenew(list->list,++list->nitem);
239 list->list[list->nitem-1]=item;
242 static void AddListFItem(t_x11 *x11, t_dlgitemlist *list,
243 t_fitem *fitem, t_id GroupID, t_id *ID,
244 int x, int *y, int *w,bool bUseMon)
246 int i,iSel,slen;
247 char buf[STRLEN];
249 switch(fitem->edlg) {
250 case edlgBN:
251 AddListItem
252 (list,CreateButton(x11,fitem->name[0],fitem->bDef,(*ID)++,GroupID,
253 x,(*y),0,0,0));
254 break;
255 case edlgRB:
256 strcpy(buf,fitem->def);
257 iSel=-1;
258 for (i=0; (i<fitem->nname); i++) {
259 char buf2[100];
261 strcpy(buf2,fitem->name[i]);
262 buf2[strlen(buf)]='\0'; /* truncate itemname */
263 if (strcasecmp(buf2,buf)==0)
264 iSel=i;
267 for(i=0; (i<fitem->nname); i++) {
268 AddListItem(list,
269 CreateRadioButton(x11,fitem->name[i],(iSel==i),
270 (*ID)++,GroupID,x,(*y),0,0,0));
271 (*y)+=list->list[list->nitem-1]->win.height+OFFS_Y;
272 (*w)=max((*w),list->list[list->nitem-1]->win.width);
273 SetDlgitemOpts(list->list[list->nitem-1],bUseMon,
274 fitem->set,fitem->get,fitem->help);
276 break;
277 case edlgCB: {
278 bool bCheck;
280 bCheck=strcasecmp(fitem->def,"TRUE")==0;
281 AddListItem(list,CreateCheckBox(x11,fitem->name[0],bCheck,
282 (*ID)++,GroupID,x,(*y),0,0,0));
283 break;
285 case edlgST:
286 AddListItem(list,
287 CreateStaticText(x11,fitem->nname,fitem->name,(*ID)++,
288 GroupID,x,(*y),0,0,0));
289 break;
290 case edlgET:
291 slen=strlen(fitem->name[0])+strlen(fitem->def);
292 AddListItem(list,CreateEditText(x11,fitem->name[0],slen,fitem->def,
293 (*ID)++,GroupID,x,(*y),0,0,0));
294 break;
295 case edlgPM:
296 case edlgGB:
297 default:
298 gmx_fatal(FARGS,"Invalid list->list type: %d\n",fitem->edlg);
300 SetDlgitemOpts(list->list[list->nitem-1],bUseMon,
301 fitem->set,fitem->get,fitem->help);
303 if (fitem->edlg != edlgRB) {
304 (*y)+=list->list[list->nitem-1]->win.height+OFFS_Y;
305 (*w)=max((*w),list->list[list->nitem-1]->win.width);
309 static void AddListFGroup(t_x11 *x11, t_dlgitemlist **grid,
310 t_fgroup *fgroup, t_id *ID,bool bUseMon)
312 int i;
313 t_id GroupID,*ids;
314 t_dlgitemlist *item;
315 int x,y,w;
317 GroupID=(*ID)++;
318 item=&(grid[fgroup->x][fgroup->y]);
319 AddListItem(item,CreateGroupBox(x11,fgroup->name,GroupID,
320 0,NULL,0,0,0,0,0));
321 x=2*OFFS_X;
322 y=item->list[0]->win.y+item->list[0]->win.height;
323 w=0;
324 for(i=0; (i<fgroup->nfitem); i++)
325 AddListFItem(x11,item,fgroup->fitem[i],GroupID,ID,x,&y,&w,bUseMon);
327 w=max(w,item->list[0]->win.width+4*OFFS_X);
328 sfree(item->list[0]->u.groupbox.item);
329 sfree(item->list[0]->win.text);
330 snew(ids,item->nitem);
331 for(i=0; (i<item->nitem-1); i++)
332 ids[i]=GroupID+i+1;
333 item->list[0]=
334 CreateGroupBox(x11,fgroup->name,GroupID,item->nitem-1,ids,
335 2*OFFS_X,2*OFFS_Y,w+2*OFFS_X,y,0);
336 sfree(ids);
337 item->w=fgroup->w;
338 item->h=fgroup->h;
341 static void AddListFSimple(t_x11 *x11, t_dlgitemlist **grid,
342 t_fsimple *fsimple, t_id *ID,bool bUseMon)
344 t_dlgitemlist *item;
345 int x,y,w;
347 item=&(grid[fsimple->x][fsimple->y]);
348 x=0;
349 y=0;
350 w=0;
351 AddListFItem(x11,item,fsimple->fitem,*ID,ID,x,&y,&w,bUseMon);
352 item->w=fsimple->w;
353 item->h=fsimple->h;
356 t_dlg *ReadDlg(t_x11 *x11,Window Parent, const char *title,
357 unsigned long fg, unsigned long bg, const char *infile,
358 int x0, int y0, bool bAutoPosition,bool bUseMon,
359 DlgCallback *cb,void *data)
361 t_fgrid *fgrid;
362 t_dlgitemlist **grid;
363 t_dlg *dlg;
364 int i;
365 t_id ID;
367 fgrid=FGridFromFile(infile);
368 dlg=CreateDlg(x11,Parent,title,x0,y0,0,0,0,fg,bg,cb,data);
369 grid=NewDlgitemList(fgrid->w,fgrid->h);
370 ID=0;
372 for(i=0; (i<fgrid->nfgroup); i++)
373 AddListFGroup(x11,grid,fgrid->fgroup[i],&ID,bUseMon);
374 for(i=0; (i<fgrid->nfsimple); i++)
375 AddListFSimple(x11,grid,fgrid->fsimple[i],&ID,bUseMon);
376 AddDlgItemGroups(dlg,fgrid->w,fgrid->h,grid,bAutoPosition);
378 DoneFGrid(fgrid);
380 return dlg;