#include <stdio.h>
#include "window.h"
#include "keys.h"

#define ID_STOP     100
#define ID_START    101
#define ID_EXIT     102
#define ID_INTERVAL 105

#define NUMTIMERS  8
HWND hTimerWnd[NUMTIMERS];

HWND hMain;
HWND hMenu;
extern int pascal TimerWndProc();


main()
{
  EVENT  event;
  int    i;
  char   title[16];
  HWND   hWnd;

  WinInit();

  hMain = WinCreate(NULLHWND, 0, 0, 24, 79, "Timer Demo", 0x70,
          	(WIN_HAS_BORDER), NORMAL_CLASS, 0);

  for (i = 1;  i <= NUMTIMERS;  i++)
  {
    sprintf(title, "Timer %d", i);

    hWnd = hTimerWnd[i-1] =
       WinCreate(hMain, 
                 ((i-1)/2)*5 + 3, ((i-1)%2) ? 40 : 20, 
                 ((i-1)/2)*5 + 6, ((i-1)%2) ? 65 : 45,
                 title, 0x70, WIN_HAS_BORDER | WS_CLIP, NORMAL_CLASS, 0);

    WinSetWinProc(hWnd, TimerWndProc);
    MenuInit(hWnd);
    SetMenu(hWnd, hMenu);
    SetTimer(hWnd, i, i*1000, (FARPROC) NULL);
  }

  WinDraw(hMain);
  SetFocus(hTimerWnd[0]);

  /*
    Main message loop
  */
  for (;;)
  {
    GetMessage(&event);
    TranslateAccelerator(&event);
    DispatchMessage(&event);
  }
}


MenuInit(HWND hWnd)
{
  HWND hPop1;

  /*
    Create the menu bar
  */
  hMenu = CreateMenu(hWnd);
  
  /*
    Create a pulldown
  */
  hPop1 = CreateMenu(hMenu);
  ChangeMenu(hPop1, 0, "~Stop",        ID_STOP,        MF_APPEND);
  ChangeMenu(hPop1, 0, "~Interval",    ID_INTERVAL,    MF_APPEND);
  ChangeMenu(hPop1, 0, "E~xit",        ID_EXIT,        MF_APPEND);

  /*
    Attach each of the pulldowns to their corresponding menubar entries
  */
  ChangeMenu(hMenu, 0, "~Timer",   hPop1, MF_POPUP | MF_APPEND);
}



int pascal TimerWndProc(hWnd, msg, wParam, lParam)
  HWND hWnd;
  WORD msg;
  WORD wParam;
  DWORD lParam;
{
  char szTime[80];
  int  hrs, min, sec;
  int  i;

  switch (msg)
  {
    case WM_TIMER :
      DosGetTime(&hrs, &min, &sec);
      sprintf(szTime, "%02d:%02d:%02d", hrs, min, sec);
      WinPutsCenter(hWnd, 1, szTime, WinGetAttr(hWnd));
      SoundNote(CNOTE + wParam, MIN_OCTAVE + wParam, 100);
      return TRUE;

    case WM_CHAR    :
      if (wParam == TAB)
      {
        for (i = 1;  i <= NUMTIMERS;  i++)
          if (hTimerWnd[i-1] == hWnd)
          {
            SetFocus(hTimerWnd[ (i == NUMTIMERS) ? 0 : i ]);
            return TRUE;
          }
      }
      break;

    case WM_SETFOCUS :
      WinDrawBorder(hWnd, 1);
      return DefWinProc(hWnd, msg, wParam, lParam);

    case WM_KILLFOCUS :
      WinDrawBorder(hWnd, 0);
      return DefWinProc(hWnd, msg, wParam, lParam);


    case WM_COMMAND :
      switch (wParam)
      {
        case ID_EXIT :
          exit(0);

        case ID_STOP :
          for (i = 1;  i <= NUMTIMERS;  i++)
            if (hTimerWnd[i-1] == hWnd)
            {
              KillTimer(i);
              ChangeMenu(GetSubMenu(GetMenu(hWnd), 0),
                         0, "Start", ID_START, MF_CHANGE | MF_STRING);
            }
          break;

        case ID_START:
          for (i = 1;  i <= NUMTIMERS;  i++)
            if (hTimerWnd[i-1] == hWnd)
            {
              SetTimer(hWnd, i, i*1000, (FARPROC) NULL);
              ChangeMenu(GetSubMenu(GetMenu(hWnd), 0),
                         0, "Stop", ID_STOP, MF_CHANGE | MF_STRING);
            }
          break;

        case ID_INTERVAL :
          NewInterval(hWnd);
          break;
      }
      return TRUE;

    default :
      return DefWinProc(hWnd, msg, wParam, lParam);
  }
}



NewInterval(hWnd)
  HWND hWnd;
{
  HDLG hDlg;
  int  i, tid;
  unsigned interval;
  extern BOOL pascal IntervalDlgProc();

  for (i = 1;  i <= NUMTIMERS;  i++)
    if (hTimerWnd[i-1] == hWnd)
    {
      tid = i;
      break;
    }

  hDlg = DialogCreate(NULLHWND, 8,26,13,51, "New Interval", 0x70, 0x0000,
                      IntervalDlgProc, 0);
  DialogAddItem(hDlg,
    StaticCreate(hDlg, 10,27,10,35, "Interval:", 0x70, STATIC_TEXT, 100));
  DialogAddItem(hDlg,
    EditCreate(hDlg, 10,38,10,48, "", 0x70, WIN_HAS_BORDER, ID_INTERVAL));
  DialogAddItem(hDlg,
    PushButtonCreate(hDlg, 12,30, "OK", 0x70, BUTTON_DEFAULT, IDOK));
  DialogAddItem(hDlg,
    PushButtonCreate(hDlg, 12,39, "CANCEL", 0x70, 0, IDCANCEL));

  if ((interval = DialogBox(hDlg)) != IDCANCEL)
  {
    KillTimer(tid);
    SetTimer(hWnd, tid, interval, (FARPROC) NULL);
  }
}


BOOL pascal IntervalDlgProc(hDlg, msg, wParam, lParam)
  HWND hDlg;
  WORD msg;
  WORD wParam;
  DWORD lParam;
{
  char szInterval[80];

  switch (msg)
  {
    case WM_INITDIALOG :
/*
      sprintf(szInterval, "%d", hWndInterval);
      SetDialogText(hDlg, ID_INTERVAL, szInterval);
*/
      return TRUE;

    case WM_QUIT :
      EndDialog(hDlg, IDCANCEL);
      break;

    case WM_COMMAND :
      if (wParam == IDCANCEL)
        EndDialog(hDlg, IDCANCEL);
      else if (wParam == IDOK)
      {
        GetDialogText(hDlg, ID_INTERVAL, (char far *) szInterval, 65);
        EndDialog(hDlg, atoi(szInterval));
      }
      return TRUE;

    case WM_CHAR :
      if (wParam == 27)
      {
        EndDialog(hDlg, IDCANCEL);
      }
      return FALSE;

    default :
      return FALSE;
  }
}

