1 /*
2  * $Id: x11commn.c,v 1.6 2003/02/16 12:43:37 isizaka Exp isizaka $
3  *
4  * This file is part of "Ngraph for X11".
5  *
6  * Copyright (C) 2002, Satoshi ISHIZAKA. isizaka@msa.biglobe.ne.jp
7  *
8  * "Ngraph for X11" is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * "Ngraph for X11" is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  */
23 
24 /**
25  *
26  * $Log: x11commn.c,v $
27  * Revision 1.6  2003/02/16 12:43:37  isizaka
28  * for release 6.13.18
29  *
30  * Revision 1.5  2002/07/06 08:57:25  isizaka
31  * change to GPL.
32  *
33  * Revision 1.4  1999/04/15 12:14:26  isizaka
34  * for release 6.03.01
35  *
36  * Revision 1.3  1999/04/11 06:09:20  isizaka
37  * *** empty log message ***
38  *
39  * Revision 1.2  1999/03/20 12:32:54  isizaka
40  * minor change
41  *
42  * Revision 1.1  1999/03/17 13:28:47  isizaka
43  * Initial revision
44  *
45  *
46  **/
47 
48 #include <Xm/XmAll.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <limits.h>
56 #include <fcntl.h>
57 
58 #include "ngraph.h"
59 #include "object.h"
60 #include "ioutil.h"
61 #include "shell.h"
62 #include "nstring.h"
63 #include "jnstring.h"
64 #include "odraw.h"
65 #include "config.h"
66 
67 #include "x11gui.h"
68 #include "x11dialg.h"
69 #include "ox11menu.h"
70 #include "x11menu.h"
71 #include "x11commn.h"
72 
73 #define FALSE 0
74 #define TRUE 1
75 
OpenGRA()76 void OpenGRA()
77 {
78   int i,id,otherGC;
79   char *device,*name;
80   struct narray *drawrable;
81 
82   if ((menulocal.GRAinst=chkobjinstoid(menulocal.GRAobj,menulocal.GRAoid))!=NULL) return;
83   /* search closed gra object */
84   if (chkobjinstoid(menulocal.GRAobj,menulocal.GRAoid)==NULL) CloseGRA();
85   for (i=chkobjlastinst(menulocal.GRAobj);i>=0;i--) {
86     getobj(menulocal.GRAobj,"GC",i,0,NULL,&otherGC);
87     if (otherGC<0) break;
88   }
89   if (i==-1) {
90     /* closed gra object is not found. generate new gra object */
91     id=newobj(menulocal.GRAobj);
92     menulocal.GRAinst=chkobjinst(menulocal.GRAobj,id);
93     _getobj(menulocal.GRAobj,"oid",menulocal.GRAinst,&(menulocal.GRAoid));
94     /* set page settings */
95     putobj(menulocal.GRAobj,"paper_width",id,&(menulocal.PaperWidth));
96     putobj(menulocal.GRAobj,"paper_height",id,&(menulocal.PaperHeight));
97     putobj(menulocal.GRAobj,"left_margin",id,&(menulocal.LeftMargin));
98     putobj(menulocal.GRAobj,"top_margin",id,&(menulocal.TopMargin));
99     putobj(menulocal.GRAobj,"zoom",id,&(menulocal.PaperZoom));
100     CheckPage();
101   } else {
102     /* find closed gra object */
103     id=i;
104     menulocal.GRAinst=chkobjinst(menulocal.GRAobj,id);
105     _getobj(menulocal.GRAobj,"oid",menulocal.GRAinst,&(menulocal.GRAoid));
106     /* get page settings */
107     CheckPage();
108   }
109   if (arraynum(&(menulocal.drawrable))>0) {
110     drawrable=arraynew(sizeof(char *));
111     for (i=0;i<arraynum(&(menulocal.drawrable));i++) {
112       arrayadd2(drawrable,(char **)arraynget(&(menulocal.drawrable),i));
113     }
114   } else drawrable=NULL;
115   putobj(menulocal.GRAobj,"draw_obj",id,drawrable);
116   device=(char *)memalloc(10);
117   strcpy(device,"menu:0");
118   putobj(menulocal.GRAobj,"device",id,device);
119   name=(char *)memalloc(7);
120   strcpy(name,"viewer");
121   putobj(menulocal.GRAobj,"name",id,name);
122   getobj(menulocal.GRAobj,"open",id,0,NULL,&(menulocal.GC));
123 }
124 
CheckPage()125 void CheckPage()
126 {
127   _getobj(menulocal.GRAobj,"paper_width",menulocal.GRAinst,
128           &(menulocal.PaperWidth));
129   _getobj(menulocal.GRAobj,"paper_height",menulocal.GRAinst,
130           &(menulocal.PaperHeight));
131   _getobj(menulocal.GRAobj,"left_margin",menulocal.GRAinst,
132           &(menulocal.LeftMargin));
133   _getobj(menulocal.GRAobj,"top_margin",menulocal.GRAinst,
134           &(menulocal.TopMargin));
135   _getobj(menulocal.GRAobj,"zoom",menulocal.GRAinst,
136           &(menulocal.PaperZoom));
137 }
138 
ValidGRA()139 int ValidGRA()
140 {
141   int id;
142   struct objlist *graobj;
143   char *inst;
144 
145   if ((graobj=chkobject("gra"))==NULL) return -1;
146   id=-1;
147   if ((inst=chkobjinstoid(graobj,menulocal.GRAoid))!=NULL)
148     _getobj(graobj,"id",inst,&id);
149   if (id==-1) id=chkobjlastinst(graobj);
150   return id;
151 }
152 
CloseGRA()153 void CloseGRA()
154 {
155   int id;
156 
157   if ((menulocal.GRAinst=chkobjinstoid(menulocal.GRAobj,menulocal.GRAoid))==NULL) return;
158   if (chkobjinstoid(menulocal.GRAobj,menulocal.GRAoid)!=NULL) {
159     _getobj(menulocal.GRAobj,"id",menulocal.GRAinst,&id);
160     exeobj(menulocal.GRAobj,"close",id,0,NULL);
161     delobj(menulocal.GRAobj,id);
162     menulocal.GRAinst=NULL;
163     menulocal.GRAoid=-1;
164   }
165 }
166 
ChangeGRA()167 void ChangeGRA()
168 {
169   int i,otherGC;
170 
171   /* search for closed gra object */
172   for (i=chkobjlastinst(menulocal.GRAobj);i>=0;i--) {
173     getobj(menulocal.GRAobj,"GC",i,0,NULL,&otherGC);
174     if (otherGC<0) break;
175   }
176   if (i==-1) {
177     /* closed gra is not find. maintain present gra object */
178     if ((menulocal.GRAinst=chkobjinstoid(menulocal.GRAobj,menulocal.GRAoid))
179         ==NULL) {
180       ChangePage();
181     }
182   } else {
183     /* use closed gra object */
184     ChangePage();
185   }
186   CheckPage();
187 }
188 
SetPageSettingsToGRA()189 void SetPageSettingsToGRA()
190 {
191   int i,j,num,otherGC,id;
192   struct objlist *obj;
193   char *inst;
194   struct narray *drawrable;
195 
196   if ((obj=chkobject("gra"))==NULL) return;
197   for (i=chkobjlastinst(obj);i>=0;i--) {
198     getobj(obj,"GC",i,0,NULL,&otherGC);
199     if (otherGC<0) break;
200   }
201   if (i>=0) {
202     id=i;
203     inst=chkobjinst(obj,id);
204     putobj(obj,"paper_width",id,&(menulocal.PaperWidth));
205     putobj(obj,"paper_height",id,&(menulocal.PaperHeight));
206     putobj(obj,"left_margin",id,&(menulocal.LeftMargin));
207     putobj(obj,"top_margin",id,&(menulocal.TopMargin));
208     putobj(obj,"zoom",id,&(menulocal.PaperZoom));
209     _getobj(obj,"draw_obj",inst,&drawrable);
210     arrayfree2(drawrable);
211     drawrable=arraynew(sizeof(char *));
212     num=arraynum(&(menulocal.drawrable));
213     for (j=0;j<num;j++)
214       arrayadd2(drawrable,(char **)arraynget(&(menulocal.drawrable),j));
215     _putobj(obj,"draw_obj",inst,&drawrable);
216   }
217 }
218 
GetPageSettingsFromGRA()219 void GetPageSettingsFromGRA()
220 {
221   int i,j,num,otherGC,id;
222   struct objlist *obj;
223   char *inst;
224   struct narray *drawrable;
225 
226   if ((obj=chkobject("gra"))==NULL) return;
227   for (i=chkobjlastinst(obj);i>=0;i--) {
228     getobj(obj,"GC",i,0,NULL,&otherGC);
229     if (otherGC<0) break;
230   }
231   if (i>=0) {
232     id=i;
233     inst=chkobjinst(obj,id);
234     CheckPage();
235     _getobj(obj,"draw_obj",inst,&drawrable);
236     arraydel2(&(menulocal.drawrable));
237     num=arraynum(drawrable);
238     for (j=0;j<num;j++) {
239       arrayadd2(&(menulocal.drawrable),(char **)arraynget(drawrable,j));
240     }
241   }
242   ChangeGRA();
243 }
244 
AxisDel2(int id)245 void AxisDel2(int id)
246 {
247   struct objlist *obj,*aobj;
248   int i,spc,aid1=0,aid2=0;
249   char *axis,*axis2;
250   struct narray iarray;
251   int anum;
252   char *inst;
253   char *axisx,*axisy;
254   int findx,findy;
255 
256   if ((obj=getobject("axisgrid"))!=NULL) {
257     for (i=chkobjlastinst(obj);i>=0;i--) {
258       if ((inst=chkobjinst(obj,i))!=NULL) {
259         _getobj(obj,"axis_x",inst,&axisx);
260         _getobj(obj,"axis_y",inst,&axisy);
261         findx=findy=FALSE;
262         if (axisx!=NULL) {
263           arrayinit(&iarray,sizeof(int));
264           if (!getobjilist(axisx,&aobj,&iarray,FALSE,NULL)) {
265             anum=arraynum(&iarray);
266             if (anum>0) {
267               aid1=*(int *)arraylast(&iarray);
268               if (aid1==id) findx=TRUE;
269             }
270            }
271           arraydel(&iarray);
272         }
273         if (axisy!=NULL) {
274           arrayinit(&iarray,sizeof(int));
275           if (!getobjilist(axisy,&aobj,&iarray,FALSE,NULL)) {
276             anum=arraynum(&iarray);
277             if (anum>0) {
278               aid1=*(int *)arraylast(&iarray);
279               if (aid1==id) findy=TRUE;
280             }
281           }
282           arraydel(&iarray);
283         }
284         if (findx || findy) delobj(obj,i);
285       }
286     }
287   }
288   if ((obj=getobject("file"))==NULL) return;
289   for (i=0;i<=chkobjlastinst(obj);i++) {
290     if ((getobj(obj,"axis_x",i,0,NULL,&axis)>=0) && (axis!=NULL)) {
291       arrayinit(&iarray,sizeof(int));
292       if (!getobjilist(axis,&aobj,&iarray,FALSE,&spc)) {
293         anum=arraynum(&iarray);
294         if ((anum>0) && (spc==1)) {
295           aid1=*(int *)arraylast(&iarray);
296           if (aid1>id) aid1--;
297         } else aid1=-1;
298         arraydel(&iarray);
299       }
300     }
301     if ((getobj(obj,"axis_y",i,0,NULL,&axis)>=0) && (axis!=NULL)) {
302       arrayinit(&iarray,sizeof(int));
303       if (!getobjilist(axis,&aobj,&iarray,FALSE,&spc)) {
304         anum=arraynum(&iarray);
305         if ((anum>0) && (spc==1)) {
306           aid2=*(int *)arraylast(&iarray);
307           if (aid2>id) aid2--;
308         } else aid2=-1;
309         arraydel(&iarray);
310       }
311     }
312     if ((aid1>=0) && (aid2>=0)) {
313       if (aid1==aid2) aid2=aid1+1;
314     }
315     if ((aid1>=0) && (aid2>=0)) {
316       if ((axis2=(char *)memalloc(strlen(chkobjectname(aobj))+10))!=NULL) {
317         sprintf(axis2,"%s:%d",chkobjectname(aobj),aid1);
318         putobj(obj,"axis_x",i,axis2);
319       }
320       if ((axis2=(char *)memalloc(strlen(chkobjectname(aobj))+10))!=NULL) {
321         sprintf(axis2,"%s:%d",chkobjectname(aobj),aid2);
322         putobj(obj,"axis_y",i,axis2);
323       }
324     }
325   }
326 }
327 
AxisDel(int id)328 void AxisDel(int id)
329 {
330   struct objlist *obj;
331   int i,lastinst;
332   char *group,*group2;
333   char type;
334   char *inst,*inst2;
335   char group3[20];
336 
337   if ((obj=chkobject("axis"))==NULL) return;
338   if ((inst=chkobjinst(obj,id))==NULL) return;
339   _getobj(obj,"group",inst,&group);
340   if ((group==NULL) || (group[0]=='a')) {
341     AxisDel2(id);
342     delobj(obj,id);
343     return;
344   }
345   lastinst=chkobjlastinst(obj);
346   type=group[0];
347   strcpy(group3,group);
348   for (i=lastinst;i>=0;i--) {
349     inst2=chkobjinst(obj,i);
350     _getobj(obj,"group",inst2,&group2);
351     if ((group2!=NULL) && (group2[0]==type)) {
352       if (strcmp(group3+2,group2+2)==0) {
353         AxisDel2(i);
354         delobj(obj,i);
355       }
356     }
357   }
358 }
359 
AxisMove(int id1,int id2)360 void AxisMove(int id1,int id2)
361 {
362   struct objlist *obj,*aobj;
363   int i,spc,aid;
364   char *axis,*axis2;
365   struct narray iarray;
366   int anum;
367 
368   if ((obj=getobject("file"))==NULL) return;
369   for (i=0;i<=chkobjlastinst(obj);i++) {
370     if ((getobj(obj,"axis_x",i,0,NULL,&axis)>=0) && (axis!=NULL)) {
371       arrayinit(&iarray,sizeof(int));
372       if (!getobjilist(axis,&aobj,&iarray,FALSE,&spc)) {
373         anum=arraynum(&iarray);
374         if ((anum>0) && (spc==1)) {
375           aid=*(int *)arraylast(&iarray);
376           if (aid==id1) aid=id2;
377           else {
378             if (aid>id1) aid--;
379             if (aid>=id2) aid++;
380           }
381           if ((axis2=(char *)memalloc(strlen(chkobjectname(aobj))+10))!=NULL) {
382             sprintf(axis2,"%s:%d",chkobjectname(aobj),aid);
383             putobj(obj,"axis_x",i,axis2);
384           }
385         }
386         arraydel(&iarray);
387       }
388     }
389     if ((getobj(obj,"axis_y",i,0,NULL,&axis)>=0) && (axis!=NULL)) {
390       arrayinit(&iarray,sizeof(int));
391       if (!getobjilist(axis,&aobj,&iarray,FALSE,&spc)) {
392         anum=arraynum(&iarray);
393         if ((anum>0) && (spc==1)) {
394           aid=*(int *)arraylast(&iarray);
395           if (aid==id1) aid=id2;
396           else {
397             if (aid>id1) aid--;
398             if (aid>=id2) aid++;
399           }
400           if ((axis2=(char *)memalloc(strlen(chkobjectname(aobj))+10))!=NULL) {
401             sprintf(axis2,"%s:%d",chkobjectname(aobj),aid);
402             putobj(obj,"axis_y",i,axis2);
403           }
404         }
405         arraydel(&iarray);
406       }
407     }
408   }
409 }
410 
AxisNameToGroup()411 void AxisNameToGroup()
412 {
413   int idx,idy,idu,idr;
414   int findX,findY,findU,findR,graphtype;
415   int id,id2,num;
416   struct objlist *obj;
417   char *name,*name2,*gp;
418   struct narray group,iarray;
419   int a,j,anum;
420   char *argv[2];
421   int *data;
422   char *inst;
423 
424 
425   if ((obj=(struct objlist *)chkobject("axis"))==NULL) return;
426   num=chkobjlastinst(obj);
427   arrayinit(&iarray,sizeof(int));
428   for (id=0;id<=num;id++) {
429     anum=arraynum(&iarray);
430     data=(int *)arraydata(&iarray);
431     for (j=0;j<anum;j++) if (data[j]==id) break;
432     inst=chkobjinst(obj,id);
433     _getobj(obj,"name",inst,&name);
434     _getobj(obj,"group",inst,&gp);
435     if ((j==anum) && (gp[0]=='a')) {
436       findX=findY=findU=findR=FALSE;
437       graphtype=-1;
438       if (name!=NULL) {
439         if (strncmp(name,"sectionX",8)==0) {
440           graphtype=0;
441           findX=TRUE;
442           idx=id;
443         } else if (strncmp(name,"sectionY",8)==0) {
444           graphtype=0;
445           findY=TRUE;
446           idy=id;
447         } else if (strncmp(name,"sectionU",8)==0) {
448           graphtype=0;
449           findU=TRUE;
450           idu=id;
451         } else if (strncmp(name,"sectionR",8)==0) {
452           graphtype=0;
453           findR=TRUE;
454           idr=id;
455         } else if (strncmp(name,"frameX",6)==0) {
456           graphtype=1;
457           findX=TRUE;
458           idx=id;
459         } else if (strncmp(name,"frameY",6)==0) {
460           graphtype=1;
461           findY=TRUE;
462           idy=id;
463         } else if (strncmp(name,"frameU",6)==0) {
464           graphtype=1;
465           findU=TRUE;
466           idu=id;
467         } else if (strncmp(name,"frameR",6)==0) {
468           graphtype=1;
469           findR=TRUE;
470           idr=id;
471         } else if (strncmp(name,"crossX",6)==0) {
472           graphtype=2;
473           findX=TRUE;
474           idx=id;
475         } else if (strncmp(name,"crossY",6)==0) {
476           graphtype=2;
477           findY=TRUE;
478           idy=id;
479         }
480       }
481       for (id2=id+1;id2<=num;id2++) {
482         for (j=0;j<anum;j++) if (data[j]==id2) break;
483         inst=chkobjinst(obj,id2);
484         _getobj(obj,"name",inst,&name2);
485         _getobj(obj,"group",inst,&gp);
486         if ((j==anum) && (gp[0]=='a')) {
487           if (graphtype==0) {
488             if (name2!=NULL) {
489               if ((strncmp(name2,"sectionX",8)==0)
490               && (strcmp(name+8,name2+8)==0)) {
491                 findX=TRUE;
492                 idx=id2;
493               } else if ((strncmp(name2,"sectionY",8)==0)
494               && (strcmp(name+8,name2+8)==0)) {
495                 findY=TRUE;
496                 idy=id2;
497               } else if ((strncmp(name2,"sectionU",8)==0)
498               && (strcmp(name+8,name2+8)==0)) {
499                 findU=TRUE;
500                 idu=id2;
501               } else if ((strncmp(name2,"sectionR",8)==0)
502               && (strcmp(name+8,name2+8)==0)) {
503                 findR=TRUE;
504                 idr=id2;
505               }
506             }
507           } else if (graphtype==1) {
508             if (name2!=NULL) {
509               if ((strncmp(name2,"frameX",6)==0)
510               && (strcmp(name+6,name2+6)==0)) {
511                 findX=TRUE;
512                 idx=id2;
513               } else if ((strncmp(name2,"frameY",6)==0)
514               && (strcmp(name+6,name2+6)==0)) {
515                 findY=TRUE;
516                 idy=id2;
517               } else if ((strncmp(name2,"frameU",6)==0)
518               && (strcmp(name+6,name2+6)==0)) {
519                 findU=TRUE;
520                 idu=id2;
521               } else if ((strncmp(name2,"frameR",6)==0)
522               && (strcmp(name+6,name2+6)==0)) {
523                 findR=TRUE;
524                 idr=id2;
525               }
526             }
527           } else if (graphtype==2) {
528             if (name2!=NULL) {
529               if ((strncmp(name2,"crossX",6)==0)
530               && (strcmp(name+6,name2+6)==0)) {
531                 findX=TRUE;
532                 idx=id2;
533               } else if ((strncmp(name2,"crossY",6)==0)
534               && (strcmp(name+6,name2+6)==0)) {
535                 findY=TRUE;
536                 idy=id2;
537               }
538             }
539           }
540         }
541       }
542       if ((graphtype==0) && findX && findY && findU && findR) {
543         arrayinit(&group,sizeof(int));
544         a=2;
545         arrayadd(&group,&a);
546         arrayadd(&group,&idx);
547         arrayadd(&group,&idy);
548         arrayadd(&group,&idu);
549         arrayadd(&group,&idr);
550         argv[0]=(char *)&group;
551         argv[1]=NULL;
552         exeobj(obj,"grouping",id,1,argv);
553         arraydel(&group);
554         arrayadd(&iarray,&idx);
555         arrayadd(&iarray,&idy);
556         arrayadd(&iarray,&idu);
557         arrayadd(&iarray,&idr);
558       } else if ((graphtype==1) && findX && findY && findU && findR) {
559         arrayinit(&group,sizeof(int));
560         a=1;
561         arrayadd(&group,&a);
562         arrayadd(&group,&idx);
563         arrayadd(&group,&idy);
564         arrayadd(&group,&idu);
565         arrayadd(&group,&idr);
566         argv[0]=(char *)&group;
567         argv[1]=NULL;
568         exeobj(obj,"grouping",id,1,argv);
569         arraydel(&group);
570         arrayadd(&iarray,&idx);
571         arrayadd(&iarray,&idy);
572         arrayadd(&iarray,&idu);
573         arrayadd(&iarray,&idr);
574       } else if ((graphtype==2) && findX && findY) {
575         arrayinit(&group,sizeof(int));
576         a=3;
577         arrayadd(&group,&a);
578         arrayadd(&group,&idx);
579         arrayadd(&group,&idy);
580         argv[0]=(char *)&group;
581         argv[1]=NULL;
582         exeobj(obj,"grouping",id,1,argv);
583         arraydel(&group);
584         arrayadd(&iarray,&idx);
585         arrayadd(&iarray,&idy);
586       }
587     }
588   }
589   arraydel(&iarray);
590 }
591 
FitDel(struct objlist * obj,int id)592 void FitDel(struct objlist *obj,int id)
593 {
594   char *fit;
595   struct objlist *fitobj;
596   int fitid,idnum;
597   struct narray iarray;
598 
599   if ((getobj(obj,"fit",id,0,NULL,&fit)>=0) && (fit!=NULL)) {
600     arrayinit(&iarray,sizeof(int));
601     if (getobjilist(fit,&fitobj,&iarray,FALSE,NULL)==0) {
602       idnum=arraynum(&iarray);
603       if (idnum>=1) {
604         fitid=*(int *)arraylast(&iarray);
605         delobj(fitobj,fitid);
606       }
607     }
608     arraydel(&iarray);
609   }
610 }
611 
FitCopy(struct objlist * obj,int did,int sid)612 void FitCopy(struct objlist *obj,int did,int sid)
613 {
614   char *fit;
615   struct objlist *fitobj;
616   int fitid;
617   struct narray iarray;
618   struct narray iarray2;
619   int j,id2,perm,type,idnum,idnum2;
620   char *field;
621   int fitoid;
622   char *inst;
623 
624   if ((getobj(obj,"fit",sid,0,NULL,&fit)>=0) && (fit!=NULL)) {
625     arrayinit(&iarray,sizeof(int));
626     if (getobjilist(fit,&fitobj,&iarray,FALSE,NULL)==0) {
627       idnum=arraynum(&iarray);
628       if (idnum>=1) {
629         fitid=*(int *)arraylast(&iarray);
630         if ((getobj(obj,"fit",did,0,NULL,&fit)>=0) && (fit!=NULL)) {
631           arrayinit(&iarray2,sizeof(int));
632           if (getobjilist(fit,&fitobj,&iarray2,FALSE,NULL)==0) {
633             idnum2=arraynum(&iarray2);
634             if (idnum2>=1) {
635               id2=*(int *)arraylast(&iarray2);
636             } else id2=newobj(fitobj);
637           } else id2=newobj(fitobj);
638           arraydel(&iarray2);
639         } else id2=newobj(fitobj);
640         if (id2>=0) {
641           for (j=0;j<chkobjfieldnum(fitobj);j++) {
642             field=chkobjfieldname(fitobj,j);
643             perm=chkobjperm(fitobj,field);
644             type=chkobjfieldtype(fitobj,field);
645             if ((strcmp2(field,"name")!=0) && (strcmp2(field,"equation")!=0)
646               && ((perm&NREAD)!=0) && ((perm&NWRITE)!=0) && (type<NVFUNC))
647               copyobj(fitobj,field,id2,fitid);
648           }
649           inst=getobjinst(fitobj,id2);
650           _getobj(fitobj,"oid",inst,&fitoid);
651           if ((fit=mkobjlist(fitobj,NULL,fitoid,NULL,TRUE))!=NULL) {
652             if (putobj(obj,"fit",did,fit)==-1) memfree(fit);
653           }
654         }
655       }
656     }
657     arraydel(&iarray);
658   }
659 }
660 
FitClear()661 void FitClear()
662 {
663   struct objlist *obj,*fitobj;
664   int i,anum,id,hidden;
665   char *fit;
666   struct narray iarray;
667 
668   if (menulock || globallock) return;
669   if ((obj=chkobject("file"))==NULL) return;
670   if ((fitobj=chkobject("fit"))==NULL) return;
671   for (i=0;i<=chkobjlastinst(obj);i++) {
672     getobj(obj,"fit",i,0,NULL,&fit);
673     if (fit!=NULL) {
674       arrayinit(&iarray,sizeof(int));
675       if (getobjilist(fit,&fitobj,&iarray,FALSE,NULL)==0) {
676         anum=arraynum(&iarray);
677         if (anum>=1) {
678           id=*(int *)arraylast(&iarray);
679           getobj(obj,"hidden",i,0,NULL,&hidden);
680           if (!hidden) putobj(fitobj,"equation",id,NULL);
681         }
682       }
683       arraydel(&iarray);
684     }
685   }
686 }
687 
DeleteDrawable()688 void DeleteDrawable()
689 {
690   struct objlist *fileobj,*drawobj;
691   int i;
692 
693   if ((fileobj=chkobject("file"))!=NULL) {
694     for (i=0;i<=chkobjlastinst(fileobj);i++) FitDel(fileobj,i);
695   }
696   if ((drawobj=chkobject("draw"))!=NULL) delchildobj(drawobj);
697 }
698 
SaveParent(HANDLE hFile,struct objlist * parent,int storedata,int storemerge)699 void SaveParent(HANDLE hFile,struct objlist *parent,int storedata,
700                 int storemerge)
701 {
702   struct objlist *ocur;
703   int i,j,instnum;
704   char *s;
705   char *inst,*fname,*fname2;
706 
707   ocur=chkobjroot();
708   while (ocur!=NULL) {
709     if (chkobjparent(ocur)==parent) {
710       if ((instnum=chkobjlastinst(ocur))!=-1) {
711         for (i=0;i<=instnum;i++) {
712           getobj(ocur,"save",i,0,NULL,&s);
713           nwrite(hFile,s,strlen(s));
714           nwrite(hFile,"\n",1);
715           if (storedata && (ocur==chkobject("file"))) {
716             getobj(ocur,"file",i,0,NULL,&fname);
717             if (fname!=NULL) {
718               for (j=i-1;j>=0;j--) {
719                 getobj(ocur,"file",j,0,NULL,&fname2);
720                 if ((fname2!=NULL) && (strcmp0(fname,fname2)==0)) break;
721               }
722               inst=chkobjinst(ocur,i);
723               if (j==-1) {
724                 while (_exeobj(ocur,"store_data",inst,0,NULL)==0) {
725                   _getobj(ocur,"store_data",inst,&s);
726                   nwrite(hFile,s,strlen(s));
727                   nwrite(hFile,"\n",1);
728                 }
729               } else  {
730                 while (_exeobj(ocur,"store_dummy",inst,0,NULL)==0) {
731                   _getobj(ocur,"store_dummy",inst,&s);
732                   nwrite(hFile,s,strlen(s));
733                   nwrite(hFile,"\n",1);
734                 }
735               }
736             }
737           }
738           if (storemerge && (ocur==chkobject("merge"))) {
739             getobj(ocur,"file",i,0,NULL,&fname);
740             if (fname!=NULL) {
741               for (j=i-1;j>=0;j--) {
742                 getobj(ocur,"file",j,0,NULL,&fname2);
743                 if ((fname2!=NULL) && (strcmp0(fname,fname2)==0)) break;
744               }
745               inst=chkobjinst(ocur,i);
746               if (j==-1) {
747                 while (_exeobj(ocur,"store_data",inst,0,NULL)==0) {
748                   _getobj(ocur,"store_data",inst,&s);
749                   nwrite(hFile,s,strlen(s));
750                   nwrite(hFile,"\n",1);
751                 }
752               } else {
753                 while (_exeobj(ocur,"store_dummy",inst,0,NULL)==0) {
754                   _getobj(ocur,"store_dummy",inst,&s);
755                   nwrite(hFile,s,strlen(s));
756                   nwrite(hFile,"\n",1);
757                 }
758               }
759             }
760           }
761         }
762       }
763       SaveParent(hFile,ocur,storedata,storemerge);
764     }
765     ocur=ocur->next;
766   }
767 }
768 
SaveDrawrable(char * name,int storedata,int storemerge)769 int SaveDrawrable(char *name,int storedata,int storemerge)
770 {
771   HANDLE hFile;
772   struct objlist *sysobj,*drawobj,*graobj;
773   int id,len,error;
774   char *arg[2];
775   struct narray sarray;
776   char *inst,*ver,*sysname,*s,*opt,*comment;
777 
778   error=FALSE;
779   hFile=nopen(name,O_CREAT|O_TRUNC|O_RDWR,NFMODE);
780   sysobj=chkobject("system");
781   inst=chkobjinst(sysobj,0);
782   _getobj(sysobj,"name",inst,&sysname);
783   _getobj(sysobj,"version",inst,&ver);
784   if ((comment=(char *)memalloc(256))!=NULL) {
785     len=sprintf(comment,"#!ngraph\n#%%creator: %s %s \n#%%version: %s\n",
786                 sysname,PLATFORM,ver);
787     if (nwrite(hFile,comment,len)!=len) error=TRUE;
788     memfree(comment);
789   }
790   if ((drawobj=chkobject("draw"))!=NULL) {
791     SaveParent(hFile,drawobj,storedata,storemerge);
792     if ((graobj=chkobject("gra"))!=NULL) {
793       id=ValidGRA();
794       if (id!=-1) {
795         arrayinit(&sarray,sizeof(char *));
796         opt="device";
797         arrayadd(&sarray,&opt);
798         arg[0]=(char *)&sarray;
799         arg[1]=NULL;
800         getobj(graobj,"save",id,1,arg,&s);
801         arraydel(&sarray);
802         len=strlen(s);
803         if (nwrite(hFile,s,strlen(s))!=len) error=TRUE;
804       } else error=TRUE;
805     }
806   }
807   nclose(hFile);
808   if (error) MessageBox(TopLevel,"I/O error: Write",NULL,MB_OK);
809   return !error;
810 }
811 
GraphSave(int overwrite)812 int GraphSave(int overwrite)
813 {
814   char mes[256],*buf;
815   int i,path,error;
816   struct objlist *obj;
817   int sdata,smerge;
818   int ret;
819   char *ext;
820   char *initfil;
821   char *file;
822 
823   error=FALSE;
824   if (NgraphApp.FileName!=NULL) {
825     initfil=NgraphApp.FileName;
826     if ((ext=getextention(initfil))!=NULL) {
827       if ((strcmp0(ext,"PRM")==0) || (strcmp0(ext,"prm")==0))
828         strcpy(ext,"ngp");
829     }
830   } else initfil=NULL;
831   if ((initfil==NULL) || (!overwrite || (access(initfil,04)==-1))) {
832     ret=nGetSaveFileName(TopLevel,"Save NGP file","ngp",
833                    &(menulocal.graphloaddir),initfil,
834                    &file,"*.ngp",menulocal.changedirectory);
835   } else {
836     if ((file=malloc(strlen(initfil)+1))==NULL) return IDCANCEL;
837     strcpy(file,initfil);
838     ret=IDOK;
839   }
840   if (ret==IDOK) {
841     SaveDialog(&DlgSave,&sdata,&smerge);
842     if ((ret=DialogExecute(TopLevel,&DlgSave))==IDOK) {
843       path=DlgSave.Path;
844       if ((obj=chkobject("file"))!=NULL) {
845         for (i=0;i<=chkobjlastinst(obj);i++)
846           putobj(obj,"save_path",i,&path);
847       }
848       if ((obj=chkobject("merge"))!=NULL) {
849         for (i=0;i<=chkobjlastinst(obj);i++)
850           putobj(obj,"save_path",i,&path);
851       }
852       if ((!overwrite) && (access(file,04)==0)) {
853         buf=(char *)memalloc(strlen(file)+60);
854         if (buf!=NULL) {
855           sprintf(buf,"`%s'\n\nOverwrite existing file?",file);
856           if (MessageBox(TopLevel,buf,"Save",MB_YESNO)!=IDYES) {
857             memfree(buf);
858             free(file);
859             return IDCANCEL;
860           }
861           memfree(buf);
862         } else {
863           if (MessageBox(TopLevel,"Overwrite existing file?",
864           "Save",MB_YESNO)!=IDYES)
865            free(file);
866            return IDCANCEL;
867         }
868       }
869       sprintf(mes,"Saving `%.128s'.",file);
870       SetStatusBar(mes);
871       error=!SaveDrawrable(file,sdata,smerge);
872       changefilename(file);
873       AddNgpFileList(file);
874       ResetStatusBar();
875       SetFileName(file);
876       SetCaption(file);
877       NgraphApp.Changed=FALSE;
878     }
879   }
880   free(file);
881   if (error) ret=IDCANCEL;
882   return ret;
883 }
884 
ToFullPath()885 void ToFullPath()
886 {
887   struct objlist *obj;
888   int i;
889   char *file,*file2;
890 
891   if ((obj=chkobject("file"))!=NULL) {
892     for (i=0;i<=chkobjlastinst(obj);i++) {
893       getobj(obj,"file",i,0,NULL,&file);
894       file2=getfullpath(file);
895       putobj(obj,"file",i,file2);
896     }
897   }
898   if ((obj=chkobject("merge"))!=NULL) {
899     for (i=0;i<=chkobjlastinst(obj);i++) {
900       getobj(obj,"file",i,0,NULL,&file);
901       file2=getfullpath(file);
902       putobj(obj,"file",i,file2);
903     }
904   }
905 }
906 
LoadNgpFile(char * File,int ignorepath,int expand,char * exdir,int console,char * option)907 void LoadNgpFile(char *File,int ignorepath,int expand,char *exdir,
908                  int console,char *option)
909 {
910   struct objlist *sys;
911   char *expanddir;
912   struct objlist *obj,*aobj;
913   char *name;
914   int i,newid,allocnow=FALSE;
915   char *s;
916   int len;
917   char *argv[2];
918   struct narray sarray;
919   char mes[256];
920   int sec;
921   char *inst;
922 
923   if ((sys=chkobject("system"))==NULL) return;
924   if ((expanddir=(char *)memalloc(strlen(exdir)+1))==NULL) return;
925   strcpy(expanddir,exdir);
926   putobj(sys,"expand_dir",0,expanddir);
927   putobj(sys,"expand_file",0,&expand);
928   putobj(sys,"ignore_path",0,&ignorepath);
929   if ((obj=chkobject("shell"))==NULL) return;
930   newid=newobj(obj);
931   if (newid>=0) {
932     inst=chkobjinst(obj,newid);
933     arrayinit(&sarray,sizeof(char *));
934     while ((s=getitok2(&option,&len," \t"))!=NULL) {
935       if (arrayadd(&sarray,&s)==NULL) {
936         memfree(s);
937         arraydel2(&sarray);
938         ignorepath=FALSE;
939         putobj(sys,"ignore_path",0,&ignorepath);
940         return;
941       }
942     }
943     if ((name=(char *)memalloc(strlen(File)+1))==NULL) {
944       arraydel2(&sarray);
945       ignorepath=FALSE;
946       putobj(sys,"ignore_path",0,&ignorepath);
947       return;
948     }
949     SetFileName(File);
950     SetCaption(File);
951     strcpy(name,File);
952     changefilename(name);
953     if (arrayadd(&sarray,&name)==NULL) {
954       memfree(name);
955       arraydel2(&sarray);
956       ignorepath=FALSE;
957       putobj(sys,"ignore_path",0,&ignorepath);
958       return;
959     }
960     DeleteDrawable();
961     if (console) allocnow=AllocConsole();
962     sec=TRUE;
963     argv[0]=(char *)&sec;
964     argv[1]=NULL;
965     _exeobj(obj,"security",inst,1,argv);
966     argv[0]=(char *)&sarray;
967     argv[1]=NULL;
968     AddNgpFileList(name);
969     sprintf(mes,"Loading `%.128s'.",name);
970     SetStatusBar(mes);
971     _exeobj(obj,"shell",inst,1,argv);
972     sec=FALSE;
973     argv[0]=(char *)&sec;
974     argv[1]=NULL;
975     _exeobj(obj,"security",inst,1,argv);
976     if ((aobj=getobject("axis"))!=NULL) {
977       for (i=0;i<=chkobjlastinst(aobj);i++) exeobj(aobj,"tight",i,0,NULL);
978     }
979     if ((aobj=getobject("axisgrid"))!=NULL) {
980       for (i=0;i<=chkobjlastinst(aobj);i++) exeobj(aobj,"tight",i,0,NULL);
981     }
982     AxisNameToGroup();
983     ResetStatusBar();
984     arraydel2(&sarray);
985     if (console) FreeConsole(allocnow);
986     GetPageSettingsFromGRA();
987     UpdateAll();
988     delobj(obj,newid);
989   }
990   if (menulocal.expandtofullpath && (!ignorepath)) ToFullPath();
991   ignorepath=FALSE;
992   putobj(sys,"ignore_path",0,&ignorepath);
993 }
994 
LoadPrmFile(char * File)995 void LoadPrmFile(char *File)
996 {
997   struct objlist *obj;
998   char *name;
999   int id;
1000   char mes[256];
1001 
1002   if ((obj=chkobject("prm"))==NULL) return;
1003   if ((id=newobj(obj))>=0) {
1004     if ((name=(char *)memalloc(strlen(File)+1))==NULL) {
1005       delobj(obj,id);
1006       return;
1007     }
1008     strcpy(name,File);
1009     SetFileName(name);
1010     changefilename(name);
1011     putobj(obj,"file",id,name);
1012     PrmDialog(&DlgPrm,obj,id);
1013     if (DialogExecute(TopLevel,&DlgPrm)==IDOK) {
1014       sprintf(mes,"Loading `%.128s'.",name);
1015       SetStatusBar(mes);
1016       DeleteDrawable();
1017       exeobj(obj,"load",id,0,NULL);
1018       GetPageSettingsFromGRA();
1019       UpdateAll();
1020       ResetStatusBar();
1021       SetCaption(File);
1022     }
1023     delobj(obj,id);
1024   }
1025 }
1026 
FileAutoScale()1027 void FileAutoScale()
1028 {
1029   int anum,room;
1030   struct objlist *aobj,*aobj2;
1031   double min,max,inc;
1032   char *argv2[3];
1033   char *buf;
1034   struct objlist *fobj;
1035   int lastinst;
1036   int i,j,a;
1037   char *ref;
1038   struct narray iarray;
1039   int anum2,aid2;
1040   char *inst,*group,*refgroup;
1041   int refother;
1042 
1043   if ((fobj=chkobject("file"))==NULL) return;
1044   lastinst=chkobjlastinst(fobj);
1045   aobj=chkobject("axis");
1046   anum=chkobjlastinst(aobj);
1047   if ((lastinst>=0) && (aobj!=NULL) && (anum!=0)) {
1048     if ((buf=(char *)memalloc(6*(lastinst+1)+6))!=NULL) {
1049       j=0;
1050       j+=sprintf(buf+j,"file:");
1051       for (i=0;i<=lastinst;i++) {
1052         getobj(fobj,"hidden",i,0,NULL,&a);
1053         if (!a) j+=sprintf(buf+j,"%d,",i);
1054       }
1055       if (buf[j]==',') buf[j]='\0';
1056       room=0;
1057       argv2[0]=(char *)buf;
1058       argv2[1]=(char *)&room;
1059       argv2[2]=NULL;
1060       for (i=0;i<=anum;i++) {
1061         getobj(aobj,"min",i,0,NULL,&min);
1062         getobj(aobj,"max",i,0,NULL,&max);
1063         getobj(aobj,"inc",i,0,NULL,&inc);
1064         getobj(aobj,"group",i,0,NULL,&group);
1065         getobj(aobj,"reference",i,0,NULL,&ref);
1066         refother=FALSE;
1067         if (ref!=NULL) {
1068           refother=TRUE;
1069           arrayinit(&iarray,sizeof(int));
1070           if (!getobjilist(ref,&aobj2,&iarray,FALSE,NULL)) {
1071             anum2=arraynum(&iarray);
1072             if (anum2>0) {
1073               aid2=*(int *)arraylast(&iarray);
1074               arraydel(&iarray);
1075               if ((anum2>0) && ((inst=getobjinst(aobj2,aid2))!=NULL)) {
1076                 _getobj(aobj2,"group",inst,&refgroup);
1077                 if ((refgroup!=NULL) && (group!=NULL)
1078                 && (refgroup[0]==group[0])
1079                 && (strcmp(refgroup+2,group+2)==0)) refother=FALSE;
1080               }
1081             }
1082           }
1083         }
1084         if ((!refother) && ((min==max) || (inc==0)))
1085           exeobj(aobj,"auto_scale",i,2,argv2);
1086       }
1087       memfree(buf);
1088     }
1089   }
1090 }
1091 
AdjustAxis()1092 void AdjustAxis()
1093 {
1094   struct objlist *aobj;
1095   int i,anum;
1096 
1097   if ((aobj=chkobject("axis"))==NULL) return;
1098   anum=chkobjlastinst(aobj);
1099   for (i=0;i<=anum;i++) exeobj(aobj,"adjust",i,0,NULL);
1100 }
1101 
CheckSave()1102 int CheckSave()
1103 {
1104   int ret;
1105 
1106   if (NgraphApp.Changed) {
1107     ret=MessageBox(TopLevel,"This graph is modified.\nSave this graph?",
1108                    "Modified",MB_YESNOCANCEL);
1109     if (ret==IDYES) {
1110       if (GraphSave(TRUE)==IDCANCEL) return FALSE;
1111     } else if (ret!=IDNO) return FALSE;
1112   }
1113   return TRUE;
1114 }
1115 
AddMathList(char * math)1116 void AddMathList(char *math)
1117 {
1118   int i,j,num;
1119   char **data;
1120   char *s;
1121   struct narray *mathlist;
1122 
1123   if ((math==NULL) || (math[0]=='\0')) return;
1124   mathlist=menulocal.mathlist;
1125   num=arraynum(mathlist);
1126   data=(char **)arraydata(mathlist);
1127   for (i=0;i<num;i++)
1128     if (strcmp0(data[i],math)==0) break;
1129   if (i==num) {
1130     if (num>=10) arrayndel2(mathlist,num-1);
1131     else num++;
1132     arrayins2(mathlist,&math,0);
1133   } else {
1134     s=data[i];
1135     for (j=i-1;j>=0;j--) data[j+1]=data[j];
1136     data[0]=s;
1137   }
1138 }
1139 
AddNgpFileList(char * file)1140 void AddNgpFileList(char *file)
1141 {
1142   int i,j,num,num2;
1143   char **data,**data2;
1144   char *s,*cwd;
1145   struct narray *ngpfilelist,*ngpdirlist;
1146   XmString xs;
1147 
1148   if ((file==NULL) || (file[0]=='\0')) return;
1149   ngpfilelist=menulocal.ngpfilelist;
1150   ngpdirlist=menulocal.ngpdirlist;
1151   num=arraynum(ngpfilelist);
1152   num2=arraynum(ngpdirlist);
1153   if (num!=num2) return;
1154   data=(char **)arraydata(ngpfilelist);
1155   data2=(char **)arraydata(ngpdirlist);
1156   for (i=0;i<num;i++)
1157     if (strcmp0(data[i],file)==0) break;
1158   if (i==num) {
1159     if (num>=10) {
1160       arrayndel2(ngpfilelist,num-1);
1161       arrayndel2(ngpdirlist,num-1);
1162     }
1163     arrayins2(ngpfilelist,&file,0);
1164     cwd=ngetcwd();
1165     arrayins(ngpdirlist,&cwd,0);
1166   } else {
1167     s=data[i];
1168     for (j=i-1;j>=0;j--) data[j+1]=data[j];
1169     data[0]=s;
1170     s=data2[i];
1171     for (j=i-1;j>=0;j--) data2[j+1]=data2[j];
1172     cwd=ngetcwd();
1173     memfree(s);
1174     data2[0]=cwd;
1175   }
1176   num=arraynum(ngpfilelist);
1177   data=(char **)arraydata(ngpfilelist);
1178   for (i=0;i<10;i++) {
1179     XtUnmanageChild(NgraphApp.ghistory[i]);
1180     if (i<num) {
1181       xs=XmStringCreate(data[i],XmFONTLIST_DEFAULT_TAG);
1182       XtVaSetValues(NgraphApp.ghistory[i],XmNlabelString,xs,NULL);
1183       XtManageChild(NgraphApp.ghistory[i]);
1184       XmStringFree(xs);
1185     }
1186   }
1187 }
1188 
AddDataFileList(char * file)1189 void AddDataFileList(char *file)
1190 {
1191   int i,j,num;
1192   char **data;
1193   char *s;
1194   struct narray *datafilelist;
1195   XmString xs;
1196 
1197   if ((file==NULL) || (file[0]=='\0')) return;
1198   datafilelist=menulocal.datafilelist;
1199   num=arraynum(datafilelist);
1200   data=(char **)arraydata(datafilelist);
1201   for (i=0;i<num;i++)
1202     if (strcmp0(data[i],file)==0) break;
1203   if (i==num) {
1204     if (num>=10) arrayndel2(datafilelist,num-1);
1205     arrayins2(datafilelist,&file,0);
1206   } else {
1207     s=data[i];
1208     for (j=i-1;j>=0;j--) data[j+1]=data[j];
1209     data[0]=s;
1210   }
1211   num=arraynum(datafilelist);
1212   data=(char **)arraydata(datafilelist);
1213   for (i=0;i<10;i++) {
1214     XtUnmanageChild(NgraphApp.fhistory[i]);
1215     if (i<num) {
1216       xs=XmStringCreate(data[i],XmFONTLIST_DEFAULT_TAG);
1217       XtVaSetValues(NgraphApp.fhistory[i],XmNlabelString,xs,NULL);
1218       XtManageChild(NgraphApp.fhistory[i]);
1219       XmStringFree(xs);
1220     }
1221   }
1222 }
1223 
SetFileName(char * name)1224 void SetFileName(char *name)
1225 {
1226   char *ngp;
1227 
1228   memfree(NgraphApp.FileName);
1229   if (name==NULL) {
1230     NgraphApp.FileName=NULL;
1231     ngp=NULL;
1232   } else {
1233     NgraphApp.FileName=getfullpath(name);
1234     ngp=getfullpath(name);
1235   }
1236   putobj(menulocal.obj,"fullpath_ngp",0,ngp);
1237 }
1238 
1239 int nallocconsole();
1240 void nfreeconsole();
1241 void nforegroundconsole();
1242 
AllocConsole()1243 int AllocConsole()
1244 {
1245   int allocnow;
1246 
1247   loadstdio(&x11iosave);
1248   allocnow=nallocconsole();
1249   nforegroundconsole();
1250   return allocnow;
1251 }
1252 
FreeConsole(int allocnow)1253 void FreeConsole(int allocnow)
1254 {
1255   if (allocnow) nfreeconsole();
1256   putstderr=mx11putstderr;
1257   printfstderr=mx11printfstderr;
1258   inputyn=mx11inputyn;
1259   ndisplaydialog=mx11displaydialog;
1260   ndisplaystatus=mx11displaystatus;
1261 }
1262 
FileCB(struct objlist * obj,int id)1263 char *FileCB(struct objlist *obj,int id)
1264 {
1265   char *valstr,*file,*s;
1266 
1267   if ((s=(char *)memalloc(128))==NULL) return NULL;
1268   getobj(obj,"file",id,0,NULL,&file);
1269   valstr=getbasename(file);
1270   if (valstr!=NULL) {
1271     sprintf(s,"%-5d %.100s",id,valstr);
1272     memfree(valstr);
1273   } else sprintf(s,"%-5d %.100s",id,"....................");
1274   return s;
1275 }
1276 
SetFileHidden()1277 int SetFileHidden()
1278 {
1279   struct objlist *fobj;
1280   int lastinst;
1281   struct narray farray,ifarray;
1282   int i,a,num,*array;
1283 
1284   if ((fobj=chkobject("file"))==NULL) return 1;
1285   lastinst=chkobjlastinst(fobj);
1286   if (lastinst>=0) {
1287     arrayinit(&ifarray,sizeof(int));
1288     for (i=0;i<=lastinst;i++) {
1289       getobj(fobj,"hidden",i,0,NULL,&a);
1290       if (!a) arrayadd(&ifarray,&i);
1291     }
1292     SelectDialog(&DlgSelect,fobj,FileCB,(struct narray *)&farray,
1293                                         (struct narray *)&ifarray);
1294     if (DialogExecute(TopLevel,&DlgSelect)==IDOK) {
1295       a=TRUE;
1296       for (i=0;i<=lastinst;i++) putobj(fobj,"hidden",i,&a);
1297       num=arraynum(&farray);
1298       array=(int *)arraydata(&farray);
1299       a=FALSE;
1300       for (i=0;i<num;i++) putobj(fobj,"hidden",array[i],&a);
1301     } else {
1302       arraydel(&farray);
1303       return 0;
1304     }
1305     arraydel(&ifarray);
1306     arraydel(&farray);
1307   }
1308   return 1;
1309 }
1310 
CheckIniFile()1311 int CheckIniFile()
1312 {
1313   int ret;
1314 
1315   ret=writecheckconfig();
1316   if (ret==0) {
1317     MessageBox(TopLevel,"Ngraph.ini is not found.",
1318                "Ngraph.ini",MB_OK);
1319     return FALSE;
1320   } else if ((ret==-1) || (ret==-3)) {
1321     MessageBox(TopLevel,"Ngraph.ini is write protected.",
1322                "Ngraph.ini",MB_OK);
1323     return FALSE;
1324   } else if ((ret==-2) || (ret==3) || (ret==2)) {
1325     if (MessageBox(TopLevel,
1326       "Install `Ngraph.ini' to your $NGRAPHHOME or $HOME directory?",
1327       "Ngraph.ini",MB_YESNO)==IDYES) {
1328       if (!copyconfig()) {
1329         MessageBox(TopLevel,"Ngraph.ini could not be copied.",
1330                    "Ngraph.ini",MB_OK);
1331         return FALSE;
1332       }
1333     } else {
1334       return FALSE;
1335     }
1336   }
1337   return TRUE;
1338 }
1339 
SaveHistory()1340 void SaveHistory()
1341 {
1342   struct narray conf;
1343   char *buf;
1344   int i,num,num2;
1345   char **data;
1346 
1347   if (!menulocal.savehistory) return;
1348   if (!CheckIniFile()) return;
1349   arrayinit(&conf,sizeof(char *));
1350   num=arraynum(menulocal.ngpfilelist);
1351   data=(char **)arraydata(menulocal.ngpfilelist);
1352   for (i=0;i<num;i++) {
1353     if ((data[i]!=NULL)
1354     && ((buf=(char *)memalloc(13+strlen(data[i])))!=NULL)) {
1355       sprintf(buf,"ngp_history=%s",data[i]);
1356       arrayadd(&conf,&buf);
1357     }
1358   }
1359   num2=arraynum(menulocal.ngpdirlist);
1360   data=(char **)arraydata(menulocal.ngpdirlist);
1361   for (i=0;i<num;i++) {
1362     if ((i>=num2) || (data[i]==NULL)) {
1363       if ((buf=(char *)memalloc(17))!=NULL) {
1364         sprintf(buf,"ngp_dir_history=");
1365         arrayadd(&conf,&buf);
1366       }
1367     } else if ((buf=(char *)memalloc(17+strlen(data[i])))!=NULL) {
1368       sprintf(buf,"ngp_dir_history=%s",data[i]);
1369       arrayadd(&conf,&buf);
1370     }
1371   }
1372   num=arraynum(menulocal.datafilelist);
1373   data=(char **)arraydata(menulocal.datafilelist);
1374   for (i=0;i<num;i++) {
1375     if ((data[i]!=NULL)
1376     && ((buf=(char *)memalloc(14+strlen(data[i])))!=NULL)) {
1377       sprintf(buf,"data_history=%s",data[i]);
1378       arrayadd(&conf,&buf);
1379     }
1380   }
1381   replaceconfig("[x11menu]",&conf);
1382   arraydel2(&conf);
1383   arrayinit(&conf,sizeof(char *));
1384   if (arraynum(menulocal.ngpfilelist)==0) {
1385     if ((buf=(char *)memalloc(20))!=NULL) {
1386       sprintf(buf,"ngp_history");
1387       arrayadd(&conf,&buf);
1388     }
1389   }
1390   if (arraynum(menulocal.datafilelist)==0) {
1391     if ((buf=(char *)memalloc(20))!=NULL) {
1392       sprintf(buf,"data_history");
1393       arrayadd(&conf,&buf);
1394     }
1395   }
1396   removeconfig("[x11menu]",&conf);
1397   arraydel2(&conf);
1398 }
1399 
ExtTextOut(Display * disp,Window win,GC gc,int x,int y,XRectangle * rect,char * buf,int len)1400 void ExtTextOut(Display *disp,Window win,GC gc,
1401                 int x,int y,XRectangle *rect,char *buf,int len)
1402 {
1403   Region region;
1404   XRectangle rect2;
1405 
1406   region=XCreateRegion();
1407   XUnionRectWithRegion(rect,region,region);
1408   XSetRegion(disp,gc,region);
1409   XmbDrawString(disp,win,FFont,gc,x,y,buf,len);
1410   XDestroyRegion(region);
1411   rect2.x=0;
1412   rect2.y=0;
1413   rect2.width=SHRT_MAX;
1414   rect2.height=SHRT_MAX;
1415   region=XCreateRegion();
1416   XUnionRectWithRegion(&rect2,region,region);
1417   XSetRegion(disp,gc,region);
1418   XDestroyRegion(region);
1419 }
1420