Annotation of mstools/samples/porttool/ptprint.c, revision 1.1

1.1     ! root        1: #include "PortTool.h"
        !             2: 
        !             3: HWND   hCancelDlg = 0;
        !             4: 
        !             5: 
        !             6: // get default printer configuration and save in hWnd extra bytes for use later
        !             7: BOOL WINAPI GetPrinterConfig (
        !             8:     HWND    hWnd)
        !             9: {
        !            10:     char       lpszAppName[25];
        !            11:     char       lpszDeviceName[25];
        !            12:     char       lpszDefPtr[MAXPTRDEFINITION];
        !            13:     char       lpszDevice[MAXPTRDEVICE];
        !            14:     char       lpszDriver[MAXPTRDRIVER];
        !            15:     char       lpszPort[MAXPTRPORT];
        !            16:     HANDLE     hDevNames;
        !            17: 
        !            18:     LoadString ((HANDLE)GetModuleHandle (NULL),
        !            19:                IDS_WINDOWSAPPNAME,
        !            20:                lpszAppName,
        !            21:                sizeof (lpszAppName));
        !            22:     LoadString ((HANDLE)GetModuleHandle (NULL),
        !            23:                IDS_DEVICENAME,
        !            24:                lpszDeviceName,
        !            25:                sizeof (lpszDeviceName));
        !            26: 
        !            27:     // get default printer from WIN.INI and parse into separate strings
        !            28:     if(GetProfileString (lpszAppName, lpszDeviceName, "", lpszDefPtr, sizeof (lpszDefPtr)))
        !            29:     {
        !            30:         strcpy (lpszDevice, strtok (lpszDefPtr, ","));
        !            31:         strcpy (lpszDriver, strtok (lpszDefPtr, ","));
        !            32:         strcpy (lpszPort, strtok (lpszDefPtr, ""));
        !            33:     }
        !            34: 
        !            35:     // if invalid strings, remove config and exit
        !            36:     if (!*lpszDevice || !*lpszDriver || !*lpszPort)
        !            37:        {
        !            38:        if (hDevNames = (HANDLE)GetWindowLong (hWnd, WL_HPTRDEVNAMES))
        !            39:            {
        !            40:            LocalFree (hDevNames);
        !            41:            SetWindowLong (hWnd, WL_HPTRDEVNAMES, NULL);
        !            42:            }
        !            43:        return FALSE;
        !            44:        }
        !            45: 
        !            46:     // allocate memory for DEVNAMES structure and strings
        !            47:     if (hDevNames = (HANDLE)GetWindowLong (hWnd, WL_HPTRDEVNAMES))
        !            48:        hDevNames = LocalReAlloc (hDevNames,
        !            49:                                  strlen (lpszDevice) + 1 +
        !            50:                                  strlen (lpszDriver) + 1 +
        !            51:                                  strlen (lpszPort) + 1 +
        !            52:                                  sizeof (DEVNAMES),
        !            53:                                  LHND);
        !            54:     else
        !            55:        hDevNames = LocalAlloc (LHND,
        !            56:                                strlen (lpszDevice) + 1 +
        !            57:                                strlen (lpszDriver) + 1 +
        !            58:                                strlen (lpszPort) + 1 +
        !            59:                                sizeof (DEVNAMES));
        !            60: 
        !            61:     // fill out DEVNAMES structure and keep in window extra bytes
        !            62:     if (hDevNames)
        !            63:        {
        !            64:        DEVNAMES    *lpdn;
        !            65:        char        *lpsz;
        !            66: 
        !            67:        lpdn = (DEVNAMES *)LocalLock (hDevNames);
        !            68:        lpdn->wDeviceOffset = 0;
        !            69:        lpdn->wDriverOffset = lpdn->wDeviceOffset + sizeof (lpszDevice) + 1;
        !            70:        lpdn->wOutputOffset = lpdn->wDriverOffset + sizeof (lpszDriver) + 1;
        !            71:        lpdn->wDefault = 1;
        !            72:        lpsz = (char *)(lpdn++);
        !            73:        strcpy (lpsz, lpszDevice);
        !            74:        lpsz++;
        !            75:        strcpy (lpsz, lpszDriver);
        !            76:        lpsz++;
        !            77:        strcpy (lpsz, lpszPort);
        !            78: 
        !            79:        LocalUnlock (hDevNames);
        !            80:        SetWindowLong (hWnd, WL_HPTRDEVNAMES, (LONG)hDevNames);
        !            81:        }
        !            82:     else
        !            83:        {
        !            84:        SetWindowLong (hWnd, WL_HPTRDEVNAMES, NULL);
        !            85:        return FALSE;
        !            86:        }
        !            87: 
        !            88:     return TRUE;
        !            89: }
        !            90: 
        !            91: 
        !            92: 
        !            93: // abort proc called by gdi during print download process
        !            94: int WINAPI AbortProc (
        !            95:     HDC     hdc,
        !            96:     int     nErr)
        !            97: {
        !            98:     BOOL    fContinue = TRUE;
        !            99:     MSG     msg;
        !           100: 
        !           101:     // process messages for cancel dialog and other apps
        !           102:     while (PeekMessage (&msg, NULL, NULL, NULL, PM_REMOVE))
        !           103:        {
        !           104:        if (msg.message == UM_CANCELPRINT)
        !           105:            {
        !           106:            fContinue = FALSE;
        !           107:            break;
        !           108:            }
        !           109: 
        !           110:        else if (!hCancelDlg || !IsDialogMessage (hCancelDlg, &msg))
        !           111:            {
        !           112:            TranslateMessage (&msg);
        !           113:            DispatchMessage  (&msg);
        !           114:            }
        !           115:        }
        !           116: 
        !           117:     return fContinue;
        !           118: }
        !           119: 
        !           120: 
        !           121: 
        !           122: 
        !           123: BOOL WINAPI CancelDlgProc (
        !           124:     HWND    hWnd,
        !           125:     UINT    uMsg,
        !           126:     UINT    uParam,
        !           127:     LONG    lParam)
        !           128: {
        !           129: 
        !           130:     switch (uMsg)
        !           131:        {
        !           132:        case WM_INITDIALOG:
        !           133:            {
        !           134:            char    *lpdn;
        !           135:            char    lpszTitle[MAX_PATH];
        !           136: 
        !           137:            // initialize dialog control information
        !           138:            lpdn = LocalLock ((HANDLE)GetWindowLong (hWnd, WL_HPTRDEVNAMES));
        !           139:            SetDlgItemText (hWnd,
        !           140:                            IDC_PRINTDEVICE,
        !           141:                            lpdn + sizeof (DEVNAMES) +
        !           142:                                ((DEVNAMES *)lpdn)->wDeviceOffset);
        !           143:            SetDlgItemText (hWnd,
        !           144:                            IDC_PRINTPORT,
        !           145:                            lpdn + sizeof (DEVNAMES) +
        !           146:                                ((DEVNAMES *)lpdn)->wOutputOffset);
        !           147:            LocalUnlock ((HANDLE)GetWindowLong (hWnd, WL_HPTRDEVNAMES));
        !           148:            GetWindowText (hWnd, lpszTitle, sizeof (lpszTitle));
        !           149:            SetDlgItemText (hWnd, IDC_PRINTTITLE, lpszTitle);
        !           150:            }
        !           151:            break;
        !           152: 
        !           153:        case WM_COMMAND:
        !           154:            // if cancel button selected, post message to cancel print job
        !           155:            if (LOWORD (uParam) == IDCANCEL)
        !           156:                {
        !           157:                PostMessage (GetParent (hWnd), UM_CANCELPRINT, 0, 0);
        !           158:                DestroyWindow (hWnd);
        !           159:                }
        !           160:            break;
        !           161: 
        !           162:        default:
        !           163:            return FALSE;
        !           164:     }
        !           165:     return TRUE;
        !           166: }
        !           167: 
        !           168: 
        !           169: 
        !           170: // put up the print common dialog, and print
        !           171: int WINAPI PrintFile (
        !           172:     HWND    hWnd)
        !           173: {
        !           174:     char       *lpEditData;
        !           175:     HANDLE     hEditData;
        !           176:     SIZE       sLine;
        !           177:     int        yLineExt;
        !           178:     int        yExt;
        !           179:     int        yPageExt;
        !           180:     UINT       uLine;
        !           181:     UINT       uNumLines;
        !           182:     UINT       uOffset;
        !           183:     UINT       uLineLen;
        !           184:     HWND       hWndEdit;
        !           185:     PRINTDLG   pdPrint;
        !           186:     DOCINFO    diPrint;
        !           187:     char       lpszJobName[MAX_PATH];
        !           188: 
        !           189: 
        !           190:     // call common print dialog to get initialized printer DC
        !           191:     pdPrint.lStructSize = sizeof (PRINTDLG);
        !           192:     pdPrint.hwndOwner = hWnd;
        !           193:     pdPrint.hDevMode = NULL;
        !           194:     pdPrint.hDevNames = (HANDLE)GetWindowLong (hWnd, WL_HPTRDEVNAMES);
        !           195:     pdPrint.hDC = NULL;
        !           196:     pdPrint.Flags = PD_RETURNDC;
        !           197:     pdPrint.nFromPage = 0;
        !           198:     pdPrint.nToPage = 0;
        !           199:     pdPrint.nMinPage = 0;
        !           200:     pdPrint.nMaxPage = 0;
        !           201:     pdPrint.nCopies = 0;
        !           202:     pdPrint.hInstance = (HANDLE)GetModuleHandle (NULL);
        !           203:     pdPrint.lCustData = NULL;
        !           204:     pdPrint.lpfnPrintHook = NULL;
        !           205:     pdPrint.lpfnSetupHook = NULL;
        !           206:     pdPrint.lpPrintTemplateName = NULL;
        !           207:     pdPrint.lpSetupTemplateName = NULL;
        !           208:     pdPrint.hPrintTemplate = NULL;
        !           209:     pdPrint.hSetupTemplate = NULL;
        !           210: 
        !           211:     // call common print dialog
        !           212:     if (!PrintDlg (&pdPrint))
        !           213:        return IDS_PTRCOMMDLGFAILED;
        !           214: 
        !           215:     // start cancel dialog box
        !           216:     hCancelDlg = CreateDialog ((HANDLE)GetModuleHandle (NULL),
        !           217:                               IDD_CANCELDLG,
        !           218:                               hWnd,
        !           219:                               CancelDlgProc);
        !           220: 
        !           221: 
        !           222:     if (!hCancelDlg)
        !           223:        return IDS_CANCELDLGFAILED;
        !           224: 
        !           225:     ShowWindow (hCancelDlg, SW_SHOW);
        !           226:     UpdateWindow (hCancelDlg);
        !           227: 
        !           228:     // set AbortProc callback
        !           229:     if (SetAbortProc (pdPrint.hDC, (PROC)AbortProc) < 0)
        !           230:        {
        !           231:        // on error, clean up and go away
        !           232:        DestroyWindow (hCancelDlg);
        !           233:        DeleteDC (pdPrint.hDC);
        !           234:        return IDS_SETABORTPROCFAILED;
        !           235:        }
        !           236: 
        !           237:     // initialize printer for job
        !           238:     GetWindowText (hWnd, lpszJobName, sizeof (lpszJobName));
        !           239:     diPrint.cbSize = sizeof (DOCINFO);
        !           240:     diPrint.lpszDocName = lpszJobName;
        !           241:     diPrint.lpszOutput = NULL;
        !           242:     if (StartDoc (pdPrint.hDC, &diPrint) == SP_ERROR)
        !           243:        {
        !           244:        // on error, clean up and go away
        !           245:        DestroyWindow (hCancelDlg);
        !           246:        DeleteDC (pdPrint.hDC);
        !           247:        return IDS_STARTDOCFAILED;
        !           248:        }
        !           249: 
        !           250:     // job started, so display cancel dialog
        !           251:     ShowWindow (hCancelDlg, SW_SHOW);
        !           252:     UpdateWindow (hCancelDlg);
        !           253: 
        !           254:     // retrieve dimensions for printing and init loop variables
        !           255:     hWndEdit = (HWND)GetWindowLong(hWnd, WL_HWNDEDIT);
        !           256:     hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0L);
        !           257:     uNumLines = (WORD)SendMessage (hWndEdit, EM_GETLINECOUNT, 0, 0L);
        !           258:     GetTextExtentPoint (pdPrint.hDC, "CC", 2, &sLine);
        !           259:     yLineExt = sLine.cy;
        !           260:     yPageExt = GetDeviceCaps (pdPrint.hDC, VERTRES);
        !           261:     yExt = 0;
        !           262:     uLine = 0;
        !           263: 
        !           264:     // print text line by line from top to bottom
        !           265:     while (uLine < uNumLines)
        !           266:        {
        !           267:        // if at end of page, start a new page
        !           268:        if ((yExt + yLineExt) > yPageExt)
        !           269:            {
        !           270:            if (!EndPage (pdPrint.hDC))
        !           271:                {
        !           272:                DestroyWindow (hCancelDlg);
        !           273:                DeleteDC (pdPrint.hDC);
        !           274:                return IDS_PRINTABORTED;
        !           275:                }
        !           276:            yExt = 0;
        !           277:            }
        !           278: 
        !           279:        // determine buffer offset for current line and line length
        !           280:        uOffset = SendMessage (hWndEdit, EM_LINEINDEX, uLine, 0);
        !           281:        uLineLen = SendMessage (hWndEdit, EM_LINELENGTH, uOffset, 0);
        !           282:        lpEditData = (char *)LocalLock (hEditData) + uOffset;
        !           283: 
        !           284:        // print current the line and unlock the text handle
        !           285:        TextOut (pdPrint.hDC, 0, yExt, lpEditData, uLineLen);
        !           286:        LocalUnlock (hEditData);
        !           287: 
        !           288:        // increment page position
        !           289:        yExt += yLineExt;
        !           290:        uLine++;
        !           291:        }
        !           292: 
        !           293:     // end the last page and document
        !           294:     EndPage (pdPrint.hDC);
        !           295:     EndDoc (pdPrint.hDC);
        !           296: 
        !           297:     // end cancel dialog box, clean up and exit
        !           298:     DestroyWindow (hCancelDlg);
        !           299:     DeleteDC(pdPrint.hDC);
        !           300:     return TRUE;
        !           301: }
        !           302: 
        !           303: 
        !           304: 
        !           305: // printer setup common dialog
        !           306: int WINAPI PrinterSetup (
        !           307:     HWND    hWnd)
        !           308: {
        !           309:     PRINTDLG   pdPrint;
        !           310: 
        !           311:     // call common print dialog to get initialized printer DC
        !           312:     pdPrint.lStructSize = sizeof (PRINTDLG);
        !           313:     pdPrint.hwndOwner = hWnd;
        !           314:     pdPrint.hDevMode = NULL;
        !           315:     pdPrint.hDevNames = (HANDLE)GetWindowLong (hWnd, WL_HPTRDEVNAMES);
        !           316:     pdPrint.hDC = NULL;
        !           317:     pdPrint.Flags = PD_PRINTSETUP;
        !           318:     pdPrint.nFromPage = 0;
        !           319:     pdPrint.nToPage = 0;
        !           320:     pdPrint.nMinPage = 0;
        !           321:     pdPrint.nMaxPage = 0;
        !           322:     pdPrint.nCopies = 0;
        !           323:     pdPrint.hInstance = (HANDLE)GetModuleHandle (NULL);
        !           324:     pdPrint.lCustData = NULL;
        !           325:     pdPrint.lpfnPrintHook = NULL;
        !           326:     pdPrint.lpfnSetupHook = NULL;
        !           327:     pdPrint.lpPrintTemplateName = NULL;
        !           328:     pdPrint.lpSetupTemplateName = NULL;
        !           329:     pdPrint.hPrintTemplate = NULL;
        !           330:     pdPrint.hSetupTemplate = NULL;
        !           331: 
        !           332:     // call common print dialog
        !           333:     if (!PrintDlg (&pdPrint) && CommDlgExtendedError ())
        !           334:        return IDS_PTRCOMMDLGFAILED;
        !           335: }

unix.superglobalmegacorp.com

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