Annotation of mstools/mfc/samples/multipad/mpprint.cpp, revision 1.1.1.2

1.1       root        1: // mpprint.cpp : Defines the printing logic.
                      2: //
                      3: // This is a part of the Microsoft Foundation Classes C++ library.
                      4: // Copyright (C) 1992 Microsoft Corporation
                      5: // All rights reserved.
                      6: //
                      7: // This source code is only intended as a supplement to the
                      8: // Microsoft Foundation Classes Reference and Microsoft
                      9: // QuickHelp documentation provided with the library.
                     10: // See these sources for detailed information regarding the
                     11: // Microsoft Foundation Classes product.
                     12: 
                     13: #include "multipad.h"
                     14: 
                     15: #include <commdlg.h>
                     16: 
1.1.1.2 ! root       17: #ifndef _NTWIN
1.1       root       18: #pragma code_seg("_MPPRINT")
1.1.1.2 ! root       19: #endif
1.1       root       20: 
                     21: CPrinter* thePrinter;
                     22: char BASED_CODE szExtDeviceMode[] = "EXTDEVICEMODE";
                     23: 
                     24: /////////////////////////////////////////////////////////////////////////////
                     25: 
                     26: // AbortProc:
                     27: // While printing, this replaces the normal message receiver loop.
                     28: // This provides simplified message routing until the modeless "Abort"
                     29: // dialog can be closed.
                     30: //
                     31: BOOL FAR PASCAL EXPORT AbortProc(HDC, int)
                     32: {
                     33:        MSG msg;
                     34:        
                     35:        // Allow other apps to run, while polling the abort status.
                     36:        //
                     37:        while (!thePrinter->fAbort &&
                     38:                PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
                     39:        {
                     40:                if (!thePrinter->hwndPDlg ||
                     41:                        !IsDialogMessage(thePrinter->hwndPDlg, &msg))
                     42:                {
                     43:                        TranslateMessage(&msg);
                     44:                        DispatchMessage(&msg);
                     45:                }
                     46:        }
                     47:        
                     48:        return !thePrinter->fAbort;
                     49: }
                     50: 
                     51: class CPrintCanDlg : public CDialog
                     52: {
                     53: public:
                     54:        CPrintCanDlg();
                     55:        
                     56:        BOOL OnInitDialog();
                     57:        afx_msg void OnCancel();
                     58:        
                     59:        DECLARE_MESSAGE_MAP();
                     60: };
                     61: 
                     62: BEGIN_MESSAGE_MAP(CPrintCanDlg, CDialog)
                     63:        ON_COMMAND(IDOK, OnCancel)
                     64: END_MESSAGE_MAP()
                     65: 
                     66: CPrintCanDlg::CPrintCanDlg()
                     67: {
                     68:        VERIFY( Create(IDD_PRINT) );
                     69: }
                     70: 
                     71: BOOL CPrintCanDlg::OnInitDialog()
                     72: {
                     73:        SetDlgItemText(IDD_PRINTDEVICE, thePrinter->printDlg.GetDeviceName());
                     74:        SetDlgItemText(IDD_PRINTPORT, thePrinter->printDlg.GetPortName());
                     75:        SetDlgItemText(IDD_PRINTTITLE, thePrinter->szTitle);
                     76:        return TRUE;
                     77: }
                     78: 
                     79: void CPrintCanDlg::OnCancel()
                     80: {
                     81:        thePrinter->fAbort = TRUE;
                     82: }
                     83: 
                     84: // StartJob:
                     85: // Prepare the printer DC and open the Cancel dialog, ready for printing.
                     86: // The application's frame is disabled, mostly to protect the data while
                     87: // printing is in progress.
                     88: //
                     89: BOOL CPrinter::StartJob(char* szDocName)
                     90: {
                     91:        char sz [32];
                     92: 
                     93:        fAbort = FALSE;
                     94:        fError = TRUE; // Assume an error until done.
                     95:        pdc = NULL;
                     96:        pdlg = NULL;
                     97:        
                     98:        // Create the job title by loading the title string from STRINGTABLE.
                     99:        //
                    100:        int cch = LoadString(AfxGetInstanceHandle(), IDS_PRINTJOB,
                    101:                sz, sizeof(sz));
                    102:        strncpy(sz + cch, szDocName, sizeof (sz) - cch - 1);
                    103:        sz[sizeof(sz)-1] = '\0';
                    104:        strncpy(szTitle, szDocName, sizeof (szTitle) - 1);
                    105:        
                    106:        // Use standard PrintDlg to get printer DC
                    107:        //
                    108:        //If DoModalPrint returns 0 then user canceled or an error happened.
                    109:        if ( printDlg.DoModal() == IDCANCEL)
                    110:                return FALSE;
                    111: 
                    112:        pdc = new CDC;
                    113:        pdc->Attach(printDlg.GetPrinterDC());
                    114: 
                    115:        pdlg = new CPrintCanDlg;
                    116:        
                    117:        hwndPDlg = pdlg->m_hWnd;
                    118:        
                    119:        // Disable the main application window and create the Cancel dialog.
                    120:        //
                    121:        AfxGetApp()->m_pMainWnd->EnableWindow(FALSE);
                    122: 
                    123:        // Allow the app to inform GDI of the escape function to call.
                    124:        //
                    125:        if (pdc->SetAbortProc(AbortProc) < 0)
                    126:                goto printFailed;
                    127:        
                    128:        pdlg->ShowWindow(SW_SHOW);
                    129:        pdlg->UpdateWindow();
                    130:        
                    131:        // Initialize the document.
                    132:        //
                    133: #ifndef _NTWIN
                    134:        if (pdc->StartDoc(sz) < 0)
                    135: #else
                    136:        DOCINFO di;
                    137:        di.cbSize = sizeof(di);
                    138:        di.lpszDocName = sz;
                    139:        di.lpszOutput = NULL;
                    140:        if (pdc->StartDoc(&di) < 0)
                    141: #endif
                    142:                goto printFailed;
                    143:        
                    144:        return TRUE;
                    145: 
                    146:        printFailed: 
                    147:                delete pdc;
                    148:                return FALSE;
                    149: }
                    150: 
                    151: // EndJob:
                    152: // Do a final page-eject, shut down the printer context, and re-enable the
                    153: // application.
                    154: //
                    155: void CPrinter::EndJob()
                    156: {
                    157:        if (pdc != NULL)
                    158:        {
                    159:                if (fAbort || pdc->EndPage() < 0 || pdc->EndDoc() < 0)
                    160:                {
                    161:                        pdc->AbortDoc();
                    162:                }
                    163:                else
                    164:                {
                    165:                        fError = FALSE;
                    166:                }
                    167:                
                    168:                delete pdc;
                    169:                pdc = NULL;
                    170:        }
                    171: 
                    172:        if (pdlg != NULL)
                    173:        {
                    174:                AfxGetApp()->m_pMainWnd->EnableWindow(TRUE);
                    175:                
                    176:                delete pdlg;
                    177:                pdlg = NULL;
                    178:                hwndPDlg = NULL;
                    179:                
                    180:        }
                    181:        
                    182:        // Error? Make sure the user knows.
                    183:        //
                    184:        if (fError)
                    185:                MPError(MB_OK | MB_ICONEXCLAMATION, IDS_PRINTERROR, (LPCSTR)szTitle);
                    186: }
                    187: 
                    188: /////////////////////////////////////////////////////////////////////////////
                    189: 
                    190: // PrintFile:
                    191: // This does all the work of printing the text buffer.
                    192: //
                    193: void CMPChild::PrintFile()
                    194: {
                    195:        int     yExtPage;
                    196:        UINT    cch;
                    197:        UINT    ich;
                    198:        PSTR    pch;
                    199:        UINT    iLine;
                    200:        UINT    nLinesEc;
                    201:        HANDLE  hT;
                    202:        UINT    dy;
                    203:        int     yExtSoFar;
                    204:        
                    205:        char szDocName[256];
                    206:        GetWindowText(szDocName, sizeof (szDocName));
                    207:        
                    208:        if ( !thePrinter->StartJob(szDocName) )
                    209:                return;
                    210:        
                    211:        dy = thePrinter->pdc->GetTextExtent("CC", 2).cy;
                    212:        yExtPage = thePrinter->pdc->GetDeviceCaps(VERTRES);
                    213:        
                    214:        // Get the lines in document and and a handle to the text buffer.
                    215:        //
                    216:        iLine = 0;
                    217:        yExtSoFar = 0;
                    218:        nLinesEc = m_edit.GetLineCount();
                    219:        hT = m_edit.GetHandle();
                    220: 
                    221:        // While more lines, print out the text.
                    222:        //
                    223:        while (iLine < nLinesEc)
                    224:        {
                    225:                if (yExtSoFar + (int)dy > yExtPage)
                    226:                {
                    227:                        // Reached the end of a page. Tell the device driver to eject a
                    228:                        // page.
                    229:                        //
                    230:                        if (thePrinter->pdc->EndPage() < 0 || thePrinter->fAbort)
                    231:                                break;
                    232:                        
                    233:                        yExtSoFar = 0;
                    234:                }
                    235:                
                    236:                // Get the length and position of the line in the buffer
                    237:                // and lock from that offset into the buffer.
                    238:                //
                    239:                ich = m_edit.LineIndex(iLine);
                    240:                cch = m_edit.LineLength(ich);
                    241:                pch = (PSTR)LocalLock(hT) + ich;
                    242:                
                    243:                // Print the line and unlock the text handle.
                    244:                //
                    245:                thePrinter->pdc->TabbedTextOut(0, yExtSoFar, (LPCSTR)pch, cch,
                    246:                        0, NULL, 0);
                    247:                LocalUnlock(hT);
                    248:                
                    249:                // Test and see if the Abort flag has been set. If yes, exit.
                    250:                //
                    251:                if (thePrinter->fAbort)
                    252:                        break;
                    253:                
                    254:                // Move down the page.
                    255:                //
                    256:                yExtSoFar += dy;
                    257:                iLine++;
                    258:        }
                    259:        
                    260:        thePrinter->EndJob();
                    261: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.