1 /*
2  *  win.c
3  *
4  *  Copyright (C) 1997, 2000, 2001, 2006, 2007  Staf Wagemakers Belgie/Belgium
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License along
17  *  with this program; if not, write to the Free Software Foundation, Inc.,
18  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21 
22 #include "win.h"
wexit(int i)23 void wexit(int i) {
24 	endwin();
25 	exit(i);
26 }
27 /* ----------------------------------------- */
28 /* Print een kar. als een string af, dit     */
29 /* geeft minder problemen met speciale kar.  */
30 /* ----------------------------------------- */
waddch_fix(WINDOW * w,char c)31 void waddch_fix (WINDOW *w,char c)
32 {
33 char s[2]={'\0','\0'};
34 if (isbin(c)) c='.';
35 s[0]=c;
36 waddstr(w,s);
37 }
38 /* ------------------------------------------- */
39 /* Tekenen van horizontale lijn		       */
40 /* ------------------------------------------- */
win_hline(WINDOW * w,int h)41 void win_hline(WINDOW *w,int h)
42 {
43 int i;
44 for (i=0;i<h;i++) waddch(w,ACS_HLINE);
45 }
46 /* ------------------------------------------- */
47 /* Tekenen van een vertikale lijn	       */
48 /* ------------------------------------------- */
win_vline(WINDOW * w,int v)49 void win_vline(WINDOW *w,int v)
50 {
51 int i;
52 for (i=0;i<v;i++) waddch(w,ACS_VLINE);
53 }
54 /* ----------------------------------------- */
55 /* Tekenen van een kader in een venster	...  */
56 /*					     */
57 /* w     : venster			     */
58 /* y,x   : horizontale & vertikale grootte   */
59 /* yp,xp : horizontale & vertikale positie   */
60 /* ----------------------------------------- */
win_box(WINDOW * w,int y,int x,int yp,int xp)61 void win_box(WINDOW *w,int y,int x,int yp,int xp)
62 {
63 mvwaddch (w,yp,xp,ACS_ULCORNER);
64 win_hline(w,x-1);
65 mvwaddch(w,yp,xp+x,ACS_URCORNER);
66 wmove(w,yp+1,xp+x);
67 wvline(w,ACS_VLINE,y-1);
68 wmove(w,yp+1,xp);
69 wvline(w,ACS_VLINE,y-1);
70 mvwaddch(w,yp+y,xp,ACS_LLCORNER);
71 whline(w,ACS_HLINE,x-1);
72 mvwaddch(w,yp+y,xp+x,ACS_LRCORNER);
73 }
74 /* -------------------------------------------- */
75 /* Procedure voor het openen van een venster +	*/
76 /* fout afhandeling				*/
77 /* -------------------------------------------- */
open_win(int yy,int xx,int y,int x)78 WINDOW * open_win(int yy,int xx,int y,int x)
79 {
80 WINDOW *w;
81 if (!(w=newwin(yy,xx,y,x))) {
82    endwin();
83    fprintf(stderr,"Failed to create ncurses windows");
84    exit(1);
85    }
86 keypad(w,TRUE);
87 meta(w,TRUE);
88 return(w);
89 }
90 /* -------------------------------------------- */
91 /* Procedure voor het openen van een gecenteerd */
92 /* venster					*/
93 /* int yy,xx  : lengte, breedte			*/
94 /*						*/
95 /* Geeft een pointer naar venster terug		*/
96 /* -------------------------------------------- */
open_cwin(int yy,int xx)97 WINDOW * open_cwin (int yy,int xx)
98 {
99 WINDOW *w;
100 w=open_win(yy,xx,LINES/2-yy/2,COLS/2-xx/2);
101 return(w);
102 }
103 /* -------------------------------------------- */
104 /* Procedure voor het openen van een		*/
105 /* "ok"-venster					*/
106 /* int yy,xx  : lengte, breedte			*/
107 /* char **txt : pointer naar array van strings  */
108 /*		NULL = einde array		*/
109 /* chtype   c : normale kleur			*/
110 /* -------------------------------------------- */
open_okwin(int yy,int xx,MENU * m,char ** txt,WINDOW * win1)111 void open_okwin (int yy,int xx,MENU *m,char **txt,WINDOW *win1)
112 {
113 WINDOW *w;
114 int i;
115 unsigned pl_ok[2];
116 int key_m[]={'\n','\n','\n','\n','\n','\n'};
117 w=open_cwin(yy,xx);
118 leaveok(w,TRUE);
119 curs_set(0);
120 pl_ok[0]=yy-2;
121 pl_ok[1]=xx/2-strlen(m->txt[0])/2;
122 m->w=w;
123 m->amount=1;
124 m->l=0;
125 m->place=pl_ok;
126 m->sel=m->used=0;
127 m->key=key_m;
128 wbkgdset(w,m->color1);
129 werase(w);
130 i=0;
131 while (txt[i]!=NULL) mvwaddstr(w,2+i,3,txt[i++]);
132 box(w,0,0);
133 do i=menu_key(m); while(!i);
134 wrefresh(w);
135 delwin(w);
136 if (win1!=NULL) touchwin(win1);
137 }
138 /* -------------------------------------------- */
139 /* Procedure het open van een "ja/nee"-venster  */
140 /* int yy,xx  : lengte, breedte			*/
141 /* char **txt : pointer naar array van strings  */
142 /*		NULL = einde array		*/
143 /* chtype   c : normale kleur			*/
144 /*						*/
145 /* Geeft terug 0 : -> nee / ESC			*/
146 /*	       1 : -> ja			*/
147 /* -------------------------------------------- */
open_ynwin(int yy,int xx,MENU * m,char ** txt,WINDOW * win1)148 int open_ynwin (int yy,int xx,MENU *m,char **txt,WINDOW *win1)
149 {
150 #define TAB 9
151 WINDOW *w;
152 int s,i;
153 unsigned pl_jn[4];
154 int key_m[]={KEY_LEFT,KEY_RIGHT,'\n',TAB,'\n',27};
155 w=open_cwin(yy,xx);
156 leaveok(w,TRUE);
157 curs_set(0);
158 keypad(w,TRUE);
159 nodelay(w,TRUE);
160 pl_jn[0]=pl_jn[2]=yy-2;
161 pl_jn[1]=xx/2-7-strlen(m->txt[0])/2;
162 pl_jn[3]=xx/2+7-strlen(m->txt[1])/2;
163 m->w=w;
164 m->amount=2;
165 m->l=0;
166 m->place=pl_jn;
167 m->sel=m->used=0;
168 m->key=key_m;
169 wbkgdset(w,m->color1);
170 werase(w);
171 i=0;
172 while (txt[i]!=NULL) mvwaddstr(w,2+i,3,txt[i++]);
173 box(w,0,0);
174 menu_print(m);
175 s=0;
176 do {
177  menu_sel(m,s);
178  do i=menu_key(m); while(i<3);
179  if (++s>1) s=0;
180  } while (i==4);
181 wrefresh(w);
182 delwin(w);
183 if (win1!=NULL) touchwin(win1);
184 if (i==6)  return(0);
185 if (m->sel) return(0);
186 return(1);
187 }
188 /* -------------------------------------------- */
189 /* Procedure voor het openen van een		*/
190 /* anim-venster					*/
191 /* int yy,xx  : lengte, breedte			*/
192 /* char **txt : pointer naar array van strings  */
193 /*		NULL = einde array		*/
194 /* chtype   c : normale kleur			*/
195 /* char   mode: 0 -> stop                       */
196 /*              1 -> don't stop                 */
197 /* -------------------------------------------- */
open_animwin(int yy,int xx,MENU * m,char ** txt,char * animtxt,WINDOW * win1,char mode)198 void open_animwin (int yy,int xx,MENU *m,char **txt,char *animtxt,WINDOW *win1,char mode)
199 {
200 WINDOW *w;
201 int i;
202 unsigned pl_ok[2];
203 int key_m[]={'\n','\n','\n','\n','\n','\n'};
204 int xp,yp,t;
205 fd_set set;
206 struct timeval timeout;
207 w=open_cwin(yy,xx);
208 leaveok(w,TRUE);
209 curs_set(0);
210 nodelay(w,TRUE);
211 pl_ok[0]=yy-2;
212 pl_ok[1]=xx/2-strlen(m->txt[0])/2;
213 m->w=w;
214 m->amount=1;
215 m->l=0;
216 m->place=pl_ok;
217 m->sel=m->used=0;
218 m->key=key_m;
219 wbkgdset(w,m->color1);
220 werase(w);
221 i=0;
222 while (txt[i]!=NULL) mvwaddstr(w,2+i,3,txt[i++]);
223 getyx(w,yp,xp);
224 box(w,0,0);
225 do {
226    i=menu_key(m);
227    wbkgdset(w,m->color1);
228    for (t=0;t<strlen(animtxt);t++) {
229        for (xp=getmaxx(w)-4;xp>(2+t);xp--) {
230 	   mvwaddch(w,yp,xp,animtxt[t]);
231            mvwaddch(w,yp,xp+1,' ');
232            wrefresh(w);
233 	 {
234 	/* Initialize the file descriptor set. */
235 	FD_ZERO (&set);
236 	FD_SET (STDIN_FILENO,&set);
237 	/* Initialize the timeout data structure */
238 	timeout.tv_sec=0;
239 	timeout.tv_usec=2;
240 	select (FD_SETSIZE,&set,NULL,NULL,&timeout);
241 	 }
242        }
243        }
244        timeout.tv_sec=3;
245        select (FD_SETSIZE,&set,NULL,NULL,&timeout);
246     if (!mode) break;
247     for (xp=0;xp<strlen(animtxt);xp++) mvwaddch(w,yp,xp+3,' ');
248     wrefresh(w);
249     i=menu_key(m);
250 } while(!i);
251 nodelay(w,FALSE);
252 if(!mode) {do {i=menu_key(m);} while(!i);}
253 
254 wrefresh(w);
255 delwin(w);
256 if (win1!=NULL) touchwin(win1);
257 }
258 /* ------------------------------------------- */
259 /* Procedure voor het openen van menu venster  */
260 /* ------------------------------------------- */
m_open(MENU * m,WINDOW * win1)261 void m_open(MENU *m,WINDOW *win1)
262 {
263 touchwin(win1);
264 leaveok(m->w,TRUE);
265 curs_set(0);
266 keypad(m->w,TRUE);
267 nodelay(m->w,FALSE);
268 wbkgdset (m->w,m->color1);
269 werase(m->w);
270 box(m->w,0,0);
271 wrefresh(m->w);
272 }
print_item(MENU * m)273 void print_item(MENU *m)
274 {
275 INPUT_STRING str;
276 str.w=m->w;
277 str.xp=22;
278 str.yp=3;
279 str.c=m->txt[m->sel];
280 str.count=0;
281 str.ox=0;
282 str.n=45;
283 str.m=0;
284 str.mode=0;
285 wbkgdset(m->w,m->color3);
286 input_show_string(&str);
287 }
288 /* ----------------------------------------- */
289 /* Inlezen van de bestandsnaam ...	     */
290 /* *win2 : info venster indien NULL geen info */
291 /* *txt_info_open : info tekst                */
292 /* **txt      [0] : tekst in info venster    */
293 /*            [1] : geen toegang             */
294 /*            [2] : kan bestand nt openen    */
295 /*            [3] : huidige dir              */
296 /*            [4] : selektie		     */
297 /*            [5] : titel venster            */
298 /*            [6] : bestanden                */
299 /*            [7] : dirs                     */
300 /*            [8] : ok			     */
301 /* *kleur     [0] : win kleur                */
302 /*            [1] : menu color		     */
303 /*            [2] : selektie kleur           */
304 /*            [3] : balkkleur                */
305 /* *win1          : te verversen window      */
306 /*                                           */
307 /* Geeft terug :                             */
308 /* bestandsnaam                              */
309 /* NULL -> fout                              */
310 /* ----------------------------------------- */
open_filewin(WINDOW * win2,char ** txt,chtype * kleur,WINDOW * win1,int (* ends)())311 char * open_filewin(WINDOW *win2,char **txt,chtype *kleur,WINDOW *win1,int (*ends)())
312 {
313 MENU dm,bm,fm;			/* menus 		*/
314 WINDOW *w;			/* my windows 		*/
315 INPUT_STRING str;		/* file name  		*/
316 int 	 toetsen[]={27,9,0};
317 unsigned bu=0,du=0;
318 bool   	 form_input=FALSE;
319 char     *bestandsnaam,*txt_path,*txt_wd,*vb;
320 char     txt_slash[]={'/',0};
321 unsigned pl_bm[]={6,3,7,3,8,3,9,3,10,3,11,3,12,3,13,3,14,3,15,3,16,3,17,3,18,3};
322 unsigned pl_dm[]={6,36,7,36,8,36,9,36,10,36,11,36,12,36,13,36,14,36,15,36,16,36,17,36,18,36,19,36};
323 struct 	 dir_data *data;
324 unsigned brol;
325 int 	 i,sub;
326 int 	 key_m[]={KEY_UP,KEY_DOWN,'\n',KEY_PPAGE,KEY_NPAGE,27,9,'q'};
327 struct 	 stat stat_buf;
328 char 	 *s;
329 char 	 *txt_f[]={txt[2],NULL};
330 char 	 txt_ret[]=" ";
331 char 	 *txt_f_ok[]={txt[8],txt_ret,txt_ret,NULL};
332 
333 bestandsnaam=txt_path=txt_wd=vb=NULL;
334 
335 fm.txt=txt_f_ok;
336 fm.color1=kleur[1];
337 fm.color2=kleur[3];
338 fm.hkey=NULL;
339 fm.hplace=NULL;
340 str.yp=3;
341 str.xp=22;
342 str.c=NULL;
343 str.key=toetsen;
344 str.n=45;
345 str.m=0;
346 str.mode=0;
347 str.insert=TRUE;
348 str.ox=str.count=0;
349 
350 if (win2!=NULL) {
351 	werase(win2);
352         mvwaddstr(win2,0,0,txt[0]);
353         wrefresh(win2);
354 }
355 
356 vb=bestandsnaam;
357 txt_path=NULL;
358 txt_path=NULL;
359 txt_wd=NULL;
360 
361 /*
362  *  menu init
363  */
364 
365 bm.color1=kleur[2];
366 bm.color2=kleur[3];
367 bm.place=pl_bm;
368 bm.key=key_m;
369 bm.l=dm.l=30;
370 dm.color1=kleur[2];
371 dm.color2=kleur[3];
372 bm.color3=dm.color3=kleur[1];
373 dm.place=pl_dm;
374 dm.key=key_m;
375 bm.hkey=NULL;
376 bm.hplace=NULL;
377 dm.hkey=NULL;
378 dm.hplace=NULL;
379 
380 brol=0;
381 w=open_cwin(21,70);
382 str.w=w;
383 leaveok(w,TRUE);
384 curs_set(0);
385 keypad(w,TRUE);
386 nodelay(w,FALSE);
387 wbkgdset(w,kleur[0]);
388 sub=0;
389 
390 do {
391 
392   int fout;
393   if (txt_path!=NULL) {
394        wrefresh(win2);
395        chdir(txt_path);
396        xfree(txt_path);
397        txt_path=NULL;
398      }
399 
400   txt_path=(char *) xmalloc(2);
401   strcpy(txt_path,".");
402 
403   do {
404 
405    int size=100;
406    txt_wd=(char *) xrealloc(txt_wd,size);
407    fout=0;
408 
409    if (getcwd(txt_wd,size)==NULL) {
410       fout=1;
411 
412       if ((errno=EACCES)) {
413          txt_wd=(char *) xrealloc(txt_wd,strlen(txt[1])+1);
414          strcpy(txt_wd,txt[1]);
415          ++fout;
416       };
417 
418       break;
419    };
420 
421    size*=2;
422 
423   } while (fout);
424 
425   bu=du=bm.sel=dm.sel=0;
426   txt_path=xrealloc(txt_path,strlen(txt_path)+strlen(txt_slash)+1);
427   strcat(txt_path,txt_slash);
428 
429   if (fout==2) data=NULL;
430     else data=get_dir(txt_path);
431 
432   if (data==NULL) {
433      data=(struct dir_data*) xmalloc(sizeof(struct dir_data));
434      data->files=0;
435      data->dirs=0;
436      data->file=NULL;
437      data->dir=(char **) xmalloc(sizeof(char **));
438      data->dir[0]=(char *) xmalloc(strlen("/")+1);
439      strcpy(data->dir[0],"/");
440      txt_wd=(char *) xrealloc(txt_wd,strlen(txt[1])+1);
441      strcpy(txt_wd,txt[1]);
442      };
443 
444   wbkgdset(w,kleur[0]);
445   werase(w);
446   wattrset(w,kleur[0]);
447   win_box(w,14,32,5,2);
448   win_box(w,14,32,5,35);
449   box(w,0,0);
450   wattroff(w,kleur[0]);
451   mvwaddstr(w,2,1,txt[3]);
452   mvwaddstr(w,3,1,txt[4]);
453   wbkgdset(w,kleur[3]);
454   wattron(w,kleur[3]);
455   wmove(w,3,22);
456   whline(w,' ',45);
457   wattroff(w,kleur[3]);
458   wbkgdset(w,kleur[0]);
459   mvwaddnstr(w,2,23,txt_wd,44);
460   mvwaddstr(w,0,2,txt[5]);
461   mvwaddstr(w,5,4,txt[6]);
462   mvwaddstr(w,5,37,txt[7]);
463   wrefresh(w);
464 
465   if (data->files<13) bm.amount=data->files;
466     else bm.amount=13;
467   if (data->dirs<13)  dm.amount=data->dirs;
468     else dm.amount=13;
469 
470   bm.txt=data->file;
471   dm.txt=data->dir;
472   bm.w=dm.w=w;
473 
474   do {
475     form_input=FALSE;
476     if (sub>2) sub=0;
477     switch (sub) {
478 
479        case 0: if (bm.amount) {
480                   menu_print(&dm);
481     	          i=scroll_menu(&bm,data->files,print_item,&bu);
482                }
483 	       else i=2;
484 	       break;
485 
486         case 1: if (dm.amount) {
487                    menu_print(&bm);
488                 };
489 	        i=scroll_menu(&dm,data->dirs,NULL,&du);
490 	        break;
491 
492         case 2: menu_print(&dm);
493                 xfree(str.c);
494 
495                 if (data->files) {
496 	             str.c=(char *) xmalloc(strlen(data->file[bm.sel])+1);
497                      strcpy(str.c,data->file[bm.sel]);
498 	        }
499 	        else str.c=NULL;
500 
501 		wbkgdset(w,kleur[1]);
502             	str.ox=str.count=0;
503 
504 	    	switch (input(&str)) {
505 
506 			case 0: form_input=TRUE;
507                     		if (stat(str.c,&stat_buf)==-1) {
508 		    		   int l=0;
509 		    		   i=0;
510 
511 		    		   while(txt_f[i]!=NULL) {
512                          	      if (strlen(txt_f[i])>l)
513 					     l=strlen(txt_f[i]);
514 			 		  i++;
515 			 	   };
516 
517 		    		   open_okwin(i+5,l+5,&fm,txt_f,w);
518 		    		   i=2;
519 		    		   sub=1;
520 		                 }
521 		    	         else {
522                       		    if (S_ISDIR(stat_buf.st_mode)) sub=1;
523 			 	       else sub=0;
524 	 	      		    i=0;
525 		                 }
526 		    	         break;
527 	    		 case 1: i=1;
528 	            		 break;
529 	    		 case 2: break;
530 	     };
531 	    leaveok(w,TRUE);
532 	    curs_set(0);
533 	    break;
534     };
535 
536     if ((i==1)||(i==3)) {
537 	if(ends==NULL) break;
538 	if(ends(w)) break;
539     }
540 
541     if (i==2) ++sub;
542 
543   } while (i);
544 
545   if (i) break;
546 
547   if (sub) {
548     if (form_input) s=str.c;
549        else s=data->dir[dm.sel];
550     txt_path=(char *) xrealloc(txt_path,strlen(s)+1);
551     strcpy(txt_path,s);
552     get_dir_free(data);
553     data=NULL;
554     };
555 } while(sub);
556 
557 if (!i) {
558   if (form_input) s=str.c;
559      else s=data->file[bm.sel];
560   bestandsnaam=(char *) xmalloc(strlen(s)+1);
561   strcpy(bestandsnaam,s);
562 };
563 
564 if (txt_path!=NULL) xfree(txt_path);
565 
566 xfree(str.c);
567 
568 if (data!=NULL) get_dir_free(data);
569 
570 xfree(txt_wd);
571 delwin(w);
572 
573 if (win1!=NULL) wrefresh(win1);
574 
575 if (i) return (NULL);
576 
577 return (bestandsnaam);
578 }
579 /* ----------------------------------------- */
580 /* Openen van een input venster              */
581 /*					     */
582 /* INPUT_STRING *s = pointer naar string     */
583 /* char *titel = pointer naar venster-titel  */
584 /* char *tekst = pointer naar info-tekst     */
585 /* int    mode = input-mode                  */
586 /*                   0 = string              */
587 /*                   1 = dec.                */
588 /*                   2 = hex                 */
589 /* Geeft terug 1 -> ok                       */
590 /*             0 -> nok                      */
591 /* ----------------------------------------- */
open_inputwin(int yy,int xx,MENU * m,chtype kleur,INPUT_STRING * read_str,char ** txt,char mode,WINDOW * win1)592 int open_inputwin (int yy,int xx,MENU *m,chtype kleur,INPUT_STRING *read_str,char **txt,char mode,WINDOW *win1)
593 {
594 WINDOW *w;
595 int brol;
596 int toetsen[]={27,9,0};
597 unsigned pl_ok[]={5,15,5,35};
598 int key_m[]={0,TAB,' ','\n','\n','\n'};
599 w=open_cwin(7,60);
600 read_str->w=w;
601 read_str->key=toetsen;
602 read_str->c=NULL;
603 read_str->yp=2;
604 read_str->xp=21;
605 read_str->ox=0;
606 read_str->insert=1;
607 read_str->mode=mode;
608 read_str->n=32;
609 read_str->m=0;
610 read_str->count=0;
611 m->w=w;
612 m->amount=2;
613 m->l=0;
614 m->place=pl_ok;
615 m->sel=m->used=0;
616 m->key=key_m;
617 wbkgdset(w,m->color1);
618 werase(w);
619 box(w,0,0);
620 wbkgdset(w,m->color1);
621 echo();
622 leaveok(w,FALSE);
623 curs_set(0);
624 mvwaddstr(w,0,2,txt[0]);
625 mvwaddstr(w,2,2,txt[1]);
626 wmove (w,2,21);
627 wbkgdset(w,kleur);
628 for (brol=0;brol<=32;brol++) waddch(w,' ');
629 do {
630   menu_print(m);
631   m->sel=0;
632   wbkgdset(w,kleur);
633   brol=input(read_str);
634   leaveok(w,TRUE);
635   curs_set(0);
636   if (!brol)   return(0);
637   if (brol!=2) return(1);
638   do {
639     brol=menu_key(m);
640     if ((brol==2)&&(m->sel==0)) break;
641    } while(brol<3);
642 } while(brol<3);
643 delwin(w);
644 if (win1!=NULL) wrefresh(win1);
645 if (!m->sel) return(0);
646 return (1);
647 }
648 
649 
650