From 892661d66bc215875d66b866731c4c263b292744 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 19 Oct 2016 21:02:39 -0400 Subject: [PATCH] reworked color handling and moved status line handling from screen.c to xedit.c --- edit.h | 38 +++++++++++++++-------------- screen.c | 36 +++++++++------------------ xedit.c | 74 +++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 77 insertions(+), 71 deletions(-) diff --git a/edit.h b/edit.h index 674e49b..b6d013c 100644 --- a/edit.h +++ b/edit.h @@ -203,29 +203,29 @@ void screen_getsize(unsigned* nrows, unsigned* ncols); Row* screen_getrow(unsigned row); void screen_clearrow(unsigned row); unsigned screen_setcell(unsigned row, unsigned col, Rune r); -void screen_status(char* fmt, ...); UGlyph* screen_getglyph(unsigned row, unsigned col, unsigned* scrwidth); /* Color Scheme Handling *****************************************************************************/ /* color indexes for the colorscheme */ enum ColorId { - CLR_BASE03 = 0, - CLR_BASE02, - CLR_BASE01, - CLR_BASE00, - CLR_BASE0, - CLR_BASE1, - CLR_BASE2, - CLR_BASE3, - CLR_YELLOW, - CLR_ORANGE, - CLR_RED, - CLR_MAGENTA, - CLR_VIOLET, - CLR_BLUE, - CLR_CYAN, - CLR_GREEN, + CLR_BASE03 = 0, + CLR_BASE02 = 1, + CLR_BASE01 = 2, + CLR_BASE00 = 3, + CLR_BASE0 = 4, + CLR_BASE1 = 5, + CLR_BASE2 = 6, + CLR_BASE3 = 7, + CLR_YELLOW = 8, + CLR_ORANGE = 9, + CLR_RED = 10, + CLR_MAGENTA = 11, + CLR_VIOLET = 12, + CLR_BLUE = 13, + CLR_CYAN = 14, + CLR_GREEN = 15, + CLR_COUNT = 16 }; /* Represents an ARGB color value */ @@ -244,6 +244,8 @@ extern enum ColorScheme ColorBase; extern Buf Buffer; extern unsigned CursorPos; extern unsigned TargetCol; +extern unsigned DotBeg; +extern unsigned DotEnd; /* Configuration *****************************************************************************/ @@ -256,7 +258,7 @@ enum { MaxFonts = 16 /* maximum number of fonts to cache */ }; -static const Color Palette[][2] = { +static const Color ColorScheme[CLR_COUNT][2] = { /* Color Name = Dark Light */ [CLR_BASE03] = { 0x002b36, 0xfdf6e3 }, [CLR_BASE02] = { 0x073642, 0xeee8d5 }, diff --git a/screen.c b/screen.c index 92f61b2..7fbe46d 100644 --- a/screen.c +++ b/screen.c @@ -5,8 +5,8 @@ static unsigned NumCols = 0; static Row** Rows; static void screen_reflow(Buf* buf) { - unsigned pos = Rows[1]->off; - for (unsigned y = 1; y < NumRows; y++) { + unsigned pos = Rows[0]->off; + for (unsigned y = 0; y < NumRows; y++) { screen_clearrow(y); screen_getrow(y)->off = pos; for (unsigned x = 0; x < NumCols;) { @@ -128,20 +128,20 @@ static void scroll_up(Buf* buf, unsigned csr, unsigned first) { prevln = prev_screen_line(buf, prevln, first); /* delete the last row and shift the others */ free(Rows[NumRows - 1]); - memmove(&Rows[2], &Rows[1], sizeof(Row*) * (NumRows-2)); - Rows[1] = calloc(1, sizeof(Row) + (NumCols * sizeof(UGlyph))); - Rows[1]->off = prevln; + memmove(&Rows[1], &Rows[0], sizeof(Row*) * (NumRows-1)); + Rows[0] = calloc(1, sizeof(Row) + (NumCols * sizeof(UGlyph))); + Rows[0]->off = prevln; /* fill in row content */ - fill_row(buf, 1, Rows[1]->off); - first = Rows[1]->off; + fill_row(buf, 0, Rows[0]->off); + first = Rows[0]->off; } } static void scroll_dn(Buf* buf, unsigned csr, unsigned last) { while (csr > last) { /* delete the first row and shift the others */ - free(Rows[1]); - memmove(&Rows[1], &Rows[2], sizeof(Row*) * (NumRows-2)); + free(Rows[0]); + memmove(&Rows[0], &Rows[1], sizeof(Row*) * (NumRows-1)); Rows[NumRows-1] = calloc(1, sizeof(Row) + (NumCols * sizeof(UGlyph))); Rows[NumRows-1]->off = (Rows[NumRows-2]->off + Rows[NumRows-2]->rlen); /* fill in row content */ @@ -151,7 +151,7 @@ static void scroll_dn(Buf* buf, unsigned csr, unsigned last) { } static void sync_view(Buf* buf, unsigned csr) { - unsigned first = Rows[1]->off; + unsigned first = Rows[0]->off; unsigned last = Rows[NumRows-1]->off + Rows[NumRows-1]->rlen - 1; if (csr < first) { scroll_up(buf, csr, first); @@ -166,7 +166,7 @@ void screen_update(Buf* buf, unsigned csr, unsigned* csrx, unsigned* csry) { if (buf->insert_mode) screen_reflow(buf); /* find the cursor on the new screen */ - for (unsigned y = 1; y < NumRows; y++) { + for (unsigned y = 0; y < NumRows; y++) { unsigned start = Rows[y]->off; unsigned end = Rows[y]->off + Rows[y]->rlen - 1; if (start <= csr && csr <= end) { @@ -182,17 +182,3 @@ void screen_update(Buf* buf, unsigned csr, unsigned* csrx, unsigned* csry) { } } } - -void screen_status(char* fmt, ...) { - char buffer[NumCols+1]; - memset(buffer, 0, NumCols+1); - va_list args; - va_start(args, fmt); - vsnprintf(buffer, NumCols, fmt, args); - va_end(args); - screen_clearrow(0); - Rows[0]->len = NumCols; - Rows[0]->rlen = NumCols; - for (unsigned i = 0; buffer[i] && i < NumCols; i++) - Rows[0]->cols[i].rune = (Rune)buffer[i]; -} diff --git a/xedit.c b/xedit.c index 7c1ee3d..d45d6de 100644 --- a/xedit.c +++ b/xedit.c @@ -7,10 +7,18 @@ #include "edit.h" +#define BKGCLR (clr(CLR_BASE03)) +#define GTRCLR (clr(CLR_BASE02)) +#define CSRCLR (clr(CLR_BASE3)) +#define TXTCLR (clr(CLR_BASE0)) + Buf Buffer; unsigned CursorPos = 0; unsigned TargetCol = 0; +unsigned DotBeg = 0; +unsigned DotEnd = 0; enum ColorScheme ColorBase = DEFAULT_COLORSCHEME; +XftColor Palette[CLR_COUNT][2]; struct { Display* display; Visual* visual; @@ -142,15 +150,16 @@ int font_makespecs(XftGlyphFontSpec* specs, const UGlyph* glyphs, int len, int x /*****************************************************************************/ -static XftColor xftcolor(enum ColorId cid) { - Color c = Palette[cid][ColorBase]; - XftColor xc; - xc.color.red = 0xFF | ((c & 0x00FF0000) >> 8); - xc.color.green = 0xFF | ((c & 0x0000FF00)); - xc.color.blue = 0xFF | ((c & 0x000000FF) << 8); - xc.color.alpha = UINT16_MAX; - XftColorAllocValue(X.display, X.visual, X.colormap, &xc.color, &xc); - return xc; +static void xftcolor(XftColor* xc, Color c) { + xc->color.red = 0xFF | ((c & 0x00FF0000) >> 8); + xc->color.green = 0xFF | ((c & 0x0000FF00)); + xc->color.blue = 0xFF | ((c & 0x000000FF) << 8); + xc->color.alpha = UINT16_MAX; + XftColorAllocValue(X.display, X.visual, X.colormap, &(xc->color), xc); +} + +XftColor* clr(int id) { + return &(Palette[id][ColorBase]); } static void deinit(void) { @@ -209,6 +218,12 @@ static int init(void) { X.pixmap = XCreatePixmap(X.display, X.window, Width, Height, X.depth); X.xft = XftDrawCreate(X.display, X.pixmap, X.visual, X.colormap); + /* initialize the color pallette */ + for (int i = 0; i < CLR_COUNT; i++) { + xftcolor(&Palette[i][LIGHT], ColorScheme[i][LIGHT]); + xftcolor(&Palette[i][DARK], ColorScheme[i][DARK]); + } + return XConnectionNumber(X.display); } @@ -294,7 +309,7 @@ static void handle_event(XEvent* e) { X.xft = XftDrawCreate(X.display, X.pixmap, X.visual, X.colormap); screen_setsize( &Buffer, - X.height / Fonts.base.height, + X.height / Fonts.base.height - 1, X.width / Fonts.base.width); } break; @@ -308,17 +323,24 @@ void draw_runes(unsigned x, unsigned y, XftColor* fg, XftColor* bg, UGlyph* glyp XftDrawGlyphFontSpec(X.xft, fg, specs, nspecs); } -static void redraw(void) { - /* Allocate the colors */ - XftColor bkgclr = xftcolor(CLR_BASE03); - XftColor gtrclr = xftcolor(CLR_BASE02); - XftColor csrclr = xftcolor(CLR_BASE3); - XftColor txtclr = xftcolor(CLR_BASE0); +static void draw_status(XftColor* fg, unsigned ncols) { + UGlyph glyphs[ncols]; + UGlyph* status = glyphs; + (status++)->rune = (Buffer.charset == BINARY ? 'B' : 'U'); + (status++)->rune = (Buffer.crlf ? 'C' : 'N'); + (status++)->rune = (Buffer.modified ? '*' : ' '); + (status++)->rune = ' '; + char* path = Buffer.path; + while(*path) + (status++)->rune = *path++; + draw_runes(0, 0, fg, NULL, glyphs, status - glyphs); +} +static void redraw(void) { /* draw the background colors */ - XftDrawRect(X.xft, &bkgclr, 0, 0, X.width, X.height); - XftDrawRect(X.xft, >rclr, 79 * Fonts.base.width, 0, Fonts.base.width, X.height); - XftDrawRect(X.xft, &txtclr, 0, 0, X.width, Fonts.base.height); + XftDrawRect(X.xft, BKGCLR, 0, 0, X.width, X.height); + XftDrawRect(X.xft, GTRCLR, 79 * Fonts.base.width, 0, Fonts.base.width, X.height); + XftDrawRect(X.xft, TXTCLR, 0, 0, X.width, Fonts.base.height); /* update the screen buffer and retrieve cursor coordinates */ unsigned csrx, csry; @@ -327,24 +349,20 @@ static void redraw(void) { /* flush the screen buffer */ unsigned nrows, ncols; screen_getsize(&nrows, &ncols); - screen_status(" %s %c %s", - (Buffer.charset == BINARY ? "BIN" : "UTF-8"), - (Buffer.modified ? '*' : ' '), - Buffer.path - ); + draw_status(BKGCLR, ncols); for (unsigned y = 0; y < nrows; y++) { Row* row = screen_getrow(y); - draw_runes(0, y, (y == 0 ? &bkgclr : &txtclr), NULL, row->cols, row->len); + draw_runes(0, y+1, TXTCLR, NULL, row->cols, row->len); } /* Place cursor on screen */ unsigned rwidth; UGlyph* csrrune = screen_getglyph(csry, csrx, &rwidth); if (Buffer.insert_mode) { - XftDrawRect(X.xft, &csrclr, csrx * Fonts.base.width, csry * Fonts.base.height, 2, Fonts.base.height); + XftDrawRect(X.xft, CSRCLR, csrx * Fonts.base.width, (csry+1) * Fonts.base.height, 1, Fonts.base.height); } else { - XftDrawRect(X.xft, &csrclr, csrx * Fonts.base.width, csry * Fonts.base.height, rwidth * Fonts.base.width, Fonts.base.height); - draw_runes(csrx, csry, &bkgclr, NULL, csrrune, 1); + XftDrawRect(X.xft, CSRCLR, csrx * Fonts.base.width, (csry+1) * Fonts.base.height, rwidth * Fonts.base.width, Fonts.base.height); + draw_runes(csrx, csry+1, BKGCLR, NULL, csrrune, 1); } /* flush pixels to the screen */ -- 2.49.0