Annotation of mstools/samples/playsnd/sound.c, revision 1.1.1.2

1.1       root        1: /*
                      2:     sound.c
                      3: 
                      4:     Sound menu stuff
                      5: 
                      6:     Puts up a list of all the sounds in the [sounds]
                      7:     section of win.ini and plays the selected one
                      8: 
                      9: 
                     10: */
                     11: 
                     12: #include <stdlib.h>
                     13: #include <string.h>
                     14: #include <windows.h>
1.1.1.2 ! root       15: #include <commdlg.h>
1.1       root       16: #include "PlaySnd.h"
                     17: 
                     18: #define KEYBUFSIZE 2048
                     19: 
                     20: LONG SoundDlgProc(HWND hDlg, UINT msg, DWORD wParam, LONG lParam);
                     21: 
                     22: typedef struct SoundNameAlias {
                     23:        LPSTR pName;
                     24:        UINT  alias;
                     25: } SOUNDNAMEALIAS;
                     26: 
                     27: static SOUNDNAMEALIAS sss[] = {"System"};
                     28: 
                     29: static SOUNDNAMEALIAS SNA[] = {
                     30:       "SystemAsterisk",     sndAlias('S', '*')   ,
                     31:       "SystemQuestion",     sndAlias('S', '?')   ,
                     32:       "SystemHand",         sndAlias('S', 'H')   ,
                     33:       "SystemExit",         sndAlias('S', 'E')   ,
                     34:       "SystemStartup",      sndAlias('S', 'S')   ,
                     35:          "SystemWelcome",      sndAlias('S', 'W')   ,
                     36:       "SystemExclamation",  sndAlias('S', '!')  ,
                     37:       "SystemDefault",      sndAlias('S', 'D')   };
                     38:                                                                                        
                     39: #define NUMSYSTEMSOUNDS (sizeof(SNA)/sizeof(SOUNDNAMEALIAS))
                     40: 
                     41: UINT TranslateNameToAlias(LPSTR name)
                     42: {
                     43:        UINT n;
                     44: 
                     45:        for (n=0; n<NUMSYSTEMSOUNDS; ++n) {
                     46: 
                     47:                if (!lstrcmpi(name, SNA[n].pName)) {
                     48:                        return(SNA[n].alias);
                     49:                }
                     50:        }
                     51: 
                     52:        return(0);
                     53: }
                     54: 
                     55: void Sounds(HWND hWnd)
                     56: {
                     57:     WinEval(DialogBox(ghModule, MAKEINTRESOURCE(IDD_SOUNDDLG) // "SoundDlg"
                     58:                        , hWnd, (DLGPROC)SoundDlgProc) != -1);
                     59: }
                     60: 
                     61: void PlaySelection(HWND hDlg)
                     62: {
                     63:     DWORD dwSel;
1.1.1.2 ! root       64:     DWORD dwFlags = SND_ASYNC | bNoDefault | SND_NOSTOP;
1.1       root       65:     char name[40];
                     66:        UINT alias;
                     67:        LPSTR pName = name;
                     68:        BOOL fSync;
                     69:        BOOL fWait;
                     70: 
                     71:     dwSel = SendDlgItemMessage(hDlg, IDSND_LIST, LB_GETCURSEL, 0, 0);
                     72:     if (dwSel != LB_ERR) {
                     73:         SendDlgItemMessage(hDlg, IDSND_LIST,
                     74:                     LB_GETTEXT,
                     75:                     (WPARAM)dwSel,
                     76:                     (LPARAM)name);
                     77: 
                     78:         dwFlags = SND_ALIAS;
                     79: 
                     80:                fSync = (BOOL)SendDlgItemMessage(hDlg, IDSND_SYNC, BM_GETCHECK, 0, 0);
                     81: 
                     82:         if (fSync) {
                     83:                        // Turn off ASYNC
                     84:                        dwFlags &= ~SND_ASYNC;
                     85:                } else {
                     86:                        dwFlags |= SND_ASYNC;
                     87:                }
                     88: 
                     89:                fWait = (BOOL)SendDlgItemMessage(hDlg, IDSND_WAIT, BM_GETCHECK, 0, 0);
                     90:         if (!fWait) dwFlags |= SND_NOWAIT;
                     91: 
                     92:                if (1 == SendDlgItemMessage(hDlg, IDSND_IDPLAY, BM_GETCHECK, 0, 0)) {
                     93:                        if (0 != (alias = TranslateNameToAlias(name))) {
                     94:                                dwFlags |= SND_ALIAS_ID;
                     95:                                pName = (LPSTR)alias;
                     96:                        }
                     97:                }
                     98: 
1.1.1.2 ! root       99:         if (!PlaySound(pName, NULL, dwFlags | bNoDefault)) {
1.1       root      100:             Error("Failed to play alias: %s", name);
                    101:         }
                    102:     }
                    103: 
                    104: }
                    105: 
1.1.1.2 ! root      106: VOID SetSoundName(HWND hDlg)
        !           107: {
        !           108:     DWORD dwSel;
        !           109:     char name[40];
        !           110:        char filename[MAX_PATH];
        !           111:        UINT n;
        !           112: 
        !           113:     dwSel = SendDlgItemMessage(hDlg, IDSND_LIST, LB_GETCURSEL, 0, 0);
        !           114:     if (dwSel != LB_ERR
        !           115:         && SendDlgItemMessage(hDlg, IDSND_LIST,
        !           116:                     LB_GETTEXT,
        !           117:                     (WPARAM)dwSel,
        !           118:                     (LPARAM)name)
        !           119:                && (n=GetProfileString("SOUNDS", name, NULL, filename, sizeof(filename)))
        !           120:        ) {
        !           121:                do {
        !           122:                   n--;
        !           123:                   if (filename[n] == ',') {
        !           124:                           filename[n]=0;
        !           125:                   }
        !           126:                }  while (filename[n]);
        !           127:                GetFileTitle(filename, name, sizeof(name));
        !           128:                SendDlgItemMessage(hDlg, IDSND_SOUNDNAME, WM_SETTEXT, 0, (LPARAM)name);
        !           129:        } else {
        !           130:                SendDlgItemMessage(hDlg, IDSND_SOUNDNAME, WM_SETTEXT, 0, (LPARAM)"<no selection>");
        !           131:        }
        !           132: }
        !           133: 
1.1       root      134: LONG SoundDlgProc(HWND hDlg, UINT msg, DWORD wParam, LONG lParam)
                    135: {
                    136:     LPSTR lpBuf, lpKey;
                    137:     DWORD dwChars;
                    138: 
1.1.1.2 ! root      139: //  dprintf4(("SoundDlgProc: %8.8XH, %8.8XH, %8.8XH", msg, wParam, lParam));
1.1       root      140: 
                    141:     switch (msg) {
                    142:     case WM_INITDIALOG:
                    143:         // fill the listbox with the keys in the [sounds]
                    144:         // section of win.ini
                    145: 
                    146:         // allocate a buffer to put the keys in
                    147:         WinEval(lpBuf = (LPSTR) GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, KEYBUFSIZE));
                    148: 
                    149:         // get the key list
                    150:         dwChars = GetProfileString("sounds", NULL, "", lpBuf, KEYBUFSIZE);
1.1.1.2 ! root      151:         dprintf4(("%lu chars read from [sounds] section", dwChars));
1.1       root      152: 
                    153:         if (dwChars > 0) {
                    154:             // add each entry to the list box
                    155:             lpKey = lpBuf;
                    156:             while (*lpKey) {
                    157:                                // TEMPORARY SECTION
                    158:                                if (lstrcmpi((LPCTSTR)lpKey, "enable"))  // if not ENABLE
                    159:                                // END OF TEMPORARY SECTION
                    160:                 SendMessage(GetDlgItem(hDlg, IDSND_LIST),
                    161:                             LB_ADDSTRING,
                    162:                             0,
                    163:                             (LONG)lpKey);
                    164:                 lpKey += strlen(lpKey) + 1;
                    165:             }
                    166:         } else {
                    167:             // show there aren't any
                    168:             SendMessage(GetDlgItem(hDlg, IDSND_LIST),
                    169:                         LB_ADDSTRING,
                    170:                         0,
                    171:                         (LONG)(LPSTR)"[none]");
                    172:         }
1.1.1.2 ! root      173:                SendDlgItemMessage(hDlg, IDSND_SOUNDNAME, WM_SETTEXT, 0, (LPARAM)"");
1.1       root      174: 
                    175:                if (bSync) {
                    176:                        // Set the initial state of the Sync checkbox from global flag
                    177:                        SendDlgItemMessage(hDlg, IDSND_SYNC, BM_SETCHECK, 1, 0);
                    178:                }
                    179: 
                    180:                if (!bNoWait) {
                    181:                        // Set the initial state of the Wait checkbox from global flag
                    182:                        SendDlgItemMessage(hDlg, IDSND_WAIT, BM_SETCHECK, 1, 0);
                    183:                }
                    184: 
                    185:         GlobalFree((HANDLE)lpBuf);
                    186: 
                    187:         // disable the play button till we get a selection
                    188:         EnableWindow(GetDlgItem(hDlg, IDSND_PLAY), FALSE);
                    189: 
                    190:         break;
                    191: 
                    192:     case WM_COMMAND:
1.1.1.2 ! root      193:         dprintf4(("WM_COMMAND: %08lXH, %08lX", wParam, lParam));
1.1       root      194:         switch (LOWORD(wParam)) {
                    195:         case IDOK:
                    196:             EndDialog(hDlg, TRUE);
                    197:             break;
                    198: 
                    199:         case IDSND_PLAY:
                    200:             // get the current selection and try to play it
                    201:             PlaySelection(hDlg);
                    202:             break;
                    203: 
                    204:         case IDSND_LIST:
                    205:             switch (HIWORD(wParam)){
                    206:             case LBN_SELCHANGE:
                    207:                 // enable the play button
                    208:                 EnableWindow(GetDlgItem(hDlg, IDSND_PLAY), TRUE);
1.1.1.2 ! root      209:                 dprintf3(("Play button enabled"));
        !           210:                                // Set the current sound name
        !           211:                                SetSoundName(hDlg);
1.1       root      212:                 break;
                    213: 
                    214:             case LBN_DBLCLK:
                    215:                 PlaySelection(hDlg);
                    216:                 break;
                    217: 
                    218:             default:
                    219:                 break;
                    220:             }
                    221:             break;
                    222: 
                    223:         default:
                    224:             break;
                    225:         }
                    226:         break;
                    227: 
                    228:     default:
                    229:         return FALSE; // say we didn't handle it
                    230:         break;
                    231:     }
                    232: 
                    233:     return TRUE; // say we handled it
                    234: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.