home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
NOTEPAD2.ZIP
/
NPPRINT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-08
|
12KB
|
349 lines
/***************************************************************************\
* npprint.c -- This file contains the code necessary to print the text.
*
* Created by Microsoft Corporation, 1989
\***************************************************************************/
#define INCL_WIN
#define INCL_GPI
#define INCL_DEV
#include <os2.h>
#include <stdio.h>
#include <string.h>
#include <opendlg.h>
#include "notepad.h"
#include "npcmd.h"
/******************** Globals to module ********************/
static SHORT sXPrinterRes;
static SHORT sYPrinterRes;
static LONG lPrinterHeight;
static LONG lPrinterWidth;
static LONG lMaxBaselineExt;
static VOID PrintEndPage(HPS hps, HDC hdc);
static VOID PrintTextLine(HPS, IPT, IPT);
/***************************************************************************\
* BOOL SetPrinterFont(HPS hps, HDC hdc)
*
* Sets the printer font to match the font currently used for screen display.
* If no matching font can be found, then the user is notified and the
* function returns FALSE, effectively terminating printing.
\***************************************************************************/
static BOOL SetPrinterFont(HPS hps, HDC hdc)
{
FONTMETRICS fmDefault;
PFONTMETRICS pfm;
LONG cFonts, cRemFonts, i;
SHORT cBytes;
SHORT sReturn;
FATTRS fattrs;
BOOL brc;
LONG ldevcaps[2];
/* determine the font resolution of the printer DC */
brc = DevQueryCaps(hdc, CAPS_HORIZONTAL_RESOLUTION, 2L, ldevcaps);
GpiQueryFontMetrics(hps, (LONG) sizeof(fmDefault),
(PFONTMETRICS)&fmDefault);
cFonts = 0;
cRemFonts = GpiQueryFonts(hps, QF_PUBLIC | QF_PRIVATE,
CurrFaceName(),
(PLONG)&cFonts,
(LONG) 0,
NULL);
if (cRemFonts == 0) {
WinMessageBox(HWND_DESKTOP,
hwndNP,
"Current font doesn't exist for printer",
NULL,
0,
MB_CANCEL | MB_ICONEXCLAMATION | MB_APPLMODAL);
return(FALSE);
}
cBytes = (SHORT) (cRemFonts * sizeof(FONTMETRICS));
pfm = (PFONTMETRICS)WinAllocMem(hhAppHeap, cBytes);
cFonts = cRemFonts;
cRemFonts = GpiQueryFonts(hps, QF_PUBLIC | QF_PRIVATE,
CurrFaceName(),
(PLONG)&cFonts,
(LONG)sizeof(FONTMETRICS),
pfm);
for (i=0; i<cFonts; i++) {
if (((pfm+i)->sNominalPointSize == CurrFaceSize()) &&
((pfm+i)->sXDeviceRes == sXPrinterRes) &&
((pfm+i)->sYDeviceRes == sYPrinterRes))
break;
}
if (i >= cFonts) {
WinFreeMem(hhAppHeap, (NPBYTE) OFFSETOF(pfm), (USHORT) cBytes);
sReturn = WinMessageBox(HWND_DESKTOP,
hwndNP,
"Current font doesn't exist for printer",
NULL,
0,
MB_OKCANCEL | MB_ICONEXCLAMATION | MB_APPLMODAL);
if (sReturn == MBID_CANCEL) {
return(FALSE);
}
} else {
fattrs.usRecordLength = sizeof(fattrs);
fattrs.fsSelection = 0;
fattrs.lMatch = (pfm+i)->lMatch;
lstrcpy(fattrs.szFacename,(pfm+i)->szFacename);
fattrs.idRegistry = 0;
fattrs.usCodePage = 0;
fattrs.lMaxBaselineExt = 0;
fattrs.lAveCharWidth = 0;
fattrs.fsType = 0;
fattrs.fsFontUse = 0;
WinFreeMem(hhAppHeap, (NPBYTE) OFFSETOF(pfm), (USHORT) cBytes);
if (!GpiCreateLogFont(hps,(PSTR8)"DUMMY",(LONG) 3, &fattrs)) {
WinMessageBox(HWND_DESKTOP,
hwndNP,
"Error in GpiCreateLogFont; aborting",
NULL,
0,
MB_CANCEL | MB_ICONEXCLAMATION | MB_APPLMODAL);
return(FALSE);
}
if (!GpiSetCharSet(hps, (LONG) 3)) {
WinMessageBox(HWND_DESKTOP,
hwndNP,
"Error in GpiSetCharSet; aborting",
NULL,
0,
MB_CANCEL | MB_ICONEXCLAMATION | MB_APPLMODAL);
return(FALSE);
}
}
GpiQueryFontMetrics(hps, (LONG) sizeof(fmDefault),
(PFONTMETRICS)&fmDefault);
lMaxBaselineExt = fmDefault.lMaxBaselineExt;
return(TRUE);
}
/***************************************************************************\
* VOID PrintTextLine(HPS hps, IPT iptStart, IPT iptEnd)
*
* Prints the text to the PS provided. Returns FALSE if there is any problem;
* TRUE otherwise.
\***************************************************************************/
static VOID PrintTextLine(HPS hps, IPT iptStart, IPT iptEnd)
{
LONG cch, chlen;
PCHAR pch;
cch = iptEnd - iptStart;
while (cch > 0) {
chlen = cch;
pch = PVOIDFROMMR(WinSendMsg(hwndNPEdit, EM_GETTEXT, (MPARAM)iptStart, MPFROMP(&chlen)));
chlen=(chlen>cch)?cch:chlen;
GpiCharString(hps, chlen, pch);
cch -= chlen;
iptStart += chlen;
}
}
/***************************************************************************\
* BOOL PrintSetDevParms(DEVOPENSTRUC *dvo, DRIVDATA *drv,
* LONG *lDevMode, LONG *lDevOpCount)
*
* Creates a PS and hDC for printing, and sets up a few useful globals.
\***************************************************************************/
static BOOL PrintSetDevParms(DEVOPENSTRUC *dvo, DRIVDATA *drv,
LONG *lDevMode, LONG *lDevOpCount)
{
BOOL fSpoolerOn;
CHAR szPrinterName[NP_PROFILESIZE];
CHAR szPrinterConn[NP_PROFILESIZE];
CHAR szSpoolerOn[NP_PROFILESIZE];
PSZ pszPortField, pszDriverField, pszQueueField, pszEnd;
drv->cb = 4;
drv->lVersion = 0L;
lstrcpy(drv->szDeviceName,"");
drv->abGeneralData[0]='?';
WinQueryProfileString(hab, "PM_SPOOLER", "SPOOL", "0;", szSpoolerOn, NP_PROFILESIZE);
fSpoolerOn = (0 == strcmp("1;",szSpoolerOn));
if (!WinQueryProfileString(hab, "PM_SPOOLER", "PRINTER", "", szPrinterName, NP_PROFILESIZE)) {
return(FALSE);
}
*pszEnd = '\0';
if (!WinQueryProfileString(hab, "PM_SPOOLER_PRINTER", szPrinterName, "", szPrinterConn, NP_PROFILESIZE)) {
return(FALSE);
}
pszPortField = szPrinterConn;
*(pszDriverField++) = '\0';
*(pszQueueField++) = '\0';
*pszEnd = '\0';
if (fSpoolerOn) {
*lDevMode = OD_QUEUED;
dvo->pszLogAddress = pszQueueField;
} else {
*lDevMode = OD_DIRECT;
dvo->pszLogAddress = pszPortField;
}
dvo->pszDriverName = pszDriverField;
/* dvo->pdriv = drv; */
dvo->pdriv = NULL;
dvo->pszDataType = "PM_Q_RAW";
dvo->pszComment = "";
dvo->pszQueueProcName = NULL;
dvo->pszQueueProcParams = NULL;
dvo->pszSpoolerParams = NULL;
dvo->pszNetworkParams = NULL;
*lDevOpCount = 5L;
return(TRUE);
}
/***************************************************************************\
* BOOL PrintStartPage(HPS *hps, HDC *hdc)
*
* Creates a PS and hDC for printing, and sets up a few useful globals.
\***************************************************************************/
static BOOL PrintStartPage(HPS *hps, HDC *hdc)
{
DEVOPENSTRUC devop;
DRIVDATA drivdata;
LONG ldevcaps[2];
SIZEL sizel;
BOOL brc;
LONG lDevMode, lDevOpCount;
/* open the device context */
if (!PrintSetDevParms(&devop, &drivdata, &lDevMode, &lDevOpCount))
return(FALSE);
*hdc = DevOpenDC(hab,
lDevMode,
(PSZ)"*",
lDevOpCount,
(PDEVOPENDATA)&devop,
(HDC) NULL);
if (*hdc == NULL) {
return(FALSE);
}
/* determine the height of the printer display area */
brc = DevQueryCaps(*hdc, CAPS_WIDTH, 2L, ldevcaps);
sizel.cx = 0L;
sizel.cy = 0L;
lPrinterHeight = ldevcaps[1];
lPrinterWidth = ldevcaps[0];
/* create the PS */
*hps = GpiCreatePS(hab,
*hdc,
(PSIZEL)&sizel,
(ULONG)(PU_PELS | GPIA_ASSOC | GPIT_NORMAL));
if (*hps == NULL) {
DevCloseDC(*hdc);
return(FALSE);
}
DevEscape(*hdc, DEVESC_STARTDOC, (LONG)lstrlen(szFileName),
szFileName, NULL, NULL);
GpiSetBackMix(*hps, BM_LEAVEALONE);
if (!SetPrinterFont(*hps, *hdc)) {
PrintEndPage(*hps,*hdc);
return(FALSE);
}
return(TRUE);
}
/***************************************************************************\
* VOID PrintEndPage(HPS hps, HDC hdc)
*
* Given a ps/dc pair, closes them.
\***************************************************************************/
static VOID PrintEndPage(HPS hps, HDC hdc)
{
DevEscape(hdc, DEVESC_ENDDOC, 0L, NULL, 0L, NULL);
GpiAssociate(hps, NULL);
GpiDestroyPS(hps);
DevCloseDC(hdc);
}
/***************************************************************************\
* BOOL NPPrint()
*
* This function does everything that is necessary to print. The essence
* is that in order to print the following steps are required.
*
* Returns:
* TRUE if printing completed successfully (i.e. no bad return codes)
* FALSE if printing failed.
\***************************************************************************/
BOOL NPPrint()
{
BOOL fToReturn;
LONG lTextLength;
IPT ipt, iptEnd;
LONG lLine;
HPS hps;
HDC hdc;
POINTL ptl;
HPOINTER hptrSave;
lTextLength = LONGFROMMR(WinSendMsg(hwndNPEdit, EM_QTL, NULL, NULL));
lLine = 0;
if (!PrintStartPage(&hps, &hdc)) {
return(FALSE);
}
hptrSave = WinQueryPointer (HWND_DESKTOP);
WinSetPointer(HWND_DESKTOP, WinQuerySysPointer (HWND_DESKTOP, SPTR_WAIT, FALSE));
ptl.x = 0;
ptl.y = lPrinterHeight;
iptEnd = LONGFROMMR(WinSendMsg(hwndNPEdit, EM_CHARFROMLINE, MPFROMLONG(lLine),NULL));
do {
ipt = iptEnd;
iptEnd = LONGFROMMR(WinSendMsg(hwndNPEdit, EM_CHARFROMLINE, MPFROMLONG(lLine+1),NULL));
ptl.y -= lMaxBaselineExt;
if (ptl.y <= 0) {
DevEscape( hdc, DEVESC_NEWFRAME, 0L, (PBYTE)NULL, 0L, (PBYTE)NULL);
ptl.y = lPrinterHeight - lMaxBaselineExt;
}
GpiMove(hps,(PPOINTL)&ptl);
PrintTextLine(hps,ipt,((ipt != iptEnd)?(iptEnd-1):lTextLength));
lLine++;
} while (ipt != iptEnd);
PrintEndPage(hps, hdc);
WinSetPointer (HWND_DESKTOP, hptrSave);
return (fToReturn);
}