home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MSJV5-6.ZIP / FORM2.ZIP / PAGE3.C < prev    next >
C/C++ Source or Header  |  1990-11-01  |  9KB  |  334 lines

  1. /*
  2.  * Page Selection Control - STYLE MODULE
  3.  *
  4.  * LANGUAGE      : Microsoft C5.1
  5.  * MODEL         : small
  6.  * ENVIRONMENT   : Microsoft Windows 3.0 SDK
  7.  * STATUS        : operational
  8.  *
  9.  * This module defines the function responsible for displaying the
  10.  * style dialog box, through which the user can define the window
  11.  * text and page range.
  12.  *
  13.  *    Eikon Systems, Inc.
  14.  *    989 East Hillsdale Blvd, Suite 260
  15.  *    Foster City, California 94404
  16.  *    415-349-4664
  17.  *
  18.  * 11/30/89 1.00 - Kevin P. Welch - initial creation.
  19.  *
  20.  */
  21.  
  22. #define  NOCOMM
  23.  
  24. #include <windows.h>
  25. #include <control.h>
  26.  
  27. #include "page.h"
  28. #include "page.d"
  29.  
  30. #define  HCTLSTYLE      MAKEINTRESOURCE(1)
  31. #define  LPFNSTRTOIDLO  MAKEINTRESOURCE(2)
  32. #define  LPFNSTRTOIDHI  MAKEINTRESOURCE(3)
  33.  
  34. /* global variables for data exchange */
  35. static HANDLE           hCtlStyleTemp;
  36. static LPFNSTRTOID      lpfnStrToIdTemp;
  37. static LPFNIDTOSTR      lpfnIdToStrTemp;
  38.  
  39. /* local function definitions */
  40. BOOL FAR PASCAL   CenterPopup( HWND, HWND );
  41. BOOL FAR PASCAL   PageDlgFn( HWND, WORD, WORD, LONG );
  42.  
  43. /* */
  44.  
  45. /*
  46.  * PageStyle( hWnd, hCtlStyle, lpfnStrToId, lpfnIdToStr ) : LONG;
  47.  *
  48.  *    hWnd           handle to parent window
  49.  *    hCtlStyle      handle to control style block
  50.  *    lpfnStrToId    pointer to Verify id function.
  51.  *    lpfnIdToStr    pointer to GetIdStr function.
  52.  *
  53.  * This function displays a dialog box, enabling the user to define
  54.  * a new control style.  A value of TRUE is returned if the process
  55.  * was successful, FALSE otherwise.
  56.  *
  57.  * The lpfnStrToId and lpfnIdToStr functions can be optionally used
  58.  * by the calling program to verify the id value entered for the
  59.  * control.  If these two values are NULL then no verification
  60.  * is performed.
  61.  *
  62.  */
  63.  
  64. BOOL FAR PASCAL PageStyle(
  65.    HWND           hWnd,
  66.    HANDLE         hCtlStyle,
  67.    LPFNSTRTOID    lpfnStrToId,
  68.    LPFNIDTOSTR    lpfnIdToStr )
  69. {
  70.    BOOL        bResult;
  71.    FARPROC     lpProc;
  72.  
  73.    /* check for valid style block */
  74.    if ( hCtlStyle ) {
  75.  
  76.       /* initialization */
  77.       hCtlStyleTemp = hCtlStyle;
  78.       lpfnStrToIdTemp = lpfnStrToId;
  79.       lpfnIdToStrTemp = lpfnIdToStr;
  80.  
  81.       /* display dialog box */
  82.       lpProc = MakeProcInstance( PageDlgFn, hPageInst );
  83.       bResult = DialogBox( hPageInst, "PageStyle", hWnd, lpProc );
  84.       FreeProcInstance( lpProc );
  85.  
  86.    } else
  87.       bResult = FALSE;
  88.  
  89.    /* return final result */
  90.    return( bResult );
  91.  
  92. }
  93.  
  94. /* */
  95.  
  96. /*
  97.  * PageDlgFn( hDlg, wMessage, wParam, lParam ) : LONG;
  98.  *
  99.  *    hDlg           dialog box window handle
  100.  *    wMessage       dialog box message
  101.  *    wParam         word parameter
  102.  *    lParam         long parameter
  103.  *
  104.  * This function is responsible for managing the style dialog
  105.  * displayed to the user.  All responses are validated before
  106.  * the dialog box is closed.  All values are passed using some
  107.  * temporary global variables.
  108.  *
  109.  */
  110.  
  111. BOOL FAR PASCAL PageDlgFn(
  112.    HWND     hDlg,
  113.    WORD     wMessage,
  114.    WORD     wParam,
  115.    LONG     lParam )
  116. {
  117.    BOOL     bResult;
  118.  
  119.    /* warning level 3 compatibility */
  120.    lParam;
  121.    
  122.    /* initialization */
  123.    bResult = TRUE;
  124.  
  125.    /* process message */
  126.    switch ( wMessage )
  127.       {
  128.    case WM_INITDIALOG : /* initialize dialog box */
  129.  
  130.       /* initialize property list */
  131.       SetProp( hDlg, HCTLSTYLE, hCtlStyleTemp );
  132.       SetProp( hDlg, LPFNSTRTOIDLO, LOWORD(lpfnStrToIdTemp) );
  133.       SetProp( hDlg, LPFNSTRTOIDHI, HIWORD(lpfnStrToIdTemp) );
  134.  
  135.       /* center within parent window */
  136.       CenterPopup( hDlg, GetParent( hDlg ) );
  137.  
  138.       /* define dialog box contents */
  139.       if ( hCtlStyleTemp ) {
  140.  
  141.          char        szId[32];
  142.          LPCTLSTYLE  lpCtlStyle;
  143.  
  144.          /* lock the data down */
  145.          lpCtlStyle = (LPCTLSTYLE)GlobalLock( hCtlStyleTemp );
  146.          if ( lpCtlStyle ) {
  147.  
  148.             /* define page id field */
  149.             if ( lpfnIdToStrTemp && lpfnStrToIdTemp ) {
  150.                (*lpfnIdToStrTemp)( lpCtlStyle->wId, (LPSTR)szId,
  151.                   sizeof(szId) );
  152.                SetDlgItemText( hDlg, ID_IDENTIFIER, szId );
  153.             }
  154.  
  155.             /* unlock the data */
  156.             GlobalUnlock( GetProp(hDlg,HCTLSTYLE) );
  157.  
  158.          } else
  159.             MessageBox( hDlg, "Unable to retrieve style inforation!",
  160.                "Page Control", MB_OK );
  161.  
  162.       } else
  163.          MessageBox( hDlg, "Control style information undefined!",
  164.             "Page Control", MB_OK );
  165.  
  166.       break;
  167.    case WM_COMMAND:
  168.  
  169.       /* process commands */
  170.       switch( wParam )
  171.          {
  172.       case IDOK : /* OK button */
  173.          {
  174.             WORD           wSize;
  175.             BOOL           bClose;
  176.             HANDLE         hCtlStyle;   
  177.             DWORD          dwResult;    
  178.             LPCTLSTYLE     lpCtlStyle;  
  179.             LPFNSTRTOID    lpfnStrToId;
  180.             char           szId[CTLTITLE];
  181.  
  182.             /* initialization */
  183.             bClose = FALSE;
  184.             hCtlStyle = GetProp( hDlg, HCTLSTYLE );
  185.  
  186.             /* lock data down */
  187.             lpCtlStyle = hCtlStyle ?
  188.                (LPCTLSTYLE)GlobalLock(hCtlStyle) : NULL;
  189.             if (lpCtlStyle) {
  190.  
  191.                /* define new window id */
  192.                lpfnStrToId = (LPFNSTRTOID)MAKELONG(
  193.                      GetProp(hDlg,LPFNSTRTOIDLO),
  194.                      GetProp(hDlg,LPFNSTRTOIDHI)
  195.                   );
  196.                if ( lpfnStrToId ) {
  197.  
  198.                   /* retrieve identifier string */
  199.                   wSize = GetDlgItemText( hDlg, ID_IDENTIFIER, szId,
  200.                      sizeof(szId) );
  201.                   szId[wSize] = NULL;
  202.  
  203.                   /* translate indentifier */
  204.                   dwResult = (*lpfnStrToId)((LPSTR)szId);
  205.                   if ( LOWORD(dwResult) ) {
  206.                      bClose = TRUE;
  207.                      lpCtlStyle->wId = HIWORD( dwResult );
  208.                   }
  209.  
  210.                }
  211.  
  212.                /* unlock the data */
  213.                GlobalUnlock( GetProp(hDlg,HCTLSTYLE) );
  214.   
  215.             }
  216.  
  217.             /* close dialog box */
  218.             if ( bClose )
  219.                EndDialog( hDlg, TRUE );
  220.  
  221.          }
  222.          break;
  223.       case IDCANCEL : /* Cancel button */
  224.          EndDialog( hDlg, FALSE );
  225.          break;
  226.       default : /* something else - ignore */
  227.          break;
  228.       }
  229.  
  230.       break;
  231.    case WM_DESTROY : /* window being destroyed */
  232.  
  233.       /* clean up property lists */
  234.       RemoveProp( hDlg, HCTLSTYLE );
  235.       RemoveProp( hDlg, LPFNSTRTOIDLO );
  236.       RemoveProp( hDlg, LPFNSTRTOIDHI );
  237.  
  238.       break;
  239.    default : /* some other message */
  240.       bResult = FALSE;
  241.       break;
  242.    }
  243.  
  244.    /* return final result */
  245.    return bResult;
  246.  
  247. }
  248.  
  249. /* */
  250.  
  251. /*
  252.  * CenterPopup( hWnd, hParentWnd ) : BOOL;
  253.  *
  254.  *    hWnd              window handle
  255.  *    hParentWnd        parent window handle
  256.  *
  257.  * This routine centers the popup window in the screen or display
  258.  * using the window handles provided.  The window is centered over
  259.  * the parent if the parent window is valid.  Special provision
  260.  * is made for the case when the popup would be centered outside
  261.  * the screen - in this case it is positioned at the appropriate
  262.  * border.
  263.  *
  264.  */
  265.  
  266. BOOL FAR PASCAL CenterPopup(
  267.    HWND        hWnd,
  268.    HWND        hParentWnd )
  269. {
  270.    int         xPopup;
  271.    int         yPopup;
  272.    int         cxPopup;
  273.    int         cyPopup;
  274.    int         cxScreen;
  275.    int         cyScreen;
  276.    int         cxParent;
  277.    int         cyParent;
  278.    RECT        rcWindow;
  279.  
  280.    /* retrieve main display dimensions */
  281.    cxScreen = GetSystemMetrics( SM_CXSCREEN );
  282.    cyScreen = GetSystemMetrics( SM_CYSCREEN );
  283.  
  284.    /* retrieve popup rectangle  */
  285.    GetWindowRect( hWnd, (LPRECT)&rcWindow );
  286.  
  287.    /* calculate popup size */
  288.    cxPopup = rcWindow.right - rcWindow.left;
  289.    cyPopup = rcWindow.bottom - rcWindow.top;
  290.  
  291.    /* calculate bounding rectangle */
  292.    if ( hParentWnd ) {
  293.  
  294.       /* retrieve parent rectangle */
  295.       GetWindowRect( hParentWnd, (LPRECT)&rcWindow );
  296.  
  297.       /* calculate parent extents */
  298.       cxParent = rcWindow.right - rcWindow.left;
  299.       cyParent = rcWindow.bottom - rcWindow.top;
  300.  
  301.       /* center within parent window */
  302.       xPopup = rcWindow.left + ((cxParent - cxPopup)/2);
  303.       yPopup = rcWindow.top + ((cyParent - cyPopup)/2);
  304.  
  305.       /* adjust popup x-location for screen size */
  306.       if ( xPopup+cxPopup > cxScreen )
  307.          xPopup = cxScreen - cxPopup;
  308.  
  309.       /* adjust popup y-location for screen size */
  310.       if ( yPopup+cyPopup > cyScreen )
  311.          yPopup = cyScreen - cyPopup;
  312.  
  313.    } else {
  314.  
  315.       /* center within entire screen */
  316.       xPopup = (cxScreen - cxPopup) / 2;
  317.       yPopup = (cyScreen - cyPopup) / 2;
  318.  
  319.    }
  320.  
  321.    /* move window to new location & display */
  322.    MoveWindow(
  323.       hWnd,
  324.       ( xPopup > 0 ) ? xPopup : 0,
  325.       ( yPopup > 0 ) ? yPopup : 0,
  326.       cxPopup,
  327.       cyPopup,
  328.       TRUE
  329.    );
  330.  
  331.    /* normal return */
  332.    return( TRUE );
  333. }
  334.