Added Paint for ReactOS source folder
[reactos.git] / reactos / base / applications / paint / drawing.c
diff --git a/reactos/base/applications/paint/drawing.c b/reactos/base/applications/paint/drawing.c
new file mode 100755 (executable)
index 0000000..2c2ac36
--- /dev/null
@@ -0,0 +1,202 @@
+/*\r
+ * PROJECT:     PAINT for ReactOS\r
+ * LICENSE:     LGPL\r
+ * FILE:        drawing.c\r
+ * PURPOSE:     The drawing functions used by the tools\r
+ * PROGRAMMERS: Benedikt Freisen\r
+ */\r
+\r
+/* INCLUDES *********************************************************/\r
+\r
+#include <windows.h>\r
+\r
+/* FUNCTIONS ********************************************************/\r
+\r
+void Line(HDC hdc, short x1, short y1, short x2, short y2, int color, int thickness)\r
+{\r
+    HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, color));\r
+    MoveToEx(hdc, x1, y1, NULL);\r
+    LineTo(hdc, x2, y2);\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+}\r
+\r
+void Rect(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, BOOL filled)\r
+{\r
+    HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));\r
+    LOGBRUSH logbrush;\r
+    if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;\r
+    logbrush.lbColor = bg;\r
+    logbrush.lbHatch = 0;\r
+    HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));\r
+    Rectangle(hdc, x1, y1, x2, y2);\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+}\r
+\r
+void Ellp(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, BOOL filled)\r
+{\r
+    HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));\r
+    LOGBRUSH logbrush;\r
+    if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;\r
+    logbrush.lbColor = bg;\r
+    logbrush.lbHatch = 0;\r
+    HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));\r
+    Ellipse(hdc, x1, y1, x2, y2);\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+}\r
+\r
+void RRect(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, BOOL filled)\r
+{\r
+    HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));\r
+    LOGBRUSH logbrush;\r
+    if (filled) logbrush.lbStyle = BS_SOLID; else logbrush.lbStyle = BS_HOLLOW;\r
+    logbrush.lbColor = bg;\r
+    logbrush.lbHatch = 0;\r
+    HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));\r
+    RoundRect(hdc, x1, y1, x2, y2, 16, 16);\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+}\r
+\r
+void Fill(HDC hdc, int x, int y, int color)\r
+{\r
+    HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));\r
+    ExtFloodFill(hdc, x, y, GetPixel(hdc, x, y), FLOODFILLSURFACE);\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+}\r
+\r
+void Erase(HDC hdc, short x1, short y1, short x2, short y2, int color, int radius)\r
+{\r
+    HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));\r
+    HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));\r
+    short a;\r
+    for (a=0; a<=100; a++)\r
+        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);\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+}\r
+\r
+void Airbrush(HDC hdc, short x, short y, int color, int r)\r
+{\r
+    short a;\r
+    short b;\r
+    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);\r
+}\r
+\r
+void Brush(HDC hdc, short x1, short y1, short x2, short y2, int color, int style)\r
+{\r
+    HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));\r
+    HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));\r
+    short a;\r
+    switch (style)\r
+    {\r
+        case 0:\r
+            for (a=0; a<=100; a++)\r
+                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);\r
+            break;\r
+        case 1:\r
+            for (a=0; a<=100; a++)\r
+                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);\r
+            break;\r
+        case 2:\r
+            MoveToEx(hdc, x1, y1, NULL);\r
+            LineTo(hdc, x2, y2);\r
+            SetPixel(hdc, x2, y2, color);\r
+            break;\r
+        case 3:\r
+            for (a=0; a<=100; a++)\r
+                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);\r
+            break;\r
+        case 4:\r
+            for (a=0; a<=100; a++)\r
+                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);\r
+            break;\r
+        case 5:\r
+            for (a=0; a<=100; a++)\r
+                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);\r
+            break;\r
+        case 6:\r
+            for (a=0; a<=100; a++)\r
+            {\r
+                MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100+5, NULL);\r
+                LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100-3);\r
+            }\r
+            break;\r
+        case 7:\r
+            for (a=0; a<=100; a++)\r
+            {\r
+                MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100+3, NULL);\r
+                LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100-2);\r
+            }\r
+            break;\r
+        case 8:\r
+            for (a=0; a<=100; a++)\r
+            {\r
+                MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100+1, NULL);\r
+                LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100-1);\r
+            }\r
+            break;\r
+        case 9:\r
+            for (a=0; a<=100; a++)\r
+            {\r
+                MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, NULL);\r
+                LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100+5);\r
+            }\r
+            break;\r
+        case 10:\r
+            for (a=0; a<=100; a++)\r
+            {\r
+                MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100-2, NULL);\r
+                LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);\r
+            }\r
+            break;\r
+        case 11:\r
+            for (a=0; a<=100; a++)\r
+            {\r
+                MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, NULL);\r
+                LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100+1);\r
+            }\r
+            break;\r
+    }\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+}\r
+\r
+void RectSel(HDC hdc, short x1, short y1, short x2, short y2)\r
+{\r
+    HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));\r
+    LOGBRUSH logbrush;\r
+    logbrush.lbStyle = BS_HOLLOW;\r
+    logbrush.lbColor = 0;\r
+    logbrush.lbHatch = 0;\r
+    HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));\r
+    Rectangle(hdc, x1, y1, x2, y2);\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+}\r
+\r
+void SelectionFrame(HDC hdc, int x1, int y1, int x2, int y2)\r
+{\r
+    HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));\r
+    LOGBRUSH logbrush;\r
+    logbrush.lbStyle = BS_HOLLOW;\r
+    logbrush.lbColor = 0;\r
+    logbrush.lbHatch = 0;\r
+    HBRUSH oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));\r
+    Rectangle(hdc, x1, y1, x2, y2);\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+    oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x00000000));\r
+    oldBrush = SelectObject(hdc, CreateSolidBrush(0x00000000));\r
+    Rectangle(hdc, x1-1, y1-1, x1+2, y1+2);\r
+    Rectangle(hdc, x2-2, y1-1, x2+2, y1+2);\r
+    Rectangle(hdc, x1-1, y2-2, x1+2, y2+1);\r
+    Rectangle(hdc, x2-2, y2-2, x2+2, y2+1);\r
+    Rectangle(hdc, (x1+x2)/2-1, y1-1, (x1+x2)/2+2, y1+2);\r
+    Rectangle(hdc, (x1+x2)/2-1, y2-2, (x1+x2)/2+2, y2+1);\r
+    Rectangle(hdc, x1-1, (y1+y2)/2-1, x1+2, (y1+y2)/2+2);\r
+    Rectangle(hdc, x2-2, (y1+y2)/2-1, x2+1, (y1+y2)/2+2);\r
+    DeleteObject(SelectObject(hdc, oldBrush));\r
+    DeleteObject(SelectObject(hdc, oldPen));\r
+}\r