- strmini.h: Include windef.h so CDECL gets defined for msvc version of DEFINE_GUIDEX...
[reactos.git] / reactos / base / applications / paint / winproc.c
index f5a1241..9500bdb 100644 (file)
@@ -13,6 +13,7 @@
 #include <commctrl.h>
 //#include <htmlhelp.h>
 #include <stdio.h>
+#include <tchar.h>
 #include "definitions.h"
 #include "globalvar.h"
 #include "dialogs.h"
@@ -20,6 +21,7 @@
 #include "drawing.h"
 #include "history.h"
 #include "mouse.h"
+#include "registry.h"
 
 /* FUNCTIONS ********************************************************/
 
@@ -27,6 +29,7 @@ void selectTool(int tool)
 {
     ShowWindow(hSelection, SW_HIDE);
     activeTool = tool;
+    pointSP = 0; // resets the point-buffer of the polygon and bezier functions
     SendMessage(hToolSettings, WM_PAINT, 0, 0);
 }
 
@@ -58,15 +61,21 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
             break;
         case WM_CLOSE:
+            if (hwnd==hwndMiniature)
+            {
+                ShowWindow(hwndMiniature, SW_HIDE);
+                showMiniature = FALSE;
+                break;
+            }
             if (undoSteps>0)
             {
-                char programname[20];
-                char saveprompttext[100];
-                LoadString(hProgInstance, IDS_PROGRAMNAME, (LPTSTR)programname, sizeof(programname));
-                LoadString(hProgInstance, IDS_SAVEPROMPTTEXT, (LPTSTR)saveprompttext, sizeof(saveprompttext));
-                char temptext[500];
-                sprintf(temptext, saveprompttext, filename);
-                switch (MessageBox(hwnd, (LPTSTR)temptext, (LPTSTR)programname, MB_YESNOCANCEL | MB_ICONQUESTION))
+                TCHAR programname[20];
+                TCHAR saveprompttext[100];
+                TCHAR temptext[500];
+                LoadString(hProgInstance, IDS_PROGRAMNAME, programname, SIZEOF(programname));
+                LoadString(hProgInstance, IDS_SAVEPROMPTTEXT, saveprompttext, SIZEOF(saveprompttext));
+                _stprintf(temptext, saveprompttext, filename);
+                switch (MessageBox(hwnd, temptext, programname, MB_YESNOCANCEL | MB_ICONQUESTION))
                 {
                     case IDNO:
                         DestroyWindow(hwnd);
@@ -86,7 +95,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
             switch (lParam)
             {
                 case 0:
-                    if (FALSE)
+                    if (isAFile)
                     {
                         EnableMenuItem(GetMenu(hMainWnd), IDM_FILEASWALLPAPERPLANE, MF_ENABLED | MF_BYCOMMAND);
                         EnableMenuItem(GetMenu(hMainWnd), IDM_FILEASWALLPAPERCENTERED, MF_ENABLED | MF_BYCOMMAND);
@@ -142,6 +151,44 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                         CheckMenuItem(GetMenu(hMainWnd), IDM_IMAGEDRAWOPAQUE, MF_UNCHECKED | MF_BYCOMMAND);
                     break;
             }
+            if (showGrid)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWSHOWGRID, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWSHOWGRID, MF_UNCHECKED | MF_BYCOMMAND);
+            if (showMiniature)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWSHOWMINIATURE, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWSHOWMINIATURE, MF_UNCHECKED | MF_BYCOMMAND);
+
+            if (zoom==125)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM125, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM125, MF_UNCHECKED | MF_BYCOMMAND);
+            if (zoom==250)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM25, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM25, MF_UNCHECKED | MF_BYCOMMAND);
+            if (zoom==500)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM50, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM50, MF_UNCHECKED | MF_BYCOMMAND);
+            if (zoom==1000)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM100, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM100, MF_UNCHECKED | MF_BYCOMMAND);
+            if (zoom==2000)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM200, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM200, MF_UNCHECKED | MF_BYCOMMAND);
+            if (zoom==4000)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM400, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM400, MF_UNCHECKED | MF_BYCOMMAND);
+            if (zoom==8000)
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM800, MF_CHECKED | MF_BYCOMMAND);
+            else
+                CheckMenuItem(GetMenu(hMainWnd), IDM_VIEWZOOM800, MF_UNCHECKED | MF_BYCOMMAND);
+
             break;
         case WM_SIZE:
             if (hwnd==hMainWnd)
@@ -152,14 +199,42 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                 MoveWindow(hScrollbox, 56, 49,LOWORD(lParam)-56, HIWORD(lParam)-72, TRUE);
                 //InvalidateRect(hwnd, NULL, TRUE);
             }
+            if (hwnd==hImageArea)
+            {
+                MoveWindow(hSizeboxLeftTop, 
+                    0, 
+                    0, 3, 3, TRUE);
+                MoveWindow(hSizeboxCenterTop, 
+                    imgXRes*zoom/2000+3*3/4, 
+                    0, 3, 3, TRUE);
+                MoveWindow(hSizeboxRightTop, 
+                    imgXRes*zoom/1000+3, 
+                    0, 3, 3, TRUE);
+                MoveWindow(hSizeboxLeftCenter, 
+                    0, 
+                    imgYRes*zoom/2000+3*3/4, 3, 3, TRUE);
+                MoveWindow(hSizeboxRightCenter, 
+                    imgXRes*zoom/1000+3, 
+                    imgYRes*zoom/2000+3*3/4, 3, 3, TRUE);
+                MoveWindow(hSizeboxLeftBottom, 
+                    0, 
+                    imgYRes*zoom/1000+3, 3, 3, TRUE);
+                MoveWindow(hSizeboxCenterBottom, 
+                    imgXRes*zoom/2000+3*3/4, 
+                    imgYRes*zoom/1000+3, 3, 3, TRUE);
+                MoveWindow(hSizeboxRightBottom, 
+                    imgXRes*zoom/1000+3, 
+                    imgYRes*zoom/1000+3, 3, 3, TRUE);
+            }
             if ((hwnd==hImageArea)||(hwnd==hScrollbox))
             {
                 long clientRectScrollbox[4];
-                GetClientRect(hScrollbox, (LPRECT)&clientRectScrollbox);
                 long clientRectImageArea[4];
+                SCROLLINFO horzScroll;
+                SCROLLINFO vertScroll;
+                GetClientRect(hScrollbox, (LPRECT)&clientRectScrollbox);
                 GetClientRect(hImageArea, (LPRECT)&clientRectImageArea);
                 MoveWindow(hScrlClient, 0, 0, max(clientRectImageArea[2]+6, clientRectScrollbox[2]), max(clientRectImageArea[3]+6, clientRectScrollbox[3]), TRUE);
-                SCROLLINFO horzScroll;
                 horzScroll.cbSize       = sizeof(SCROLLINFO);
                 horzScroll.fMask        = SIF_PAGE | SIF_RANGE;
                 horzScroll.nMax         = 10000;
@@ -169,7 +244,6 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                 horzScroll.nTrackPos    = 0;
                 SetScrollInfo(hScrollbox, SB_HORZ, &horzScroll, TRUE);
                 GetClientRect(hScrollbox, (LPRECT)clientRectScrollbox);
-                SCROLLINFO vertScroll;
                 vertScroll.cbSize       = sizeof(SCROLLINFO);
                 vertScroll.fMask        = SIF_PAGE | SIF_RANGE;
                 vertScroll.nMax         = 10000;
@@ -216,8 +290,35 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
             {
                 HDC hdc = GetDC(hImageArea);
                 StretchBlt(hdc, 0, 0, imgXRes*zoom/1000, imgYRes*zoom/1000, hDrawingDC, 0, 0, imgXRes, imgYRes, SRCCOPY);
+                if (showGrid && (zoom>=4000))
+                {
+                    HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x00a0a0a0));
+                    int counter;
+                    for (counter = 0; counter <= imgYRes; counter++)
+                    {
+                        MoveToEx(hdc, 0, counter*zoom/1000, NULL);
+                        LineTo(hdc, imgXRes*zoom/1000, counter*zoom/1000);
+                    }
+                    for (counter = 0; counter <= imgXRes; counter++)
+                    {
+                        MoveToEx(hdc, counter*zoom/1000, 0, NULL);
+                        LineTo(hdc, counter*zoom/1000, imgYRes*zoom/1000);
+                    }
+                    DeleteObject(SelectObject(hdc, oldPen));
+                }
                 ReleaseDC(hImageArea, hdc);
                 SendMessage(hSelection, WM_PAINT, 0, 0);
+                SendMessage(hwndMiniature, WM_PAINT, 0, 0);
+            }else
+            if (hwnd==hwndMiniature)
+            {
+                long mclient[4];
+                HDC hdc;
+                GetClientRect(hwndMiniature, (LPRECT)&mclient);
+                hdc = GetDC(hwndMiniature);
+                BitBlt(hdc, -min(imgXRes*GetScrollPos(hScrollbox, SB_HORZ)/10000, imgXRes-mclient[2]), 
+                    -min(imgYRes*GetScrollPos(hScrollbox, SB_VERT)/10000, imgYRes-mclient[3]), imgXRes, imgYRes, hDrawingDC, 0, 0, SRCCOPY);
+                ReleaseDC(hwndMiniature, hdc);
             }
             break; 
             
@@ -255,7 +356,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                 {
                     SetCapture(hImageArea);
                     drawing = TRUE;
-                    startPainting(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, fgColor, bgColor);
+                    startPaintingL(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, fgColor, bgColor);
                 }else
                 {
                     SendMessage(hwnd, WM_LBUTTONUP, wParam, lParam);
@@ -272,7 +373,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                 {
                     SetCapture(hImageArea);
                     drawing = TRUE;
-                    startPainting(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, bgColor, fgColor);
+                    startPaintingR(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, fgColor, bgColor);
                 }else
                 {
                     SendMessage(hwnd, WM_RBUTTONUP, wParam, lParam);
@@ -287,7 +388,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
             { 
                 ReleaseCapture();
                 drawing = FALSE;
-                endPainting(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, fgColor, bgColor);
+                endPaintingL(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, fgColor, bgColor);
                 SendMessage(hImageArea, WM_PAINT, 0, 0);
                 if (activeTool==5)
                 {
@@ -303,7 +404,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
             { 
                 ReleaseCapture();
                 drawing = FALSE;
-                endPainting(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, bgColor, fgColor);
+                endPaintingR(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, fgColor, bgColor);
                 SendMessage(hImageArea, WM_PAINT, 0, 0);
                 if (activeTool==5)
                 {
@@ -319,38 +420,38 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
             {
                 if ((!drawing)||(activeTool<=9))
                 {
-                    char coordStr[100];
-                    sprintf(coordStr, "%d, %d", (short)LOWORD(lParam)*1000/zoom, (short)HIWORD(lParam)*1000/zoom);
+                    TCHAR coordStr[100];
+                    _stprintf(coordStr, _T("%d, %d"), (short)LOWORD(lParam)*1000/zoom, (short)HIWORD(lParam)*1000/zoom);
                     SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM)coordStr);
                 }
                 if (drawing)
                 {
                     if ((wParam&MK_LBUTTON)!=0)
                     {
-                        whilePainting(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, fgColor, bgColor);
+                        whilePaintingL(hDrawingDC, (short)LOWORD(lParam)*1000/zoom, (short)HIWORD(lParam)*1000/zoom, fgColor, bgColor);
                         SendMessage(hImageArea, WM_PAINT, 0, 0);
                         if ((activeTool>=10)||(activeTool==2))
                         {
-                            char sizeStr[100];
-                            sprintf(sizeStr, "%d x %d", (short)LOWORD(lParam)*1000/zoom-startX, (short)HIWORD(lParam)*1000/zoom-startY);
+                            TCHAR sizeStr[100];
+                            _stprintf(sizeStr, _T("%d x %d"), (short)LOWORD(lParam)*1000/zoom-startX, (short)HIWORD(lParam)*1000/zoom-startY);
                             SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM)sizeStr);
                         }
                     }
                     if ((wParam&MK_RBUTTON)!=0)
                     {
-                        whilePainting(hDrawingDC, LOWORD(lParam)*1000/zoom, HIWORD(lParam)*1000/zoom, bgColor, fgColor);
+                        whilePaintingR(hDrawingDC, (short)LOWORD(lParam)*1000/zoom, (short)HIWORD(lParam)*1000/zoom, fgColor, bgColor);
                         SendMessage(hImageArea, WM_PAINT, 0, 0);
                         if (activeTool>=10)
                         {
-                            char sizeStr[100];
-                            sprintf(sizeStr, "%d x %d", (short)LOWORD(lParam)*1000/zoom-startX, (short)HIWORD(lParam)*1000/zoom-startY);
+                            TCHAR sizeStr[100];
+                            _stprintf(sizeStr, _T("%d x %d"), (short)LOWORD(lParam)*1000/zoom-startX, (short)HIWORD(lParam)*1000/zoom-startY);
                             SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM)sizeStr);
                         }
                     }
                 }
             } else
             {
-                SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM)"");
+                SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM)_T(""));
             }
             break;
             
@@ -362,11 +463,11 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                 case IDM_HELPINFO:
                     {
                         HICON paintIcon = LoadIcon(hProgInstance, MAKEINTRESOURCE(IDI_APPICON));
-                        char infotitle[100];
-                        char infotext[200];
-                        LoadString(hProgInstance, IDS_INFOTITLE, (LPTSTR)infotitle, sizeof(infotitle));
-                        LoadString(hProgInstance, IDS_INFOTEXT, (LPTSTR)infotext, sizeof(infotext));
-                        ShellAbout(hMainWnd, (LPTSTR)infotitle, (LPTSTR)infotext, paintIcon);
+                        TCHAR infotitle[100];
+                        TCHAR infotext[200];
+                        LoadString(hProgInstance, IDS_INFOTITLE, infotitle, SIZEOF(infotitle));
+                        LoadString(hProgInstance, IDS_INFOTEXT, infotext, SIZEOF(infotext));
+                        ShellAbout(hMainWnd, infotitle, infotext, paintIcon);
                         DeleteObject(paintIcon);
                     }
                     break;
@@ -386,14 +487,14 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                         HBITMAP bmNew = (HBITMAP)LoadDIBFromFile(ofn.lpstrFile);
                         if (bmNew!=NULL)
                         {
+                            TCHAR tempstr[1000];
+                            TCHAR resstr[100];
                             insertReversible(bmNew);
                             updateCanvasAndScrollbars();
-                            char tempstr[1000];
-                            char resstr[100];
                             CopyMemory(filename, ofn.lpstrFileTitle, sizeof(filename));
                             CopyMemory(filepathname, ofn.lpstrFileTitle, sizeof(filepathname));
-                            LoadString(hProgInstance, IDS_WINDOWTITLE, (LPTSTR)resstr, sizeof(resstr));
-                            sprintf(tempstr, resstr, filename);
+                            LoadString(hProgInstance, IDS_WINDOWTITLE, resstr, SIZEOF(resstr));
+                            _stprintf(tempstr, resstr, filename);
                             SetWindowText(hMainWnd, tempstr);
                             clearHistory();
                             isAFile = TRUE;
@@ -409,17 +510,26 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                 case IDM_FILESAVEAS:
                     if (GetSaveFileName(&sfn)!=0)
                     {
+                        TCHAR tempstr[1000];
+                        TCHAR resstr[100];
                         SaveDIBToFile(hBms[currInd], sfn.lpstrFile, hDrawingDC);
-                        char tempstr[1000];
-                        char resstr[100];
                         CopyMemory(filename, sfn.lpstrFileTitle, sizeof(filename));
                         CopyMemory(filepathname, sfn.lpstrFileTitle, sizeof(filepathname));
-                        LoadString(hProgInstance, IDS_WINDOWTITLE, (LPTSTR)resstr, sizeof(resstr));
-                        sprintf(tempstr, resstr, filename);
+                        LoadString(hProgInstance, IDS_WINDOWTITLE, resstr, SIZEOF(resstr));
+                        _stprintf(tempstr, resstr, filename);
                         SetWindowText(hMainWnd, tempstr);
                         isAFile = TRUE;
                     }
                     break;
+                case IDM_FILEASWALLPAPERPLANE:
+                    SetWallpaper(filepathname, 1, 1);
+                    break;
+                case IDM_FILEASWALLPAPERCENTERED:
+                    SetWallpaper(filepathname, 1, 0);
+                    break;
+                case IDM_FILEASWALLPAPERSTRETCHED:
+                    SetWallpaper(filepathname, 2, 0);
+                    break;
                 case IDM_EDITUNDO:
                     undo();
                     SendMessage(hImageArea, WM_PAINT, 0, 0);
@@ -456,9 +566,9 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                 case IDM_EDITSELECTALL:
                     if (activeTool==2)
                     {
-                        startPainting(hDrawingDC, 0, 0, fgColor, bgColor);
-                        whilePainting(hDrawingDC, imgXRes, imgYRes, fgColor, bgColor);
-                        endPainting(hDrawingDC, imgXRes, imgYRes, fgColor, bgColor);
+                        startPaintingL(hDrawingDC, 0, 0, fgColor, bgColor);
+                        whilePaintingL(hDrawingDC, imgXRes, imgYRes, fgColor, bgColor);
+                        endPaintingL(hDrawingDC, imgXRes, imgYRes, fgColor, bgColor);
                     }
                     break;
                 case IDM_EDITCOPYTO:
@@ -473,9 +583,10 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                     break;
                 case IDM_IMAGEINVERTCOLORS:
                     {
+                        RECT tempRect;
                         newReversible();
-                        int tempRect[4] = {0, 0, imgXRes, imgYRes};
-                        InvertRect(hDrawingDC, (LPRECT)tempRect);
+                        SetRect(&tempRect, 0, 0, imgXRes, imgYRes);
+                        InvertRect(hDrawingDC, &tempRect);
                         SendMessage(hImageArea, WM_PAINT, 0, 0);
                     }
                     break; 
@@ -506,10 +617,12 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                     break;
                 case IDM_IMAGEATTRIBUTES:
                     {
-                        //int attrdata[8] = {0, 0, 0, 0, 0, 0, 0, 0};
-                        attributesDlg();
-                        //cropReversible(640, 200);
-                        //ZoomTo(zoom);
+                        int retVal = attributesDlg();
+                        if ((LOWORD(retVal)!=0)&&(HIWORD(retVal)!=0))
+                        {
+                            cropReversible(LOWORD(retVal), HIWORD(retVal), 0, 0);
+                            updateCanvasAndScrollbars();
+                        }
                     }
                     break;
                 case IDM_IMAGECHANGESIZE:
@@ -530,6 +643,17 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
                     insertReversible(CopyImage(hSelBm, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG));
                     updateCanvasAndScrollbars();
                     break;
+
+                case IDM_VIEWSHOWGRID:
+                    showGrid = !showGrid;
+                    break;
+                case IDM_VIEWSHOWMINIATURE:
+                    showMiniature = !showMiniature;
+                    if (showMiniature)
+                        ShowWindow(hwndMiniature, SW_SHOW);
+                    else
+                        ShowWindow(hwndMiniature, SW_HIDE);
+                    break;
                     
                 case IDM_VIEWZOOM125:
                     ZoomTo(125);