mudgangster

Log | Files | Refs

commit bb745abb48f995f607edcc8a0d9a5d071bfcd671
parent eae0440dfb4fb31bcbb199d9bfa33711b8f2a839
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed,  7 May 2014 22:54:45 +0100

Double buffered textboxes

Diffstat:
src/common.h | 1+
src/textbox.c | 19++++++++++++++-----
src/ui.c | 4++--
3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/common.h b/src/common.h @@ -23,6 +23,7 @@ struct int width; int height; + int depth; int hasFocus; } UI; diff --git a/src/textbox.c b/src/textbox.c @@ -121,13 +121,19 @@ void textbox_newline( TextBox* self ) void textbox_draw( TextBox* self ) { + if( self->width == 0 || self->height == 0 ) { + return; + } + + Pixmap doublebuf = XCreatePixmap( UI.display, UI.window, self->width, self->height, UI.depth ); + XSetForeground( UI.display, UI.gc, Style.bg ); - XFillRectangle( UI.display, UI.window, UI.gc, self->x, self->y, self->width, self->height ); + XFillRectangle( UI.display, doublebuf, UI.gc, 0, 0, self->width, self->height ); int rowsRemaining = self->rows; int linesRemaining = self->numLines - self->scrollDelta + 1; - int lineTop = self->y + self->height; + int lineTop = self->height; int linesPrinted = 0; int firstLine = self->head + self->numLines - self->scrollDelta; @@ -166,12 +172,12 @@ void textbox_draw( TextBox* self ) { int charsToPrint = MIN( remainingChars, self->cols - col ); - int x = self->x + ( Style.font.width * col ); + int x = ( Style.font.width * col ); int y = lineTop + ( ( Style.font.height + SPACING ) * row ); - if( y >= self->y ) + if( y >= 0 ) { - XDrawString( UI.display, UI.window, UI.gc, x, y + Style.font.ascent + SPACING, node->buffer + pos, charsToPrint ); + XDrawString( UI.display, doublebuf, UI.gc, x, y + Style.font.ascent + SPACING, node->buffer + pos, charsToPrint ); } pos += charsToPrint; @@ -197,4 +203,7 @@ void textbox_draw( TextBox* self ) linesPrinted++; } + + XCopyArea( UI.display, doublebuf, UI.window, UI.gc, 0, 0, self->width, self->height, self->x, self->y ); + XFreePixmap( UI.display, doublebuf ); } diff --git a/src/ui.c b/src/ui.c @@ -407,7 +407,7 @@ void ui_init() UI.height = -1; Window root = XRootWindow( UI.display, UI.screen ); - int depth = XDefaultDepth( UI.display, UI.screen ); + UI.depth = XDefaultDepth( UI.display, UI.screen ); Visual* visual = XDefaultVisual( UI.display, UI.screen ); UI.colorMap = XDefaultColormap( UI.display, UI.screen ); @@ -428,7 +428,7 @@ void ui_init() .colormap = UI.colorMap, }; - UI.window = XCreateWindow( UI.display, root, 0, 0, 800, 600, 0, depth, InputOutput, visual, CWBackPixel | CWEventMask | CWColormap, &attr ); + UI.window = XCreateWindow( UI.display, root, 0, 0, 800, 600, 0, UI.depth, InputOutput, visual, CWBackPixel | CWEventMask | CWColormap, &attr ); UI.gc = XCreateGC( UI.display, UI.window, 0, NULL ); XWMHints* hints = XAllocWMHints();