selection: add a selection API

This commit is contained in:
Daniel Eklöf 2019-07-11 09:51:51 +02:00
parent 1c861e5d69
commit bcf763d417
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
6 changed files with 117 additions and 30 deletions

70
selection.c Normal file
View file

@ -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);
}
}