home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / prnt3.zip / PageDlg.C < prev    next >
C/C++ Source or Header  |  1995-04-03  |  16KB  |  441 lines

  1. #pragma    title("Printer Driver  --  Version 3  --  (PageDlg.C)")
  2. #pragma    subtitle("   Application Window - Interface Definitions")
  3.  
  4. #define    INCL_GPI           /* Include OS/2 PM GPI Interface    */
  5. #define    INCL_WIN           /* Include OS/2 PM Windows Interface    */
  6.  
  7. #pragma    info(noext)
  8.  
  9. #include <malloc.h>
  10. #include <os2.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include "appdefs.h"
  16. #include "prnsetup.h"
  17.  
  18. /* This    module contains    the routines to    handle the page    setup dialogue    */
  19. /* where the header and    footer is specified along with the margins.    */
  20.  
  21. /* Filename:   PageDlg.C                        */
  22.  
  23. /*  Version:   3                            */
  24. /*  Created:   1995-04-02                        */
  25. /*  Revised:   1995-04-03                        */
  26.  
  27. /* Routines:   static BOOL fBuildPreview(HWND hWnd);            */
  28. /*           MRESULT EXPENTRY    PageSetupDlgProc(HWND hWnd, ULONG msg,    */
  29. /*                         MPARAM    mp1, MPARAM mp2;*/
  30.  
  31. /************************************************************************/
  32. /************************************************************************/
  33. /************************************************************************/
  34. /* DISCLAIMER OF WARRANTIES:                        */
  35. /* -------------------------                        */
  36. /* The following [enclosed] code is sample code    created    by IBM        */
  37. /* Corporation and Prominare Inc.  This    sample code is not part    of any    */
  38. /* standard IBM    product    and is provided    to you solely for the purpose    */
  39. /* of assisting    you in the development of your applications.  The code    */
  40. /* is provided "AS IS",    without    warranty of any    kind.  Neither IBM nor    */
  41. /* Prominare shall be liable for any damages arising out of your    */
  42. /* use of the sample code, even    if they    have been advised of the    */
  43. /* possibility of such damages.                        */
  44. /************************************************************************/
  45. /************************************************************************/
  46. /************************************************************************/
  47. /*               D I S C L A I M E R                */
  48. /* This    code is    provided on an as is basis with    no implied support.    */
  49. /* It should be    considered freeware that cannot    be rebundled as        */
  50. /* part    of a larger "*ware" offering without our consent.        */
  51. /************************************************************************/
  52. /************************************************************************/
  53. /************************************************************************/
  54.  
  55. /* Copyright ╕ International Business Machines Corp., 1995.        */
  56. /* Copyright ╕ 1995  Prominare Inc.  All Rights    Reserved.        */
  57.  
  58. /* --------------------------------------------------------------------    */
  59.  
  60. /************************************************************************/
  61. /*                                    */
  62. /*     Module Data Definitions                        */
  63. /*                                    */
  64. /************************************************************************/
  65.  
  66. static HBITMAP     hbmPreview;       /* Preview Bitmap Handle        */
  67. static RECTL     rclDest;       /* Preview Bitmap Rectangle        */
  68. static RECTL     rclPreview;       /* Preview Group Box    Rectangle    */
  69. static POINTL     aptlShade[5];       /* Shadow Points            */
  70. static PAGESETUP pagsTemp;       /* Temporary    Page Setup        */
  71. static POINTL     aptlArea[8];       /* Shadow Points            */
  72. static LONG     lScale;       /* Scaling Factor            */
  73.  
  74. #pragma    subtitle("   Module Purpose - Preview Bitmap Build Function")
  75. #pragma    page( )
  76.  
  77. /* --- fBuildPreview ----------------------------------    [ Private ] ---    */
  78. /*                                    */
  79. /*     This function is    used to    create the preview bitmap for the    */
  80. /*     page setup dialogue.                        */
  81. /*                                    */
  82. /*     Upon Entry:                            */
  83. /*                                    */
  84. /*     HWND hWnd; = Dialog Window Handle                */
  85. /*                                    */
  86. /*     Upon Exit:                            */
  87. /*                                    */
  88. /*     fBuildPreview =    TRUE : Preview Bitmap Created            */
  89. /*             = FALSE : Preview Bitmap Creation Failed        */
  90. /*                                    */
  91. /* --------------------------------------------------------------------    */
  92.  
  93. static BOOL fBuildPreview(HWND hWnd)
  94.  
  95. {
  96. HDC    hdcPrinter;           /* Printer Device Context Handle    */
  97. HPS    hPS;               /* Presentation Space Handle        */
  98. LONG    cxPrev;               /* Preview Width            */
  99. LONG    cyPrev;               /* Preview Height            */
  100. SIZEL    sizlPage;           /* Page Size    Holder            */
  101. SWP    swp;               /* Window Position            */
  102.  
  103. if ( (hdcPrinter = PrnOpenInfoDC(&prn, "PM_Q_STD")) != (HDC)NULL )
  104.    {    
  105.                /* Create a presentation    space into which the    */
  106.                /* printing will    occur using TWIPS as the unit    */
  107.                /* of measure                    */
  108.  
  109.    sizlPage.cx = sizlPage.cy = 0L;
  110.  
  111.                /* Determine the    size of    the output area        */
  112.  
  113.    GpiQueryPS(hPS = GpiCreatePS(hAB, hdcPrinter, &sizlPage, PU_TWIPS |
  114.           GPIF_DEFAULT | GPIT_NORMAL | GPIA_ASSOC),    &sizlPage);
  115.  
  116.                /* Create the bitmap image            */
  117.  
  118.    hbmPreview =    hbmCreatePreview(sizlPage.cx, sizlPage.cy, hPS,    hdcPrinter);
  119.  
  120.                /* Convert the image size from twips to pels    */
  121.  
  122.    GpiConvert(hPS, CVTC_DEFAULTPAGE, CVTC_DEVICE, 1L, (PPOINTL)(PVOID)&sizlPage);
  123.  
  124.                /* Set the page boundaries points        */
  125.  
  126.    aptlArea[0].x = (LONG)(1440.0 * pags.rdLeftMargin);
  127.    aptlArea[0].y = (LONG)(1440.0 * pags.rdBottomMargin)    / 2L;
  128.    aptlArea[1].x = (LONG)(1440.0 * pags.rdLeftMargin);
  129.    aptlArea[1].y = (LONG)(1440.0 * pags.rdTopMargin) / 2L;
  130.  
  131.    aptlArea[2].x = (LONG)(1440.0 * pags.rdRightMargin);
  132.    aptlArea[2].y = (LONG)(1440.0 * pags.rdTopMargin) / 2L;
  133.    aptlArea[3].x = (LONG)(1440.0 * pags.rdRightMargin);
  134.    aptlArea[3].y = (LONG)(1440.0 * pags.rdBottomMargin)    / 2L;
  135.  
  136.    aptlArea[4].x = (LONG)(1440.0 * pags.rdLeftMargin) /    2L;
  137.    aptlArea[4].y = (LONG)(1440.0 * pags.rdBottomMargin);
  138.    aptlArea[5].x = (LONG)(1440.0 * pags.rdRightMargin) / 2L;
  139.    aptlArea[5].y = (LONG)(1440.0 * pags.rdBottomMargin);
  140.  
  141.    aptlArea[6].x = (LONG)(1440.0 * pags.rdLeftMargin) /    2L;
  142.    aptlArea[6].y = (LONG)(1440.0 * pags.rdTopMargin);
  143.    aptlArea[7].x = (LONG)(1440.0 * pags.rdRightMargin) / 2L;
  144.    aptlArea[7].y = (LONG)(1440.0 * pags.rdTopMargin);
  145.  
  146.                /* Convert the points from twips    to pels        */
  147.  
  148.    GpiConvert(hPS, CVTC_DEFAULTPAGE, CVTC_DEVICE, 8L, aptlArea);
  149.  
  150.                /* Release the presentation space and printer    */
  151.                /* device context                */
  152.  
  153.    GpiAssociate(hPS, (HDC)NULL);
  154.    GpiDestroyPS(hPS);
  155.    DevCloseDC(hdcPrinter);
  156.  
  157.                /* Get the size and position of the preview    */
  158.                /* group    box                    */
  159.  
  160.    WinQueryWindowPos(WinWindowFromID(hWnd, GB_PREVIEW),    (PSWP)&swp);
  161.  
  162.                /* Form the rectangle for the preview image    */
  163.  
  164.    rclPreview.xRight = (rclPreview.xLeft   = swp.x + 10L) + (cxPrev = swp.cx - 20L);
  165.    rclPreview.yTop   = (rclPreview.yBottom = swp.y + 10L) + (cyPrev = swp.cy - 10L - fm.lMaxBaselineExt    * 2);
  166.  
  167.                /* Resize the image to fit the group box    making    */
  168.                /* sure that it is scaled retaining the aspect    */
  169.                /* ratio                        */
  170.  
  171.    if (    ((sizlPage.cx /    cxPrev)    + 1) > ((sizlPage.cy / cyPrev) + 1) )
  172.        cyPrev =    sizlPage.cy / (lScale =    (sizlPage.cx / cxPrev) + 1);
  173.    else
  174.        cxPrev =    sizlPage.cx / (lScale =    (sizlPage.cy / cyPrev) + 1);
  175.  
  176.                /* Form the final bitmap    destination rectangle    */
  177.  
  178.    rclDest.xRight = (rclDest.xLeft   = swp.x + ((swp.cx    - cxPrev) / 2L)) + cxPrev;
  179.    rclDest.yTop      = (rclDest.yBottom = swp.y + ((swp.cy    - cyPrev) / 2L)) + cyPrev;
  180.  
  181.                /* Form the final boundary points        */
  182.  
  183.    aptlArea[0].x = rclDest.xLeft   + aptlArea[0].x / lScale;
  184.    aptlArea[0].y = rclDest.yBottom + aptlArea[0].y / lScale;
  185.    aptlArea[1].x = rclDest.xLeft   + aptlArea[1].x / lScale;
  186.    aptlArea[1].y = rclDest.yTop       - aptlArea[1].y / lScale;
  187.  
  188.    aptlArea[2].x = rclDest.xRight  - aptlArea[2].x / lScale;
  189.    aptlArea[2].y = rclDest.yTop       - aptlArea[2].y / lScale;
  190.    aptlArea[3].x = rclDest.xRight  - aptlArea[3].x / lScale;
  191.    aptlArea[3].y = rclDest.yBottom + aptlArea[3].y / lScale;
  192.  
  193.    aptlArea[4].x = rclDest.xLeft   + aptlArea[4].x / lScale;
  194.    aptlArea[4].y = rclDest.yBottom + aptlArea[4].y / lScale;
  195.    aptlArea[5].x = rclDest.xRight  - aptlArea[5].x / lScale;
  196.    aptlArea[5].y = rclDest.yBottom + aptlArea[5].y / lScale;
  197.  
  198.    aptlArea[6].x = rclDest.xLeft   + aptlArea[6].x / lScale;
  199.    aptlArea[6].y = rclDest.yTop       - aptlArea[6].y / lScale;
  200.    aptlArea[7].x = rclDest.xRight  - aptlArea[7].x / lScale;
  201.    aptlArea[7].y = rclDest.yTop       - aptlArea[7].y / lScale;
  202.  
  203.                /* Form the 3D edge points for the preview    */
  204.  
  205.    aptlShade[0].x = rclDest.xLeft;
  206.    aptlShade[0].y = rclDest.yTop;
  207.    aptlShade[1].x = rclDest.xRight + 2L;
  208.    aptlShade[1].y = rclDest.yTop;
  209.    aptlShade[2].x = rclDest.xRight + 2L;
  210.    aptlShade[2].y = rclDest.yBottom - 2L;
  211.    aptlShade[3].x = rclDest.xLeft;
  212.    aptlShade[3].y = rclDest.yBottom - 2L;
  213.    return(TRUE);
  214.    }
  215.  
  216. return(FALSE);
  217. }
  218. #pragma    subtitle("   Module Purpose - Page Setup Dialogue Procedure")
  219. #pragma    page( )
  220.  
  221. /* --- PageSetupDlgProc    -------------------------------- [ Public ] ---    */
  222. /*                                    */
  223. /*     This function is    used to    process    the messages for the Page    */
  224. /*     Setup dialogue procedure.                    */
  225. /*                                    */
  226. /*     Upon Entry:                            */
  227. /*                                    */
  228. /*     HWND   hWnd; = Dialog Window Handle                */
  229. /*     ULONG  msg;  = PM Message                    */
  230. /*     MPARAM mp1;  = Message Parameter    1                */
  231. /*     MPARAM mp2;  = Message Parameter    2                */
  232. /*                                    */
  233. /*     Upon Exit:                            */
  234. /*                                    */
  235. /*     PageSetupDlgProc    = Message Handling Result            */
  236. /*                                    */
  237. /* --------------------------------------------------------------------    */
  238.  
  239. MRESULT    EXPENTRY PageSetupDlgProc(HWND hWnd, ULONG msg,    MPARAM mp1, MPARAM mp2)
  240.  
  241. {
  242. CHAR    szBuffer[32];           /* String Buffer            */
  243. HPS    hPS;               /* Presentation Space Handle        */
  244. MRESULT    mr;               /* Message Result            */
  245. SWP    swp;               /* Window Position            */
  246.  
  247. switch ( msg )
  248.    {
  249.                /* Perform dialog initialization            */
  250.    case    WM_INITDLG :
  251.                /* Save the page    setup                */
  252.        pagsTemp    = pags;
  253.                /* Form the margin values and place within the    */
  254.                /* entry    fields                    */
  255.  
  256.        sprintf(szBuffer, "%.2f\"", pags.rdTopMargin);
  257.        WinSetDlgItemText(hWnd, EF_MARGINTOP, szBuffer);
  258.        sprintf(szBuffer, "%.2f\"", pags.rdBottomMargin);
  259.        WinSetDlgItemText(hWnd, EF_MARGINBOTTOM,    szBuffer);
  260.        sprintf(szBuffer, "%.2f\"", pags.rdLeftMargin);
  261.        WinSetDlgItemText(hWnd, EF_MARGINLEFT, szBuffer);
  262.        sprintf(szBuffer, "%.2f\"", pags.rdRightMargin);
  263.        WinSetDlgItemText(hWnd, EF_MARGINRIGHT, szBuffer);
  264.  
  265.                /* Place    the header and footer within the    */
  266.                /* entry    fields                    */
  267.  
  268.        WinSetDlgItemText(hWnd, EF_HEADER, pags.szHeader);
  269.        WinSetDlgItemText(hWnd, EF_FOOTER, pags.szFooter);
  270.  
  271.                /* Build    the preview bitmap            */
  272.  
  273.        if ( fBuildPreview(hWnd)    )
  274.        {
  275.                /* Bitmap successfully created, center the    */
  276.                /* dialogue on the display            */
  277.  
  278.        WinQueryWindowPos(hWnd, (PSWP)&swp);
  279.        WinSetWindowPos(hWnd, HWND_TOP,
  280.                (WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN)    - swp.cx) / 2L,
  281.                (WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN)    - swp.cy) / 2L,
  282.                swp.cx, swp.cy, SWP_MOVE | SWP_SIZE);
  283.        }
  284.        else
  285.        {
  286.        WinMessageBox(HWND_DESKTOP, hWnd, "Preview bitmap creation failed.",
  287.              "Page Setup", 0UL, MB_OK);
  288.  
  289.                /* Bitmap creation failed, dismiss the        */
  290.                /* dialogue                    */
  291.  
  292.        WinDismissDlg(hWnd, FALSE);
  293.        }
  294.        break;
  295.             /* Process control selections            */
  296.    case    WM_CONTROL :
  297.        switch (    SHORT2FROMMP(mp1) )
  298.        {
  299.  
  300. /************************************************************************/
  301. /* Entry field losing focus notification                */
  302. /************************************************************************/
  303.  
  304.        case    EN_KILLFOCUS :
  305.            switch (    SHORT1FROMMP(mp1) )
  306.            {
  307.            case    EF_MARGINLEFT :
  308.            case    EF_MARGINRIGHT :
  309.            case    EF_MARGINTOP :
  310.            case    EF_MARGINBOTTOM    :
  311.            case    EF_HEADER :
  312.            case    EF_FOOTER :
  313.                GpiDeleteBitmap(hbmPreview);
  314.  
  315.                /* Get the values the user has entered within    */
  316.                /* the entry fields and save internally        */
  317.  
  318.                WinQueryDlgItemText(hWnd, EF_MARGINTOP,      8L, szBuffer);
  319.                pags.rdTopMargin    = atof(szBuffer);
  320.                WinQueryDlgItemText(hWnd, EF_MARGINBOTTOM, 8L, szBuffer);
  321.                pags.rdBottomMargin = atof(szBuffer);
  322.                WinQueryDlgItemText(hWnd, EF_MARGINLEFT,      8L, szBuffer);
  323.                pags.rdLeftMargin = atof(szBuffer);
  324.                WinQueryDlgItemText(hWnd, EF_MARGINRIGHT,  8L, szBuffer);
  325.                pags.rdRightMargin = atof(szBuffer);
  326.  
  327.                /* Get the header and footer text from the    */
  328.                /* entry    fields and save    internally for the    */
  329.                /* print    routines                */
  330.  
  331.                WinQueryDlgItemText(hWnd, EF_HEADER, 256L, pags.szHeader);
  332.                WinQueryDlgItemText(hWnd, EF_FOOTER, 256L, pags.szFooter);
  333.  
  334.                if ( fBuildPreview(hWnd)    )
  335.                WinInvalidateRect(hWnd, &rclPreview,    TRUE);
  336.                break;
  337.            }
  338.            break;
  339.        }
  340.        break;
  341.                /* Process push button selections        */
  342.    case    WM_COMMAND :
  343.        switch (    SHORT1FROMMP(mp1) )
  344.        {
  345.                /* OK push button selected            */
  346.        case    DID_OK :
  347.                /* Get the values the user has entered within    */
  348.                /* the entry fields and save internally        */
  349.  
  350.            WinQueryDlgItemText(hWnd, EF_MARGINTOP,      8L, szBuffer);
  351.            pags.rdTopMargin    = atof(szBuffer);
  352.            WinQueryDlgItemText(hWnd, EF_MARGINBOTTOM, 8L, szBuffer);
  353.            pags.rdBottomMargin = atof(szBuffer);
  354.            WinQueryDlgItemText(hWnd, EF_MARGINLEFT,      8L, szBuffer);
  355.            pags.rdLeftMargin = atof(szBuffer);
  356.            WinQueryDlgItemText(hWnd, EF_MARGINRIGHT,  8L, szBuffer);
  357.            pags.rdRightMargin = atof(szBuffer);
  358.  
  359.                /* Get the header and footer text from the    */
  360.                /* entry    fields and save    internally for the    */
  361.                /* print    routines                */
  362.  
  363.            WinQueryDlgItemText(hWnd, EF_HEADER, 256L, pags.szHeader);
  364.            WinQueryDlgItemText(hWnd, EF_FOOTER, 256L, pags.szFooter);
  365.  
  366.                /* Delete the preview bitmap            */
  367.  
  368.            GpiDeleteBitmap(hbmPreview);
  369.            WinDismissDlg(hWnd, TRUE);
  370.            break;
  371.                /* Cancel push button selected            */
  372.        case    DID_CANCEL :
  373.                /* Restore the page setup and delete the    preview    */
  374.                /* bitmap                    */
  375.  
  376.            pags = pagsTemp;
  377.            GpiDeleteBitmap(hbmPreview);
  378.            WinDismissDlg(hWnd, FALSE);
  379.            break;
  380.                /* Setup    push button selected            */
  381.  
  382.        case    DID_SETUP :
  383.                /* Since    the form selection and usage is    not    */
  384.                /* consistant, the printer setup    dialogue must    */
  385.                /* used.     Display the job properties dialogue to    */
  386.                /* allow    the form to be selected.        */
  387.  
  388.            PrnQueryJobProperties(&prn, prn.iQueue);
  389.            if ( fBuildPreview(hWnd)    )
  390.            WinInvalidateRect(hWnd, &rclPreview,    TRUE);
  391.            break;
  392.        }
  393.        break;
  394.                /* Close    requested, exit    dialogue        */
  395.    case    WM_CLOSE :
  396.                /* Restore the page setup and delete the    preview    */
  397.                /* bitmap                    */
  398.        pags = pagsTemp;
  399.        GpiDeleteBitmap(hbmPreview);
  400.        WinDismissDlg(hWnd, FALSE);
  401.        break;
  402.                /* Paint    required                */
  403.    case    WM_PAINT :
  404.        mr = WinDefDlgProc(hWnd,    msg, mp1, mp2);
  405.        WinDrawBitmap(hPS = WinGetPS(hWnd), hbmPreview,
  406.              (PRECTL)NULL, (PPOINTL)(PVOID)&rclDest,
  407.              0L, 0L, DBM_NORMAL    | DBM_STRETCH);
  408.  
  409.        GpiSetColor(hPS,    RGBCLR_PALEGRAY);
  410.  
  411.                /* Set up the starting position and start    */
  412.                /* drawing the lighted edge of the window    */
  413.  
  414.        GpiPolyLineDisjoint(hPS,    8L, aptlArea);
  415.  
  416.                /* Get the presentation space and set the first    */
  417.                /* outline edge colour to white            */
  418.  
  419.        GpiCreateLogColorTable(hPS, 0L, LCOLF_RGB, 0L, 0L, (PLONG)NULL);
  420.        GpiSetColor(hPS,    RGBCLR_SHADOW);
  421.  
  422.                /* Set up the starting position and start    */
  423.                /* drawing the lighted edge of the window    */
  424.  
  425.        GpiMove(hPS, &aptlShade[3]);
  426.        GpiPolyLine(hPS,    2L, aptlShade);
  427.  
  428.                /* Set the shaded edge colour and start the    */
  429.                /* drawing of the shaded    edge of    the window    */
  430.  
  431.        GpiSetColor(hPS,    RGB_WHITE);
  432.        GpiPolyLine(hPS,    2L, &aptlShade[2]);
  433.        WinReleasePS(hPS);
  434.        return(mr);
  435.             /* Pass    through    unhandled messages        */
  436.    default :
  437.        return(WinDefDlgProc(hWnd, msg, mp1, mp2));
  438.    }
  439. return(0L);
  440. }
  441.