251 lines
11 KiB
C
251 lines
11 KiB
C
#include "render.h"
|
|||
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#define BAR_BUF_LEN 512
|
||
|
|
|
||
|
|
void render_search_highlights(Editor *ed, int line_idx, int top_y, int visible_cols) {
|
||
|
|
if (!ed->search_mode || !ed->search_highlight_all) return;
|
||
|
|
int qlen = (int)strlen(ed->search_query);
|
||
|
|
if (qlen == 0) return;
|
||
|
|
|
||
|
|
const char *line = ed->lines[line_idx];
|
||
|
|
const char *p = line;
|
||
|
|
|
||
|
|
while ((p = strstr(p, ed->search_query)) != NULL) {
|
||
|
|
int b_start = (int)(p - line);
|
||
|
|
int b_end = b_start + qlen;
|
||
|
|
|
||
|
|
int c_start = utf8_char_count(line, b_start);
|
||
|
|
int c_end = utf8_char_count(line, b_end);
|
||
|
|
|
||
|
|
if (c_end > ed->scroll_x && c_start < ed->scroll_x + visible_cols + 1) {
|
||
|
|
int vis_s = (c_start < ed->scroll_x) ? ed->scroll_x : c_start;
|
||
|
|
int vis_e = (c_end > ed->scroll_x + visible_cols + 1) ? (ed->scroll_x + visible_cols + 1) : c_end;
|
||
|
|
int x1 = GUTTER_W + 8 + (vis_s - ed->scroll_x) * ed->char_w;
|
||
|
|
int w = (vis_e - vis_s) * ed->char_w;
|
||
|
|
if (w > 0) {
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_match_all);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, x1, top_y, w, ed->line_h);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
p += qlen;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void render_selection_bg(Editor *ed, int line_idx, int top_y, int visible_cols) {
|
||
|
|
if (!ed->has_sel) return;
|
||
|
|
|
||
|
|
int sy, sx, ey, ex;
|
||
|
|
editor_get_sel_bounds(ed, &sy, &sx, &ey, &ex);
|
||
|
|
if (line_idx < sy || line_idx > ey) return;
|
||
|
|
|
||
|
|
const char *line = ed->lines[line_idx];
|
||
|
|
int line_len = (int)strlen(line);
|
||
|
|
int b_start = (line_idx == sy) ? sx : 0;
|
||
|
|
int b_end = (line_idx == ey) ? ex : line_len;
|
||
|
|
|
||
|
|
int c_start = utf8_char_count(line, b_start);
|
||
|
|
int c_end = utf8_char_count(line, b_end);
|
||
|
|
if (line_idx < ey) c_end += 1;
|
||
|
|
|
||
|
|
if (c_end <= ed->scroll_x || c_start >= ed->scroll_x + visible_cols + 1) return;
|
||
|
|
if (c_start < ed->scroll_x) c_start = ed->scroll_x;
|
||
|
|
if (c_end > ed->scroll_x + visible_cols + 1) c_end = ed->scroll_x + visible_cols + 1;
|
||
|
|
|
||
|
|
int x1 = GUTTER_W + 8 + (c_start - ed->scroll_x) * ed->char_w;
|
||
|
|
int w = (c_end - c_start) * ed->char_w;
|
||
|
|
if (w > 0) {
|
||
|
|
unsigned long bg = (ed->search_mode && ed->search_highlight_all) ? ed->col_match_cur : ed->col_sel;
|
||
|
|
XSetForeground(ed->dpy, ed->gc, bg);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, x1, top_y, w, ed->line_h);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void render_hover_symbol_link(Editor *ed, int line_idx, int top_y, int baseline_y, int visible_cols) {
|
||
|
|
if (ed->hover_sym_line != line_idx) return;
|
||
|
|
|
||
|
|
const char *line = ed->lines[line_idx];
|
||
|
|
int vis_b_start = (ed->hover_sym_sx > utf8_byte_offset(line, ed->scroll_x))
|
||
|
|
? ed->hover_sym_sx : utf8_byte_offset(line, ed->scroll_x);
|
||
|
|
int vis_b_end = (ed->hover_sym_ex < utf8_byte_offset(line, ed->scroll_x + visible_cols + 1))
|
||
|
|
? ed->hover_sym_ex : utf8_byte_offset(line, ed->scroll_x + visible_cols + 1);
|
||
|
|
|
||
|
|
if (vis_b_start >= vis_b_end) return;
|
||
|
|
|
||
|
|
int c_start = utf8_char_count(line, vis_b_start);
|
||
|
|
int c_end = utf8_char_count(line, vis_b_end);
|
||
|
|
int x1 = GUTTER_W + 8 + (c_start - ed->scroll_x) * ed->char_w;
|
||
|
|
int w = (c_end - c_start) * ed->char_w;
|
||
|
|
|
||
|
|
if (w > 0) {
|
||
|
|
unsigned long line_bg = (line_idx == ed->cy && !ed->has_sel) ? ed->col_curline : ed->col_bg;
|
||
|
|
XSetForeground(ed->dpy, ed->gc, line_bg);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, x1, top_y, w, ed->line_h);
|
||
|
|
|
||
|
|
XftDrawStringUtf8(ed->xft_draw, &ed->xft_link, ed->font, x1, baseline_y,
|
||
|
|
(const FcChar8 *)&line[vis_b_start], vis_b_end - vis_b_start);
|
||
|
|
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->xft_link.pixel);
|
||
|
|
XDrawLine(ed->dpy, ed->backbuf, ed->gc, x1, baseline_y + 1, x1 + w - 1, baseline_y + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/* dir: 0 = вверх, 1 = вниз, 2 = влево, 3 = вправо */
|
||
|
|
static void draw_sb_arrow_btn(Editor *ed, int x, int y, int dir, int hovered, int pressed) {
|
||
|
|
unsigned long bg = pressed ? ed->col_sel : (hovered ? ed->col_bg : ed->col_status);
|
||
|
|
XSetForeground(ed->dpy, ed->gc, bg);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, x, y, SCROLLBAR_W, SCROLLBAR_W);
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb);
|
||
|
|
XDrawRectangle(ed->dpy, ed->backbuf, ed->gc, x, y, SCROLLBAR_W - 1, SCROLLBAR_W - 1);
|
||
|
|
|
||
|
|
int shift = pressed ? 1 : 0;
|
||
|
|
int cx = x + SCROLLBAR_W / 2 + shift;
|
||
|
|
int cy = y + SCROLLBAR_W / 2 + shift;
|
||
|
|
XPoint p0, p1, p2;
|
||
|
|
|
||
|
|
if (dir == 0) {
|
||
|
|
p0 = (XPoint){(short)(cx - 3), (short)(cy + 2)};
|
||
|
|
p1 = (XPoint){(short)(cx + 3), (short)(cy + 2)};
|
||
|
|
p2 = (XPoint){(short)cx, (short)(cy - 2)};
|
||
|
|
} else if (dir == 1) {
|
||
|
|
p0 = (XPoint){(short)(cx - 3), (short)(cy - 2)};
|
||
|
|
p1 = (XPoint){(short)(cx + 3), (short)(cy - 2)};
|
||
|
|
p2 = (XPoint){(short)cx, (short)(cy + 2)};
|
||
|
|
} else if (dir == 2) {
|
||
|
|
p0 = (XPoint){(short)(cx + 2), (short)(cy - 3)};
|
||
|
|
p1 = (XPoint){(short)(cx + 2), (short)(cy + 3)};
|
||
|
|
p2 = (XPoint){(short)(cx - 2), (short)cy};
|
||
|
|
} else {
|
||
|
|
p0 = (XPoint){(short)(cx - 2), (short)(cy - 3)};
|
||
|
|
p1 = (XPoint){(short)(cx - 2), (short)(cy + 3)};
|
||
|
|
p2 = (XPoint){(short)(cx + 2), (short)cy};
|
||
|
|
}
|
||
|
|
|
||
|
|
XPoint tri[3] = {p0, p1, p2};
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_fg);
|
||
|
|
XFillPolygon(ed->dpy, ed->backbuf, ed->gc, tri, 3, Convex, CoordModeOrigin);
|
||
|
|
}
|
||
|
|
|
||
|
|
void render_scrollbars(Editor *ed, int visible_lines, int visible_cols) {
|
||
|
|
int st_h = editor_status_h(ed);
|
||
|
|
int v_track_x = ed->win_w - SCROLLBAR_W;
|
||
|
|
int v_full_y = TOP_UI_H;
|
||
|
|
int v_full_h = ed->win_h - TOP_UI_H - st_h - SCROLLBAR_W;
|
||
|
|
|
||
|
|
if (v_full_h >= SCROLLBAR_W * 2) {
|
||
|
|
draw_sb_arrow_btn(ed, v_track_x, v_full_y, 0, ed->hover_btn == 2, ed->pressed_btn == 2);
|
||
|
|
draw_sb_arrow_btn(ed, v_track_x, v_full_y + v_full_h - SCROLLBAR_W, 1, ed->hover_btn == 3, ed->pressed_btn == 3);
|
||
|
|
|
||
|
|
int v_track_y = v_full_y + SCROLLBAR_W;
|
||
|
|
int v_track_h = v_full_h - SCROLLBAR_W * 2;
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_sb_track);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, v_track_x, v_track_y, SCROLLBAR_W, v_track_h);
|
||
|
|
|
||
|
|
if (ed->num_lines > visible_lines && v_track_h > 0) {
|
||
|
|
int thumb_h = (v_track_h * visible_lines) / ed->num_lines;
|
||
|
|
if (thumb_h < 16) thumb_h = 16;
|
||
|
|
if (thumb_h > v_track_h) thumb_h = v_track_h;
|
||
|
|
int max_sy = ed->num_lines - visible_lines;
|
||
|
|
int thumb_y = v_track_y + ((max_sy > 0) ? (ed->scroll_y * (v_track_h - thumb_h)) / max_sy : 0);
|
||
|
|
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, v_track_x + 2, thumb_y, SCROLLBAR_W - 4, thumb_h);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int h_full_x = GUTTER_W;
|
||
|
|
int h_track_y = ed->win_h - st_h - SCROLLBAR_W;
|
||
|
|
int h_full_w = ed->win_w - GUTTER_W - SCROLLBAR_W;
|
||
|
|
|
||
|
|
if (h_full_w >= SCROLLBAR_W * 2) {
|
||
|
|
draw_sb_arrow_btn(ed, h_full_x, h_track_y, 2, ed->hover_btn == 4, ed->pressed_btn == 4);
|
||
|
|
draw_sb_arrow_btn(ed, h_full_x + h_full_w - SCROLLBAR_W, h_track_y, 3, ed->hover_btn == 5, ed->pressed_btn == 5);
|
||
|
|
|
||
|
|
int h_track_x = h_full_x + SCROLLBAR_W;
|
||
|
|
int h_track_w = h_full_w - SCROLLBAR_W * 2;
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_sb_track);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, h_track_x, h_track_y, h_track_w, SCROLLBAR_W);
|
||
|
|
|
||
|
|
int max_cols = editor_max_line_len(ed);
|
||
|
|
if (max_cols > visible_cols && h_track_w > 0) {
|
||
|
|
int thumb_w = (h_track_w * visible_cols) / max_cols;
|
||
|
|
if (thumb_w < 16) thumb_w = 16;
|
||
|
|
if (thumb_w > h_track_w) thumb_w = h_track_w;
|
||
|
|
int max_sx = max_cols - visible_cols;
|
||
|
|
int thumb_x = h_track_x + ((max_sx > 0) ? (ed->scroll_x * (h_track_w - thumb_w)) / max_sx : 0);
|
||
|
|
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, thumb_x, h_track_y + 2, thumb_w, SCROLLBAR_W - 4);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_gutter);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, v_track_x, h_track_y, SCROLLBAR_W, SCROLLBAR_W);
|
||
|
|
|
||
|
|
int st_pressed = (ed->pressed_btn == 1);
|
||
|
|
unsigned long st_bg = st_pressed ? ed->col_sel : ((ed->hover_btn == 1) ? ed->col_bg : ed->col_gutter);
|
||
|
|
XSetForeground(ed->dpy, ed->gc, st_bg);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, 0, h_track_y, GUTTER_W, SCROLLBAR_W);
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb);
|
||
|
|
XDrawLine(ed->dpy, ed->backbuf, ed->gc, 0, h_track_y, GUTTER_W - 1, h_track_y);
|
||
|
|
XDrawLine(ed->dpy, ed->backbuf, ed->gc, GUTTER_W - 1, h_track_y, GUTTER_W - 1, h_track_y + SCROLLBAR_W);
|
||
|
|
|
||
|
|
int shift = st_pressed ? 1 : 0;
|
||
|
|
int cx = GUTTER_W / 2 + shift;
|
||
|
|
int cy = h_track_y + SCROLLBAR_W / 2 + shift;
|
||
|
|
XPoint p0, p1, p2;
|
||
|
|
if (!ed->status_collapsed) {
|
||
|
|
p0 = (XPoint){(short)(cx - 4), (short)(cy - 2)};
|
||
|
|
p1 = (XPoint){(short)(cx + 4), (short)(cy - 2)};
|
||
|
|
p2 = (XPoint){(short)cx, (short)(cy + 3)};
|
||
|
|
} else {
|
||
|
|
p0 = (XPoint){(short)(cx - 4), (short)(cy + 2)};
|
||
|
|
p1 = (XPoint){(short)(cx + 4), (short)(cy + 2)};
|
||
|
|
p2 = (XPoint){(short)cx, (short)(cy - 3)};
|
||
|
|
}
|
||
|
|
XPoint tri[3] = {p0, p1, p2};
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_fg);
|
||
|
|
XFillPolygon(ed->dpy, ed->backbuf, ed->gc, tri, 3, Convex, CoordModeOrigin);
|
||
|
|
}
|
||
|
|
|
||
|
|
void render_status_bar(Editor *ed, int cur_col) {
|
||
|
|
if (ed->status_collapsed) return;
|
||
|
|
|
||
|
|
int st_h = editor_status_h(ed);
|
||
|
|
int status_y = ed->win_h - st_h;
|
||
|
|
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_status);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, 0, status_y, ed->win_w, st_h);
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb);
|
||
|
|
XDrawLine(ed->dpy, ed->backbuf, ed->gc, 0, status_y, ed->win_w, status_y);
|
||
|
|
int baseline = status_y + ed->font->ascent + 4;
|
||
|
|
char bar[BAR_BUF_LEN];
|
||
|
|
if (ed->search_mode) {
|
||
|
|
snprintf(bar, sizeof(bar), " Find: %s_ ", ed->search_query);
|
||
|
|
int bar_len = (int)strlen(bar);
|
||
|
|
int bar_cols = utf8_char_count(bar, bar_len);
|
||
|
|
XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, 6, baseline,
|
||
|
|
(const FcChar8 *)bar, bar_len);
|
||
|
|
int box_x = 6 + bar_cols * ed->char_w;
|
||
|
|
int box_y = status_y + (st_h - 14) / 2;
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_bg);
|
||
|
|
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, box_x, box_y, 14, 14);
|
||
|
|
XSetForeground(ed->dpy, ed->gc, ed->col_fg);
|
||
|
|
XDrawRectangle(ed->dpy, ed->backbuf, ed->gc, box_x, box_y, 14, 14);
|
||
|
|
if (ed->search_highlight_all) {
|
||
|
|
XDrawLine(ed->dpy, ed->backbuf, ed->gc, box_x + 3, box_y + 7, box_x + 6, box_y + 11);
|
||
|
|
XDrawLine(ed->dpy, ed->backbuf, ed->gc, box_x + 6, box_y + 11, box_x + 11, box_y + 3);
|
||
|
|
}
|
||
|
|
const char *lbl = " All | Enter: Next | Shift+Enter: Prev | Esc: Close";
|
||
|
|
XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, box_x + 18, baseline,
|
||
|
|
(const FcChar8 *)lbl, (int)strlen(lbl));
|
||
|
|
ed->chk_x1 = box_x;
|
||
|
|
ed->chk_x2 = box_x + 18 + 4 * ed->char_w;
|
||
|
|
} else {
|
||
|
|
snprintf(bar, sizeof(bar), " %.200s | Ln %d, Col %d | Ctrl+O: Open | Ctrl+N/W: Tabs",
|
||
|
|
ed->status_msg, ed->cy + 1, cur_col + 1);
|
||
|
|
XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, 6, baseline,
|
||
|
|
(const FcChar8 *)bar, (int)strlen(bar));
|
||
|
|
}
|
||
|
|
}
|