#include "stdwin.h" TEXTEDIT *tealloc(WINDOW *win, int left, int top, int width); TEXTEDIT *tecreate(WINDOW *win, int left, int top, int right, int bottom); void tefree(TEXTEDIT *tp); void tedestroy(TEXTEDIT *tp); void tedraw(TEXTEDIT *tp); void tedrawnew(TEXTEDIT *tp, int left, int top, int right, int bottom); void temove(TEXTEDIT *tp, int left, int top, int width); void temovenew(TEXTEDIT *tp, int left, int top, int right, int bottom); void tesetfocus(TEXTEDIT *tp, int foc1, int foc2); void tereplace(TEXTEDIT *tp, char *str); void tesetbuf(TEXTEDIT *tp, char *buf, int buflen); void tearrow(TEXTEDIT *tp, int code); void tebackspace(TEXTEDIT *tp); bool teclicknew(TEXTEDIT *tp, int h, int v, bool extend); bool tedoubleclick(TEXTEDIT *tp, int h, int v); bool teevent(TEXTEDIT *tp, EVENT *ep); #define teclick(tp, h, v) teclicknew(tp, h, v, FALSE) #define teclickextend(tp, h, v) teclicknew(tp, h, v, TRUE) char *tegettext(TEXTEDIT *tp); int tegetlen(TEXTEDIT *tp); int tegetnlines(TEXTEDIT *tp); int tegetfoc1(TEXTEDIT *tp); int tegetfoc2(TEXTEDIT *tp); int tegetleft(TEXTEDIT *tp); int tegettop(TEXTEDIT *tp); int tegetright(TEXTEDIT *tp); int tegetbottom(TEXTEDIT *tp); int wdrawpar(int h, int v, char *text, int width); int wparheight(char *text, int width);
A textedit area need not occupy an entire window, and there can be more than one textedit area in a given window. Consequently, textedit areas are not resized automatically when their window is resized, and the document size is not set automatically when the textedit area's size changes. If you want to use a single textedit area in a window, and want them to be coupled more tightly, use the editwin package instead.
The functions wdrawpar and wparheight draw a paragraph of text exactly like the textedit routines would draw it, without the need to allocate a textedit data structure for it. Wdrawpar returns the ``v'' coordinate of the bottom of the paragraph drawn, and wparheight returns its height.
Available routines are:
#include "stdwin.h" WINDOW *win; TEXTEDIT *tp; void drawproc(win, left, top, right, bottom) WINDOW *win; { tedrawnew(tp,left, top, right, bottom); } main() { winit(); win= wopen("Hello world", &drawproc); tp= tecreate(win, 20, 20, 200, 200); tereplace(tp, "Guido is gek"); for (;;) { EVENT e; wgetevent(&e); if (teevent(tp, &e)) continue; /* Event has been handled */ if (e.type == WE_COMMAND && e.u.command == WC_CLOSE) break; } wdone(); exit(0); }
Tesetbuf ``gives the buffer away.'' That is, you should have allocated the buffer using malloc(3), but you shouldn't call free to get rid of it - a pointer to the buffer is incorporated in the textedit data structures, and it will be freed later by tefree. Don't pass a buffer that wasn't allocated through malloc(3)!
Tegettext returns a pointer to the internal buffer used to represent the text. Subsequent calls to textedit routines that modify the buffer may invalidate this pointer. You shouldn't modify the text found there. To get the text selected in the focus, copy the substring found between positions tegetfoc1 and tegetfoc2, for example:
/* Copy focus text into buffer, whose size is len */ getfocus(tp, buf, len) TEXTEDIT *tp; char *buf; { int f1= tegetfoc1(tp), f2= tegetfoc2(tp); char *text= tegettext(tp); if (f2-f1 < len) len= f2-f1+1; strncpy(buf, len-1, text+f1); buf[len-1]= ' '; }