home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 May / cica_0595_4.zip / cica_0595_4 / UTIL / MSWSRC35 / METER / CNTL-DE.C next >
C/C++ Source or Header  |  1992-06-11  |  7KB  |  223 lines

  1. /*****************************************************************
  2. Module name: Cntl-DE.C
  3. Programmer : Jeffrey M. Richter.
  4. *****************************************************************/
  5.  
  6. #include <windows.h>
  7. #include <custcntl.h>
  8.  
  9. #include "cntl-de.h"
  10.  
  11.  
  12. // Property string used internally to store local handle of 
  13. // CTLSTYLEDLG data structure.
  14. static char _szCtlProp[] = "CtlDlgStyleData";
  15.  
  16.  
  17. // Data structure used internally to get information into the 
  18. // style dialog box function.
  19. typedef struct {
  20.    HGLOBAL      hGlblCtlStyle;// Handle holds control's CTLSTYLE
  21.    LPFNSTRTOID  lpfnStrToId;  // Func to cnvrt string ID to #
  22.    LPFNIDTOSTR  lpfnIdToStr;  // Func to cnvrt # ID to string
  23. } CTLSTYLEDLG, FAR *LPCTLSTYLEDLG, NEAR *NPCTLSTYLEDLG;
  24.  
  25.  
  26. // This function should be called first in the ClassInfo
  27. // function to initialize the new control.
  28. HGLOBAL WINAPI ControlInfo
  29.         (WORD wVersion, LPSTR szClass, LPSTR szTitle) {
  30.  
  31.    HGLOBAL hGlbl = (HGLOBAL) NULL;
  32.    LPCTLINFO lpCtlInfo;
  33.  
  34.    hGlbl = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
  35.                         (DWORD) sizeof(CTLINFO));
  36.    if (hGlbl == NULL) return(hGlbl);
  37.    lpCtlInfo = (LPCTLINFO) GlobalLock(hGlbl);
  38.    lpCtlInfo->wVersion = wVersion;
  39.  
  40.    // Initialize wCtlTypes to zero, incremented by 
  41.    // AddControlType function.
  42.    lpCtlInfo->wCtlTypes = 0;
  43.    lstrcpy(lpCtlInfo->szClass, szClass);
  44.    lstrcpy(lpCtlInfo->szTitle, szTitle);
  45.    GlobalUnlock(hGlbl);
  46.    return(hGlbl);
  47. }
  48.  
  49.  
  50.  
  51. // This function should be called repeatedly to add new control 
  52. // types to the structure returned by the ControlInfo function.  
  53. // This function should be called in the ClassInfo function.
  54. BOOL WINAPI AddControlType (HGLOBAL hGlbl, WORD wType,
  55.         WORD wWidth, WORD wHeight, DWORD dwStyle, LPSTR szDescr) {
  56.  
  57.    LPCTLINFO lpCtlInfo; WORD wNumTypes;
  58.    lpCtlInfo = (LPCTLINFO) GlobalLock(hGlbl);
  59.    wNumTypes = lpCtlInfo->wCtlTypes;
  60.    if (wNumTypes == CTLTYPES) {
  61.       GlobalUnlock(hGlbl);
  62.       return(FALSE);
  63.    }
  64.    lpCtlInfo->Type[wNumTypes].wType   = wType;
  65.    lpCtlInfo->Type[wNumTypes].wWidth  = wWidth;
  66.    lpCtlInfo->Type[wNumTypes].wHeight = wHeight;
  67.    lpCtlInfo->Type[wNumTypes].dwStyle = dwStyle;
  68.    lstrcpy(lpCtlInfo->Type[wNumTypes].szDescr, szDescr);
  69.    lpCtlInfo->wCtlTypes++;
  70.    GlobalUnlock(hGlbl);
  71.    return(TRUE);
  72. }
  73.  
  74.  
  75.  
  76. // This function displays the control's style dialog box and 
  77. // should be called from the ClassStyle function.
  78. int WINAPI ShowStyleDlg (HINSTANCE hInstance, LPCSTR szTemplate,
  79.           HWND hWndParent, DLGPROC fpDlgProc, LPARAM lParam,
  80.           HGLOBAL hGlblCtlStyle, LPFNSTRTOID lpfnStrToId,
  81.           LPFNIDTOSTR lpfnIdToStr) {
  82.  
  83.    HLOCAL hLclCtlStyleDlg;
  84.    NPCTLSTYLEDLG npCtlStyleDlg;
  85.    int x;
  86.  
  87.    hLclCtlStyleDlg = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,
  88.       sizeof(CTLSTYLEDLG));
  89.    if (hLclCtlStyleDlg == NULL) return(FALSE);
  90.  
  91.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hLclCtlStyleDlg);
  92.    npCtlStyleDlg->hGlblCtlStyle = hGlblCtlStyle;
  93.    npCtlStyleDlg->lpfnStrToId = lpfnStrToId;
  94.    npCtlStyleDlg->lpfnIdToStr = lpfnIdToStr;
  95.    LocalUnlock(hLclCtlStyleDlg);
  96.  
  97.    // Associate property with Dialog Editor's window.
  98.    SetProp(hWndParent, _szCtlProp, hLclCtlStyleDlg);
  99.  
  100.    // Display control's Styles Dialog Box.
  101.    x = DialogBoxParam(hInstance, szTemplate, hWndParent,
  102.       fpDlgProc, lParam);
  103.  
  104.    // Remove property associated with Dialog Editor's window.
  105.    RemoveProp(hWndParent, _szCtlProp);
  106.  
  107.    LocalFree(hLclCtlStyleDlg);
  108.    return(x == IDOK);   // Return whether CTLSTYLE has changed.
  109. }
  110.  
  111.  
  112.  
  113. // This function should only be called from the ClassDlgFn
  114. // function.  It locks the memory block containing the CTLSTYLE 
  115. // structure for the selected control and returns the FAR address
  116. // to that structure.
  117. LPCTLSTYLE WINAPI CtlStyleLock (HWND hDlg) {
  118.    HLOCAL hLclCtlStyleDlg;
  119.    NPCTLSTYLEDLG npCtlStyleDlg;
  120.    LPCTLSTYLE lpCtlStyle = NULL;
  121.  
  122.    // Property is associated with Dialog Editor's window.
  123.    // Parent of the dialog box is the Dialog Editor.
  124.    hLclCtlStyleDlg = GetProp(GetParent(hDlg), _szCtlProp);
  125.  
  126.    if (hLclCtlStyleDlg == NULL) return(lpCtlStyle);
  127.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hLclCtlStyleDlg);
  128.    lpCtlStyle = (LPCTLSTYLE)
  129.       GlobalLock(npCtlStyleDlg->hGlblCtlStyle);
  130.    LocalUnlock(hLclCtlStyleDlg);
  131.    return(lpCtlStyle);
  132. }
  133.  
  134.  
  135.  
  136. // This function should only be called from the ClassDlgFn
  137. // function.  It unlocks the memory block containing the CTLSTYLE
  138. // structure for the selected control and returns whether the 
  139. // block was successfully unlocked.
  140. BOOL WINAPI CtlStyleUnlock (HWND hDlg) {
  141.    HLOCAL hLclCtlStyleDlg;
  142.    NPCTLSTYLEDLG npCtlStyleDlg;
  143.    BOOL fOk = FALSE;
  144.  
  145.    // Property is associated with Dialog Editor's window.
  146.    // Parent of the dialog box is the Dialog Editor.
  147.    hLclCtlStyleDlg = GetProp(GetParent(hDlg), _szCtlProp);
  148.  
  149.    if (hLclCtlStyleDlg == NULL) return(fOk);
  150.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hLclCtlStyleDlg);
  151.    fOk = GlobalUnlock(npCtlStyleDlg->hGlblCtlStyle);
  152.    LocalUnlock(hLclCtlStyleDlg);
  153.    return(fOk);
  154. }
  155.  
  156.  
  157.  
  158. // This function should only be called from the ClassDlgFn
  159. // function.  It converts the ID value for the control into a
  160. // identifier string and stores the string in the address passed
  161. // in.  The number of characters in the string is returned.
  162. WORD WINAPI GetIdString (HWND hDlg, LPSTR szId, WORD wIdMaxLen) {
  163.    HLOCAL hLclCtlStyleDlg;
  164.    NPCTLSTYLEDLG npCtlStyleDlg;
  165.    LPCTLSTYLE lpCtlStyle;
  166.    WORD wIdLen;
  167.  
  168.    // Property is associated with Dialog Editor's window.
  169.    // Parent of the dialog box is the Dialog Editor.
  170.    hLclCtlStyleDlg = GetProp(GetParent(hDlg), _szCtlProp);
  171.    if (hLclCtlStyleDlg == NULL) return(0);
  172.  
  173.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hLclCtlStyleDlg);
  174.    lpCtlStyle = (LPCTLSTYLE)
  175.       GlobalLock(npCtlStyleDlg->hGlblCtlStyle);
  176.  
  177.    // Call the lpfnIdToStr function to convert the numeric ID 
  178.    // to its string equivalent.
  179.    wIdLen = (*npCtlStyleDlg->lpfnIdToStr)
  180.       (lpCtlStyle->wId, szId, wIdMaxLen);
  181.  
  182.    GlobalUnlock(npCtlStyleDlg->hGlblCtlStyle);
  183.    LocalUnlock(hLclCtlStyleDlg);
  184.    return(wIdLen);
  185. }
  186.  
  187.  
  188.  
  189.  
  190. // This function should only be called from the ClassDlgFn
  191. // function.  It converts an ID string value into its numeric 
  192. // equivalent and stores the numeric value in the CTLSTYLE
  193. // structure for the control.  If the loword of the result is 0,
  194. // the ID is invalid, otherwise, the hiword contains the numeric 
  195. // value of the ID.
  196. DWORD WINAPI SetIdValue (HWND hDlg, LPSTR szId) {
  197.    HLOCAL hLclCtlStyleDlg;
  198.    NPCTLSTYLEDLG npCtlStyleDlg;
  199.    LPCTLSTYLE lpCtlStyle;
  200.    DWORD dwResult = 0;
  201.  
  202.    hLclCtlStyleDlg = GetProp(GetParent(hDlg), _szCtlProp);
  203.    if (hLclCtlStyleDlg == NULL) return(dwResult);
  204.  
  205.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hLclCtlStyleDlg);
  206.  
  207.    // Call the lpfnStrToId function to convert the string ID
  208.    // to its numeric equivalent.
  209.    dwResult = (*npCtlStyleDlg->lpfnStrToId)(szId);
  210.  
  211.    LocalUnlock(hLclCtlStyleDlg);
  212.  
  213.    // If LOWORD is zero, string NOT found.
  214.    if (LOWORD(dwResult) == 0)
  215.       return(dwResult);
  216.  
  217.    // LOWORD is not zero, numeric ID is in the HIWORD.
  218.    lpCtlStyle = CtlStyleLock(hDlg);
  219.    lpCtlStyle->wId = HIWORD(dwResult);
  220.    CtlStyleUnlock(hDlg);
  221.    return(dwResult);
  222. }
  223.