ciderpress/app/Print.cpp

738 lines
22 KiB
C++
Raw Normal View History

2007-03-27 17:47:10 +00:00
/*
* CiderPress
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
* See the file LICENSE for distribution terms.
*/
/*
* Support for printing.
*/
#include "stdafx.h"
#include "Print.h"
#include "Main.h"
#include "Preferences.h"
/*
* ==========================================================================
* PrintStuff
2007-03-27 17:47:10 +00:00
* ==========================================================================
*/
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
/*static*/ const WCHAR PrintStuff::kCourierNew[] = L"Courier New";
/*static*/ const WCHAR PrintStuff::kTimesNewRoman[] = L"Times New Roman";
2007-03-27 17:47:10 +00:00
void PrintStuff::InitBasics(CDC* pDC)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
ASSERT(pDC != NULL);
ASSERT(fpDC == NULL);
2007-03-27 17:47:10 +00:00
fpDC = pDC;
2007-03-27 17:47:10 +00:00
/* make sure we're in MM_TEXT mode */
pDC->SetMapMode(MM_TEXT);
2007-03-27 17:47:10 +00:00
/* get device capabilities; logPixels is e.g. 300 */
fVertRes = pDC->GetDeviceCaps(VERTRES);
fHorzRes = pDC->GetDeviceCaps(HORZRES);
fLogPixelsX = pDC->GetDeviceCaps(LOGPIXELSX);
fLogPixelsY = pDC->GetDeviceCaps(LOGPIXELSY);
LOGI("+++ logPixelsX=%d logPixelsY=%d fHorzRes=%d fVertRes=%d",
fLogPixelsX, fLogPixelsY, fHorzRes, fVertRes);
2007-03-27 17:47:10 +00:00
}
void PrintStuff::CreateFontByNumLines(CFont* pFont, int numLines)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
ASSERT(pFont != NULL);
ASSERT(numLines > 0);
2014-11-18 05:13:13 +00:00
ASSERT(fpDC != NULL);
/* required height */
int reqCharHeight;
reqCharHeight = (fVertRes + numLines/2) / numLines;
/* magic fudge factor */
int fudge = reqCharHeight / 24;
LOGI(" Reducing reqCharHeight from %d to %d",
reqCharHeight, reqCharHeight - fudge);
reqCharHeight -= fudge;
pFont->CreateFont(reqCharHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, kTimesNewRoman);
/*fpOldFont =*/ fpDC->SelectObject(pFont);
2007-03-27 17:47:10 +00:00
}
int PrintStuff::StringWidth(const CString& str)
2007-03-27 17:47:10 +00:00
{
CSize size;
size = fpDC->GetTextExtent(str);
return size.cx;
2007-03-27 17:47:10 +00:00
}
int PrintStuff::TrimString(CString* pStr, int width, bool addOnLeft)
2007-03-27 17:47:10 +00:00
{
static const char* kEllipsis = "...";
CString newStr;
int strWidth;
CSize size;
size = fpDC->GetTextExtent(kEllipsis);
if (width < size.cx) {
ASSERT(false);
return width;
}
newStr = *pStr;
/*
* Do a linear search. This would probably be better served with a
* binary search or at least a good textmetric-based guess.
*/
strWidth = StringWidth(newStr);
while (strWidth > width) {
if (pStr->IsEmpty()) {
ASSERT(false);
return width;
}
if (addOnLeft) {
*pStr = pStr->Right(pStr->GetLength() -1);
newStr = kEllipsis + *pStr;
} else {
*pStr = pStr->Left(pStr->GetLength() -1);
newStr = *pStr + kEllipsis;
}
if (!addOnLeft) {
LOGI("Now trying '%ls'", (LPCWSTR) newStr);
}
strWidth = StringWidth(newStr);
}
*pStr = newStr;
return strWidth;
2007-03-27 17:47:10 +00:00
}
/*
* ==========================================================================
* PrintContentList
2007-03-27 17:47:10 +00:00
* ==========================================================================
*/
void PrintContentList::Setup(CDC* pDC, CWnd* pParent)
2007-03-27 17:47:10 +00:00
{
/* init base class */
InitBasics(pDC);
2007-03-27 17:47:10 +00:00
/* init our stuff */
CreateFontByNumLines(&fPrintFont, kTargetLinesPerPage);
2007-03-27 17:47:10 +00:00
fpParentWnd = pParent;
2007-03-27 17:47:10 +00:00
/* compute text metrics */
TEXTMETRIC metrics;
pDC->GetTextMetrics(&metrics);
fCharHeight = metrics.tmHeight + metrics.tmExternalLeading;
fLinesPerPage = fVertRes / fCharHeight;
2007-03-27 17:47:10 +00:00
LOGD("fVertRes=%d, fCharHeight=%d", fVertRes, fCharHeight);
2007-03-27 17:47:10 +00:00
/* set up our slightly reduced lines per page */
ASSERT(fLinesPerPage > kHeaderLines+1);
fCLLinesPerPage = fLinesPerPage - kHeaderLines;
2007-03-27 17:47:10 +00:00
}
void PrintContentList::CalcNumPages(void)
2007-03-27 17:47:10 +00:00
{
/* set up our local goodies */
2014-11-18 05:13:13 +00:00
ASSERT(fpContentList != NULL);
int numLines = fpContentList->GetItemCount();
ASSERT(numLines > 0);
2007-03-27 17:47:10 +00:00
fNumPages = (numLines + fCLLinesPerPage -1) / fCLLinesPerPage;
ASSERT(fNumPages > 0);
2007-03-27 17:47:10 +00:00
LOGD("Using numLines=%d, fNumPages=%d, fCLLinesPerPage=%d",
numLines, fNumPages, fCLLinesPerPage);
2007-03-27 17:47:10 +00:00
}
int
PrintContentList::Print(const ContentList* pContentList)
{
fpContentList = pContentList;
CalcNumPages();
2007-03-27 17:47:10 +00:00
fFromPage = 1;
fToPage = fNumPages;
return StartPrint();
2007-03-27 17:47:10 +00:00
}
int PrintContentList::Print(const ContentList* pContentList, int fromPage,
int toPage)
2007-03-27 17:47:10 +00:00
{
fpContentList = pContentList;
CalcNumPages();
2007-03-27 17:47:10 +00:00
fFromPage = fromPage;
fToPage = toPage;
return StartPrint();
2007-03-27 17:47:10 +00:00
}
int PrintContentList::StartPrint(void)
2007-03-27 17:47:10 +00:00
{
MainWindow* pMain = (MainWindow*)::AfxGetMainWnd();
BOOL bres;
int jobID;
int result = -1;
ASSERT(fFromPage >= 1);
ASSERT(fToPage <= fNumPages);
// clear the abort flag
pMain->SetAbortPrinting(false);
// obstruct input to the main window
fpParentWnd->EnableWindow(FALSE);
// create a print-cancel dialog
// PrintCancelDialog* pPCD = new PrintCancelDialog;
CancelDialog* pPCD = new CancelDialog;
bres = pPCD->Create(&pMain->fAbortPrinting, IDD_PRINT_CANCEL, fpParentWnd);
if (bres == FALSE) {
LOGI("WARNING: PrintCancelDialog init failed");
} else {
fpDC->SetAbortProc(pMain->PrintAbortProc);
}
fDocTitle = pMain->GetPrintTitle();
// set up the print job
CString printTitle;
CheckedLoadString(&printTitle, IDS_PRINT_CL_JOB_TITLE);
DOCINFO di;
::ZeroMemory(&di, sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = printTitle;
jobID = fpDC->StartDoc(&di);
if (jobID <= 0) {
LOGI("Got invalid jobID from StartDoc");
goto bail;
}
LOGI("Got jobID=%d", jobID);
// do the printing
if (DoPrint() != 0) {
LOGI("Printing was aborted");
fpDC->AbortDoc();
} else {
LOGI("Printing was successful");
fpDC->EndDoc();
result = 0;
}
2007-03-27 17:47:10 +00:00
bail:
// destroy print-cancel dialog and restore main window
fpParentWnd->EnableWindow(TRUE);
//fpParentWnd->SetActiveWindow();
2014-11-18 05:13:13 +00:00
if (pPCD != NULL)
pPCD->DestroyWindow();
2007-03-27 17:47:10 +00:00
return result;
2007-03-27 17:47:10 +00:00
}
int PrintContentList::DoPrint(void)
2007-03-27 17:47:10 +00:00
{
LOGI("Printing from page=%d to page=%d", fFromPage, fToPage);
2007-03-27 17:47:10 +00:00
for (int page = fFromPage; page <= fToPage; page++) {
if (fpDC->StartPage() <= 0) {
LOGI("StartPage returned <= 0, returning -1");
return -1;
}
2007-03-27 17:47:10 +00:00
DoPrintPage(page);
2007-03-27 17:47:10 +00:00
// delay so we can test "cancel" button
// {
// MainWindow* pMain = (MainWindow*)::AfxGetMainWnd();
// pMain->EventPause(1000);
// }
2007-03-27 17:47:10 +00:00
if (fpDC->EndPage() <= 0) {
LOGI("EndPage returned <= 0, returning -1");
return -1;
}
}
2007-03-27 17:47:10 +00:00
return 0;
2007-03-27 17:47:10 +00:00
}
void PrintContentList::DoPrintPage(int page)
2007-03-27 17:47:10 +00:00
{
/*
* Column widths, on an arbitrary scale. These will be
* scaled appropriately for the page resolution.
*/
static const struct {
const char* name;
int width;
bool rightJust;
} kColumnWidths[kNumVisibleColumns] = {
{ "Pathname", 250, false }, // 200
{ "Type", 40, false }, // 44
{ "Auxtype", 47, false }, // 42
{ "Mod Date", 96, false }, // 99
{ "Format", 52, false }, // 54
{ "Size", 55, true }, // 60
{ "Ratio", 40, true }, // 41
{ "Packed", 55, true }, // 60
{ "Access", 39, false }, // 53 upper, 45 lower
};
const int kBorderWidth = 3;
/* normalize */
float widthMult;
int totalWidth;
totalWidth = 0;
for (int i = 0; i < NELEM(kColumnWidths); i++)
totalWidth += kColumnWidths[i].width;
widthMult = (float) fHorzRes / totalWidth;
LOGD("totalWidth=%d, fHorzRes=%d, mult=%.3f",
totalWidth, fHorzRes, widthMult);
/*
* Calculate some goodies.
*/
int start, end;
start = (page-1) * fCLLinesPerPage;
end = start + fCLLinesPerPage;
if (end >= fpContentList->GetItemCount())
end = fpContentList->GetItemCount()-1;
int offset, nextOffset, cellWidth, border;
border = (int) (kBorderWidth * widthMult);
/*
* Print page header.
*/
fpDC->TextOut(0, 1 * fCharHeight, fDocTitle);
CString pageNum;
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
pageNum.Format(L"Page %d/%d", page, fNumPages);
int pageNumWidth = StringWidth(pageNum);
fpDC->TextOut(fHorzRes - pageNumWidth, 1 * fCharHeight, pageNum);
/*
* Print data.
*/
for (int row = -1; row < fCLLinesPerPage && start + row <= end; row++) {
CString text;
offset = 0;
for (int col = 0; col < kNumVisibleColumns; col++) {
cellWidth = (int) ((float)kColumnWidths[col].width * widthMult);
nextOffset = offset + cellWidth;
if (col != kNumVisibleColumns-1)
cellWidth -= border;
if (col == 0)
cellWidth -= (border*2); // extra border on pathname
int yOffset;
if (row == -1) {
text = kColumnWidths[col].name;
yOffset = (row+kHeaderLines-1) * fCharHeight;
} else {
text = fpContentList->GetItemText(start + row, col);
yOffset = (row+kHeaderLines) * fCharHeight;
}
int strWidth;
strWidth = TrimString(&text, cellWidth, col == 0);
if (kColumnWidths[col].rightJust)
fpDC->TextOut((offset + cellWidth) - strWidth, yOffset, text);
else
fpDC->TextOut(offset, yOffset, text);
offset = nextOffset;
}
}
/*
* Add some fancy lines.
*/
CPen penBlack(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = fpDC->SelectObject(&penBlack);
fpDC->MoveTo(0, (int) (fCharHeight * (kHeaderLines - 0.5)));
fpDC->LineTo(fHorzRes, (int) (fCharHeight * (kHeaderLines - 0.5)));
//fpDC->MoveTo(0, 0);
//fpDC->LineTo(fHorzRes, fVertRes);
//fpDC->MoveTo(fHorzRes-1, 0);
//fpDC->LineTo(0, fVertRes);
fpDC->SelectObject(pOldPen);
2007-03-27 17:47:10 +00:00
}
/*
* ==========================================================================
* PrintRichEdit
2007-03-27 17:47:10 +00:00
* ==========================================================================
*/
void PrintRichEdit::Setup(CDC* pDC, CWnd* pParent)
2007-03-27 17:47:10 +00:00
{
/* preflighting can cause this to be initialized twice */
2014-11-18 05:13:13 +00:00
fpDC = NULL;
/* init base class */
InitBasics(pDC);
if (!fInitialized) {
/*
* Find a nice font for the title area.
*/
const int kPointSize = 10;
int fontHeight;
BOOL result;
fontHeight = -MulDiv(kPointSize, fLogPixelsY, 72);
result = fTitleFont.CreateFont(fontHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, kTimesNewRoman);
ASSERT(result); // everybody has Times New Roman
}
fpParentWnd = pParent;
fInitialized = true;
2007-03-27 17:47:10 +00:00
}
int PrintRichEdit::PrintPreflight(CRichEditCtrl* pREC, int* pNumPages)
2007-03-27 17:47:10 +00:00
{
fStartChar = 0;
fEndChar = -1;
fStartPage = 0;
fEndPage = -1;
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
return StartPrint(pREC, L"(test)", pNumPages, false);
2007-03-27 17:47:10 +00:00
}
int PrintRichEdit::PrintAll(CRichEditCtrl* pREC, const WCHAR* title)
2007-03-27 17:47:10 +00:00
{
fStartChar = 0;
fEndChar = -1;
fStartPage = 0;
fEndPage = -1;
2014-11-18 05:13:13 +00:00
return StartPrint(pREC, title, NULL, true);
2007-03-27 17:47:10 +00:00
}
int PrintRichEdit::PrintPages(CRichEditCtrl* pREC, const WCHAR* title,
int startPage, int endPage)
2007-03-27 17:47:10 +00:00
{
fStartChar = 0;
fEndChar = -1;
fStartPage = startPage;
fEndPage = endPage;
2014-11-18 05:13:13 +00:00
return StartPrint(pREC, title, NULL, true);
2007-03-27 17:47:10 +00:00
}
int PrintRichEdit::PrintSelection(CRichEditCtrl* pREC, const WCHAR* title,
long startChar, long endChar)
2007-03-27 17:47:10 +00:00
{
fStartChar = startChar;
fEndChar = endChar;
fStartPage = 0;
fEndPage = -1;
2014-11-18 05:13:13 +00:00
return StartPrint(pREC, title, NULL, true);
2007-03-27 17:47:10 +00:00
}
int PrintRichEdit::StartPrint(CRichEditCtrl* pREC, const WCHAR* title,
int* pNumPages, bool doPrint)
2007-03-27 17:47:10 +00:00
{
2014-11-18 05:13:13 +00:00
CancelDialog* pPCD = NULL;
MainWindow* pMain = (MainWindow*)::AfxGetMainWnd();
int result;
2007-03-27 17:47:10 +00:00
/* set up the print cancel dialog */
if (doPrint) {
BOOL bres;
2007-03-27 17:47:10 +00:00
/* disable main UI */
fpParentWnd->EnableWindow(FALSE);
2007-03-27 17:47:10 +00:00
pPCD = new CancelDialog;
bres = pPCD->Create(&pMain->fAbortPrinting, IDD_PRINT_CANCEL,
fpParentWnd);
2007-03-27 17:47:10 +00:00
/* set up the DC's print abort callback */
if (bres != FALSE)
fpDC->SetAbortProc(pMain->PrintAbortProc);
}
2007-03-27 17:47:10 +00:00
result = DoPrint(pREC, title, pNumPages, doPrint);
2007-03-27 17:47:10 +00:00
if (doPrint) {
fpParentWnd->EnableWindow(TRUE);
2014-11-18 05:13:13 +00:00
if (pPCD != NULL)
pPCD->DestroyWindow();
}
2007-03-27 17:47:10 +00:00
return result;
2007-03-27 17:47:10 +00:00
}
void PrintRichEdit::PrintPrep(FORMATRANGE* pFR)
2007-03-27 17:47:10 +00:00
{
CFont* pOldFont;
/* make sure we're in MM_TEXT mode */
fpDC->SetMapMode(MM_TEXT);
TEXTMETRIC metrics;
pOldFont = fpDC->SelectObject(&fTitleFont);
fpDC->GetTextMetrics(&metrics);
fCharHeight = metrics.tmHeight + metrics.tmExternalLeading;
fpDC->SelectObject(pOldFont);
//LOGI("CHAR HEIGHT is %d", fCharHeight);
/* compute fLeftMargin and fRightMargin */
ComputeMargins();
/*
* Set up the FORMATRANGE values. The Rich Edit stuff likes to have
* measurements in TWIPS, whereas the printer is using DC pixel
* values. fLogPixels_ tells us how many pixels per inch.
*/
memset(pFR, 0, sizeof(FORMATRANGE));
pFR->hdc = pFR->hdcTarget = fpDC->m_hDC;
/*
* Set frame for printable area, in TWIPS. The printer DC will set
* its own "reasonable" margins, so the area here is not the entire
* sheet of paper.
*/
pFR->rcPage.left = pFR->rcPage.top = 0;
pFR->rcPage.right = (fHorzRes * kTwipsPerInch) / fLogPixelsX;
pFR->rcPage.bottom = (fVertRes * kTwipsPerInch) / fLogPixelsY;
long topOffset = (long) ((fCharHeight * 1.5 * kTwipsPerInch) / fLogPixelsY);
pFR->rc.top = pFR->rcPage.top + topOffset;
pFR->rc.bottom = pFR->rcPage.bottom;
pFR->rc.left = pFR->rcPage.left + (fLeftMargin * kTwipsPerInch) / fLogPixelsX;
pFR->rc.right = pFR->rcPage.right - (fRightMargin * kTwipsPerInch) / fLogPixelsX;
LOGI("PRINTABLE AREA is %d wide x %d high (twips)",
pFR->rc.right - pFR->rc.left, pFR->rc.bottom - pFR->rc.top);
LOGI("FRAME is %d wide x %d high (twips)",
pFR->rcPage.right - pFR->rcPage.left, pFR->rcPage.bottom - pFR->rcPage.top);
pFR->chrg.cpMin = fStartChar;
pFR->chrg.cpMax = fEndChar;
2007-03-27 17:47:10 +00:00
}
void PrintRichEdit::ComputeMargins(void)
2007-03-27 17:47:10 +00:00
{
CFont tmpFont;
CFont* pOldFont;
int char80width, fontHeight, totalMargin;
BOOL result;
fontHeight = -MulDiv(10, fLogPixelsY, 72);
result = tmpFont.CreateFont(fontHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, kCourierNew);
ASSERT(result);
pOldFont = fpDC->SelectObject(&tmpFont);
// in theory we could compute one 'X' * 80; this seems more reliable
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
WCHAR str[81];
for (int i = 0; i < 80; i++)
str[i] = 'X';
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
str[80] = '\0';
char80width = StringWidth(str);
fpDC->SelectObject(pOldFont);
//LOGI("char80 string width=%d", char80width);
/*
* If there's not enough room on the page, set the margins to zero.
* If the margins required exceed two inches, just set the margin
* to one inch on either side.
*/
totalMargin = fHorzRes - char80width;
if (totalMargin < 0) {
LOGI(" Page not wide enough, setting margins to zero");
fLeftMargin = fRightMargin = 0;
} else if (totalMargin > fLogPixelsX * 2) {
LOGI(" Page too wide, setting margins to 1 inch");
fLeftMargin = fRightMargin = fLogPixelsX;
} else {
// try to get leftMargin equal to 1/2"
fLeftMargin = totalMargin / 2;
if (fLeftMargin > fLogPixelsX / 2)
fLeftMargin = fLogPixelsX / 2;
fRightMargin = totalMargin - fLeftMargin -1;
LOGI(" +++ Margins (in %d pixels/inch) are left=%ld right=%ld",
fLogPixelsX, fLeftMargin, fRightMargin);
}
2007-03-27 17:47:10 +00:00
}
// This was derived from Microsoft KB article 129860.
int PrintRichEdit::DoPrint(CRichEditCtrl* pREC, const WCHAR* title,
int* pNumPages, bool doPrint)
2007-03-27 17:47:10 +00:00
{
FORMATRANGE fr;
DOCINFO di;
long textLength, textPrinted, lastTextPrinted;
int pageNum;
LOGI("DoPrint: title='%ls' doPrint=%d", title, doPrint);
LOGI(" startChar=%d endChar=%d startPage=%d endPage=%d",
fStartChar, fEndChar, fStartPage, fEndPage);
/*
* Get the title font and fill out the FORMATRANGE structure.
*/
PrintPrep(&fr);
/* fill out a DOCINFO */
memset(&di, 0, sizeof(di));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = title;
if (doPrint)
fpDC->StartDoc(&di);
/*
* Here's the really strange part of the affair. The GetTextLength call
* shown in the MSFT KB article doesn't return the correct number of
* characters. The CRichEditView code in MFC uses a GetTextLengthEx
* call, which is documented as being part of the CRichEditCtrl but
* doesn't appear in the header files. Call it the hard way.
*
* If you print a "raw" file with carriage returns, and you use version
* 5.30.23.1200 of "riched20.dll", you get "9609" from GetTextLength and
* "9528" from GetTextLengthEx when the document's length is 9528 and
* there are 81 carriage returns. The value you want to use is 9528,
* because the print code doesn't double-up the count on CRs.
*
* If instead you use version 5.30.23.1215, you get the same answer
* from both calls, and printing works fine.
*
* GetTextLengthEx is part of "riched20.dll". Win9x uses "riched32.dll",
* which doesn't support the call.
*/
GETTEXTLENGTHEX exLenReq;
long basicTextLength, extdTextLength;
basicTextLength = pREC->GetTextLength();
exLenReq.flags = GTL_PRECISE | GTL_NUMCHARS;
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
#ifdef _UNICODE
exLenReq.codepage = 1200; // UTF-16 little-endian
#else
exLenReq.codepage = CP_ACP;
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
#endif
extdTextLength = (long)::SendMessage(pREC->m_hWnd, EM_GETTEXTLENGTHEX,
(WPARAM) &exLenReq, (LPARAM) NULL);
LOGD("RichEdit text length: std=%ld extd=%ld",
basicTextLength, extdTextLength);
if (fEndChar == -1) {
if (extdTextLength > 0)
textLength = extdTextLength;
else
textLength = basicTextLength;
} else
textLength = fEndChar - fStartChar;
LOGD(" +++ starting while loop, textLength=%ld", textLength);
pageNum = 0;
lastTextPrinted = -1;
do {
bool skipPage = false;
pageNum++;
LOGD(" +++ while loop: pageNum is %d", pageNum);
if (fEndPage > 0) {
if (pageNum < fStartPage)
skipPage = true;
if (pageNum > fEndPage)
break; // out of while, ending print
}
if (doPrint && !skipPage) {
fpDC->StartPage();
CFont* pOldFont = fpDC->SelectObject(&fTitleFont);
fpDC->TextOut(0, 0 * fCharHeight, title);
CString pageNumStr;
Large set of changes to restore CiderPress build. CiderPress and MDC now compile, and execute far enough to open their respective "about" boxes, but I doubt they'll do much more than that. * Switch from MBCS to UNICODE APIs Microsoft switched to UTF-16 (by way of UCS-2) a long time ago, and the support for MBCS seems to be getting phased out. So it's time to switch to wide strings. This is a bit awkward for CiderPress because it works with disk and file archives with 8-bit filenames, and I want NufxLib and DiskImgLib to continue to work on Linux (which has largely taken the UTF-8 approach to Unicode). The libraries will continue to work with 8-bit filenames, with CiderPress/MDC doing the conversion at the appropriate point. There were a couple of places where strings from a structure handed back by one of the libraries were used directly in the UI, or vice-versa, which is a problem because we have nowhere to store the result of the conversion. These currently have fixed place-holder "xyzzy" strings. All UI strings are now wide. Various format strings now use "%ls" and "%hs" to explicitly specify wide and narrow. This doesn't play well with gcc, so only the Windows-specific parts use those. * Various updates to vcxproj files The project-file conversion had some cruft that is now largely gone. The build now has a common output directory for the EXEs and libraries, avoiding the old post-build copy steps. * Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots The old "prebuilts" directory is now gone. The libraries are now built as part of building the apps. I added a minimal set of files for zlib, and a full set for nufxlib. The Linux-specific nufxlib goodies are included for the benefit of the Linux utilities, which are currently broken (don't build). * Replace symbols used for include guards Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
pageNumStr.Format(L"Page %d", pageNum);
int pageNumWidth = StringWidth(pageNumStr);
fpDC->TextOut(fHorzRes - pageNumWidth, 0 * fCharHeight, pageNumStr);
fpDC->SelectObject(pOldFont);
CPen penBlack(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = fpDC->SelectObject(&penBlack);
int ycoord = (int) (fCharHeight * 1.25);
fpDC->MoveTo(0, ycoord-1);
fpDC->LineTo(fHorzRes, ycoord-1);
fpDC->MoveTo(0, ycoord);
fpDC->LineTo(fHorzRes, ycoord);
fpDC->SelectObject(pOldPen);
}
//LOGI(" +++ calling FormatRange(%d)", doPrint && !skipPage);
//LogHexDump(&fr, sizeof(fr));
/* print a page full of RichEdit stuff */
textPrinted = pREC->FormatRange(&fr, doPrint && !skipPage);
LOGD(" +++ returned from FormatRange (textPrinted=%d)",
textPrinted);
if (textPrinted <= lastTextPrinted) {
/* the earlier StartPage can't be undone, so we'll get an
extra blank page at the very end */
LOGW("GLITCH: no new text printed (printed=%ld, last=%ld, len=%ld)",
textPrinted, lastTextPrinted, textLength);
pageNum--; // fix page count estimator
break;
}
lastTextPrinted = textPrinted;
// delay so we can test "cancel" button
//((MainWindow*)::AfxGetMainWnd())->EventPause(1000);
if (doPrint && !skipPage) {
if (fpDC->EndPage() <= 0) {
/* the "cancel" button was hit */
LOGI("EndPage returned <= 0 (cancelled)");
fpDC->AbortDoc();
return -1;
}
}
if (textPrinted < textLength) {
fr.chrg.cpMin = textPrinted;
fr.chrg.cpMax = fEndChar; // -1 if nothing selected
}
} while (textPrinted < textLength);
LOGV(" +++ calling FormatRange(NULL, FALSE)");
2014-11-18 05:13:13 +00:00
pREC->FormatRange(NULL, FALSE);
LOGV(" +++ returned from final FormatRange");
if (doPrint)
fpDC->EndDoc();
2014-11-18 05:13:13 +00:00
if (pNumPages != NULL)
*pNumPages = pageNum;
LOGI("Printing completed (textPrinted=%ld)", textPrinted);
return 0;
2007-03-27 17:47:10 +00:00
}