Files

276 lines
9.5 KiB
C
Raw Permalink Normal View History

2026-11-30 22:12:00 +03:00
#include "input.h"
/* Возвращает: 0 - нет, 1 - статус-бар, 2 - вверх, 3 - вниз, 4 - влево, 5 - вправо */
static int get_hovered_ui_button(const Editor *ed, int mx, int my) {
if (ed->menu_open >= 0 || ed->confirm_mode != 0 || ed->fbrowser_open) return 0;
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;
int h_track_y = ed->win_h - st_h - SCROLLBAR_W;
int h_full_x = GUTTER_W;
int h_full_w = ed->win_w - GUTTER_W - SCROLLBAR_W;
if (mx >= 0 && mx < GUTTER_W && my >= h_track_y && my < h_track_y + SCROLLBAR_W) {
return 1;
}
if (v_full_h >= SCROLLBAR_W * 2 && mx >= v_track_x && mx < ed->win_w) {
if (my >= v_full_y && my < v_full_y + SCROLLBAR_W) return 2;
if (my >= v_full_y + v_full_h - SCROLLBAR_W && my < v_full_y + v_full_h) return 3;
}
if (h_full_w >= SCROLLBAR_W * 2 && my >= h_track_y && my < h_track_y + SCROLLBAR_W) {
if (mx >= h_full_x && mx < h_full_x + SCROLLBAR_W) return 4;
if (mx >= v_track_x - SCROLLBAR_W && mx < v_track_x) return 5;
}
return 0;
}
int mouse_update_cursor(Editor *ed, int mx, int my, unsigned int state) {
(void)state;
int old_btn = ed->hover_btn;
ed->hover_btn = get_hovered_ui_button(ed, mx, my);
int want_hand = (ed->hover_sym_line >= 0) || (ed->hover_btn > 0) ||
(ed->hover_tab != -1) ||
(ed->confirm_mode != 0 && ed->confirm_hover_btn >= 0) ||
(ed->fbrowser_open && ed->fb_hover_btn >= 0);
if (want_hand && !ed->cursor_is_hand) {
XDefineCursor(ed->dpy, ed->win, ed->cursor_hand);
ed->cursor_is_hand = 1;
} else if (!want_hand && ed->cursor_is_hand) {
XUndefineCursor(ed->dpy, ed->win);
ed->cursor_is_hand = 0;
}
return (old_btn != ed->hover_btn);
}
static void set_cursor_from_mouse(Editor *ed, int mx, int my) {
int target_y = ed->scroll_y + ((my - TOP_UI_H) / ed->line_h);
if (target_y >= ed->num_lines) target_y = ed->num_lines - 1;
if (target_y < 0) target_y = 0;
ed->cy = target_y;
int target_col = ed->scroll_x + ((mx - GUTTER_W - 8 + ed->char_w / 2) / ed->char_w);
if (target_col < 0) target_col = 0;
ed->cx = utf8_byte_offset(ed->lines[ed->cy], target_col);
}
static void update_vscroll_from_mouse(Editor *ed, int my) {
int v_full_h = ed->win_h - TOP_UI_H - editor_status_h(ed) - SCROLLBAR_W;
int visible_lines = v_full_h / ed->line_h;
int max_sy = ed->num_lines - visible_lines;
if (max_sy <= 0) return;
int v_track_y = TOP_UI_H + SCROLLBAR_W;
int v_track_h = v_full_h - SCROLLBAR_W * 2;
if (v_track_h <= 0) return;
int thumb_h = (v_track_h * visible_lines) / ed->num_lines;
if (thumb_h < 16) thumb_h = 16;
int usable = v_track_h - thumb_h;
if (usable <= 0) return;
int pos = (my - v_track_y) - ed->drag_offset;
if (pos < 0) pos = 0;
if (pos > usable) pos = usable;
ed->scroll_y = (pos * max_sy) / usable;
}
static void update_hscroll_from_mouse(Editor *ed, int mx) {
int h_full_w = ed->win_w - GUTTER_W - SCROLLBAR_W;
int visible_cols = (h_full_w - 8) / ed->char_w;
int max_cols = editor_max_line_len(ed);
int max_sx = max_cols - visible_cols;
if (max_sx <= 0) return;
int h_track_x = GUTTER_W + SCROLLBAR_W;
int h_track_w = h_full_w - SCROLLBAR_W * 2;
if (h_track_w <= 0) return;
int thumb_w = (h_track_w * visible_cols) / max_cols;
if (thumb_w < 16) thumb_w = 16;
int usable = h_track_w - thumb_w;
if (usable <= 0) return;
int pos = (mx - h_track_x) - ed->drag_offset;
if (pos < 0) pos = 0;
if (pos > usable) pos = usable;
ed->scroll_x = (pos * max_sx) / usable;
}
void editor_handle_button_press(Editor *ed, XButtonEvent *bev) {
int mx = bev->x, my = bev->y;
if (ed->confirm_mode != 0) {
if (bev->button == Button1) confirm_handle_click(ed, mx, my);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (ed->fbrowser_open) {
fbrowser_handle_click(ed, mx, my, bev->button, bev->time);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (menu_handle_click(ed, mx, my, bev->button)) {
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (tabs_handle_click(ed, mx, my, bev->button)) {
tabs_handle_motion(ed, mx, my);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
int st_h = editor_status_h(ed);
int status_y = ed->win_h - st_h;
int h_track_y = ed->win_h - st_h - SCROLLBAR_W;
if (bev->button == Button1 && mx >= 0 && mx < GUTTER_W &&
my >= h_track_y && my < h_track_y + SCROLLBAR_W) {
ed->pressed_btn = 1;
ed->status_collapsed = !ed->status_collapsed;
editor_ensure_cursor_visible(ed);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (bev->button == Button1 && !ed->status_collapsed && my >= status_y) {
if (ed->search_mode && mx >= ed->chk_x1 && mx <= ed->chk_x2) {
ed->search_highlight_all = !ed->search_highlight_all;
}
return;
}
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;
int h_full_x = GUTTER_W;
int h_full_w = ed->win_w - GUTTER_W - SCROLLBAR_W;
int visible_lines = (v_full_h > 0) ? (v_full_h / ed->line_h) : 1;
int visible_cols = (h_full_w > 8) ? ((h_full_w - 8) / ed->char_w) : 1;
int max_sy = ed->num_lines - visible_lines;
if (max_sy < 0) max_sy = 0;
int max_sx = editor_max_line_len(ed) - visible_cols;
if (max_sx < 0) max_sx = 0;
if (bev->button == Button4) {
ed->scroll_y -= 3;
if (ed->scroll_y < 0) ed->scroll_y = 0;
return;
}
if (bev->button == Button5) {
ed->scroll_y += 3;
if (ed->scroll_y > max_sy) ed->scroll_y = max_sy;
return;
}
if (bev->button != Button1) return;
if (mx >= v_track_x && my >= v_full_y && my < v_full_y + v_full_h) {
if (my < v_full_y + SCROLLBAR_W) {
ed->pressed_btn = 2;
if (ed->scroll_y > 0) ed->scroll_y--;
return;
}
if (my >= v_full_y + v_full_h - SCROLLBAR_W) {
ed->pressed_btn = 3;
if (ed->scroll_y < max_sy) ed->scroll_y++;
return;
}
if (max_sy > 0) {
int v_track_y = v_full_y + SCROLLBAR_W;
int v_track_h = v_full_h - SCROLLBAR_W * 2;
int thumb_h = (v_track_h * visible_lines) / ed->num_lines;
if (thumb_h < 16) thumb_h = 16;
int thumb_y = v_track_y + (ed->scroll_y * (v_track_h - thumb_h)) / max_sy;
ed->drag_vscroll = 1;
if (my >= thumb_y && my <= thumb_y + thumb_h) ed->drag_offset = my - thumb_y;
else {
ed->drag_offset = thumb_h / 2;
update_vscroll_from_mouse(ed, my);
}
}
} else if (my >= h_track_y && my < h_track_y + SCROLLBAR_W && mx >= h_full_x && mx < v_track_x) {
if (mx < h_full_x + SCROLLBAR_W) {
ed->pressed_btn = 4;
if (ed->scroll_x > 0) ed->scroll_x--;
return;
}
if (mx >= v_track_x - SCROLLBAR_W) {
ed->pressed_btn = 5;
if (ed->scroll_x < max_sx) ed->scroll_x++;
return;
}
if (max_sx > 0) {
int h_track_x = h_full_x + SCROLLBAR_W;
int h_track_w = h_full_w - SCROLLBAR_W * 2;
int max_cols = editor_max_line_len(ed);
int thumb_w = (h_track_w * visible_cols) / max_cols;
if (thumb_w < 16) thumb_w = 16;
int thumb_x = h_track_x + (ed->scroll_x * (h_track_w - thumb_w)) / max_sx;
ed->drag_hscroll = 1;
if (mx >= thumb_x && mx <= thumb_x + thumb_w) ed->drag_offset = mx - thumb_x;
else {
ed->drag_offset = thumb_w / 2;
update_hscroll_from_mouse(ed, mx);
}
}
} else if (mx >= GUTTER_W && mx < v_track_x && my >= TOP_UI_H && my < h_track_y) {
int ctrl = (bev->state & ControlMask);
int shift = (bev->state & ShiftMask);
if (ctrl) {
set_cursor_from_mouse(ed, mx, my);
ed->has_sel = 0;
ed->hover_sym_line = -1;
menu_goto_symbol_at_cursor(ed);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (shift) {
if (!ed->has_sel) {
ed->sel_ax = ed->cx;
ed->sel_ay = ed->cy;
}
set_cursor_from_mouse(ed, mx, my);
ed->has_sel = (ed->cx != ed->sel_ax || ed->cy != ed->sel_ay);
} else {
set_cursor_from_mouse(ed, mx, my);
ed->has_sel = 0;
ed->sel_ax = ed->cx;
ed->sel_ay = ed->cy;
}
ed->drag_select = 1;
}
}
void editor_handle_motion(Editor *ed, XMotionEvent *mev) {
if (ed->drag_vscroll) update_vscroll_from_mouse(ed, mev->y);
else if (ed->drag_hscroll) update_hscroll_from_mouse(ed, mev->x);
else if (ed->drag_select) {
set_cursor_from_mouse(ed, mev->x, mev->y);
ed->has_sel = (ed->cx != ed->sel_ax || ed->cy != ed->sel_ay);
}
}
void editor_handle_button_release(Editor *ed, XButtonEvent *bev) {
if (bev->button == Button1) {
ed->drag_vscroll = 0;
ed->drag_hscroll = 0;
ed->drag_select = 0;
ed->pressed_btn = 0;
}
}