home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_03 / fader.exe / FADER / FADER2.C < prev   
Text File  |  1991-11-17  |  8KB  |  289 lines

  1.  /*
  2.      File name:    FADER.C
  3.      Description:  SDK Dialog Editor (DIALOG.EXE) support funcs.
  4.      Sequence:     This is Listing #2
  5. */
  6.  
  7. #include <windows.h>
  8. #include <custcntl.h>
  9. #include "fader.h"
  10. #include "dialog.h"
  11.  
  12.  
  13. /* Property string used internally for local handle of CTLSTYLEDLG
  14. structure */
  15. static char szFaderCtlProp[] = "CtlDlgStyleData";
  16. extern HANDLE hGlobFaderInstance;
  17. extern char szGlobControlName[];
  18.  
  19.  
  20. /* Data structure used internally to access data in the style
  21.   dialog box function. */
  22. typedef struct {
  23.    GLOBALHANDLE hCtlStyle;    /* Handle holds CTLSTYLE */
  24.    LPFNSTRTOID  lpfnStrToId;  /* convert string ID to number */
  25.    LPFNIDTOSTR  lpfnIdToStr;  /* convert numeric ID to string */
  26. } CTLSTYLEDLG, FAR *LPCTLSTYLEDLG, NEAR *NPCTLSTYLEDLG;
  27.  
  28. /* Forward declarations, for completeness */
  29.  
  30. LPCTLSTYLE FAR PASCAL CtlStyleLock (HWND hDlg);
  31. VOID FAR PASCAL CtlStyleUnlock (HWND hDlg);
  32. WORD FAR PASCAL GetIdString (HWND hDlg, LPSTR szId,
  33.      WORD wIdMaxLen);
  34. DWORD FAR PASCAL PutIdValue (HWND hDlg, LPSTR szId);
  35. BOOL FAR PASCAL FaderDlgFn (HWND hDlg, WORD wMessage,
  36.      WORD wParam, LONG lParam);
  37. BOOL FAR PASCAL FaderDlgCmdFn (HWND hDlg, WORD wParam,
  38.      LONG lParam);
  39.  
  40.  
  41. GLOBALHANDLE FAR PASCAL FaderInfo (void)
  42. {
  43.    GLOBALHANDLE hCtlInfo = NULL;
  44.    LPCTLINFO lpCtlInfo; WORD wNumTypes;
  45.  
  46.    hCtlInfo = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
  47.                         (DWORD) sizeof(CTLINFO));
  48.    if (hCtlInfo == NULL)
  49.       return NULL;
  50.    lpCtlInfo = (LPCTLINFO) GlobalLock(hCtlInfo);
  51.    lpCtlInfo->wVersion  = 0x0100;
  52.  
  53.    /* Initialize wCtlTypes to zero */
  54.    lpCtlInfo->wCtlTypes = 0;
  55.    lstrcpy(lpCtlInfo->szClass, szGlobControlName);
  56.    lstrcpy(lpCtlInfo->szTitle, szGlobControlName);
  57.    wNumTypes = lpCtlInfo->wCtlTypes;
  58.    if (wNumTypes == CTLTYPES) {
  59.       GlobalUnlock(hCtlInfo);
  60.       return hCtlInfo;
  61.       }
  62.  
  63.    lpCtlInfo->Type[wNumTypes].wType   = 0;
  64.    lpCtlInfo->Type[wNumTypes].wWidth  = 12;
  65.    lpCtlInfo->Type[wNumTypes].wHeight = 24;
  66.    lpCtlInfo->Type[wNumTypes].dwStyle = WS_BORDER | WS_CHILD | WS_TABSTOP;
  67.    lstrcpy(lpCtlInfo->Type[wNumTypes].szDescr,szGlobControlName);
  68.    lpCtlInfo->wCtlTypes++;
  69.    GlobalUnlock(hCtlInfo);
  70.    return hCtlInfo;
  71. }
  72.  
  73.  
  74. BOOL FAR PASCAL FaderStyle (HWND hWnd, GLOBALHANDLE hCtlStyle,
  75.    LPFNSTRTOID lpfnStrToId, LPFNIDTOSTR lpfnIdToStr)
  76. {
  77.  
  78.    LOCALHANDLE hCtlStyleDlg;
  79.    NPCTLSTYLEDLG npCtlStyleDlg;
  80.    int x;
  81.  
  82.    hCtlStyleDlg =
  83.       LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, sizeof(CTLSTYLEDLG));
  84.    if (hCtlStyleDlg == NULL)
  85.        return FALSE;
  86.  
  87.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  88.    npCtlStyleDlg->hCtlStyle = hCtlStyle;
  89.    npCtlStyleDlg->lpfnStrToId = lpfnStrToId;
  90.    npCtlStyleDlg->lpfnIdToStr = lpfnIdToStr;
  91.    LocalUnlock(hCtlStyleDlg);
  92.  
  93.    /* Associate property with Dialog Editor's window. */
  94.    SetProp(hWnd, szFaderCtlProp, hCtlStyleDlg);
  95.  
  96.    /* Display control's Styles Dialog Box. */
  97.    x = DialogBoxParam(hGlobFaderInstance, "StyleDlg", hWnd, FaderDlgFn, 0);
  98.  
  99.    /* Remove property associated with Dialog Editor's window. */
  100.    RemoveProp(hWnd, szFaderCtlProp);
  101.  
  102.    LocalFree(hCtlStyleDlg);
  103.    /* Return whether CTLSTYLE structure has been changed. */
  104.    return x==IDOK;
  105. }
  106.  
  107.  
  108.  
  109.  
  110. BOOL FAR PASCAL FaderDlgFn (HWND hDlg, WORD wMsg,
  111.    WORD wParam, LONG lParam)
  112. {
  113.    BOOL bProcMsg = TRUE;
  114.    char szId[20];
  115.    LPCTLSTYLE lpCtlStyle;
  116.  
  117.    switch (wMsg)
  118.       {
  119.       case WM_INITDIALOG:
  120.          /* Set the "EDIT" control with the name of
  121.             this control's ID. */
  122.          GetIdString(hDlg, szId, sizeof(szId));
  123.          SetDlgItemText(hDlg, ID_VALUE, szId);
  124.  
  125.          /* Initialize check box w/Fader control's styles. */
  126.          lpCtlStyle = CtlStyleLock(hDlg);
  127.          SendDlgItemMessage(hDlg, ID_TRACK, BM_SETCHECK,
  128.             (BOOL) (lpCtlStyle->dwStyle & FDRS_TRACK), 1L);
  129.          CtlStyleUnlock(hDlg);
  130.          break;
  131.  
  132.       case WM_COMMAND:
  133.          FaderDlgCmdFn(hDlg, wParam, lParam);
  134.          break;
  135.  
  136.       default:
  137.          bProcMsg = FALSE;
  138.          break;
  139.    }
  140.    return bProcMsg;
  141. }
  142.  
  143.  
  144. BOOL FAR PASCAL FaderDlgCmdFn (HWND hDlg, WORD wParam, LONG lParam)
  145. {
  146.    DWORD dwResult;
  147.    char szId[20];
  148.    LPCTLSTYLE lpCtlStyle;
  149.  
  150.    switch(wParam)
  151.       {
  152.       case IDOK:
  153.          /* Convert the string ID value to its numeric equivalent. */
  154.          GetDlgItemText(hDlg, ID_VALUE, szId, sizeof(szId));
  155.          dwResult = PutIdValue(hDlg, szId);
  156.  
  157.          /* If string ID not found or added, stay in Dialog box. */
  158.          if (LOWORD(dwResult) == 0)
  159.             break;
  160.  
  161.          /* Calculate the new control's styles. */
  162.          lpCtlStyle = CtlStyleLock(hDlg);
  163.  
  164.          /* Clear control-specific flags */
  165.          lpCtlStyle->dwStyle &= 0xFFFF0000L;
  166.  
  167.          if (SendDlgItemMessage(hDlg, ID_TRACK, BM_GETCHECK, 0, 0L))
  168.             lpCtlStyle->dwStyle |= FDRS_TRACK;
  169.  
  170.          CtlStyleUnlock(hDlg);
  171.          EndDialog(hDlg, wParam);
  172.          break;
  173.  
  174.       case IDCANCEL:
  175.          EndDialog(hDlg, wParam);
  176.          break;
  177.  
  178.       case ID_VALUE:
  179.          /* Disable IDOK button if no text exists in ID_VALUE box. */
  180.          if (HIWORD(lParam) == EN_CHANGE)
  181.             EnableWindow(GetDlgItem(hDlg, IDOK),
  182.                SendMessage(LOWORD(lParam), WM_GETTEXTLENGTH, 0, 0L)
  183.                ? TRUE : FALSE);
  184.          break;
  185.  
  186.       default:
  187.          return FALSE;
  188.          break;
  189.    }
  190.  
  191.    return TRUE;
  192. }
  193.  
  194.  
  195. WORD FAR PASCAL FaderFlags (DWORD dwFlags, LPSTR szString, WORD wMaxString)
  196. {
  197.    *szString = 0;
  198.    if (dwFlags & FDRS_TRACK)
  199.       lstrcat(szString, "FDRS_TRACK");
  200.    return lstrlen(szString);
  201. }
  202.  
  203.  
  204. LPCTLSTYLE FAR PASCAL CtlStyleLock (HWND hDlg) {
  205.    LOCALHANDLE hCtlStyleDlg;
  206.    NPCTLSTYLEDLG npCtlStyleDlg;
  207.    LPCTLSTYLE lpCtlStyle;
  208.  
  209.    if ( (hCtlStyleDlg=GetProp(GetParent(hDlg), szFaderCtlProp)) == NULL)
  210.        return NULL;
  211.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  212.    lpCtlStyle = (LPCTLSTYLE) GlobalLock(npCtlStyleDlg->hCtlStyle);
  213.    LocalUnlock(hCtlStyleDlg);
  214.    return lpCtlStyle;
  215. }
  216.  
  217.  
  218. VOID FAR PASCAL CtlStyleUnlock (HWND hDlg) {
  219.    LOCALHANDLE hCtlStyleDlg;
  220.    NPCTLSTYLEDLG npCtlStyleDlg;
  221.  
  222.    if ((hCtlStyleDlg=GetProp(GetParent(hDlg), szFaderCtlProp)) == NULL)
  223.       return;
  224.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  225.    GlobalUnlock(npCtlStyleDlg->hCtlStyle);
  226.    LocalUnlock(hCtlStyleDlg);
  227. }
  228.  
  229.  
  230.  
  231. /* Convert number into ID string */
  232.  
  233. WORD FAR PASCAL GetIdString (HWND hDlg, LPSTR szId, WORD wIdMaxLen) {
  234.    LOCALHANDLE hCtlStyleDlg;
  235.    NPCTLSTYLEDLG npCtlStyleDlg;
  236.    LPCTLSTYLE lpCtlStyle;
  237.    WORD wIdLen;
  238.  
  239.    /* Property is associated with Dialog Editor's window.
  240.       Parent of the dialog box is the Dialog Editor. */
  241.    if (( hCtlStyleDlg=GetProp(GetParent(hDlg), szFaderCtlProp))
  242.        == NULL)
  243.        return 0;
  244.  
  245.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  246.    lpCtlStyle = (LPCTLSTYLE) GlobalLock(npCtlStyleDlg->hCtlStyle);
  247.  
  248.    /* Call the lpfnIdToStr function to convert the numeric ID to its
  249.    string equivalent. */
  250.    wIdLen = (*npCtlStyleDlg->lpfnIdToStr)
  251.               (lpCtlStyle->wId, szId, wIdMaxLen);
  252.  
  253.    GlobalUnlock(npCtlStyleDlg->hCtlStyle);
  254.    LocalUnlock(hCtlStyleDlg);
  255.    return wIdLen;
  256. }
  257.  
  258.  
  259. /* Convert ID string value into number and store in CTLSTYLE */
  260.  
  261. DWORD FAR PASCAL PutIdValue (HWND hDlg, LPSTR szId) {
  262.    LOCALHANDLE hCtlStyleDlg;
  263.    NPCTLSTYLEDLG npCtlStyleDlg;
  264.    LPCTLSTYLE lpCtlStyle;
  265.    DWORD dwResult = 0;
  266.  
  267.    if ((hCtlStyleDlg=GetProp(GetParent(hDlg), szFaderCtlProp))
  268.       == NULL )
  269.       return NULL;
  270.  
  271.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  272.  
  273.    /* Call the lpfnStrToId function to convert the string
  274.       ID to its numeric equivalent. */
  275.    dwResult = (*npCtlStyleDlg->lpfnStrToId)(szId);
  276.  
  277.    LocalUnlock(hCtlStyleDlg);
  278.  
  279.    /* If LOWORD is zero, string NOT found. */
  280.    if (LOWORD(dwResult) == 0)
  281.       return dwResult;
  282.  
  283.    /* LOWORD is not zero, numeric ID is in the HIWORD. */
  284.    lpCtlStyle = CtlStyleLock(hDlg);
  285.    lpCtlStyle->wId = HIWORD(dwResult);
  286.    CtlStyleUnlock(hDlg);
  287.    return dwResult;
  288. }
  289.