#include <suntool/sunview.h>
#include <suntool/canvas.h>
#include <stdio.h>
#include "textwindow.h"


#define MAX(a, b)  ((a) < (b) ? (b) : (a))
#define MIN(a, b)  ((a) > (b) ? (b) : (a))


extern char   *malloc();


    
Textwindow   *textwindow_create(canvas, font,
				charwidth, charheight,
				width, height)
Canvas    canvas;
Pixfont   *font;
int       charwidth;
int       charheight;
int       width;
int       height;
{
    Textwindow   *win;
    int          i;

    win = (Textwindow *) malloc(sizeof(Textwindow));
    if (win == NULL) {
	fprintf(stderr, "Couldn't allocate space for a textwindow.");
	exit(1);
    }

    win->canvas = canvas;
    win->font   = font;
    win->charheight = charheight;
    win->charwidth = charwidth;
    win->height = height;
    win->width = width;
    win->cur_row = 0;
    win->cur_col = 0;

    win->buffer = malloc((height + 1) * (width + 1));
    for (i = 0; i < (height + 1) * (width + 1); ++i)
	*(win->buffer + i) =  (i % (width + 1) != width)  ? ' ' : '\0';

    win->scroll_begin = 0;
    win->scroll_end = height - 1;

    return win;
}




void   textwindow_scroll(win)
Textwindow   *win;
{
    int   row;
    int   i;
    
    for (row = win->scroll_begin; row < win->scroll_end; ++row) {
	strncpy(win->buffer + (win->width + 1) * row,
		win->buffer + (win->width + 1) * (row + 1),
		win->width);
	pw_text(canvas_pixwin(win->canvas),
		0, -1 + (row + 1) * win->charheight, PIX_SRC,
		win->font, win->buffer + (win->width + 1) * row);
    }
    for (i = 0; i < win->width; ++i)
	*(win->buffer + win->scroll_end * (win->width + 1) + i) = ' ';
    pw_text(canvas_pixwin(win->canvas),
	    0, -1 + (win->scroll_end + 1) * win->charheight,
	    PIX_SRC, win->font,
	    win->buffer + win->scroll_end * (win->width + 1));
}



void   textwindow_print(win, str)
Textwindow   *win;
char         *str;
{
    while (*str) {
	if (*str == '\n') {
	    win->cur_col = 0;
	    if (win->cur_row++ == win->scroll_end) {
		--win->cur_row;
		textwindow_scroll(win);
	    }
	    ++str;
	} else {
	    *(win->buffer + win->cur_row * (win->width + 1) + win->cur_col)
		= *str;
	    pw_char(canvas_pixwin(win->canvas),
		    win->cur_col++ * win->charwidth,
		    -1 + (win->cur_row + 1) * win->charheight,
		    PIX_SRC, win->font, *str++);
	    if (win->cur_col == win->width) {
		win->cur_col = 0;
		if (win->cur_row++ == win->scroll_end) {
		    --win->cur_row;
		    textwindow_scroll(win);
		}
	    }
	}
    }
}



void   textwindow_putcur(win, row, col)
Textwindow   *win;
int          row;
int          col;
{
    win->cur_row = MIN(row, win->height - 1);
    win->cur_col = MIN(col, win->width - 1);
}



void    textwindow_set_scrollregion(win, begin, end)
Textwindow   *win;
int          begin;
int          end;
{
    int   temp;

    if (begin > end) {
	temp = begin;
	begin = end;
	end = temp;
    }

    begin = MAX(begin, 0);
    end = MIN(end, win->height - 1);
    
    win->scroll_begin = MIN(begin, win->height - 1);
    win->scroll_end = MAX(end, 0);
}
