-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.c
89 lines (74 loc) · 2.24 KB
/
view.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <curses.h>
#include <stdbool.h>
#include "alloc.h"
#include "buffer.h"
#include "view.h"
View *view_new(int rows, int cols, int begin_row, int begin_col, Line *top) {
View *view = malloc_or_exit(sizeof(View));
view->win = newwin(cols, rows, begin_col, begin_row);
view->top_line = top;
view->top_row = 0;
view->top_col = 0;
keypad(view->win, true);
return view;
}
void view_destroy(View *view) {
delwin(view->win);
free(view);
}
void view_scroll(View *view, Line *cur_line, int row, int col) {
if (col < 0) {
/* Move up */
while (view->top_line->prev != NULL && col < 0) {
view->top_line = view->top_line->prev;
view->top_col--;
col++;
}
} else {
/* Move down */
while (view->top_line->next != NULL && col > 0) {
view->top_line = view->top_line->next;
view->top_col++;
col--;
}
}
int new_row = (int) view->top_row + row;
if (new_row < 0)
view->top_row = 0;
else if ((size_t) new_row > cur_line->len)
view->top_row = cur_line->len - 1;
else
view->top_row = new_row;
}
void view_redraw_line(View *view, Line *line, int col) {
if (line->len > view->top_row)
mvwprintw(view->win, col, 0, "%.*s",
getmaxx(view->win), &line->content[view->top_row]);
else
wmove(view->win, col, 0);
/* If the current line is too long to fit on one
* line, the cursor will move down to the next line.
* To avoid clearing the next line, we check if it's
* still on the right line first.
*/
int x, y;
getyx(view->win, y, x);
if (y == col)
wclrtoeol(view->win);
/* Shut up the compiler about x being unused. */
(void) x;
}
void view_redraw_lines(View *view, Line *line, int col) {
for (; line != NULL && col <= getmaxy(view->win); col++) {
view_redraw_line(view, line, col);
line = line->next;
}
wclrtobot(view->win);
}
void view_redraw_all_lines(View *view) {
view_redraw_lines(view, view->top_line, 0);
}
void view_update_cursor(View *view, int row, int col) {
wmove(view->win, view_get_col(view, col), view_get_row(view, row));
wrefresh(view->win);
}