Initial commit: X11 C code editor
This commit is contained in:
commit
8eae6763f6
21 files changed
+36389
No files matched your search
@@ -0,0 +1,539 @@
|
||||
#include "editor.h"
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DROP_W 245
|
||||
#define SUB_W 240
|
||||
#define ITEM_H 24
|
||||
#define HDR_BUF_LEN 64
|
||||
|
||||
static const char *top_titles[] = {"File", "Edit", "Navigation"};
|
||||
static const int top_x[] = {8, 64, 120};
|
||||
static const int top_w[] = {52, 52, 100};
|
||||
|
||||
void menu_init(Editor *ed) {
|
||||
ed->menu_open = -1;
|
||||
ed->menu_sub_open = 0;
|
||||
ed->menu_hover_item = -1;
|
||||
ed->menu_hover_sym = -1;
|
||||
ed->menu_sym_scroll = 0;
|
||||
}
|
||||
|
||||
static void nav_push_back(Editor *ed, int cx, int cy) {
|
||||
ed->nav_fwd_count = 0;
|
||||
if (ed->nav_back_count > 0 &&
|
||||
ed->nav_back[ed->nav_back_count - 1].cy == cy &&
|
||||
ed->nav_back[ed->nav_back_count - 1].cx == cx) {
|
||||
return;
|
||||
}
|
||||
if (ed->nav_back_count >= MAX_NAV_HISTORY) {
|
||||
memmove(ed->nav_back, ed->nav_back + 1, (MAX_NAV_HISTORY - 1) * sizeof(NavPoint));
|
||||
ed->nav_back_count = MAX_NAV_HISTORY - 1;
|
||||
}
|
||||
ed->nav_back[ed->nav_back_count].cx = cx;
|
||||
ed->nav_back[ed->nav_back_count].cy = cy;
|
||||
ed->nav_back_count++;
|
||||
}
|
||||
|
||||
void nav_go_back(Editor *ed) {
|
||||
if (ed->nav_back_count <= 0) {
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Нет точек для возврата назад");
|
||||
return;
|
||||
}
|
||||
if (ed->nav_fwd_count >= MAX_NAV_HISTORY) {
|
||||
memmove(ed->nav_fwd, ed->nav_fwd + 1, (MAX_NAV_HISTORY - 1) * sizeof(NavPoint));
|
||||
ed->nav_fwd_count = MAX_NAV_HISTORY - 1;
|
||||
}
|
||||
ed->nav_fwd[ed->nav_fwd_count].cx = ed->cx;
|
||||
ed->nav_fwd[ed->nav_fwd_count].cy = ed->cy;
|
||||
ed->nav_fwd_count++;
|
||||
|
||||
ed->nav_back_count--;
|
||||
ed->cx = ed->nav_back[ed->nav_back_count].cx;
|
||||
ed->cy = ed->nav_back[ed->nav_back_count].cy;
|
||||
if (ed->cy >= ed->num_lines) ed->cy = ed->num_lines - 1;
|
||||
int len = (int)strlen(ed->lines[ed->cy]);
|
||||
if (ed->cx > len) ed->cx = len;
|
||||
|
||||
ed->has_sel = 0;
|
||||
editor_ensure_cursor_visible(ed);
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Назад: Ln %d", ed->cy + 1);
|
||||
}
|
||||
|
||||
void nav_go_forward(Editor *ed) {
|
||||
if (ed->nav_fwd_count <= 0) {
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Нет точек для перехода вперед");
|
||||
return;
|
||||
}
|
||||
if (ed->nav_back_count >= MAX_NAV_HISTORY) {
|
||||
memmove(ed->nav_back, ed->nav_back + 1, (MAX_NAV_HISTORY - 1) * sizeof(NavPoint));
|
||||
ed->nav_back_count = MAX_NAV_HISTORY - 1;
|
||||
}
|
||||
ed->nav_back[ed->nav_back_count].cx = ed->cx;
|
||||
ed->nav_back[ed->nav_back_count].cy = ed->cy;
|
||||
ed->nav_back_count++;
|
||||
|
||||
ed->nav_fwd_count--;
|
||||
ed->cx = ed->nav_fwd[ed->nav_fwd_count].cx;
|
||||
ed->cy = ed->nav_fwd[ed->nav_fwd_count].cy;
|
||||
if (ed->cy >= ed->num_lines) ed->cy = ed->num_lines - 1;
|
||||
int len = (int)strlen(ed->lines[ed->cy]);
|
||||
if (ed->cx > len) ed->cx = len;
|
||||
|
||||
ed->has_sel = 0;
|
||||
editor_ensure_cursor_visible(ed);
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Вперед: Ln %d", ed->cy + 1);
|
||||
}
|
||||
|
||||
static void jump_to_line_with_history(Editor *ed, int target_line, const char *sym_name) {
|
||||
if (target_line != ed->cy) {
|
||||
nav_push_back(ed, ed->cx, ed->cy);
|
||||
}
|
||||
ed->cy = target_line;
|
||||
ed->cx = 0;
|
||||
ed->has_sel = 0;
|
||||
ed->scroll_x = 0;
|
||||
ed->scroll_y = (ed->cy > 3) ? (ed->cy - 3) : 0;
|
||||
if (sym_name) {
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Jumped to %s (Ln %d)", sym_name, ed->cy + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static int is_c_control_word(const char *w, int len) {
|
||||
const char *ctrl[] = {"if", "else", "for", "while", "switch", "return", "sizeof", "typedef", NULL};
|
||||
for (int i = 0; ctrl[i]; i++) {
|
||||
if ((int)strlen(ctrl[i]) == len && strncmp(ctrl[i], w, (size_t)len) == 0) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void menu_refresh_symbols(Editor *ed) {
|
||||
ed->num_symbols = 0;
|
||||
int brace_depth = 0;
|
||||
int in_comment = 0;
|
||||
|
||||
for (int y = 0; y < ed->num_lines; y++) {
|
||||
const char *line = ed->lines[y];
|
||||
int len = (int)strlen(line);
|
||||
int i = 0;
|
||||
|
||||
int first_non_space = 0;
|
||||
while (first_non_space < len && isspace((unsigned char)line[first_non_space])) first_non_space++;
|
||||
int is_preproc = (!in_comment && first_non_space < len && line[first_non_space] == '#');
|
||||
|
||||
if (!in_comment && !is_preproc && brace_depth == 0 && ed->num_symbols < MAX_SYMBOLS) {
|
||||
const char *paren = strchr(line, '(');
|
||||
if (paren && strchr(line, ';') == NULL && strchr(line, '=') == NULL) {
|
||||
int p_idx = (int)(paren - line) - 1;
|
||||
while (p_idx >= 0 && isspace((unsigned char)line[p_idx])) p_idx--;
|
||||
int name_end = p_idx + 1;
|
||||
while (p_idx >= 0 && (isalnum((unsigned char)line[p_idx]) || line[p_idx] == '_')) p_idx--;
|
||||
int name_start = p_idx + 1;
|
||||
int name_len = name_end - name_start;
|
||||
|
||||
if (name_len > 0 && !isdigit((unsigned char)line[name_start]) &&
|
||||
!is_c_control_word(&line[name_start], name_len)) {
|
||||
int before = name_start - 1;
|
||||
while (before >= 0 && (isspace((unsigned char)line[before]) || line[before] == '*')) before--;
|
||||
if (before >= 0) {
|
||||
int copy_len = (name_len < SYM_NAME_LEN - 1) ? name_len : (SYM_NAME_LEN - 1);
|
||||
memcpy(ed->symbols[ed->num_symbols].name, &line[name_start], (size_t)copy_len);
|
||||
ed->symbols[ed->num_symbols].name[copy_len] = '\0';
|
||||
ed->symbols[ed->num_symbols].line = y;
|
||||
ed->num_symbols++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (i < len) {
|
||||
if (in_comment) {
|
||||
if (line[i] == '*' && i + 1 < len && line[i + 1] == '/') {
|
||||
in_comment = 0;
|
||||
i += 2;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
} else if (line[i] == '/' && i + 1 < len && line[i + 1] == '/') {
|
||||
break;
|
||||
} else if (line[i] == '/' && i + 1 < len && line[i + 1] == '*') {
|
||||
in_comment = 1;
|
||||
i += 2;
|
||||
} else if (line[i] == '"' || line[i] == '\'') {
|
||||
char q = line[i++];
|
||||
while (i < len && line[i] != q) {
|
||||
if (line[i] == '\\' && i + 1 < len) i++;
|
||||
i++;
|
||||
}
|
||||
if (i < len) i++;
|
||||
} else {
|
||||
if (line[i] == '{') brace_depth++;
|
||||
else if (line[i] == '}' && brace_depth > 0) brace_depth--;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int menu_goto_symbol_at_cursor(Editor *ed) {
|
||||
const char *line = ed->lines[ed->cy];
|
||||
int len = (int)strlen(line);
|
||||
int pos = ed->cx;
|
||||
|
||||
if (pos > 0 && (pos == len || (!isalnum((unsigned char)line[pos]) && line[pos] != '_'))) {
|
||||
if (isalnum((unsigned char)line[pos - 1]) || line[pos - 1] == '_') {
|
||||
pos--;
|
||||
}
|
||||
}
|
||||
|
||||
if (pos >= len || (!isalnum((unsigned char)line[pos]) && line[pos] != '_')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int start = pos;
|
||||
while (start > 0 && (isalnum((unsigned char)line[start - 1]) || line[start - 1] == '_')) {
|
||||
start--;
|
||||
}
|
||||
int end = pos;
|
||||
while (end < len && (isalnum((unsigned char)line[end]) || line[end] == '_')) {
|
||||
end++;
|
||||
}
|
||||
|
||||
int wlen = end - start;
|
||||
if (wlen <= 0 || wlen >= SYM_NAME_LEN) return 0;
|
||||
|
||||
char word[SYM_NAME_LEN];
|
||||
memcpy(word, &line[start], (size_t)wlen);
|
||||
word[wlen] = '\0';
|
||||
|
||||
menu_refresh_symbols(ed);
|
||||
for (int i = 0; i < ed->num_symbols; i++) {
|
||||
if (strcmp(ed->symbols[i].name, word) == 0) {
|
||||
jump_to_line_with_history(ed, ed->symbols[i].line, word);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Definition not found: %s", word);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int menu_update_hover_symbol(Editor *ed, int mx, int my, unsigned int state) {
|
||||
int old_line = ed->hover_sym_line;
|
||||
int old_sx = ed->hover_sym_sx;
|
||||
int old_ex = ed->hover_sym_ex;
|
||||
|
||||
ed->hover_sym_line = -1;
|
||||
ed->hover_sym_sx = 0;
|
||||
ed->hover_sym_ex = 0;
|
||||
ed->hover_sym_target = -1;
|
||||
|
||||
int v_track_x = ed->win_w - SCROLLBAR_W;
|
||||
int h_track_y = ed->win_h - editor_status_h(ed) - SCROLLBAR_W;
|
||||
|
||||
if ((state & ControlMask) && ed->menu_open < 0 && ed->confirm_mode == 0 && !ed->fbrowser_open &&
|
||||
mx >= GUTTER_W && mx < v_track_x && my >= TOP_UI_H && my < h_track_y) {
|
||||
int y = ed->scroll_y + ((my - TOP_UI_H) / ed->line_h);
|
||||
if (y >= 0 && y < ed->num_lines) {
|
||||
int col = ed->scroll_x + ((mx - GUTTER_W - 8) / ed->char_w);
|
||||
if (col >= 0) {
|
||||
const char *line = ed->lines[y];
|
||||
int len = (int)strlen(line);
|
||||
int pos = utf8_byte_offset(line, col);
|
||||
|
||||
if (pos < len && (isalnum((unsigned char)line[pos]) || line[pos] == '_')) {
|
||||
int start = pos;
|
||||
while (start > 0 && (isalnum((unsigned char)line[start - 1]) || line[start - 1] == '_')) {
|
||||
start--;
|
||||
}
|
||||
int end = pos;
|
||||
while (end < len && (isalnum((unsigned char)line[end]) || line[end] == '_')) {
|
||||
end++;
|
||||
}
|
||||
|
||||
int wlen = end - start;
|
||||
if (wlen > 0 && wlen < SYM_NAME_LEN) {
|
||||
char word[SYM_NAME_LEN];
|
||||
memcpy(word, &line[start], (size_t)wlen);
|
||||
word[wlen] = '\0';
|
||||
|
||||
menu_refresh_symbols(ed);
|
||||
for (int i = 0; i < ed->num_symbols; i++) {
|
||||
if (strcmp(ed->symbols[i].name, word) == 0 && ed->symbols[i].line != y) {
|
||||
ed->hover_sym_line = y;
|
||||
ed->hover_sym_sx = start;
|
||||
ed->hover_sym_ex = end;
|
||||
ed->hover_sym_target = ed->symbols[i].line;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (old_line != ed->hover_sym_line || old_sx != ed->hover_sym_sx || old_ex != ed->hover_sym_ex);
|
||||
}
|
||||
static void draw_dropdown_box(Editor *ed, int x, int y, int w, int h) {
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_gutter);
|
||||
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, x, y, w, h);
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb);
|
||||
XDrawRectangle(ed->dpy, ed->backbuf, ed->gc, x, y, w - 1, h - 1);
|
||||
}
|
||||
static void render_symbols_submenu(Editor *ed, int sub_x, int sub_y) {
|
||||
int st_h = editor_status_h(ed);
|
||||
int max_vis = (ed->win_h - sub_y - st_h - 8) / ITEM_H;
|
||||
if (max_vis < 1) max_vis = 1;
|
||||
int count = ed->num_symbols;
|
||||
int vis_count = (count == 0) ? 1 : ((count < max_vis) ? count : max_vis);
|
||||
int sub_h = vis_count * ITEM_H + 4;
|
||||
draw_dropdown_box(ed, sub_x, sub_y, SUB_W, sub_h);
|
||||
if (count == 0) {
|
||||
int base = sub_y + 2 + (ITEM_H + ed->font->ascent - ed->font->descent) / 2;
|
||||
const char *empty_msg = "(Нет функций)";
|
||||
XftDrawStringUtf8(ed->xft_draw, &ed->xft_lineno, ed->font, sub_x + 10, base,
|
||||
(const FcChar8 *)empty_msg, (int)strlen(empty_msg));
|
||||
return;
|
||||
}
|
||||
if (ed->menu_sym_scroll > count - vis_count) ed->menu_sym_scroll = count - vis_count;
|
||||
if (ed->menu_sym_scroll < 0) ed->menu_sym_scroll = 0;
|
||||
int active_sym = -1;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (ed->symbols[i].line <= ed->cy) active_sym = i;
|
||||
else break;
|
||||
}
|
||||
int max_chars = (SUB_W - 36) / ed->char_w;
|
||||
if (max_chars < 1) max_chars = 1;
|
||||
for (int i = 0; i < vis_count; i++) {
|
||||
int idx = ed->menu_sym_scroll + i;
|
||||
int iy = sub_y + 2 + i * ITEM_H;
|
||||
int base = iy + (ITEM_H + ed->font->ascent - ed->font->descent) / 2;
|
||||
if (idx == ed->menu_hover_sym) {
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_match_cur);
|
||||
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, sub_x + 2, iy, SUB_W - 4, ITEM_H);
|
||||
} else if (idx == active_sym) {
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_sel);
|
||||
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, sub_x + 2, iy, SUB_W - 4, ITEM_H);
|
||||
}
|
||||
const char *fn_icon = "ƒ";
|
||||
XftDrawStringUtf8(ed->xft_draw, &ed->xft_kw, ed->font, sub_x + 8, base,
|
||||
(const FcChar8 *)fn_icon, (int)strlen(fn_icon));
|
||||
int nlen = (int)strlen(ed->symbols[idx].name);
|
||||
if (nlen > max_chars) nlen = max_chars;
|
||||
XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, sub_x + 24, base,
|
||||
(const FcChar8 *)ed->symbols[idx].name, nlen);
|
||||
}
|
||||
}
|
||||
void menu_render(Editor *ed) {
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_status);
|
||||
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, 0, 0, ed->win_w, MENUBAR_H);
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb);
|
||||
XDrawLine(ed->dpy, ed->backbuf, ed->gc, 0, MENUBAR_H - 1, ed->win_w, MENUBAR_H - 1);
|
||||
int base_top = (MENUBAR_H + ed->font->ascent - ed->font->descent) / 2;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (ed->menu_open == i) {
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_sel);
|
||||
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, top_x[i], 2, top_w[i], MENUBAR_H - 3);
|
||||
}
|
||||
XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, top_x[i] + 8, base_top,
|
||||
(const FcChar8 *)top_titles[i], (int)strlen(top_titles[i]));
|
||||
}
|
||||
if (ed->menu_open < 0) return;
|
||||
int dx = top_x[ed->menu_open];
|
||||
int dy = MENUBAR_H;
|
||||
if (ed->menu_open == 0) {
|
||||
const char *file_items[6];
|
||||
file_items[0] = "New Tab Ctrl+N";
|
||||
file_items[1] = "Open... Ctrl+O";
|
||||
file_items[2] = "Open>>> Ctrl+Shift+O";
|
||||
file_items[3] = "Save Ctrl+S";
|
||||
file_items[4] = "Close Tab Ctrl+W";
|
||||
file_items[5] = "Quit Ctrl+Q";
|
||||
draw_dropdown_box(ed, dx, dy, DROP_W, 6 * ITEM_H + 4);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
int iy = dy + 2 + i * ITEM_H;
|
||||
if (ed->menu_hover_item == i) {
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_match_cur);
|
||||
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, dx + 2, iy, DROP_W - 4, ITEM_H);
|
||||
}
|
||||
int base = iy + (ITEM_H + ed->font->ascent - ed->font->descent) / 2;
|
||||
XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, dx + 10, base,
|
||||
(const FcChar8 *)file_items[i], (int)strlen(file_items[i]));
|
||||
}
|
||||
} else if (ed->menu_open == 1) {
|
||||
const char *edit_items[3];
|
||||
edit_items[0] = "Undo Ctrl+Z";
|
||||
edit_items[1] = "Redo Ctrl+Y";
|
||||
edit_items[2] = "Find Ctrl+F";
|
||||
draw_dropdown_box(ed, dx, dy, DROP_W, 3 * ITEM_H + 4);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int iy = dy + 2 + i * ITEM_H;
|
||||
if (ed->menu_hover_item == i) {
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_match_cur);
|
||||
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, dx + 2, iy, DROP_W - 4, ITEM_H);
|
||||
}
|
||||
int base = iy + (ITEM_H + ed->font->ascent - ed->font->descent) / 2;
|
||||
XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, dx + 10, base,
|
||||
(const FcChar8 *)edit_items[i], (int)strlen(edit_items[i]));
|
||||
}
|
||||
} else if (ed->menu_open == 2) {
|
||||
char lbl0[HDR_BUF_LEN];
|
||||
snprintf(lbl0, sizeof(lbl0), "Список функций (%d) ▶", ed->num_symbols);
|
||||
const char *nav_items[3];
|
||||
nav_items[0] = lbl0;
|
||||
nav_items[1] = "Назад Alt+Left";
|
||||
nav_items[2] = "Вперед Alt+Right";
|
||||
int nav_w = DROP_W + 10;
|
||||
draw_dropdown_box(ed, dx, dy, nav_w, 3 * ITEM_H + 4);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int iy = dy + 2 + i * ITEM_H;
|
||||
int is_active = (ed->menu_hover_item == i) || (i == 0 && ed->menu_sub_open);
|
||||
if (is_active) {
|
||||
XSetForeground(ed->dpy, ed->gc, ed->col_match_cur);
|
||||
XFillRectangle(ed->dpy, ed->backbuf, ed->gc, dx + 2, iy, nav_w - 4, ITEM_H);
|
||||
}
|
||||
XftColor *col = &ed->xft_fg;
|
||||
if ((i == 1 && ed->nav_back_count == 0) || (i == 2 && ed->nav_fwd_count == 0)) {
|
||||
col = &ed->xft_lineno;
|
||||
}
|
||||
int base = iy + (ITEM_H + ed->font->ascent - ed->font->descent) / 2;
|
||||
XftDrawStringUtf8(ed->xft_draw, col, ed->font, dx + 10, base,
|
||||
(const FcChar8 *)nav_items[i], (int)strlen(nav_items[i]));
|
||||
}
|
||||
if (ed->menu_sub_open) {
|
||||
render_symbols_submenu(ed, dx + nav_w, dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
int menu_handle_motion(Editor *ed, int mx, int my) {
|
||||
if (ed->menu_open < 0) return 0;
|
||||
if (my >= 0 && my < MENUBAR_H) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (mx >= top_x[i] && mx < top_x[i] + top_w[i]) {
|
||||
if (ed->menu_open != i) {
|
||||
ed->menu_open = i;
|
||||
ed->menu_sub_open = (i == 2);
|
||||
ed->menu_hover_item = -1;
|
||||
ed->menu_hover_sym = -1;
|
||||
if (i == 2) menu_refresh_symbols(ed);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
int dx = top_x[ed->menu_open];
|
||||
int dy = MENUBAR_H;
|
||||
int dw = (ed->menu_open == 2) ? (DROP_W + 10) : DROP_W;
|
||||
int item_cnt = (ed->menu_open == 0) ? 6 : 3;
|
||||
if (mx >= dx && mx < dx + dw && my >= dy + 2 && my < dy + 2 + item_cnt * ITEM_H) {
|
||||
ed->menu_hover_item = (my - (dy + 2)) / ITEM_H;
|
||||
ed->menu_hover_sym = -1;
|
||||
if (ed->menu_open == 2) {
|
||||
if (ed->menu_hover_item == 0 && !ed->menu_sub_open) {
|
||||
menu_refresh_symbols(ed);
|
||||
ed->menu_sub_open = 1;
|
||||
} else if (ed->menu_hover_item != 0) {
|
||||
ed->menu_sub_open = 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (ed->menu_open == 2 && ed->menu_sub_open) {
|
||||
int sub_x = dx + dw;
|
||||
int max_vis = (ed->win_h - dy - editor_status_h(ed) - 8) / ITEM_H;
|
||||
if (max_vis < 1) max_vis = 1;
|
||||
int vis_count = (ed->num_symbols < max_vis) ? ed->num_symbols : max_vis;
|
||||
if (mx >= sub_x && mx < sub_x + SUB_W && my >= dy + 2 && my < dy + 2 + vis_count * ITEM_H) {
|
||||
ed->menu_hover_sym = ed->menu_sym_scroll + ((my - (dy + 2)) / ITEM_H);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
ed->menu_hover_item = -1;
|
||||
ed->menu_hover_sym = -1;
|
||||
return 1;
|
||||
}
|
||||
int menu_handle_click(Editor *ed, int mx, int my, int button) {
|
||||
if (my >= 0 && my < MENUBAR_H) {
|
||||
if (button != Button1) return 1;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (mx >= top_x[i] && mx < top_x[i] + top_w[i]) {
|
||||
if (ed->menu_open == i) {
|
||||
menu_init(ed);
|
||||
} else {
|
||||
ed->menu_open = i;
|
||||
ed->menu_sub_open = (i == 2);
|
||||
ed->menu_hover_item = -1;
|
||||
ed->menu_hover_sym = -1;
|
||||
if (i == 2) menu_refresh_symbols(ed);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
menu_init(ed);
|
||||
return 1;
|
||||
}
|
||||
if (ed->menu_open < 0) return 0;
|
||||
int dx = top_x[ed->menu_open];
|
||||
int dy = MENUBAR_H;
|
||||
int dw = (ed->menu_open == 2) ? (DROP_W + 10) : DROP_W;
|
||||
if (ed->menu_open == 2 && ed->menu_sub_open) {
|
||||
int sub_x = dx + dw;
|
||||
int max_vis = (ed->win_h - dy - editor_status_h(ed) - 8) / ITEM_H;
|
||||
if (max_vis < 1) max_vis = 1;
|
||||
int vis_count = (ed->num_symbols < max_vis) ? ed->num_symbols : max_vis;
|
||||
int sub_h = (vis_count > 0 ? vis_count : 1) * ITEM_H + 4;
|
||||
if (mx >= sub_x && mx < sub_x + SUB_W && my >= dy && my < dy + sub_h) {
|
||||
if (button == Button4) {
|
||||
ed->menu_sym_scroll -= 2;
|
||||
if (ed->menu_sym_scroll < 0) ed->menu_sym_scroll = 0;
|
||||
return 1;
|
||||
}
|
||||
if (button == Button5) {
|
||||
ed->menu_sym_scroll += 2;
|
||||
return 1;
|
||||
}
|
||||
if (button == Button1 && ed->num_symbols > 0) {
|
||||
int idx = ed->menu_sym_scroll + ((my - (dy + 2)) / ITEM_H);
|
||||
if (idx >= 0 && idx < ed->num_symbols) {
|
||||
jump_to_line_with_history(ed, ed->symbols[idx].line, ed->symbols[idx].name);
|
||||
menu_init(ed);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
int item_cnt = (ed->menu_open == 0) ? 6 : 3;
|
||||
if (button == Button1 && mx >= dx && mx < dx + dw && my >= dy + 2 && my < dy + 2 + item_cnt * ITEM_H) {
|
||||
int item = (my - (dy + 2)) / ITEM_H;
|
||||
int open_m = ed->menu_open;
|
||||
if (open_m == 2 && item == 0) {
|
||||
menu_refresh_symbols(ed);
|
||||
ed->menu_sub_open = !ed->menu_sub_open;
|
||||
return 1;
|
||||
}
|
||||
menu_init(ed);
|
||||
if (open_m == 0) {
|
||||
if (item == 0) tabs_new(ed, NULL);
|
||||
else if (item == 1) fbrowser_show(ed);
|
||||
else if (item == 2) editor_open_system_dialog(ed);
|
||||
else if (item == 3) editor_save(ed);
|
||||
else if (item == 4) tabs_request_close(ed, ed->active_tab);
|
||||
else if (item == 5) tabs_request_quit(ed);
|
||||
} else if (open_m == 1) {
|
||||
if (item == 0) history_undo(ed);
|
||||
else if (item == 1) history_redo(ed);
|
||||
else if (item == 2) {
|
||||
ed->status_collapsed = 0;
|
||||
ed->search_mode = 1;
|
||||
}
|
||||
} else if (open_m == 2) {
|
||||
if (item == 1) nav_go_back(ed);
|
||||
else if (item == 2) nav_go_forward(ed);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
menu_init(ed);
|
||||
return 1;
|
||||
}
|
||||
Reference in new issue
Block a user