SVN maintenance
[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 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
26 LOGBRUSH logbrush;
27 if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;
28 logbrush.lbColor = bg;
29 logbrush.lbHatch = 0;
30 HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
31 Rectangle(hdc, x1, y1, x2, y2);
32 DeleteObject(SelectObject(hdc, oldBrush));
33 DeleteObject(SelectObject(hdc, oldPen));
34 }
35
36 void Ellp(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, BOOL filled)
37 {
38 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
39 LOGBRUSH logbrush;
40 if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;
41 logbrush.lbColor = bg;
42 logbrush.lbHatch = 0;
43 HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
44 Ellipse(hdc, x1, y1, x2, y2);
45 DeleteObject(SelectObject(hdc, oldBrush));
46 DeleteObject(SelectObject(hdc, oldPen));
47 }
48
49 void RRect(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, BOOL filled)
50 {
51 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
52 LOGBRUSH logbrush;
53 if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;
54 logbrush.lbColor = bg;
55 logbrush.lbHatch = 0;
56 HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
57 RoundRect(hdc, x1, y1, x2, y2, 16, 16);
58 DeleteObject(SelectObject(hdc, oldBrush));
59 DeleteObject(SelectObject(hdc, oldPen));
60 }
61
62 void Fill(HDC hdc, int x, int y, int color)
63 {
64 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
65 ExtFloodFill(hdc, x, y, GetPixel(hdc, x, y), FLOODFILLSURFACE);
66 DeleteObject(SelectObject(hdc, oldBrush));
67 }
68
69 void Erase(HDC hdc, short x1, short y1, short x2, short y2, int color, int radius)
70 {
71 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
72 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
73 short a;
74 for (a=0; a<=100; a++)
75 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);
76 DeleteObject(SelectObject(hdc, oldBrush));
77 DeleteObject(SelectObject(hdc, oldPen));
78 }
79
80 void Airbrush(HDC hdc, short x, short y, int color, int r)
81 {
82 short a;
83 short b;
84 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);
85 }
86
87 void Brush(HDC hdc, short x1, short y1, short x2, short y2, int color, int style)
88 {
89 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
90 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
91 short a;
92 switch (style)
93 {
94 case 0:
95 for (a=0; a<=100; a++)
96 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);
97 break;
98 case 1:
99 for (a=0; a<=100; a++)
100 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);
101 break;
102 case 2:
103 MoveToEx(hdc, x1, y1, NULL);
104 LineTo(hdc, x2, y2);
105 SetPixel(hdc, x2, y2, color);
106 break;
107 case 3:
108 for (a=0; a<=100; a++)
109 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);
110 break;
111 case 4:
112 for (a=0; a<=100; a++)
113 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);
114 break;
115 case 5:
116 for (a=0; a<=100; a++)
117 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);
118 break;
119 case 6:
120 for (a=0; a<=100; a++)
121 {
122 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100+5, NULL);
123 LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100-3);
124 }
125 break;
126 case 7:
127 for (a=0; a<=100; a++)
128 {
129 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100+3, NULL);
130 LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100-2);
131 }
132 break;
133 case 8:
134 for (a=0; a<=100; a++)
135 {
136 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100+1, NULL);
137 LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100-1);
138 }
139 break;
140 case 9:
141 for (a=0; a<=100; a++)
142 {
143 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, NULL);
144 LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100+5);
145 }
146 break;
147 case 10:
148 for (a=0; a<=100; a++)
149 {
150 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100-2, NULL);
151 LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);
152 }
153 break;
154 case 11:
155 for (a=0; a<=100; a++)
156 {
157 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, NULL);
158 LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100+1);
159 }
160 break;
161 }
162 DeleteObject(SelectObject(hdc, oldBrush));
163 DeleteObject(SelectObject(hdc, oldPen));
164 }
165
166 void RectSel(HDC hdc, short x1, short y1, short x2, short y2)
167 {
168 HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));
169 LOGBRUSH logbrush;
170 logbrush.lbStyle = BS_HOLLOW;
171 logbrush.lbColor = 0;
172 logbrush.lbHatch = 0;
173 HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
174 Rectangle(hdc, x1, y1, x2, y2);
175 DeleteObject(SelectObject(hdc, oldBrush));
176 DeleteObject(SelectObject(hdc, oldPen));
177 }
178
179 void SelectionFrame(HDC hdc, int x1, int y1, int x2, int y2)
180 {
181 HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));
182 LOGBRUSH logbrush;
183 logbrush.lbStyle = BS_HOLLOW;
184 logbrush.lbColor = 0;
185 logbrush.lbHatch = 0;
186 HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
187 Rectangle(hdc, x1, y1, x2, y2);
188 DeleteObject(SelectObject(hdc, oldBrush));
189 DeleteObject(SelectObject(hdc, oldPen));
190 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x00000000));
191 oldBrush = SelectObject(hdc, CreateSolidBrush(0x00000000));
192 Rectangle(hdc, x1-1, y1-1, x1+2, y1+2);
193 Rectangle(hdc, x2-2, y1-1, x2+2, y1+2);
194 Rectangle(hdc, x1-1, y2-2, x1+2, y2+1);
195 Rectangle(hdc, x2-2, y2-2, x2+2, y2+1);
196 Rectangle(hdc, (x1+x2)/2-1, y1-1, (x1+x2)/2+2, y1+2);
197 Rectangle(hdc, (x1+x2)/2-1, y2-2, (x1+x2)/2+2, y2+1);
198 Rectangle(hdc, x1-1, (y1+y2)/2-1, x1+2, (y1+y2)/2+2);
199 Rectangle(hdc, x2-2, (y1+y2)/2-1, x2+1, (y1+y2)/2+2);
200 DeleteObject(SelectObject(hdc, oldBrush));
201 DeleteObject(SelectObject(hdc, oldPen));
202 }