commit 01167201845aad62bd3efe45e8559e65a03908bb
parent 0f1b2a2a20522e638c97d39cdd5f42c5f11fba3d
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Sun, 12 Dec 2021 07:54:52 +0100
Merge pull request #79 from in0rdr/feature/60/mouse-support
Feature/60/mouse support
Diffstat:
M | src/diary.c | | | 61 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 61 insertions(+), 0 deletions(-)
diff --git a/src/diary.c b/src/diary.c
@@ -604,6 +604,9 @@ int main(int argc, char** argv) {
WINDOW* prev = newwin(prev_height, prev_width, 1, ASIDE_WIDTH + CAL_WIDTH);
display_entry(CONFIG.dir, diary_dir_size, &today, prev, prev_width);
+ mmask_t oldmask;
+ mousemask(ALL_MOUSE_EVENTS, &oldmask);
+ MEVENT event;
do {
ch = wgetch(cal);
@@ -685,6 +688,62 @@ int main(int argc, char** argv) {
}
curs_set(0);
break;
+ // jump to specific date using the mouse
+ case KEY_MOUSE:
+ if(getmouse(&event) == OK) {
+ if (wenclose(cal, event.y, event.x)) {
+ if(event.bstate & (BUTTON1_PRESSED|BUTTON1_CLICKED)) {
+ // regular left-mouse button click or tap on a touchpad:
+ // BUTTON1_PRESSED detects touch events (touch pads)
+ // BUTTON1_CLICKED detects click events (mouse)
+ int cy, cx;
+ getyx(cal, cy, cx);
+ int pad_cy = cy - pad_pos + 1;
+ int pad_cx = cx + ASIDE_WIDTH;
+
+ int diff_weeks = abs(pad_cy - event.y);
+ int diff_wdays = abs((pad_cx - event.x) / 3);
+
+ int diff_days = 0;
+ if (pad_cy > event.y) {
+ // current position cy is more recent, jump backward by diff_days
+ diff_days -= diff_weeks * 7;
+ } else {
+ // new y event is more recent, jump forward by diff_days
+ diff_days = diff_weeks * 7;
+ }
+
+ if (cx > event.x) {
+ // jump backwards, mouse click was before current position
+ diff_days -= diff_wdays;
+ } else {
+ // jump forward, mouse click was after current position
+ diff_days += diff_wdays;
+ }
+
+ curs_date.tm_mday += diff_days;
+ time_t new_date = mktime(&curs_date);
+ mv_valid = go_to(cal, aside, new_date, &pad_pos, &curs_date, &cal_start, &cal_end);
+
+ if (event.bstate & (BUTTON1_DOUBLE_CLICKED)) {
+ // unset previous click events
+ event.bstate &= ~BUTTON1_DOUBLE_CLICKED;
+ event.bstate &= ~BUTTON1_PRESSED;
+ event.bstate &= ~BUTTON1_CLICKED;
+ // simulate edit char to enter edit mode
+ ungetch('e');
+ }
+ }
+ if (event.bstate & (BUTTON1_DOUBLE_CLICKED)) {
+ // double-click left-mouse button or double tap on a touchpad
+ // simulate single-click to select the new date
+ event.bstate |= BUTTON1_PRESSED;
+ ungetmouse(&event);
+ }
+ }
+ }
+ break;
+
// today shortcut
case 't':
new_date = today;
@@ -738,7 +797,9 @@ int main(int argc, char** argv) {
break;
}
curs_set(1);
+ mousemask(oldmask, NULL);
system(ecmd);
+ mousemask(ALL_MOUSE_EVENTS, &oldmask);
curs_set(0);
keypad(cal, TRUE);