| 1 |
/* $Id: draw.c,v 1.15 2024/12/05 00:38:44 tom Exp $ */ |
| 2 |
|
| 3 |
#include <vttest.h> |
| 4 |
#include <draw.h> |
| 5 |
#include <esc.h> |
| 6 |
|
| 7 |
/* |
| 8 |
* Useful drawing functions for vttest. |
| 9 |
*/ |
| 10 |
|
| 11 |
static int |
| 12 |
check_box_params(BOX *box) |
| 13 |
{ |
| 14 |
if (box->top >= box->bottom || box->left >= box->right) { |
| 15 |
printxx("The screen is too small for box with margins %d,%d.", box->top, box->left); |
| 16 |
holdit(); |
| 17 |
return -1; |
| 18 |
} |
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
/* |
| 23 |
* Compute box params for given vertical and horizontal margin, |
| 24 |
* returns -1 if the screen is too small, 0 otherwise. |
| 25 |
*/ |
| 26 |
int |
| 27 |
make_box_params(BOX *box, int vmargin, int hmargin) |
| 28 |
{ |
| 29 |
box->top = vmargin; |
| 30 |
box->bottom = get_bottom_margin(max_lines) - vmargin; |
| 31 |
box->left = hmargin; |
| 32 |
box->right = get_right_margin() - hmargin; |
| 33 |
return check_box_params(box); |
| 34 |
} |
| 35 |
|
| 36 |
void |
| 37 |
draw_box_outline(const BOX *box, int mark) |
| 38 |
{ |
| 39 |
int j; |
| 40 |
int tlc = (mark < 0) ? 'l' : mark; |
| 41 |
int trc = (mark < 0) ? 'k' : mark; |
| 42 |
int llc = (mark < 0) ? 'm' : mark; |
| 43 |
int lrc = (mark < 0) ? 'j' : mark; |
| 44 |
int vrt = (mark < 0) ? 'x' : mark; |
| 45 |
int hrz = (mark < 0) ? 'q' : mark; |
| 46 |
int dot; |
| 47 |
|
| 48 |
if (mark < 0) |
| 49 |
scs(0, '0'); |
| 50 |
|
| 51 |
for (j = box->top, dot = tlc; j < box->bottom; j++) { |
| 52 |
__(cup(j, box->left), putchar(dot)); |
| 53 |
dot = vrt; |
| 54 |
} |
| 55 |
for (j = box->top, dot = trc; j < box->bottom; j++) { |
| 56 |
__(cup(j, box->right), putchar(dot)); |
| 57 |
dot = vrt; |
| 58 |
} |
| 59 |
|
| 60 |
cup(box->top, box->left + 1); |
| 61 |
for (j = box->left + 1; j < box->right; j++) |
| 62 |
putchar(hrz); |
| 63 |
|
| 64 |
cup(box->bottom, box->left + 1); |
| 65 |
for (j = box->left + 1; j < box->right; j++) |
| 66 |
putchar(hrz); |
| 67 |
|
| 68 |
__(cup(box->bottom, box->left), putchar(llc)); |
| 69 |
__(cup(box->bottom, box->right), putchar(lrc)); |
| 70 |
|
| 71 |
if (mark < 0) |
| 72 |
scs(0, 'B'); |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
draw_box_filled(const BOX *box, int mark) |
| 77 |
{ |
| 78 |
int i, j; |
| 79 |
int ch = (mark < 0) ? 'A' : mark; |
| 80 |
|
| 81 |
for (i = box->top; i < box->bottom; i++) { |
| 82 |
cup(i, box->left); |
| 83 |
for (j = box->left; j < box->right; j++) { |
| 84 |
putchar(ch); |
| 85 |
if (mark < 0) |
| 86 |
ch = ((ch - 'A' + 1) % 26) + 'A'; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static int |
| 92 |
next_word(const char *s) |
| 93 |
{ |
| 94 |
const char *base; |
| 95 |
while (*s == ' ') |
| 96 |
s++; |
| 97 |
base = s; |
| 98 |
while (*s && *s != ' ') |
| 99 |
s++; |
| 100 |
return (int) (s - base); |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
draw_box_caption(const BOX *box, int margin, const char **c) |
| 105 |
{ |
| 106 |
int x0 = (box->left + margin); |
| 107 |
int y0 = (box->top + margin); |
| 108 |
int x1 = (box->right - margin); |
| 109 |
int y1 = (box->bottom - margin); |
| 110 |
int x = x0; |
| 111 |
int y = y0; |
| 112 |
const char *s; |
| 113 |
|
| 114 |
while ((s = *c++) != NULL) { |
| 115 |
int t; |
| 116 |
while ((t = *s++) != 0) { |
| 117 |
if (x == x0) { |
| 118 |
if (t == ' ') |
| 119 |
continue; |
| 120 |
cup(y, x++); |
| 121 |
putchar(' '); |
| 122 |
} |
| 123 |
putchar(t); |
| 124 |
x++; |
| 125 |
if ((t == ' ') && (next_word(s) > (x1 - x - 2))) { |
| 126 |
while (x < x1) { |
| 127 |
putchar(' '); |
| 128 |
x++; |
| 129 |
} |
| 130 |
} |
| 131 |
if (x >= x1) { |
| 132 |
putchar(' '); |
| 133 |
x = x0; |
| 134 |
y++; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
while (y <= y1) { |
| 139 |
if (x == x0) { |
| 140 |
cup(y, x); |
| 141 |
} |
| 142 |
putchar(' '); |
| 143 |
if (++x >= x1) { |
| 144 |
putchar(' '); |
| 145 |
x = x0; |
| 146 |
y++; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void |
| 152 |
ruler(int row, int width) |
| 153 |
{ |
| 154 |
int col; |
| 155 |
vt_move(row, 1); |
| 156 |
for (col = 1; col <= width; ++col) { |
| 157 |
int ch = (((col % 10) == 0) |
| 158 |
? ('0' + (col / 10) % 10) |
| 159 |
: (((col % 5) == 0) |
| 160 |
? '+' |
| 161 |
: '-')); |
| 162 |
putchar(ch); |
| 163 |
} |
| 164 |
putchar('\n'); |
| 165 |
} |