From 7d2efc06f261fa8a88bf6a73b4b09ac093b6b512 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Sat, 25 Oct 2008 10:58:35 +0900 Subject: [PATCH] [3/19] paint: Add TOOL_BOX, TOOL_ELLIPSE and TOOL_ROUNDRECT --- programs/paint/canvas.c | 198 +++++++++++++++++++++++++++++++++++++++++++++ programs/paint/fill.bmp | Bin 0 -> 1638 bytes programs/paint/main.c | 41 +++++++++- programs/paint/main.h | 1 + programs/paint/resource.h | 1 + programs/paint/rsrc.rc | 2 + 6 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 programs/paint/fill.bmp diff --git a/programs/paint/canvas.c b/programs/paint/canvas.c index 3d18902..4c8d416 100644 --- a/programs/paint/canvas.c +++ b/programs/paint/canvas.c @@ -48,6 +48,22 @@ VOID ImageToCanvas2(POINT *ppt) ppt->y = ppt->y * Globals.nZoom + 4; } +INT sgn(INT x) +{ + if (x > 0) return 1; + if (x < 0) return -1; + return 0; +} + +VOID Regularize(POINT pt0, LPPOINT ppt1) +{ + INT cx = abs(ppt1->x - pt0.x); + INT cy = abs(ppt1->y - pt0.y); + INT m = min(cx, cy); + ppt1->x = pt0.x + sgn(ppt1->x - pt0.x) * m; + ppt1->y = pt0.y + sgn(ppt1->y - pt0.y) * m; +} + VOID PrepareForUndo(VOID) { Globals.fCanUndo = TRUE; @@ -78,6 +94,76 @@ VOID Canvas_DrawBuffer(HDC hDC) DeleteObject(hPen); break; + case TOOL_BOX: + case TOOL_ELLIPSE: + case TOOL_ROUNDRECT: + if (Globals.fSwapColor) + { + switch (Globals.iFillStyle) + { + case 0: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbBack); + hbr = (HBRUSH)GetStockObject(NULL_BRUSH); + break; + + case 1: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbBack); + hbr = CreateSolidBrush(Globals.rgbFore); + break; + + default: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbBack); + hbr = CreateSolidBrush(Globals.rgbBack); + } + } + else + { + switch (Globals.iFillStyle) + { + case 0: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbFore); + hbr = (HBRUSH)GetStockObject(NULL_BRUSH); + break; + + case 1: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbFore); + hbr = CreateSolidBrush(Globals.rgbBack); + break; + + default: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbFore); + hbr = CreateSolidBrush(Globals.rgbFore); + } + } + hpenOld = SelectObject(hDC, hPen); + hbrOld = SelectObject(hDC, hbr); + if (GetKeyState(VK_SHIFT) < 0) + Regularize(Globals.pt0, &Globals.pt1); + switch(Globals.iToolSelect) + { + case TOOL_BOX: + Rectangle(hDC, Globals.pt0.x, Globals.pt0.y, Globals.pt1.x, Globals.pt1.y); + break; + + case TOOL_ELLIPSE: + Ellipse(hDC, Globals.pt0.x, Globals.pt0.y, Globals.pt1.x, Globals.pt1.y); + break; + + default: + RoundRect(hDC, Globals.pt0.x, Globals.pt0.y, Globals.pt1.x, Globals.pt1.y, 16, 16); + } + SelectObject(hDC, hpenOld); + SelectObject(hDC, hbrOld); + DeleteObject(hPen); + DeleteObject(hbr); + break; + default: break; } @@ -330,6 +416,9 @@ VOID Canvas_OnButtonDown(HWND hWnd, INT x, INT y, BOOL fRight) break; case TOOL_LINE: + case TOOL_BOX: + case TOOL_ELLIPSE: + case TOOL_ROUNDRECT: SetCursor(Globals.hcurCross2); Globals.mode = MODE_CANVAS; SetCapture(hWnd); @@ -447,6 +536,18 @@ VOID Canvas_OnMouseMove(HWND hWnd, INT x, INT y, BOOL fLeftDown, BOOL fRightDown UpdateWindow(hWnd); break; + case TOOL_BOX: + case TOOL_ELLIPSE: + case TOOL_ROUNDRECT: + SetCursor(Globals.hcurCross2); + CanvasToImage(&pt); + Globals.pt1 = pt; + ShowSize(Globals.pt1.x - Globals.pt0.x, + Globals.pt1.y - Globals.pt0.y); + InvalidateRect(hWnd, NULL, FALSE); + UpdateWindow(hWnd); + break; + case TOOL_PENCIL: SetCursor(Globals.hcurPencil); CanvasToImage(&pt); @@ -491,6 +592,9 @@ VOID Canvas_OnMouseMove(HWND hWnd, INT x, INT y, BOOL fLeftDown, BOOL fRightDown switch (Globals.iToolSelect) { case TOOL_LINE: + case TOOL_BOX: + case TOOL_ELLIPSE: + case TOOL_ROUNDRECT: SetCursor(Globals.hcurCross2); CanvasToImage(&pt); Globals.pt1 = pt; @@ -719,6 +823,100 @@ VOID Canvas_OnButtonUp(HWND hWnd, INT x, INT y, BOOL fRight) UpdateWindow(hWnd); break; + case TOOL_BOX: + case TOOL_ELLIPSE: + case TOOL_ROUNDRECT: + CanvasToImage(&pt); + PrepareForUndo(); + hDC = GetDC(hWnd); + if (hDC != NULL) + { + hMemDC = CreateCompatibleDC(hDC); + if (hMemDC != NULL) + { + HGDIOBJ hpenOld, hbrOld, hbmOld; + HPEN hPen; + HBRUSH hbr; + + if (Globals.fSwapColor) + { + switch (Globals.iFillStyle) + { + case 0: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbBack); + hbr = (HBRUSH)GetStockObject(NULL_BRUSH); + break; + + case 1: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbBack); + hbr = CreateSolidBrush(Globals.rgbFore); + break; + + default: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbBack); + hbr = CreateSolidBrush(Globals.rgbBack); + } + } + else + { + switch (Globals.iFillStyle) + { + case 0: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbFore); + hbr = (HBRUSH)GetStockObject(NULL_BRUSH); + break; + + case 1: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbFore); + hbr = CreateSolidBrush(Globals.rgbBack); + break; + + default: + hPen = CreatePen(PS_SOLID, Globals.nLineWidth, + Globals.rgbFore); + hbr = CreateSolidBrush(Globals.rgbFore); + } + } + + hbmOld = SelectObject(hMemDC, Globals.hbmImage); + hpenOld = SelectObject(hMemDC, hPen); + hbrOld = SelectObject(hMemDC, hbr); + if (GetKeyState(VK_SHIFT) < 0) + Regularize(Globals.pt0, &pt); + switch(Globals.iToolSelect) + { + case TOOL_BOX: + Rectangle(hMemDC, Globals.pt0.x, Globals.pt0.y, + pt.x, pt.y); + break; + + case TOOL_ELLIPSE: + Ellipse(hMemDC, Globals.pt0.x, Globals.pt0.y, + pt.x, pt.y); + break; + + default: + RoundRect(hMemDC, Globals.pt0.x, Globals.pt0.y, + pt.x, pt.y, 16, 16); + } + SelectObject(hMemDC, hpenOld); + SelectObject(hMemDC, hbrOld); + SelectObject(hMemDC, hbmOld); + DeleteObject(hPen); + DeleteObject(hbr); + DeleteDC(hMemDC); + Globals.fModified = TRUE; + } + ReleaseDC(hWnd, hDC); + } + SetRectEmpty((RECT*)&Globals.pt0); + break; + case TOOL_LINE: CanvasToImage(&pt); PrepareForUndo(); diff --git a/programs/paint/fill.bmp b/programs/paint/fill.bmp new file mode 100644 index 0000000000000000000000000000000000000000..897e204251ef17a8b9755e90cd2d8dd00d7b83af GIT binary patch literal 1638 zcmZ?rO=DvKgEAng0mVK*nvsD8EdGI&f#C=r1cL?OL<0i@5HU0~Kqw#yWCC#m!+`?_ zfD-?~h~fW#5DSQb5F`ZR4=V5h=sF|-cH3|k8&rWmK#sv0za7Y+0g?`og*t|a7*b*e aG6yJxhlaIS(*cUpKS0vKP!t<9=>P!K=il}K literal 0 HcmV?d00001 diff --git a/programs/paint/main.c b/programs/paint/main.c index 0a77ab1..e0376a6 100644 --- a/programs/paint/main.c +++ b/programs/paint/main.c @@ -292,6 +292,7 @@ static VOID PAINT_InitData(VOID) Globals.hcurCross2 = LoadCursor(Globals.hInstance, MAKEINTRESOURCE(12)); Globals.nLineWidth = 1; + Globals.iFillStyle = 0; Globals.nZoom = 1; Globals.fShowGrid = FALSE; @@ -562,6 +563,23 @@ VOID ToolBox_OnPaint(HWND hWnd, HDC hDC) InvertRect(hDC, &rc); break; + case TOOL_BOX: + case TOOL_ROUNDRECT: + case TOOL_ELLIPSE: + hbm = LoadBitmap(Globals.hInstance, MAKEINTRESOURCE(IDB_FILL)); + hbmOld = SelectObject(hdcMem, hbm); + BitBlt(hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, + hdcMem, 0, 0, SRCCOPY); + SelectObject(hdcMem, hbmOld); + DeleteObject(hbm); + + rc.left = 6; + rc.top = 210 + (290 - 210) * Globals.iFillStyle / 3; + rc.right = 48; + rc.bottom = rc.top + (290 - 210) / 3; + InvertRect(hDC, &rc); + break; + default: break; } @@ -608,9 +626,6 @@ void ToolBox_OnLButton(HWND hWnd, int x, int y, BOOL fDown) case TOOL_POLYGON: case TOOL_MAGNIFIER: case TOOL_CURVE: - case TOOL_BOX: - case TOOL_ELLIPSE: - case TOOL_ROUNDRECT: NotSupportedYet(); break; @@ -631,6 +646,26 @@ void ToolBox_OnLButton(HWND hWnd, int x, int y, BOOL fDown) switch (Globals.iToolSelect) { + case TOOL_BOX: + case TOOL_ROUNDRECT: + case TOOL_ELLIPSE: + for (i = 0; i < 3; i++) + { + rc.left = 6; + rc.top = 210 + (290 - 210) * i / 3; + rc.right = 48; + rc.bottom = rc.top + (290 - 210) / 3; + + if (PtInRect(&rc, pt)) + { + Globals.iFillStyle = i; + InvalidateRect(hWnd, NULL, TRUE); + UpdateWindow(hWnd); + break; + } + } + break; + case TOOL_LINE: rc.left = 6; rc.right = 48; diff --git a/programs/paint/main.h b/programs/paint/main.h index c12bf48..b016f09 100644 --- a/programs/paint/main.h +++ b/programs/paint/main.h @@ -108,6 +108,7 @@ typedef struct BOOL fShowGrid; INT nLineWidth; + INT iFillStyle; INT xScrollPos; INT yScrollPos; diff --git a/programs/paint/resource.h b/programs/paint/resource.h index 2fd5fe9..d85872b 100644 --- a/programs/paint/resource.h +++ b/programs/paint/resource.h @@ -24,6 +24,7 @@ #define ID_ACCEL 0x204 #define IDB_TOOLS 0x205 #define IDB_LINE 0x20D +#define IDB_FILL 0x20F #define IDD_ZOOM 0x207 #define IDD_STRETCH_SKEW 0x208 #define IDD_ATTRIBUTES 0x209 diff --git a/programs/paint/rsrc.rc b/programs/paint/rsrc.rc index ee15c2a..5435e7d 100644 --- a/programs/paint/rsrc.rc +++ b/programs/paint/rsrc.rc @@ -63,6 +63,8 @@ IDI_PAINT ICON "paint.ico" IDB_TOOLS BITMAP "tools.bmp" /* @makedep: line.bmp */ IDB_LINE BITMAP "line.bmp" +/* @makedep: fill.bmp */ +IDB_FILL BITMAP "fill.bmp" /* @makedep: horizon.cur */ 1 CURSOR "horizon.cur" -- 1.6.0.2