1 | /* |
---|
2 | * libcucul Unicode canvas library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This library is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
8 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
10 | */ |
---|
11 | |
---|
12 | /** \file box.c |
---|
13 | * \version \$Id: box.c 540 2006-03-07 09:17:35Z sam $ |
---|
14 | * \author Sam Hocevar <sam@zoy.org> |
---|
15 | * \brief Simple box drawing |
---|
16 | * |
---|
17 | * This file contains box drawing functions, both filled and outline. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #include <stdlib.h> |
---|
23 | |
---|
24 | #include "cucul.h" |
---|
25 | #include "cucul_internals.h" |
---|
26 | |
---|
27 | /** |
---|
28 | * \brief Draw a box on the screen using the given character. |
---|
29 | * |
---|
30 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
31 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
32 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
33 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
34 | * \param c Character to draw the box outline with. |
---|
35 | * \return void |
---|
36 | */ |
---|
37 | void cucul_draw_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c) |
---|
38 | { |
---|
39 | cucul_draw_line(qq, x1, y1, x1, y2, c); |
---|
40 | cucul_draw_line(qq, x1, y2, x2, y2, c); |
---|
41 | cucul_draw_line(qq, x2, y2, x2, y1, c); |
---|
42 | cucul_draw_line(qq, x2, y1, x1, y1, c); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * \brief Draw a thin box on the screen. |
---|
47 | * |
---|
48 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
49 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
50 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
51 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
52 | * \return void |
---|
53 | */ |
---|
54 | void cucul_draw_thin_box(cucul_t *qq, int x1, int y1, int x2, int y2) |
---|
55 | { |
---|
56 | int x, y, xmax, ymax; |
---|
57 | |
---|
58 | if(x1 > x2) |
---|
59 | { |
---|
60 | int tmp = x1; |
---|
61 | x1 = x2; x2 = tmp; |
---|
62 | } |
---|
63 | |
---|
64 | if(y1 > y2) |
---|
65 | { |
---|
66 | int tmp = y1; |
---|
67 | y1 = y2; y2 = tmp; |
---|
68 | } |
---|
69 | |
---|
70 | xmax = qq->width - 1; |
---|
71 | ymax = qq->height - 1; |
---|
72 | |
---|
73 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
74 | return; |
---|
75 | |
---|
76 | /* Draw edges */ |
---|
77 | if(y1 >= 0) |
---|
78 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
79 | cucul_putchar(qq, x, y1, '-'); |
---|
80 | |
---|
81 | if(y2 <= ymax) |
---|
82 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
83 | cucul_putchar(qq, x, y2, '-'); |
---|
84 | |
---|
85 | if(x1 >= 0) |
---|
86 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
87 | cucul_putchar(qq, x1, y, '|'); |
---|
88 | |
---|
89 | if(x2 <= xmax) |
---|
90 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
91 | cucul_putchar(qq, x2, y, '|'); |
---|
92 | |
---|
93 | /* Draw corners */ |
---|
94 | if(x1 >= 0 && y1 >= 0) |
---|
95 | cucul_putchar(qq, x1, y1, ','); |
---|
96 | |
---|
97 | if(x1 >= 0 && y2 <= ymax) |
---|
98 | cucul_putchar(qq, x1, y2, '`'); |
---|
99 | |
---|
100 | if(x2 <= xmax && y1 >= 0) |
---|
101 | cucul_putchar(qq, x2, y1, '.'); |
---|
102 | |
---|
103 | if(x2 <= xmax && y2 <= ymax) |
---|
104 | cucul_putchar(qq, x2, y2, '\''); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * \brief Fill a box on the screen using the given character. |
---|
109 | * |
---|
110 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
111 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
112 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
113 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
114 | * \param c Character to fill the box with. |
---|
115 | * \return void |
---|
116 | */ |
---|
117 | void cucul_fill_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c) |
---|
118 | { |
---|
119 | int x, y, xmax, ymax; |
---|
120 | |
---|
121 | if(x1 > x2) |
---|
122 | { |
---|
123 | int tmp = x1; |
---|
124 | x1 = x2; x2 = tmp; |
---|
125 | } |
---|
126 | |
---|
127 | if(y1 > y2) |
---|
128 | { |
---|
129 | int tmp = y1; |
---|
130 | y1 = y2; y2 = tmp; |
---|
131 | } |
---|
132 | |
---|
133 | xmax = qq->width - 1; |
---|
134 | ymax = qq->height - 1; |
---|
135 | |
---|
136 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
137 | return; |
---|
138 | |
---|
139 | if(x1 < 0) x1 = 0; |
---|
140 | if(y1 < 0) y1 = 0; |
---|
141 | if(x2 > xmax) x2 = xmax; |
---|
142 | if(y2 > ymax) y2 = ymax; |
---|
143 | |
---|
144 | for(y = y1; y <= y2; y++) |
---|
145 | for(x = x1; x <= x2; x++) |
---|
146 | cucul_putchar(qq, x, y, c); |
---|
147 | } |
---|
148 | |
---|