home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbcs.zip / PAGEDLG.C < prev    next >
C/C++ Source or Header  |  1996-01-21  |  16KB  |  431 lines

  1. #pragma    title("DBCS Driver  --  Version 1  --  (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:   1996-01-19                        */
  25. /*  Revised:   1996-01-19                        */
  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., 1996.        */
  56. /* Copyright ╕ 1996  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.  
  246. switch ( msg )
  247.    {
  248.                /* Perform dialog initialization            */
  249.    case    WM_INITDLG :
  250.                /* Save the page    setup                */
  251.        pagsTemp    = pags;
  252.                /* Form the margin values and place within the    */
  253.                /* entry    fields                    */
  254.  
  255.        sprintf(szBuffer, "%.2f\"", pags.rdTopMargin);
  256.        WinSetDlgItemText(hWnd, EF_MARGINTOP, szBuffer);
  257.        sprintf(szBuffer, "%.2f\"", pags.rdBottomMargin);
  258.        WinSetDlgItemText(hWnd, EF_MARGINBOTTOM,    szBuffer);
  259.        sprintf(szBuffer, "%.2f\"", pags.rdLeftMargin);
  260.        WinSetDlgItemText(hWnd, EF_MARGINLEFT, szBuffer);
  261.        sprintf(szBuffer, "%.2f\"", pags.rdRightMargin);
  262.        WinSetDlgItemText(hWnd, EF_MARGINRIGHT, szBuffer);
  263.  
  264.                /* Place    the header and footer within the    */
  265.                /* entry    fields                    */
  266.  
  267.        WinSetDlgItemText(hWnd, EF_HEADER, pags.szHeader);
  268.        WinSetDlgItemText(hWnd, EF_FOOTER, pags.szFooter);
  269.  
  270.                /* Build    the preview bitmap            */
  271.  
  272.        if ( fBuildPreview(hWnd)    )
  273.        CenterDlg(hWnd);
  274.        else
  275.        {
  276.        WinMessageBox(HWND_DESKTOP, hWnd, "Preview bitmap creation failed.",
  277.              "Page Setup", 0UL, MB_OK);
  278.  
  279.                /* Bitmap creation failed, dismiss the        */
  280.                /* dialogue                    */
  281.  
  282.        WinDismissDlg(hWnd, FALSE);
  283.        }
  284.        break;
  285.             /* Process control selections            */
  286.    case    WM_CONTROL :
  287.        switch (    SHORT2FROMMP(mp1) )
  288.        {
  289.  
  290. /************************************************************************/
  291. /* Entry field losing focus notification                */
  292. /************************************************************************/
  293.  
  294.        case    EN_KILLFOCUS :
  295.            switch (    SHORT1FROMMP(mp1) )
  296.            {
  297.            case    EF_MARGINLEFT :
  298.            case    EF_MARGINRIGHT :
  299.            case    EF_MARGINTOP :
  300.            case    EF_MARGINBOTTOM    :
  301.            case    EF_HEADER :
  302.            case    EF_FOOTER :
  303.                GpiDeleteBitmap(hbmPreview);
  304.  
  305.                /* Get the values the user has entered within    */
  306.                /* the entry fields and save internally        */
  307.  
  308.                WinQueryDlgItemText(hWnd, EF_MARGINTOP,      8L, szBuffer);
  309.                pags.rdTopMargin    = atof(szBuffer);
  310.                WinQueryDlgItemText(hWnd, EF_MARGINBOTTOM, 8L, szBuffer);
  311.                pags.rdBottomMargin = atof(szBuffer);
  312.                WinQueryDlgItemText(hWnd, EF_MARGINLEFT,      8L, szBuffer);
  313.                pags.rdLeftMargin = atof(szBuffer);
  314.                WinQueryDlgItemText(hWnd, EF_MARGINRIGHT,  8L, szBuffer);
  315.                pags.rdRightMargin = atof(szBuffer);
  316.  
  317.                /* Get the header and footer text from the    */
  318.                /* entry    fields and save    internally for the    */
  319.                /* print    routines                */
  320.  
  321.                WinQueryDlgItemText(hWnd, EF_HEADER, 256L, pags.szHeader);
  322.                WinQueryDlgItemText(hWnd, EF_FOOTER, 256L, pags.szFooter);
  323.  
  324.                if ( fBuildPreview(hWnd)    )
  325.                WinInvalidateRect(hWnd, &rclPreview,    TRUE);
  326.                break;
  327.            }
  328.            break;
  329.        }
  330.        break;
  331.                /* Process push button selections        */
  332.    case    WM_COMMAND :
  333.        switch (    SHORT1FROMMP(mp1) )
  334.        {
  335.                /* OK push button selected            */
  336.        case    DID_OK :
  337.                /* Get the values the user has entered within    */
  338.                /* the entry fields and save internally        */
  339.  
  340.            WinQueryDlgItemText(hWnd, EF_MARGINTOP,      8L, szBuffer);
  341.            pags.rdTopMargin    = atof(szBuffer);
  342.            WinQueryDlgItemText(hWnd, EF_MARGINBOTTOM, 8L, szBuffer);
  343.            pags.rdBottomMargin = atof(szBuffer);
  344.            WinQueryDlgItemText(hWnd, EF_MARGINLEFT,      8L, szBuffer);
  345.            pags.rdLeftMargin = atof(szBuffer);
  346.            WinQueryDlgItemText(hWnd, EF_MARGINRIGHT,  8L, szBuffer);
  347.            pags.rdRightMargin = atof(szBuffer);
  348.  
  349.                /* Get the header and footer text from the    */
  350.                /* entry    fields and save    internally for the    */
  351.                /* print    routines                */
  352.  
  353.            WinQueryDlgItemText(hWnd, EF_HEADER, 256L, pags.szHeader);
  354.            WinQueryDlgItemText(hWnd, EF_FOOTER, 256L, pags.szFooter);
  355.  
  356.                /* Delete the preview bitmap            */
  357.  
  358.            GpiDeleteBitmap(hbmPreview);
  359.            WinDismissDlg(hWnd, TRUE);
  360.            break;
  361.                /* Cancel push button selected            */
  362.        case    DID_CANCEL :
  363.                /* Restore the page setup and delete the    preview    */
  364.                /* bitmap                    */
  365.  
  366.            pags = pagsTemp;
  367.            GpiDeleteBitmap(hbmPreview);
  368.            WinDismissDlg(hWnd, FALSE);
  369.            break;
  370.                /* Setup    push button selected            */
  371.  
  372.        case    DID_SETUP :
  373.                /* Since    the form selection and usage is    not    */
  374.                /* consistant, the printer setup    dialogue must    */
  375.                /* used.     Display the job properties dialogue to    */
  376.                /* allow    the form to be selected.        */
  377.  
  378.            PrnQueryJobProperties(&prn, prn.iQueue);
  379.            if ( fBuildPreview(hWnd)    )
  380.            WinInvalidateRect(hWnd, &rclPreview,    TRUE);
  381.            break;
  382.        }
  383.        break;
  384.                /* Close    requested, exit    dialogue        */
  385.    case    WM_CLOSE :
  386.                /* Restore the page setup and delete the    preview    */
  387.                /* bitmap                    */
  388.        pags = pagsTemp;
  389.        GpiDeleteBitmap(hbmPreview);
  390.        WinDismissDlg(hWnd, FALSE);
  391.        break;
  392.                /* Paint    required                */
  393.    case    WM_PAINT :
  394.        mr = WinDefDlgProc(hWnd,    msg, mp1, mp2);
  395.        WinDrawBitmap(hPS = WinGetPS(hWnd), hbmPreview,
  396.              (PRECTL)NULL, (PPOINTL)(PVOID)&rclDest,
  397.              0L, 0L, DBM_NORMAL    | DBM_STRETCH);
  398.  
  399.        GpiSetColor(hPS,    RGBCLR_PALEGRAY);
  400.  
  401.                /* Set up the starting position and start    */
  402.                /* drawing the lighted edge of the window    */
  403.  
  404.        GpiPolyLineDisjoint(hPS,    8L, aptlArea);
  405.  
  406.                /* Get the presentation space and set the first    */
  407.                /* outline edge colour to white            */
  408.  
  409.        GpiCreateLogColorTable(hPS, 0L, LCOLF_RGB, 0L, 0L, (PLONG)NULL);
  410.        GpiSetColor(hPS,    RGBCLR_SHADOW);
  411.  
  412.                /* Set up the starting position and start    */
  413.                /* drawing the lighted edge of the window    */
  414.  
  415.        GpiMove(hPS, &aptlShade[3]);
  416.        GpiPolyLine(hPS,    2L, aptlShade);
  417.  
  418.                /* Set the shaded edge colour and start the    */
  419.                /* drawing of the shaded    edge of    the window    */
  420.  
  421.        GpiSetColor(hPS,    RGB_WHITE);
  422.        GpiPolyLine(hPS,    2L, &aptlShade[2]);
  423.        WinReleasePS(hPS);
  424.        return(mr);
  425.             /* Pass    through    unhandled messages        */
  426.    default :
  427.        return(WinDefDlgProc(hWnd, msg, mp1, mp2));
  428.    }
  429. return(0L);
  430. }
  431.