home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / prnt1.zip / PrnAPI.C < prev    next >
C/C++ Source or Header  |  1995-02-10  |  17KB  |  515 lines

  1. #pragma    title("Printer Driver  --  Version 1  --  (PrnAPI.C)")
  2. #pragma    subtitle("   Printer API's - Interface Definitions")
  3.  
  4. #pragma    info(noext)
  5.  
  6. #define    INCL_DEV           /* Include OS/2 Device Interface    */
  7. #define    INCL_DOS           /* Include OS/2 DOS Kernal        */
  8. #define    INCL_GPI           /* Include OS/2 PM GPI Interface    */
  9. #define    INCL_SPL           /* Include OS/2 PM Spooler Interface    */
  10. #define    INCL_SPLDOSPRINT
  11. #define    INCL_WIN           /* Include OS/2 PM Windows Interface    */
  12.  
  13. #include <malloc.h>
  14. #include <os2.h>
  15. #include <string.h>
  16.  
  17. #include "appdefs.h"
  18. #include "prnsetup.h"
  19.  
  20. /* This    file contains the printer support functions that can be    used    */
  21. /* to select a printer,    manipulate job properties, and open a DC    */
  22. /* for printing.                            */
  23.  
  24. /* Filename:   PrnAPI.C                            */
  25.  
  26. /*  Version:   1                            */
  27. /*  Created:   1995-02-09                        */
  28. /*  Revised:   1995-02-09                        */
  29.  
  30. /* Routines:   BOOL PrnCreatePrinterList(PPRN pprn);            */
  31. /*           BOOL PrnDestroyPrinterList(PPRN pprn);            */
  32. /*           HDC PrnOpenDC(PPRN pprn,    PSZ pszDataType);        */
  33. /*           BOOL PrnQueryJobProperties(PPRN pprn, INT iQueue);    */
  34. /*           PSZ PrnQueryPrinterName(PPRN pprn);            */
  35. /*           MRESULT EXPENTRY    PrnSetupDlgProc(HWND hWnd, ULONG msg,    */
  36. /*                        MPARAM mp1, MPARAM mp2);*/
  37.  
  38. /************************************************************************/
  39. /************************************************************************/
  40. /************************************************************************/
  41. /* DISCLAIMER OF WARRANTIES:                        */
  42. /* -------------------------                        */
  43. /* The following [enclosed] code is sample code    created    by IBM        */
  44. /* Corporation and Prominare Inc.  This    sample code is not part    of any    */
  45. /* standard IBM    product    and is provided    to you solely for the purpose    */
  46. /* of assisting    you in the development of your applications.  The code    */
  47. /* is provided "AS IS",    without    warranty of any    kind.  Neither IBM nor    */
  48. /* Prominare shall be liable for any damages arising out of your    */
  49. /* use of the sample code, even    if they    have been advised of the    */
  50. /* possibility of such damages.                        */
  51. /************************************************************************/
  52. /************************************************************************/
  53. /************************************************************************/
  54. /*               D I S C L A I M E R                */
  55. /* This    code is    provided on an as is basis with    no implied support.    */
  56. /* It should be    considered freeware that cannot    be rebundled as        */
  57. /* part    of a larger "*ware" offering without our consent.        */
  58. /************************************************************************/
  59. /************************************************************************/
  60. /************************************************************************/
  61.  
  62. /* Copyright ╕ International Business Machines Corp., 1995.        */
  63. /* Copyright ╕ 1995  Prominare Inc.  All Rights    Reserved.        */
  64.  
  65. /* --------------------------------------------------------------------    */
  66.  
  67. #pragma    subtitle("   Printer Control - Create Printer List Function")
  68. #pragma    page( )
  69.  
  70. /* --- PrnCreatePrinterList ---------------------------- [ Public ] ---    */
  71. /*                                    */
  72. /*     This function is    construct a list of printers.            */
  73. /*                                    */
  74. /*     Upon Entry:                            */
  75. /*                                    */
  76. /*     PPRN pprn; = Printer Control Structure Pointer            */
  77. /*                                    */
  78. /*     Upon Exit:                            */
  79. /*                                    */
  80. /*     PrnCreatePrinterList =  TRUE : Queue List Retrieved        */
  81. /*                = FALSE : No Queue List Retrieved        */
  82. /*                                    */
  83. /* --------------------------------------------------------------------    */
  84.  
  85. BOOL PrnCreatePrinterList(PPRN pprn)
  86.  
  87. {
  88. PCHAR      pch;               /* Character    Pointer            */
  89. PPRQINFO3 pQueueInfo;           /* Queue Information            */
  90. ULONG      cReturned;           /* Returned Queue Count        */
  91. ULONG      cTotal;           /* Total Count            */
  92. ULONG      cbNeeded;           /* Buffer Size            */
  93. register INT i;               /* Loop Counter            */
  94. register UINT n;           /* Loop Counter            */
  95.  
  96.                /* Determine the    amount of memory required for    */
  97.                /* queue    information                */
  98.  
  99. SplEnumQueue((PSZ)NULL,    3UL, (PVOID)NULL, 0UL, &cReturned, &cTotal,
  100.          &cbNeeded,    NULL);
  101.  
  102.                /* Check    to see if any printers were detected    */
  103.                /* and if none were found, return with the no    */
  104.                /* no queue list    retrieved flag            */
  105. if ( cTotal == 0L )
  106.    {
  107.    pprn->hAB     = 0UL;
  108.    pprn->cQueues = 0;
  109.    return(FALSE);
  110.    }
  111.                /* Allocate memory for the queue    list        */
  112.  
  113. if ( DosAllocMem((PPVOID)(PVOID)&pQueueInfo, cbNeeded, PAG_READ    | PAG_WRITE | PAG_COMMIT) )
  114.    {
  115.    pprn->hAB     = 0UL;
  116.    pprn->cQueues = 0;
  117.    return(FALSE);
  118.    }
  119.                /* Retrieve the queue list from the system    */
  120.  
  121. SplEnumQueue((PSZ)NULL,    3UL, (PVOID)pQueueInfo,    cbNeeded, &cReturned,
  122.          &cTotal, &cbNeeded, NULL);
  123.  
  124. pprn->pquei = (PQUEINFO)malloc(sizeof(QUEINFO) * cTotal);
  125.  
  126.                /* Scan the queue list looking for the entry    */
  127.                /* that contains    the default queue and when    */
  128.                /* found, save the queue    index entry        */
  129.  
  130. for ( i    = 0; i < cReturned; i++    )
  131.    {
  132.    if (    pQueueInfo[i].pszName )
  133.        {
  134.        pprn->pquei[i].pszName =    (PSZ)malloc((n = strlen(pQueueInfo[i].pszName) + 1));
  135.        memcpy(pprn->pquei[i].pszName, pQueueInfo[i].pszName, n);
  136.        }
  137.    else
  138.        pprn->pquei[i].pszName =    NULL;
  139.  
  140.    if (    pQueueInfo[i].pszDriverName )
  141.        {
  142.        pprn->pquei[i].pszDriverName = (PSZ)malloc((n = strlen(pQueueInfo[i].pszDriverName) + 1));
  143.        memcpy(pprn->pquei[i].pszDriverName, pQueueInfo[i].pszDriverName, n);
  144.        }
  145.    else
  146.        pprn->pquei[i].pszDriverName = NULL;
  147.  
  148.    if (    pQueueInfo[i].pszPrinters )
  149.        {
  150.        pprn->pquei[i].pszPrinters = (PSZ)malloc((n = strlen(pQueueInfo[i].pszPrinters) + 1));
  151.        memcpy(pprn->pquei[i].pszPrinters, pQueueInfo[i].pszPrinters, n);
  152.        }
  153.    else
  154.        pprn->pquei[i].pszPrinters = NULL;
  155.  
  156.    pprn->pquei[i].pDriverData =    (PSZ)malloc((ULONG)pQueueInfo[i].pDriverData->cb);
  157.    memcpy(pprn->pquei[i].pDriverData, pQueueInfo[i].pDriverData, (ULONG)pQueueInfo[i].pDriverData->cb);
  158.    }
  159.                /* Get the default queue    to use            */
  160.  
  161. if ( PrfQueryProfileString(HINI_PROFILE, "PM_SPOOLER", "QUEUE",    NULL,
  162.                pprn->szQueue, 256UL) )
  163.    {
  164.                /* Truncate the semicolon delimiter in the queue    */
  165.                /* name                        */
  166.  
  167.    pch = strchr(pprn->szQueue, ';');
  168.    *pch    = 0;
  169.    }
  170. else
  171.                /* No default queue name    found, use the first    */
  172.                /* printer from the list                */
  173.  
  174.    strcpy(pprn->szQueue, pQueueInfo->pszName);
  175.  
  176.                /* Scan the queue list looking for the entry    */
  177.                /* that contains    the default queue and when    */
  178.                /* found, save the queue    index entry        */
  179.  
  180. for ( i    = 0; i < cReturned; i++    )
  181.    if (    !strcmp(pprn->szQueue, pQueueInfo[i].pszName) )
  182.        {
  183.        pprn->iQueue = i;
  184.        break;
  185.        }
  186.                /* Save the queue information within the    printer    */
  187.                /* information structure    and save the number of    */
  188.                /* queues found within the system as well and    */
  189.                /* return the queues found flag            */
  190.  
  191. pprn->cQueues =    (INT)cReturned;
  192.  
  193. DosFreeMem((PVOID)pQueueInfo);
  194.  
  195. return(TRUE);
  196. }
  197. #pragma    subtitle("   Printer Control - Destroy Printer List Function")
  198. #pragma    page( )
  199.  
  200. /* --- PrnDestroyPrinterList --------------------------- [ Public ] ---    */
  201. /*                                    */
  202. /*     This function is    used to    destroy    a constructed printer list.    */
  203. /*                                    */
  204. /*     Upon Entry:                            */
  205. /*                                    */
  206. /*     PPRN pprn; = Printer Control Structure Pointer            */
  207. /*                                    */
  208. /*     Upon Exit:                            */
  209. /*                                    */
  210. /*     PrnDestroyPrinterList =    TRUE : List Destroyed            */
  211. /*                 = FALSE : Error                */
  212. /*                                    */
  213. /* --------------------------------------------------------------------    */
  214.  
  215. BOOL PrnDestroyPrinterList(PPRN    pprn)
  216.  
  217. {
  218. register INT i;               /* Loop Counter            */
  219.  
  220.                /* Check    to make    sure that memory was allocated    */
  221.                /* for the list and if the case,    release    it    */
  222. if ( pprn->hAB )
  223.    {
  224.    for ( i = 0;    i < pprn->cQueues; i++ )
  225.        {
  226.        if ( pprn->pquei[i].pszName )
  227.        free(pprn->pquei[i].pszName);
  228.  
  229.        if ( pprn->pquei[i].pszDriverName )
  230.        free(pprn->pquei[i].pszDriverName);
  231.  
  232.        if ( pprn->pquei[i].pszPrinters )
  233.        free(pprn->pquei[i].pszPrinters);
  234.  
  235.        free(pprn->pquei[i].pDriverData);
  236.        }
  237.  
  238.    pprn->hAB = 0;
  239.    pprn->cQueues = 0;
  240.    return(TRUE);
  241.    }
  242.                /* No memory allocated for the list, return    */
  243.                /* error    flag                    */
  244. return(FALSE);
  245. }
  246. #pragma    subtitle("   Printer Control - Printer DC Open Function")
  247. #pragma    page( )
  248.  
  249. /* --- PrnOpenDC --------------------------------------- [ Public ] ---    */
  250. /*                                    */
  251. /*     This function is    used to    open a device context on a specified    */
  252. /*     printer.                                */
  253. /*                                    */
  254. /*     Upon Entry:                            */
  255. /*                                    */
  256. /*     PPRN pprn;     = Printer Control Structure Pointer        */
  257. /*     PSZ  pszDataType; = Data    Type (PM_Q_STD or PM_Q_RAW)        */
  258. /*                                    */
  259. /*     Upon Exit:                            */
  260. /*                                    */
  261. /*     PrnOpenDC = Printer Device Context                */
  262. /*                                    */
  263. /* --------------------------------------------------------------------    */
  264.  
  265. HDC PrnOpenDC(PPRN pprn, PSZ pszDataType)
  266.  
  267. {
  268. CHAR         achDriverName[256];   /* Driver Name            */
  269. DEVOPENSTRUC dop;           /* Device Open Structure        */
  270. register INT i;               /* Loop Counter            */
  271.  
  272. if ( !pprn->cQueues )
  273.    return((HDC)NULL);
  274.                /* Initialize the driver    data structure        */
  275.  
  276. memset(&dop, 0,    sizeof(DEVOPENSTRUC));
  277.  
  278.                /* Set the logical address            */
  279.  
  280. dop.pszLogAddress = pprn->pquei[pprn->iQueue].pszName;
  281.  
  282.                /* Set the driver name (ie. PSCRIPT)        */
  283.  
  284. if ( (i    = (INT)strcspn(pprn->pquei[pprn->iQueue].pszDriverName,    ".")) != 0 )
  285.    {
  286.    dop.pszDriverName = memcpy(achDriverName, pprn->pquei[pprn->iQueue].pszDriverName, (UINT)i);
  287.    achDriverName[i] = 0;
  288.    }
  289. else
  290.    dop.pszDriverName = strcpy(achDriverName, pprn->pquei[pprn->iQueue].pszDriverName);
  291.  
  292. dop.pdriv  = (PVOID)pprn->pquei[pprn->iQueue].pDriverData;
  293.  
  294.                /* Set data type    (ie. PM_Q_STD or PM_Q_RAW)    */
  295.  
  296. dop.pszDataType    = pszDataType;
  297.  
  298. /* Other data values include:                        */
  299. /*                                    */
  300. /*  dop.pszComment    =    "comments"; // Comment for OD_Q            */
  301. /*  dop.pszQueueProcName   = NULL;  // queue processor;            */
  302. /*                    //    NULL =>    use default        */
  303. /*  dop.pszQueueProcParams = NULL;  // parms for queue processor    */
  304. /*  dop.pszSpoolerParams   = NULL;  // spooler parms (use NULL!)    */
  305. /*  dop.pszNetworkParams   = NULL;  // network parms (use NULL!)    */
  306.  
  307. return(DevOpenDC(pprn->hAB, OD_QUEUED, "*", 9L,    (PDEVOPENDATA)(PVOID)&dop, (HDC)0L));
  308. }
  309. #pragma    subtitle("   Printer Control - Job Properies Query Function")
  310. #pragma    page( )
  311.  
  312. /* --- PrnQueryJobProperties --------------------------- [ Public ] ---    */
  313. /*                                    */
  314. /*     This function is    used to    get printer job    properties.  It    is    */
  315. /*     valid for a printer to require no job properties.  In this    */
  316. /*     case, *pcb == 0.                            */
  317. /*                                    */
  318. /*     Upon Entry:                            */
  319. /*                                    */
  320. /*     PPRN pprn;   = Printer Control Structure    Pointer            */
  321. /*     INT  iQueue; = Selected Queue Index                */
  322. /*                                    */
  323. /*     Upon Exit:                            */
  324. /*                                    */
  325. /*     PrnQueryJobProperties =    TRUE : Properties Retrieved        */
  326. /*                 = FALSE : Buffer to Small            */
  327. /*                                    */
  328. /* --------------------------------------------------------------------    */
  329.  
  330. BOOL PrnQueryJobProperties(PPRN    pprn, INT iQueue)
  331.  
  332. {
  333. CHAR      achDeviceName[256];       /* Device Name            */
  334. CHAR      achDriverName[256];       /* Driver Name            */
  335. PCHAR      pch;               /* Character    Pointer            */
  336. register INT i;               /* Index                */
  337.  
  338. if ( !pprn->cQueues )
  339.    return(FALSE);
  340.                /* Find the driver/device delimiter and parse    */
  341.                /* out the driver name and the device name    */
  342.  
  343. if ( (i    = (INT)strcspn(pprn->pquei[iQueue].pszDriverName, "."))    != 0 )
  344.    {
  345.    memcpy(achDriverName, pprn->pquei[iQueue].pszDriverName, (UINT)i);
  346.    achDriverName[i] = 0;
  347.                /* Set the device name                */
  348.  
  349.    strcpy(achDeviceName, &pprn->pquei[iQueue].pszDriverName[i +    1]);
  350.    }
  351. else
  352.    {
  353.    strcpy(achDriverName, pprn->pquei[iQueue].pszDriverName);
  354.    achDeviceName[0] = 0;
  355.    }
  356.                /* Get terminate    properly the printer name    */
  357.  
  358. if ( (pch = strchr(pprn->pquei[iQueue].pszPrinters, ',')) != NULL )
  359.    *pch    = 0;
  360.  
  361. return((BOOL)(DevPostDeviceModes(pprn->hAB, (PVOID)pprn->pquei[iQueue].pDriverData, achDriverName,
  362.                  achDeviceName,    pprn->pquei[iQueue].pszPrinters,
  363.                  DPDM_POSTJOBPROP) == DEV_OK));
  364. }
  365. #pragma    subtitle("   Printer Control - Printer Information Query Function")
  366. #pragma    page( )
  367.  
  368. /* --- PdsPrintQueryPrinterName    ------------------------ [ Public ] ---    */
  369. /*                                    */
  370. /*     This function is    used to    get information    on a printer.        */
  371. /*                                    */
  372. /*     Upon Entry:                            */
  373. /*                                    */
  374. /*     PPRN pprn; = Printer Control Structure Pointer            */
  375. /*                                    */
  376. /*     Upon Exit:                            */
  377. /*                                    */
  378. /*     PrnQueryPrinterName = Address of    Printer    Name            */
  379. /*                                    */
  380. /* --------------------------------------------------------------------    */
  381.  
  382. PSZ PrnQueryPrinterName(PPRN pprn)
  383.  
  384. {
  385.  
  386. if ( !pprn->cQueues )
  387.    return((PSZ)NULL);
  388.                /* Return the address of    the printer name    */
  389.  
  390. return(pprn->pquei[pprn->iQueue].pszName);
  391. }
  392. #pragma    subtitle("   Printer Control - Printer Setup Dialogue Procedure")
  393. #pragma    page( )
  394.  
  395. /* --- PrnSetupDlgProc --------------------------------- [ Public ] ---    */
  396. /*                                    */
  397. /*     This function is    used to    process    the Printer Setup dialogue    */
  398. /*     procedure.                            */
  399. /*                                    */
  400. /*     Upon Entry:                            */
  401. /*                                    */
  402. /*     HWND   hWnd; = Dialogue Window Handle                */
  403. /*     ULONG  msg;  = PM Message                    */
  404. /*     MPARAM mp1;  = Message Parameter    1                */
  405. /*     MPARAM mp2;  = Message Parameter    2                */
  406. /*                                    */
  407. /*     Upon Exit:                            */
  408. /*                                    */
  409. /*     PrnSetupDlgProc = Message Handling Result            */
  410. /*                                    */
  411. /* --------------------------------------------------------------------    */
  412.  
  413. MRESULT    EXPENTRY PrnSetupDlgProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  414.  
  415. {
  416. CHAR szPrinter[CCHMAXPATH];       /* Printer Name Holder        */
  417. PPRN pprn;               /* Printer List Pointer        */
  418. register INT i,    n;           /* Loop Counters            */
  419.  
  420. switch ( msg )
  421.    {
  422.    case    WM_INITDLG :
  423.                /* Save the pointer to user style information    */
  424.                /* within the dialog's reserved memory           */
  425.  
  426.        WinSetWindowPtr(hWnd, QWL_USER, (PVOID)(pprn = (PPRN)mp2));
  427.  
  428.                /* Go through the queue list and    add each of the    */
  429.                /* queue    entries    to the list box    from which the    */
  430.                /* user can select a printer to use and/or    */
  431.                /* configure                    */
  432.  
  433.        for ( i = 0; i <    pprn->cQueues; i++ )
  434.        {
  435.                /* Insert item and store    index            */
  436.  
  437.        WinSendDlgItemMsg(hWnd, LB_PRINTERS,    LM_SETITEMHANDLE,
  438.                  MPFROMSHORT(n = (INT)LONGFROMMR(WinSendDlgItemMsg(hWnd, LB_PRINTERS, LM_INSERTITEM,
  439.                                            MPFROMSHORT(LIT_SORTASCENDING),
  440.                                            MPFROMP(strcat(strcat(strcpy(szPrinter,
  441.                                                pprn->pquei[i].pszName),    ": "),
  442.                                                pprn->pquei[i].pszDriverName))))),
  443.                  MPFROMP(i));
  444.  
  445.                /* If the default printer, save the name    to set    */
  446.                /* selection                    */
  447.  
  448.        if (    i == pprn->iQueue )
  449.            WinSendDlgItemMsg(hWnd, LB_PRINTERS, LM_SELECTITEM, MPFROMSHORT(n),
  450.                  MPFROMSHORT(TRUE));
  451.        }
  452.        break;
  453.                /* Process list box selections            */
  454.    case    WM_CONTROL :
  455.        switch (    SHORT2FROMMP(mp1) )
  456.        {
  457.                /* Printer entry    selected            */
  458.  
  459.        case    LN_SELECT :
  460.            WinEnableWindow(WinWindowFromID(hWnd, DID_SETUP), TRUE);
  461.            WinEnableWindow(WinWindowFromID(hWnd, DID_OK),     TRUE);
  462.            break;
  463.        }
  464.        break;
  465.  
  466.    case    WM_COMMAND :
  467.        switch (    SHORT1FROMMP(mp1) )
  468.        {
  469.        case    DID_OK :
  470.                /* Get selection    from the list box        */
  471.  
  472.            pprn = (PPRN)WinQueryWindowPtr(hWnd, QWL_USER);
  473.  
  474.                /* Get printer handle                */
  475.  
  476.            pprn->iQueue = (INT)LONGFROMMR(WinSendDlgItemMsg(hWnd, LB_PRINTERS, LM_QUERYITEMHANDLE,
  477.                                 MPFROMLONG(WinSendDlgItemMsg(hWnd, LB_PRINTERS,
  478.                                                  LM_QUERYSELECTION,
  479.                                                  0L, 0L)),
  480.                                 0L));
  481.            WinDismissDlg(hWnd, TRUE);
  482.            break;
  483.  
  484.        case    DID_SETUP :
  485.  
  486.                /* Get printer handle                */
  487.  
  488.            PrnQueryJobProperties((PPRN)WinQueryWindowPtr(hWnd, QWL_USER),
  489.                      (INT)LONGFROMMR(WinSendDlgItemMsg(hWnd,
  490.                                        LB_PRINTERS,
  491.                                        LM_QUERYITEMHANDLE,
  492.                                        MPFROMLONG(WinSendDlgItemMsg(hWnd,
  493.                                                     LB_PRINTERS,
  494.                                                     LM_QUERYSELECTION,
  495.                                                     0L,    0L)),
  496.                                        0L)));
  497.            break;
  498.  
  499.        case    DID_CANCEL :
  500.            WinDismissDlg(hWnd, FALSE);
  501.            break;
  502.        }
  503.        break;
  504.                /* Close    received, exit dialog            */
  505.    case    WM_CLOSE :
  506.        WinDismissDlg(hWnd, FALSE);
  507.        break;
  508.  
  509.    default :
  510.        return(WinDefDlgProc(hWnd, msg, mp1, mp2));
  511.    }
  512.                /* Pass through unhandled messages        */
  513. return(0L);
  514. }
  515.