/* Text buffer code V1.10 16/4/04
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   WOUM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _TEXTBUF_H
#define _TEXTBUF_H

typedef struct _textbuf {
	int w,h; /* Current width & height (exclusive) */
	int minh,maxh; /* Minimum & maximum height (exclusive) */
	int cx,cy; /* Cursor pos */
	int nl; /* Character code to use for newline, or 0 for cr & lf */
	char **b; /* Pointer to array of row pointers */
	/* These can be for update callbacks to occur (preferably all or none should be set) */
	void (*redraw)(struct _textbuf *t,int minx,int miny,int maxx,int maxy); /* Inclusive-exclusive redraw info */
	void (*resize)(struct _textbuf *t); /* Size has changed */
	void (*scroll)(struct _textbuf *t); /* Scroll the display up 1 line */
} textbuf;


textbuf *textbuf_new(int w,int h,int minh,int maxh,int newline);
/* Create a new buffer, with a current size of (w,h) (exclusive) and a min/max
   sizes of minh, maxh (inclusive). newline is the newline character to use, or
   0 for just cr and lf on their own.
   Returns 0 on failure. */

void textbuf_kill(textbuf *t);
/* Destroy the buffer */


void textbuf_setcur(textbuf *t,int x,int y);
/* Set the cursor to x,y, bounding it to the current size */

void textbuf_writec(textbuf *t,int c);
/* Write character 'c'. Handles ASCII control chars 127 (delete), 13 (carriage
   return), and 10 (form feed). All other chars less than 32 are ignored (while those equal to or above are drawn) */

void textbuf_write(textbuf *t,int nchars,char *chars);
/* Writes a string of specified length, using the same processing as
   textbuf_writec */
   
int textbuf_clear(textbuf *t,int h);
/* Clear the buffer and resize to minimum size, keeping h lines of text. The
   cursor will be on the bottom row, with the text directly above it.
   Returns !0 on failure */

int textbuf_resize(textbuf *t,int w,int h);
/* Resize to a given width and height, shifting existing data so that the cursor
   is at the bottom line and as many lines above as possible are kept.
   Returns !0 on failure */

char *textbuf_getline(textbuf *t,int y);
/* Return a pointer to the line data for y pos 'y', bounding y to the current
   size */

char textbuf_getchar(textbuf *t,int x,int y);
/* Return the character at x,y, bounding x and y to the current size */

#endif
