From 25bb787f08688b1f1fd7d94bc8ef87ca9e4b7029 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 29 Jul 2019 11:35:10 -0400 Subject: [PATCH] Prepare for per-cell configurable colors So far, this jst consists of initializing color in curses mode, and making the display start and end part of intializing and freeing a sheet (so that it can control allocating the palette, for example, where the data structure used depends on what kind of display it is. Next up will be to allocate and destroy the color palette, and set up the default colors for cells to use (0 for foreground, TEAPOT_WHITE for background.) The outline beyond that is to allow setting of the cell colors, then actually display those colors, and finally edit the palette. --- src/common/cell.h | 8 +++++++- src/common/main.c | 35 +++++++++++++---------------------- src/common/main.h | 7 ++++--- src/common/sheet.c | 10 ++++++++++ src/common/sheet.h | 6 ++++-- src/display.c | 11 ++++++++++- src/fteapot.fl | 2 +- 7 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/common/cell.h b/src/common/cell.h index 3b4fd80..80fd520 100644 --- a/src/common/cell.h +++ b/src/common/cell.h @@ -10,7 +10,11 @@ extern "C" { typedef enum { LEFT=0, RIGHT=1, CENTER=2, AUTOADJUST=3 } Adjust; typedef enum { BASE=0, ITERATIVE=1, CONTINGENT=2 } ContentVariety; - + +typedef unsigned char ColorNum; + +#define MAX_MAX_COLORS UCHAR_MAX; + typedef struct { Token **contents[CONTINGENT]; @@ -19,6 +23,8 @@ typedef struct Token resvalue; Adjust adjust; int precision; + ColorNum foreground; + ColorNum background; unsigned int updated:1; unsigned int shadowed:1; unsigned int scientific:1; diff --git a/src/common/main.c b/src/common/main.c index 0c96e00..074b915 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -46,13 +46,14 @@ int getopt(int argc, char * const *argv, const char *optstring); /* variables */ /*{{{*/ char helpfile[PATH_MAX]; -int batch=0; +bool batch = false; unsigned int batchln=0; -int def_precision=DEF_PRECISION; -int quote=0; -int header=1; -int debug_level=0; -static int usexdr=1; +int def_precision = DEF_PRECISION; +bool quote = false; +bool header = true; +bool always_redraw = false; +int debug_level = 0; +static bool usexdr = true; /*}}}*/ void moveto(Sheet *sheet, int x, int y, int z) @@ -1801,7 +1802,6 @@ int main(int argc, char *argv[]) Sheet sheet,*cursheet; int o; const char *loadfile; - int always_redraw=0; char ln[1024]; /*}}}*/ @@ -1814,29 +1814,29 @@ int main(int argc, char *argv[]) /* a -- use ascii as default */ /*{{{*/ case 'a': { - usexdr=0; + usexdr = false; break; } /*}}}*/ /* b -- run batch */ /*{{{*/ - case 'b': batch=1; break; + case 'b': batch = true; break; /*}}}*/ /* d -- increase debug level */ /*{{{*/ case 'd': ++debug_level; break; /*}}}*/ /* n -- no quoted strings */ /*{{{*/ - case 'n': quote=0; break; + case 'n': quote = false; break; /*}}}*/ /* q -- force quoted strings */ /*{{{*/ - case 'q': quote=1; break; + case 'q': quote = true; break; /*}}}*/ /* H -- no row/column headers */ /*{{{*/ - case 'H': header=0; break; + case 'H': header = false; break; /*}}}*/ /* r -- always redraw */ /*{{{*/ case 'r': { - always_redraw=1; + always_redraw = true; break; } /*}}}*/ @@ -1878,12 +1878,6 @@ int main(int argc, char *argv[]) /*}}}*/ cursheet = &sheet; initialize_sheet(cursheet); - /* start display */ /*{{{*/ - if (!batch) { - display_init(cursheet, always_redraw); - line_msg((const char*)0,""); - } - /*}}}*/ if (loadfile) /* load given sheet */ /*{{{*/ { const char *msg; @@ -1967,10 +1961,7 @@ int main(int argc, char *argv[]) } /*}}}*/ else /* process interactive input */ /*{{{*/ - { display_main(cursheet); - display_end(); - } /*}}}*/ freesheet(cursheet,1); fclose(stdin); diff --git a/src/common/main.h b/src/common/main.h index 6ad975e..17da764 100644 --- a/src/common/main.h +++ b/src/common/main.h @@ -11,11 +11,12 @@ extern "C" { #endif -extern int batch; +extern bool batch; extern int def_precision; -extern int quote; -extern int header; +extern bool quote; +extern bool header; extern int debug_level; +extern bool always_redraw; extern unsigned int batchln; /* A variable of type Key is either a special symbol from this enum, representing diff --git a/src/common/sheet.c b/src/common/sheet.c index f3f5dd8..88ab92f 100644 --- a/src/common/sheet.c +++ b/src/common/sheet.c @@ -253,6 +253,12 @@ void initialize_sheet(Sheet *sheet) { sheet->moveonly = 0; sheet->clk = 0; (void)memset(sheet->labelcache, 0, sizeof(sheet->labelcache)); + sheet->max_colors = MAX_MAX_COLORS; + /* start display of this sheet */ + if (!batch) { + display_init(sheet, always_redraw); + line_msg((const char*)0,""); + } } /*}}}*/ @@ -425,6 +431,10 @@ void freesheet(Sheet *sheet, int all) if (sheet->sheet) free(sheet->sheet); if (sheet->column) free(sheet->column); if (sheet->name) free(sheet->name); + if (!batch) + { + display_end(sheet); + } } else { diff --git a/src/common/sheet.h b/src/common/sheet.h index 8aab356..0ade666 100644 --- a/src/common/sheet.h +++ b/src/common/sheet.h @@ -37,7 +37,6 @@ typedef struct struct Label *labelcache[LABEL_CACHE]; Location cur; Location mark1, mark2; - MarkState marking; int offx, offy; Cell **sheet; int *column; @@ -45,10 +44,13 @@ typedef struct int orix, oriy, maxx, maxy; int width; char *name; + void *display; + int max_colors; + void *palette; + MarkState marking; unsigned int changed:1; unsigned int moveonly:1; unsigned int clk:1; - void *display; } Sheet; #define LOC_WITHINC(s,x,y,z) (xdimx && ydimy && zdimz) diff --git a/src/display.c b/src/display.c index 805b6f5..fed3697 100644 --- a/src/display.c +++ b/src/display.c @@ -394,9 +394,18 @@ void display_main(Sheet *cursheet) } while (k == K_INVALID || !do_sheetcmd(cursheet,k,0) || doanyway(cursheet,_("Sheet modified, leave anyway?"))!=1); } +#define TEAPOT_WHITE 16 +#define CHANNEL_MAX 1000 + void display_init(Sheet *cursheet, int always_redraw) { initscr(); + start_color(); + init_color(TEAPOT_WHITE, CHANNEL_MAX, CHANNEL_MAX, CHANNEL_MAX); + assume_default_colors(COLOR_BLACK, TEAPOT_WHITE); + if (debug_level > 1) + printf("Terminal has colors: %d, #colors:%d, #pairs:%d", + has_colors(), COLORS, COLOR_PAIRS); curs_set(0); noecho(); raw(); @@ -409,7 +418,7 @@ void display_init(Sheet *cursheet, int always_redraw) #endif } -void display_end(void) +void display_end(Sheet* sheet) { curs_set(1); echo(); diff --git a/src/fteapot.fl b/src/fteapot.fl index 0d2175c..f75ca6f 100644 --- a/src/fteapot.fl +++ b/src/fteapot.fl @@ -801,7 +801,7 @@ sheet->changed = ch; new MainWindow(sheet);} {} } -Function {display_end()} {C return_type void +Function {display_end(Sheet* sheet)} {C return_type void } { code {} {} }