[NOTEPAD] Calculate the border around the printing area based on the margins the user has selected in the page setup dialog, instead of using a fixed size one. Brought to you by Ricardo Hanke with minor changes by me. CORE-10184

svn path=/trunk/; revision=69222
This commit is contained in:
Amine Khaldi 2015-09-14 09:32:57 +00:00
parent 06cad138ea
commit 0e960cab93
3 changed files with 51 additions and 31 deletions

View file

@ -225,6 +225,31 @@ int GetSelectionText(HWND hWnd, LPTSTR lpString, int nMaxCount)
} }
} }
static RECT
GetPrintingRect(HDC hdc, RECT margins)
{
int iLogPixelsX, iLogPixelsY;
int iHorzRes, iVertRes;
int iPhysPageX, iPhysPageY, iPhysPageW, iPhysPageH;
RECT rcPrintRect;
iPhysPageX = GetDeviceCaps(hdc, PHYSICALOFFSETX);
iPhysPageY = GetDeviceCaps(hdc, PHYSICALOFFSETY);
iPhysPageW = GetDeviceCaps(hdc, PHYSICALWIDTH);
iPhysPageH = GetDeviceCaps(hdc, PHYSICALHEIGHT);
iLogPixelsX = GetDeviceCaps(hdc, LOGPIXELSX);
iLogPixelsY = GetDeviceCaps(hdc, LOGPIXELSY);
iHorzRes = GetDeviceCaps(hdc, HORZRES);
iVertRes = GetDeviceCaps(hdc, VERTRES);
rcPrintRect.left = (margins.left * iLogPixelsX / 2540) - iPhysPageX;
rcPrintRect.top = (margins.top * iLogPixelsY / 2540) - iPhysPageY;
rcPrintRect.right = iHorzRes - (((margins.left * iLogPixelsX / 2540) - iPhysPageX) + ((margins.right * iLogPixelsX / 2540) - (iPhysPageW - iPhysPageX - iHorzRes)));
rcPrintRect.bottom = iVertRes - (((margins.top * iLogPixelsY / 2540) - iPhysPageY) + ((margins.bottom * iLogPixelsY / 2540) - (iPhysPageH - iPhysPageY - iVertRes)));
return rcPrintRect;
}
static BOOL DoSaveFile(VOID) static BOOL DoSaveFile(VOID)
{ {
BOOL bRet = TRUE; BOOL bRet = TRUE;
@ -518,7 +543,7 @@ VOID DIALOG_FilePrint(VOID)
TEXTMETRIC tm; TEXTMETRIC tm;
PRINTDLG printer; PRINTDLG printer;
SIZE szMetric; SIZE szMetric;
int cWidthPels, cHeightPels, border; int border;
int xLeft, yTop, pagecount, dopage, copycount; int xLeft, yTop, pagecount, dopage, copycount;
unsigned int i; unsigned int i;
LOGFONT hdrFont; LOGFONT hdrFont;
@ -526,6 +551,7 @@ VOID DIALOG_FilePrint(VOID)
DWORD size; DWORD size;
LPTSTR pTemp; LPTSTR pTemp;
static const TCHAR times_new_roman[] = _T("Times New Roman"); static const TCHAR times_new_roman[] = _T("Times New Roman");
RECT rcPrintRect;
/* Get a small font and print some header info on each page */ /* Get a small font and print some header info on each page */
ZeroMemory(&hdrFont, sizeof(hdrFont)); ZeroMemory(&hdrFont, sizeof(hdrFont));
@ -591,9 +617,6 @@ VOID DIALOG_FilePrint(VOID)
return; return;
} }
/* Get the page dimensions in pixels. */
cWidthPels = GetDeviceCaps(printer.hDC, HORZRES);
cHeightPels = GetDeviceCaps(printer.hDC, VERTRES);
/* Get the file text */ /* Get the file text */
if (printer.Flags & PD_SELECTION) if (printer.Flags & PD_SELECTION)
@ -623,13 +646,16 @@ VOID DIALOG_FilePrint(VOID)
size = GetWindowText(Globals.hEdit, pTemp, size); size = GetWindowText(Globals.hEdit, pTemp, size);
} }
/* Get the current printing area */
rcPrintRect = GetPrintingRect(printer.hDC, Globals.lMargins);
/* Ensure that each logical unit maps to one pixel */ /* Ensure that each logical unit maps to one pixel */
SetMapMode(printer.hDC, MM_TEXT); SetMapMode(printer.hDC, MM_TEXT);
/* Needed to get the correct height of a text line */ /* Needed to get the correct height of a text line */
GetTextMetrics(printer.hDC, &tm); GetTextMetrics(printer.hDC, &tm);
border = 150; border = 15;
for (copycount=1; copycount <= printer.nCopies; copycount++) { for (copycount=1; copycount <= printer.nCopies; copycount++) {
i = 0; i = 0;
pagecount = 1; pagecount = 1;
@ -667,8 +693,11 @@ VOID DIALOG_FilePrint(VOID)
AlertPrintError(); AlertPrintError();
return; return;
} }
SetViewportOrgEx(printer.hDC, rcPrintRect.left, rcPrintRect.top, NULL);
/* Write a rectangle and header at the top of each page */ /* Write a rectangle and header at the top of each page */
Rectangle(printer.hDC, border, border, cWidthPels-border, border + tm.tmHeight * 2); Rectangle(printer.hDC, border, border, rcPrintRect.right - border, border + tm.tmHeight * 2);
/* I don't know what's up with this TextOut command. This comes out /* I don't know what's up with this TextOut command. This comes out
kind of mangled. kind of mangled.
*/ */
@ -680,7 +709,7 @@ VOID DIALOG_FilePrint(VOID)
} }
/* The starting point for the main text */ /* The starting point for the main text */
xLeft = border * 2; xLeft = 0;
yTop = border + tm.tmHeight * 4; yTop = border + tm.tmHeight * 4;
SelectObject(printer.hDC, old_font); SelectObject(printer.hDC, old_font);
@ -689,7 +718,7 @@ VOID DIALOG_FilePrint(VOID)
* text one character at a time. */ * text one character at a time. */
do { do {
if (pTemp[i] == '\n') { if (pTemp[i] == '\n') {
xLeft = border * 2; xLeft = 0;
yTop += tm.tmHeight; yTop += tm.tmHeight;
} }
else if (pTemp[i] != '\r') { else if (pTemp[i] != '\r') {
@ -701,13 +730,13 @@ VOID DIALOG_FilePrint(VOID)
xLeft += szMetric.cx; xLeft += szMetric.cx;
/* Insert a line break if the current line does not fit into the printing area */ /* Insert a line break if the current line does not fit into the printing area */
if (xLeft > (cWidthPels - border * 2)) if (xLeft > rcPrintRect.right)
{ {
xLeft = border * 2; xLeft = 0;
yTop = yTop + tm.tmHeight; yTop = yTop + tm.tmHeight;
} }
} }
} while (i++ < size && yTop < (cHeightPels - border * 2)); } while (i++ < size && yTop < rcPrintRect.bottom);
if (dopage) if (dopage)
EndPage(printer.hDC); EndPage(printer.hDC);
@ -1184,10 +1213,7 @@ VOID DIALOG_FilePageSetup(void)
page.hwndOwner = Globals.hMainWnd; page.hwndOwner = Globals.hMainWnd;
page.Flags = PSD_ENABLEPAGESETUPTEMPLATE | PSD_ENABLEPAGESETUPHOOK | PSD_MARGINS; page.Flags = PSD_ENABLEPAGESETUPTEMPLATE | PSD_ENABLEPAGESETUPHOOK | PSD_MARGINS;
page.hInstance = Globals.hInstance; page.hInstance = Globals.hInstance;
page.rtMargin.left = Globals.lMarginLeft; page.rtMargin = Globals.lMargins;
page.rtMargin.top = Globals.lMarginTop;
page.rtMargin.right = Globals.lMarginRight;
page.rtMargin.bottom = Globals.lMarginBottom;
page.hDevMode = Globals.hDevMode; page.hDevMode = Globals.hDevMode;
page.hDevNames = Globals.hDevNames; page.hDevNames = Globals.hDevNames;
page.lpPageSetupTemplateName = MAKEINTRESOURCE(DIALOG_PAGESETUP); page.lpPageSetupTemplateName = MAKEINTRESOURCE(DIALOG_PAGESETUP);
@ -1197,10 +1223,7 @@ VOID DIALOG_FilePageSetup(void)
Globals.hDevMode = page.hDevMode; Globals.hDevMode = page.hDevMode;
Globals.hDevNames = page.hDevNames; Globals.hDevNames = page.hDevNames;
Globals.lMarginLeft = page.rtMargin.left; Globals.lMargins = page.rtMargin;
Globals.lMarginTop = page.rtMargin.top;
Globals.lMarginRight = page.rtMargin.right;
Globals.lMarginBottom = page.rtMargin.bottom;
} }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

View file

@ -73,10 +73,7 @@ typedef struct
TCHAR szFileName[MAX_PATH]; TCHAR szFileName[MAX_PATH];
TCHAR szFileTitle[MAX_PATH]; TCHAR szFileTitle[MAX_PATH];
TCHAR szFilter[2 * MAX_STRING_LEN + 100]; TCHAR szFilter[2 * MAX_STRING_LEN + 100];
LONG lMarginTop; RECT lMargins;
LONG lMarginBottom;
LONG lMarginLeft;
LONG lMarginRight;
TCHAR szHeader[MAX_PATH]; TCHAR szHeader[MAX_PATH];
TCHAR szFooter[MAX_PATH]; TCHAR szFooter[MAX_PATH];
TCHAR szStatusBarLineCol[MAX_PATH]; TCHAR szStatusBarLineCol[MAX_PATH];

View file

@ -144,10 +144,10 @@ void NOTEPAD_LoadSettingsFromRegistry(void)
QueryBool(hKey, _T("fStatusBar"), &Globals.bShowStatusBar); QueryBool(hKey, _T("fStatusBar"), &Globals.bShowStatusBar);
QueryString(hKey, _T("szHeader"), Globals.szHeader, ARRAY_SIZE(Globals.szHeader)); QueryString(hKey, _T("szHeader"), Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
QueryString(hKey, _T("szTrailer"), Globals.szFooter, ARRAY_SIZE(Globals.szFooter)); QueryString(hKey, _T("szTrailer"), Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
QueryDword(hKey, _T("iMarginLeft"), (DWORD*)&Globals.lMarginLeft); QueryDword(hKey, _T("iMarginLeft"), (DWORD*)&Globals.lMargins.left);
QueryDword(hKey, _T("iMarginTop"), (DWORD*)&Globals.lMarginTop); QueryDword(hKey, _T("iMarginTop"), (DWORD*)&Globals.lMargins.top);
QueryDword(hKey, _T("iMarginRight"), (DWORD*)&Globals.lMarginRight); QueryDword(hKey, _T("iMarginRight"), (DWORD*)&Globals.lMargins.right);
QueryDword(hKey, _T("iMarginBottom"), (DWORD*)&Globals.lMarginBottom); QueryDword(hKey, _T("iMarginBottom"), (DWORD*)&Globals.lMargins.bottom);
QueryDword(hKey, _T("iWindowPosX"), (DWORD*)&Globals.main_rect.left); QueryDword(hKey, _T("iWindowPosX"), (DWORD*)&Globals.main_rect.left);
QueryDword(hKey, _T("iWindowPosY"), (DWORD*)&Globals.main_rect.top); QueryDword(hKey, _T("iWindowPosY"), (DWORD*)&Globals.main_rect.top);
@ -239,10 +239,10 @@ void NOTEPAD_SaveSettingsToRegistry(void)
SaveDword(hKey, _T("fStatusBar"), Globals.bShowStatusBar ? 1 : 0); SaveDword(hKey, _T("fStatusBar"), Globals.bShowStatusBar ? 1 : 0);
SaveString(hKey, _T("szHeader"), Globals.szHeader); SaveString(hKey, _T("szHeader"), Globals.szHeader);
SaveString(hKey, _T("szTrailer"), Globals.szFooter); SaveString(hKey, _T("szTrailer"), Globals.szFooter);
SaveDword(hKey, _T("iMarginLeft"), Globals.lMarginLeft); SaveDword(hKey, _T("iMarginLeft"), Globals.lMargins.left);
SaveDword(hKey, _T("iMarginTop"), Globals.lMarginTop); SaveDword(hKey, _T("iMarginTop"), Globals.lMargins.top);
SaveDword(hKey, _T("iMarginRight"), Globals.lMarginRight); SaveDword(hKey, _T("iMarginRight"), Globals.lMargins.right);
SaveDword(hKey, _T("iMarginBottom"), Globals.lMarginBottom); SaveDword(hKey, _T("iMarginBottom"), Globals.lMargins.bottom);
SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left); SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left);
SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top); SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top);
SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left); SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left);