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 527 2006-03-06 08:32:40Z 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 | #if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME) |
---|
23 | # include <inttypes.h> |
---|
24 | #else |
---|
25 | typedef unsigned char uint8_t; |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <stdlib.h> |
---|
29 | |
---|
30 | #include "cucul.h" |
---|
31 | #include "cucul_internals.h" |
---|
32 | |
---|
33 | /** |
---|
34 | * \brief Draw a box on the screen using the given character. |
---|
35 | * |
---|
36 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
37 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
38 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
39 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
40 | * \param c Character to draw the box outline with. |
---|
41 | * \return void |
---|
42 | */ |
---|
43 | void cucul_draw_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c) |
---|
44 | { |
---|
45 | cucul_draw_line(qq, x1, y1, x1, y2, c); |
---|
46 | cucul_draw_line(qq, x1, y2, x2, y2, c); |
---|
47 | cucul_draw_line(qq, x2, y2, x2, y1, c); |
---|
48 | cucul_draw_line(qq, x2, y1, x1, y1, c); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * \brief Draw a thin box on the screen. |
---|
53 | * |
---|
54 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
55 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
56 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
57 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
58 | * \return void |
---|
59 | */ |
---|
60 | void cucul_draw_thin_box(cucul_t *qq, int x1, int y1, int x2, int y2) |
---|
61 | { |
---|
62 | int x, y, xmax, ymax; |
---|
63 | |
---|
64 | if(x1 > x2) |
---|
65 | { |
---|
66 | int tmp = x1; |
---|
67 | x1 = x2; x2 = tmp; |
---|
68 | } |
---|
69 | |
---|
70 | if(y1 > y2) |
---|
71 | { |
---|
72 | int tmp = y1; |
---|
73 | y1 = y2; y2 = tmp; |
---|
74 | } |
---|
75 | |
---|
76 | xmax = qq->width - 1; |
---|
77 | ymax = qq->height - 1; |
---|
78 | |
---|
79 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
80 | return; |
---|
81 | |
---|
82 | /* Draw edges */ |
---|
83 | if(y1 >= 0) |
---|
84 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
85 | cucul_putchar(qq, x, y1, '-'); |
---|
86 | |
---|
87 | if(y2 <= ymax) |
---|
88 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
89 | cucul_putchar(qq, x, y2, '-'); |
---|
90 | |
---|
91 | if(x1 >= 0) |
---|
92 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
93 | cucul_putchar(qq, x1, y, '|'); |
---|
94 | |
---|
95 | if(x2 <= xmax) |
---|
96 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
97 | cucul_putchar(qq, x2, y, '|'); |
---|
98 | |
---|
99 | /* Draw corners */ |
---|
100 | if(x1 >= 0 && y1 >= 0) |
---|
101 | cucul_putchar(qq, x1, y1, ','); |
---|
102 | |
---|
103 | if(x1 >= 0 && y2 <= ymax) |
---|
104 | cucul_putchar(qq, x1, y2, '`'); |
---|
105 | |
---|
106 | if(x2 <= xmax && y1 >= 0) |
---|
107 | cucul_putchar(qq, x2, y1, '.'); |
---|
108 | |
---|
109 | if(x2 <= xmax && y2 <= ymax) |
---|
110 | cucul_putchar(qq, x2, y2, '\''); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * \brief Fill a box on the screen using the given character. |
---|
115 | * |
---|
116 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
117 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
118 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
119 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
120 | * \param c Character to fill the box with. |
---|
121 | * \return void |
---|
122 | */ |
---|
123 | void cucul_fill_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c) |
---|
124 | { |
---|
125 | int x, y, xmax, ymax; |
---|
126 | |
---|
127 | if(x1 > x2) |
---|
128 | { |
---|
129 | int tmp = x1; |
---|
130 | x1 = x2; x2 = tmp; |
---|
131 | } |
---|
132 | |
---|
133 | if(y1 > y2) |
---|
134 | { |
---|
135 | int tmp = y1; |
---|
136 | y1 = y2; y2 = tmp; |
---|
137 | } |
---|
138 | |
---|
139 | xmax = qq->width - 1; |
---|
140 | ymax = qq->height - 1; |
---|
141 | |
---|
142 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
143 | return; |
---|
144 | |
---|
145 | if(x1 < 0) x1 = 0; |
---|
146 | if(y1 < 0) y1 = 0; |
---|
147 | if(x2 > xmax) x2 = xmax; |
---|
148 | if(y2 > ymax) y2 = ymax; |
---|
149 | |
---|
150 | for(y = y1; y <= y2; y++) |
---|
151 | for(x = x1; x <= x2; x++) |
---|
152 | cucul_putchar(qq, x, y, c); |
---|
153 | } |
---|
154 | |
---|