home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 December (Special) / DOSV2002_12.iso / utility / tcl230ja95.lzh / source.lzh / exe / soundselect.c < prev    next >
C/C++ Source or Header  |  2001-02-09  |  7KB  |  269 lines

  1. /*-------------------------------------------
  2.   soundselect.c
  3.   select a sound file with "Open" dialog
  4.   KAZUBON 1997-2001
  5. ---------------------------------------------*/
  6.  
  7. #include "tclock.h"
  8.  
  9. BOOL CALLBACK HookProcAlarm(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  10. static void OnInitDialog(HWND hDlg);
  11. static void OnFileNameChanged(HWND hDlg);
  12. static void OnTestSound(HWND hDlg);
  13. static void GetMMFileExts(char* dst);
  14. static BOOL bPlaying = FALSE;
  15.  
  16. // OPENFILENAME struct for Win Me/2000
  17. typedef struct _tagOFNA {
  18.    DWORD        lStructSize;
  19.    HWND         hwndOwner;
  20.    HINSTANCE    hInstance;
  21.    LPCSTR       lpstrFilter;
  22.    LPSTR        lpstrCustomFilter;
  23.    DWORD        nMaxCustFilter;
  24.    DWORD        nFilterIndex;
  25.    LPSTR        lpstrFile;
  26.    DWORD        nMaxFile;
  27.    LPSTR        lpstrFileTitle;
  28.    DWORD        nMaxFileTitle;
  29.    LPCSTR       lpstrInitialDir;
  30.    LPCSTR       lpstrTitle;
  31.    DWORD        Flags;
  32.    WORD         nFileOffset;
  33.    WORD         nFileExtension;
  34.    LPCSTR       lpstrDefExt;
  35.    LPARAM       lCustData;
  36.    LPOFNHOOKPROC lpfnHook;
  37.    LPCSTR       lpTemplateName;
  38.    void *       pvReserved;
  39.    DWORD        dwReserved;
  40.    DWORD        FlagsEx;
  41. } _OPENFILENAMEA, *_LPOPENFILENAMEA;
  42.  
  43. /*------------------------------------------------
  44.    open dialog to browse sound files
  45. --------------------------------------------------*/
  46. BOOL BrowseSoundFile(HWND hDlg, const char *deffile, char *fname)
  47. {
  48.     _OPENFILENAMEA ofn;
  49.     OSVERSIONINFO osver;
  50.     char filter[1024], mmfileexts[1024];
  51.     char ftitle[MAX_PATH], initdir[MAX_PATH];
  52.     
  53.     memset(&ofn, '\0', sizeof(_OPENFILENAMEA));
  54.     
  55.     filter[0] = filter[1] = 0;
  56.     str0cat(filter, MyString(IDS_MMFILE));
  57.     GetMMFileExts(mmfileexts);
  58.     str0cat(filter, mmfileexts);
  59.     str0cat(filter, MyString(IDS_ALLFILE));
  60.     str0cat(filter, "*.*");
  61.     
  62.     if(deffile[0] == 0 || IsMMFile(deffile))
  63.         ofn.nFilterIndex = 1;
  64.     else ofn.nFilterIndex = 2;
  65.     
  66.     strcpy(initdir, g_mydir);
  67.     if(deffile[0])
  68.     {
  69.         WIN32_FIND_DATA fd;
  70.         HANDLE hfind;
  71.         hfind = FindFirstFile(deffile, &fd);
  72.         if(hfind != INVALID_HANDLE_VALUE)
  73.         {
  74.             FindClose(hfind);
  75.             strcpy(initdir, deffile);
  76.             del_title(initdir);
  77.         }
  78.     }
  79.     
  80.     fname[0] = 0;
  81.     
  82.     osver.dwOSVersionInfoSize = sizeof(osver);
  83.     GetVersionEx(&osver);
  84.     if((osver.dwPlatformId == VER_PLATFORM_WIN32_NT &&  // Win 2000
  85.             osver.dwMajorVersion == 5)
  86.      ||(osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&  // Win Me
  87.             osver.dwMinorVersion == 90))
  88.     {
  89.         ofn.lStructSize = sizeof(_OPENFILENAMEA);
  90.     }
  91.     else ofn.lStructSize = sizeof(OPENFILENAME);
  92.     
  93.     ofn.hwndOwner = hDlg;
  94.     ofn.hInstance = g_hInstResource;
  95.     ofn.lpstrFilter = filter;
  96.     ofn.lpstrFile= fname;
  97.     ofn.nMaxFile = MAX_PATH;
  98.     ofn.lpstrFileTitle = ftitle;
  99.     ofn.nMaxFileTitle = MAX_PATH;
  100.     ofn.lpstrInitialDir = initdir;
  101.     ofn.lpfnHook = HookProcAlarm;
  102.     ofn.lpTemplateName = MAKEINTRESOURCE(IDD_TESTSOUND);
  103.     ofn.Flags = OFN_HIDEREADONLY|OFN_EXPLORER|OFN_FILEMUSTEXIST|
  104.         OFN_ENABLEHOOK| OFN_ENABLETEMPLATE;
  105.     
  106.     return GetOpenFileName((LPOPENFILENAME)&ofn);
  107. }
  108.  
  109. /*------------------------------------------------
  110.  MCIファイルかどうか
  111. --------------------------------------------------*/
  112. BOOL IsMMFile(const char* fname)
  113. {
  114.     char s[1024], *sp;
  115.     
  116.     if(lstrcmpi(fname, "cdaudio") == 0) return TRUE;
  117.  
  118.     GetProfileString("mci extensions", NULL, "",
  119.         s, 1024);
  120.     
  121.     sp = s;
  122.     while(*sp)
  123.     {
  124.         if(ext_cmp(fname, sp) == 0) return TRUE;
  125.         while(*sp) sp++; sp++;
  126.     }
  127.     return FALSE;
  128. }
  129.  
  130. /*------------------------------------------------
  131.  コモンダイアログのフックプロシージャ
  132.  参考:Visual C++ 4.xのサンプルCOMDLG32
  133. --------------------------------------------------*/
  134. BOOL CALLBACK HookProcAlarm(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  135. {
  136.     switch(message)
  137.     {
  138.         case WM_INITDIALOG:
  139.             OnInitDialog(hDlg);
  140.             break;
  141.         case WM_DESTROY:
  142.             if(bPlaying) StopFile(); bPlaying = FALSE;
  143.             break;
  144.         case WM_NOTIFY:
  145.             switch(((LPOFNOTIFY)lParam)->hdr.code)
  146.             {
  147.                 case CDN_SELCHANGE:
  148.                 case CDN_FOLDERCHANGE:
  149.                     OnFileNameChanged(hDlg);
  150.                 break;
  151.             }
  152.             return FALSE;
  153.         case WM_COMMAND:
  154.             if(LOWORD(wParam) == IDC_TESTSOUND)
  155.                 OnTestSound(hDlg);
  156.             return FALSE;
  157.         case MM_MCINOTIFY:
  158.         case MM_WOM_DONE:
  159.             StopFile(); bPlaying = FALSE;
  160.             SendDlgItemMessage(hDlg, IDC_TESTSOUND, BM_SETIMAGE, IMAGE_ICON,
  161.                 (LPARAM)g_hIconPlay);
  162.             InvalidateRect(GetDlgItem(hDlg, IDC_TESTSOUND), NULL, FALSE);
  163.             return FALSE;
  164.         default:
  165.             return FALSE;
  166.     }
  167.     return TRUE;
  168. }
  169.  
  170. void OnInitDialog(HWND hDlg)
  171. {
  172.     HWND hwndStatic;
  173.     RECT rc1, rc2;
  174.     POINT pt;
  175.     int dx;
  176.     
  177.     SendDlgItemMessage(hDlg, IDC_TESTSOUND, BM_SETIMAGE, IMAGE_ICON,
  178.         (LPARAM)g_hIconPlay);
  179.     EnableDlgItem(hDlg, IDC_TESTSOUND, FALSE);
  180.     
  181.     bPlaying = FALSE;
  182.  
  183.     // find "File Name:" Label
  184.     hwndStatic = GetDlgItem(GetParent(hDlg), 0x442);
  185.     if(hwndStatic == NULL) return;
  186.     GetWindowRect(hwndStatic, &rc1);
  187.     // move "Test:" Label
  188.     GetWindowRect(GetDlgItem(hDlg, IDC_LABTESTSOUND), &rc2);
  189.     dx = rc1.left - rc2.left;
  190.     pt.x = rc2.left + dx; pt.y = rc2.top;
  191.     ScreenToClient(hDlg, &pt);
  192.     SetWindowPos(GetDlgItem(hDlg, IDC_LABTESTSOUND), NULL, pt.x, pt.y, 0, 0,
  193.         SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
  194.     // move play button
  195.     GetWindowRect(GetDlgItem(hDlg, IDC_TESTSOUND), &rc2);
  196.     pt.x = rc2.left + dx; pt.y = rc2.top;
  197.     ScreenToClient(hDlg, &pt);
  198.     SetWindowPos(GetDlgItem(hDlg, IDC_TESTSOUND), NULL, pt.x, pt.y, 0, 0,
  199.         SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
  200. }
  201.  
  202. void OnFileNameChanged(HWND hDlg)
  203. {
  204.     char fname[MAX_PATH];
  205.     WIN32_FIND_DATA fd;
  206.     HANDLE hfind = INVALID_HANDLE_VALUE;
  207.     BOOL b = FALSE;
  208.     
  209.     if (CommDlg_OpenSave_GetFilePath(GetParent(hDlg),
  210.         fname, sizeof(fname)) <= sizeof(fname))
  211.     {
  212.         hfind = FindFirstFile(fname, &fd);
  213.         if(hfind != INVALID_HANDLE_VALUE)
  214.         {
  215.             if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  216.                 b = TRUE;
  217.             FindClose(hfind);
  218.         }
  219.     }
  220.     EnableDlgItem(hDlg, IDC_TESTSOUND, b);
  221. }
  222.  
  223. void OnTestSound(HWND hDlg)
  224. {
  225.     char fname[MAX_PATH];
  226.     
  227.     if(CommDlg_OpenSave_GetFilePath(GetParent(hDlg),
  228.             fname, sizeof(fname)) <= sizeof(fname))
  229.     {
  230.         if((HICON)SendDlgItemMessage(hDlg, IDC_TESTSOUND,
  231.             BM_GETIMAGE, IMAGE_ICON, 0) == g_hIconPlay)
  232.         {
  233.             if(PlayFile(hDlg, fname, 0))
  234.             {
  235.                 SendDlgItemMessage(hDlg, IDC_TESTSOUND,
  236.                     BM_SETIMAGE, IMAGE_ICON, (LPARAM)g_hIconStop);
  237.                 InvalidateRect(GetDlgItem(hDlg, IDC_TESTSOUND), NULL, FALSE);
  238.                 bPlaying = TRUE;
  239.             }
  240.         }
  241.         else
  242.         {
  243.             StopFile(); bPlaying = FALSE;
  244.         }
  245.     }
  246. }
  247.  
  248. /*------------------------------------------------
  249.  WIN.INIからMCIファイルの拡張子を取得
  250. --------------------------------------------------*/
  251. void GetMMFileExts(char* dst)
  252. {
  253.     char s[1024], *sp, *dp;
  254.     
  255.     GetProfileString("mci extensions", NULL, "",
  256.         s, 1024);
  257.  
  258.     sp = s; dp = dst;
  259.     while(*sp)
  260.     {
  261.         if(dp != dst) *dp++ = ';';
  262.         *dp++ = '*'; *dp++ = '.';
  263.         while(*sp) *dp++ = *sp++;
  264.         sp++;
  265.     }
  266.     *dp = 0;
  267. }
  268.  
  269.