uifc.h 21.1 KB
Newer Older
1
/* Text-mode User Interface Library (inspired by Novell SYSCON look & feel) */
2

3
4
5
6
7
8
/* $Id$ */

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

deuce's avatar
deuce committed
36
37
38
#ifndef _UIFC_H_
#define _UIFC_H_

39
40
41
42
43
44
45
#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
46
/* OS Specific */
47
48
49
#if defined(_WIN32)
    #include <windows.h>
#endif
50
51
#if defined(__unix__)
	#include <sys/param.h>	/* PATH_MAX */
52
#endif
53

deuce's avatar
deuce committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifdef _WIN32
        #ifdef __BORLANDC__
                #define UIFCCALL __stdcall
        #else
                #define UIFCCALL
        #endif
        #if defined(UIFC_IMPORTS) || defined(UIFC_EXPORTS)
                #if defined(UIFC_IMPORTS)
                        #define UIFCEXPORT __declspec( dllimport )
                        #define UIFCEXPORTVAR __declspec( dllimport )
                #else
                        #define UIFCEXPORT __declspec( dllexport )
                        #define UIFCEXPORTVAR __declspec( dllexport )
                #endif
        #else   /* self-contained executable */
                #define UIFCEXPORT
                #define UIFCEXPORTVAR	extern
        #endif
#else
        #define UIFCCALL
        #define UIFCEXPORT
        #define UIFCEXPORTVAR	extern
#endif

rswindell's avatar
rswindell committed
78
79
#if defined(__unix__) && !defined(stricmp)
    #define stricmp strcasecmp
80
	#define strnicmp strncasecmp
rswindell's avatar
rswindell committed
81
#endif
82

deuce's avatar
deuce committed
83
#if !defined(FREE_AND_NULL)
deuce's avatar
deuce committed
84
	#define FREE_AND_NULL(x)			if(x!=NULL) { free(x); x=NULL; }
deuce's avatar
deuce committed
85
#endif
86

rswindell's avatar
rswindell committed
87
88
89
90
91
92
93
94
95
96
97
98
#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

99
100
101
102
103
104
105
#define MAX_OPTS			10000
#define MSK_ON				0xf0000000
#define MSK_OFF 			0x0fffffff
#define MSK_INS 			0x10000000
#define MSK_DEL 			0x20000000
#define MSK_COPY 			0x30000000
#define MSK_CUT 			0x40000000
106
107
#define MSK_PASTE			0x50000000	/* Overwrite selected item with previously copied item */
#define MSK_EDIT			0x60000000
108
109
110

/* Legacy terms (get/put instead of copy/paste) */
#define MSK_GET		MSK_COPY
111
#define MSK_PUT		MSK_PASTE
112

113
/* Don't forget, negative return values are used for extended keys (if WIN_EXTKEYS used)! */
114
115
#define MAX_OPLN	75		/* Maximum length of each option per menu call */
#define MAX_BUFS	7		/* Maximum number of screen buffers to save */
116
#define MIN_LINES   14      /* Minimum number of screen lines supported */
117
118
119
#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 */
120
121
122
123
124

#ifndef uint
#define uint unsigned int
#endif

125
126
127
128
129
130
131
132
133
134
135
136
								/**************************/
						        /* Bits in uifcapi_t.mode */
								/**************************/
#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 */
#define UIFC_IBM		(1<<4)	/* Force use of IBM charset	*/
#define UIFC_NOCTRL		(1<<5)	/* Don't allow usage of CTRL keys for movement 
								 * etc in menus (Still available in text boxes) */
#define UIFC_NHM		(1<<6)	/* Don't hide the mouse pointer */
#define UIFC_NOMOUSE	(1<<7)	/* Don't enable/use the mouse */
137

138
139
140
							/*******************************/
                            /* Bits in uifcapi_t.list mode */
							/*******************************/
141
142
143
144
145
146
147
148
149
150
151
#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 */
152
#define WIN_BOT 	(1<<11) /* Place window against bottom of screen */
153
154
#define WIN_COPY 	(1<<12) /* Allows F5 to Get (copy) a menu item */
#define WIN_PASTE 	(1<<13) /* Allows F6 to Put (paste) a menu item */
155
156
#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 */
157
#define WIN_DYN 	(1<<16) /* Dynamic window - return at least every second */
158
159
160
#define WIN_CUT		(1<<17)	/* Allow ^X (cut) a menu item */
#define WIN_HLP 	(1<<17) /* Parse 'Help codes' - showbuf() and bottomline */
#define WIN_PACK 	(1<<18) /* Pack text in window (No padding) - showbuf() */
161
162
#define WIN_IMM 	(1<<19) /* Draw window and return immediately */
#define WIN_FAT		(1<<20)	/* Do not pad outside borders */
163
164
#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
165
#define WIN_EXTKEYS	(1<<23) /* Return on any keypress... if it's not handled internally
166
167
							 * Return value is -2 - keyvalue */
#define WIN_NOBRDR	(1<<24)	/* Do not draw a border around the window */
168
#define WIN_FIXEDHEIGHT	(1<<25)	/* Use list_height from uifc struct */
169
170
#define WIN_UNGETMOUSE  (1<<26) /* If the mouse is clicked outside the window, */
								/* Put the mouse event back into the event queue */
171
172
173
174
175
176
177
#define WIN_EDIT		(1<<27)	/* Allow F2 to edit a menu item */
#define WIN_PASTEXTR	(1<<28)	/* Allow paste into extra (blank) item */
#define WIN_INACT		(1<<29)	/* Draw window inactive... intended for use with WIN_IMM */
#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 */
178
179

#define WIN_MID WIN_L2R|WIN_T2B  /* Place window in middle of screen */
180
181
#define WIN_GET	WIN_COPY
#define WIN_PUT WIN_PASTE
182
183
184

#define SCRN_TOP	3
#define SCRN_LEFT	5
185
#define SCRN_RIGHT 	((int)api->scrn_width-4)
186
187
188
189
190
191
192
193
194
195
196
197
198

								/* 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 		*/
199
#define K_SCANNING	(1L<<11)	/* UPC Scanner is active... return on '%'	*/
200
#define K_TABEXIT	(1L<<12)	/* Return on TAB or BACKTAB			    	*/
201
#define K_DECIMAL	(1L<<13)	/* Allow floating point numbers only		*/
202
203
#define K_DEUCEEXIT	(1L<<14)	/* Return whenever Deuce wants to exit		*/
								/* Returns on up/down/F2					*/
204
205
206
#define K_MOUSEEXIT	(1L<<15)	/* Returns when mouse is clicked outside of */
								/* Input area (NOT outside of window!)		*/
								/* And ungets the mouse event.				*/
207
#define K_PASSWORD	(1L<<16)	/* Does not display text while editing		*/
208

209

210
211
212
						/* Extra exit flags */
#define UIFC_XF_QUIT	(1<<0)	/* Returned -1 due to CIO_KEY_QUIT */

213
214
#define HELPBUF_SIZE 4000

215
#ifndef _GEN_DEFS_H
216
217
218
									/* Control characters */
#define STX 	0x02				/* Start of text			^B	*/
#define ETX 	0x03				/* End of text				^C	*/
219
220
221
#define BS		'\b'				/* Back space				^H	*/
#define TAB 	'\t'				/* Horizontal tabulation	^I	*/
#define LF		'\n'				/* Line feed				^J	*/
222
#define FF		0x0c				/* Form feed				^L	*/
223
#define CR		'\r'				/* Carriage return			^M	*/
224
#define ESC 	0x1b				/* Escape					^[	*/
225
#define DEL     0x7f                /* Delete                   ^BS */
226

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
};

256
#endif
257
258
259
260
261
262
263
264
265
266
267

#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

268
269
#ifndef BOOL
#define BOOL    int
deuce's avatar
deuce committed
270
#ifndef TRUE
271
#define TRUE    1
deuce's avatar
deuce committed
272
273
#endif
#ifndef FALSE
274
275
#define FALSE   0
#endif
deuce's avatar
deuce committed
276
#endif
277
278

typedef struct {
279
	int		left,top,right,bot;
280
	int		*cur,*bar;
281
    uchar*	buf;
282
} win_t;
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
330
331
332
333
334
335
336
337
typedef struct {
	char	background;
	char	help_char;
	char	close_char;
	char	up_arrow;
	char	down_arrow;
	char	button_left;
	char	button_right;

	char	list_top_left;
	char	list_top;
	char	list_top_right;
	char	list_separator_left;
	char	list_separator_right;
	char	list_horizontal_separator;
	char	list_left;
	char	list_right;
	char	list_bottom_left;
	char	list_bottom_right;
	char	list_bottom;
	char	list_scrollbar_separator;

	char	input_top_left;
	char	input_top;
	char	input_top_right;
	char	input_left;
	char	input_right;
	char	input_bottom_left;
	char	input_bottom_right;
	char	input_bottom;

	char	popup_top_left;
	char	popup_top;
	char	popup_top_right;
	char	popup_left;
	char	popup_right;
	char	popup_bottom_left;
	char	popup_bottom_right;
	char	popup_bottom;

	char	help_top_left;
	char	help_top;
	char	help_top_right;
	char	help_left;
	char	help_right;
	char	help_bottom_left;
	char	help_bottom_right;
	char	help_bottom;
	char	help_titlebreak_left;
	char	help_titlebreak_right;
	char	help_hitanykey_left;
	char	help_hitanykey_right;
} uifc_graphics_t;

338
typedef struct {
339
340
341
/****************************************************************************/
/* Size of the structure (for version compatibility verification).			*/
/****************************************************************************/
342
    size_t  size;
343
344
345
/****************************************************************************/
/* Controls general UIFC library behavior.									*/
/****************************************************************************/
346
    long    mode;
347
348
349
/****************************************************************************/
/* Set to TRUE when changes to data have been made by input function.		*/ 
/****************************************************************************/
350
    BOOL    changes;
351
352
353
/****************************************************************************/
/* The overlapped-window save buffer number.								*/
/****************************************************************************/
354
    uint    savnum;
355
356
357
358
359
360
361
/****************************************************************************/
/* The current overlapped-window save buffer depth.							*/
/****************************************************************************/
    uint    savdepth;
/****************************************************************************/
/* Screen length															*/
/****************************************************************************/
362
    uint    scrn_len;
363
/****************************************************************************/
364
365
366
367
/* Screen Width 															*/
/****************************************************************************/
    uint    scrn_width;
/****************************************************************************/
368
369
370
371
/* ESC key delay for curses													*/
/****************************************************************************/
    uint    esc_delay;
/****************************************************************************/
372
373
/* Alternative method of setting current help text.							*/
/****************************************************************************/
374
    char*   helpbuf;
375
376
377
/****************************************************************************/
/* Location of the help data and index files.								*/
/****************************************************************************/
378
379
    char    helpdatfile[MAX_PATH+1];
    char    helpixbfile[MAX_PATH+1];
380
	BOOL	help_available;
381
/****************************************************************************/
382
383
384
385
386
387
388
389
/* Help and exit button locations for current/last window					*/
/****************************************************************************/
	int		buttony;
	int		exitstart;
	int		exitend;
	int		helpstart;
	int		helpend;
/****************************************************************************/
390
391
392
/* List height for WIN_FIXEDHEIGHT lists.									*/
/****************************************************************************/
	int		list_height;
393
394
395
396

/****************************************************************************/
/* Colours for the various bits												*/
/****************************************************************************/
397
	uchar	hclr,lclr,bclr,cclr,lbclr;
398

399
400
401
402
403
/****************************************************************************/
/* Have we initialized successfully?										*/
/****************************************************************************/
	BOOL	initialized;

404
405
406
407
408
/****************************************************************************/
/* UIFC_XF_* bits for extra exit status										*/
/****************************************************************************/
	uint	exit_flags;

409
410
411
412
413
/****************************************************************************/
/* Custom drawing characters												*/
/****************************************************************************/
	uifc_graphics_t	*chars;

414
/****************************************************************************/
415
416
/* Exit/uninitialize function.												*/
/****************************************************************************/
417
    void    (*bail) (void);
418
419
420
421
422
/****************************************************************************/
/* Fill the screen with the appropriate background attribute.				*/
/* str is the title for the application banner.								*/
/* Returns 0 on success, non-zero on failure.								*/
/****************************************************************************/
423
    int     (*scrn) (char* str);
424
425
426
/****************************************************************************/
/* Popup a message, maybe wait for the user to hit a key or click button.	*/
/****************************************************************************/
427
    void    (*msg)  (char* str);
428
429
430
431
432
/****************************************************************************/
/* 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).			*/
/****************************************************************************/
433
    void    (*pop)  (char* str);
434
435
436
437
438
439
440
441
/****************************************************************************/
/* 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.		*/
442
/* mode bits are set with macros WIN_*.										*/
443
444
445
/* 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	*/
446
/* option number OR'd with MSK_INS, MSK_DEL, MSK_GET, MSK_PUT, or MSK_EDIT.	*/
447
/****************************************************************************/
448
    int     (*list) (int mode, int left, int top, int width, int* dflt
449
                        ,int* bar, char *title, char** option);
450
451
452
453
454
455
/****************************************************************************/
/* 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.										*/
456
457
458
/* 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.	*/
459
460
/* Returns the length of the string or -1 on escape/abort.					*/
/****************************************************************************/
461
462
    int     (*input)(int mode, int left, int top, char* prompt, char* str
            	        ,int len, int kmode);
463
464
465
/****************************************************************************/
/* Sets the current help index by source code file and line number.			*/
/****************************************************************************/
466
    void    (*sethelp)(int line, char* file);
467
468
469
470
471

/****************************************************************************/
/* Shows the current help text												*/
/****************************************************************************/
    void    (*showhelp)(void);
472
473
	
/****************************************************************************/
474
/* Shows a scrollable text buffer - optionally parsing "help markup codes"	*/
475
/****************************************************************************/
476
477
	void	(*showbuf)(int mode, int left, int top, int width, int height
							,char *title, char *hbuf, int *curp, int *barp);
478

479
480
481
482
/****************************************************************************/
/* Updates time in upper left corner of screen with current time in ASCII/  */
/* Unix format																*/
/****************************************************************************/
483
	void	(*timedisplay)(BOOL force);
484

485
/****************************************************************************/
486
/* Displays the bottom line using the WIN_* mode flags						*/
487
/****************************************************************************/
488
    void	(*bottomline)(int mode);
489

490
491
492
/****************************************************************************/
/* String input/exit box at a specified position							*/
/****************************************************************************/
493
494
	int		(*getstrxy)(int left, int top, int width, char *outstr, int max
							,long mode, int *lastkey);
495

496
497
498
/****************************************************************************/
/* Formatted print with attribute											*/
/****************************************************************************/
499
	int		(*printf)(int x, int y, unsigned attr, char *fmat, ...);
500

501
502
} uifcapi_t;

503
504
505
506
507
508
509
510
511
512
#ifdef __cplusplus
extern "C" {
#endif

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

deuce's avatar
deuce committed
513
UIFCEXPORTVAR char* uifcYesNoOpts[];
514

515
516
517
518
519
/****************************************************************************/
/* Initialization routines for each UIFC implementation.					*/
/* Returns 0 on success, non-zero on failure.								*/
/****************************************************************************/
int uifcini(uifcapi_t*);	/* Original implementation based on conio		*/
520
UIFCEXPORT int UIFCCALL uifcinix(uifcapi_t*);	/* Standard I/O implementation					*/
deuce's avatar
deuce committed
521
UIFCEXPORT int UIFCCALL uifcini32(uifcapi_t*);	/* modern implementation	*/
522
/****************************************************************************/
deuce's avatar
deuce committed
523

524
525
526
527
#ifdef __cplusplus
}
#endif

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