attempt to convert most of this mess into something which at least look like C code
[reactos.git] / reactos / base / applications / paint / drawing.c
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: drawing.c
5 * PURPOSE: The drawing functions used by the tools
6 * PROGRAMMERS: Benedikt Freisen
7 */
8
9 /* INCLUDES *********************************************************/
10
11 #include <windows.h>
12
13 /* FUNCTIONS ********************************************************/
14
15 void Line(HDC hdc, short x1, short y1, short x2, short y2, int color, int thickness)
16 {
17 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, color));
18 MoveToEx(hdc, x1, y1, NULL);
19 LineTo(hdc, x2, y2);
20 DeleteObject(SelectObject(hdc, oldPen));
21 }
22
23 void Rect(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, BOOL filled)
24 {
25 HBRUSH oldBrush;
26 LOGBRUSH logbrush;
27 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
28 if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;
29 logbrush.lbColor = bg;
30 logbrush.lbHatch = 0;
31 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
32 Rectangle(hdc, x1, y1, x2, y2);
33 DeleteObject(SelectObject(hdc, oldBrush));
34 DeleteObject(SelectObject(hdc, oldPen));
35 }
36
37 void Ellp(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, BOOL filled)
38 {
39 HBRUSH oldBrush;
40 LOGBRUSH logbrush;
41 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
42 if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;
43 logbrush.lbColor = bg;
44 logbrush.lbHatch = 0;
45 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
46 Ellipse(hdc, x1, y1, x2, y2);
47 DeleteObject(SelectObject(hdc, oldBrush));
48 DeleteObject(SelectObject(hdc, oldPen));
49 }
50
51 void RRect(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, BOOL filled)
52 {
53 LOGBRUSH logbrush;
54 HBRUSH oldBrush;
55 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
56 if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;
57 logbrush.lbColor = bg;
58 logbrush.lbHatch = 0;
59 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
60 RoundRect(hdc, x1, y1, x2, y2, 16, 16);
61 DeleteObject(SelectObject(hdc, oldBrush));
62 DeleteObject(SelectObject(hdc, oldPen));
63 }
64
65 void Fill(HDC hdc, int x, int y, int color)
66 {
67 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
68 ExtFloodFill(hdc, x, y, GetPixel(hdc, x, y), FLOODFILLSURFACE);
69 DeleteObject(SelectObject(hdc, oldBrush));
70 }
71
72 void Erase(HDC hdc, short x1, short y1, short x2, short y2, int color, int radius)
73 {
74 short a;
75 HPEN oldPen;
76 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
77 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
78 for (a=0; a<=100; a++)
79 Rectangle(hdc, (x1*(100-a)+x2*a)/100-radius+1, (y1*(100-a)+y2*a)/100-radius+1, (x1*(100-a)+x2*a)/100+radius+1, (y1*(100-a)+y2*a)/100+radius+1);
80 DeleteObject(SelectObject(hdc, oldBrush));
81 DeleteObject(SelectObject(hdc, oldPen));
82 }
83
84 void Airbrush(HDC hdc, short x, short y, int color, int r)
85 {
86 short a;
87 short b;
88 for (b=-r; b<=r; b++) for (a=-r; a<=r; a++) if ((a*a+b*b<=r*r)&&(rand()%4==0)) SetPixel(hdc, x+a, y+b, color);
89 }
90
91 void Brush(HDC hdc, short x1, short y1, short x2, short y2, int color, int style)
92 {
93 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
94 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
95 short a;
96 switch (style)
97 {
98 case 0:
99 for (a=0; a<=100; a++)
100 Ellipse(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, (x1*(100-a)+x2*a)/100+4, (y1*(100-a)+y2*a)/100+4);
101 break;
102 case 1:
103 for (a=0; a<=100; a++)
104 Ellipse(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);
105 break;
106 case 2:
107 MoveToEx(hdc, x1, y1, NULL);
108 LineTo(hdc, x2, y2);
109 SetPixel(hdc, x2, y2, color);
110 break;
111 case 3:
112 for (a=0; a<=100; a++)
113 Rectangle(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100+5);
114 break;
115 case 4:
116 for (a=0; a<=100; a++)
117 Rectangle(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100-2, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);
118 break;
119 case 5:
120 for (a=0; a<=100; a++)
121 Rectangle(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100+1);
122 break;
123 case 6:
124 for (a=0; a<=100; a++)
125 {
126 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100+5, NULL);
127 LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100-3);
128 }
129 break;
130 case 7:
131 for (a=0; a<=100; a++)
132 {
133 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100+3, NULL);
134 LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100-2);
135 }
136 break;
137 case 8:
138 for (a=0; a<=100; a++)
139 {
140 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100+1, NULL);
141 LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100-1);
142 }
143 break;
144 case 9:
145 for (a=0; a<=100; a++)
146 {
147 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, NULL);
148 LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100+5);
149 }
150 break;
151 case 10:
152 for (a=0; a<=100; a++)
153 {
154 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100-2, NULL);
155 LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);
156 }
157 break;
158 case 11:
159 for (a=0; a<=100; a++)
160 {
161 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, NULL);
162 LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100+1);
163 }
164 break;
165 }
166 DeleteObject(SelectObject(hdc, oldBrush));
167 DeleteObject(SelectObject(hdc, oldPen));
168 }
169
170 void RectSel(HDC hdc, short x1, short y1, short x2, short y2)
171 {
172 HBRUSH oldBrush;
173 LOGBRUSH logbrush;
174 HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));
175 logbrush.lbStyle = BS_HOLLOW;
176 logbrush.lbColor = 0;
177 logbrush.lbHatch = 0;
178 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
179 Rectangle(hdc, x1, y1, x2, y2);
180 DeleteObject(SelectObject(hdc, oldBrush));
181 DeleteObject(SelectObject(hdc, oldPen));
182 }
183
184 void SelectionFrame(HDC hdc, int x1, int y1, int x2, int y2)
185 {
186 HBRUSH oldBrush;
187 LOGBRUSH logbrush;
188 HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));
189 logbrush.lbStyle = BS_HOLLOW;
190 logbrush.lbColor = 0;
191 logbrush.lbHatch = 0;
192 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
193 Rectangle(hdc, x1, y1, x2, y2);
194 DeleteObject(SelectObject(hdc, oldBrush));
195 DeleteObject(SelectObject(hdc, oldPen));
196 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x00000000));
197 oldBrush = SelectObject(hdc, CreateSolidBrush(0x00000000));
198 Rectangle(hdc, x1-1, y1-1, x1+2, y1+2);
199 Rectangle(hdc, x2-2, y1-1, x2+2, y1+2);
200 Rectangle(hdc, x1-1, y2-2, x1+2, y2+1);
201 Rectangle(hdc, x2-2, y2-2, x2+2, y2+1);
202 Rectangle(hdc, (x1+x2)/2-1, y1-1, (x1+x2)/2+2, y1+2);
203 Rectangle(hdc, (x1+x2)/2-1, y2-2, (x1+x2)/2+2, y2+1);
204 Rectangle(hdc, x1-1, (y1+y2)/2-1, x1+2, (y1+y2)/2+2);
205 Rectangle(hdc, x2-2, (y1+y2)/2-1, x2+1, (y1+y2)/2+2);
206 DeleteObject(SelectObject(hdc, oldBrush));
207 DeleteObject(SelectObject(hdc, oldPen));
208 }