aboutsummaryrefslogtreecommitdiff
path: root/selection.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-11 07:51:51 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-11 15:38:01 +0000
commitbcf763d41776705ad62972e766d73fd9533bbb9e (patch)
tree55e146eed65d0666a9fb1b3d47da49ed4c61df3a /selection.c
parentselection: only left button can be used for selection (diff)
selection: add a selection API
Diffstat (limited to 'selection.c')
-rw-r--r--selection.c70
1 files changed, 70 insertions, 0 deletions
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);
+ }
+}