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

  1. #pragma    title("Printer Driver  --  Version 3  --  (PrevDlg.C)")
  2. #pragma    subtitle("   Application Window - Interface Definitions")
  3.  
  4. #define    INCL_DEV           /* Include OS/2 Device Interface    */
  5. #define    INCL_DOS           /* Include OS/2 DOS Kernal        */
  6. #define    INCL_DOSERRORS           /* Include OS/2 DOS Errors        */
  7. #define    INCL_GPI           /* Include OS/2 PM GPI Interface    */
  8. #define    INCL_WIN           /* Include OS/2 PM Windows Interface    */
  9.  
  10. #pragma    info(noext)
  11.  
  12. #include <malloc.h>
  13. #include <os2.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #include "appdefs.h"
  19. #include "prnsetup.h"
  20.  
  21. /* This    module contains    the routines to    print preview dialogue        */
  22. /* including the construction of the preview image.            */
  23.  
  24. /* Filename:   PrevDlg.C                        */
  25.  
  26. /*  Version:   3                            */
  27. /*  Created:   1995-03-07                        */
  28. /*  Revised:   1995-04-03                        */
  29.  
  30. /* Routines:   HBITMAP hbmCreatePreview(LONG cx, LONG cy,        */
  31. /*                    HPS hpsPrinter,    HDC hdcPrinter);*/
  32. /*           MRESULT EXPENTRY    PrintPreviewDlgProc(HWND hWnd,        */
  33. /*                            ULONG msg,        */
  34. /*                            MPARAM mp1,        */
  35. /*                            MPARAM mp2);    */
  36.  
  37. /************************************************************************/
  38. /************************************************************************/
  39. /************************************************************************/
  40. /* DISCLAIMER OF WARRANTIES:                        */
  41. /* -------------------------                        */
  42. /* The following [enclosed] code is sample code    created    by IBM        */
  43. /* Corporation and Prominare Inc.  This    sample code is not part    of any    */
  44. /* standard IBM    product    and is provided    to you solely for the purpose    */
  45. /* of assisting    you in the development of your applications.  The code    */
  46. /* is provided "AS IS",    without    warranty of any    kind.  Neither IBM nor    */
  47. /* Prominare shall be liable for any damages arising out of your    */
  48. /* use of the sample code, even    if they    have been advised of the    */
  49. /* possibility of such damages.                        */
  50. /************************************************************************/
  51. /************************************************************************/
  52. /************************************************************************/
  53. /*               D I S C L A I M E R                */
  54. /* This    code is    provided on an as is basis with    no implied support.    */
  55. /* It should be    considered freeware that cannot    be rebundled as        */
  56. /* part    of a larger "*ware" offering without our consent.        */
  57. /************************************************************************/
  58. /************************************************************************/
  59. /************************************************************************/
  60.  
  61. /* Copyright ╕ International Business Machines Corp., 1995.        */
  62. /* Copyright ╕ 1995  Prominare Inc.  All Rights    Reserved.        */
  63.  
  64. /* --------------------------------------------------------------------    */
  65.  
  66. /************************************************************************/
  67. /*                                    */
  68. /*     Module Data Definitions                        */
  69. /*                                    */
  70. /************************************************************************/
  71.  
  72. static HBITMAP hbmPreview;       /* Preview Bitmap Handle        */
  73. static RECTL   rclDest;           /* Preview Bitmap Rectangle        */
  74. static POINTL  aptlShade[5];       /* Shadow Points            */
  75. static POINTL  aptlArea[8];       /* Print Boundary Points        */
  76. static LONG    lScale;           /* Scaling Factor            */
  77.  
  78. /************************************************************************/
  79. /*                                    */
  80. /*     Module Prototype    Definitions                    */
  81. /*                                    */
  82. /************************************************************************/
  83.  
  84. #pragma    subtitle("   Print Preview Dialogue - Preview Bitmap Create")
  85. #pragma    page( )
  86.  
  87. /* --- hbmCreatePreview    -------------------------------- [ Public ] ---    */
  88. /*                                    */
  89. /*     This function is    used to    process    the messages for the dialogue    */
  90. /*     procedure.                            */
  91. /*                                    */
  92. /*     Upon Entry:                            */
  93. /*                                    */
  94. /*     LONG cx;    = Bitmap Width                        */
  95. /*     LONG cy;    = Bitmap Height                        */
  96. /*                                    */
  97. /*     Upon Exit:                            */
  98. /*                                    */
  99. /*     hbmCreatePreview    = Bitmap Handle                    */
  100. /*                                    */
  101. /* --------------------------------------------------------------------    */
  102.  
  103. HBITMAP    hbmCreatePreview(LONG cx, LONG cy, HPS hpsPrinter, HDC hdcPrinter)
  104.  
  105. {
  106. BITMAPINFOHEADER2 bminfo2;       /* Bitmap Information Header        */
  107. FONTCACHE      fc;           /* Font Cache            */
  108. FONTSEL          fselPreview;       /* Preview Font Selections        */
  109. HBITMAP          hbmConstruct;       /* Bitmap Handle            */
  110. HDC          hDC;           /* Device Context Handle        */
  111. HPS          hpsBitmap;       /* Bitmap Presentation Space    Handle    */
  112. LONG          cFormats;       /* Formats Count            */
  113. PLONG          plFormats;       /* Formats Array            */
  114. POINTL          ptl;           /* Conversion Point            */
  115. SIZEL          sizl;           /* Sizing Structure            */
  116.  
  117.                /* Get bitmap device context handle for the main    */
  118.                /* Client Window                    */
  119.  
  120. if ( !(hDC = DevOpenDC(hAB, OD_MEMORY, "*", 0L,    0L, 0L)) )
  121.    return((HBITMAP)NULL);
  122.                /* Create bitmap    presentation space specifying    */
  123.                /* entire map Client Window for size required    */
  124. sizl.cx    = cx;
  125. sizl.cy    = cy;
  126.  
  127. if ( !(hpsBitmap = GpiCreatePS(hAB, hDC, &sizl,    PU_TWIPS | GPIT_NORMAL | GPIA_ASSOC)) )
  128.    {
  129.                /* Error    occurred during    creation of        */
  130.                /* presentation space, close device context    */
  131.    DevCloseDC(hDC);
  132.    return((HBITMAP)NULL);
  133.    }
  134.                /* Get the number of bitmap formats that    the    */
  135.                /* display driver supports            */
  136.  
  137. DevQueryCaps(hDC, CAPS_BITMAP_FORMATS, 1L, &cFormats);
  138.  
  139.                /* Get the bitmap display formats.  The first    */
  140.                /* set within the array will be the one that    */
  141.                /* most closely matches the display device.    */
  142.  
  143. GpiQueryDeviceBitmapFormats(hpsBitmap, cFormats    * 2L,
  144.                 plFormats =    (PLONG)malloc(2UL * cFormats * sizeof(LONG)));
  145.  
  146.                /* Create actual    bitmap storage for colour wheel    */
  147.                /* having the default plane and bit count    */
  148. ptl.x =    cx;
  149. ptl.y =    cy;
  150. GpiConvert(hpsBitmap, CVTC_DEFAULTPAGE,    CVTC_DEVICE, 1L, &ptl);
  151. memset(&bminfo2, 0, sizeof(BITMAPINFOHEADER2));
  152. bminfo2.cbFix      = sizeof(BITMAPINFOHEADER2);
  153. bminfo2.cx      = (ULONG)ptl.x;
  154. bminfo2.cy      = (ULONG)ptl.y;
  155. bminfo2.cPlanes      = (USHORT)plFormats[0];
  156. bminfo2.cBitCount = (USHORT)plFormats[1];
  157.  
  158. free(plFormats);
  159.  
  160. if ( !(hbmConstruct = GpiCreateBitmap(hpsBitmap, &bminfo2, 0L, 0L, 0L))    )
  161.    {
  162.                /* Error    occurred during    creation of bitmap    */
  163.                /* storage, destroy presentation    space created    */
  164.                /* and close device context opened        */
  165.  
  166.    GpiDestroyPS(hpsBitmap);
  167.    DevCloseDC(hDC);
  168.    return((HBITMAP)NULL);
  169.    }
  170.                /* Set bitmap as    current    bitmap to use        */
  171.  
  172. GpiSetBitmap(hpsBitmap,    hbmConstruct);
  173.  
  174.                /* Draw the page                    */
  175. GpiErase(hpsBitmap);
  176. if ( fQuerySelectedFont(WinWindowFromID(hwndToolBar, CBX_FONT),
  177.             WinWindowFromID(hwndToolBar, CBX_FONTSIZE), &fselPreview) )
  178.    {
  179.    BuildFontCache(hpsBitmap, hpsPrinter, hdcPrinter,
  180.           fselPreview.szFacename, fselPreview.lNominalPointSize,
  181.           &fc);
  182.    DrawCommon(hpsBitmap, hpsPrinter, &fc,
  183.           (LONG)(1440.0 * pags.rdLeftMargin),
  184.           (LONG)(1440.0 * pags.rdBottomMargin),
  185.           (LONG)(1440.0 * pags.rdRightMargin),
  186.           (LONG)(1440.0 * pags.rdTopMargin),
  187.           pags.szHeader, pags.szFooter,
  188.           TRUE);
  189.  
  190.    DeleteFontCache(hpsBitmap, &fc);
  191.    }
  192.                /* Set the bitmap to allow completion of    bitmap    */
  193.                /* in memory                    */
  194.  
  195. GpiSetBitmap(hpsBitmap,    (HDC)NULL);
  196.  
  197.                /* Destroy the memory device context        */
  198.  
  199. GpiAssociate(hpsBitmap,    (HDC)NULL);
  200.  
  201.                /* Destroy the presentation spaces used        */
  202. GpiDestroyPS(hpsBitmap);
  203. DevCloseDC(hDC);
  204.                /* Return the bitmap handle that    will be    used in    */
  205.                /* painting the image on    the window        */
  206. return(hbmConstruct);
  207. }
  208. #pragma    subtitle("   Module Purpose - Dialogue Procedure")
  209. #pragma    page( )
  210.  
  211. /* --- PrintPreviewDlgProc ----------------------------- [ Public ] ---    */
  212. /*                                    */
  213. /*     This function is    used to    process    the messages for the dialogue    */
  214. /*     procedure.                            */
  215. /*                                    */
  216. /*     Upon Entry:                            */
  217. /*                                    */
  218. /*     HWND   hWnd; = Dialog Window Handle                */
  219. /*     ULONG  msg;  = PM Message                    */
  220. /*     MPARAM mp1;  = Message Parameter    1                */
  221. /*     MPARAM mp2;  = Message Parameter    2                */
  222. /*                                    */
  223. /*     Upon Exit:                            */
  224. /*                                    */
  225. /*     PrintPreviewDlgProc = Message Handling Result            */
  226. /*                                    */
  227. /* --------------------------------------------------------------------    */
  228.  
  229. MRESULT    EXPENTRY PrintPreviewDlgProc(HWND hWnd,    ULONG msg, MPARAM mp1, MPARAM mp2)
  230.  
  231. {
  232. HDC    hdcPrinter;           /* Printer Device Context Handle    */
  233. HPS    hPS;               /* Presentation Space Handle        */
  234. LONG    cxDlg;               /* Dialogue Width            */
  235. LONG    cyDlg;               /* Dialogue Height            */
  236. MRESULT    mr;               /* Message Result            */
  237. SIZEL    sizlPage;           /* Page Size    Holder            */
  238. SWP    swp;               /* Window Position            */
  239.  
  240. switch ( msg )
  241.    {
  242.             /* Perform dialog initialization        */
  243.    case    WM_INITDLG :
  244.        if ( (hdcPrinter    = PrnOpenInfoDC(&prn, "PM_Q_STD")) != (HDC)NULL    )
  245.        {
  246.                /* Get the size and position of the dialogue    */
  247.  
  248.        WinQueryWindowPos(hWnd, (PSWP)&swp);
  249.        cxDlg = swp.cx;
  250.        cyDlg = swp.cy;
  251.  
  252.                /* Create a presentation    space into which the    */
  253.                /* printing will    occur using TWIPS as the unit    */
  254.                /* of measure                    */
  255.  
  256.        sizlPage.cx = sizlPage.cy = 0L;
  257.  
  258.                /* Determine the    size of    the output area        */
  259.  
  260.        GpiQueryPS(hPS = GpiCreatePS(hAB, hdcPrinter, &sizlPage, PU_TWIPS |
  261.                     GPIF_DEFAULT | GPIT_NORMAL | GPIA_ASSOC),
  262.               &sizlPage);
  263.  
  264.                /* Create the preview bitmap            */
  265.  
  266.        hbmPreview =    hbmCreatePreview(sizlPage.cx, sizlPage.cy, hPS,    hdcPrinter);
  267.  
  268.                /* Convert the image size from twips to pels    */
  269.  
  270.        GpiConvert(hPS, CVTC_DEFAULTPAGE, CVTC_DEVICE, 1L, (PPOINTL)(PVOID)&sizlPage);
  271.  
  272.                /* Set the starting position of the bitmap    */
  273.                /* image    for the    lower left corner of the    */
  274.                /* dialogue at position 5, 5            */
  275.  
  276.        rclDest.xLeft = rclDest.yBottom = 5L;
  277.        WinMapDlgPoints(HWND_DESKTOP, (PPOINTL)(PVOID)&rclDest, 1UL,    TRUE);
  278.  
  279.                /* Get the size and location of the Print push    */
  280.                /* button                    */
  281.  
  282.        WinQueryWindowPos(WinWindowFromID(hWnd, DID_OK), &swp);
  283.  
  284.                /* Form the right and top edge of the preview    */
  285.                /* image                        */
  286.  
  287.        rclDest.xRight = swp.x - rclDest.xLeft;
  288.        rclDest.yTop    = swp.y    + swp.cy;
  289.  
  290.                /* Determine the    size of    the final image    along    */
  291.                /* with the positions of    the push buttons    */
  292.  
  293.        if (    (sizlPage.cx / (rclDest.xRight - rclDest.xLeft)) >
  294.         (sizlPage.cy / (rclDest.yTop - rclDest.yBottom)) )
  295.            {
  296.                /* Landscape mode, resize and psoition things    */
  297.  
  298.            rclDest.yTop = sizlPage.cy / (lScale = (sizlPage.cx / (rclDest.xRight - rclDest.xLeft)) + 1L);
  299.            cyDlg = rclDest.yTop + rclDest.yBottom +    WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR);
  300.            WinSetWindowPos(WinWindowFromID(hWnd, DID_OK), HWND_TOP,
  301.                    swp.x, rclDest.yTop - swp.cy,
  302.                    0L, 0L, SWP_MOVE);
  303.            WinSetWindowPos(WinWindowFromID(hWnd, DID_CANCEL), HWND_TOP,
  304.                    swp.x, rclDest.yTop - swp.cy - 10L - swp.cy,
  305.                    0L, 0L, SWP_MOVE);
  306.            }
  307.        else
  308.            {
  309.                /* Portrait mode, resize    and psoition things    */
  310.  
  311.            rclDest.xRight =    sizlPage.cx / (lScale =    (sizlPage.cy / (rclDest.yTop - rclDest.yBottom)) + 1L);
  312.            cxDlg = rclDest.xRight +    (rclDest.xLeft * 2L) + swp.cx;
  313.            WinSetWindowPos(WinWindowFromID(hWnd, DID_OK), HWND_TOP,
  314.                    rclDest.xRight +    rclDest.xLeft, swp.y,
  315.                    0L, 0L, SWP_MOVE);
  316.            WinSetWindowPos(WinWindowFromID(hWnd, DID_CANCEL), HWND_TOP,
  317.                    rclDest.xRight +    rclDest.xLeft, swp.y - 10L - swp.cy,
  318.                    0L, 0L, SWP_MOVE);
  319.            }
  320.                /* Set the page boundaries points        */
  321.  
  322.        aptlArea[0].x = (LONG)(1440.0 * pags.rdLeftMargin);
  323.        aptlArea[0].y = (LONG)(1440.0 * pags.rdBottomMargin)    / 2L;
  324.        aptlArea[1].x = (LONG)(1440.0 * pags.rdLeftMargin);
  325.        aptlArea[1].y = (LONG)(1440.0 * pags.rdTopMargin) / 2L;
  326.  
  327.        aptlArea[2].x = (LONG)(1440.0 * pags.rdRightMargin);
  328.        aptlArea[2].y = (LONG)(1440.0 * pags.rdTopMargin) / 2L;
  329.        aptlArea[3].x = (LONG)(1440.0 * pags.rdRightMargin);
  330.        aptlArea[3].y = (LONG)(1440.0 * pags.rdBottomMargin)    / 2L;
  331.  
  332.        aptlArea[4].x = (LONG)(1440.0 * pags.rdLeftMargin) /    2L;
  333.        aptlArea[4].y = (LONG)(1440.0 * pags.rdBottomMargin);
  334.        aptlArea[5].x = (LONG)(1440.0 * pags.rdRightMargin) / 2L;
  335.        aptlArea[5].y = (LONG)(1440.0 * pags.rdBottomMargin);
  336.  
  337.        aptlArea[6].x = (LONG)(1440.0 * pags.rdLeftMargin) /    2L;
  338.        aptlArea[6].y = (LONG)(1440.0 * pags.rdTopMargin);
  339.        aptlArea[7].x = (LONG)(1440.0 * pags.rdRightMargin) / 2L;
  340.        aptlArea[7].y = (LONG)(1440.0 * pags.rdTopMargin);
  341.  
  342.                /* Convert the boundary points to pels from    */
  343.                /* twips                        */
  344.  
  345.        GpiConvert(hPS, CVTC_DEFAULTPAGE, CVTC_DEVICE, 8L, aptlArea);
  346.  
  347.                /* Release the presentation space and printer    */
  348.                /* device context                */
  349.  
  350.        GpiAssociate(hPS, (HDC)NULL);
  351.        GpiDestroyPS(hPS);
  352.        DevCloseDC(hdcPrinter);
  353.  
  354.                /* Form the final boundary points        */
  355.  
  356.        aptlArea[0].x = rclDest.xLeft   + aptlArea[0].x / lScale;
  357.        aptlArea[0].y = rclDest.yBottom + aptlArea[0].y / lScale;
  358.        aptlArea[1].x = rclDest.xLeft   + aptlArea[1].x / lScale;
  359.        aptlArea[1].y = rclDest.yTop       - aptlArea[1].y / lScale;
  360.  
  361.        aptlArea[2].x = rclDest.xRight  - aptlArea[2].x / lScale;
  362.        aptlArea[2].y = rclDest.yTop       - aptlArea[2].y / lScale;
  363.        aptlArea[3].x = rclDest.xRight  - aptlArea[3].x / lScale;
  364.        aptlArea[3].y = rclDest.yBottom + aptlArea[3].y / lScale;
  365.  
  366.        aptlArea[4].x = rclDest.xLeft   + aptlArea[4].x / lScale;
  367.        aptlArea[4].y = rclDest.yBottom + aptlArea[4].y / lScale;
  368.        aptlArea[5].x = rclDest.xRight  - aptlArea[5].x / lScale;
  369.        aptlArea[5].y = rclDest.yBottom + aptlArea[5].y / lScale;
  370.  
  371.        aptlArea[6].x = rclDest.xLeft   + aptlArea[6].x / lScale;
  372.        aptlArea[6].y = rclDest.yTop       - aptlArea[6].y / lScale;
  373.        aptlArea[7].x = rclDest.xRight  - aptlArea[7].x / lScale;
  374.        aptlArea[7].y = rclDest.yTop       - aptlArea[7].y / lScale;
  375.  
  376.        aptlShade[0].x = rclDest.xLeft;
  377.        aptlShade[0].y = rclDest.yTop;
  378.        aptlShade[1].x = rclDest.xRight + 2L;
  379.        aptlShade[1].y = rclDest.yTop;
  380.        aptlShade[2].x = rclDest.xRight + 2L;
  381.        aptlShade[2].y = rclDest.yBottom - 2L;
  382.        aptlShade[3].x = rclDest.xLeft;
  383.        aptlShade[3].y = rclDest.yBottom - 2L;
  384.  
  385.                /* Center the dialogue within the display    */
  386.  
  387.        WinQueryWindowPos(hWnd, (PSWP)&swp);
  388.        WinSetWindowPos(hWnd, HWND_TOP,
  389.                (WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN)    - swp.cx) / 2L,
  390.                (WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN)    - swp.cy) / 2L,
  391.                cxDlg, cyDlg, SWP_MOVE | SWP_SIZE);
  392.        }
  393.        else
  394.                /* Printer device open failed            */
  395.  
  396.        WinDismissDlg(hWnd, FALSE);
  397.        break;
  398.             /* Process push    button selections        */
  399.    case    WM_COMMAND :
  400.     switch ( SHORT1FROMMP(mp1) )
  401.         {
  402.                /* Print    push button selected            */
  403.        case    DID_OK :
  404.                /* Delete the preview bitmap            */
  405.  
  406.            GpiDeleteBitmap(hbmPreview);
  407.            WinDismissDlg(hWnd, TRUE);
  408.            break;
  409.                /* Cancel push button selected            */
  410.  
  411.        case    DID_CANCEL :
  412.                /* Delete the preview bitmap            */
  413.  
  414.            GpiDeleteBitmap(hbmPreview);
  415.            WinDismissDlg(hWnd, FALSE);
  416.            break;
  417.         }
  418.     break;
  419.                /* Close    requested, exit    dialogue        */
  420.    case    WM_CLOSE :
  421.                /* Delete the preview bitmap            */
  422.  
  423.        GpiDeleteBitmap(hbmPreview);
  424.        WinDismissDlg(hWnd, FALSE);
  425.        break;
  426.                /* Paint    required                */
  427.    case    WM_PAINT :
  428.        mr = WinDefDlgProc(hWnd,    msg, mp1, mp2);
  429.        WinDrawBitmap(hPS = WinGetPS(hWnd), hbmPreview,
  430.              (PRECTL)NULL, (PPOINTL)(PVOID)&rclDest, 0L, 0L,
  431.              DBM_NORMAL    | DBM_STRETCH);
  432.  
  433.        GpiSetColor(hPS,    RGBCLR_PALEGRAY);
  434.  
  435.                /* Set up the starting position and start    */
  436.                /* drawing the lighted edge of the window    */
  437.  
  438.        GpiPolyLineDisjoint(hPS,    8L, aptlArea);
  439.  
  440.                /* Get the presentation space and set the first    */
  441.                /* outline edge colour to white            */
  442.  
  443.        GpiCreateLogColorTable(hPS, 0L, LCOLF_RGB, 0L, 0L, (PLONG)NULL);
  444.        GpiSetColor(hPS,    RGBCLR_SHADOW);
  445.  
  446.                /* Set up the starting position and start    */
  447.                /* drawing the lighted edge of the window    */
  448.  
  449.        GpiMove(hPS, &aptlShade[3]);
  450.        GpiPolyLine(hPS,    2L, aptlShade);
  451.  
  452.                /* Set the shaded edge colour and start the    */
  453.                /* drawing of the shaded    edge of    the window    */
  454.  
  455.        GpiSetColor(hPS,    RGB_WHITE);
  456.        GpiPolyLine(hPS,    2L, &aptlShade[2]);
  457.        WinReleasePS(hPS);
  458.        return(mr);
  459.             /* Pass    through    unhandled messages        */
  460.    default :
  461.        return(WinDefDlgProc(hWnd, msg, mp1, mp2));
  462.    }
  463. return(0L);
  464. }
  465.