13b079154SKatayama Hirofumi MZ /*
23b079154SKatayama Hirofumi MZ  * PROJECT:    ReactOS Notepad
33b079154SKatayama Hirofumi MZ  * LICENSE:    LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
43b079154SKatayama Hirofumi MZ  * PURPOSE:    Providing a Windows-compatible simple text editor for ReactOS
5fb1d9d72SKatayama Hirofumi MZ  * COPYRIGHT:  Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
6fb1d9d72SKatayama Hirofumi MZ  *             Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
7fb1d9d72SKatayama Hirofumi MZ  *             Copyright 2002 Andriy Palamarchuk
8fb1d9d72SKatayama Hirofumi MZ  *             Copyright 2020-2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
93b079154SKatayama Hirofumi MZ  */
103b079154SKatayama Hirofumi MZ 
113b079154SKatayama Hirofumi MZ #include "notepad.h"
123b079154SKatayama Hirofumi MZ 
133b079154SKatayama Hirofumi MZ #include <strsafe.h>
143b079154SKatayama Hirofumi MZ #include <assert.h>
153b079154SKatayama Hirofumi MZ 
AlertPrintError(VOID)163b079154SKatayama Hirofumi MZ static VOID AlertPrintError(VOID)
173b079154SKatayama Hirofumi MZ {
183b079154SKatayama Hirofumi MZ     TCHAR szUntitled[MAX_STRING_LEN];
193b079154SKatayama Hirofumi MZ 
20*d28e39e4SKatayama Hirofumi MZ     LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, _countof(szUntitled));
213b079154SKatayama Hirofumi MZ 
223b079154SKatayama Hirofumi MZ     DIALOG_StringMsgBox(Globals.hMainWnd, STRING_PRINTERROR,
233b079154SKatayama Hirofumi MZ                         Globals.szFileName[0] ? Globals.szFileName : szUntitled,
243b079154SKatayama Hirofumi MZ                         MB_ICONEXCLAMATION | MB_OK);
253b079154SKatayama Hirofumi MZ }
263b079154SKatayama Hirofumi MZ 
273b079154SKatayama Hirofumi MZ static RECT
GetPrintingRect(IN HDC hdc,IN LPCRECT pMargins)283b079154SKatayama Hirofumi MZ GetPrintingRect(IN HDC hdc, IN LPCRECT pMargins)
293b079154SKatayama Hirofumi MZ {
303b079154SKatayama Hirofumi MZ     INT iLogPixelsX = GetDeviceCaps(hdc, LOGPIXELSX);
313b079154SKatayama Hirofumi MZ     INT iLogPixelsY = GetDeviceCaps(hdc, LOGPIXELSY);
323b079154SKatayama Hirofumi MZ     INT iHorzRes = GetDeviceCaps(hdc, HORZRES); /* in pixels */
333b079154SKatayama Hirofumi MZ     INT iVertRes = GetDeviceCaps(hdc, VERTRES); /* in pixels */
343b079154SKatayama Hirofumi MZ     RECT rcPrintRect, rcPhysical;
353b079154SKatayama Hirofumi MZ 
363b079154SKatayama Hirofumi MZ #define CONVERT_X(x) MulDiv((x), iLogPixelsX, 2540) /* 100th millimeters to pixels */
373b079154SKatayama Hirofumi MZ #define CONVERT_Y(y) MulDiv((y), iLogPixelsY, 2540) /* 100th millimeters to pixels */
383b079154SKatayama Hirofumi MZ     SetRect(&rcPrintRect,
393b079154SKatayama Hirofumi MZ             CONVERT_X(pMargins->left), CONVERT_Y(pMargins->top),
403b079154SKatayama Hirofumi MZ             iHorzRes - CONVERT_X(pMargins->right),
413b079154SKatayama Hirofumi MZ             iVertRes - CONVERT_Y(pMargins->bottom));
423b079154SKatayama Hirofumi MZ 
433b079154SKatayama Hirofumi MZ     rcPhysical.left = GetDeviceCaps(hdc, PHYSICALOFFSETX);
443b079154SKatayama Hirofumi MZ     rcPhysical.right = rcPhysical.left + GetDeviceCaps(hdc, PHYSICALWIDTH);
453b079154SKatayama Hirofumi MZ     rcPhysical.top = GetDeviceCaps(hdc, PHYSICALOFFSETY);
463b079154SKatayama Hirofumi MZ     rcPhysical.bottom = rcPhysical.top + GetDeviceCaps(hdc, PHYSICALHEIGHT);
473b079154SKatayama Hirofumi MZ 
483b079154SKatayama Hirofumi MZ     /* Adjust the margin */
493b079154SKatayama Hirofumi MZ     rcPrintRect.left = max(rcPrintRect.left, rcPhysical.left);
503b079154SKatayama Hirofumi MZ     rcPrintRect.top = max(rcPrintRect.top, rcPhysical.top);
513b079154SKatayama Hirofumi MZ     rcPrintRect.right = min(rcPrintRect.right, rcPhysical.right);
523b079154SKatayama Hirofumi MZ     rcPrintRect.bottom = min(rcPrintRect.bottom, rcPhysical.bottom);
533b079154SKatayama Hirofumi MZ 
543b079154SKatayama Hirofumi MZ     return rcPrintRect;
553b079154SKatayama Hirofumi MZ }
563b079154SKatayama Hirofumi MZ 
GetSelectionTextLength(HWND hWnd)573b079154SKatayama Hirofumi MZ static INT GetSelectionTextLength(HWND hWnd)
583b079154SKatayama Hirofumi MZ {
593b079154SKatayama Hirofumi MZ     DWORD dwStart = 0, dwEnd = 0;
603b079154SKatayama Hirofumi MZ     SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
613b079154SKatayama Hirofumi MZ     return dwEnd - dwStart;
623b079154SKatayama Hirofumi MZ }
633b079154SKatayama Hirofumi MZ 
GetSelectionText(HWND hWnd,LPTSTR lpString,INT nMaxCount)643b079154SKatayama Hirofumi MZ static INT GetSelectionText(HWND hWnd, LPTSTR lpString, INT nMaxCount)
653b079154SKatayama Hirofumi MZ {
663b079154SKatayama Hirofumi MZ     DWORD dwStart = 0, dwEnd = 0;
673b079154SKatayama Hirofumi MZ     INT cchText = GetWindowTextLength(hWnd);
683b079154SKatayama Hirofumi MZ     LPTSTR pszText;
693b079154SKatayama Hirofumi MZ     HLOCAL hLocal;
703b079154SKatayama Hirofumi MZ     HRESULT hr;
713b079154SKatayama Hirofumi MZ 
723b079154SKatayama Hirofumi MZ     SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
733b079154SKatayama Hirofumi MZ     if (!lpString || dwStart == dwEnd || cchText == 0)
743b079154SKatayama Hirofumi MZ         return 0;
753b079154SKatayama Hirofumi MZ 
763b079154SKatayama Hirofumi MZ     hLocal = (HLOCAL)SendMessage(hWnd, EM_GETHANDLE, 0, 0);
773b079154SKatayama Hirofumi MZ     pszText = (LPTSTR)LocalLock(hLocal);
783b079154SKatayama Hirofumi MZ     if (!pszText)
793b079154SKatayama Hirofumi MZ         return 0;
803b079154SKatayama Hirofumi MZ 
813b079154SKatayama Hirofumi MZ     hr = StringCchCopyN(lpString, nMaxCount, pszText + dwStart, dwEnd - dwStart);
823b079154SKatayama Hirofumi MZ     LocalUnlock(hLocal);
833b079154SKatayama Hirofumi MZ 
843b079154SKatayama Hirofumi MZ     switch (hr)
853b079154SKatayama Hirofumi MZ     {
863b079154SKatayama Hirofumi MZ         case S_OK:
873b079154SKatayama Hirofumi MZ             return dwEnd - dwStart;
883b079154SKatayama Hirofumi MZ 
893b079154SKatayama Hirofumi MZ         case STRSAFE_E_INSUFFICIENT_BUFFER:
903b079154SKatayama Hirofumi MZ             return nMaxCount - 1;
913b079154SKatayama Hirofumi MZ 
923b079154SKatayama Hirofumi MZ         default:
933b079154SKatayama Hirofumi MZ             return 0;
943b079154SKatayama Hirofumi MZ     }
953b079154SKatayama Hirofumi MZ }
963b079154SKatayama Hirofumi MZ 
973b079154SKatayama Hirofumi MZ typedef struct
983b079154SKatayama Hirofumi MZ {
993b079154SKatayama Hirofumi MZ     PRINTDLG printer;
1003b079154SKatayama Hirofumi MZ     HWND hwndDlg;
1013b079154SKatayama Hirofumi MZ     INT status;
1023b079154SKatayama Hirofumi MZ     INT currentPage;
1033b079154SKatayama Hirofumi MZ     RECT printRect;
1043b079154SKatayama Hirofumi MZ     SYSTEMTIME stNow;
1053b079154SKatayama Hirofumi MZ     HFONT hHeaderFont;
1063b079154SKatayama Hirofumi MZ     HFONT hBodyFont;
1073b079154SKatayama Hirofumi MZ     LPTSTR pszText;
1083b079154SKatayama Hirofumi MZ     DWORD ich;
1093b079154SKatayama Hirofumi MZ     DWORD cchText;
1103b079154SKatayama Hirofumi MZ     INT cyHeader;
1113b079154SKatayama Hirofumi MZ     INT cySpacing;
1123b079154SKatayama Hirofumi MZ     INT cyFooter;
1133b079154SKatayama Hirofumi MZ } PRINT_DATA, *PPRINT_DATA;
1143b079154SKatayama Hirofumi MZ 
1153b079154SKatayama Hirofumi MZ /* Convert the points into pixels */
1163b079154SKatayama Hirofumi MZ #define X_POINTS_TO_PIXELS(hDC, points) MulDiv((points), GetDeviceCaps((hDC), LOGPIXELSX), 72)
1173b079154SKatayama Hirofumi MZ #define Y_POINTS_TO_PIXELS(hDC, points) MulDiv((points), GetDeviceCaps((hDC), LOGPIXELSY), 72)
1183b079154SKatayama Hirofumi MZ 
1193b079154SKatayama Hirofumi MZ /*
1203b079154SKatayama Hirofumi MZ  * See also:
1213b079154SKatayama Hirofumi MZ  * https://support.microsoft.com/en-us/windows/changing-header-and-footer-commands-in-notepad-c1b0e27b-497d-c478-c4c1-0da491cac148
1223b079154SKatayama Hirofumi MZ  */
1233b079154SKatayama Hirofumi MZ static VOID
DrawHeaderOrFooter(HDC hDC,LPRECT pRect,LPCTSTR pszFormat,INT nPageNo,const SYSTEMTIME * pstNow)1243b079154SKatayama Hirofumi MZ DrawHeaderOrFooter(HDC hDC, LPRECT pRect, LPCTSTR pszFormat, INT nPageNo, const SYSTEMTIME *pstNow)
1253b079154SKatayama Hirofumi MZ {
1263b079154SKatayama Hirofumi MZ     TCHAR szText[256], szField[128];
1273b079154SKatayama Hirofumi MZ     const TCHAR *pchFormat;
1283b079154SKatayama Hirofumi MZ     UINT uAlign = DT_CENTER, uFlags = DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX;
1293b079154SKatayama Hirofumi MZ     HGDIOBJ hOldPen, hOldBrush;
1303b079154SKatayama Hirofumi MZ 
1313b079154SKatayama Hirofumi MZ     /* Draw a rectangle */
1323b079154SKatayama Hirofumi MZ     hOldPen = SelectObject(hDC, GetStockObject(BLACK_PEN));
1333b079154SKatayama Hirofumi MZ     hOldBrush = SelectObject(hDC, GetStockObject(NULL_BRUSH));
1343b079154SKatayama Hirofumi MZ     Rectangle(hDC, pRect->left, pRect->top, pRect->right, pRect->bottom);
1353b079154SKatayama Hirofumi MZ     SelectObject(hDC, hOldBrush);
1363b079154SKatayama Hirofumi MZ     SelectObject(hDC, hOldPen);
1373b079154SKatayama Hirofumi MZ 
1383b079154SKatayama Hirofumi MZ     InflateRect(pRect, -X_POINTS_TO_PIXELS(hDC, 3), 0); /* Shrink 3pt */
1393b079154SKatayama Hirofumi MZ 
1403b079154SKatayama Hirofumi MZ     szText[0] = 0;
1413b079154SKatayama Hirofumi MZ 
1423b079154SKatayama Hirofumi MZ     for (pchFormat = pszFormat; *pchFormat; ++pchFormat)
1433b079154SKatayama Hirofumi MZ     {
1443b079154SKatayama Hirofumi MZ         if (*pchFormat != _T('&'))
1453b079154SKatayama Hirofumi MZ         {
146*d28e39e4SKatayama Hirofumi MZ             StringCchCatN(szText, _countof(szText), pchFormat, 1);
1473b079154SKatayama Hirofumi MZ             continue;
1483b079154SKatayama Hirofumi MZ         }
1493b079154SKatayama Hirofumi MZ 
1503b079154SKatayama Hirofumi MZ         ++pchFormat;
1513b079154SKatayama Hirofumi MZ         if (*pchFormat == 0)
1523b079154SKatayama Hirofumi MZ             break;
1533b079154SKatayama Hirofumi MZ 
1543b079154SKatayama Hirofumi MZ         switch (_totupper(*pchFormat)) /* Make it uppercase */
1553b079154SKatayama Hirofumi MZ         {
1563b079154SKatayama Hirofumi MZ             case _T('&'): /* Found double ampersand */
157*d28e39e4SKatayama Hirofumi MZ                 StringCchCat(szText, _countof(szText), TEXT("&"));
1583b079154SKatayama Hirofumi MZ                 break;
1593b079154SKatayama Hirofumi MZ 
1603b079154SKatayama Hirofumi MZ             case _T('L'): /* Left */
1613b079154SKatayama Hirofumi MZ                 DrawText(hDC, szText, -1, pRect, uAlign | uFlags);
1623b079154SKatayama Hirofumi MZ                 szText[0] = 0;
1633b079154SKatayama Hirofumi MZ                 uAlign = DT_LEFT;
1643b079154SKatayama Hirofumi MZ                 break;
1653b079154SKatayama Hirofumi MZ 
1663b079154SKatayama Hirofumi MZ             case _T('C'): /* Center */
1673b079154SKatayama Hirofumi MZ                 DrawText(hDC, szText, -1, pRect, uAlign | uFlags);
1683b079154SKatayama Hirofumi MZ                 szText[0] = 0;
1693b079154SKatayama Hirofumi MZ                 uAlign = DT_CENTER;
1703b079154SKatayama Hirofumi MZ                 break;
1713b079154SKatayama Hirofumi MZ 
1723b079154SKatayama Hirofumi MZ             case _T('R'): /* Right */
1733b079154SKatayama Hirofumi MZ                 DrawText(hDC, szText, -1, pRect, uAlign | uFlags);
1743b079154SKatayama Hirofumi MZ                 szText[0] = 0;
1753b079154SKatayama Hirofumi MZ                 uAlign = DT_RIGHT;
1763b079154SKatayama Hirofumi MZ                 break;
1773b079154SKatayama Hirofumi MZ 
1783b079154SKatayama Hirofumi MZ             case _T('D'): /* Date */
1793b079154SKatayama Hirofumi MZ                 GetDateFormat(LOCALE_USER_DEFAULT, 0, pstNow, NULL,
180*d28e39e4SKatayama Hirofumi MZ                               szField, (INT)_countof(szField));
181*d28e39e4SKatayama Hirofumi MZ                 StringCchCat(szText, _countof(szText), szField);
1823b079154SKatayama Hirofumi MZ                 break;
1833b079154SKatayama Hirofumi MZ 
1843b079154SKatayama Hirofumi MZ             case _T('T'): /* Time */
1853b079154SKatayama Hirofumi MZ                 GetTimeFormat(LOCALE_USER_DEFAULT, 0, pstNow, NULL,
186*d28e39e4SKatayama Hirofumi MZ                               szField, (INT)_countof(szField));
187*d28e39e4SKatayama Hirofumi MZ                 StringCchCat(szText, _countof(szText), szField);
1883b079154SKatayama Hirofumi MZ                 break;
1893b079154SKatayama Hirofumi MZ 
1903b079154SKatayama Hirofumi MZ             case _T('F'): /* Filename */
191*d28e39e4SKatayama Hirofumi MZ                 StringCchCat(szText, _countof(szText), Globals.szFileTitle);
1923b079154SKatayama Hirofumi MZ                 break;
1933b079154SKatayama Hirofumi MZ 
1943b079154SKatayama Hirofumi MZ             case _T('P'): /* Page number */
195*d28e39e4SKatayama Hirofumi MZ                 StringCchPrintf(szField, _countof(szField), TEXT("%u"), nPageNo);
196*d28e39e4SKatayama Hirofumi MZ                 StringCchCat(szText, _countof(szText), szField);
1973b079154SKatayama Hirofumi MZ                 break;
1983b079154SKatayama Hirofumi MZ 
1993b079154SKatayama Hirofumi MZ             default: /* Otherwise */
2003b079154SKatayama Hirofumi MZ                 szField[0] = _T('&');
2013b079154SKatayama Hirofumi MZ                 szField[1] = *pchFormat;
2023b079154SKatayama Hirofumi MZ                 szField[2] = 0;
203*d28e39e4SKatayama Hirofumi MZ                 StringCchCat(szText, _countof(szText), szField);
2043b079154SKatayama Hirofumi MZ                 break;
2053b079154SKatayama Hirofumi MZ         }
2063b079154SKatayama Hirofumi MZ     }
2073b079154SKatayama Hirofumi MZ 
2083b079154SKatayama Hirofumi MZ     DrawText(hDC, szText, -1, pRect, uAlign | uFlags);
2093b079154SKatayama Hirofumi MZ }
2103b079154SKatayama Hirofumi MZ 
DoPrintBody(PPRINT_DATA pData,DWORD PageCount,BOOL bSkipPage)2113b079154SKatayama Hirofumi MZ static BOOL DoPrintBody(PPRINT_DATA pData, DWORD PageCount, BOOL bSkipPage)
2123b079154SKatayama Hirofumi MZ {
2133b079154SKatayama Hirofumi MZ     LPPRINTDLG pPrinter = &pData->printer;
2143b079154SKatayama Hirofumi MZ     RECT printRect = pData->printRect;
2153b079154SKatayama Hirofumi MZ     INT xLeft = printRect.left, yTop = printRect.top + pData->cyHeader + pData->cySpacing;
2163b079154SKatayama Hirofumi MZ     INT xStart, tabWidth;
2173b079154SKatayama Hirofumi MZ     DWORD ichStart;
2183b079154SKatayama Hirofumi MZ     SIZE charMetrics;
2193b079154SKatayama Hirofumi MZ     TEXTMETRIC tmText;
2203b079154SKatayama Hirofumi MZ 
2213b079154SKatayama Hirofumi MZ     /* Calculate a tab width */
2223b079154SKatayama Hirofumi MZ #define TAB_STOP 8
2233b079154SKatayama Hirofumi MZ     GetTextMetrics(pPrinter->hDC, &tmText);
2243b079154SKatayama Hirofumi MZ     tabWidth = TAB_STOP * tmText.tmAveCharWidth;
2253b079154SKatayama Hirofumi MZ 
2263b079154SKatayama Hirofumi MZ #define DO_FLUSH() do { \
2273b079154SKatayama Hirofumi MZ     if (ichStart < pData->ich && !bSkipPage) { \
2283b079154SKatayama Hirofumi MZ         TextOut(pPrinter->hDC, xStart, yTop, &pData->pszText[ichStart], pData->ich - ichStart); \
2293b079154SKatayama Hirofumi MZ     } \
2303b079154SKatayama Hirofumi MZ     ichStart = pData->ich; \
2313b079154SKatayama Hirofumi MZ     xStart = xLeft; \
2323b079154SKatayama Hirofumi MZ     if (pData->status == STRING_PRINTCANCELING) return FALSE; \
2333b079154SKatayama Hirofumi MZ } while (0)
2343b079154SKatayama Hirofumi MZ 
2353b079154SKatayama Hirofumi MZ     /* The drawing-body loop */
2363b079154SKatayama Hirofumi MZ     for (ichStart = pData->ich, xStart = xLeft; pData->ich < pData->cchText; )
2373b079154SKatayama Hirofumi MZ     {
2383b079154SKatayama Hirofumi MZ         TCHAR ch = pData->pszText[pData->ich];
2393b079154SKatayama Hirofumi MZ 
2403b079154SKatayama Hirofumi MZ         if (ch == _T('\r'))
2413b079154SKatayama Hirofumi MZ         {
2423b079154SKatayama Hirofumi MZ             DO_FLUSH();
2433b079154SKatayama Hirofumi MZ 
2443b079154SKatayama Hirofumi MZ             pData->ich++; /* Next char */
2453b079154SKatayama Hirofumi MZ             ichStart = pData->ich;
2463b079154SKatayama Hirofumi MZ             continue;
2473b079154SKatayama Hirofumi MZ         }
2483b079154SKatayama Hirofumi MZ 
2493b079154SKatayama Hirofumi MZ         if (ch == _T('\n'))
2503b079154SKatayama Hirofumi MZ         {
2513b079154SKatayama Hirofumi MZ             DO_FLUSH();
2523b079154SKatayama Hirofumi MZ 
2533b079154SKatayama Hirofumi MZ             /* Next line */
2543b079154SKatayama Hirofumi MZ             yTop += tmText.tmHeight;
2553b079154SKatayama Hirofumi MZ             xLeft = xStart = printRect.left;
2563b079154SKatayama Hirofumi MZ         }
2573b079154SKatayama Hirofumi MZ         else
2583b079154SKatayama Hirofumi MZ         {
2593b079154SKatayama Hirofumi MZ             if (ch == _T('\t'))
2603b079154SKatayama Hirofumi MZ             {
2613b079154SKatayama Hirofumi MZ                 INT nStepWidth = tabWidth - ((xLeft - printRect.left) % tabWidth);
2623b079154SKatayama Hirofumi MZ 
2633b079154SKatayama Hirofumi MZ                 DO_FLUSH();
2643b079154SKatayama Hirofumi MZ 
2653b079154SKatayama Hirofumi MZ                 /* Go to the next tab stop */
2663b079154SKatayama Hirofumi MZ                 xLeft += nStepWidth;
2673b079154SKatayama Hirofumi MZ                 xStart = xLeft;
2683b079154SKatayama Hirofumi MZ             }
2693b079154SKatayama Hirofumi MZ             else /* Normal char */
2703b079154SKatayama Hirofumi MZ             {
2713b079154SKatayama Hirofumi MZ                 GetTextExtentPoint32(pPrinter->hDC, &ch, 1, &charMetrics);
2723b079154SKatayama Hirofumi MZ                 xLeft += charMetrics.cx;
2733b079154SKatayama Hirofumi MZ             }
2743b079154SKatayama Hirofumi MZ 
2753b079154SKatayama Hirofumi MZ             /* Insert a line break if the next position reached the right edge */
2763b079154SKatayama Hirofumi MZ             if (xLeft + charMetrics.cx >= printRect.right)
2773b079154SKatayama Hirofumi MZ             {
2783b079154SKatayama Hirofumi MZ                 if (ch != _T('\t'))
2793b079154SKatayama Hirofumi MZ                     DO_FLUSH();
2803b079154SKatayama Hirofumi MZ 
2813b079154SKatayama Hirofumi MZ                 /* Next line */
2823b079154SKatayama Hirofumi MZ                 yTop += tmText.tmHeight;
2833b079154SKatayama Hirofumi MZ                 xLeft = xStart = printRect.left;
2843b079154SKatayama Hirofumi MZ             }
2853b079154SKatayama Hirofumi MZ         }
2863b079154SKatayama Hirofumi MZ 
2873b079154SKatayama Hirofumi MZ         pData->ich++; /* Next char */
2883b079154SKatayama Hirofumi MZ         if (ch == _T('\t') || ch == _T('\n'))
2893b079154SKatayama Hirofumi MZ             ichStart = pData->ich;
2903b079154SKatayama Hirofumi MZ 
2913b079154SKatayama Hirofumi MZ         if (yTop + tmText.tmHeight >= printRect.bottom - pData->cyFooter)
2923b079154SKatayama Hirofumi MZ             break; /* The next line reached the body bottom */
2933b079154SKatayama Hirofumi MZ     }
2943b079154SKatayama Hirofumi MZ 
2953b079154SKatayama Hirofumi MZ     DO_FLUSH();
2963b079154SKatayama Hirofumi MZ     return TRUE;
2973b079154SKatayama Hirofumi MZ }
2983b079154SKatayama Hirofumi MZ 
DoPrintPage(PPRINT_DATA pData,DWORD PageCount)2993b079154SKatayama Hirofumi MZ static BOOL DoPrintPage(PPRINT_DATA pData, DWORD PageCount)
3003b079154SKatayama Hirofumi MZ {
3013b079154SKatayama Hirofumi MZ     LPPRINTDLG pPrinter = &pData->printer;
3023b079154SKatayama Hirofumi MZ     BOOL bSkipPage, ret;
3033b079154SKatayama Hirofumi MZ     HFONT hOldFont;
3043b079154SKatayama Hirofumi MZ 
3053b079154SKatayama Hirofumi MZ     /* Should we skip this page? */
3063b079154SKatayama Hirofumi MZ     bSkipPage = !(pPrinter->Flags & PD_SELECTION) &&
3073b079154SKatayama Hirofumi MZ                 (pPrinter->Flags & PD_PAGENUMS) &&
3083b079154SKatayama Hirofumi MZ                 !(pPrinter->nFromPage <= PageCount && PageCount <= pPrinter->nToPage);
3093b079154SKatayama Hirofumi MZ 
3103b079154SKatayama Hirofumi MZ     /* The prologue of a page */
3113b079154SKatayama Hirofumi MZ     if (!bSkipPage)
3123b079154SKatayama Hirofumi MZ     {
3133b079154SKatayama Hirofumi MZ         if (StartPage(pPrinter->hDC) <= 0)
3143b079154SKatayama Hirofumi MZ         {
3153b079154SKatayama Hirofumi MZ             pData->status = STRING_PRINTFAILED;
3163b079154SKatayama Hirofumi MZ             return FALSE;
3173b079154SKatayama Hirofumi MZ         }
3183b079154SKatayama Hirofumi MZ 
3193b079154SKatayama Hirofumi MZ         if (pData->cyHeader > 0)
3203b079154SKatayama Hirofumi MZ         {
3213b079154SKatayama Hirofumi MZ             /* Draw the page header */
3223b079154SKatayama Hirofumi MZ             RECT rc = pData->printRect;
3233b079154SKatayama Hirofumi MZ             rc.bottom = rc.top + pData->cyHeader;
3243b079154SKatayama Hirofumi MZ 
3253b079154SKatayama Hirofumi MZ             hOldFont = SelectObject(pPrinter->hDC, pData->hHeaderFont);
3263b079154SKatayama Hirofumi MZ             DrawHeaderOrFooter(pPrinter->hDC, &rc, Globals.szHeader, PageCount, &pData->stNow);
3273b079154SKatayama Hirofumi MZ             SelectObject(pPrinter->hDC, hOldFont); /* De-select the font */
3283b079154SKatayama Hirofumi MZ         }
3293b079154SKatayama Hirofumi MZ     }
3303b079154SKatayama Hirofumi MZ 
3313b079154SKatayama Hirofumi MZ     hOldFont = SelectObject(pPrinter->hDC, pData->hBodyFont);
3323b079154SKatayama Hirofumi MZ     ret = DoPrintBody(pData, PageCount, bSkipPage);
3333b079154SKatayama Hirofumi MZ     SelectObject(pPrinter->hDC, hOldFont);
3343b079154SKatayama Hirofumi MZ     if (!ret)
3353b079154SKatayama Hirofumi MZ         return FALSE; /* Canceled */
3363b079154SKatayama Hirofumi MZ 
3373b079154SKatayama Hirofumi MZ     /* The epilogue of a page */
3383b079154SKatayama Hirofumi MZ     if (!bSkipPage)
3393b079154SKatayama Hirofumi MZ     {
3403b079154SKatayama Hirofumi MZ         if (pData->cyFooter > 0)
3413b079154SKatayama Hirofumi MZ         {
3423b079154SKatayama Hirofumi MZ             /* Draw the page footer */
3433b079154SKatayama Hirofumi MZ             RECT rc = pData->printRect;
3443b079154SKatayama Hirofumi MZ             rc.top = rc.bottom - pData->cyFooter;
3453b079154SKatayama Hirofumi MZ 
3463b079154SKatayama Hirofumi MZ             hOldFont = SelectObject(pPrinter->hDC, pData->hHeaderFont);
3473b079154SKatayama Hirofumi MZ             DrawHeaderOrFooter(pPrinter->hDC, &rc, Globals.szFooter, PageCount, &pData->stNow);
3483b079154SKatayama Hirofumi MZ             SelectObject(pPrinter->hDC, hOldFont);
3493b079154SKatayama Hirofumi MZ         }
3503b079154SKatayama Hirofumi MZ 
3513b079154SKatayama Hirofumi MZ         if (EndPage(pPrinter->hDC) <= 0)
3523b079154SKatayama Hirofumi MZ         {
3533b079154SKatayama Hirofumi MZ             pData->status = STRING_PRINTFAILED;
3543b079154SKatayama Hirofumi MZ             return FALSE;
3553b079154SKatayama Hirofumi MZ         }
3563b079154SKatayama Hirofumi MZ     }
3573b079154SKatayama Hirofumi MZ 
3583b079154SKatayama Hirofumi MZ     return TRUE;
3593b079154SKatayama Hirofumi MZ }
3603b079154SKatayama Hirofumi MZ 
3613b079154SKatayama Hirofumi MZ #define BODY_FONT_SIZE      10 /* 10pt */
3623b079154SKatayama Hirofumi MZ #define HEADER_FONT_SIZE    9  /* 9pt */
3633b079154SKatayama Hirofumi MZ #define SPACING_HEIGHT      4  /* 4pt */
3643b079154SKatayama Hirofumi MZ #define PRINTING_MESSAGE (WM_USER + 100)
3653b079154SKatayama Hirofumi MZ 
DoCreatePrintFonts(LPPRINTDLG pPrinter,PPRINT_DATA pPrintData)3663b079154SKatayama Hirofumi MZ static BOOL DoCreatePrintFonts(LPPRINTDLG pPrinter, PPRINT_DATA pPrintData)
3673b079154SKatayama Hirofumi MZ {
3683b079154SKatayama Hirofumi MZ     LOGFONT lfBody, lfHeader;
3693b079154SKatayama Hirofumi MZ 
3703b079154SKatayama Hirofumi MZ     /* Create the main text font for printing */
3713b079154SKatayama Hirofumi MZ     lfBody = Globals.lfFont;
3723b079154SKatayama Hirofumi MZ     lfBody.lfHeight = -Y_POINTS_TO_PIXELS(pPrinter->hDC, BODY_FONT_SIZE);
3733b079154SKatayama Hirofumi MZ     pPrintData->hBodyFont = CreateFontIndirect(&lfBody);
3743b079154SKatayama Hirofumi MZ     if (pPrintData->hBodyFont == NULL)
3753b079154SKatayama Hirofumi MZ         return FALSE;
3763b079154SKatayama Hirofumi MZ 
3773b079154SKatayama Hirofumi MZ     /* Create the header/footer font */
3783b079154SKatayama Hirofumi MZ     lfHeader = Globals.lfFont;
3793b079154SKatayama Hirofumi MZ     lfHeader.lfHeight = -Y_POINTS_TO_PIXELS(pPrinter->hDC, HEADER_FONT_SIZE);
3803b079154SKatayama Hirofumi MZ     lfHeader.lfWeight = FW_BOLD;
3813b079154SKatayama Hirofumi MZ     pPrintData->hHeaderFont = CreateFontIndirect(&lfHeader);
3823b079154SKatayama Hirofumi MZ     if (pPrintData->hHeaderFont == NULL)
3833b079154SKatayama Hirofumi MZ         return FALSE;
3843b079154SKatayama Hirofumi MZ 
3853b079154SKatayama Hirofumi MZ     return TRUE;
3863b079154SKatayama Hirofumi MZ }
3873b079154SKatayama Hirofumi MZ 
DoPrintDocument(PPRINT_DATA printData)3883b079154SKatayama Hirofumi MZ static BOOL DoPrintDocument(PPRINT_DATA printData)
3893b079154SKatayama Hirofumi MZ {
3903b079154SKatayama Hirofumi MZ     DOCINFO docInfo;
3913b079154SKatayama Hirofumi MZ     LPPRINTDLG pPrinter = &printData->printer;
3923b079154SKatayama Hirofumi MZ     DWORD CopyCount, PageCount;
3933b079154SKatayama Hirofumi MZ     TEXTMETRIC tmHeader;
3943b079154SKatayama Hirofumi MZ     BOOL ret = FALSE;
3953b079154SKatayama Hirofumi MZ     HFONT hOldFont;
3963b079154SKatayama Hirofumi MZ 
3973b079154SKatayama Hirofumi MZ     GetLocalTime(&printData->stNow);
3983b079154SKatayama Hirofumi MZ 
3993b079154SKatayama Hirofumi MZ     printData->printRect = GetPrintingRect(pPrinter->hDC, &Globals.lMargins);
4003b079154SKatayama Hirofumi MZ 
4013b079154SKatayama Hirofumi MZ     if (!DoCreatePrintFonts(pPrinter, printData))
4023b079154SKatayama Hirofumi MZ     {
4033b079154SKatayama Hirofumi MZ         printData->status = STRING_PRINTFAILED;
4043b079154SKatayama Hirofumi MZ         goto Quit;
4053b079154SKatayama Hirofumi MZ     }
4063b079154SKatayama Hirofumi MZ 
4073b079154SKatayama Hirofumi MZ     if (pPrinter->Flags & PD_SELECTION)
4083b079154SKatayama Hirofumi MZ         printData->cchText = GetSelectionTextLength(Globals.hEdit);
4093b079154SKatayama Hirofumi MZ     else
4103b079154SKatayama Hirofumi MZ         printData->cchText = GetWindowTextLength(Globals.hEdit);
4113b079154SKatayama Hirofumi MZ 
4123b079154SKatayama Hirofumi MZ     /* Allocate a buffer for the text */
4133b079154SKatayama Hirofumi MZ     printData->pszText = HeapAlloc(GetProcessHeap(), 0, (printData->cchText + 1) * sizeof(TCHAR));
4143b079154SKatayama Hirofumi MZ     if (!printData->pszText)
4153b079154SKatayama Hirofumi MZ     {
4163b079154SKatayama Hirofumi MZ         printData->status = STRING_PRINTFAILED;
4173b079154SKatayama Hirofumi MZ         goto Quit;
4183b079154SKatayama Hirofumi MZ     }
4193b079154SKatayama Hirofumi MZ 
4203b079154SKatayama Hirofumi MZ     if (pPrinter->Flags & PD_SELECTION)
4213b079154SKatayama Hirofumi MZ         GetSelectionText(Globals.hEdit, printData->pszText, printData->cchText + 1);
4223b079154SKatayama Hirofumi MZ     else
4233b079154SKatayama Hirofumi MZ         GetWindowText(Globals.hEdit, printData->pszText, printData->cchText + 1);
4243b079154SKatayama Hirofumi MZ 
4253b079154SKatayama Hirofumi MZ     /* Start a document */
4263b079154SKatayama Hirofumi MZ     ZeroMemory(&docInfo, sizeof(docInfo));
4273b079154SKatayama Hirofumi MZ     docInfo.cbSize = sizeof(DOCINFO);
4283b079154SKatayama Hirofumi MZ     docInfo.lpszDocName = Globals.szFileTitle;
4293b079154SKatayama Hirofumi MZ     if (StartDoc(pPrinter->hDC, &docInfo) <= 0)
4303b079154SKatayama Hirofumi MZ     {
4313b079154SKatayama Hirofumi MZ         printData->status = STRING_PRINTFAILED;
4323b079154SKatayama Hirofumi MZ         goto Quit;
4333b079154SKatayama Hirofumi MZ     }
4343b079154SKatayama Hirofumi MZ 
4353b079154SKatayama Hirofumi MZ     /* Calculate the header and footer heights */
4363b079154SKatayama Hirofumi MZ     hOldFont = SelectObject(pPrinter->hDC, printData->hHeaderFont);
4373b079154SKatayama Hirofumi MZ     GetTextMetrics(pPrinter->hDC, &tmHeader);
4383b079154SKatayama Hirofumi MZ     printData->cyHeader = printData->cyFooter = 2 * tmHeader.tmHeight;
4393b079154SKatayama Hirofumi MZ     printData->cySpacing = Y_POINTS_TO_PIXELS(pPrinter->hDC, SPACING_HEIGHT);
4403b079154SKatayama Hirofumi MZ     SelectObject(pPrinter->hDC, hOldFont); /* De-select the font */
4413b079154SKatayama Hirofumi MZ     if (!Globals.szHeader[0])
4423b079154SKatayama Hirofumi MZ         printData->cyHeader = printData->cySpacing = 0;
4433b079154SKatayama Hirofumi MZ     if (!Globals.szFooter[0])
4443b079154SKatayama Hirofumi MZ         printData->cyFooter = 0;
4453b079154SKatayama Hirofumi MZ 
4463b079154SKatayama Hirofumi MZ     /* The printing-copies loop */
4473b079154SKatayama Hirofumi MZ     for (CopyCount = 1; CopyCount <= pPrinter->nCopies; ++CopyCount)
4483b079154SKatayama Hirofumi MZ     {
4493b079154SKatayama Hirofumi MZ         /* The printing-pages loop */
4503b079154SKatayama Hirofumi MZ         for (PageCount = 1, printData->ich = 0; printData->ich < printData->cchText; ++PageCount)
4513b079154SKatayama Hirofumi MZ         {
4523b079154SKatayama Hirofumi MZ             printData->currentPage = PageCount;
4533b079154SKatayama Hirofumi MZ             PostMessage(printData->hwndDlg, PRINTING_MESSAGE, 0, 0);
4543b079154SKatayama Hirofumi MZ 
4553b079154SKatayama Hirofumi MZ             if (!DoPrintPage(printData, PageCount))
4563b079154SKatayama Hirofumi MZ             {
4573b079154SKatayama Hirofumi MZ                 AbortDoc(pPrinter->hDC); /* Cancel printing */
4583b079154SKatayama Hirofumi MZ                 goto Quit;
4593b079154SKatayama Hirofumi MZ             }
4603b079154SKatayama Hirofumi MZ         }
4613b079154SKatayama Hirofumi MZ     }
4623b079154SKatayama Hirofumi MZ 
4633b079154SKatayama Hirofumi MZ     if (EndDoc(pPrinter->hDC) <= 0)
4643b079154SKatayama Hirofumi MZ     {
4653b079154SKatayama Hirofumi MZ         printData->status = STRING_PRINTFAILED;
4663b079154SKatayama Hirofumi MZ         goto Quit;
4673b079154SKatayama Hirofumi MZ     }
4683b079154SKatayama Hirofumi MZ 
4693b079154SKatayama Hirofumi MZ     ret = TRUE;
4703b079154SKatayama Hirofumi MZ     printData->status = STRING_PRINTCOMPLETE;
4713b079154SKatayama Hirofumi MZ 
4723b079154SKatayama Hirofumi MZ Quit:
4733b079154SKatayama Hirofumi MZ     DeleteObject(printData->hHeaderFont);
4743b079154SKatayama Hirofumi MZ     DeleteObject(printData->hBodyFont);
4753b079154SKatayama Hirofumi MZ     if (printData->pszText)
4763b079154SKatayama Hirofumi MZ         HeapFree(GetProcessHeap(), 0, printData->pszText);
4773b079154SKatayama Hirofumi MZ     if (printData->status == STRING_PRINTCANCELING)
4783b079154SKatayama Hirofumi MZ         printData->status = STRING_PRINTCANCELED;
4793b079154SKatayama Hirofumi MZ     PostMessage(printData->hwndDlg, PRINTING_MESSAGE, 0, 0);
4803b079154SKatayama Hirofumi MZ     return ret;
4813b079154SKatayama Hirofumi MZ }
4823b079154SKatayama Hirofumi MZ 
PrintThreadFunc(LPVOID arg)4833b079154SKatayama Hirofumi MZ static DWORD WINAPI PrintThreadFunc(LPVOID arg)
4843b079154SKatayama Hirofumi MZ {
4853b079154SKatayama Hirofumi MZ     PPRINT_DATA pData = arg;
4863b079154SKatayama Hirofumi MZ     pData->currentPage = 1;
4873b079154SKatayama Hirofumi MZ     pData->status = STRING_NOWPRINTING;
4883b079154SKatayama Hirofumi MZ     PostMessage(pData->hwndDlg, PRINTING_MESSAGE, 0, 0);
4893b079154SKatayama Hirofumi MZ     return DoPrintDocument(pData);
4903b079154SKatayama Hirofumi MZ }
4913b079154SKatayama Hirofumi MZ 
4923b079154SKatayama Hirofumi MZ static INT_PTR CALLBACK
DIALOG_Printing_DialogProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)4933b079154SKatayama Hirofumi MZ DIALOG_Printing_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
4943b079154SKatayama Hirofumi MZ {
4953b079154SKatayama Hirofumi MZ     TCHAR szText[MAX_STRING_LEN];
4963b079154SKatayama Hirofumi MZ     static TCHAR s_szPage[64];
4973b079154SKatayama Hirofumi MZ     static PPRINT_DATA s_pData = NULL;
4983b079154SKatayama Hirofumi MZ     static HANDLE s_hThread = NULL;
4993b079154SKatayama Hirofumi MZ 
5003b079154SKatayama Hirofumi MZ     switch (uMsg)
5013b079154SKatayama Hirofumi MZ     {
5023b079154SKatayama Hirofumi MZ         case WM_INITDIALOG:
5033b079154SKatayama Hirofumi MZ             s_pData = (PPRINT_DATA)lParam;
5043b079154SKatayama Hirofumi MZ             s_pData->hwndDlg = hwnd;
5053b079154SKatayama Hirofumi MZ             SetDlgItemText(hwnd, IDC_PRINTING_FILENAME, Globals.szFileTitle);
506*d28e39e4SKatayama Hirofumi MZ             GetDlgItemText(hwnd, IDC_PRINTING_PAGE, s_szPage, _countof(s_szPage));
5073b079154SKatayama Hirofumi MZ             SetDlgItemText(hwnd, IDC_PRINTING_PAGE, NULL);
5083b079154SKatayama Hirofumi MZ 
5093b079154SKatayama Hirofumi MZ             s_hThread = CreateThread(NULL, 0, PrintThreadFunc, s_pData, 0, NULL);
5103b079154SKatayama Hirofumi MZ             if (!s_hThread)
5113b079154SKatayama Hirofumi MZ             {
5123b079154SKatayama Hirofumi MZ                 s_pData->status = STRING_PRINTFAILED;
5133b079154SKatayama Hirofumi MZ                 EndDialog(hwnd, IDABORT);
5143b079154SKatayama Hirofumi MZ             }
5153b079154SKatayama Hirofumi MZ             return TRUE;
5163b079154SKatayama Hirofumi MZ 
5173b079154SKatayama Hirofumi MZ         case PRINTING_MESSAGE:
5183b079154SKatayama Hirofumi MZ             switch (s_pData->status)
5193b079154SKatayama Hirofumi MZ             {
5203b079154SKatayama Hirofumi MZ                 case STRING_NOWPRINTING:
5213b079154SKatayama Hirofumi MZ                 case STRING_PRINTCANCELING:
522*d28e39e4SKatayama Hirofumi MZ                     StringCchPrintf(szText, _countof(szText), s_szPage, s_pData->currentPage);
5233b079154SKatayama Hirofumi MZ                     SetDlgItemText(hwnd, IDC_PRINTING_PAGE, szText);
5243b079154SKatayama Hirofumi MZ 
525*d28e39e4SKatayama Hirofumi MZ                     LoadString(Globals.hInstance, s_pData->status, szText, _countof(szText));
5263b079154SKatayama Hirofumi MZ                     SetDlgItemText(hwnd, IDC_PRINTING_STATUS, szText);
5273b079154SKatayama Hirofumi MZ                     break;
5283b079154SKatayama Hirofumi MZ 
5293b079154SKatayama Hirofumi MZ                 case STRING_PRINTCOMPLETE:
5303b079154SKatayama Hirofumi MZ                 case STRING_PRINTCANCELED:
5313b079154SKatayama Hirofumi MZ                 case STRING_PRINTFAILED:
532*d28e39e4SKatayama Hirofumi MZ                     LoadString(Globals.hInstance, s_pData->status, szText, _countof(szText));
5333b079154SKatayama Hirofumi MZ                     SetDlgItemText(hwnd, IDC_PRINTING_STATUS, szText);
5343b079154SKatayama Hirofumi MZ 
5353b079154SKatayama Hirofumi MZ                     if (s_pData->status == STRING_PRINTCOMPLETE)
5363b079154SKatayama Hirofumi MZ                         EndDialog(hwnd, IDOK);
5373b079154SKatayama Hirofumi MZ                     else if (s_pData->status == STRING_PRINTFAILED)
5383b079154SKatayama Hirofumi MZ                         EndDialog(hwnd, IDABORT);
5393b079154SKatayama Hirofumi MZ                     else
5403b079154SKatayama Hirofumi MZ                         EndDialog(hwnd, IDCANCEL);
5413b079154SKatayama Hirofumi MZ                     break;
5423b079154SKatayama Hirofumi MZ             }
5433b079154SKatayama Hirofumi MZ             break;
5443b079154SKatayama Hirofumi MZ 
5453b079154SKatayama Hirofumi MZ         case WM_COMMAND:
5463b079154SKatayama Hirofumi MZ             if (LOWORD(wParam) == IDCANCEL && s_pData->status == STRING_NOWPRINTING)
5473b079154SKatayama Hirofumi MZ             {
5483b079154SKatayama Hirofumi MZ                 EnableWindow(GetDlgItem(hwnd, IDCANCEL), FALSE);
5493b079154SKatayama Hirofumi MZ                 s_pData->status = STRING_PRINTCANCELING;
5503b079154SKatayama Hirofumi MZ                 PostMessage(hwnd, PRINTING_MESSAGE, 0, 0);
5513b079154SKatayama Hirofumi MZ             }
5523b079154SKatayama Hirofumi MZ             break;
5533b079154SKatayama Hirofumi MZ 
5543b079154SKatayama Hirofumi MZ         case WM_DESTROY:
5553b079154SKatayama Hirofumi MZ             if (s_hThread)
5563b079154SKatayama Hirofumi MZ                 CloseHandle(s_hThread);
5573b079154SKatayama Hirofumi MZ             DeleteDC(s_pData->printer.hDC);
5583b079154SKatayama Hirofumi MZ             s_pData = LocalFree(s_pData);
5593b079154SKatayama Hirofumi MZ             break;
5603b079154SKatayama Hirofumi MZ     }
5613b079154SKatayama Hirofumi MZ 
5623b079154SKatayama Hirofumi MZ     return 0;
5633b079154SKatayama Hirofumi MZ }
5643b079154SKatayama Hirofumi MZ 
DIALOG_FilePrint(VOID)5653b079154SKatayama Hirofumi MZ VOID DIALOG_FilePrint(VOID)
5663b079154SKatayama Hirofumi MZ {
5673b079154SKatayama Hirofumi MZ     BOOL ret;
5683b079154SKatayama Hirofumi MZ     LPPRINTDLG printer;
5693b079154SKatayama Hirofumi MZ     PPRINT_DATA printData = LocalAlloc(LPTR, sizeof(PRINT_DATA));
5703b079154SKatayama Hirofumi MZ     if (!printData)
5713b079154SKatayama Hirofumi MZ     {
5723b079154SKatayama Hirofumi MZ         ShowLastError();
5733b079154SKatayama Hirofumi MZ         return;
5743b079154SKatayama Hirofumi MZ     }
5753b079154SKatayama Hirofumi MZ 
5763b079154SKatayama Hirofumi MZ     printer = &printData->printer;
5773b079154SKatayama Hirofumi MZ     printer->lStructSize = sizeof(PRINTDLG);
5783b079154SKatayama Hirofumi MZ     printer->hwndOwner = Globals.hMainWnd;
5793b079154SKatayama Hirofumi MZ     printer->Flags = PD_RETURNDC | PD_SELECTION;
5803b079154SKatayama Hirofumi MZ 
5813b079154SKatayama Hirofumi MZ     /* Disable the selection radio button if there is no text selected */
5823b079154SKatayama Hirofumi MZ     if (!GetSelectionTextLength(Globals.hEdit))
5833b079154SKatayama Hirofumi MZ         printer->Flags |= PD_NOSELECTION;
5843b079154SKatayama Hirofumi MZ 
5853b079154SKatayama Hirofumi MZ     printer->nFromPage = 1;
5863b079154SKatayama Hirofumi MZ     printer->nToPage = MAXWORD;
5873b079154SKatayama Hirofumi MZ     printer->nMinPage = 1;
5883b079154SKatayama Hirofumi MZ     printer->nMaxPage = MAXWORD;
5893b079154SKatayama Hirofumi MZ 
5903b079154SKatayama Hirofumi MZ     printer->hDevMode = Globals.hDevMode;
5913b079154SKatayama Hirofumi MZ     printer->hDevNames = Globals.hDevNames;
5923b079154SKatayama Hirofumi MZ 
5933b079154SKatayama Hirofumi MZ     ret = PrintDlg(printer);
5943b079154SKatayama Hirofumi MZ     /* NOTE: Even if PrintDlg returns FALSE, hDevMode and hDevNames may have changed. */
5953b079154SKatayama Hirofumi MZ     Globals.hDevMode = printer->hDevMode;
5963b079154SKatayama Hirofumi MZ     Globals.hDevNames = printer->hDevNames;
5973b079154SKatayama Hirofumi MZ 
5983b079154SKatayama Hirofumi MZ     if (!ret)
5993b079154SKatayama Hirofumi MZ     {
6003b079154SKatayama Hirofumi MZ         LocalFree(printData);
6013b079154SKatayama Hirofumi MZ         return; /* The user canceled printing */
6023b079154SKatayama Hirofumi MZ     }
6033b079154SKatayama Hirofumi MZ     assert(printer->hDC != NULL);
6043b079154SKatayama Hirofumi MZ 
6053b079154SKatayama Hirofumi MZ     /* Ensure that each logical unit maps to one pixel */
6063b079154SKatayama Hirofumi MZ     SetMapMode(printer->hDC, MM_TEXT);
6073b079154SKatayama Hirofumi MZ 
6083b079154SKatayama Hirofumi MZ     if (DialogBoxParam(Globals.hInstance,
6093b079154SKatayama Hirofumi MZ                        MAKEINTRESOURCE(DIALOG_PRINTING),
6103b079154SKatayama Hirofumi MZ                        Globals.hMainWnd,
6113b079154SKatayama Hirofumi MZ                        DIALOG_Printing_DialogProc,
6123b079154SKatayama Hirofumi MZ                        (LPARAM)printer) == IDABORT)
6133b079154SKatayama Hirofumi MZ     {
6143b079154SKatayama Hirofumi MZ         AlertPrintError();
6153b079154SKatayama Hirofumi MZ     }
6163b079154SKatayama Hirofumi MZ }
6173b079154SKatayama Hirofumi MZ 
6183b079154SKatayama Hirofumi MZ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
6193b079154SKatayama Hirofumi MZ  *           DIALOG_PAGESETUP_Hook
6203b079154SKatayama Hirofumi MZ  */
DIALOG_PAGESETUP_Hook(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)6213b079154SKatayama Hirofumi MZ static UINT_PTR CALLBACK DIALOG_PAGESETUP_Hook(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
6223b079154SKatayama Hirofumi MZ {
6233b079154SKatayama Hirofumi MZ     switch (uMsg)
6243b079154SKatayama Hirofumi MZ     {
6253b079154SKatayama Hirofumi MZ         case WM_INITDIALOG:
6263b079154SKatayama Hirofumi MZ             /* fetch last user input prior to display dialog */
6273b079154SKatayama Hirofumi MZ             SetDlgItemText(hDlg, 0x141, Globals.szHeader);
6283b079154SKatayama Hirofumi MZ             SetDlgItemText(hDlg, 0x143, Globals.szFooter);
6293b079154SKatayama Hirofumi MZ             break;
6303b079154SKatayama Hirofumi MZ 
6313b079154SKatayama Hirofumi MZ         case WM_COMMAND:
6323b079154SKatayama Hirofumi MZ         {
6333b079154SKatayama Hirofumi MZ             if (HIWORD(wParam) == BN_CLICKED)
6343b079154SKatayama Hirofumi MZ             {
6353b079154SKatayama Hirofumi MZ                 switch (LOWORD(wParam))
6363b079154SKatayama Hirofumi MZ                 {
6373b079154SKatayama Hirofumi MZ                 case IDOK:
6383b079154SKatayama Hirofumi MZ                     /* save user input and close dialog */
639*d28e39e4SKatayama Hirofumi MZ                     GetDlgItemText(hDlg, 0x141, Globals.szHeader, _countof(Globals.szHeader));
640*d28e39e4SKatayama Hirofumi MZ                     GetDlgItemText(hDlg, 0x143, Globals.szFooter, _countof(Globals.szFooter));
6413b079154SKatayama Hirofumi MZ                     return FALSE;
6423b079154SKatayama Hirofumi MZ 
6433b079154SKatayama Hirofumi MZ                 case IDCANCEL:
6443b079154SKatayama Hirofumi MZ                     /* discard user input and close dialog */
6453b079154SKatayama Hirofumi MZ                     return FALSE;
6463b079154SKatayama Hirofumi MZ 
6473b079154SKatayama Hirofumi MZ                 case IDHELP:
6483b079154SKatayama Hirofumi MZ                     {
6493b079154SKatayama Hirofumi MZ                         /* FIXME: Bring this to work */
6503b079154SKatayama Hirofumi MZ                         static const TCHAR sorry[] = _T("Sorry, no help available");
6513b079154SKatayama Hirofumi MZ                         static const TCHAR help[] = _T("Help");
6523b079154SKatayama Hirofumi MZ                         MessageBox(Globals.hMainWnd, sorry, help, MB_ICONEXCLAMATION);
6533b079154SKatayama Hirofumi MZ                         return TRUE;
6543b079154SKatayama Hirofumi MZ                     }
6553b079154SKatayama Hirofumi MZ 
6563b079154SKatayama Hirofumi MZ                 default:
6573b079154SKatayama Hirofumi MZ                     break;
6583b079154SKatayama Hirofumi MZ                 }
6593b079154SKatayama Hirofumi MZ             }
6603b079154SKatayama Hirofumi MZ             break;
6613b079154SKatayama Hirofumi MZ         }
6623b079154SKatayama Hirofumi MZ     }
6633b079154SKatayama Hirofumi MZ 
6643b079154SKatayama Hirofumi MZ     return FALSE;
6653b079154SKatayama Hirofumi MZ }
6663b079154SKatayama Hirofumi MZ 
6673b079154SKatayama Hirofumi MZ /***********************************************************************
6683b079154SKatayama Hirofumi MZ  *           DIALOG_FilePageSetup
6693b079154SKatayama Hirofumi MZ  */
DIALOG_FilePageSetup(VOID)6703b079154SKatayama Hirofumi MZ VOID DIALOG_FilePageSetup(VOID)
6713b079154SKatayama Hirofumi MZ {
6723b079154SKatayama Hirofumi MZ     PAGESETUPDLG page;
6733b079154SKatayama Hirofumi MZ 
6743b079154SKatayama Hirofumi MZ     ZeroMemory(&page, sizeof(page));
6753b079154SKatayama Hirofumi MZ     page.lStructSize = sizeof(page);
6763b079154SKatayama Hirofumi MZ     page.hwndOwner = Globals.hMainWnd;
6773b079154SKatayama Hirofumi MZ     page.Flags = PSD_ENABLEPAGESETUPTEMPLATE | PSD_ENABLEPAGESETUPHOOK | PSD_MARGINS;
6783b079154SKatayama Hirofumi MZ     page.hInstance = Globals.hInstance;
6793b079154SKatayama Hirofumi MZ     page.rtMargin = Globals.lMargins;
6803b079154SKatayama Hirofumi MZ     page.hDevMode = Globals.hDevMode;
6813b079154SKatayama Hirofumi MZ     page.hDevNames = Globals.hDevNames;
6823b079154SKatayama Hirofumi MZ     page.lpPageSetupTemplateName = MAKEINTRESOURCE(DIALOG_PAGESETUP);
6833b079154SKatayama Hirofumi MZ     page.lpfnPageSetupHook = DIALOG_PAGESETUP_Hook;
6843b079154SKatayama Hirofumi MZ 
6853b079154SKatayama Hirofumi MZ     PageSetupDlg(&page);
6863b079154SKatayama Hirofumi MZ 
6873b079154SKatayama Hirofumi MZ     /* NOTE: Even if PageSetupDlg returns FALSE, the following members may have changed */
6883b079154SKatayama Hirofumi MZ     Globals.hDevMode = page.hDevMode;
6893b079154SKatayama Hirofumi MZ     Globals.hDevNames = page.hDevNames;
6903b079154SKatayama Hirofumi MZ     Globals.lMargins = page.rtMargin;
6913b079154SKatayama Hirofumi MZ }
692