home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / prtsampl.zip / PRTPAGE.C < prev    next >
C/C++ Source or Header  |  1998-04-20  |  29KB  |  817 lines

  1. /**************************************************************************
  2.  *  File name  :  prtpage.c
  3.  *
  4.  *  Description:  This module contains all the functions needed to display
  5.  *                the page setup dialog
  6.  *
  7.  *                This source file contains the following functions:
  8.  *
  9.  *                MMtoFixedInch( lMM )
  10.  *                PageDlgProc( hwnd, msg, mp1, mp2 )
  11.  *                QueryDlgItemFixed( hwnd, idItem, pfxResult )
  12.  *                SetDlgItemFixed( hwnd, idItem, fxValue )
  13.  *                VerifyFixedLimits( fxValue, fxMin, fxMax )
  14.  *
  15.  *  Concepts   :  Forms, page margins
  16.  *
  17.  *  API's      :  DosBeep
  18.  *                WinInsertLboxItem
  19.  *                WinQueryLboxSelectedItem
  20.  *                WinSendMsg
  21.  *                WinSetWindowULong
  22.  *                WinShowWindow
  23.  *                WinWindowFromID
  24.  *
  25.  *    Files    :  OS2.H, PRTSAMP.H, PRTSDLG.H, PRTSHLP.H, PMASSERT.H
  26.  *
  27.  *  Copyright (C) 1991-1993 IBM Corporation
  28.  *
  29.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  30.  *      sample code created by IBM Corporation. This sample code is not
  31.  *      part of any standard or IBM product and is provided to you solely
  32.  *      for  the purpose of assisting you in the development of your
  33.  *      applications.  The code is provided "AS IS", without
  34.  *      warranty of any kind.  IBM shall not be liable for any damages
  35.  *      arising out of your use of the sample code, even if they have been
  36.  *      advised of the possibility of such damages.                                                    *
  37.  *************************************************************************/
  38.  
  39. /* Include the required sections from the PM header file.  */
  40. #define INCL_DEV
  41. #define INCL_DEVERRORS
  42. #define INCL_GPIBITMAPS
  43. #define INCL_GPICONTROL
  44. #define INCL_GPILCIDS
  45. #define INCL_GPIMETAFILES
  46. #define INCL_GPITRANSFORMS
  47. #define INCL_SPL
  48. #define INCL_SPLDOSPRINT
  49. #define INCL_WINDIALOGS
  50. #define INCL_WINERRORS
  51. #define INCL_WINENTRYFIELDS
  52. #define INCL_WINHELP
  53. #define INCL_WINLISTBOXES
  54. #define INCL_WINSTDFILE
  55. #define INCL_WINSTDFONT
  56. #define INCL_WINWINDOWMGR
  57. #include <os2.h>
  58.  
  59. /* c language includes */
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include <ctype.h>
  64. #include <stddef.h>
  65. #include <process.h>
  66. #include <memory.h>
  67. #include <sys\types.h>
  68. #include <sys\stat.h>
  69.  
  70. /* application includes */
  71. #include "prtsamp.h"
  72. #include "prtsdlg.h"
  73. #include "pmassert.h"
  74. #include "prtshlp.h"
  75.  
  76.  
  77. /* static function prototypes */
  78.  
  79. FIXED MMtoFixedInch( LONG lMM );
  80. MRESULT EXPENTRY PageDlgProc(HWND, USHORT, MPARAM, MPARAM);
  81. BOOL QueryDlgItemFixed( HWND hwnd, ULONG idItem, PFIXED pfxResult);
  82. BOOL SetDlgItemFixed( HWND hwnd, ULONG idItem, FIXED fxValue);
  83. FIXED VerifyFixedLimits( FIXED fxValue, FIXED fxMin, FIXED fxMax );
  84.  
  85.  
  86. /*************************************************************************
  87.  *  Function   :  PageDlgProc
  88.  *
  89.  *  Description:  This function is the dialog window procedure for page setup
  90.  *
  91.  *************************************************************************/
  92. MRESULT EXPENTRY PageDlgProc( HWND hwnd,
  93.                               USHORT msg,
  94.                               MPARAM mp1,
  95.                               MPARAM mp2 )
  96. {
  97.    static PMAIN_PARM   pmp;
  98.    SHORT               i, index;
  99.    PSZ                 psz;
  100.    HAB                 hab;
  101.    HWND                hwndListbox;
  102.    ULONG               ulPanel;
  103.    FIXED               fxMargin, fxOther;
  104.    BOOL                bOK;
  105.    FIXED               fxWidth, fxHeight, fxLeft, fxRight, fxTop, fxBottom;
  106.  
  107.    switch (msg)
  108.    {
  109.    case WM_INITDLG:
  110.       /* get application specific data and store in window ULONG */
  111.       pmp = (PMAIN_PARM) mp2;
  112.       WinSetWindowULong( hwnd, QWL_USER, (ULONG) mp2 );
  113.       hab = pmp->hab;
  114.  
  115.       /*
  116.        * Hide the duplex control.  The reason it is included in the
  117.        * print sample DLG file is for the benefit of programmers who
  118.        * want their dialogs to look exactly like those presented in
  119.        * the Programming Guide for printing (Chapter 18).
  120.        */
  121.       WinShowWindow(WinWindowFromID(hwnd, IDD_DUPLEX), FALSE);
  122.  
  123.       /* fill listbox with form names */
  124.       hwndListbox = WinWindowFromID( hwnd, IDD_FORM );
  125.       pmassert( hab, hwndListbox );
  126.  
  127.       for (i = 0; i < pmp->cForms; ++i)
  128.       {
  129.          psz = pmp->hcinfo[i].szFormname;
  130.          index = WinInsertLboxItem( hwndListbox, LIT_END, psz );
  131.  
  132.          /* preselect this one? */
  133.          if (! strcmp( psz, pmp->form.achFormName ) )
  134.          {
  135.             WinSendMsg( hwndListbox, LM_SELECTITEM,
  136.                                             (MPARAM)index, (MPARAM)TRUE );
  137.          }
  138.       }
  139.  
  140.       /* one must be selected */
  141.       index = WinQueryLboxSelectedItem( hwndListbox );
  142.       pmassert( hab, index != LIT_NONE );
  143.  
  144.       /* fill entry fields with user margins */
  145.       bOK = SetDlgItemFixed(hwnd, IDD_MARGIN_LEFT, pmp->form.fxLeftMargin );
  146.       pmassert( hab, bOK );
  147.       bOK = SetDlgItemFixed(hwnd, IDD_MARGIN_RIGHT, pmp->form.fxRightMargin );
  148.       pmassert( hab, bOK );
  149.       bOK = SetDlgItemFixed(hwnd, IDD_MARGIN_TOP, pmp->form.fxTopMargin );
  150.       pmassert( hab, bOK );
  151.       bOK = SetDlgItemFixed(hwnd, IDD_MARGIN_BOTTOM, pmp->form.fxBottomMargin );
  152.       pmassert( hab, bOK );
  153.  
  154.       return FALSE;
  155.  
  156.    case WM_HELP:
  157.       /* Get pointer to application specific data */
  158.       pmp = (PMAIN_PARM)WinQueryWindowULong( hwnd, QWL_USER );
  159.  
  160.       if( pmp->hwndHelp )
  161.       {
  162.          ulPanel = PANEL_QUERYFORM;
  163.          WinSendMsg( pmp->hwndHelp, HM_DISPLAY_HELP,
  164.                      (MPARAM)&ulPanel, (MPARAM)HM_RESOURCEID );
  165.          return (MRESULT)NULL;
  166.       }
  167.       break;
  168.  
  169.  
  170.    case WM_CONTROL:
  171.       /* Get pointer to application specific data */
  172.       pmp = (PMAIN_PARM)WinQueryWindowULong( hwnd, QWL_USER );
  173.       hab = pmp->hab;
  174.  
  175.       /* initialize clip limits in fixed inch units */
  176.       index = pmp->iForm;
  177.       fxWidth = MMtoFixedInch( pmp->hcinfo[index].cx );
  178.       fxHeight = MMtoFixedInch( pmp->hcinfo[index].cy );
  179.       fxLeft = MMtoFixedInch( pmp->hcinfo[index].xLeftClip );
  180.       fxRight = fxWidth - MMtoFixedInch( pmp->hcinfo[index].xRightClip );
  181.       fxTop = fxHeight - MMtoFixedInch( pmp->hcinfo[index].yTopClip );
  182.       fxBottom = MMtoFixedInch( pmp->hcinfo[index].yBottomClip );
  183.  
  184.       switch(SHORT1FROMMP(mp1))
  185.       {
  186.       case IDD_MARGIN_LEFT:
  187.  
  188.          switch (SHORT2FROMMP(mp1))
  189.          {
  190.          case EN_CHANGE:
  191.             /*
  192.              *  Verify that a fixed number has been typed in the entry field
  193.              *  If not, beep and set it back to the original value
  194.              */
  195.              if (!QueryDlgItemFixed(hwnd, IDD_MARGIN_LEFT, &fxMargin) )
  196.              {
  197.                 DosBeep(600, 5);
  198.                 bOK = SetDlgItemFixed(hwnd, IDD_MARGIN_LEFT,
  199.                                       pmp->form.fxLeftMargin );
  200.                 pmassert( hab, bOK );
  201.                 return 0;
  202.              }
  203.              break;
  204.  
  205.          case EN_KILLFOCUS:
  206.             /*
  207.              *  Verify that a fixed number has been typed in the entry field
  208.              *  and it is not outside the hardware clip limits of the printer
  209.              *  and fits in the form
  210.              */
  211.              fxOther = fxWidth- fxRight;
  212.              QueryDlgItemFixed( hwnd, IDD_MARGIN_LEFT, &fxMargin );
  213.              fxMargin = VerifyFixedLimits( fxMargin, fxLeft, fxOther );
  214.              bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_LEFT, fxMargin );
  215.              pmassert( hab, bOK );
  216.  
  217.              /* verify the right margin is still within the form */
  218.              fxOther = fxWidth - fxMargin;
  219.              QueryDlgItemFixed(hwnd, IDD_MARGIN_RIGHT, &fxMargin);
  220.              fxMargin = VerifyFixedLimits( fxMargin, fxRight, fxOther );
  221.              bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_RIGHT, fxMargin );
  222.              pmassert( hab, bOK );
  223.              break;
  224.          }
  225.          return 0;
  226.  
  227.       case IDD_MARGIN_RIGHT:
  228.  
  229.          switch (SHORT2FROMMP(mp1))
  230.          {
  231.          case EN_CHANGE:
  232.             /*
  233.              *  Verify that a fixed number has been typed in the entry field
  234.              *  If not, beep and set it back to the original value
  235.              */
  236.              if (!QueryDlgItemFixed(hwnd, IDD_MARGIN_RIGHT, &fxMargin) )
  237.              {
  238.                 bOK = SetDlgItemFixed(hwnd, IDD_MARGIN_RIGHT,
  239.                                       pmp->form.fxRightMargin );
  240.                 pmassert( hab, bOK );
  241.                 DosBeep(600, 5);
  242.                 return 0;
  243.              }
  244.              break;
  245.  
  246.          case EN_KILLFOCUS:
  247.             /*
  248.              *  Verify that a fixed number has been typed in the entry field
  249.              *  and it is not outside the hardware clip limits of the printer
  250.              *  and fits in the form
  251.              */
  252.              QueryDlgItemFixed( hwnd, IDD_MARGIN_LEFT, &fxMargin );
  253.              fxOther = fxWidth - fxMargin;
  254.              QueryDlgItemFixed(hwnd, IDD_MARGIN_RIGHT, &fxMargin);
  255.              fxMargin = VerifyFixedLimits( fxMargin, fxRight, fxOther );
  256.              bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_RIGHT, fxMargin );
  257.              pmassert( hab, bOK );
  258.              break;
  259.          }
  260.          return 0;
  261.  
  262.       case IDD_MARGIN_TOP:
  263.  
  264.          switch (SHORT2FROMMP(mp1))
  265.          {
  266.          case EN_CHANGE:
  267.             /*
  268.              *  Verify that a fixed number has been typed in the entry field
  269.              *  If not, beep and set it back to the original value
  270.              */
  271.              if (!QueryDlgItemFixed(hwnd, IDD_MARGIN_TOP, &fxMargin) )
  272.              {
  273.                 bOK = SetDlgItemFixed(hwnd, IDD_MARGIN_TOP,
  274.                                       pmp->form.fxTopMargin );
  275.                 pmassert( hab, bOK );
  276.                 DosBeep(600, 5);
  277.                 return 0;
  278.              }
  279.              break;
  280.  
  281.          case EN_KILLFOCUS:
  282.             /*
  283.              *  Verify that a fixed number has been typed in the entry field
  284.              *  and it is not outside the hardware clip limits of the printer
  285.              */
  286.              QueryDlgItemFixed(hwnd, IDD_MARGIN_BOTTOM, &fxMargin);
  287.              fxOther = fxHeight - fxMargin;
  288.              QueryDlgItemFixed( hwnd, IDD_MARGIN_TOP, &fxMargin );
  289.              fxMargin = VerifyFixedLimits( fxMargin, fxTop, fxOther );
  290.              bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_TOP, fxMargin );
  291.              pmassert( hab, bOK );
  292.              break;
  293.          }
  294.          return 0;
  295.  
  296.       case IDD_MARGIN_BOTTOM:
  297.  
  298.          switch (SHORT2FROMMP(mp1))
  299.          {
  300.          case EN_CHANGE:
  301.             /*
  302.              *  Verify that a fixed number has been typed in the entry field
  303.              *  If not, beep and set it back to the original value
  304.              */
  305.              if (!QueryDlgItemFixed(hwnd, IDD_MARGIN_BOTTOM, &fxMargin) )
  306.              {
  307.                 bOK = SetDlgItemFixed(hwnd, IDD_MARGIN_BOTTOM,
  308.                                       pmp->form.fxBottomMargin );
  309.                 pmassert( hab, bOK );
  310.                 DosBeep(600, 5);
  311.                 return 0;
  312.              }
  313.              break;
  314.  
  315.          case EN_KILLFOCUS:
  316.             /*
  317.              *  Verify that a fixed number has been typed in the entry field
  318.              *  and it is not outside the hardware clip limits of the printer
  319.              */
  320.              fxOther = fxHeight - fxBottom;
  321.              QueryDlgItemFixed( hwnd, IDD_MARGIN_BOTTOM, &fxMargin );
  322.              fxMargin = VerifyFixedLimits( fxMargin, fxBottom, fxOther );
  323.              bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_BOTTOM, fxMargin );
  324.              pmassert( hab, bOK );
  325.  
  326.              /* verify the top margin is still within the form */
  327.              fxOther = fxHeight - fxMargin;
  328.              QueryDlgItemFixed( hwnd, IDD_MARGIN_TOP, &fxMargin );
  329.              fxMargin = VerifyFixedLimits( fxMargin, fxTop, fxOther );
  330.              bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_TOP, fxMargin );
  331.              pmassert( hab, bOK );
  332.              break;
  333.          }
  334.          return 0;
  335.  
  336.       case IDD_FORM:
  337.          /*
  338.           * Check if a new form has been selected. Choose the maximum of
  339.           * the current margin and the form clip limit and show the new
  340.           * margins. The actual form is not changed until the user presses
  341.           * OK to ensure that CANCEL still works.
  342.           */
  343.          if (SHORT2FROMMP(mp1) == LN_SELECT)
  344.          {
  345.             hwndListbox = WinWindowFromID( hwnd, IDD_FORM );
  346.             pmassert( hab, hwndListbox );
  347.             index = (SHORT) WinSendMsg( hwndListbox, LM_QUERYSELECTION,
  348.                                     (MPARAM)LIT_FIRST, 0 );
  349.             pmassert( hab, index != LIT_NONE );
  350.  
  351.             /* reset clip limits to new form */
  352.             fxWidth = MMtoFixedInch( pmp->hcinfo[index].cx );
  353.             fxHeight = MMtoFixedInch( pmp->hcinfo[index].cy );
  354.             fxLeft = MMtoFixedInch( pmp->hcinfo[index].xLeftClip );
  355.             fxRight = fxWidth - MMtoFixedInch( pmp->hcinfo[index].xRightClip );
  356.             fxTop = fxHeight - MMtoFixedInch( pmp->hcinfo[index].yTopClip );
  357.             fxBottom = MMtoFixedInch( pmp->hcinfo[index].yBottomClip );
  358.  
  359.             bOK = QueryDlgItemFixed(hwnd, IDD_MARGIN_LEFT, &fxMargin);
  360.             pmassert( hab, bOK );
  361.             bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_LEFT,
  362.                                    max( fxMargin, fxLeft ) );
  363.             pmassert( hab, bOK );
  364.  
  365.             bOK = QueryDlgItemFixed(hwnd, IDD_MARGIN_RIGHT, &fxMargin);
  366.             pmassert( hab, bOK );
  367.             bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_RIGHT,
  368.                                    max( fxMargin, fxRight ) );
  369.             pmassert( hab, bOK );
  370.  
  371.             bOK = QueryDlgItemFixed(hwnd, IDD_MARGIN_TOP, &fxMargin);
  372.             pmassert( hab, bOK );
  373.             bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_TOP,
  374.                                    max( fxMargin, fxTop ) );
  375.             pmassert( hab, bOK );
  376.  
  377.             bOK = QueryDlgItemFixed(hwnd, IDD_MARGIN_BOTTOM, &fxMargin);
  378.             pmassert( hab, bOK );
  379.             bOK = SetDlgItemFixed( hwnd, IDD_MARGIN_BOTTOM,
  380.                                    max( fxMargin, fxBottom ) );
  381.             pmassert( hab, bOK );
  382.          }
  383.          return 0;
  384.  
  385.       case IDD_DUPLEX:
  386.          /* duplex control is not used in this sample program */
  387.          return 0;
  388.       }
  389.       break;
  390.  
  391.  
  392.    case WM_COMMAND:
  393.       /* Get pointer to application specific data */
  394.       pmp = (PMAIN_PARM)WinQueryWindowULong( hwnd, QWL_USER );
  395.       hab = pmp->hab;
  396.  
  397.  
  398.       switch(SHORT1FROMMP(mp1))
  399.       {
  400.  
  401.       case DID_OK:
  402.  
  403.          /* retrieve form chosen by user */
  404.          hwndListbox = WinWindowFromID( hwnd, IDD_FORM );
  405.          pmassert( hab, hwndListbox );
  406.          index = WinQueryLboxSelectedItem( hwndListbox );
  407.          pmassert( hab, index != LIT_NONE );
  408.          pmp->iForm = index;
  409.          strcpy( pmp->form.achFormName, pmp->hcinfo[index].szFormname );
  410.  
  411.          /*
  412.           * Get each margin and check that it is within the clip and
  413.           * form limits. Assign each margin to the form data structure
  414.           *
  415.           * Once the left and top margins are evaluated, the maximum value
  416.           * for the right and bottom margins is defined as whatever is
  417.           * left over in the form
  418.           */
  419.  
  420.          fxWidth = MMtoFixedInch( pmp->hcinfo[index].cx );
  421.          fxHeight = MMtoFixedInch( pmp->hcinfo[index].cy );
  422.          fxLeft = MMtoFixedInch( pmp->hcinfo[index].xLeftClip );
  423.          fxRight = fxWidth - MMtoFixedInch( pmp->hcinfo[index].xRightClip );
  424.          fxTop = fxHeight - MMtoFixedInch( pmp->hcinfo[index].yTopClip );
  425.          fxBottom = MMtoFixedInch( pmp->hcinfo[index].yBottomClip );
  426.  
  427.          fxOther = fxWidth - fxRight;
  428.          bOK = QueryDlgItemFixed(hwnd, IDD_MARGIN_LEFT, &fxMargin);
  429.          pmassert( hab, bOK );
  430.          pmp->form.fxLeftMargin = VerifyFixedLimits( fxMargin, fxLeft, fxOther );
  431.  
  432.          fxOther = fxWidth - pmp->form.fxLeftMargin;
  433.          bOK = QueryDlgItemFixed(hwnd, IDD_MARGIN_RIGHT, &fxMargin);
  434.          pmassert( hab, bOK );
  435.          pmp->form.fxRightMargin = VerifyFixedLimits( fxMargin, fxRight, fxOther );
  436.  
  437.          fxOther = fxHeight - fxBottom;
  438.          bOK = QueryDlgItemFixed(hwnd, IDD_MARGIN_BOTTOM, &fxMargin);
  439.          pmassert( hab, bOK );
  440.          pmp->form.fxBottomMargin = VerifyFixedLimits( fxMargin, fxBottom, fxOther );
  441.  
  442.          fxOther = fxHeight - pmp->form.fxBottomMargin;
  443.          bOK = QueryDlgItemFixed(hwnd, IDD_MARGIN_TOP, &fxMargin);
  444.          pmassert( hab, bOK );
  445.          pmp->form.fxTopMargin = VerifyFixedLimits( fxMargin, fxTop, fxOther );
  446.  
  447.          WinDismissDlg(hwnd, DID_OK);
  448.          return 0;
  449.  
  450.       case DID_CANCEL:
  451.          WinDismissDlg(hwnd, DID_CANCEL);
  452.          return (MRESULT)NULL;
  453.       }
  454.       break;
  455.  
  456.    }
  457.    return( WinDefDlgProc(hwnd, msg, mp1, mp2) );
  458. } /* End of QueryPrintDlgProc */
  459.  
  460.  
  461. /*************************************************************************
  462.  *  Function   :  QueryPrinterForms
  463.  *
  464.  *  Description:  This function queries the forms that are available in the
  465.  *                printer or returns some default forms.
  466.  *                It uses DevPostDeviceModes API.
  467.  *
  468.  *  API's      :  DevQueryHardcopyCaps
  469.  *
  470.  *************************************************************************/
  471. LONG QueryPrinterForms( PMAIN_PARM pmp, HCINFO hcinfo[], ULONG cMaxForms )
  472. {
  473.    LONG cForms;
  474.  
  475.    /* if printer is defined, then query forms using DevPostDeviceModes */
  476.    if (pmp->hpsPrinterInfo)
  477.    {
  478.        pmassert( pmp->hab, pmp->hdcPrinterInfo );
  479.        cForms = DevQueryHardcopyCaps( pmp->hdcPrinterInfo, 0L,
  480.                                       cMaxForms, hcinfo );
  481.        pmassert( pmp->hab, pmp->cForms != DQHC_ERROR );
  482.    }
  483.    else
  484.    {
  485.       /* hard-code some default forms */
  486.       cForms = 0;
  487.  
  488.       strcpy( hcinfo[cForms].szFormname, "Letter" );
  489.       hcinfo[cForms].cx = 216;
  490.       hcinfo[cForms].cy = 279;
  491.       hcinfo[cForms].flAttributes = HCAPS_CURRENT | HCAPS_SELECTABLE;
  492.       hcinfo[cForms].xLeftClip = 10;
  493.       hcinfo[cForms].yBottomClip = 10;
  494.       hcinfo[cForms].xRightClip = 206;
  495.       hcinfo[cForms].yTopClip = 269;
  496.       cForms++;
  497.  
  498.       strcpy( hcinfo[cForms].szFormname, "A4" );
  499.       hcinfo[cForms].cx = 211;
  500.       hcinfo[cForms].cy = 297;
  501.       hcinfo[cForms].flAttributes = 0;
  502.       hcinfo[cForms].xLeftClip = 6;
  503.       hcinfo[cForms].yBottomClip = 10;
  504.       hcinfo[cForms].xRightClip = 205;
  505.       hcinfo[cForms].yTopClip = 287;
  506.       cForms++;
  507.    }
  508.  
  509.    return cForms;
  510. } /* End of QueryPrinterForms */
  511.  
  512.  
  513. /*************************************************************************
  514.  *  Function   :  MMtoFixedInch
  515.  *
  516.  *  Description:  Convert Millimetres to Inches in FIXED format
  517.  *
  518.  *  Parameters :  LONG       millimetres
  519.  *
  520.  *  API's      :  [none]
  521.  *
  522.  *  Return     :  FIXED      inches
  523.  *
  524.  *************************************************************************/
  525. FIXED MMtoFixedInch( LONG lMM )
  526. {
  527.    USHORT integer;
  528.    USHORT fractional;
  529.  
  530.    integer = (USHORT)( ((float)(lMM)) / 25.4 );
  531.    fractional = (USHORT)( (((float)(lMM)) / 25.4 - integer) * 0x10000 + 0.5 );
  532.    return MAKEFIXED(integer, fractional);
  533.  
  534. } /* end of MMtoFixedInch */
  535.  
  536.  
  537.  
  538. /*************************************************************************
  539.  *  Function   :  QueryDlgItemFixed
  540.  *
  541.  *  Description:  Query a dialog item for a fixed value
  542.  *
  543.  *  Parameters :  HWND       dialog window handle
  544.  *                ULONG      dialog item to query
  545.  *                PFIXED     value queried from dialog item
  546.  *
  547.  *  API's      :  WinQueryDlgItemText
  548.  *
  549.  *  Return     :  BOOL (TRUE means a FIXED value was found)
  550.  *
  551.  *************************************************************************/
  552. BOOL QueryDlgItemFixed( HWND hwnd, ULONG idItem, PFIXED pfxResult)
  553. {
  554.    CHAR  sz[20];
  555.    float Result;
  556.    SHORT integer;
  557.    SHORT fractional;
  558.  
  559.    *pfxResult = MAKEFIXED( 0, 0 );
  560.    WinQueryDlgItemText (hwnd, idItem, 20, sz );
  561.    if (! sz)
  562.       return FALSE;
  563.    Result = atof( sz );
  564.    integer = (SHORT) Result;
  565.    fractional = (SHORT)( (Result - ((float)integer)) * 0x10000 + 0.5 );
  566.    *pfxResult = MAKEFIXED( integer, fractional );
  567.    return TRUE;
  568. } /* end of QueryDlgItemFixed */
  569.  
  570.  
  571. /*************************************************************************
  572.  *  Function   :  SetDlgItemFixed
  573.  *
  574.  *  Description:  Set a dialog item to a fixed value
  575.  *
  576.  *  Parameters :  HWND       dialog window handle
  577.  *                ULONG      dialog item to query
  578.  *                FIXED      value to set in dialog item
  579.  *
  580.  *  API's      :  WinSetDlgItemText
  581.  *
  582.  *  Return     :  BOOL (TRUE means function was successful)
  583.  *
  584.  *************************************************************************/
  585. BOOL SetDlgItemFixed( HWND hwnd, ULONG idItem, FIXED fxValue)
  586. {
  587.    CHAR  sz[20];
  588.    BOOL  bResult;
  589.  
  590.    sprintf(sz, "%-3.2f",
  591.            ((float)FIXEDINT(fxValue) + (float)FIXEDFRAC(fxValue) / 0x10000 ) );
  592.    bResult = WinSetDlgItemText( hwnd, idItem, sz);
  593.    return bResult;
  594. } /* end of SetDlgItemFixed */
  595.  
  596.  
  597. /*************************************************************************
  598.  *  Function   :  VerifyFixedLimits
  599.  *
  600.  *  Description:  Verify that a fixed value in within a min and max limit
  601.  *                and return the nearest limit if outside
  602.  *
  603.  *  Parameters :  FIXED      value to verify
  604.  *                FIXED      minimum limit
  605.  *                FIXED      maximum limit
  606.  *
  607.  *  API's      :  [none]
  608.  *
  609.  *  Return     :  FIXED      closest limit or value if within limits
  610.  *
  611.  *************************************************************************/
  612. FIXED VerifyFixedLimits( FIXED fxValue, FIXED fxMin, FIXED fxMax )
  613. {
  614.    if ( fxValue < fxMin )
  615.    {
  616.       return fxMin;
  617.    }
  618.    if ( fxValue > fxMax )
  619.    {
  620.       return fxMax;
  621.    }
  622.    return fxValue;
  623. } /* end of VerifyFixedLimits */
  624.  
  625.  
  626.  
  627. /*************************************************************************
  628.  *  Function   :  FindForm
  629.  *
  630.  *  Description:  Finds a form name in an array of form structures
  631.  *                Only the first word of the form names are compared so that
  632.  *                "Letter" and "Letter (8.5 x 11)" are considered the same
  633.  *                This routine cannot coope with the difference between
  634.  *                "EXEC" and "EXECUTIVE" or "10" and "COM-10"
  635.  *
  636.  *  Parameters :  PSZ        value to form name of find
  637.  *                PHCINFO    array of form data structures
  638.  *                LONG       number of forms for comparison
  639.  *
  640.  *  API's      :  [none]
  641.  *
  642.  *  Return     :  LONG       index of form name found or -1 if not found
  643.  *
  644.  *************************************************************************/
  645. LONG FindForm( PSZ pszFormName, PHCINFO pForms, LONG lCount)
  646. {
  647.    LONG   i;
  648.    CHAR   szFormtoFind[32], szFormtoCompare[32];
  649.    PSZ    pszTmp;
  650.  
  651.    strcpy( szFormtoFind, pszFormName );
  652.    pszTmp = strchr( szFormtoFind, ' ' );
  653.    if (pszTmp)
  654.    {
  655.       *pszTmp = '\0';
  656.    }
  657.    for (i=0L; i < lCount; ++i)
  658.    {
  659.       strcpy( szFormtoCompare, pForms[i].szFormname );
  660.       pszTmp = strchr( szFormtoCompare, ' ' );
  661.       if (pszTmp)
  662.       {
  663.           *pszTmp = '\0';
  664.       }
  665.       if ( !strcmp( szFormtoFind, szFormtoCompare ) )
  666.           return i;
  667.    }
  668.  
  669.    return -1L;
  670. } /* end of FindForm */
  671.  
  672.  
  673. /*************************************************************************
  674.  *  Function   :  QueryForm
  675.  *
  676.  *  Description:  Query all forms, display them in a list box and do a selection
  677.  *
  678.  *  Parameters :  PMAIN_PARM  pmp has global application data
  679.  *                BOOL        flag, if TRUE the page setup dialog is displayed
  680.  *
  681.  *  API's      :  WinDlgBox
  682.  *                WinLoadString
  683.  *                WinMessageBox
  684.  *                WinPostMsg
  685.  *                WinSendMsg
  686.  *
  687.  *  Return     :  BOOL        TRUE if the form changed, otherwise false
  688.  *
  689.  *************************************************************************/
  690. BOOL QueryForm( PMAIN_PARM pmp, BOOL fNeedDialog )
  691.  
  692. {
  693.    SHORT               i, index;
  694.    BOOL                bOK;
  695.    ULONG               ulLeftMargin;
  696.    ULONG               ulRightMargin;
  697.    ULONG               ulTopMargin;
  698.    ULONG               ulBottomMargin;
  699.    CHAR                szTmp[ LEN_WORKSTRING ], szWork[ LEN_WORKSTRING ];
  700.    CHAR                szOldForm[32];
  701.    ULONG               ul;
  702.  
  703.    /* retrieve form names and sizes */
  704.    pmp->cForms = QueryPrinterForms( pmp, pmp->hcinfo, MAX_FORMS );
  705.    pmassert( pmp->hab, pmp->cForms );
  706.  
  707.    /*
  708.     * Find form name if previously set, a return of -1 means that the form
  709.     * name was not found.
  710.     */
  711.    strcpy( szOldForm, pmp->form.achFormName );
  712.    index = FindForm( szOldForm, pmp->hcinfo, pmp->cForms );
  713.  
  714.    /*
  715.     * Select form name from pmp structure. Either use the form
  716.     * previously found or if it wasn't found, then pre-select the
  717.     * one with HCAPS_CURRENT set.
  718.     * Fill in all the form sizes and margins before selecting the form.
  719.     */
  720.    for (i = 0; i < pmp->cForms; ++i)
  721.    {
  722.       if ( ((index == -1) && (pmp->hcinfo[i].flAttributes & HCAPS_CURRENT)) ||
  723.            (i == index) )
  724.       {
  725.          pmp->iForm = i;
  726.          strcpy(pmp->form.achFormName, pmp->hcinfo[i].szFormname );
  727.  
  728.          /* check that the margins are greater than the clip limits */
  729.          pmp->form.fxLeftMargin = max( pmp->form.fxLeftMargin,
  730.                                 MMtoFixedInch(pmp->hcinfo[i].xLeftClip) );
  731.          pmp->form.fxRightMargin = max( pmp->form.fxRightMargin,
  732.              MMtoFixedInch(pmp->hcinfo[i].cx - pmp->hcinfo[i].xRightClip) );
  733.          pmp->form.fxTopMargin = max( pmp->form.fxTopMargin,
  734.              MMtoFixedInch(pmp->hcinfo[i].cy - pmp->hcinfo[i].yTopClip) );
  735.          pmp->form.fxBottomMargin = max( pmp->form.fxBottomMargin,
  736.                                 MMtoFixedInch(pmp->hcinfo[i].yBottomClip) );
  737.       }
  738.    }
  739.  
  740.    /*
  741.     * if form is not found and a previous form was not initialized,
  742.     * display a warning message box, that a default form has been chosen
  743.     */
  744.    if ( index == -1L && strcmp( szOldForm, "" ) )
  745.    {
  746.       bOK = WinLoadString( pmp->hab, (HMODULE)0,
  747.                    ERRMSG_FORM_CHANGE, LEN_WORKSTRING, szTmp );
  748.       pmassert( pmp->hab, bOK );
  749.       sprintf( szWork, szTmp, szOldForm, pmp->form.achFormName );
  750.       WinMessageBox( HWND_DESKTOP,
  751.                      pmp->hwndFrame,
  752.                      szWork,
  753.                      pmp->pszTitle,
  754.                      (USHORT)0,
  755.                      MB_OK | MB_MOVEABLE | MB_CUAWARNING | MB_APPLMODAL);
  756.  
  757.    }
  758.  
  759.    if ( fNeedDialog )
  760.    {
  761.       /*
  762.        * Query user page setup parameters.
  763.        */
  764.       ul = WinDlgBox( HWND_DESKTOP,
  765.                       pmp->hwndFrame,
  766.                       (PFNWP)PageDlgProc,
  767.                       (HMODULE)0,
  768.                       IDD_PAGESETUP,
  769.                       (PVOID)pmp);
  770.  
  771.       pmassert( pmp->hab, ul != DID_ERROR );
  772.  
  773.       if( ul == DID_CANCEL )
  774.       {
  775.            /* indicate to caller that no change occurred  */
  776.            return FALSE;
  777.       }
  778.  
  779.    }
  780.    /*
  781.     * At this point the form or margins changed so recalculate the
  782.     * form and page size
  783.     */
  784.    i = pmp->iForm;
  785.    pmp->form.fxxWidth = MMtoFixedInch( pmp->hcinfo[i].cx );
  786.    pmp->form.fxyHeight = MMtoFixedInch( pmp->hcinfo[i].cy );
  787.    ulLeftMargin = FixedInchesToTwips(pmp->form.fxLeftMargin);
  788.    ulRightMargin = FixedInchesToTwips(pmp->form.fxRightMargin);
  789.    ulTopMargin = FixedInchesToTwips(pmp->form.fxTopMargin);
  790.    ulBottomMargin = FixedInchesToTwips(pmp->form.fxBottomMargin);
  791.    pmp->sizelPage.cx = FixedInchesToTwips(pmp->form.fxxWidth) -
  792.                        ulLeftMargin - ulRightMargin;
  793.    pmp->sizelPage.cy = FixedInchesToTwips(pmp->form.fxyHeight) -
  794.                        ulTopMargin - ulBottomMargin;
  795.  
  796.    /* Cause a redraw of the screen to show the new margins */
  797.    WinSendMsg( pmp->hwndClient, WM_SIZE, 0, 0 );
  798.    WinSendMsg( pmp->hwndClient, WM_USER_DISABLE_CLIENT, 0, 0 );
  799.  
  800.  
  801.    /* re-paginate text for the screen */
  802.    if( pmp->f )
  803.    {
  804.        /* seek top of file to display document in new font */
  805.        fseek( pmp->f, 0, SEEK_SET );
  806.        WinPostMsg( pmp->hwndObject, WM_USER_PAGINATE,
  807.                    (MPARAM)pmp->hwndClient,
  808.                    (MPARAM)FLAGS_SCREEN );
  809.    }
  810.  
  811.    /* return indication that something changed  */
  812.    return TRUE;
  813.  
  814. } /* end of QueryForm */
  815.  
  816. /***************************  End of prtpage.c *****************************/
  817.