From 95f46081dc27ed1f34a6f6866cb8231a9a7ceb28 Mon Sep 17 00:00:00 2001 From: Matthew Beaudouin-Lafon Date: Thu, 30 May 2019 01:00:29 +0200 Subject: [PATCH 1/4] added reverse video to current canvas mode --- src/fe_modes.c | 26 +++++++++++++++++++++++++- src/frontend.c | 6 ++++++ src/frontend.h | 2 ++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/fe_modes.c b/src/fe_modes.c index 8d17a5f..4e16839 100644 --- a/src/fe_modes.c +++ b/src/fe_modes.c @@ -270,19 +270,43 @@ int mode_picker(reason_t reason, State *state) { char msg[128] = ""; int num_left = sizeof(msg) / sizeof(char); + int write_x = 0; + char buffer[16]; + + // these variables are used to reverse-video the current canvas mode + int selected_x = 0; + int selected_num_char = -1; + bool found_selected = FALSE; for (int i = mode_first; i < mode_list_end; i++) { int num_to_write = snprintf(buffer, sizeof(buffer) / sizeof(char), - "%i: %s|", i - mode_first + 1, modes[i].name); + " [%i] %s ", i - mode_first + 1, modes[i].name); + if (num_left - num_to_write < 0) { break; } strncat(msg, buffer, num_to_write); num_left -= num_to_write; + + // Find the current canvas mode + if (state->last_canvas_mode == i) { + found_selected = TRUE; + selected_num_char = num_to_write; + } + + // Keep track of where the selected mode text starts in the window + if (!found_selected) { + selected_x += num_to_write; + } } print_mode_win(msg); + // Reverse-video the current canvas mode + if (selected_num_char != -1) { + change_mode_attribute(0, selected_x + 1, selected_num_char - 1, A_REVERSE); + } + // INTERPRET KEYS if (reason == NEW_KEY) { // only accept characters within the bounds of the list diff --git a/src/frontend.c b/src/frontend.c index 7276e61..9a9e2b4 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -528,6 +528,12 @@ int print_mode_win(char *format, ...) { return res; } +/* Change the mode attribute + */ +int change_mode_attribute(int y, int x, int num_ch, attr_t attribute) { + mvwchgat(status_interface->mode_win, y, x, num_ch, attribute, 0, NULL); +} + /* Update the info subwindow in status_win with relevant info. * * Prints the current mode, cursor coordinates, and canvas dimensions diff --git a/src/frontend.h b/src/frontend.h index 6558fc8..75d9795 100644 --- a/src/frontend.h +++ b/src/frontend.h @@ -30,6 +30,8 @@ void destroy_win(); int print_msg_win(char *format, ...); int print_info_win(char *format, ...); +int mva_print_mode_win(int y, attr_t attribute, char *format, ...); +int change_mode_attribute(int y, int x, int num_ch, attr_t attribute); int print_mode_win(char *format, ...); void update_info_win(const Mode_ID current_mode, const int x, const int y, const int w, const int h); From dd160da6b996b98580c32edad90101bfdb8a327d Mon Sep 17 00:00:00 2001 From: Matthew Beaudouin-Lafon Date: Thu, 30 May 2019 01:30:58 +0200 Subject: [PATCH 2/4] changed tab in mode_picker to cycle through modes (doesn't show it though) --- src/fe_modes.c | 25 ++++++++++++++++++++----- src/frontend.c | 6 +++--- src/frontend.h | 3 +-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/fe_modes.c b/src/fe_modes.c index 4e16839..2230524 100644 --- a/src/fe_modes.c +++ b/src/fe_modes.c @@ -122,6 +122,19 @@ void switch_mode(Mode_ID new_mode, State *state) { refresh_screen(); } +Mode_ID next_canvas_mode(Mode_ID mode) { + int mode_first = MODE_PICKER + 1; // beginning of selectable modes + int mode_list_end = LAST; // length of total mode list + + if (mode < mode_first || mode_list_end <= mode) { + logd("mode \"%s\" is not a canvas mode", mode); + return mode; + } + + // loop properly between mode_first and last mode + return ((mode - mode_first) + 1) % (mode_list_end - mode_first) + mode_first; +} + /* Handler run in frontend main loop. * * Interprets keypresses, manages global keys, and passes data to modes. @@ -148,8 +161,9 @@ int master_handler(State *state, WINDOW *canvas_win, WINDOW *status_win) { } else if (c == KEY_TAB) { // switching modes if (state->current_mode == MODE_PICKER && state->last_canvas_mode != MODE_PICKER) { - logd("Reverting to last mode\n"); - switch_mode(state->last_canvas_mode, state); + // logd("Reverting to last mode\n"); + // switch_mode(state->last_canvas_mode, state); + state->last_canvas_mode = next_canvas_mode(state->last_canvas_mode); } else { switch_mode(MODE_PICKER, state); } @@ -270,8 +284,6 @@ int mode_picker(reason_t reason, State *state) { char msg[128] = ""; int num_left = sizeof(msg) / sizeof(char); - int write_x = 0; - char buffer[16]; // these variables are used to reverse-video the current canvas mode @@ -304,7 +316,7 @@ int mode_picker(reason_t reason, State *state) { // Reverse-video the current canvas mode if (selected_num_char != -1) { - change_mode_attribute(0, selected_x + 1, selected_num_char - 1, A_REVERSE); + highlight_mode_text(selected_x, selected_num_char); } // INTERPRET KEYS @@ -315,6 +327,9 @@ int mode_picker(reason_t reason, State *state) { Mode_ID new_mode = mode_first + state->ch_in - '1'; switch_mode(new_mode, state); return 0; + } else if (state->ch_in == KEY_ENTER) { + switch_mode(state->last_canvas_mode, state); + return 0; } } return 0; diff --git a/src/frontend.c b/src/frontend.c index 9a9e2b4..41b2843 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -528,10 +528,10 @@ int print_mode_win(char *format, ...) { return res; } -/* Change the mode attribute +/* Reverse-video the text starting at x in mode window, for num_ch characters. */ -int change_mode_attribute(int y, int x, int num_ch, attr_t attribute) { - mvwchgat(status_interface->mode_win, y, x, num_ch, attribute, 0, NULL); +void highlight_mode_text(int x, int num_ch) { + mvwchgat(status_interface->mode_win, 0, x, num_ch, A_REVERSE, 0, NULL); } /* Update the info subwindow in status_win with relevant info. diff --git a/src/frontend.h b/src/frontend.h index 75d9795..c61a46f 100644 --- a/src/frontend.h +++ b/src/frontend.h @@ -30,8 +30,7 @@ void destroy_win(); int print_msg_win(char *format, ...); int print_info_win(char *format, ...); -int mva_print_mode_win(int y, attr_t attribute, char *format, ...); -int change_mode_attribute(int y, int x, int num_ch, attr_t attribute); +void highlight_mode_text(int x, int num_ch); int print_mode_win(char *format, ...); void update_info_win(const Mode_ID current_mode, const int x, const int y, const int w, const int h); From e5bb79f551208c9d76ac5457e81977c157c8367f Mon Sep 17 00:00:00 2001 From: Matthew Beaudouin-Lafon Date: Thu, 30 May 2019 01:44:42 +0200 Subject: [PATCH 3/4] refresh mode picker on tab while in mode picker --- src/fe_modes.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/fe_modes.c b/src/fe_modes.c index 2230524..411b56a 100644 --- a/src/fe_modes.c +++ b/src/fe_modes.c @@ -135,6 +135,19 @@ Mode_ID next_canvas_mode(Mode_ID mode) { return ((mode - mode_first) + 1) % (mode_list_end - mode_first) + mode_first; } +Mode_ID previous_canvas_mode(Mode_ID mode) { + int mode_first = MODE_PICKER + 1; // beginning of selectable modes + int mode_list_end = LAST; // length of total mode list + + if (mode < mode_first || mode_list_end <= mode) { + logd("mode \"%s\" is not a canvas mode", mode); + return mode; + } + + // loop properly between mode_first and last mode + return ((mode - mode_first) - 1) % (mode_list_end - mode_first) + mode_first; +} + /* Handler run in frontend main loop. * * Interprets keypresses, manages global keys, and passes data to modes. @@ -161,13 +174,26 @@ int master_handler(State *state, WINDOW *canvas_win, WINDOW *status_win) { } else if (c == KEY_TAB) { // switching modes if (state->current_mode == MODE_PICKER && state->last_canvas_mode != MODE_PICKER) { - // logd("Reverting to last mode\n"); - // switch_mode(state->last_canvas_mode, state); state->last_canvas_mode = next_canvas_mode(state->last_canvas_mode); + + // update mode_picker() to redraw the highlight + state->ch_in = c; + call_mode(state->current_mode, NEW_KEY, state); } else { switch_mode(MODE_PICKER, state); } return 0; + } else if (c == KEY_SHIFT_TAB) { + if (state->current_mode == MODE_PICKER && + state->last_canvas_mode != MODE_PICKER) { + state->last_canvas_mode = previous_canvas_mode(state->last_canvas_mode); + + // update mode_picker() to redraw the highlight + state->ch_in = c; + call_mode(state->current_mode, NEW_KEY, state); + } else { + switch_mode(MODE_PICKER, state); + } } else if (c == KEY_NPAGE || c == KEY_PPAGE || c == KEY_SLEFT || c == KEY_SRIGHT) { // shift view down/up/left/right From 338f5208aab1456ee9e5b779fd861af06a5da489 Mon Sep 17 00:00:00 2001 From: Matthew Beaudouin-Lafon Date: Thu, 30 May 2019 01:52:54 +0200 Subject: [PATCH 4/4] cleaned up next/previous canvas mode functions --- src/fe_modes.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/fe_modes.c b/src/fe_modes.c index 411b56a..a8527e7 100644 --- a/src/fe_modes.c +++ b/src/fe_modes.c @@ -122,7 +122,7 @@ void switch_mode(Mode_ID new_mode, State *state) { refresh_screen(); } -Mode_ID next_canvas_mode(Mode_ID mode) { +Mode_ID add_mod_canvas_mode(Mode_ID mode, int n) { int mode_first = MODE_PICKER + 1; // beginning of selectable modes int mode_list_end = LAST; // length of total mode list @@ -132,20 +132,13 @@ Mode_ID next_canvas_mode(Mode_ID mode) { } // loop properly between mode_first and last mode - return ((mode - mode_first) + 1) % (mode_list_end - mode_first) + mode_first; + return ((mode - mode_first) + n) % (mode_list_end - mode_first) + mode_first; } -Mode_ID previous_canvas_mode(Mode_ID mode) { - int mode_first = MODE_PICKER + 1; // beginning of selectable modes - int mode_list_end = LAST; // length of total mode list - - if (mode < mode_first || mode_list_end <= mode) { - logd("mode \"%s\" is not a canvas mode", mode); - return mode; - } +Mode_ID next_canvas_mode(Mode_ID mode) { return add_mod_canvas_mode(mode, 1); } - // loop properly between mode_first and last mode - return ((mode - mode_first) - 1) % (mode_list_end - mode_first) + mode_first; +Mode_ID previous_canvas_mode(Mode_ID mode) { + return add_mod_canvas_mode(mode, -1); } /* Handler run in frontend main loop.