From bcf763d41776705ad62972e766d73fd9533bbb9e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 11 Jul 2019 09:51:51 +0200 Subject: selection: add a selection API --- selection.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 selection.c (limited to 'selection.c') diff --git a/selection.c b/selection.c new file mode 100644 index 00000000..b0b688b9 --- /dev/null +++ b/selection.c @@ -0,0 +1,70 @@ +#include "selection.h" + +#define LOG_MODULE "selection" +#define LOG_ENABLE_DBG 1 +#include "log.h" +#include "render.h" + +#define min(x, y) ((x) < (y) ? (x) : (y)) +#define max(x, y) ((x) > (y) ? (x) : (y)) + +void +selection_start(struct terminal *term, int col, int row) +{ + selection_cancel(term); + + LOG_DBG("selection started at %d,%d", row, col); + term->selection.start = (struct coord){col, row}; + term->selection.end = (struct coord){-1, -1}; +} + +void +selection_update(struct terminal *term, int col, int row) +{ + LOG_DBG("selection updated: start = %d,%d, end = %d,%d -> %d, %d", + term->selection.start.row, term->selection.start.col, + term->selection.end.row, term->selection.end.col, + row, col); + + term->selection.end = (struct coord){col, term->grid->view + row}; + + assert(term->selection.start.row != -1 && term->selection.end.row != -1); + term_damage_rows_in_view( + term, + min(term->selection.start.row, term->selection.end.row) - term->grid->view, + max(term->selection.start.row, term->selection.end.row) - term->grid->view); + + if (term->frame_callback == NULL) + grid_render(term); +} + +void +selection_finalizie(struct terminal *term) +{ + assert(term->selection.start.row != -1); + assert(term->selection.end.row != -1); +} + +void +selection_cancel(struct terminal *term) +{ + LOG_DBG("selection cancelled: start = %d,%d end = %d,%d", + term->selection.start.row, term->selection.start.col, + term->selection.end.row, term->selection.end.col); + + int start_row = term->selection.start.row; + int end_row = term->selection.end.row; + + term->selection.start = (struct coord){-1, -1}; + term->selection.end = (struct coord){-1, -1}; + + if (start_row != -1 && end_row != -1) { + term_damage_rows_in_view( + term, + min(start_row, end_row) - term->grid->view, + max(start_row, end_row) - term->grid->view); + + if (term->frame_callback == NULL) + grid_render(term); + } +} -- cgit v1.3