1 #ifndef H_NEWT
2 #define H_NEWT
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include <stdarg.h>
9 
10 #define NEWT_COLORSET_ROOT 		2
11 #define NEWT_COLORSET_BORDER 		3
12 #define NEWT_COLORSET_WINDOW		4
13 #define NEWT_COLORSET_SHADOW		5
14 #define NEWT_COLORSET_TITLE		6
15 #define NEWT_COLORSET_BUTTON		7
16 #define NEWT_COLORSET_ACTBUTTON		8
17 #define NEWT_COLORSET_CHECKBOX		9
18 #define NEWT_COLORSET_ACTCHECKBOX	10
19 #define NEWT_COLORSET_ENTRY		11
20 #define NEWT_COLORSET_LABEL		12
21 #define NEWT_COLORSET_LISTBOX		13
22 #define NEWT_COLORSET_ACTLISTBOX	14
23 #define NEWT_COLORSET_TEXTBOX		15
24 #define NEWT_COLORSET_ACTTEXTBOX	16
25 #define NEWT_COLORSET_HELPLINE		17
26 #define NEWT_COLORSET_ROOTTEXT		18
27 #define NEWT_COLORSET_EMPTYSCALE	19
28 #define NEWT_COLORSET_FULLSCALE		20
29 #define NEWT_COLORSET_DISENTRY		21
30 #define NEWT_COLORSET_COMPACTBUTTON	22
31 #define NEWT_COLORSET_ACTSELLISTBOX	23
32 #define NEWT_COLORSET_SELLISTBOX	24
33 
34 #define NEWT_COLORSET_CUSTOM(x)		(30 + (x))
35 
36 #define NEWT_ARG_LAST			-100000
37 #define NEWT_ARG_APPEND			-1
38 
39 struct newtColors {
40     char * rootFg, * rootBg;
41     char * borderFg, * borderBg;
42     char * windowFg, * windowBg;
43     char * shadowFg, * shadowBg;
44     char * titleFg, * titleBg;
45     char * buttonFg, * buttonBg;
46     char * actButtonFg, * actButtonBg;
47     char * checkboxFg, * checkboxBg;
48     char * actCheckboxFg, * actCheckboxBg;
49     char * entryFg, * entryBg;
50     char * labelFg, * labelBg;
51     char * listboxFg, * listboxBg;
52     char * actListboxFg, * actListboxBg;
53     char * textboxFg, * textboxBg;
54     char * actTextboxFg, * actTextboxBg;
55     char * helpLineFg, * helpLineBg;
56     char * rootTextFg, * rootTextBg;
57     char * emptyScale, * fullScale;
58     char * disabledEntryFg, * disabledEntryBg;
59     char * compactButtonFg, * compactButtonBg;
60     char * actSelListboxFg, * actSelListboxBg;
61     char * selListboxFg, * selListboxBg;
62 };
63 
64 enum newtFlagsSense { NEWT_FLAGS_SET, NEWT_FLAGS_RESET, NEWT_FLAGS_TOGGLE };
65 
66 #define NEWT_FLAG_RETURNEXIT 	(1 << 0)
67 #define NEWT_FLAG_HIDDEN 	(1 << 1)
68 #define NEWT_FLAG_SCROLL 	(1 << 2)
69 #define NEWT_FLAG_DISABLED 	(1 << 3)
70 /* OBSOLETE #define NEWT_FLAG_NOSCROLL 	(1 << 4)	for listboxes */
71 #define NEWT_FLAG_BORDER	(1 << 5)
72 #define NEWT_FLAG_WRAP		(1 << 6)
73 #define NEWT_FLAG_NOF12		(1 << 7)
74 #define NEWT_FLAG_MULTIPLE      (1 << 8)
75 #define NEWT_FLAG_SELECTED	(1 << 9)
76 #define NEWT_FLAG_CHECKBOX	(1 << 10)
77 #define NEWT_FLAG_PASSWORD      (1 << 11)  /* draw '*'  of chars in entrybox */
78 #define NEWT_FLAG_SHOWCURSOR    (1 << 12) /* Only applies to listbox for now */
79 #define NEWT_FD_READ		(1 << 0)
80 #define NEWT_FD_WRITE		(1 << 1)
81 #define NEWT_FD_EXCEPT		(1 << 2)
82 
83 #define NEWT_CHECKBOXTREE_UNSELECTABLE	(1 << 12)
84 #define NEWT_CHECKBOXTREE_HIDE_BOX	(1 << 13)
85 
86 #define NEWT_CHECKBOXTREE_COLLAPSED	'\0'
87 #define NEWT_CHECKBOXTREE_EXPANDED	'\1'
88 #define NEWT_CHECKBOXTREE_UNSELECTED	' '
89 #define NEWT_CHECKBOXTREE_SELECTED	'*'
90 
91 /* Backwards compatibility */
92 #define NEWT_LISTBOX_RETURNEXIT NEWT_FLAG_RETURNEXIT
93 #define NEWT_ENTRY_SCROLL	NEWT_FLAG_SCROLL
94 #define NEWT_ENTRY_HIDDEN	NEWT_FLAG_HIDDEN
95 #define NEWT_ENTRY_RETURNEXIT	NEWT_FLAG_RETURNEXIT
96 #define NEWT_ENTRY_DISABLED	NEWT_FLAG_DISABLED
97 
98 #define NEWT_TEXTBOX_WRAP	NEWT_FLAG_WRAP
99 #define NEWT_TEXTBOX_SCROLL	NEWT_FLAG_SCROLL
100 #define NEWT_FORM_NOF12		NEWT_FLAG_NOF12
101 
102 #define newtListboxAddEntry	newtListboxAppendEntry
103 
104 
105 typedef struct newtComponent_struct * newtComponent;
106 
107 extern const struct newtColors newtDefaultColorPalette;
108 
109 typedef void (*newtCallback)(newtComponent, void *);
110 typedef void (*newtSuspendCallback)(void * data);
111 
112 int newtInit(void);
113 int newtFinished(void);
114 void newtCls(void);
115 void newtResizeScreen(int redraw);
116 void newtWaitForKey(void);
117 void newtClearKeyBuffer(void);
118 void newtDelay(unsigned int usecs);
119 /* top, left are *not* counting the border */
120 int newtOpenWindow(int left,int top,
121 		   unsigned int width,unsigned  int height,
122  		  const char * title);
123 int newtCenteredWindow(unsigned int width,unsigned int height, const char * title);
124 void newtPopWindow(void);
125 void newtPopWindowNoRefresh(void);
126 void newtSetColors(struct newtColors colors);
127 void newtSetColor(int colorset, char *fg, char *bg);
128 void newtRefresh(void);
129 void newtSuspend(void);
130 void newtSetSuspendCallback(newtSuspendCallback cb, void * data);
131 void newtSetHelpCallback(newtCallback cb);
132 int  newtResume(void);
133 void newtPushHelpLine(const char * text);
134 void newtRedrawHelpLine(void);
135 void newtPopHelpLine(void);
136 void newtDrawRootText(int col, int row, const char * text);
137 void newtBell(void);
138 void newtCursorOff(void);
139 void newtCursorOn(void);
140 
141 /* Components */
142 
143 newtComponent newtCompactButton(int left, int top, const char * text);
144 newtComponent newtButton(int left, int top, const char * text);
145 newtComponent newtCheckbox(int left, int top, const char * text, char defValue,
146 			   const char * seq, char * result);
147 char newtCheckboxGetValue(newtComponent co);
148 void newtCheckboxSetValue(newtComponent co, char value);
149 void newtCheckboxSetFlags(newtComponent co, int flags, enum newtFlagsSense sense);
150 
151 
152 newtComponent newtRadiobutton(int left, int top, const char * text, int isDefault,
153 			      newtComponent prevButton);
154 newtComponent newtRadioGetCurrent(newtComponent setMember);
155 void newtRadioSetCurrent(newtComponent setMember);
156 void newtGetScreenSize(int * cols, int * rows);
157 
158 newtComponent newtLabel(int left, int top, const char * text);
159 void newtLabelSetText(newtComponent co, const char * text);
160 void newtLabelSetColors(newtComponent co, int colorset);
161 newtComponent newtVerticalScrollbar(int left, int top, int height,
162 				    int normalColorset, int thumbColorset);
163 void newtScrollbarSet(newtComponent co, int where, int total);
164 void newtScrollbarSetColors(newtComponent co, int normal, int thumb);
165 
166 newtComponent newtListbox(int left, int top, int height, int flags);
167 void * newtListboxGetCurrent(newtComponent co);
168 void newtListboxSetCurrent(newtComponent co, int num);
169 void newtListboxSetCurrentByKey(newtComponent co, void * key);
170 void newtListboxSetEntry(newtComponent co, int num, const char * text);
171 void newtListboxSetWidth(newtComponent co, int width);
172 void newtListboxSetData(newtComponent co, int num, void * data);
173 int newtListboxAppendEntry(newtComponent co, const char * text,
174 			   const void * data);
175 /* Send the key to insert after, or NULL to insert at the top */
176 int newtListboxInsertEntry(newtComponent co, const char * text, const void * data, void * key);
177 int newtListboxDeleteEntry(newtComponent co, void * data);
178 void newtListboxClear(newtComponent co); /* removes all entries from listbox */
179 void newtListboxGetEntry(newtComponent co, int num, char **text, void **data);
180 /* Returns an array of data pointers from items, last element is NULL */
181 void **newtListboxGetSelection(newtComponent co, int *numitems);
182 void newtListboxClearSelection(newtComponent co);
183 void newtListboxSelectItem(newtComponent co, const void * key,
184 	enum newtFlagsSense sense);
185 /* Returns number of items currently in listbox. */
186 int newtListboxItemCount(newtComponent co);
187 
188 newtComponent newtCheckboxTree(int left, int top, int height, int flags);
189 newtComponent newtCheckboxTreeMulti(int left, int top, int height, char *seq, int flags);
190 const void ** newtCheckboxTreeGetSelection(newtComponent co, int *numitems);
191 const void * newtCheckboxTreeGetCurrent(newtComponent co);
192 void newtCheckboxTreeSetCurrent(newtComponent co, void * item);
193 const void ** newtCheckboxTreeGetMultiSelection(newtComponent co, int *numitems, char seqnum);
194 /* last item is NEWT_ARG_LAST for all of these */
195 int newtCheckboxTreeAddItem(newtComponent co,
196 			    const char * text, const void * data,
197 			    int flags, int index, ...);
198 int newtCheckboxTreeAddArray(newtComponent co,
199 			     const char * text, const void * data,
200 			     int flags, int * indexes);
201 int * newtCheckboxTreeFindItem(newtComponent co, void * data);
202 void newtCheckboxTreeSetEntry(newtComponent co, const void * data,
203 			      const char * text);
204 void newtCheckboxTreeSetWidth(newtComponent co, int width);
205 char newtCheckboxTreeGetEntryValue(newtComponent co, const void * data);
206 void newtCheckboxTreeSetEntryValue(newtComponent co, const void * data,
207 				   char value);
208 
209 newtComponent newtTextboxReflowed(int left, int top, char * text, int width,
210 				  int flexDown, int flexUp, int flags);
211 newtComponent newtTextbox(int left, int top, int width, int height, int flags);
212 void newtTextboxSetText(newtComponent co, const char * text);
213 void newtTextboxSetHeight(newtComponent co, int height);
214 int newtTextboxGetNumLines(newtComponent co);
215 void newtTextboxSetColors(newtComponent co, int normal, int active);
216 char * newtReflowText(char * text, int width, int flexDown, int flexUp,
217 		      int * actualWidth, int * actualHeight);
218 
219 struct newtExitStruct {
220     enum { NEWT_EXIT_HOTKEY, NEWT_EXIT_COMPONENT, NEWT_EXIT_FDREADY,
221 	   NEWT_EXIT_TIMER, NEWT_EXIT_ERROR } reason;
222     union {
223 	int watch;
224 	int key;
225 	newtComponent co;
226     } u;
227 } ;
228 
229 newtComponent newtForm(newtComponent vertBar, void * helpTag, int flags);
230 void newtFormSetTimer(newtComponent form, int millisecs);
231 void newtFormWatchFd(newtComponent form, int fd, int fdFlags);
232 void newtFormSetSize(newtComponent co);
233 newtComponent newtFormGetCurrent(newtComponent co);
234 void newtFormSetBackground(newtComponent co, int color);
235 void newtFormSetCurrent(newtComponent co, newtComponent subco);
236 void newtFormAddComponent(newtComponent form, newtComponent co);
237 void newtFormAddComponents(newtComponent form, ...);
238 void newtFormSetHeight(newtComponent co, int height);
239 void newtFormSetWidth(newtComponent co, int width);
240 newtComponent newtRunForm(newtComponent form);		/* obsolete */
241 void newtFormRun(newtComponent co, struct newtExitStruct * es);
242 void newtDrawForm(newtComponent form);
243 void newtFormAddHotKey(newtComponent co, int key);
244 int newtFormGetScrollPosition(newtComponent co);
245 void newtFormSetScrollPosition(newtComponent co, int position);
246 
247 typedef int (*newtEntryFilter)(newtComponent entry, void * data, int ch,
248 			       int cursor);
249 newtComponent newtEntry(int left, int top, const char * initialValue, int width,
250 			const char ** resultPtr, int flags);
251 void newtEntrySet(newtComponent co, const char * value, int cursorAtEnd);
252 void newtEntrySetFilter(newtComponent co, newtEntryFilter filter, void * data);
253 char * newtEntryGetValue(newtComponent co);
254 void newtEntrySetFlags(newtComponent co, int flags, enum newtFlagsSense sense);
255 void newtEntrySetColors(newtComponent co, int normal, int disabled);
256 int newtEntryGetCursorPosition(newtComponent co);
257 void newtEntrySetCursorPosition(newtComponent co, int position);
258 
259 newtComponent newtScale(int left, int top, int width, long long fullValue);
260 void newtScaleSet(newtComponent co, unsigned long long amount);
261 void newtScaleSetColors(newtComponent co, int empty, int full);
262 
263 void newtComponentAddCallback(newtComponent co, newtCallback f, void * data);
264 void newtComponentTakesFocus(newtComponent co, int val);
265 
266 void newtComponentGetPosition(newtComponent co, int * left, int * top);
267 void newtComponentGetSize(newtComponent co, int * width, int * height);
268 
269 /* This callback is called when a component is destroyed. */
270 void newtComponentAddDestroyCallback(newtComponent co,
271 				      newtCallback f, void * data);
272 
273 /* this also destroys all of the components (including other forms) on the
274    form */
275 void newtFormDestroy(newtComponent form);
276 
277 /* NB: You SHOULD NOT call this for components which have been added
278  * to a form (ie. almost all components).  They are destroyed along
279  * with the form when you call newtFormDestroy.
280  */
281 void newtComponentDestroy(newtComponent co);
282 
283 /* Key codes */
284 
285 #define NEWT_KEY_TAB			'\t'
286 #define NEWT_KEY_ENTER			'\r'
287 #define NEWT_KEY_SUSPEND		'\032'			/* ctrl - z*/
288 #define NEWT_KEY_ESCAPE			''
289 #define NEWT_KEY_RETURN			NEWT_KEY_ENTER
290 
291 #define NEWT_KEY_EXTRA_BASE		0x8000
292 #define NEWT_KEY_UP			NEWT_KEY_EXTRA_BASE + 1
293 #define NEWT_KEY_DOWN			NEWT_KEY_EXTRA_BASE + 2
294 #define NEWT_KEY_LEFT			NEWT_KEY_EXTRA_BASE + 4
295 #define NEWT_KEY_RIGHT			NEWT_KEY_EXTRA_BASE + 5
296 #define NEWT_KEY_BKSPC			NEWT_KEY_EXTRA_BASE + 6
297 #define NEWT_KEY_DELETE			NEWT_KEY_EXTRA_BASE + 7
298 #define NEWT_KEY_HOME			NEWT_KEY_EXTRA_BASE + 8
299 #define NEWT_KEY_END			NEWT_KEY_EXTRA_BASE + 9
300 #define NEWT_KEY_UNTAB			NEWT_KEY_EXTRA_BASE + 10
301 #define NEWT_KEY_PGUP			NEWT_KEY_EXTRA_BASE + 11
302 #define NEWT_KEY_PGDN			NEWT_KEY_EXTRA_BASE + 12
303 #define NEWT_KEY_INSERT			NEWT_KEY_EXTRA_BASE + 13
304 
305 #define NEWT_KEY_F1			NEWT_KEY_EXTRA_BASE + 101
306 #define NEWT_KEY_F2			NEWT_KEY_EXTRA_BASE + 102
307 #define NEWT_KEY_F3			NEWT_KEY_EXTRA_BASE + 103
308 #define NEWT_KEY_F4			NEWT_KEY_EXTRA_BASE + 104
309 #define NEWT_KEY_F5			NEWT_KEY_EXTRA_BASE + 105
310 #define NEWT_KEY_F6			NEWT_KEY_EXTRA_BASE + 106
311 #define NEWT_KEY_F7			NEWT_KEY_EXTRA_BASE + 107
312 #define NEWT_KEY_F8			NEWT_KEY_EXTRA_BASE + 108
313 #define NEWT_KEY_F9			NEWT_KEY_EXTRA_BASE + 109
314 #define NEWT_KEY_F10			NEWT_KEY_EXTRA_BASE + 110
315 #define NEWT_KEY_F11			NEWT_KEY_EXTRA_BASE + 111
316 #define NEWT_KEY_F12			NEWT_KEY_EXTRA_BASE + 112
317 
318 /* not really a key, but newtGetKey returns it */
319 #define NEWT_KEY_RESIZE			NEWT_KEY_EXTRA_BASE + 113
320 #define NEWT_KEY_ERROR			NEWT_KEY_EXTRA_BASE + 114
321 
322 #define NEWT_ANCHOR_LEFT		(1 << 0)
323 #define NEWT_ANCHOR_RIGHT		(1 << 1)
324 #define NEWT_ANCHOR_TOP			(1 << 2)
325 #define NEWT_ANCHOR_BOTTOM		(1 << 3)
326 
327 #define NEWT_GRID_FLAG_GROWX		(1 << 0)
328 #define NEWT_GRID_FLAG_GROWY		(1 << 1)
329 
330 typedef struct grid_s * newtGrid;
331 enum newtGridElement { NEWT_GRID_EMPTY = 0,
332 		       NEWT_GRID_COMPONENT, NEWT_GRID_SUBGRID };
333 
334 newtGrid newtCreateGrid(int cols, int rows);
335 /* TYPE, what, TYPE, what, ..., NULL */
336 newtGrid newtGridVStacked(enum newtGridElement type, void * what, ...);
337 newtGrid newtGridVCloseStacked(enum newtGridElement type, void * what, ...);
338 newtGrid newtGridHStacked(enum newtGridElement type1, void * what1, ...);
339 newtGrid newtGridHCloseStacked(enum newtGridElement type1, void * what1, ...);
340 newtGrid newtGridBasicWindow(newtComponent text, newtGrid middle,
341 			     newtGrid buttons);
342 newtGrid newtGridSimpleWindow(newtComponent text, newtComponent middle,
343 			     newtGrid buttons);
344 void newtGridSetField(newtGrid grid, int col, int row,
345 		      enum newtGridElement type, void * val, int padLeft,
346 		      int padTop, int padRight, int padBottom, int anchor,
347 		      int flags);
348 void newtGridPlace(newtGrid grid, int left, int top);
349 #define newtGridDestroy newtGridFree
350 void newtGridFree(newtGrid grid, int recurse);
351 void newtGridGetSize(newtGrid grid, int * width, int * height);
352 void newtGridWrappedWindow(newtGrid grid, char * title);
353 void newtGridWrappedWindowAt(newtGrid grid, char * title, int left, int top);
354 void newtGridAddComponentsToForm(newtGrid grid, newtComponent form,
355 				 int recurse);
356 
357 /* convienve */
358 newtGrid newtButtonBarv(char * button1, newtComponent * b1comp, va_list args);
359 newtGrid newtButtonBar(char * button1, newtComponent * b1comp, ...);
360 
361 /* automatically centered and shrink wrapped */
362 void newtWinMessage(char * title, char * buttonText, char * text, ...);
363 void newtWinMessagev(char * title, char * buttonText, char * text,
364 		     va_list argv);
365 
366 /* having separate calls for these two seems silly, but having two separate
367    variable length-arg lists seems like a bad idea as well */
368 
369 /* Returns 0 if F12 was pressed, 1 for button1, 2 for button2 */
370 int newtWinChoice(char * title, char * button1, char * button2,
371 		   char * text, ...);
372 /* Returns 0 if F12 was pressed, 1 for button1, 2 for button2,
373    3 for button3 */
374 int newtWinTernary(char * title, char * button1, char * button2,
375 		   char * button3, char * message, ...);
376 
377 /* Returns the button number pressed, 0 on F12 */
378 int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown,
379 		int flexUp, int maxListHeight, char ** items, int * listItem,
380 		char * button1, ...);
381 
382 struct newtWinEntry {
383     char * text;
384     char ** value;		/* may be initialized to set default */
385     int flags;
386 };
387 
388 /* Returns the button number pressed, 0 on F12. The final values are
389    dynamically allocated, and need to be freed. */
390 int newtWinEntries(char * title, char * text, int suggestedWidth, int flexDown,
391 		   int flexUp, int dataWidth,
392 		   struct newtWinEntry * items, char * button1, ...);
393 
394 #ifdef __cplusplus
395 } /* End of extern "C" { */
396 #endif
397 
398 #endif /* H_NEWT */
399