uifcc.c 55.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* uifcc.c */

/* Curses implementation of UIFC (user interface) library based on uifc.c */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2002 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

36
37
#include <stdio.h>
#include <curs_fix.h>
38
39
#include <unistd.h>
#include <sys/time.h>
40
#include "uifc.h"
deuce's avatar
deuce committed
41
42
43
#ifdef __QNX__
#include <strings.h>
#endif
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#if defined(__OS2__)

#define INCL_BASE
#include <os2.h>

void mswait(int msec)
{
DosSleep(msec ? msec : 1);
}

#elif defined(_WIN32)
	#include <windows.h>
	#define mswait(x) Sleep(x)
#elif defined(__FLAT__)
    #define mswait(x) delay(x)
#endif

							/* Bottom line elements */
#define BL_INS      (1<<0)  /* INS key */
#define BL_DEL      (1<<1)  /* DEL key */
#define BL_GET      (1<<2)  /* Get key */
#define BL_PUT      (1<<3)  /* Put key */

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
enum {
	 BLACK
	,BLUE	
	,GREEN	
	,CYAN	
	,RED		
	,MAGENTA	
	,BROWN	
	,LIGHTGRAY	
	,DARKGRAY	
	,LIGHTBLUE	
	,LIGHTGREEN	
	,LIGHTCYAN	
	,LIGHTRED	
	,LIGHTMAGENTA
	,YELLOW
	,WHITE
};

87
88
89
#define BLINK	128
#define SH_DENYWR	1
#define SH_DENYRW	2
deuce's avatar
deuce committed
90
#ifndef O_BINARY
91
#define O_BINARY	0
deuce's avatar
deuce committed
92
#endif
93

94
static char hfclr,hbclr,hclr,lclr,bclr,cclr,show_free_mem=0;
95
96
97
98
99
100
101
static char* helpfile=0;
static uint helpline=0;
static char blk_scrn[MAX_BFLN];
static win_t sav[MAX_BUFS];
static uint max_opts=MAX_OPTS;
static uifcapi_t* api;
static int lastattr=0;
102
static int scrn_width;
103
104
105
106
107
108
109
110
111
112
113

/* Prototypes */
static int  uprintf(int x, int y, unsigned char attr, char *fmt,...);
static void bottomline(int line);
static char *utimestr(time_t *intime);
static void help();
static int puttext(int sx, int sy, int ex, int ey, unsigned char *fill);
static int gettext(int sx, int sy, int ex, int ey, unsigned char *fill);
static short curses_color(short color);
static void textattr(unsigned char attr);
static int kbhit(void);
deuce's avatar
deuce committed
114
#ifndef __QNX__
115
static void delay(int msec);
deuce's avatar
deuce committed
116
#endif
117
118
119
120
121
122
static int ugetstr(char *outstr, int max, long mode);
static int wherey(void);
static int wherex(void);
static int cprintf(char *fmat, ...);
static void cputs(char *str);
static void gotoxy(int x, int y);
123
static void _putch(unsigned char ch, BOOL refresh);
124
static void timedisplay(void);
125
#define putch(x)	_putch(x,TRUE)
126
127
128
129
130
131
132
133
134
135
136

/* API routines */
static void uifcbail(void);
static int uscrn(char *str);
static int ulist(int mode, int left, int top, int width, int *dflt, int *bar
	,char *title, char **option);
static int uinput(int imode, int left, int top, char *prompt, char *str
	,int len ,int kmode);
static void umsg(char *str);
static void upop(char *str);
static void sethelp(int line, char* file);
137
static void showbuf(char *buf, char *title, BOOL markup);
138

139
/* Dynamic menu support */
140
141
static int *last_menu_cur=NULL;
static int *last_menu_bar=NULL;
142
143
static int save_menu_cur=-1;
static int save_menu_bar=-1;
144

145
void reset_dynamic(void) {
deuce's avatar
deuce committed
146
147
	last_menu_cur=NULL;
	last_menu_bar=NULL;
148
149
150
151
	save_menu_cur=-1;
	save_menu_bar=-1;
}

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
int inkey(int mode)
{
	if(mode)
		return(kbhit());
	return(getch());
}

/****************************************************************************/
/* Initialization function, see uifc.h for details.							*/
/* Returns 0 on success.													*/
/****************************************************************************/
int uifcinic(uifcapi_t* uifcapi)
{
	int 	i;
	int		height, width;
	short	fg, bg, pair=0;
#ifndef __FLAT__
	union	REGS r;
#endif

    if(uifcapi==NULL || uifcapi->size!=sizeof(uifcapi_t))
        return(-1);

    api=uifcapi;

    /* install function handlers */            
    api->bail=uifcbail;
    api->scrn=uscrn;
    api->msg=umsg;
    api->pop=upop;
    api->list=ulist;
    api->input=uinput;
    api->sethelp=sethelp;
185
186
#ifdef __unix__
    api->showhelp=help;
187
	api->showbuf=showbuf;
188
	api->timedisplay=timedisplay;
189
#endif
190

191
192
193
194
#if defined(LOCALE)
  (void) setlocale(LC_ALL, "");
#endif

195
196
197
198
199
200
201
202
	initscr();
	start_color();
	cbreak();
	noecho();
	nonl();
//	intrflush(stdscr, FALSE);
	keypad(stdscr, TRUE);
	scrollok(stdscr,FALSE);
rswindell's avatar
rswindell committed
203
	raw();
204
	#ifdef NCURSES_VERSION_MAJOR
205
	ESCDELAY=api->esc_delay;
206
	#endif
207
208
209
210
211
212
213

	// Set up color pairs
	for(bg=0;bg<8;bg++)  {
		for(fg=0;fg<8;fg++) {
			init_pair(++pair,curses_color(fg),curses_color(bg));
		}
	}
214

215
    clear();
216
	refresh();
217
218
219
220
221
222
223
224
225
	getmaxyx(stdscr,height,width);
    /* unsupported mode? */
    if(height<MIN_LINES
        || height>MAX_LINES
        || width<80) {
	}
	
    api->scrn_len=height;
    if(api->scrn_len<MIN_LINES || api->scrn_len>MAX_LINES) {
226
		uifcbail();
rswindell's avatar
rswindell committed
227
        fprintf(stderr,"\7UIFC: Screen length (%u) must be between %d and %d lines\n"
228
229
230
            ,api->scrn_len,MIN_LINES,MAX_LINES);
        return(-2);
    }
231
    if(width*height*2>MAX_BFLN) {
232
		uifcbail();
rswindell's avatar
rswindell committed
233
        fprintf(stderr,"\7UIFC: Screen size (%u x %u) must be smaller than %u\n"
234
            ,width,height,(uchar)(MAX_BFLN/2));
235
236
        return(-2);
    }
237
    api->scrn_len--; /* account for status line */
238
239
	scrn_width=width;
	
240
241

    if(width<80) {
242
		uifcbail();
rswindell's avatar
rswindell committed
243
        fprintf(stderr,"\7UIFC: Screen width (%u) must be at least 80 characters\n"
244
245
246
247
248
249
250
251
252
253
254
255
            ,width);
        return(-3);
    }


/* ToDo - Set up mousemask() here */

    if(!has_colors()) {
        bclr=BLACK;
        hclr=WHITE;
        lclr=LIGHTGRAY;
        cclr=LIGHTGRAY;
256
257
		hbclr=BLACK;		/* Highlight Background Colour */
		hfclr=WHITE;		/* Highlight Foreground Colour */
258
259
260
261
262
    } else {
        bclr=BLUE;
        hclr=YELLOW;
        lclr=WHITE;
        cclr=CYAN;
263
264
		hbclr=LIGHTGRAY;
		hfclr=YELLOW;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
    }
    for(i=0;i<MAX_BFLN;i+=2) {
        blk_scrn[i]='';
        blk_scrn[i+1]=cclr|(bclr<<4);
    }

	curs_set(0);
	refresh();

    return(0);
}

short curses_color(short color)
{
	switch(color)
	{
		case 0 :
			return(COLOR_BLACK);
		case 1 :
			return(COLOR_BLUE);
		case 2 :
			return(COLOR_GREEN);
		case 3 :
			return(COLOR_CYAN);
		case 4 :
			return(COLOR_RED);
		case 5 :
			return(COLOR_MAGENTA);
		case 6 :
			return(COLOR_YELLOW);
		case 7 :
			return(COLOR_WHITE);
		case 8 :
			return(COLOR_BLACK);
		case 9 :
			return(COLOR_BLUE);
		case 10 :
			return(COLOR_GREEN);
		case 11 :
			return(COLOR_CYAN);
		case 12 :
			return(COLOR_RED);
		case 13 :
			return(COLOR_MAGENTA);
		case 14 :
			return(COLOR_YELLOW);
		case 15 :
			return(COLOR_WHITE);
	}
	return(0);
}

static void hidemouse(void)
{
/* ToDo - Mouse stuff here */
}

static void showmouse(void)
{
/* ToDo - Mouse stuff here */
}


void uifcbail(void)
{
330
331
332
333
	curs_set(1);
	clear();
	nl();
	nocbreak();
rswindell's avatar
rswindell committed
334
	noraw();
335
336
	refresh();
	endwin();
337
338
339
340
341
342
343
344
345
346
347
348
349
}

/****************************************************************************/
/* Clear screen, fill with background attribute, display application title.	*/
/* Returns 0 on success.													*/
/****************************************************************************/
int uscrn(char *str)
{
    textattr(bclr|(cclr<<4));
    gotoxy(1,1);
    clrtoeol();
    gotoxy(3,1);
	cputs(str);
350
    if(!puttext(1,2,scrn_width,api->scrn_len,blk_scrn))
351
352
353
354
        return(-1);
    gotoxy(1,api->scrn_len+1);
    clrtoeol();
	refresh();
355
	reset_dynamic();
356
357
358
359
360
361
362
363
364
    return(0);
}

/****************************************************************************/
/****************************************************************************/
static void scroll_text(int x1, int y1, int x2, int y2, int down)
{
	uchar buf[MAX_BFLN];

365
366
367
368
369
	gettext(x1,y1,x2,y2,buf);
	if(down)
		puttext(x1,y1+1,x2,y2,buf);
	else
		puttext(x1,y1,x2,y2-1,buf+(((x2-x1)+1)*2));
370
371
372
373
374
375
376
377
378
379
380
}

/****************************************************************************/
/* Updates time in upper left corner of screen with current time in ASCII/  */
/* Unix format																*/
/****************************************************************************/
static void timedisplay()
{
	static time_t savetime;
	time_t now;

381
382
383
384
385
	now=time(NULL);
	if(difftime(now,savetime)>=60) {
		uprintf(55,1,bclr|(cclr<<4),utimestr(&now));
		savetime=now; 
	}
386
387
388
389
390
391
392
393
394
395
396
}

/****************************************************************************/
/* Truncates white-space chars off end of 'str'								*/
/****************************************************************************/
static void truncsp(char *str)
{
	uint c;

	c=strlen(str);
	while(c && (uchar)str[c-1]<=SP) c--;
397
398
	if(str[c]!=0)	/* don't write to string constants */
		str[c]=0;
399
400
401
402
403
404
405
406
407
408
409
410
}

/****************************************************************************/
/* General menu function, see uifc.h for details.							*/
/****************************************************************************/
int ulist(int mode, int left, int top, int width, int *cur, int *bar
	, char *title, char **option)
{
	uchar line[256],shade[256],win[MAX_BFLN],*ptr,a,b,c,longopt
		,search[MAX_OPLN],bline=0;
	int height,y;
	int i,j,opts=0,s=0; /* s=search index into options */
411
	int	is_redraw=0;
412

413
	#ifndef __FLAT__
414
	/* ToDo Mouse stuff */
415
416
417
418
	#endif

	if(mode&WIN_SAV && api->savnum>=MAX_BUFS-1)
		putch(7);
419
	i=0;
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
	if(mode&WIN_INS) bline|=BL_INS;
	if(mode&WIN_DEL) bline|=BL_DEL;
	if(mode&WIN_GET) bline|=BL_GET;
	if(mode&WIN_PUT) bline|=BL_PUT;
	bottomline(bline);
	while(opts<max_opts && opts<MAX_OPTS)
		if(option[opts][0]==0)
			break;
		else opts++;
	if(mode&WIN_XTR && opts<max_opts && opts<MAX_OPTS)
		option[opts++][0]=0;
	height=opts+4;
	if(top+height>api->scrn_len-3)
		height=(api->scrn_len-3)-top;
	if(!width || width<strlen(title)+6) {
		width=strlen(title)+6;
		for(i=0;i<opts;i++) {
			truncsp(option[i]);
			if((j=strlen(option[i])+5)>width)
				width=j; } }
	if(width>(SCRN_RIGHT+1)-SCRN_LEFT)
		width=(SCRN_RIGHT+1)-SCRN_LEFT;
	if(mode&WIN_L2R)
		left=36-(width/2);
	else if(mode&WIN_RHT)
		left=SCRN_RIGHT-(width+4+left);
	if(mode&WIN_T2B)
		top=(api->scrn_len/2)-(height/2)-2;
	else if(mode&WIN_BOT)
		top=api->scrn_len-height-3-top;
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

	/* Dynamic Menus */
#ifdef __unix__
	if(mode&WIN_DYN
			&& cur != NULL 
			&& bar != NULL 
			&& last_menu_cur==cur 
			&& last_menu_bar==bar
			&& save_menu_cur==*cur
			&& save_menu_bar==*bar)
		is_redraw=1;
#endif

	if(!is_redraw) {
		if(mode&WIN_SAV && api->savdepth==api->savnum) {
			if((sav[api->savnum].buf=(char *)MALLOC((width+3)*(height+2)*2))==NULL) {
				cprintf("UIFC line %d: error allocating %u bytes."
					,__LINE__,(width+3)*(height+2)*2);
				return(-1); }
			gettext(SCRN_LEFT+left,SCRN_TOP+top,SCRN_LEFT+left+width+1
				,SCRN_TOP+top+height,sav[api->savnum].buf);
			sav[api->savnum].left=SCRN_LEFT+left;
			sav[api->savnum].top=SCRN_TOP+top;
			sav[api->savnum].right=SCRN_LEFT+left+width+1;
			sav[api->savnum].bot=SCRN_TOP+top+height;
			api->savdepth++; }
		else if(mode&WIN_SAV
			&& (sav[api->savnum].left!=SCRN_LEFT+left
			|| sav[api->savnum].top!=SCRN_TOP+top
			|| sav[api->savnum].right!=SCRN_LEFT+left+width+1
			|| sav[api->savnum].bot!=SCRN_TOP+top+height)) { /* dimensions have changed */
			puttext(sav[api->savnum].left,sav[api->savnum].top,sav[api->savnum].right,sav[api->savnum].bot
				,sav[api->savnum].buf);	/* put original window back */
			FREE(sav[api->savnum].buf);
			if((sav[api->savnum].buf=(char *)MALLOC((width+3)*(height+2)*2))==NULL) {
				cprintf("UIFC line %d: error allocating %u bytes."
					,__LINE__,(width+3)*(height+2)*2);
				return(-1); }
			gettext(SCRN_LEFT+left,SCRN_TOP+top,SCRN_LEFT+left+width+1
				,SCRN_TOP+top+height,sav[api->savnum].buf);	  /* save again */
			sav[api->savnum].left=SCRN_LEFT+left;
			sav[api->savnum].top=SCRN_TOP+top;
			sav[api->savnum].right=SCRN_LEFT+left+width+1;
			sav[api->savnum].bot=SCRN_TOP+top+height; } }
494
495
496
497
498
499


	#ifndef __FLAT__
	if(show_free_mem) {
	/* ToDo Show free memory */
	//	uprintf(58,1,bclr|(cclr<<4),"%10u bytes free",coreleft());
500
		}
501
502
503
	#endif


504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
	if(!is_redraw) {
		if(mode&WIN_ORG) { /* Clear around menu */
			if(top)
				puttext(SCRN_LEFT,SCRN_TOP,SCRN_RIGHT+2,SCRN_TOP+top-1,blk_scrn);
			if(SCRN_TOP+height+top<=api->scrn_len)
				puttext(SCRN_LEFT,SCRN_TOP+height+top,SCRN_RIGHT+2,api->scrn_len,blk_scrn);
			if(left)
				puttext(SCRN_LEFT,SCRN_TOP+top,SCRN_LEFT+left-1,SCRN_TOP+height+top
					,blk_scrn);
			if(SCRN_LEFT+left+width<=SCRN_RIGHT)
				puttext(SCRN_LEFT+left+width,SCRN_TOP+top,SCRN_RIGHT+2
					,SCRN_TOP+height+top,blk_scrn); }
		ptr=win;
		*(ptr++)='';
		*(ptr++)=hclr|(bclr<<4);
519

520
521
522
523
524
525
526
527
528
529
		if(api->mode&UIFC_MOUSE) {
			/* ToDo Mouse stuff */
			i=0;
		}
		else
			i=0;
		for(;i<width-2;i++) {
			*(ptr++)='';
			*(ptr++)=hclr|(bclr<<4); }
		*(ptr++)='';
530
		*(ptr++)=hclr|(bclr<<4);
531
		*(ptr++)='';
532
		*(ptr++)=hclr|(bclr<<4);
533
534
535
		a=strlen(title);
		b=(width-a-1)/2;
		for(i=0;i<b;i++) {
536
			*(ptr++)=' ';
537
538
539
540
541
542
543
			*(ptr++)=hclr|(bclr<<4); }
		for(i=0;i<a;i++) {
			*(ptr++)=title[i];
			*(ptr++)=hclr|(bclr<<4); }
		for(i=0;i<width-(a+b)-2;i++) {
			*(ptr++)=' ';
			*(ptr++)=hclr|(bclr<<4); }
544
545
		*(ptr++)='';
		*(ptr++)=hclr|(bclr<<4);
546
547
548
549
550
551
552
		*(ptr++)='';
		*(ptr++)=hclr|(bclr<<4);
		for(i=0;i<width-2;i++) {
			*(ptr++)='';
			*(ptr++)=hclr|(bclr<<4); }
		*(ptr++)='';
		*(ptr++)=hclr|(bclr<<4);
553

554
555
		if((*cur)>=opts)
			(*cur)=opts-1;			/* returned after scrolled */
556

557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
		if(!bar) {
			if((*cur)>height-5)
				(*cur)=height-5;
			i=0; }
		else {
			if((*bar)>=opts)
				(*bar)=opts-1;
			if((*bar)>height-5)
				(*bar)=height-5;
			if((*cur)==opts-1)
				(*bar)=height-5;
			if((*bar)<0)
				(*bar)=0;
			if((*cur)<(*bar))
				(*cur)=(*bar);
			i=(*cur)-(*bar);
	//
			if(i+(height-5)>=opts) {
				i=opts-(height-4);
				(*cur)=i+(*bar);
				}
			}
			if((*cur)<0)
				(*cur)=0;

			j=0;
			if(i<0) i=0;
			longopt=0;
			while(j<height-4 && i<opts) {
				*(ptr++)='';
				*(ptr++)=hclr|(bclr<<4);
				*(ptr++)=' ';
				*(ptr++)=hclr|(bclr<<4);
				*(ptr++)='';
				*(ptr++)=lclr|(bclr<<4);
				if(i==(*cur))
					a=hfclr|(hbclr<<4);
				else
					a=lclr|(bclr<<4);
				b=strlen(option[i]);
				if(b>longopt)
					longopt=b;
				if(b+4>width)
					b=width-4;
				for(c=0;c<b;c++) {
					*(ptr++)=option[i][c];
					*(ptr++)=a; }
				while(c<width-4) {
					*(ptr++)=' ';
					*(ptr++)=a;
					c++; }
				*(ptr++)='';
				*(ptr++)=hclr|(bclr<<4);
				i++;
				j++; }
			*(ptr++)='';
			*(ptr++)=hclr|(bclr<<4);
			for(i=0;i<width-2;i++) {
				*(ptr++)='';
				*(ptr++)=hclr|(bclr<<4); }
			*(ptr++)='';
			*(ptr++)=hclr|(bclr<<4);
			puttext(SCRN_LEFT+left,SCRN_TOP+top,SCRN_LEFT+left+width-1
				,SCRN_TOP+top+height-1,win);
			if(bar)
				y=top+3+(*bar);
			else
				y=top+3+(*cur);
			if(opts+4>height && ((!bar && (*cur)!=opts-1)
				|| (bar && ((*cur)-(*bar))+(height-4)<opts))) {
				gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+height-2);
				textattr(lclr|(bclr<<4));
				putch(31);	   /* put down arrow */
				textattr(hclr|(bclr<<4)); }

			if(bar && (*bar)!=(*cur)) {
				gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+3);
				textattr(lclr|(bclr<<4));
				putch(30);	   /* put the up arrow */
				textattr(hclr|(bclr<<4)); }

			if(bclr==BLUE) {
				gettext(SCRN_LEFT+left+width,SCRN_TOP+top+1,SCRN_LEFT+left+width+1
					,SCRN_TOP+top+height-1,shade);
				for(i=1;i<height*4;i+=2)
					shade[i]=DARKGRAY;
				puttext(SCRN_LEFT+left+width,SCRN_TOP+top+1,SCRN_LEFT+left+width+1
					,SCRN_TOP+top+height-1,shade);
				gettext(SCRN_LEFT+left+2,SCRN_TOP+top+height,SCRN_LEFT+left+width+1
					,SCRN_TOP+top+height,shade);
				for(i=1;i<width*2;i+=2)
					shade[i]=DARKGRAY;
				puttext(SCRN_LEFT+left+2,SCRN_TOP+top+height,SCRN_LEFT+left+width+1
					,SCRN_TOP+top+height,shade); }
		showmouse();
	}
	else {	/* Is a redraw */
		i=(*cur)-(*bar);
		j=2;

deuce's avatar
deuce committed
657
		longopt=0;
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
		while(j<height-2 && i<opts) {
			ptr=win;
			if(i==(*cur))
				a=hfclr|(hbclr<<4);
			else
				a=lclr|(bclr<<4);
			b=strlen(option[i]);
			if(b>longopt)
				longopt=b;
			if(b+4>width)
				b=width-4;
			for(c=0;c<b;c++) {
				*(ptr++)=option[i][c];
				*(ptr++)=a; }
			while(c<width-4) {
				*(ptr++)=' ';
				*(ptr++)=a;
				c++; }
			i++;
			j++; 
			puttext(SCRN_LEFT+left+3,SCRN_TOP+top+j,SCRN_LEFT+left+width-2
				,SCRN_TOP+top+j,win); }
680
681
682
683
		if(bar)
			y=top+3+(*bar);
		else
			y=top+3+(*cur);
684
685
686
687
688
	}

	#ifdef __unix__
	last_menu_cur=cur;
	last_menu_bar=bar;
689
	#endif
690
691
692
693
694
695
696
697
698
699
700
701
702
	while(1) {
	#if 0					/* debug */
		gotoxy(30,1);
		cprintf("y=%2d h=%2d c=%2d b=%2d s=%2d o=%2d"
			,y,height,*cur,bar ? *bar :0xff,api->savdepth,opts);
	#endif
		if(!show_free_mem)
			timedisplay();
	#ifndef __FLAT__
		if(api->mode&UIFC_MOUSE) {
		/* ToDo Serious mouse stuff here */
		}
	#endif
703

704
		i=0;
705
706
		if(inkey(1)) {
			i=inkey(0);
707
			if(i==KEY_BACKSPACE || i==BS)
708
				i=ESC;
709
710
711
712
713
714
715
716
717
718
719
720
721
			if(i>255) {
				s=0;
				switch(i) {
					/* ToDo extended keys */
					case KEY_HOME:	/* home */
						if(!opts)
							break;
						if(opts+4>height) {
							hidemouse();
							gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+3);
							textattr(lclr|(bclr<<4));
							putch(' ');    /* Delete the up arrow */
							gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+height-2);
rswindell's avatar
rswindell committed
722
							putch(31);	   /* put the down arrow */
723
							uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+3
724
								,hfclr|(hbclr<<4)
725
726
727
728
729
730
731
732
733
734
735
								,"%-*.*s",width-4,width-4,option[0]);
							for(i=1;i<height-4;i++)    /* re-display options */
								uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+3+i
									,lclr|(bclr<<4)
									,"%-*.*s",width-4,width-4,option[i]);
							(*cur)=0;
							if(bar)
								(*bar)=0;
							y=top+3;
							showmouse();
							break; }
736
						hidemouse();
737
738
739
740
741
742
						gettext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						for(i=1;i<width*2;i+=2)
							line[i]=lclr|(bclr<<4);
						puttext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
743
744
745
746
						(*cur)=0;
						if(bar)
							(*bar)=0;
						y=top+3;
747
748
749
						gettext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						for(i=1;i<width*2;i+=2)
750
							line[i]=hfclr|(hbclr<<4);
751
752
						puttext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
753
754
						showmouse();
						break;
755
756
757
758
759
760
					case KEY_UP:	/* up arrow */
						if(!opts)
							break;
						if(!(*cur) && opts+4>height) {
							hidemouse();
							gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+3); /* like end */
761
							textattr(lclr|(bclr<<4));
rswindell's avatar
rswindell committed
762
							putch(30);	   /* put the up arrow */
763
							gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+height-2);
764
765
766
							putch(' ');    /* delete the down arrow */
							for(i=(opts+4)-height,j=0;i<opts;i++,j++)
								uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+3+j
767
									,i==opts-1 ? hfclr|(hbclr<<4)
768
769
770
771
772
773
774
775
										: lclr|(bclr<<4)
									,"%-*.*s",width-4,width-4,option[i]);
							(*cur)=opts-1;
							if(bar)
								(*bar)=height-5;
							y=top+height-2;
							showmouse();
							break; }
776
777
778
779
						hidemouse();
						gettext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						for(i=1;i<width*2;i+=2)
780
							line[i]=lclr|(bclr<<4);
781
782
						puttext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
						showmouse();
						if(!(*cur)) {
							y=top+height-2;
							(*cur)=opts-1;
							if(bar)
								(*bar)=height-5; }
						else {
							(*cur)--;
							y--;
							if(bar && *bar)
								(*bar)--; }
						if(y<top+3) {	/* scroll */
							hidemouse();
							if(!(*cur)) {
								gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+3);
								textattr(lclr|(bclr<<4));
								putch(' '); }  /* delete the up arrow */
							if((*cur)+height-4==opts-1) {
								gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+height-2);
								textattr(lclr|(bclr<<4));
rswindell's avatar
rswindell committed
803
								putch(31); }   /* put the dn arrow */
804
805
806
807
							y++;
							scroll_text(SCRN_LEFT+left+2,SCRN_TOP+top+3
								,SCRN_LEFT+left+width-3,SCRN_TOP+top+height-2,1);
							uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+3
808
								,hfclr|(hbclr<<4)
809
810
811
812
813
814
815
								,"%-*.*s",width-4,width-4,option[*cur]);
							showmouse(); }
						else {
							hidemouse();
							gettext(SCRN_LEFT+3+left,SCRN_TOP+y
								,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
							for(i=1;i<width*2;i+=2)
816
								line[i]=hfclr|(hbclr<<4);
817
818
819
							puttext(SCRN_LEFT+3+left,SCRN_TOP+y
								,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
							showmouse(); }
820
						break;
821
822
823
824
825
826
827
828
829
#if 0
					case KEY_PPAGE;	/* PgUp */
					case KEY_NPAGE;	/* PgDn */
						if(!opts || (*cur)==(opts-1))
							break;
						(*cur)+=(height-4);
						if((*cur)>(opts-1))
							(*cur)=(opts-1);

830
						hidemouse();
831
832
833
834
835
836
837
						gettext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						for(i=1;i<width*2;i+=2)
							line[i]=lclr|(bclr<<4);
						puttext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);

838
839
						for(i=(opts+4)-height,j=0;i<opts;i++,j++)
							uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+3+j
840
								,i==(*cur) hfclr|(hbclr<<4) : lclr|(bclr<<4)
841
842
843
844
845
								,"%-*.*s",width-4,width-4,option[i]);
						y=top+height-2;
						if(bar)
							(*bar)=height-5;
						gettext(SCRN_LEFT+3+left,SCRN_TOP+y
846
847
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						for(i=1;i<148;i+=2)
848
							line[i]=hfclr|(hbclr<<4);
849
						puttext(SCRN_LEFT+3+left,SCRN_TOP+y
850
851
852
853
854
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						showmouse();
						break;
#endif
					case KEY_END:	/* end */
855
						if(!opts)
856
857
							break;
						if(opts+4>height) {	/* Scroll mode */
858
859
860
							hidemouse();
							gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+3);
							textattr(lclr|(bclr<<4));
rswindell's avatar
rswindell committed
861
							putch(30);	   /* put the up arrow */
862
863
864
							gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+height-2);
							putch(' ');    /* delete the down arrow */
							for(i=(opts+4)-height,j=0;i<opts;i++,j++)
865
								uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+3+j
866
									,i==opts-1 ? hfclr|(hbclr<<4)
867
868
										: lclr|(bclr<<4)
									,"%-*.*s",width-4,width-4,option[i]);
869
							(*cur)=opts-1;
870
871
872
							y=top+height-2;
							if(bar)
								(*bar)=height-5;
873
							showmouse();
874
							break; }
875
876
877
878
879
880
881
882
883
884
885
886
887
888
						hidemouse();
						gettext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						for(i=1;i<width*2;i+=2)
							line[i]=lclr|(bclr<<4);
						puttext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						(*cur)=opts-1;
						y=top+height-2;
						if(bar)
							(*bar)=height-5;
						gettext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						for(i=1;i<148;i+=2)
889
							line[i]=hfclr|(hbclr<<4);
890
891
892
893
894
895
896
897
898
						puttext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						showmouse();
						break;
					case KEY_DOWN:	/* dn arrow */
						if(!opts)
							break;
						if((*cur)==opts-1 && opts+4>height) { /* like home */
							hidemouse();
899
900
							gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+3);
							textattr(lclr|(bclr<<4));
901
							putch(' ');    /* Delete the up arrow */
902
							gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+height-2);
rswindell's avatar
rswindell committed
903
							putch(31);	   /* put the down arrow */
904
							uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+3
905
								,hfclr|(hbclr<<4)
906
907
								,"%-*.*s",width-4,width-4,option[0]);
							for(i=1;i<height-4;i++)    /* re-display options */
908
909
								uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+3+i
									,lclr|(bclr<<4)
910
911
									,"%-*.*s",width-4,width-4,option[i]);
							(*cur)=0;
912
913
914
							y=top+3;
							if(bar)
								(*bar)=0;
915
							showmouse();
916
917
918
919
920
921
922
923
924
							break; }
						hidemouse();
						gettext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						for(i=1;i<width*2;i+=2)
							line[i]=lclr|(bclr<<4);
						puttext(SCRN_LEFT+3+left,SCRN_TOP+y
							,SCRN_LEFT+left+width-2,SCRN_TOP+y,line);
						showmouse();
925
926
927
928
929
930
931
932
933
934
935
936
937
						if((*cur)==opts-1) {
							(*cur)=0;
							y=top+3;
							if(bar) {
								/* gotoxy(1,1); cprintf("bar=%08lX ",bar); */
								(*bar)=0; } }
						else {
							(*cur)++;
							y++;
							if(bar && (*bar)<height-5) {
								/* gotoxy(1,1); cprintf("bar=%08lX ",bar); */
								(*bar)++; } }
						if(y==top+height-1) {	/* scroll */
938
							hidemouse();
939
940
941
942
943
944
945
							if(*cur==opts-1) {
								gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+height-2);
								textattr(lclr|(bclr<<4));
								putch(' '); }  /* delete the down arrow */
							if((*cur)+4==height) {
								gotoxy(SCRN_LEFT+left+1,SCRN_TOP+top+3);
								textattr(lclr|(bclr<<4));
rswindell's avatar
rswindell committed
946
								putch(30); }   /* put the up arrow */
947
948
949
950
951
952
							y--;
							/* gotoxy(1,1); cprintf("\rdebug: %4d ",__LINE__); */
							scroll_text(SCRN_LEFT+left+2,SCRN_TOP+top+3
								,SCRN_LEFT+left+width-3,SCRN_TOP+top+height-2,0);
							/* gotoxy(1,1); cprintf("\rdebug: %4d ",__LINE__); */
							uprintf(SCRN_LEFT+left+3,SCRN_TOP+top+height-2
953
								,hfclr|(hbclr<<4)
954
								,"%-*.*s",width-4,width-4,option[*cur]);
955
							showmouse(); }
956
						else {
957
							hidemouse();
958
959
960
961
							gettext(SCRN_LEFT+3+left,SCRN_TOP+y
								,SCRN_LEFT+left+width-2,SCRN_TOP+y
								,line);
							for(i=1;i<width*2;i+=2)
962
								line[i]=hfclr|(hbclr<<4);
963
964
965
							puttext(SCRN_LEFT+3+left,SCRN_TOP+y
								,SCRN_LEFT+left+width-2,SCRN_TOP+y
								,line);
966
							showmouse(); }
967
968
969
970
971
						break;
					case KEY_F(1):	/* F1 */
						help();
						break;
					case KEY_F(5):	/* F5 */
972
973
						if(mode&WIN_GET && !(mode&WIN_XTR && (*cur)==opts-1)) {
							return((*cur)|MSK_GET); }
974
975
						break;
					case KEY_F(6):	/* F6 */
976
977
						if(mode&WIN_PUT && !(mode&WIN_XTR && (*cur)==opts-1)) {
							return((*cur)|MSK_PUT); }
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
						break;
					case KEY_IC:	/* insert */
						if(mode&WIN_INS) {
							if(mode&WIN_INSACT) {
								hidemouse();
								gettext(SCRN_LEFT+left,SCRN_TOP+top,SCRN_LEFT
									+left+width-1,SCRN_TOP+top+height-1,win);
								for(i=1;i<(width*height*2);i+=2)
									win[i]=lclr|(cclr<<4);
								if(opts) {
									j=(((y-top)*width)*2)+7+((width-4)*2);
									for(i=(((y-top)*width)*2)+7;i<j;i+=2)
										win[i]=hclr|(cclr<<4); }
								puttext(SCRN_LEFT+left,SCRN_TOP+top,SCRN_LEFT
									+left+width-1,SCRN_TOP+top+height-1,win);
								showmouse(); }
994
995
							if(!opts) {
								return(MSK_INS); }
996
997
998
999
1000
							return((*cur)|MSK_INS); }
						break;
					case KEY_DC:	/* delete */
						if(mode&WIN_XTR && (*cur)==opts-1)	/* can't delete */
							break;							/* extra line */
For faster browsing, not all history is shown. View entire blame