-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw.c
165 lines (148 loc) · 3.2 KB
/
draw.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* $Id: draw.c,v 1.15 2024/12/05 00:38:44 tom Exp $ */
#include <vttest.h>
#include <draw.h>
#include <esc.h>
/*
* Useful drawing functions for vttest.
*/
static int
check_box_params(BOX *box)
{
if (box->top >= box->bottom || box->left >= box->right) {
printxx("The screen is too small for box with margins %d,%d.", box->top, box->left);
holdit();
return -1;
}
return 0;
}
/*
* Compute box params for given vertical and horizontal margin,
* returns -1 if the screen is too small, 0 otherwise.
*/
int
make_box_params(BOX *box, int vmargin, int hmargin)
{
box->top = vmargin;
box->bottom = get_bottom_margin(max_lines) - vmargin;
box->left = hmargin;
box->right = get_right_margin() - hmargin;
return check_box_params(box);
}
void
draw_box_outline(const BOX *box, int mark)
{
int j;
int tlc = (mark < 0) ? 'l' : mark;
int trc = (mark < 0) ? 'k' : mark;
int llc = (mark < 0) ? 'm' : mark;
int lrc = (mark < 0) ? 'j' : mark;
int vrt = (mark < 0) ? 'x' : mark;
int hrz = (mark < 0) ? 'q' : mark;
int dot;
if (mark < 0)
scs(0, '0');
for (j = box->top, dot = tlc; j < box->bottom; j++) {
__(cup(j, box->left), putchar(dot));
dot = vrt;
}
for (j = box->top, dot = trc; j < box->bottom; j++) {
__(cup(j, box->right), putchar(dot));
dot = vrt;
}
cup(box->top, box->left + 1);
for (j = box->left + 1; j < box->right; j++)
putchar(hrz);
cup(box->bottom, box->left + 1);
for (j = box->left + 1; j < box->right; j++)
putchar(hrz);
__(cup(box->bottom, box->left), putchar(llc));
__(cup(box->bottom, box->right), putchar(lrc));
if (mark < 0)
scs(0, 'B');
}
void
draw_box_filled(const BOX *box, int mark)
{
int i, j;
int ch = (mark < 0) ? 'A' : mark;
for (i = box->top; i < box->bottom; i++) {
cup(i, box->left);
for (j = box->left; j < box->right; j++) {
putchar(ch);
if (mark < 0)
ch = ((ch - 'A' + 1) % 26) + 'A';
}
}
}
static int
next_word(const char *s)
{
const char *base;
while (*s == ' ')
s++;
base = s;
while (*s && *s != ' ')
s++;
return (int) (s - base);
}
void
draw_box_caption(const BOX *box, int margin, const char **c)
{
int x0 = (box->left + margin);
int y0 = (box->top + margin);
int x1 = (box->right - margin);
int y1 = (box->bottom - margin);
int x = x0;
int y = y0;
const char *s;
while ((s = *c++) != NULL) {
int t;
while ((t = *s++) != 0) {
if (x == x0) {
if (t == ' ')
continue;
cup(y, x++);
putchar(' ');
}
putchar(t);
x++;
if ((t == ' ') && (next_word(s) > (x1 - x - 2))) {
while (x < x1) {
putchar(' ');
x++;
}
}
if (x >= x1) {
putchar(' ');
x = x0;
y++;
}
}
}
while (y <= y1) {
if (x == x0) {
cup(y, x);
}
putchar(' ');
if (++x >= x1) {
putchar(' ');
x = x0;
y++;
}
}
}
void
ruler(int row, int width)
{
int col;
vt_move(row, 1);
for (col = 1; col <= width; ++col) {
int ch = (((col % 10) == 0)
? ('0' + (col / 10) % 10)
: (((col % 5) == 0)
? '+'
: '-'));
putchar(ch);
}
putchar('\n');
}