📄 printer.cpp.svn-base
字号:
//this file is part of notepad++
//Copyright (C)2003 Don HO ( donho@altern.org )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "Printer.h"
void replaceStr(string & str, string str2BeReplaced, string replacement)
{
size_t pos = str.find(str2BeReplaced);
if (pos != str.npos)
str.replace(pos, str2BeReplaced.length(), replacement);
}
void Printer::init(HINSTANCE hInst, HWND hwnd, ScintillaEditView *pSEView, bool showDialog, int startPos, int endPos)
{
_pSEView = pSEView;
_startPos = startPos;
_endPos = endPos;
_pdlg.lStructSize = sizeof(PRINTDLG);
_pdlg.hwndOwner = hwnd;
_pdlg.hInstance = hInst;
_pdlg.Flags = PD_USEDEVMODECOPIES | PD_ALLPAGES | PD_RETURNDC;
_pdlg.nFromPage = 1;
_pdlg.nToPage = 1;
_pdlg.nMinPage = 1;
_pdlg.nMaxPage = 0xffffU; // We do not know how many pages in the
// document until the printer is selected and the paper size is known.
_pdlg.nCopies = 1;
_pdlg.hDC = 0;
_pdlg.hDevMode = NULL;
_pdlg.hDevNames = NULL;
_pdlg.lCustData = 0;
_pdlg.lpfnPrintHook = NULL;
_pdlg.lpfnSetupHook = NULL;
_pdlg.lpPrintTemplateName = NULL;
_pdlg.lpSetupTemplateName = NULL;
_pdlg.hPrintTemplate = NULL;
_pdlg.hSetupTemplate = NULL;
// See if a range has been selected
_pdlg.Flags |= (_startPos != _endPos)?PD_SELECTION:PD_NOSELECTION;
if (!showDialog)
{
// Don't display dialog box, just use the default printer and options
_pdlg.Flags |= PD_RETURNDEFAULT;
}
}
size_t Printer::doPrint(bool justDoIt)
{/*
if (!::PrintDlg(&_pdlg))
return 0;
*/
const NppGUI & nppGUI = (NppParameters::getInstance())->getNppGUI();
POINT ptPage;
POINT ptDpi;
RECT rectMargins;
RECT rectPhysMargins;
RECT userMargins;
// Get printer resolution
ptDpi.x = GetDeviceCaps(_pdlg.hDC, LOGPIXELSX); // dpi in X direction
ptDpi.y = GetDeviceCaps(_pdlg.hDC, LOGPIXELSY); // dpi in Y direction
// Start by getting the physical page size (in device units).
ptPage.x = GetDeviceCaps(_pdlg.hDC, PHYSICALWIDTH); // device units
ptPage.y = GetDeviceCaps(_pdlg.hDC, PHYSICALHEIGHT); // device units
// Get the dimensions of the unprintable
// part of the page (in device units).
rectPhysMargins.left = GetDeviceCaps(_pdlg.hDC, PHYSICALOFFSETX);
rectPhysMargins.top = GetDeviceCaps(_pdlg.hDC, PHYSICALOFFSETY);
// To get the right and lower unprintable area,
// we take the entire width and height of the paper and
// subtract everything else.
rectPhysMargins.right = ptPage.x // total paper width
- GetDeviceCaps(_pdlg.hDC, HORZRES) // printable width
- rectPhysMargins.left; // left unprintable margin
rectPhysMargins.bottom = ptPage.y // total paper height
- GetDeviceCaps(_pdlg.hDC, VERTRES) // printable height
- rectPhysMargins.top; // right unprintable margin
if (nppGUI._printSettings.isUserMargePresent())
{
userMargins.left = MulDiv(nppGUI._printSettings._marge.left*100, ptDpi.x, 2540);
userMargins.top = MulDiv(nppGUI._printSettings._marge.top*100, ptDpi.y, 2540);
userMargins.right = MulDiv(nppGUI._printSettings._marge.right*100, ptDpi.x, 2540);
userMargins.bottom = MulDiv(nppGUI._printSettings._marge.bottom*100, ptDpi.y, 2540);
rectMargins.left = max(rectPhysMargins.left, userMargins.left);
rectMargins.top = max(rectPhysMargins.top, userMargins.top);
rectMargins.right = max(rectPhysMargins.right, userMargins.right);
rectMargins.bottom = max(rectPhysMargins.bottom, userMargins.bottom);
}
else
{
rectMargins.left = rectPhysMargins.left;
rectMargins.top = rectPhysMargins.top;
rectMargins.right = rectPhysMargins.right;
rectMargins.bottom = rectPhysMargins.bottom;
}
// Convert device coordinates into logical coordinates
DPtoLP(_pdlg.hDC, (LPPOINT)&rectMargins, 2);
DPtoLP(_pdlg.hDC, (LPPOINT)&rectPhysMargins, 2);
// Convert page size to logical units and we're done!
DPtoLP(_pdlg.hDC, &ptPage, 1);
TEXTMETRIC tm;
int fontSize = nppGUI._printSettings._headerFontSize?nppGUI._printSettings._headerFontSize:9;
int fontWeight = nppGUI._printSettings._headerFontStyle & FONTSTYLE_BOLD?FW_BOLD:FW_NORMAL;
int isFontItalic = nppGUI._printSettings._headerFontStyle & FONTSTYLE_ITALIC?TRUE:FALSE;
const char *fontFace = (nppGUI._printSettings._headerFontName != "")?nppGUI._printSettings._headerFontName.c_str():"Arial";
int headerLineHeight = ::MulDiv(fontSize, ptDpi.y, 72);
//char toto[10];
//::MessageBox(NULL, itoa(nppGUI._printSettings._headerFontStyle, toto, 10), "header", MB_OK);
HFONT fontHeader = ::CreateFont(headerLineHeight,
0, 0, 0,
fontWeight,
isFontItalic,
FALSE,
0, 0, 0,
0, 0, 0,
fontFace);
::SelectObject(_pdlg.hDC, fontHeader);
::GetTextMetrics(_pdlg.hDC, &tm);
headerLineHeight = tm.tmHeight + tm.tmExternalLeading;
fontSize = nppGUI._printSettings._footerFontSize?nppGUI._printSettings._footerFontSize:9;
fontWeight = nppGUI._printSettings._footerFontStyle & FONTSTYLE_BOLD?FW_BOLD:FW_NORMAL;
isFontItalic = nppGUI._printSettings._footerFontStyle & FONTSTYLE_ITALIC?TRUE:FALSE;
fontFace = (nppGUI._printSettings._footerFontName != "")?nppGUI._printSettings._footerFontName.c_str():"Arial";
//::MessageBox(NULL, itoa(nppGUI._printSettings._footerFontStyle, , 10), "footer", MB_OK);
int footerLineHeight = ::MulDiv(fontSize, ptDpi.y, 72);
HFONT fontFooter = ::CreateFont(footerLineHeight,
0, 0, 0,
fontWeight,
isFontItalic,
FALSE,
0, 0, 0,
0, 0, 0,
fontFace);
::SelectObject(_pdlg.hDC, fontFooter);
::GetTextMetrics(_pdlg.hDC, &tm);
footerLineHeight = tm.tmHeight + tm.tmExternalLeading;
::GetTextMetrics(_pdlg.hDC, &tm);
int printMarge = tm.tmHeight + tm.tmExternalLeading;
printMarge = printMarge + printMarge / 2;
DOCINFO docInfo;
docInfo.cbSize = sizeof(DOCINFO);
docInfo.lpszDocName = _pSEView->getCurrentTitle();
docInfo.lpszOutput = NULL;
if (::StartDoc(_pdlg.hDC, &docInfo) < 0)
{
MessageBox(NULL, "Can not start printer document.", 0, MB_OK);
return 0;
}
// By default, we will print all the document
long lengthPrinted = 0;
long lengthDoc = _pSEView->getCurrentDocLen();
long lengthDocMax = lengthDoc;
// In the case that the print dialog was launched and that there's a range of selection
// We print the range of selection
if ((!(_pdlg.Flags & PD_RETURNDEFAULT)) && (_pdlg.Flags & PD_SELECTION))
{
if (_startPos > _endPos)
{
lengthPrinted = _endPos;
lengthDoc = _startPos;
}
else
{
lengthPrinted = _startPos;
lengthDoc = _endPos;
}
if (lengthPrinted < 0)
lengthPrinted = 0;
if (lengthDoc > lengthDocMax)
lengthDoc = lengthDocMax;
}
RangeToFormat frPrint;
frPrint.hdc = _pdlg.hDC;
frPrint.hdcTarget = _pdlg.hDC;
frPrint.rc.left = rectMargins.left - rectPhysMargins.left;
frPrint.rc.top = rectMargins.top - rectPhysMargins.top;
frPrint.rc.right = ptPage.x - rectMargins.right - rectPhysMargins.left;
frPrint.rc.bottom = ptPage.y - rectMargins.bottom - rectPhysMargins.top;
frPrint.rcPage.left = 0;
frPrint.rcPage.top = 0;
frPrint.rcPage.right = ptPage.x - rectPhysMargins.left - rectPhysMargins.right - 1;
frPrint.rcPage.bottom = ptPage.y - rectPhysMargins.top - rectPhysMargins.bottom - 1;
frPrint.rc.top += printMarge;
frPrint.rc.bottom -= printMarge;
frPrint.rc.left += printMarge;
frPrint.rc.right -= printMarge;
char headerL[256] = "";
char headerM[256] = "";
char headerR[256] = "";
char footerL[256] = "";
char footerM[256] = "";
char footerR[256] = "";
const char shortDateVar[] = "$(SHORT_DATE)";
const char longDateVar[] = "$(LONG_DATE)";
const char timeVar[] = "$(TIME)";
char shortDate[64];
char longDate[64];
char time[64];
SYSTEMTIME st;
::GetLocalTime(&st);
::GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, shortDate, sizeof(shortDate));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -