NEWT C interface module

Featuring the Dragonfly web framework
Locked
ryuo
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

NEWT C interface module

Post by ryuo »

It has taken me several days, but I have carefully converted NEWT's C API so you can access most of it from newLISP. The main thing I could not find a way to map is C variadic functions. But those are mainly used for convenience functions in NEWT.

For those who do not know what NEWT is, it is an old library for TUIs (Text User Interfaces). It is still useful today when you only have a text console. For example, SSH or RS232 connections to an external computer.

This library is my favorite TUI for Linux, but it can allegedly be used on any platform that SLANG works on. I ported the C interface to newLISP because there were no existing modules for TUIs that I could find.

I have included the source code in this post for both the NEWT module and a module with a "Hello World" example of how to use NEWT. I am thinking of adding an object layer for the parts of NEWT that resemble objects, but that is for another day. Please note you must have NEWT version 0.52.16 or newer to use this module and your newLISP must be compiled with FFI support.

Code: Select all

; Based off NEWT 0.52.16

(context 'Newt)

(constant 'Prefix "newt")

(constant 'Library "libnewt.so")

; (MyImport '("" ""))
(macro (MyImport ImportArgs)
	(constant
		(sym (replace Prefix (copy (first ImportArgs)) ""))
		(eval (append '(import Library) ImportArgs))
	)
)

; typedef void (*newtCallback)(newtComponent, void *);
(macro (Callback Function) (callback Function "void" "void*" "void*"))

; typedef void (*newtSuspendCallback)(void * data);
(macro (SuspendCallback Function) (callback Function "void" "void*"))

; typedef int (*newtEntryFilter)(newtComponent entry, void * data, int ch, int cursor);
(macro (EntryFilter Function) (callback Function "int" "void*" "void*" "int" "int"))

; #define NEWT_ARG_LAST			-100000
(constant 'ArgLast -100000)

; #define NEWT_ARG_APPEND			-1
(constant 'ArgAppend -1)

; #define NEWT_FD_READ		(1 << 0)
(constant 'FdRead 1)

; #define NEWT_FD_WRITE		(1 << 1)
(constant 'FdWrite 2)

; #define NEWT_FD_EXCEPT		(1 << 2)
(constant 'FdExcept 4)

; #define NEWT_COLORSET_ROOT 		2
(constant 'ColorSetRoot 2)

; #define NEWT_COLORSET_BORDER 		3
(constant 'ColorSetBorder 3)

; #define NEWT_COLORSET_WINDOW		4
(constant 'ColorSetWindow 4)

; #define NEWT_COLORSET_SHADOW		5
(constant 'ColorSetShadow 5)

; #define NEWT_COLORSET_TITLE		6
(constant 'ColorSetTitle 6)

; #define NEWT_COLORSET_BUTTON		7
(constant 'ColorSetButton 7)

; #define NEWT_COLORSET_ACTBUTTON		8
(constant 'ColorSetActiveButton 8)

; #define NEWT_COLORSET_CHECKBOX		9
(constant 'ColorSetCheckBox 9)

; #define NEWT_COLORSET_ACTCHECKBOX	10
(constant 'ColorSetActiveCheckBox 10)

; #define NEWT_COLORSET_ENTRY		11
(constant 'ColorSetEntry 11)

; #define NEWT_COLORSET_LABEL		12
(constant 'ColorSetLabel 12)

; #define NEWT_COLORSET_LISTBOX		13
(constant 'ColorSetListBox 13)

; #define NEWT_COLORSET_ACTLISTBOX	14
(constant 'ColorSetActiveListBox 14)

; #define NEWT_COLORSET_TEXTBOX		15
(constant 'ColorSetTextBox 15)

; #define NEWT_COLORSET_ACTTEXTBOX	16
(constant 'ColorSetActiveTextBox 16)

; #define NEWT_COLORSET_HELPLINE		17
(constant 'ColorSetHelpLine 17)

; #define NEWT_COLORSET_ROOTTEXT		18
(constant 'ColorSetRootText 18)

; #define NEWT_COLORSET_EMPTYSCALE	19
(constant 'ColorSetEmptyScale 19)

; #define NEWT_COLORSET_FULLSCALE		20
(constant 'ColorSetFullScale 20)

; #define NEWT_COLORSET_DISENTRY		21
(constant 'ColorSetDisabledEntry 21)

; #define NEWT_COLORSET_COMPACTBUTTON	22
(constant 'ColorSetCompactButton 22)

; #define NEWT_COLORSET_ACTSELLISTBOX	23
(constant 'ColorSetActiveSelectedListBox 23)

; #define NEWT_COLORSET_SELLISTBOX	24
(constant 'ColorSetSelectedListBox 24)

; #define NEWT_COLORSET_CUSTOM(x)		(30 + (x))
(macro (ColorSetCustom X) (+ 30 X))

; #define NEWT_FLAG_RETURNEXIT 	(1 << 0)
(constant 'FlagReturnExit 1)

; #define NEWT_FLAG_HIDDEN 	(1 << 1)
(constant 'FlagHidden 2)

; #define NEWT_FLAG_SCROLL 	(1 << 2)
(constant 'FlagScroll 4)

; #define NEWT_FLAG_DISABLED 	(1 << 3)
(constant 'FlagDisabled 8)

; #define NEWT_FLAG_NOSCROLL 	(1 << 4)
(constant 'FlagNoScroll 16)

; #define NEWT_FLAG_BORDER	(1 << 5)
(constant 'FlagBorder 32)

; #define NEWT_FLAG_WRAP		(1 << 6)
(constant 'FlagWrap 64)

; #define NEWT_FLAG_NOF12		(1 << 7)
(constant 'FlagNoF12 128)

; #define NEWT_FLAG_MULTIPLE      (1 << 8)
(constant 'FlagMultiple 256)

; #define NEWT_FLAG_SELECTED	(1 << 9)
(constant 'FlagSelected 512)

; #define NEWT_FLAG_CHECKBOX	(1 << 10)
(constant 'FlagCheckBox 1024)

; #define NEWT_FLAG_PASSWORD      (1 << 11)
(constant 'FlagPassWord 2048)

; #define NEWT_FLAG_SHOWCURSOR    (1 << 12)
(constant 'FlagShowCursor 4096)

; #define NEWT_CHECKBOXTREE_UNSELECTABLE	(1 << 12)
(constant 'CheckBoxTreeUnSelectAble 4096)

; #define NEWT_CHECKBOXTREE_HIDE_BOX	(1 << 13)
(constant 'CheckBoxTreeHideBox 8192)

; #define NEWT_CHECKBOXTREE_COLLAPSED	'\0'
(constant 'CheckBoxTreeCollapsed 0)

; #define NEWT_CHECKBOXTREE_EXPANDED	'\1'
(constant 'CheckBoxTreeExpanded 1)

; #define NEWT_CHECKBOXTREE_UNSELECTED	' '
(constant 'CheckBoxTreeUnSelected 32)

; #define NEWT_CHECKBOXTREE_SELECTED	'*'
(constant 'CheckBoxTreeSelected 42)

; #define NEWT_KEY_TAB			'\t'
(constant 'KeyTab 9)

; #define NEWT_KEY_ENTER			'\r'
(constant 'KeyEnter 13)

; #define NEWT_KEY_SUSPEND		'\032'
(constant 'KeySuspend 26)

; #define NEWT_KEY_ESCAPE			''
(constant 'KeyEscape 27)

; #define NEWT_KEY_EXTRA_BASE		0x8000
(constant 'KeyExtraBase 32768)

; #define NEWT_KEY_UP			NEWT_KEY_EXTRA_BASE + 1
(constant 'KeyUp 32769)

; #define NEWT_KEY_DOWN			NEWT_KEY_EXTRA_BASE + 2
(constant 'KeyDown 32770)

; #define NEWT_KEY_LEFT			NEWT_KEY_EXTRA_BASE + 4
(constant 'KeyLeft 32772)

; #define NEWT_KEY_RIGHT			NEWT_KEY_EXTRA_BASE + 5
(constant 'KeyRight 32773)

; #define NEWT_KEY_BKSPC			NEWT_KEY_EXTRA_BASE + 6
(constant 'KeyBackSpace 32774)

; #define NEWT_KEY_DELETE			NEWT_KEY_EXTRA_BASE + 7
(constant 'KeyDelete 32775)

; #define NEWT_KEY_HOME			NEWT_KEY_EXTRA_BASE + 8
(constant 'KeyHome 32776)

; #define NEWT_KEY_END			NEWT_KEY_EXTRA_BASE + 9
(constant 'KeyEnd 32777)

; #define NEWT_KEY_UNTAB			NEWT_KEY_EXTRA_BASE + 10
(constant 'KeyUnTab 32778)

; #define NEWT_KEY_PGUP			NEWT_KEY_EXTRA_BASE + 11
(constant 'KeyPageUp 32779)

; #define NEWT_KEY_PGDN			NEWT_KEY_EXTRA_BASE + 12
(constant 'KeyPageDown 32780)

; #define NEWT_KEY_INSERT			NEWT_KEY_EXTRA_BASE + 13
(constant 'KeyInsert 32781)

; #define NEWT_KEY_F1			NEWT_KEY_EXTRA_BASE + 101
(constant 'KeyF1 32869)

; #define NEWT_KEY_F2			NEWT_KEY_EXTRA_BASE + 102
(constant 'KeyF2 32870)

; #define NEWT_KEY_F3			NEWT_KEY_EXTRA_BASE + 103
(constant 'KeyF3 32871)

; #define NEWT_KEY_F4			NEWT_KEY_EXTRA_BASE + 104
(constant 'KeyF4 32872)

; #define NEWT_KEY_F5			NEWT_KEY_EXTRA_BASE + 105
(constant 'KeyF5 32873)

; #define NEWT_KEY_F6			NEWT_KEY_EXTRA_BASE + 106
(constant 'KeyF6 32874)

; #define NEWT_KEY_F7			NEWT_KEY_EXTRA_BASE + 107
(constant 'KeyF7 32875)

; #define NEWT_KEY_F8			NEWT_KEY_EXTRA_BASE + 108
(constant 'KeyF8 32876)

; #define NEWT_KEY_F9			NEWT_KEY_EXTRA_BASE + 109
(constant 'KeyF9 32877)

; #define NEWT_KEY_F10			NEWT_KEY_EXTRA_BASE + 110
(constant 'KeyF10 32878)

; #define NEWT_KEY_F11			NEWT_KEY_EXTRA_BASE + 111
(constant 'KeyF11 32879)

; #define NEWT_KEY_F12			NEWT_KEY_EXTRA_BASE + 112
(constant 'KeyF12 32880)

; #define NEWT_KEY_RESIZE			NEWT_KEY_EXTRA_BASE + 113
(constant 'KeyResize 32881)

; #define NEWT_KEY_ERROR			NEWT_KEY_EXTRA_BASE + 114
(constant 'KeyError 32882)

; #define NEWT_ANCHOR_LEFT		(1 << 0)
(constant 'AnchorLeft 1)

; #define NEWT_ANCHOR_RIGHT		(1 << 1)
(constant 'AnchorRight 2)

; #define NEWT_ANCHOR_TOP			(1 << 2)
(constant 'AnchorTop 4)

; #define NEWT_ANCHOR_BOTTOM		(1 << 3)
(constant 'AnchorBottom 8)

; #define NEWT_GRID_FLAG_GROWX		(1 << 0)
(constant 'GridFlagGrowX 1)

; #define NEWT_GRID_FLAG_GROWY		(1 << 1)
(constant 'GridFlagGrowY 2)

; enum newtFlagsSense { NEWT_FLAGS_SET, NEWT_FLAGS_RESET, NEWT_FLAGS_TOGGLE };
(constant 'FlagsSet 0)
(constant 'FlagsReset 1)
(constant 'FlagsToggle 2)

; enum newtGridElement { NEWT_GRID_EMPTY = 0, NEWT_GRID_COMPONENT, NEWT_GRID_SUBGRID };
(constant 'GridEmpty 0)
(constant 'GridComponent 1)
(constant 'GridSubGrid 2)

; struct newtColors {
;     char * rootFg, * rootBg;
;     char * borderFg, * borderBg;
;     char * windowFg, * windowBg;
;     char * shadowFg, * shadowBg;
;     char * titleFg, * titleBg;
;     char * buttonFg, * buttonBg;
;     char * actButtonFg, * actButtonBg;
;     char * checkboxFg, * checkboxBg;
;     char * actCheckboxFg, * actCheckboxBg;
;     char * entryFg, * entryBg;
;     char * labelFg, * labelBg;
;     char * listboxFg, * listboxBg;
;     char * actListboxFg, * actListboxBg;
;     char * textboxFg, * textboxBg;
;     char * actTextboxFg, * actTextboxBg;
;     char * helpLineFg, * helpLineBg;
;     char * rootTextFg, * rootTextBg;
;     char * emptyScale, * fullScale;
;     char * disabledEntryFg, * disabledEntryBg;
;     char * compactButtonFg, * compactButtonBg;
;     char * actSelListboxFg, * actSelListboxBg;
;     char * selListboxFg, * selListboxBg;
; };
(eval (append '(struct 'Colors) (dup "char*" 44 true)))

; struct newtExitStruct {
;     enum { NEWT_EXIT_HOTKEY, NEWT_EXIT_COMPONENT, NEWT_EXIT_FDREADY,
; 	   NEWT_EXIT_TIMER, NEWT_EXIT_ERROR } reason;
;     union {
; 	int watch;
; 	int key;
; 	newtComponent co;
;     } u;
; } ;
(constant 'ExitHotKey 0)
(constant 'ExitComponent 1)
(constant 'ExitFdReady 2)
(constant 'ExitTimer 3)
(constant 'ExitError 4)
(struct 'ExitStruct "int" "void*")

; extern const struct newtColors newtDefaultColorPalette;
(constant 'DefaultColorPalette (unpack Colors (address (import Library "newtDefaultColorPalette"))))

; int newtInit(void);
(MyImport '("newtInit" "int"))

; int newtFinished(void);
(MyImport '("newtFinished" "int"))

; void newtCls(void);
(MyImport '("newtCls" "void"))

; void newtResizeScreen(int redraw);
(MyImport '("newtResizeScreen" "void" "int"))

; void newtWaitForKey(void);
(MyImport '("newtWaitForKey" "void"))

; void newtClearKeyBuffer(void);
(MyImport '("newtClearKeyBuffer" "void"))

; void newtDelay(unsigned int usecs);
(MyImport '("newtDelay" "void" "unsigned int"))

; int newtOpenWindow(int left,int top, unsigned int width,unsigned  int height, const char * title);
(MyImport '("newtOpenWindow" "int" "int" "int" "unsigned int" "unsigned int" "char*"))

; int newtCenteredWindow(unsigned int width,unsigned int height, const char * title);
(MyImport '("newtCenteredWindow" "int" "unsigned int" "unsigned int" "char*"))

; void newtPopWindow(void);
(MyImport '("newtPopWindow" "void"))

; void newtPopWindowNoRefresh(void);
(MyImport '("newtPopWindowNoRefresh" "void"))

; void newtSetColors(struct newtColors colors);
(MyImport '("newtSetColors" "void" "Colors"))

; void newtSetColor(int colorset, char *fg, char *bg);
(MyImport '("newtSetColor" "void" "int" "char*" "char*"))

; void newtRefresh(void);
(MyImport '("newtRefresh" "void"))

; void newtSuspend(void);
(MyImport '("newtSuspend" "void"))

; void newtSetSuspendCallback(newtSuspendCallback cb, void * data);
(MyImport '("newtSetSuspendCallback" "void" "void*" "void*"))

; void newtSetHelpCallback(newtCallback cb);
(MyImport '("newtSetHelpCallback" "void" "void*"))

; int  newtResume(void);
(MyImport '("newtResume" "int"))

; void newtPushHelpLine(const char * text);
(MyImport '("newtPushHelpLine" "void" "char*"))

; void newtRedrawHelpLine(void);
(MyImport '("newtRedrawHelpLine" "void"))

; void newtPopHelpLine(void);
(MyImport '("newtPopHelpLine" "void"))

; void newtDrawRootText(int col, int row, const char * text);
(MyImport '("newtDrawRootText" "void" "int" "int" "char*"))

; void newtBell(void);
(MyImport '("newtBell" "void"))

; void newtCursorOff(void);
(MyImport '("newtCursorOff" "void"))

; void newtCursorOn(void);
(MyImport '("newtCursorOn" "void"))

; void newtGetScreenSize(int * cols, int * rows);
(MyImport '("newtGetScreenSize" "void" "void*" "void*"))

; newtComponent newtCompactButton(int left, int top, const char * text);
(MyImport '("newtCompactButton" "void*" "int" "int" "char*"))

; newtComponent newtButton(int left, int top, const char * text);
(MyImport '("newtButton" "void*" "int" "int" "char*"))

; newtComponent newtLabel(int left, int top, const char * text);
(MyImport '("newtLabel" "void*" "int" "int" "char*"))

; void newtLabelSetText(newtComponent co, const char * text);
(MyImport '("newtLabelSetText" "void" "void*" "char*"))

; void newtLabelSetColors(newtComponent co, int colorset);
(MyImport '("newtLabelSetColors" "void" "void*" "int"))

; newtComponent newtScale(int left, int top, int width, long long fullValue);
(MyImport '("newtScale" "void*" "int" "int" "int" "long long"))

; void newtScaleSet(newtComponent co, unsigned long long amount);
(MyImport '("newtScaleSet" "void" "void*" "long long"))

; void newtScaleSetColors(newtComponent co, int empty, int full);
(MyImport '("newtScaleSetColors" "void" "void*" "int" "int"))

; newtComponent newtListbox(int left, int top, int height, int flags);
(MyImport '("newtListbox" "void*" "int" "int" "int" "int"))

; void * newtListboxGetCurrent(newtComponent co);
(MyImport '("newtListboxGetCurrent" "void*" "void*"))

; void newtListboxSetCurrent(newtComponent co, int num);
(MyImport '("newtListboxSetCurrent" "void" "void*" "int"))

; void newtListboxSetCurrentByKey(newtComponent co, void * key);
(MyImport '("newtListboxSetCurrentByKey" "void" "void*" "void*"))

; void newtListboxSetEntry(newtComponent co, int num, const char * text);
(MyImport '("newtListboxSetEntry" "void" "void*" "int" "char*"))

; void newtListboxSetWidth(newtComponent co, int width);
(MyImport '("newtListboxSetWidth" "void" "void*" "int"))

; void newtListboxSetData(newtComponent co, int num, void * data);
(MyImport '("newtListboxSetData" "void" "void*" "int" "void*"))

; int newtListboxAppendEntry(newtComponent co, const char * text, const void * data);
(MyImport '("newtListboxAppendEntry" "int" "void*" "char*" "void*"))

; int newtListboxInsertEntry(newtComponent co, const char * text, const void * data, void * key);
(MyImport '("newtListboxInsertEntry" "int" "void*" "char*" "void*" "void*"))

; int newtListboxDeleteEntry(newtComponent co, void * data);
(MyImport '("newtListboxDeleteEntry" "int" "void*" "void*"))

; void newtListboxClear(newtComponent co);
(MyImport '("newtListboxClear" "void" "void*"))

; void newtListboxGetEntry(newtComponent co, int num, char **text, void **data);
(MyImport '("newtListboxGetEntry" "void" "void*" "int" "void*" "void*"))

; void **newtListboxGetSelection(newtComponent co, int *numitems);
(MyImport '("newtListboxGetSelection" "void*" "void*" "void*"))

; void newtListboxClearSelection(newtComponent co);
(MyImport '("newtListboxClearSelection" "void" "void*"))

; void newtListboxSelectItem(newtComponent co, const void * key, enum newtFlagsSense sense);
(MyImport '("newtListboxSelectItem" "void" "void*" "void*" "int"))

; int newtListboxItemCount(newtComponent co);
(MyImport '("newtListboxItemCount" "int" "void*"))

; newtComponent newtCheckbox(int left, int top, const char * text, char defValue, const char * seq, char * result);
(MyImport '("newtCheckbox" "void*" "int" "int" "char*" "char" "char*" "void*"))

; char newtCheckboxGetValue(newtComponent co);
(MyImport '("newtCheckboxGetValue" "char" "void*"))

; void newtCheckboxSetValue(newtComponent co, char value);
(MyImport '("newtCheckboxSetValue" "void" "void*" "char"))

; void newtCheckboxSetFlags(newtComponent co, int flags, enum newtFlagsSense sense);
(MyImport '("newtCheckboxSetFlags" "void" "void*" "int" "int"))

; newtComponent newtRadiobutton(int left, int top, const char * text, int isDefault, newtComponent prevButton);
(MyImport '("newtRadiobutton" "void*" "int" "int" "char*" "int" "void*"))

; newtComponent newtRadioGetCurrent(newtComponent setMember);
(MyImport '("newtRadioGetCurrent" "void*" "void*"))

; void newtRadioSetCurrent(newtComponent setMember);
(MyImport '("newtRadioSetCurrent" "void" "void*"))

; newtComponent newtVerticalScrollbar(int left, int top, int height, int normalColorset, int thumbColorset);
(MyImport '("newtVerticalScrollbar" "void*" "int" "int" "int" "int" "int"))

; void newtScrollbarSet(newtComponent co, int where, int total);
(MyImport '("newtScrollbarSet" "void" "void*" "int" "int"))

; void newtScrollbarSetColors(newtComponent co, int normal, int thumb);
(MyImport '("newtScrollbarSetColors" "void" "void*" "int" "int"))

; newtComponent newtEntry(int left, int top, const char * initialValue, int width, const char ** resultPtr, int flags);
(MyImport '("newtEntry" "void*" "int" "int" "char*" "int" "void*" "int"))

; void newtEntrySet(newtComponent co, const char * value, int cursorAtEnd);
(MyImport '("newtEntrySet" "void" "void*" "char*" "int"))

; void newtEntrySetFilter(newtComponent co, newtEntryFilter filter, void * data);
(MyImport '("newtEntrySetFilter" "void" "void*" "void*" "void*"))

; char * newtEntryGetValue(newtComponent co);
(MyImport '("newtEntryGetValue" "char*" "void*"))

; void newtEntrySetFlags(newtComponent co, int flags, enum newtFlagsSense sense);
(MyImport '("newtEntrySetFlags" "void" "void*" "int" "int"))

; void newtEntrySetColors(newtComponent co, int normal, int disabled);
(MyImport '("newtEntrySetColors" "void" "void*" "int" "int"))

; newtComponent newtCheckboxTree(int left, int top, int height, int flags);
(MyImport '("newtCheckboxTree" "void*" "int" "int" "int" "int"))

; newtComponent newtCheckboxTreeMulti(int left, int top, int height, char *seq, int flags);
(MyImport '("newtCheckboxTreeMulti" "void*" "int" "int" "int" "char*" "int"))

; const void ** newtCheckboxTreeGetSelection(newtComponent co, int *numitems);
(MyImport '("newtCheckboxTreeGetSelection" "void*" "void*" "void*"))

; const void * newtCheckboxTreeGetCurrent(newtComponent co);
(MyImport '("newtCheckboxTreeGetCurrent" "void*" "void*"))

; void newtCheckboxTreeSetCurrent(newtComponent co, void * item);
(MyImport '("newtCheckboxTreeSetCurrent" "void" "void*" "void*"))

; const void ** newtCheckboxTreeGetMultiSelection(newtComponent co, int *numitems, char seqnum);
(MyImport '("newtCheckboxTreeGetMultiSelection" "void*" "void*" "void*" "char"))

; int newtCheckboxTreeAddArray(newtComponent co, const char * text, const void * data, int flags, int * indexes);
(MyImport '("newtCheckboxTreeAddArray" "int" "void*" "char*" "void*" "int" "void*"))

; int * newtCheckboxTreeFindItem(newtComponent co, void * data);
(MyImport '("newtCheckboxTreeFindItem" "void*" "void*" "void*"))

; void newtCheckboxTreeSetEntry(newtComponent co, const void * data, const char * text);
(MyImport '("newtCheckboxTreeSetEntry" "void" "void*" "void*" "char*"))

; void newtCheckboxTreeSetWidth(newtComponent co, int width);
(MyImport '("newtCheckboxTreeSetWidth" "void" "void*" "int"))

; char newtCheckboxTreeGetEntryValue(newtComponent co, const void * data);
(MyImport '("newtCheckboxTreeGetEntryValue" "char" "void*" "void*"))

; void newtCheckboxTreeSetEntryValue(newtComponent co, const void * data, char value);
(MyImport '("newtCheckboxTreeSetEntryValue" "void" "void*" "void*" "char"))

; newtComponent newtTextboxReflowed(int left, int top, char * text, int width, int flexDown, int flexUp, int flags);
(MyImport '("newtTextboxReflowed" "void*" "int" "int" "char*" "int" "int" "int" "int"))

; newtComponent newtTextbox(int left, int top, int width, int height, int flags);
(MyImport '("newtTextbox" "void*" "int" "int" "int" "int" "int"))

; void newtTextboxSetText(newtComponent co, const char * text);
(MyImport '("newtTextboxSetText" "void" "void*" "char*"))

; void newtTextboxSetHeight(newtComponent co, int height);
(MyImport '("newtTextboxSetHeight" "void" "void*" "int"))

; int newtTextboxGetNumLines(newtComponent co);
(MyImport '("newtTextboxGetNumLines" "int" "void*"))

; void newtTextboxSetColors(newtComponent co, int normal, int active);
(MyImport '("newtTextboxSetColors" "void" "void*" "int" "int"))

; char * newtReflowText(char * text, int width, int flexDown, int flexUp, int * actualWidth, int * actualHeight);
(MyImport '("newtReflowText" "char*" "char*" "int" "int" "int" "void*" "void*"))

; newtComponent newtForm(newtComponent vertBar, void * helpTag, int flags);
(MyImport '("newtForm" "void*" "void*" "void*" "int"))

; void newtFormSetTimer(newtComponent form, int millisecs);
(MyImport '("newtFormSetTimer" "void" "void*" "int"))

; void newtFormWatchFd(newtComponent form, int fd, int fdFlags);
(MyImport '("newtFormWatchFd" "void" "void*" "int" "int"))

; void newtFormSetSize(newtComponent co);
(MyImport '("newtFormSetSize" "void" "void*"))

; newtComponent newtFormGetCurrent(newtComponent co);
(MyImport '("newtFormGetCurrent" "void*" "void*"))

; void newtFormSetBackground(newtComponent co, int color);
(MyImport '("newtFormSetBackground" "void" "void*" "int"))

; void newtFormSetCurrent(newtComponent co, newtComponent subco);
(MyImport '("newtFormSetCurrent" "void" "void*" "void*"))

; void newtFormAddComponent(newtComponent form, newtComponent co);
(MyImport '("newtFormAddComponent" "void" "void*" "void*"))

; void newtFormSetHeight(newtComponent co, int height);
(MyImport '("newtFormSetHeight" "void" "void*" "int"))

; void newtFormSetWidth(newtComponent co, int width);
(MyImport '("newtFormSetWidth" "void" "void*" "int"))

; newtComponent newtRunForm(newtComponent form);
(MyImport '("newtRunForm" "void*" "void*"))

; void newtFormRun(newtComponent co, struct newtExitStruct * es);
(MyImport '("newtFormRun" "void" "void*" "void*"))

; void newtDrawForm(newtComponent form);
(MyImport '("newtDrawForm" "void" "void*"))

; void newtFormAddHotKey(newtComponent co, int key);
(MyImport '("newtFormAddHotKey" "void" "void*" "int"))

; void newtFormDestroy(newtComponent form);
(MyImport '("newtFormDestroy" "void" "void*"))

; void newtComponentAddCallback(newtComponent co, newtCallback f, void * data);
(MyImport '("newtComponentAddCallback" "void" "void*" "void*" "void*"))

; void newtComponentTakesFocus(newtComponent co, int val);
(MyImport '("newtComponentTakesFocus" "void" "void*" "int"))

; void newtComponentGetPosition(newtComponent co, int * left, int * top);
(MyImport '("newtComponentGetPosition" "void" "void*" "void*" "void*"))

; void newtComponentGetSize(newtComponent co, int * width, int * height);
(MyImport '("newtComponentGetSize" "void" "void*" "void*" "void*"))

; void newtComponentAddDestroyCallback(newtComponent co, newtCallback f, void * data);
(MyImport '("newtComponentAddDestroyCallback" "void" "void*" "void*" "void*"))

; void newtComponentDestroy(newtComponent co);
(MyImport '("newtComponentDestroy" "void" "void*"))

; newtGrid newtCreateGrid(int cols, int rows);
(MyImport '("newtCreateGrid" "void*" "int" "int"))

; newtGrid newtGridBasicWindow(newtComponent text, newtGrid middle, newtGrid buttons);
(MyImport '("newtGridBasicWindow" "void*" "void*" "void*" "void*"))

; newtGrid newtGridSimpleWindow(newtComponent text, newtComponent middle, newtGrid buttons);
(MyImport '("newtGridSimpleWindow" "void*" "void*" "void*" "void*"))

; void newtGridSetField(newtGrid grid, int col, int row, enum newtGridElement type, void * val, int padLeft, int padTop, int padRight, int padBottom, int anchor, int flags);
(MyImport '("newtGridSetField" "void" "void*" "int" "int" "int" "void*" "int" "int" "int" "int" "int" "int"))

; void newtGridPlace(newtGrid grid, int left, int top);
(MyImport '("newtGridPlace" "void" "void*" "int" "int"))

; void newtGridFree(newtGrid grid, int recurse);
(MyImport '("newtGridFree" "void" "void*" "int"))

; void newtGridGetSize(newtGrid grid, int * width, int * height);
(MyImport '("newtGridGetSize" "void" "void*" "void*" "void*"))

; void newtGridWrappedWindow(newtGrid grid, char * title);
(MyImport '("newtGridWrappedWindow" "void" "void*" "char*"))

; void newtGridWrappedWindowAt(newtGrid grid, char * title, int left, int top);
(MyImport '("newtGridWrappedWindowAt" "void" "void*" "char*" "int" "int"))

; void newtGridAddComponentsToForm(newtGrid grid, newtComponent form, int recurse);
(MyImport '("newtGridAddComponentsToForm" "void" "void*" "void*" "int"))

; Currently unmapped to newLISP:
; #define newtGridDestroy newtGridFree
; #define NEWT_KEY_RETURN			NEWT_KEY_ENTER
; #define NEWT_LISTBOX_RETURNEXIT NEWT_FLAG_RETURNEXIT
; #define NEWT_ENTRY_SCROLL	NEWT_FLAG_SCROLL
; #define NEWT_ENTRY_HIDDEN	NEWT_FLAG_HIDDEN
; #define NEWT_ENTRY_RETURNEXIT	NEWT_FLAG_RETURNEXIT
; #define NEWT_ENTRY_DISABLED	NEWT_FLAG_DISABLED
; #define NEWT_TEXTBOX_WRAP	NEWT_FLAG_WRAP
; #define NEWT_TEXTBOX_SCROLL	NEWT_FLAG_SCROLL
; #define NEWT_FORM_NOF12		NEWT_FLAG_NOF12
; #define newtListboxAddEntry	newtListboxAppendEntry
; typedef struct newtComponent_struct * newtComponent;
; typedef struct grid_s * newtGrid;
; newtGrid newtButtonBarv(char * button1, newtComponent * b1comp, va_list args);
; newtGrid newtButtonBar(char * button1, newtComponent * b1comp, ...);
; void newtWinMessage(char * title, char * buttonText, char * text, ...);
; void newtWinMessagev(char * title, char * buttonText, char * text, va_list argv);
; int newtWinChoice(char * title, char * button1, char * button2, char * text, ...);
; int newtWinTernary(char * title, char * button1, char * button2, char * button3, char * message, ...);
; int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown, int flexUp, int maxListHeight, char ** items, int * listItem, char * button1, ...);
; int newtWinEntries(char * title, char * text, int suggestedWidth, int flexDown, int flexUp, int dataWidth, struct newtWinEntry * items, char * button1, ...);
; struct newtWinEntry {
;     char * text;
;     char ** value;
;     int flags;
; };
; newtGrid newtGridVStacked(enum newtGridElement type, void * what, ...);
; newtGrid newtGridVCloseStacked(enum newtGridElement type, void * what, ...);
; newtGrid newtGridHStacked(enum newtGridElement type1, void * what1, ...);
; newtGrid newtGridHCloseStacked(enum newtGridElement type1, void * what1, ...);
; int newtCheckboxTreeAddItem(newtComponent co, const char * text, const void * data, int flags, int index, ...);
; void newtFormAddComponents(newtComponent form, ...);

(context 'MAIN)

Code: Select all

(load "newt.lsp")

(Newt:Init)

(Newt:Cls)

(Newt:CenteredWindow 30 10 "Hello World")

(set 'button (Newt:Button 8 3 "Click Me!"))

(set 'form (Newt:Form 0 0 0))

(set 'es (pack Newt:ExitStruct 0 0))

(Newt:FormAddComponent form button)

(Newt:FormRun form es)

(Newt:FormDestroy form)

(Newt:PopWindow)

(Newt:Finished)

(exit 0)

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: NEWT C interface module

Post by TedWalther »

Very nice, I hope this gets included as a standard module in newlisp!
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

ryuo
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: NEWT C interface module

Post by ryuo »

Indeed. I'm working on an object layer for the widgets using the FOOP system. I'm also seeing if I can get NEWT to work on Windows at all. The library it uses for console control has a windows port, but I'm not sure if NEWT will work on a non-POSIX platform.

Darth.Severus
Posts: 14
Joined: Mon Sep 17, 2012 3:28 am

Re: NEWT C interface module

Post by Darth.Severus »

First, many thanks for your work an sharing your code.

I changed the code of your module to be more flexible. It finds the right name of the installed version of libnewt.so and uses this one. I think, this is somehow like the use of libraries should be implemented in such modules.

Code: Select all

(context 'Newt)

	(define (find-libnewt) 
		(if (!= (setq result-of-find (exec "find /usr/lib -name *libnewt*")) '()) 
			((exec (string "basename " (last result-of-find)))0) 
			(throw-error "libnewt not found in /usr/lib! You might need to install libnewt.")))

    (constant 'Prefix "newt")

    (constant 'Library (find-libnewt))
However I got this error message trying your example:
ERR: import function not found in function import : "/usr/lib/i386-linux-gnu/libnewt.so.0.52.14: undefined symbol: newtComponentGetPosition"
called from user function module
My guess is, I'm using another libnewt library.

Locked