[PAINT]
[reactos.git] / reactos / base / applications / paint / mouse.c
index 87cee6c..d46bbe3 100644 (file)
@@ -25,8 +25,38 @@ placeSelWin()
     //SendMessage(hSelection, WM_PAINT, 0, 0);
 }
 
+void
+regularize(short x0, short y0, short *x1, short *y1)
+{
+    if (abs(*x1 - x0) >= abs(*y1 - y0))
+        *y1 = y0 + (*y1 > y0 ? abs(*x1 - x0) : -abs(*x1 - x0));
+    else
+        *x1 = x0 + (*x1 > x0 ? abs(*y1 - y0) : -abs(*y1 - y0));
+}
+
+void
+roundTo8Directions(short x0, short y0, short *x1, short *y1)
+{
+    if (abs(*x1 - x0) >= abs(*y1 - y0))
+    {
+        if (abs(*y1 - y0) * 5 < abs(*x1 - x0) * 2)
+            *y1 = y0;
+        else
+            *y1 = y0 + (*y1 > y0 ? abs(*x1 - x0) : -abs(*x1 - x0));
+    }
+    else
+    {
+        if (abs(*x1 - x0) * 5 < abs(*y1 - y0) * 2)
+            *x1 = x0;
+        else
+            *x1 = x0 + (*x1 > x0 ? abs(*y1 - y0) : -abs(*y1 - y0));
+    }
+}
+
 POINT pointStack[256];
 short pointSP;
+POINT *ptStack = NULL;
+int ptSP = 0;
 
 void
 startPaintingL(HDC hdc, short x, short y, int fg, int bg)
@@ -37,38 +67,48 @@ startPaintingL(HDC hdc, short x, short y, int fg, int bg)
     lastY = y;
     switch (activeTool)
     {
-        case 1:
-        case 10:
-        case 11:
-        case 13:
-        case 15:
-        case 16:
+        case TOOL_FREESEL:
+            ShowWindow(hSelection, SW_HIDE);
+            if (ptStack != NULL)
+                HeapFree(GetProcessHeap(), 0, ptStack);
+            ptStack = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sizeof(POINT) * 1024);
+            ptSP = 0;
+            ptStack[0].x = x;
+            ptStack[0].y = y;
+            break;
+        case TOOL_TEXT:
+        case TOOL_LINE:
+        case TOOL_RECT:
+        case TOOL_ELLIPSE:
+        case TOOL_RRECT:
             newReversible();
-        case 2:
+            break;
+        case TOOL_RECTSEL:
             newReversible();
             ShowWindow(hSelection, SW_HIDE);
+            rectSel_src[2] = rectSel_src[3] = 0;
             break;
-        case 3:
+        case TOOL_RUBBER:
             newReversible();
             Erase(hdc, x, y, x, y, bg, rubberRadius);
             break;
-        case 4:
+        case TOOL_FILL:
             newReversible();
             Fill(hdc, x, y, fg);
             break;
-        case 7:
+        case TOOL_PEN:
             newReversible();
             SetPixel(hdc, x, y, fg);
             break;
-        case 8:
+        case TOOL_BRUSH:
             newReversible();
             Brush(hdc, x, y, x, y, fg, brushStyle);
             break;
-        case 9:
+        case TOOL_AIRBRUSH:
             newReversible();
             Airbrush(hdc, x, y, fg, airBrushWidth);
             break;
-        case 12:
+        case TOOL_BEZIER:
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
             if (pointSP == 0)
@@ -77,7 +117,7 @@ startPaintingL(HDC hdc, short x, short y, int fg, int bg)
                 pointSP++;
             }
             break;
-        case 14:
+        case TOOL_SHAPE:
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
             if (pointSP + 1 >= 2)
@@ -96,7 +136,18 @@ whilePaintingL(HDC hdc, short x, short y, int fg, int bg)
 {
     switch (activeTool)
     {
-        case 2:
+        case TOOL_FREESEL:
+            if (ptSP == 0)
+                newReversible();
+            ptSP++;
+            if (ptSP % 1024 == 0)
+                ptStack = HeapReAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, ptStack, sizeof(POINT) * (ptSP + 1024));
+            ptStack[ptSP].x = max(0, min(x, imgXRes));
+            ptStack[ptSP].y = max(0, min(y, imgYRes));
+            resetToU1();
+            Poly(hdc, ptStack, ptSP + 1, 0, 0, 2, 0, FALSE);
+            break;
+        case TOOL_RECTSEL:
         {
             short tempX;
             short tempY;
@@ -110,23 +161,25 @@ whilePaintingL(HDC hdc, short x, short y, int fg, int bg)
             RectSel(hdc, startX, startY, tempX, tempY);
             break;
         }
-        case 3:
+        case TOOL_RUBBER:
             Erase(hdc, lastX, lastY, x, y, bg, rubberRadius);
             break;
-        case 7:
+        case TOOL_PEN:
             Line(hdc, lastX, lastY, x, y, fg, 1);
             break;
-        case 8:
+        case TOOL_BRUSH:
             Brush(hdc, lastX, lastY, x, y, fg, brushStyle);
             break;
-        case 9:
+        case TOOL_AIRBRUSH:
             Airbrush(hdc, x, y, fg, airBrushWidth);
             break;
-        case 11:
+        case TOOL_LINE:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                roundTo8Directions(startX, startY, &x, &y);
             Line(hdc, startX, startY, x, y, fg, lineWidth);
             break;
-        case 12:
+        case TOOL_BEZIER:
             resetToU1();
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
@@ -144,23 +197,32 @@ whilePaintingL(HDC hdc, short x, short y, int fg, int bg)
                     break;
             }
             break;
-        case 13:
+        case TOOL_RECT:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             Rect(hdc, startX, startY, x, y, fg, bg, lineWidth, shapeStyle);
             break;
-        case 14:
+        case TOOL_SHAPE:
             resetToU1();
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
+            if ((pointSP > 0) && (GetAsyncKeyState(VK_SHIFT) < 0))
+                roundTo8Directions(pointStack[pointSP - 1].x, pointStack[pointSP - 1].y,
+                                   (short *)&pointStack[pointSP].x, (short *)&pointStack[pointSP].y);
             if (pointSP + 1 >= 2)
                 Poly(hdc, pointStack, pointSP + 1, fg, bg, lineWidth, shapeStyle, FALSE);
             break;
-        case 15:
+        case TOOL_ELLIPSE:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             Ellp(hdc, startX, startY, x, y, fg, bg, lineWidth, shapeStyle);
             break;
-        case 16:
+        case TOOL_RRECT:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             RRect(hdc, startX, startY, x, y, fg, bg, lineWidth, shapeStyle);
             break;
     }
@@ -174,43 +236,113 @@ endPaintingL(HDC hdc, short x, short y, int fg, int bg)
 {
     switch (activeTool)
     {
-        case 2:
+        case TOOL_FREESEL:
+        {
+            POINT *ptStackCopy;
+            int i;
+            rectSel_src[0] = rectSel_src[1] = 0x7fffffff;
+            rectSel_src[2] = rectSel_src[3] = 0;
+            for (i = 0; i <= ptSP; i++)
+            {
+                if (ptStack[i].x < rectSel_src[0])
+                    rectSel_src[0] = ptStack[i].x;
+                if (ptStack[i].y < rectSel_src[1])
+                    rectSel_src[1] = ptStack[i].y;
+                if (ptStack[i].x > rectSel_src[2])
+                    rectSel_src[2] = ptStack[i].x;
+                if (ptStack[i].y > rectSel_src[3])
+                    rectSel_src[3] = ptStack[i].y;
+            }
+            rectSel_src[2] += 1 - rectSel_src[0];
+            rectSel_src[3] += 1 - rectSel_src[1];
+            rectSel_dest[0] = rectSel_src[0];
+            rectSel_dest[1] = rectSel_src[1];
+            rectSel_dest[2] = rectSel_src[2];
+            rectSel_dest[3] = rectSel_src[3];
+            if (ptSP != 0)
+            {
+                DeleteObject(hSelMask);
+                hSelMask = CreateBitmap(rectSel_src[2], rectSel_src[3], 1, 1, NULL);
+                DeleteObject(SelectObject(hSelDC, hSelMask));
+                ptStackCopy = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sizeof(POINT) * (ptSP + 1));
+                for (i = 0; i <= ptSP; i++)
+                {
+                    ptStackCopy[i].x = ptStack[i].x - rectSel_src[0];
+                    ptStackCopy[i].y = ptStack[i].y - rectSel_src[1];
+                }
+                Poly(hSelDC, ptStackCopy, ptSP + 1, 0x00ffffff, 0x00ffffff, 1, 2, TRUE);
+                HeapFree(GetProcessHeap(), 0, ptStackCopy);
+                SelectObject(hSelDC, hSelBm = CreateDIBWithProperties(rectSel_src[2], rectSel_src[3]));
+                resetToU1();
+                MaskBlt(hSelDC, 0, 0, rectSel_src[2], rectSel_src[3], hDrawingDC, rectSel_src[0],
+                        rectSel_src[1], hSelMask, 0, 0, MAKEROP4(SRCCOPY, WHITENESS));
+                Poly(hdc, ptStack, ptSP + 1, bg, bg, 1, 2, TRUE);
+                newReversible();
+
+                MaskBlt(hDrawingDC, rectSel_src[0], rectSel_src[1], rectSel_src[2], rectSel_src[3], hSelDC, 0,
+                        0, hSelMask, 0, 0, MAKEROP4(SRCCOPY, SRCAND));
+
+                placeSelWin();
+                ShowWindow(hSelection, SW_SHOW);
+            }
+            HeapFree(GetProcessHeap(), 0, ptStack);
+            ptStack = NULL;
+            break;
+        }
+        case TOOL_RECTSEL:
             resetToU1();
             if ((rectSel_src[2] != 0) && (rectSel_src[3] != 0))
             {
-                DeleteObject(SelectObject
-                             (hSelDC, hSelBm =
-                              (HBITMAP) CreateDIBWithProperties(rectSel_src[2], rectSel_src[3])));
+                DeleteObject(hSelMask);
+                hSelMask = CreateBitmap(rectSel_src[2], rectSel_src[3], 1, 1, NULL);
+                DeleteObject(SelectObject(hSelDC, hSelMask));
+                Rect(hSelDC, 0, 0, rectSel_src[2], rectSel_src[3], 0x00ffffff, 0x00ffffff, 1, 2);
+                SelectObject(hSelDC, hSelBm = CreateDIBWithProperties(rectSel_src[2], rectSel_src[3]));
+                resetToU1();
                 BitBlt(hSelDC, 0, 0, rectSel_src[2], rectSel_src[3], hDrawingDC, rectSel_src[0],
                        rectSel_src[1], SRCCOPY);
+                Rect(hdc, rectSel_src[0], rectSel_src[1], rectSel_src[0] + rectSel_src[2],
+                     rectSel_src[1] + rectSel_src[3], bgColor, bgColor, 0, TRUE);
+                newReversible();
+
+                BitBlt(hDrawingDC, rectSel_src[0], rectSel_src[1], rectSel_src[2], rectSel_src[3], hSelDC, 0,
+                       0, SRCCOPY);
+
                 placeSelWin();
                 ShowWindow(hSelection, SW_SHOW);
             }
             break;
-        case 3:
+        case TOOL_RUBBER:
             Erase(hdc, lastX, lastY, x, y, bg, rubberRadius);
             break;
-        case 7:
+        case TOOL_PEN:
             Line(hdc, lastX, lastY, x, y, fg, 1);
             SetPixel(hdc, x, y, fg);
             break;
-        case 11:
+        case TOOL_LINE:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                roundTo8Directions(startX, startY, &x, &y);
             Line(hdc, startX, startY, x, y, fg, lineWidth);
             break;
-        case 12:
+        case TOOL_BEZIER:
             pointSP++;
             if (pointSP == 4)
                 pointSP = 0;
             break;
-        case 13:
+        case TOOL_RECT:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             Rect(hdc, startX, startY, x, y, fg, bg, lineWidth, shapeStyle);
             break;
-        case 14:
+        case TOOL_SHAPE:
             resetToU1();
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
+            if ((pointSP > 0) && (GetAsyncKeyState(VK_SHIFT) < 0))
+                roundTo8Directions(pointStack[pointSP - 1].x, pointStack[pointSP - 1].y,
+                                   (short *)&pointStack[pointSP].x, (short *)&pointStack[pointSP].y);
             pointSP++;
             if (pointSP >= 2)
             {
@@ -228,12 +360,16 @@ endPaintingL(HDC hdc, short x, short y, int fg, int bg)
             if (pointSP == 255)
                 pointSP--;
             break;
-        case 15:
+        case TOOL_ELLIPSE:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             Ellp(hdc, startX, startY, x, y, fg, bg, lineWidth, shapeStyle);
             break;
-        case 16:
+        case TOOL_RRECT:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             RRect(hdc, startX, startY, x, y, fg, bg, lineWidth, shapeStyle);
             break;
     }
@@ -248,34 +384,35 @@ startPaintingR(HDC hdc, short x, short y, int fg, int bg)
     lastY = y;
     switch (activeTool)
     {
-        case 1:
-        case 10:
-        case 11:
-        case 13:
-        case 15:
-        case 16:
+        case TOOL_FREESEL:
+        case TOOL_TEXT:
+        case TOOL_LINE:
+        case TOOL_RECT:
+        case TOOL_ELLIPSE:
+        case TOOL_RRECT:
             newReversible();
-        case 3:
+            break;
+        case TOOL_RUBBER:
             newReversible();
             Replace(hdc, x, y, x, y, fg, bg, rubberRadius);
             break;
-        case 4:
+        case TOOL_FILL:
             newReversible();
             Fill(hdc, x, y, bg);
             break;
-        case 7:
+        case TOOL_PEN:
             newReversible();
             SetPixel(hdc, x, y, bg);
             break;
-        case 8:
+        case TOOL_BRUSH:
             newReversible();
             Brush(hdc, x, y, x, y, bg, brushStyle);
             break;
-        case 9:
+        case TOOL_AIRBRUSH:
             newReversible();
             Airbrush(hdc, x, y, bg, airBrushWidth);
             break;
-        case 12:
+        case TOOL_BEZIER:
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
             if (pointSP == 0)
@@ -284,7 +421,7 @@ startPaintingR(HDC hdc, short x, short y, int fg, int bg)
                 pointSP++;
             }
             break;
-        case 14:
+        case TOOL_SHAPE:
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
             if (pointSP + 1 >= 2)
@@ -303,23 +440,25 @@ whilePaintingR(HDC hdc, short x, short y, int fg, int bg)
 {
     switch (activeTool)
     {
-        case 3:
+        case TOOL_RUBBER:
             Replace(hdc, lastX, lastY, x, y, fg, bg, rubberRadius);
             break;
-        case 7:
+        case TOOL_PEN:
             Line(hdc, lastX, lastY, x, y, bg, 1);
             break;
-        case 8:
+        case TOOL_BRUSH:
             Brush(hdc, lastX, lastY, x, y, bg, brushStyle);
             break;
-        case 9:
+        case TOOL_AIRBRUSH:
             Airbrush(hdc, x, y, bg, airBrushWidth);
             break;
-        case 11:
+        case TOOL_LINE:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                roundTo8Directions(startX, startY, &x, &y);
             Line(hdc, startX, startY, x, y, bg, lineWidth);
             break;
-        case 12:
+        case TOOL_BEZIER:
             resetToU1();
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
@@ -337,23 +476,32 @@ whilePaintingR(HDC hdc, short x, short y, int fg, int bg)
                     break;
             }
             break;
-        case 13:
+        case TOOL_RECT:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             Rect(hdc, startX, startY, x, y, bg, fg, lineWidth, shapeStyle);
             break;
-        case 14:
+        case TOOL_SHAPE:
             resetToU1();
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
+            if ((pointSP > 0) && (GetAsyncKeyState(VK_SHIFT) < 0))
+                roundTo8Directions(pointStack[pointSP - 1].x, pointStack[pointSP - 1].y,
+                                   (short *)&pointStack[pointSP].x, (short *)&pointStack[pointSP].y);
             if (pointSP + 1 >= 2)
                 Poly(hdc, pointStack, pointSP + 1, bg, fg, lineWidth, shapeStyle, FALSE);
             break;
-        case 15:
+        case TOOL_ELLIPSE:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             Ellp(hdc, startX, startY, x, y, bg, fg, lineWidth, shapeStyle);
             break;
-        case 16:
+        case TOOL_RRECT:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             RRect(hdc, startX, startY, x, y, bg, fg, lineWidth, shapeStyle);
             break;
     }
@@ -367,30 +515,37 @@ endPaintingR(HDC hdc, short x, short y, int fg, int bg)
 {
     switch (activeTool)
     {
-        case 3:
+        case TOOL_RUBBER:
             Replace(hdc, lastX, lastY, x, y, fg, bg, rubberRadius);
             break;
-        case 7:
+        case TOOL_PEN:
             Line(hdc, lastX, lastY, x, y, bg, 1);
             SetPixel(hdc, x, y, bg);
             break;
-        case 11:
+        case TOOL_LINE:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                roundTo8Directions(startX, startY, &x, &y);
             Line(hdc, startX, startY, x, y, bg, lineWidth);
             break;
-        case 12:
+        case TOOL_BEZIER:
             pointSP++;
             if (pointSP == 4)
                 pointSP = 0;
             break;
-        case 13:
+        case TOOL_RECT:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             Rect(hdc, startX, startY, x, y, bg, fg, lineWidth, shapeStyle);
             break;
-        case 14:
+        case TOOL_SHAPE:
             resetToU1();
             pointStack[pointSP].x = x;
             pointStack[pointSP].y = y;
+            if ((pointSP > 0) && (GetAsyncKeyState(VK_SHIFT) < 0))
+                roundTo8Directions(pointStack[pointSP - 1].x, pointStack[pointSP - 1].y,
+                                   (short *)&pointStack[pointSP].x, (short *)&pointStack[pointSP].y);
             pointSP++;
             if (pointSP >= 2)
             {
@@ -408,12 +563,16 @@ endPaintingR(HDC hdc, short x, short y, int fg, int bg)
             if (pointSP == 255)
                 pointSP--;
             break;
-        case 15:
+        case TOOL_ELLIPSE:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             Ellp(hdc, startX, startY, x, y, bg, fg, lineWidth, shapeStyle);
             break;
-        case 16:
+        case TOOL_RRECT:
             resetToU1();
+            if (GetAsyncKeyState(VK_SHIFT) < 0)
+                regularize(startX, startY, &x, &y);
             RRect(hdc, startX, startY, x, y, bg, fg, lineWidth, shapeStyle);
             break;
     }