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

  1. /*-------------------------------------------
  2.   timer.c
  3.            Kazubon 1998-1999
  4. ---------------------------------------------*/
  5.  
  6. #include "tclock.h"
  7.  
  8. // structure for timer setting
  9. typedef struct _tagTimerStruct
  10. {
  11.     char name[80];
  12.     int id;
  13.     int minute;
  14.     char fname[1024];
  15.     BOOL bRepeat;
  16.     BOOL bBlink;
  17. } TIMERSTRUCT;
  18. typedef TIMERSTRUCT* PTIMERSTRUCT;
  19.  
  20. // structure for timer executing
  21. typedef struct _tagTimerStruct2
  22. {
  23.     char name[80];
  24.     int id;
  25.     DWORD seconds;
  26.     DWORD tickonstart;
  27. } TIMERSTRUCT2;
  28. typedef TIMERSTRUCT2* PTIMERSTRUCT2;
  29.  
  30. BOOL CALLBACK DlgProcTimer(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  31. static void OnInit(HWND hDlg);
  32. static void OnDestroy(HWND hDlg);
  33. static void OnOK(HWND hDlg);
  34. static void OnTimerName(HWND hDlg);
  35. static void OnSanshoAlarm(HWND hDlg, WORD id);
  36. static void OnDel(HWND hDlg);
  37. static void OnTest(HWND hDlg, WORD id);
  38. static void Ring(HWND hwnd, int id);
  39.  
  40. static int nTimerCount = 0;
  41. static PTIMERSTRUCT2 pTimersWorking = NULL;
  42.  
  43. // propsheet.c
  44. void SetMyDialgPos(HWND hwnd);
  45.  
  46. /*------------------------------------------------
  47.    open "Timer" dialog
  48. --------------------------------------------------*/
  49. void DialogTimer(HWND hwnd)
  50. {
  51.     HWND hwndTray;
  52.     
  53.     hwndTray = FindWindow("Shell_TrayWnd", NULL);
  54.     if(g_hDlgTimer && IsWindow(g_hDlgTimer))
  55.         ;
  56.     else
  57.         g_hDlgTimer = CreateDialog(GetLangModule(), MAKEINTRESOURCE(IDD_TIMER),
  58.             NULL, (DLGPROC)DlgProcTimer);
  59.     SetForegroundWindow98(g_hDlgTimer);
  60. }
  61.  
  62. /*------------------------------------------------
  63.    dialog procedure of "Timer" dialog
  64. --------------------------------------------------*/
  65. BOOL CALLBACK DlgProcTimer(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  66. {
  67.     switch(message)
  68.     {
  69.         case WM_INITDIALOG:
  70.             SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)g_hIconTClock);
  71.             OnInit(hDlg);
  72.             SetMyDialgPos(hDlg);
  73.             return TRUE;
  74.         case WM_COMMAND:
  75.         {
  76.             WORD id, code;
  77.             id = LOWORD(wParam); code = HIWORD(wParam);
  78.             switch(id)
  79.             {
  80.                 case IDC_TIMERNAME:
  81.                     if(code == CBN_EDITCHANGE)
  82.                         OnTimerName(hDlg);
  83.                     else if(code == CBN_SELCHANGE)
  84.                         PostMessage(hDlg, WM_COMMAND,
  85.                             MAKELONG(id, CBN_EDITCHANGE), 0);
  86.                     break;
  87.                 case IDC_TIMERDEL:
  88.                     OnDel(hDlg);
  89.                     break;
  90.                 case IDC_TIMERMINUTE:
  91.                     break;
  92.                 case IDC_TIMERFILE:
  93.                     break;
  94.                 case IDC_TIMERSANSHO:
  95.                     OnSanshoAlarm(hDlg, id); break;
  96.                 case IDC_TIMERTEST:
  97.                     OnTest(hDlg, id); break;
  98.                 case IDC_TIMERHELP:
  99.                     MyHelp(GetParent(hDlg), 8); break;
  100.                 case IDOK:
  101.                     OnOK(hDlg);
  102.                 case IDCANCEL:
  103.                     DestroyWindow(hDlg);
  104.             }
  105.             return TRUE;
  106.         }
  107.         case WM_DESTROY:
  108.             // MyHelp(hDlg, -1);
  109.             StopFile();
  110.             OnDestroy(hDlg);
  111.             g_hDlgTimer = NULL;
  112.             break;
  113.         case MM_MCINOTIFY:
  114.         case MM_WOM_DONE:
  115.             StopFile();
  116.             SendDlgItemMessage(hDlg, IDC_TIMERTEST, BM_SETIMAGE, IMAGE_ICON,
  117.                 (LPARAM)g_hIconPlay);
  118.             return TRUE;
  119.     }
  120.     return FALSE;
  121. }
  122.  
  123. /*------------------------------------------------
  124.    initialize "Timer" dialog
  125. --------------------------------------------------*/
  126. void OnInit(HWND hDlg)
  127. {
  128.     HFONT hfont;
  129.     char subkey[20];
  130.     int i, count;
  131.     
  132.     hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  133.     if(hfont)
  134.     {
  135.         SendDlgItemMessage(hDlg, IDC_TIMERNAME,
  136.             WM_SETFONT, (WPARAM)hfont, 0);
  137.         SendDlgItemMessage(hDlg, IDC_TIMERFILE,
  138.             WM_SETFONT, (WPARAM)hfont, 0);
  139.     }
  140.     
  141.     SendDlgItemMessage(hDlg, IDC_TIMERSPIN, UDM_SETRANGE, 0,
  142.         MAKELONG(1440, 0));
  143.     
  144.     count = GetMyRegLong("", "TimerNum", 0);
  145.     for(i = 0; i < count; i++)
  146.     {
  147.         PTIMERSTRUCT pts;
  148.         int index;
  149.         
  150.         pts = (PTIMERSTRUCT)malloc(sizeof(TIMERSTRUCT));
  151.         wsprintf(subkey, "Timer%d", i + 1);
  152.         GetMyRegStr(subkey, "Name", pts->name, 80, "");
  153.         pts->id = GetMyRegLong(subkey, "ID", 0);
  154.         pts->minute = GetMyRegLong(subkey, "Minute", 10);
  155.         GetMyRegStr(subkey, "File", pts->fname, 1024, "");
  156.         pts->bRepeat = GetMyRegLong(subkey, "Repeat", FALSE);
  157.         pts->bBlink = GetMyRegLong(subkey, "Blink", FALSE);
  158.         index = CBAddString(hDlg, IDC_TIMERNAME, (LPARAM)pts->name);
  159.         CBSetItemData(hDlg, IDC_TIMERNAME, index, (LPARAM)pts);
  160.     }
  161.     if(count > 0)
  162.         CBSetCurSel(hDlg, IDC_TIMERNAME, 0);
  163.     else
  164.         SendDlgItemMessage(hDlg, IDC_TIMERSPIN, UDM_SETPOS, 0, 10);
  165.     OnTimerName(hDlg);
  166.     
  167.     SendDlgItemMessage(hDlg, IDC_TIMERTEST, BM_SETIMAGE, IMAGE_ICON,
  168.         (LPARAM)g_hIconPlay);
  169.     
  170.     SendDlgItemMessage(hDlg, IDC_TIMERDEL, BM_SETIMAGE, IMAGE_ICON,
  171.         (LPARAM)g_hIconDel);
  172. }
  173.  
  174. /*------------------------------------------------
  175.   free memories associated with combo box.
  176. --------------------------------------------------*/
  177. void OnDestroy(HWND hDlg)
  178. {
  179.     int i, count;
  180.     
  181.     count = CBGetCount(hDlg, IDC_TIMERNAME);
  182.     for(i = 0; i < count; i++)
  183.     {
  184.         PTIMERSTRUCT pts;
  185.         pts = (PTIMERSTRUCT)CBGetItemData(hDlg, IDC_TIMERNAME, i);
  186.         free(pts);
  187.     }
  188. }
  189.  
  190. /*------------------------------------------------
  191.    called when "OK" button is clicked,
  192.    start timer
  193. --------------------------------------------------*/
  194. void OnOK(HWND hDlg)
  195. {
  196.     char subkey[20], s[1024], name[80];
  197.     int i, j, id, count, minutes;
  198.     PTIMERSTRUCT2 temp;
  199.     
  200.     GetDlgItemText(hDlg, IDC_TIMERNAME, s, 80);
  201.     strcpy(name, s);
  202.     
  203.     // save settings
  204.     j = 1; id = -1;
  205.     count = CBGetCount(hDlg, IDC_TIMERNAME);
  206.     for(i = 0; i < count; i++)
  207.     {
  208.         PTIMERSTRUCT pts;
  209.         pts = (PTIMERSTRUCT)CBGetItemData(hDlg, IDC_TIMERNAME, i);
  210.         if(strcmp(pts->name, name) != 0)
  211.         {
  212.             wsprintf(subkey, "Timer%d", j + 1);
  213.             SetMyRegStr(subkey, "Name", pts->name);
  214.             SetMyRegLong(subkey, "ID", pts->id);
  215.             SetMyRegLong(subkey, "Minute", pts->minute);
  216.             SetMyRegStr(subkey, "File", pts->fname);
  217.             SetMyRegLong(subkey, "Repeat", pts->bRepeat);
  218.             SetMyRegLong(subkey, "Blink", pts->bBlink);
  219.             j++;
  220.         }
  221.         else id = pts->id;
  222.     }
  223.     SetMyRegLong("", "TimerNum", j);
  224.     
  225.     if(id < 0)
  226.     {
  227.         id = 0;
  228.         for(i = 0; i < count; i++)
  229.         {
  230.             PTIMERSTRUCT pts;
  231.             pts = (PTIMERSTRUCT)CBGetItemData(hDlg, IDC_TIMERNAME, i);
  232.             if(pts->id >= id) id = pts->id + 1;
  233.         }
  234.     }
  235.     strcpy(subkey, "Timer1");
  236.     SetMyRegStr(subkey, "Name", name);
  237.     SetMyRegLong(subkey, "ID", id);
  238.     minutes = SendDlgItemMessage(hDlg, IDC_TIMERSPIN, UDM_GETPOS, 0, 0);
  239.     SetMyRegLong(subkey, "Minute", minutes);
  240.     GetDlgItemText(hDlg, IDC_TIMERFILE, s, MAX_PATH);
  241.     SetMyRegStr(subkey, "File", s);
  242.     SetMyRegLong(subkey, "Repeat",
  243.         IsDlgButtonChecked(hDlg, IDC_TIMERREPEAT));
  244.     SetMyRegLong(subkey, "Blink",
  245.         IsDlgButtonChecked(hDlg, IDC_TIMERBLINK));
  246.     
  247.     // start timer
  248.     temp = pTimersWorking;
  249.     pTimersWorking = (PTIMERSTRUCT2)malloc(
  250.         sizeof(TIMERSTRUCT2)*(nTimerCount + 1));
  251.     for(i = 0; i < nTimerCount; i++) pTimersWorking[i] = temp[i];
  252.     if(temp) free(temp);
  253.     strcpy(pTimersWorking[i].name, name);
  254.     pTimersWorking[i].id = id;
  255.     pTimersWorking[i].seconds = minutes * 60;
  256.     pTimersWorking[i].tickonstart = GetTickCount();
  257.     
  258.     nTimerCount++;
  259. }
  260.  
  261. /*------------------------------------------------
  262.    called when "Name" edit box is changed
  263. --------------------------------------------------*/
  264. void OnTimerName(HWND hDlg)
  265. {
  266.     char s[1024];
  267.     int i, count;
  268.     
  269.     GetDlgItemText(hDlg, IDC_TIMERNAME, s, 80);
  270.     count = CBGetCount(hDlg, IDC_TIMERNAME);
  271.     for(i = 0; i < count; i++)
  272.     {
  273.         PTIMERSTRUCT pts;
  274.         pts = (PTIMERSTRUCT)CBGetItemData(hDlg, IDC_TIMERNAME, i);
  275.         if(strcmp(s, pts->name) == 0)
  276.         {
  277.             SetDlgItemInt(hDlg, IDC_TIMERMINUTE, pts->minute, FALSE);
  278.             SetDlgItemText(hDlg, IDC_TIMERFILE, pts->fname);
  279.             CheckDlgButton(hDlg, IDC_TIMERREPEAT, pts->bRepeat);
  280.             CheckDlgButton(hDlg, IDC_TIMERBLINK, pts->bBlink);
  281.             break;
  282.         }
  283.     }
  284.     
  285.     EnableDlgItem(hDlg, IDC_TIMERDEL, i < count);
  286. }
  287.  
  288. /*------------------------------------------------
  289.   browse sound file
  290. --------------------------------------------------*/
  291. void OnSanshoAlarm(HWND hDlg, WORD id)
  292. {
  293.     char deffile[MAX_PATH], fname[MAX_PATH];
  294.     
  295.     GetDlgItemText(hDlg, id - 1, deffile, MAX_PATH);
  296.     
  297.     if(!BrowseSoundFile(hDlg, deffile, fname)) // soundselect.c
  298.         return;
  299.     
  300.     SetDlgItemText(hDlg, id - 1, fname);
  301.     PostMessage(hDlg, WM_NEXTDLGCTL, 1, FALSE);
  302. }
  303.  
  304. /*------------------------------------------------
  305.    delete one setting of timer
  306. --------------------------------------------------*/
  307. void OnDel(HWND hDlg)
  308. {
  309.     char s[1024], subkey[20];
  310.     int i, count;
  311.     
  312.     GetDlgItemText(hDlg, IDC_TIMERNAME, s, 80);
  313.     count = CBGetCount(hDlg, IDC_TIMERNAME);
  314.     for(i = 0; i < count; i++)
  315.     {
  316.         PTIMERSTRUCT pts;
  317.         pts = (PTIMERSTRUCT)CBGetItemData(hDlg, IDC_TIMERNAME, i);
  318.         if(strcmp(s, pts->name) == 0) { free(pts); break; }
  319.     }
  320.     if(i >= count) return;
  321.     
  322.     CBDeleteString(hDlg, IDC_TIMERNAME, i);
  323.     if(count > 1)
  324.         CBSetCurSel(hDlg, IDC_TIMERNAME, (i>0)?(i-1):i);
  325.     else
  326.         SetDlgItemText(hDlg, IDC_TIMERNAME, "");
  327.     OnTimerName(hDlg);
  328.     PostMessage(hDlg, WM_NEXTDLGCTL, 1, FALSE);
  329.     
  330.     wsprintf(subkey, "Timer%d", count);
  331.     DelMyRegKey(subkey);
  332.     
  333.     for(i = 0; i < count - 1; i++)
  334.     {
  335.         PTIMERSTRUCT pts;
  336.         pts = (PTIMERSTRUCT)CBGetItemData(hDlg, IDC_TIMERNAME, i);
  337.         wsprintf(subkey, "Timer%d", i + 1);
  338.         SetMyRegStr(subkey, "Name", pts->name);
  339.         SetMyRegLong(subkey, "ID", pts->id);
  340.         SetMyRegLong(subkey, "Minute", pts->minute);
  341.         SetMyRegStr(subkey, "File", pts->fname);
  342.         SetMyRegLong(subkey, "Repeat", pts->bRepeat);
  343.         SetMyRegLong(subkey, "Blink", pts->bBlink);
  344.     }
  345.     SetMyRegLong("", "TimerNum", count - 1);
  346. }
  347.  
  348. /*------------------------------------------------
  349.    play or stop sound in dialog
  350. --------------------------------------------------*/
  351. void OnTest(HWND hDlg, WORD id)
  352. {
  353.     char fname[MAX_PATH];
  354.     
  355.     GetDlgItemText(hDlg, id - 3, fname, MAX_PATH);
  356.     if(fname[0] == 0) return;
  357.  
  358.     if((HICON)SendDlgItemMessage(hDlg, id, BM_GETIMAGE, IMAGE_ICON, 0)
  359.         == g_hIconPlay)
  360.     {
  361.         if(PlayFile(hDlg, fname, 0))
  362.         {
  363.             SendDlgItemMessage(hDlg, id, BM_SETIMAGE, IMAGE_ICON,
  364.                 (LPARAM)g_hIconStop);
  365.             InvalidateRect(GetDlgItem(hDlg, id), NULL, FALSE);
  366.         }
  367.     }
  368.     else StopFile();
  369. }
  370.  
  371. /*------------------------------------------------
  372.     called when main window receives WM_TIMER
  373. --------------------------------------------------*/
  374. void CheckTimer(HWND hwnd, SYSTEMTIME* st)
  375. {
  376.     int i;
  377.     DWORD tick;
  378.     
  379.     if(nTimerCount == 0) return;
  380.     tick = GetTickCount();
  381.     for(i = 0; i < nTimerCount; i++)
  382.     {
  383.         DWORD seconds;
  384.         
  385.         seconds = (tick - pTimersWorking[i].tickonstart) / 1000;
  386.         if(seconds > pTimersWorking[i].seconds)
  387.         {
  388.             Ring(hwnd, pTimersWorking[i].id);
  389.             StopTimer(hwnd, i);
  390.             i--;
  391.         }
  392.     }
  393. }
  394.  
  395. /*------------------------------------------------
  396.     sound or open file of timer.
  397. --------------------------------------------------*/
  398. void Ring(HWND hwnd, int id)
  399. {
  400.     char subkey[20], fname[1024];
  401.     int i, count;
  402.     
  403.     count = GetMyRegLong("", "TimerNum", 0);
  404.     for(i = 0; i < count; i++)
  405.     {
  406.         wsprintf(subkey, "Timer%d", i + 1);
  407.         if(id == GetMyRegLong(subkey, "ID", 0))
  408.         {
  409.             GetMyRegStr(subkey, "File", fname, 1024, "");
  410.             PlayFile(hwnd, fname,
  411.                 GetMyRegLong(subkey, "Repeat", FALSE)?(-1):0);
  412.             if(GetMyRegLong(subkey, "Blink", FALSE))
  413.                 PostMessage(g_hwndClock, CLOCKM_BLINK, FALSE, 0);
  414.             break;
  415.         }
  416.     }
  417. }
  418.  
  419. /*------------------------------------------------
  420.     clear all timer
  421. --------------------------------------------------*/
  422. void EndTimer(void)
  423. {
  424.     if(pTimersWorking) free(pTimersWorking);
  425.     nTimerCount = 0;
  426. }
  427.  
  428. /*----------------------------------------------------------------
  429.     get timer name and remained time for popup menu.
  430. ------------------------------------------------------------------*/
  431. int GetTimerInfo(char *dst, int num)
  432. {
  433.     DWORD tick;
  434.     int seconds;
  435.     
  436.     if(num < nTimerCount)
  437.     {
  438.         tick = GetTickCount();
  439.         seconds = (tick - pTimersWorking[num].tickonstart) / 1000;
  440.         seconds = pTimersWorking[num].seconds - seconds;
  441.         wsprintf(dst, "  %s %02d:%02d",
  442.             pTimersWorking[num].name, seconds / 60, seconds % 60);
  443.         return strlen(dst);
  444.     }
  445.     return 0;
  446. }
  447.  
  448. /*---------------------------------------------------------------
  449.     free memory to clear a timer
  450. -----------------------------------------------------------------*/
  451. void StopTimer(HWND hwnd, int tostop)
  452. {
  453.     PTIMERSTRUCT2 temp;
  454.     int i, j;
  455.     
  456.     if(tostop >= nTimerCount) return;
  457.     
  458.     temp = pTimersWorking;
  459.     if(nTimerCount > 1)
  460.     {
  461.         pTimersWorking = (PTIMERSTRUCT2)malloc(
  462.             sizeof(TIMERSTRUCT2)*(nTimerCount - 1));
  463.         for(i = 0, j = 0; i < nTimerCount; i++)
  464.         {
  465.             if(tostop != i) pTimersWorking[j++] = temp[i];
  466.         }
  467.     }
  468.     else pTimersWorking = NULL;
  469.     if(temp) free(temp);
  470.     nTimerCount--;
  471. }
  472.  
  473.