Add commands to edit color and add color rendering in terminal teapot

Now the foreground and background color can be set and displayed in
  either user interfaces, which finishes off #21.
  Closes #21.
This commit is contained in:
Glen Whitney 2019-08-28 23:19:08 -07:00
parent 06938ec494
commit 253062e906
6 changed files with 66 additions and 9 deletions

View File

@ -341,6 +341,7 @@ static void do_attribute(Sheet *cursheet, Key action)
line_msg(_("Cell attribute:"),_("Cell is locked"));
return;
}
ColorAspect ca = FOREGROUND;
switch ((int)action)
{
case ADJUST_CENTER: ++adj; /* drop through */
@ -373,12 +374,7 @@ static void do_attribute(Sheet *cursheet, Key action)
/* 5 -- set precision */ /*{{{*/
case ADJUST_PRECISION:
{
size_t ex,offx;
int n;
offx=0;
ex=0;
n = getprecision(fcell);
int n = getprecision(fcell);
const char* prompt = _("Precision for block:");
if (onecell) prompt = _("Precision for cell:");
int c = line_numedit(&n, prompt);
@ -388,6 +384,22 @@ static void do_attribute(Sheet *cursheet, Key action)
break;
}
/*}}}*/
/* Set a color */ /*{{{*/
case ADJUST_BACKGROUND:
ca = BACKGROUND;
/* FALL THROUGH */
case ADJUST_FOREGROUND: {
int n = getcolor(fcell, ca);
const char* prompt = _("%s for block:");
if (onecell) prompt = _("%s for cell:");
char pbuf[256];
sprintf(pbuf, prompt, ColorAspect_Name[ca]);
int c = line_numedit(&n, pbuf);
if (c < 0) return;
for (ALL_LOCS_IN_REGION(cursheet,w))
changed = setcolor(cursheet, w, ca, (ColorNum)n) || changed;
break;
}
/* 6 -- shadow */ /*{{{*/
case ADJUST_SHADOW:
{
@ -1443,6 +1455,8 @@ int do_sheetcmd(Sheet *cursheet, Key c, int moveonly)
case ADJUST_SHADOW:
case ADJUST_BOLD:
case ADJUST_UNDERLINE:
case ADJUST_FOREGROUND:
case ADJUST_BACKGROUND:
case ADJUST_TRANSPARENT:
case ADJUST_LABEL:
case ADJUST_LOCK:

View File

@ -85,7 +85,9 @@ typedef enum {
FILL_BLOCK = -58,
ADJUST_DECIMAL = -59,
ADJUST_COMPACT = -60,
ADJUST_HEXACT = -61
ADJUST_HEXACT = -61,
ADJUST_FOREGROUND = -62,
ADJUST_BACKGROUND = -63
} Key;
extern int do_sheetcmd(Sheet *cursheet, Key c, int moveonly);

View File

@ -731,6 +731,21 @@ DEFSETTER(igncell, bool, ignored);
DEFSETTER(setfltformat, FloatFormat, fform);
DEFSETTER(setprecision, int, precision);
/* sadly color has an extra argument */
bool setcolor(Sheet *sheet, const Location at, ColorAspect asp, ColorNum col)
{
assert(sheet != (Sheet*)0);
bool isnew = false;
Cell* thecell = initcellofsheet(sheet, at, &isnew);
if (isnew) sheet->changed = true;
if (thecell->aspect[asp] != col) {
thecell->aspect[asp] = col;
sheet->changed = true;
return true;
}
return isnew;
}
/* clk -- clock cell */ /*{{{*/
void clk(Sheet *sheet, const Location at)
{

View File

@ -100,6 +100,7 @@ bool setadjust(Sheet *sheet, const Location at, Adjust adjust);
bool shadow(Sheet *sheet, const Location at, bool yep);
bool bold(Sheet *sheet, const Location at, bool yep);
bool underline(Sheet *sheet, const Location at, bool yep);
bool setcolor(Sheet *sheet, const Location at, ColorAspect asp, ColorNum col);
bool lockcell(Sheet *sheet, const Location at, bool yep);
bool maketrans(Sheet *sheet, const Location at, bool yep);
bool igncell(Sheet *sheet, const Location at, bool yep);

View File

@ -117,12 +117,15 @@ static int do_attribute(Sheet *cursheet)
case 2:
{
const char *typemenu[] =
{ _("bB)old"), _("uU)nderline"), NULL };
{ _("bB)old"), _("uU)nderline"),
_("fF)oreground"), _("kBack)ground"), NULL };
switch (c = line_menu(prompt, typemenu, 0))
{
case -2: case -1: c = K_INVALID; break;
case 0: c = ADJUST_BOLD; break;
case 1: c = ADJUST_UNDERLINE; break;
case 2: c = ADJUST_FOREGROUND; break;
case 3: c = ADJUST_BACKGROUND; break;
default: assert(0);
}
break;
@ -530,6 +533,17 @@ void redraw_sheet(Sheet *sheet)
}
else realsize=size;
ms = getmarkstate(sheet);
int usecp = 0;
ColorNum fg = getcolor(cell, FOREGROUND);
ColorNum bg = getcolor(cell, BACKGROUND);
while (usecp < curcp) {
unsigned short pfg, pbg;
pair_content(usecp, &pfg, &pbg);
if (fg == pfg && bg == pbg) break;
++usecp;
}
if (usecp == curcp) init_pair(curcp++, fg, bg);
wcolor_set(stdscr, usecp, NULL);
invert =
(ms != UNMARKED) && loc_in_box(tmp, sheet->mark1, sheet->mark2);
if (x == sheet->cur[X] && (y-header+sheet->offy) == sheet->cur[Y])
@ -540,6 +554,7 @@ void redraw_sheet(Sheet *sheet)
(void)mvwaddstr(stdscr,sheet->oriy+y,sheet->orix+width,buf+cutoff);
for (fill=mbslen(buf+cutoff); fill<realsize; ++fill)
(void)waddch(stdscr,(chtype)(unsigned char)' ');
wcolor_set(stdscr, 0, NULL);
wstandend(stdscr);
}
}

View File

@ -554,7 +554,17 @@ class MainWindow {open}
MenuItem {} {
label {&Precision...}
user_data ADJUST_PRECISION
xywh {0 0 36 21} shortcut 0x80070 divider
xywh {0 0 36 21} shortcut 0x80070
}
MenuItem {} {
label {&Foreground...}
user_data ADJUST_FOREGROUND
xywh {0 0 36 21}
}
MenuItem {} {
label {&Background...}
user_data ADJUST_BACKGROUND
xywh {0 0 36 21} divider
}
menuitem dec {
label {&Decimal}