spmenu/libs/draw.c

398 lines
10 KiB
C
Raw Normal View History

2023-03-02 11:40:52 +01:00
void
drawhighlights(struct item *item, int x, int y, int w)
2023-03-02 11:40:52 +01:00
{
2023-03-16 11:58:38 +01:00
int i, indent;
char *highlight;
char c;
2023-03-08 19:20:18 +01:00
// limitation in order to prevent highlighting from drawing when the text isn't visible
if (columns > 5 && lines > 1) return;
2023-03-16 11:58:38 +01:00
char *itemtext = item->text;
2023-03-08 19:20:18 +01:00
if (!(strlen(itemtext) && strlen(text))) return;
2023-03-16 11:58:38 +01:00
drw_setscheme(drw, scheme[item == sel
? SchemeSelHighlight
: SchemeNormHighlight]);
for (i = 0, highlight = itemtext; *highlight && text[i];) {
if (((fuzzy && !fstrncmp(&(*highlight), &text[i], 1)) || (!fuzzy && *highlight == text[i]))) {
2023-03-16 11:58:38 +01:00
c = *highlight;
*highlight = '\0';
indent = TEXTW(itemtext) - lrpad;
*highlight = c;
// highlight character
2023-03-16 11:58:38 +01:00
c = highlight[1];
highlight[1] = '\0';
drw_text(
drw,
x + indent + (lrpad / 2),
y,
MIN(w - indent - lrpad, TEXTW(highlight) - lrpad),
bh, 0, highlight, 0, pango_highlight ? True : False);
highlight[1] = c;
i++;
2023-03-02 11:40:52 +01:00
}
2023-03-16 11:58:38 +01:00
highlight++;
2023-03-02 11:40:52 +01:00
}
}
int
2023-03-26 16:42:11 +02:00
drawitemtext(struct item *item, int x, int y, int w)
2023-03-02 11:40:52 +01:00
{
char buffer[MAXITEMLENGTH]; // buffer containing item text
Clr scm[2]; // color scheme
int leftpadding = lrpad / 2; // padding
int wr, rd; // character
int fg = 7; // foreground
int bg = 0; // background
int bgfg = 0; // both
int ignore = 0; // ignore colors
int skiphighlight = 0; // skip highlighting
// memcpy the correct scheme
2023-03-02 11:40:52 +01:00
if (item == sel) {
memcpy(scm, scheme[SchemeItemSel], sizeof(scm));
2023-03-09 10:10:29 +01:00
if (item->hp)
memcpy(scm, scheme[SchemeItemSelPri], sizeof(scm));
2023-03-02 11:40:52 +01:00
} else {
memcpy(scm, scheme[SchemeItemNorm], sizeof(scm));
2023-03-09 10:10:29 +01:00
if (item->hp)
memcpy(scm, scheme[SchemeItemNormPri], sizeof(scm));
2023-03-02 11:40:52 +01:00
}
2023-03-28 15:10:57 +02:00
// don't color
if (!coloritems) memcpy(scm, scheme[SchemeItemNorm], sizeof(scm));
drw_setscheme(drw, scm); // set scheme
2023-03-02 11:40:52 +01:00
// parse item text
2023-03-02 11:40:52 +01:00
for (wr = 0, rd = 0; item->text[rd]; rd++) {
if (item->text[rd] == '' && item->text[rd + 1] == '[') {
size_t alen = strspn(item->text + rd + 2, "0123456789;");
if (item->text[rd + alen + 2] == 'm') { // last character in sequence is always 'm'
buffer[wr] = '\0';
2023-03-02 11:40:52 +01:00
apply_fribidi(buffer);
drw_text(drw, x, y, MIN(w, TEXTW(buffer) - lrpad) + leftpadding, bh, leftpadding, isrtl ? fribidi_text : buffer, 0, pango_item ? True : False);
2023-03-02 11:40:52 +01:00
// position and width
x += MIN(w, TEXTW(buffer) - lrpad) + leftpadding;
w -= MIN(w, TEXTW(buffer) - lrpad) + leftpadding;
2023-03-29 20:23:49 +02:00
// no highlighting if colored text
skiphighlight = 1;
leftpadding = 0;
2023-03-02 11:40:52 +01:00
char *character = item->text + rd + 1; // current character
2023-03-02 11:40:52 +01:00
// parse hex colors in scm, m is always the last character
while (*character != 'm') {
unsigned nextchar = strtoul(character + 1, &character, 10);
2023-03-04 02:07:06 +01:00
if (ignore)
continue;
if (bgfg) {
if (bgfg < 4 && nextchar == 5) {
2023-03-04 02:07:06 +01:00
bgfg <<= 1;
continue;
}
if (bgfg == 4)
scm[0] = textclrs[fg = nextchar];
2023-03-04 02:07:06 +01:00
else if (bgfg == 6)
scm[1] = textclrs[bg = nextchar];
2023-03-04 02:07:06 +01:00
ignore = 1;
continue;
}
if (nextchar == 1) {
2023-03-02 11:40:52 +01:00
fg |= 8;
scm[0] = textclrs[fg];
} else if (nextchar == 22) {
2023-03-02 11:40:52 +01:00
fg &= ~8;
scm[0] = textclrs[fg];
} else if (nextchar >= 30 && nextchar <= 37) {
fg = nextchar % 10 | (fg & 8);
2023-03-02 11:40:52 +01:00
scm[0] = textclrs[fg];
} else if (nextchar == 38) {
2023-03-02 11:40:52 +01:00
bgfg = 2;
} else if (nextchar >= 40 && nextchar <= 47) {
bg = nextchar % 10;
2023-03-02 11:40:52 +01:00
scm[1] = textclrs[bg];
} else if (nextchar == 48) {
2023-03-02 11:40:52 +01:00
bgfg = 3;
}
}
rd += alen + 2;
wr = 0;
drw_setscheme(drw, scm); // set scheme
2023-03-02 11:40:52 +01:00
continue;
}
}
buffer[wr++] = item->text[rd];
}
buffer[wr] = '\0';
// now draw any non-colored text
2023-03-02 11:40:52 +01:00
apply_fribidi(buffer);
int r = drw_text(drw, x, y, w, bh, leftpadding, isrtl ? fribidi_text : buffer, 0, pango_item ? True : False);
if (!hidehighlight && !skiphighlight) drawhighlights(item, x, y, w);
2023-03-02 11:40:52 +01:00
// copy current buffer to item->clntext instead of item->text, this way SGR sequences aren't drawn
item->clntext = malloc(sizeof(buffer));
memcpy(item->clntext, buffer, sizeof(buffer));
2023-03-02 11:40:52 +01:00
return r;
}
2023-03-26 16:42:11 +02:00
int
drawitem(int x, int y, int w)
2023-03-02 11:40:52 +01:00
{
struct item *item;
int numberWidth = 0;
int modeWidth = 0;
int rarrowWidth = 0;
// add width
2023-03-02 18:25:28 +01:00
if (!hidemode) modeWidth = pango_mode ? TEXTWM(modetext) : TEXTW(modetext);
if (!hiderarrow) rarrowWidth = pango_rightarrow ? TEXTWM(rightarrow) : TEXTW(rightarrow);
2023-03-26 16:42:11 +02:00
if (!hidematchcount) numberWidth = pango_numbers ? TEXTWM(numbers) : TEXTW(numbers);
2023-03-12 21:03:35 +01:00
// mode indicator is always going to be at the right
if (hidemode) {
numberWidth += 2 * sp + borderwidth;
} else {
modeWidth += 2 * sp + borderwidth;
}
2023-03-26 16:42:11 +02:00
#if USEIMAGE
int ox = 0; // original x position
#endif
2023-03-02 11:40:52 +01:00
// draw items and image
2023-03-02 11:40:52 +01:00
if (lines > 0) {
int i = 0;
2023-03-24 04:04:37 +01:00
int rx = 0;
// draw image first
2023-03-02 11:40:52 +01:00
#if USEIMAGE
2023-03-02 16:22:59 +01:00
if (!hideimage && longestedge != 0) {
2023-03-24 04:04:37 +01:00
rx = ox;
rx += (imagegaps * 2) + imagewidth;
} else
2023-03-02 11:40:52 +01:00
#endif
2023-03-24 04:04:37 +01:00
if (!indentitems) {
rx = 0;
} else {
rx = x;
}
2023-03-13 22:45:04 +01:00
for (item = curr; item != next; item = item->right, i++) {
2023-03-26 16:42:11 +02:00
drawitemtext(
2023-03-02 11:40:52 +01:00
item,
2023-03-24 04:04:37 +01:00
rx + ((i / lines) * ((mw - rx) / columns)),
2023-03-02 11:40:52 +01:00
y + (((i % lines) + 1) * bh),
2023-03-24 04:04:37 +01:00
(mw - rx) / columns
2023-03-02 11:40:52 +01:00
);
2023-03-13 22:45:04 +01:00
}
// horizontal list
2023-03-02 11:40:52 +01:00
} else if (matches) {
x += inputw;
2023-03-26 16:42:11 +02:00
drawlarrow(x, y, w);
for (item = curr; item != next; item = item->right) // draw items
2023-03-26 16:42:11 +02:00
x = drawitemtext(item, x, 0, MIN(pango_item ? TEXTWM(item->text) : TEXTW(item->text), mw - x - rarrowWidth - numberWidth - modeWidth));
2023-03-26 16:42:11 +02:00
drawrarrow(x, y, w, numberWidth, modeWidth, rarrowWidth);
}
2023-03-02 11:40:52 +01:00
2023-03-26 16:42:11 +02:00
return x;
}
int
drawprompt(int x, int y, int w)
{
if (prompt && *prompt && !hideprompt) {
drw_setscheme(drw, scheme[SchemePrompt]);
x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0, pango_prompt ? True : False);
if (!hidepowerline) {
drw_settrans(drw, scheme[SchemePrompt], scheme[SchemeMenu]);
drw_arrow(drw, x, 0, plw, bh, 1, 0);
x += plw;
}
}
return x;
}
int
drawinput(int x, int y, int w)
{
char *censort; // censor text (password)
unsigned int curpos = 0;
int fh = drw->font->h;
// draw input
w = (lines > 0 || !matches) ? mw - x : inputw;
drw_setscheme(drw, scheme[SchemeInput]);
if (passwd && !hideprompt) {
2023-03-28 23:35:46 +02:00
censort = ecalloc(1, sizeof(text));
memset(censort, (int)password, strlen(text));
2023-03-26 16:42:11 +02:00
apply_fribidi(censort);
drw_text(drw, x, 0, w, bh, lrpad / 2, isrtl ? fribidi_text : censort, 0, pango_password ? True : False);
curpos = TEXTW(censort) - TEXTW(&text[cursor]);
free(censort);
} else if (!passwd && !hideprompt) {
apply_fribidi(text);
drw_text(drw, x, 0, w, bh, lrpad / 2, isrtl ? fribidi_text : text, 0, pango_input ? True : False);
curpos = TEXTW(text) - TEXTW(&text[cursor]);
}
if ((curpos += lrpad / 2 - 1) < w && !hidecursor && !hideprompt) {
drw_setscheme(drw, scheme[SchemeCaret]);
drw_rect(drw, x + curpos, 2 + (bh - fh) / 2, 2, fh - 4, 1, 0);
}
return x;
}
int
drawlarrow(int x, int y, int w)
{
if (hidelarrow) {
return x;
}
if (curr->left) { // draw left arrow
drw_setscheme(drw, scheme[SchemeLArrow]);
drw_text(drw, x, 0, w, bh, lrpad / 2, leftarrow, 0, pango_leftarrow ? True : False);
}
x += w;
return x;
}
int
drawrarrow(int x, int y, int w, int numberWidth, int modeWidth, int rarrowWidth)
{
if (next && !hiderarrow) { // draw right arrow
w = rarrowWidth;
drw_setscheme(drw, scheme[SchemeRArrow]);
drw_text(drw, mw - w, 0, w, bh, lrpad / 2, rightarrow, 0, pango_rightarrow ? True : False);
2023-03-02 11:40:52 +01:00
}
2023-03-26 16:42:11 +02:00
return x;
}
int
drawnumber(int x, int y, int w, int numberWidth, int modeWidth)
{
if (!hidematchcount) { // draw match count
2023-03-02 11:40:52 +01:00
drw_setscheme(drw, scheme[SchemeNumber]);
2023-03-21 16:40:11 +01:00
2023-03-21 17:10:16 +01:00
drw_text(drw, mw - numberWidth - modeWidth, 0, numberWidth, bh, lrpad / 2 + plw / 2, numbers, 0, pango_numbers ? True : False);
2023-03-21 16:40:11 +01:00
// draw powerline for match count
if (!hidepowerline) {
drw_settrans(drw, scheme[SchemeNumber], scheme[SchemeMenu]);
drw_arrow(drw, mw - numberWidth - modeWidth, 0, plw, bh, 0, 0);
x += plw;
}
2023-03-02 11:40:52 +01:00
}
2023-03-26 16:42:11 +02:00
return x;
}
int
drawmode(int x, int y, int w, int numberWidth, int modeWidth)
{
if (!hidemode) { // draw mode indicator
2023-03-02 11:40:52 +01:00
drw_setscheme(drw, scheme[SchemeMode]);
2023-03-21 16:40:11 +01:00
2023-03-21 17:10:16 +01:00
drw_text(drw, mw - modeWidth, 0, modeWidth, bh, lrpad / 2 + plw / 2, modetext, 0, pango_mode ? True : False);
2023-03-21 16:40:11 +01:00
// draw powerline for match count
if (!hidepowerline) {
drw_settrans(drw, scheme[SchemeMode], hidematchcount ? scheme[SchemeMenu] : scheme[SchemeNumber]);
drw_arrow(drw, mw - modeWidth, 0, plw, bh, 0, 0);
x += plw;
}
2023-03-02 11:40:52 +01:00
}
2023-03-26 16:42:11 +02:00
return x;
}
void
drawmenu(void)
{
int x = 0, y = 0, w = 0;
plw = hidepowerline ? 0 : drw->font->h / 2 + 1; // powerline size
// draw menu first using menu scheme
drw_setscheme(drw, scheme[SchemeMenu]);
drw_rect(drw, 0, 0, mw, mh, 1, 1);
int numberWidth = 0;
int modeWidth = 0;
// add width
if (!hidemode) modeWidth = pango_mode ? TEXTWM(modetext) : TEXTW(modetext);
// calculate match count
if (!hidematchcount) {
recalculatenumbers();
numberWidth = TEXTW(numbers);
// mode indicator is always going to be at the right
if (hidemode) {
numberWidth += 2 * sp + borderwidth;
} else {
modeWidth += 2 * sp + borderwidth;
}
}
// why have an empty line?
if (hideprompt && hideinput && hidemode && hidematchcount) {
y -= bh;
mh = (lines + 1) * bh - bh;
2023-03-26 16:42:11 +02:00
if (!win) return;
XResizeWindow(dpy, win, mw, mh);
drw_resize(drw, mw, mh);
}
if (!hideprompt) x = drawprompt(x, y, w);
if (!hideinput) x = drawinput(x, y, w);
2023-03-26 16:42:11 +02:00
if (!hidemode) modeWidth = pango_mode ? TEXTWM(modetext) : TEXTW(modetext);
drawitem(x, y, w);
if (!hidematchcount) drawnumber(x, y, w, numberWidth, modeWidth);
if (!hidemode) drawmode(x, y, w, numberWidth, modeWidth);
2023-03-26 16:42:11 +02:00
2023-03-02 11:40:52 +01:00
drw_map(drw, win, 0, 0, mw, mh);
}