uifc.h 18.5 KB
Newer Older
1
/* uifc.h */
2
3
4

/* Rob Swindell's Text-mode User Interface Library */

5
6
7
8
9
10
/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2010 Rob Swindell - http://www.synchro.net/copyright.html		*
12
 *																			*
rswindell's avatar
rswindell committed
13
14
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
15
16
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
rswindell's avatar
rswindell committed
17
18
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *																			*
 * 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.	*
 ****************************************************************************/
37

deuce's avatar
deuce committed
38
39
40
#ifndef _UIFC_H_
#define _UIFC_H_

41
42
43
44
45
46
47
#include <time.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
rswindell's avatar
rswindell committed
48
/* OS Specific */
49
50
51
#if defined(_WIN32)
    #include <windows.h>
#endif
52
53
#if defined(__unix__)
	#include <sys/param.h>	/* PATH_MAX */
54
#endif
55

rswindell's avatar
rswindell committed
56
57
#if defined(__unix__) && !defined(stricmp)
    #define stricmp strcasecmp
58
	#define strnicmp strncasecmp
rswindell's avatar
rswindell committed
59
#endif
60

deuce's avatar
deuce committed
61
#if !defined(FREE_AND_NULL)
deuce's avatar
deuce committed
62
	#define FREE_AND_NULL(x)			if(x!=NULL) { free(x); x=NULL; }
deuce's avatar
deuce committed
63
#endif
64

rswindell's avatar
rswindell committed
65
66
67
68
69
70
71
72
73
74
75
76
#if !defined(MAX_PATH)	/* maximum path length */
	#if defined MAXPATHLEN
		#define MAX_PATH MAXPATHLEN	/* clib.h */
	#elif defined PATH_MAX
		#define MAX_PATH PATH_MAX
	#elif defined _MAX_PATH
		#define MAX_PATH _MAX_PATH
	#else
		#define MAX_PATH 260		
	#endif
#endif

deuce's avatar
deuce committed
77
78
79
80
81
82
83
84
85
#define MAX_OPTS	10000
#define MSK_ON		0xf0000000
#define MSK_OFF 	0x0fffffff
#define MSK_INS 	0x10000000
#define MSK_DEL 	0x20000000
#define MSK_GET 	0x30000000
#define MSK_PUT 	0x40000000
#define MSK_EDIT 	0x50000000
/* Dont forget, negative return values are used for extended keys (if WIN_EXTKEYS used)! */
86
87
#define MAX_OPLN	75		/* Maximum length of each option per menu call */
#define MAX_BUFS	7		/* Maximum number of screen buffers to save */
88
#define MIN_LINES   14      /* Minimum number of screen lines supported */
89
90
91
#define MAX_LINES   255     /* Maximum number of screen lines supported (ciolib screenheight is a uchar) */ 
#define MAX_COLS	255		/* Maximum number of screen columns supported (ciolib screenwidth is a uchar) */ 
#define MAX_BFLN	(MAX_COLS*MAX_LINES*2)	/* Maximum size of screen buffers, char + attr */
92
93
94
95
96

#ifndef uint
#define uint unsigned int
#endif

97
							/**************************/
98
                            /* Bits in uifcapi_t.mode */
99
							/**************************/
100
101
102
103
#define UIFC_INMSG	(1<<0)	/* Currently in Message Routine non-recursive */
#define UIFC_MOUSE	(1<<1)	/* Mouse installed and available */
#define UIFC_MONO	(1<<2)	/* Force monochrome mode */
#define UIFC_COLOR	(1<<3)	/* Force color mode */
104
#define UIFC_IBM	(1<<4)	/* Force use of IBM charset	*/
105
106
#define UIFC_NOCTRL	(1<<5)	/* Don't allow useage of CTRL keys for movement 
							 * etc in menus (Still available in text boxes) */
107
#define UIFC_NHM	(1<<6)	/* Don't hide the mouse pointer */
108

109
110
111
							/*******************************/
                            /* Bits in uifcapi_t.list mode */
							/*******************************/
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#define WIN_ORG 	(1<<0)	/* Original menu - destroy valid screen area */
#define WIN_SAV 	(1<<1)	/* Save existing text and replace when finished */
#define WIN_ACT 	(1<<2)	/* Menu remains active after a selection */
#define WIN_L2R 	(1<<3)	/* Center the window based on 'width'   */
#define WIN_T2B 	(1<<4)	/* Center the window based on 'height'  */
#define WIN_INS 	(1<<5)	/* Allows user to user insert key */
#define WIN_INSACT	(1<<6)	/* Remains active after insert key */
#define WIN_DEL 	(1<<7)	/* Allows user to use delete key */
#define WIN_DELACT	(1<<8)	/* Remains active after delete key */
#define WIN_ESC 	(1<<9)	/* Screen is active when escape is hit			 */
#define WIN_RHT 	(1<<10) /* Place window against right side of screen */
#define WIN_BOT 	(1<<11) /* Place window against botton of screen */
#define WIN_GET 	(1<<12) /* Allows F5 to Get a menu item */
#define WIN_PUT 	(1<<13) /* Allows F6 to Put a menu item */
#define WIN_CHE 	(1<<14) /* Stay active after escape if changes */
#define WIN_XTR 	(1<<15) /* Add extra line at end for inserting at end */
128
129
130
#define WIN_DYN 	(1<<16) /* Dynamic window - return at least every second */
#define WIN_HLP 	(1<<17) /* Parse 'Help codes' */
#define WIN_PACK 	(1<<18) /* Pack text in window (No padding) */
131
132
#define WIN_IMM 	(1<<19) /* Draw window and return immediately */
#define WIN_FAT		(1<<20)	/* Do not pad outside borders */
133
134
#define WIN_REDRAW	(1<<21) /* Force redraw on dynamic window */
#define WIN_NODRAW	(1<<22) /* Force not to redraw on dynamic window */
deuce's avatar
deuce committed
135
#define WIN_EXTKEYS	(1<<23) /* Return on any keypress... if it's not handled internally
136
137
							 * Return value is -2 - keyvalue */
#define WIN_NOBRDR	(1<<24)	/* Do not draw a border around the window */
138
#define WIN_FIXEDHEIGHT	(1<<25)	/* Use list_height from uifc struct */
139
140
#define WIN_UNGETMOUSE  (1<<26) /* If the mouse is clicked outside the window, */
								/* Put the mouse event back into the event queue */
141
142
#define WIN_EDIT	(1<<27)	/* Allow F2 to edit a menu item */
#define WIN_EDITACT	(1<<28)	/* Remain active after edit key */
143
#define WIN_INACT	(1<<29)	/* Draw window inactive... intended for use with WIN_IMM */
144
145
146
147
#define WIN_POP		(1<<30)	/* Exit the list. Act as though ESC was pressed. */
							/* Intended for use after a WIN_EXTKEYS or WIN_DYN */
#define WIN_SEL		(1<<31)	/* Exit the list. Act as though ENTER was pressed. */
							/* Intended for use after a WIN_EXTKEYS or WIN_DYN */
148
149
150
151
152

#define WIN_MID WIN_L2R|WIN_T2B  /* Place window in middle of screen */

#define SCRN_TOP	3
#define SCRN_LEFT	5
153
#define SCRN_RIGHT 	((int)api->scrn_width-4)
154
155
156
157
158
159
160
161
162
163
164
165
166

								/* Bits in 'mode' for getkey and getstr     */
#define K_UPPER 	(1L<<0) 	/* Converts all letters to upper case		*/
#define K_UPRLWR	(1L<<1) 	/* Upper/Lower case automatically			*/
#define K_NUMBER	(1L<<2) 	/* Allow numbers only						*/
#define K_WRAP		(1L<<3) 	/* Allows word wrap 						*/
#define K_MSG		(1L<<4) 	/* Allows ANSI, ^N ^A ^G					*/
#define K_SPIN		(1L<<5) 	/* Spinning cursor (same as SPIN)			*/
#define K_LINE		(1L<<6) 	/* Input line (inverse color)				*/
#define K_EDIT		(1L<<7) 	/* Edit string passed						*/
#define K_CHAT		(1L<<8) 	/* In chat multi-chat						*/
#define K_NOCRLF	(1L<<9) 	/* Don't print CRLF after string input      */
#define K_ALPHA 	(1L<<10)	/* Only allow alphabetic characters 		*/
167
#define K_SCANNING	(1L<<11)	/* UPC Scanner is active... return on '%'	*/
168
#define K_TABEXIT	(1L<<12)	/* Return on TAB or BACKTAB			    	*/
169
#define K_DECIMAL	(1L<<13)	/* Allow floating point numbers only		*/
170
171
#define K_DEUCEEXIT	(1L<<14)	/* Return whenever Deuce wants to exit		*/
								/* Returns on up/down/F2					*/
172
173
174
#define K_MOUSEEXIT	(1L<<15)	/* Returns when mouse is clicked outside of */
								/* Input area (NOT outside of window!)		*/
								/* And ungets the mouse event.				*/
175
#define K_PASSWORD	(1L<<16)	/* Does not display text while editing		*/
176

177
178
179
180
181
182
						/* 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 */
#define BL_EDIT     (1<<4)  /* Edit key */
183
#define BL_HELP     (1<<5)  /* Help key */
184

185
186
#define HELPBUF_SIZE 4000

187
#ifndef _GEN_DEFS_H
188
189
190
									/* Control characters */
#define STX 	0x02				/* Start of text			^B	*/
#define ETX 	0x03				/* End of text				^C	*/
191
192
193
#define BS		'\b'				/* Back space				^H	*/
#define TAB 	'\t'				/* Horizontal tabulation	^I	*/
#define LF		'\n'				/* Line feed				^J	*/
194
#define FF		0x0c				/* Form feed				^L	*/
195
#define CR		'\r'				/* Carriage return			^M	*/
196
#define ESC 	0x1b				/* Escape					^[	*/
197
#define DEL     0x7f                /* Delete                   ^BS */
198

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
enum {
	 CTRL_A=1
	,CTRL_B
	,CTRL_C
	,CTRL_D	
	,CTRL_E
	,CTRL_F
	,CTRL_G
	,CTRL_H
	,CTRL_I
	,CTRL_J
	,CTRL_K
	,CTRL_L
	,CTRL_M
	,CTRL_N
	,CTRL_O
	,CTRL_P
	,CTRL_Q
	,CTRL_R
	,CTRL_S
	,CTRL_T
	,CTRL_U
	,CTRL_V
	,CTRL_W
	,CTRL_X
	,CTRL_Y
	,CTRL_Z
};

228
#endif
229
230
231
232
233
234
235
236
237
238
239

#ifndef uchar				/* Short-hand for unsigned data types */
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#ifndef ulong
#define ulong unsigned long
#endif

240
241
#ifndef BOOL
#define BOOL    int
deuce's avatar
deuce committed
242
#ifndef TRUE
243
#define TRUE    1
deuce's avatar
deuce committed
244
245
#endif
#ifndef FALSE
246
247
#define FALSE   0
#endif
deuce's avatar
deuce committed
248
#endif
249
250

typedef struct {
251
	int		left,top,right,bot;
252
	int		*cur,*bar;
253
    uchar*	buf;
254
} win_t;
255

256
typedef struct {
257
258
259
/****************************************************************************/
/* Size of the structure (for version compatibility verification).			*/
/****************************************************************************/
260
    size_t  size;
261
262
263
/****************************************************************************/
/* Controls general UIFC library behavior.									*/
/****************************************************************************/
264
    long    mode;
265
266
267
/****************************************************************************/
/* Set to TRUE when changes to data have been made by input function.		*/ 
/****************************************************************************/
268
    BOOL    changes;
269
270
271
/****************************************************************************/
/* The overlapped-window save buffer number.								*/
/****************************************************************************/
272
    uint    savnum;
273
274
275
276
277
278
279
/****************************************************************************/
/* The current overlapped-window save buffer depth.							*/
/****************************************************************************/
    uint    savdepth;
/****************************************************************************/
/* Screen length															*/
/****************************************************************************/
280
    uint    scrn_len;
281
/****************************************************************************/
282
283
284
285
/* Screen Width 															*/
/****************************************************************************/
    uint    scrn_width;
/****************************************************************************/
286
287
288
289
/* ESC key delay for curses													*/
/****************************************************************************/
    uint    esc_delay;
/****************************************************************************/
290
291
/* Alternative method of setting current help text.							*/
/****************************************************************************/
292
    char*   helpbuf;
293
294
295
/****************************************************************************/
/* Location of the help data and index files.								*/
/****************************************************************************/
296
297
    char    helpdatfile[MAX_PATH+1];
    char    helpixbfile[MAX_PATH+1];
298
/****************************************************************************/
299
300
301
302
303
304
305
306
/* Help and exit button locations for current/last window					*/
/****************************************************************************/
	int		buttony;
	int		exitstart;
	int		exitend;
	int		helpstart;
	int		helpend;
/****************************************************************************/
307
308
309
/* List height for WIN_FIXEDHEIGHT lists.									*/
/****************************************************************************/
	int		list_height;
310
311
312
313

/****************************************************************************/
/* Colours for the various bits												*/
/****************************************************************************/
314
	uchar	hclr,lclr,bclr,cclr,lbclr;
315

316
317
318
319
320
/****************************************************************************/
/* Have we initialized successfully?										*/
/****************************************************************************/
	BOOL	initialized;

321
/****************************************************************************/
322
323
/* Exit/uninitialize function.												*/
/****************************************************************************/
324
    void    (*bail) (void);
325
326
327
328
329
/****************************************************************************/
/* Fill the screen with the appropriate background attribute.				*/
/* str is the title for the application banner.								*/
/* Returns 0 on success, non-zero on failure.								*/
/****************************************************************************/
330
    int     (*scrn) (char* str);
331
332
333
/****************************************************************************/
/* Popup a message, maybe wait for the user to hit a key or click button.	*/
/****************************************************************************/
334
    void    (*msg)  (char* str);
335
336
337
338
339
/****************************************************************************/
/* Popup/down a status message.												*/
/* str is the message to display on popup.									*/
/* if str==NULL, then the the status is to be cleared (popdown).			*/
/****************************************************************************/
340
    void    (*pop)  (char* str);
341
342
343
344
345
346
347
348
/****************************************************************************/
/* General menu function.													*/
/* mode contains WIN_* flags to control display and functionality.			*/
/* left, top and width specify desired screen locations and window size.	*/
/* cur is a pointer to the current (default) option.						*/
/* bar is a pointer to the current location of the lightbar (which used).	*/
/* title is the caption for the menu.										*/
/* Menus can centered left to right and top to bottom automatically.		*/
349
/* mode bits are set with macros WIN_*.										*/
350
351
352
/* option is an array of char arrays, first element of last char array		*/
/* must be NULL.															*/
/* Returns the 0-based selected option number, -1 for ESC, or the selected	*/
353
/* option number OR'd with MSK_INS, MSK_DEL, MSK_GET, MSK_PUT, or MSK_EDIT.	*/
354
/****************************************************************************/
355
    int     (*list) (int mode, int left, int top, int width, int* dflt
356
                        ,int* bar, char *title, char** option);
357
358
359
360
361
362
/****************************************************************************/
/* Windowed string input routine.											*/
/* mode contains WIN_* flags to control display and functionality.			*/
/* left and top specify desired screen location.							*/
/* prompt is displayed before the input is requested.						*/
/* str is the string to input or edit.										*/
363
364
365
/* len is the maximum length of the string.									*/
/* kmode contains flags that control the string input (K_* macros).			*/
/* This function sets uifcapi_t.changes to TRUE if the string is modified.	*/
366
367
/* Returns the length of the string or -1 on escape/abort.					*/
/****************************************************************************/
368
369
    int     (*input)(int mode, int left, int top, char* prompt, char* str
            	        ,int len, int kmode);
370
371
372
/****************************************************************************/
/* Sets the current help index by source code file and line number.			*/
/****************************************************************************/
373
    void    (*sethelp)(int line, char* file);
374
375
376
377
378

/****************************************************************************/
/* Shows the current help text												*/
/****************************************************************************/
    void    (*showhelp)(void);
379
380
	
/****************************************************************************/
381
/* Shows a scrollable text buffer - optionally parsing "help markup codes"	*/
382
/****************************************************************************/
383
384
	void	(*showbuf)(int mode, int left, int top, int width, int height
							,char *title, char *hbuf, int *curp, int *barp);
385

386
387
388
389
/****************************************************************************/
/* Updates time in upper left corner of screen with current time in ASCII/  */
/* Unix format																*/
/****************************************************************************/
390
	void	(*timedisplay)(BOOL force);
391

392
393
394
/****************************************************************************/
/* Displays the bottom line using the BL_* macros							*/
/****************************************************************************/
395
    void	(*bottomline)(int line);
396

397
398
399
/****************************************************************************/
/* String input/exit box at a specified position							*/
/****************************************************************************/
400
401
	int		(*getstrxy)(int left, int top, int width, char *outstr, int max
							,long mode, int *lastkey);
402

403
404
405
/****************************************************************************/
/* Formatted print with attribute											*/
/****************************************************************************/
406
	int		(*printf)(int x, int y, unsigned attr, char *fmat, ...);
407

408
409
} uifcapi_t;

410
411
412
413
414
415
416
417
418
419
420
421
#ifdef __cplusplus
extern "C" {
#endif

/* Return value from uifc_api.list() when uifcYesNoOpts is used */
enum {
	uifcYes=0,
	uifcNo=1
};

extern char* uifcYesNoOpts[];

422
423
424
425
426
427
/****************************************************************************/
/* Initialization routines for each UIFC implementation.					*/
/* Returns 0 on success, non-zero on failure.								*/
/****************************************************************************/
int uifcini(uifcapi_t*);	/* Original implementation based on conio		*/
int uifcinix(uifcapi_t*);	/* Standard I/O implementation					*/
deuce's avatar
deuce committed
428
int uifcini32(uifcapi_t*);	/* conio/curses implementation					*/
429
/****************************************************************************/
deuce's avatar
deuce committed
430

431
432
433
434
#ifdef __cplusplus
}
#endif

deuce's avatar
deuce committed
435
#endif /* Don't add anything after this line! */