/* mrr1conf.c */
/*  This file consists of the window procedure and support routines
 *  for the main window to the application MRR1 (prototype of WINC).
 */
#include "windows.h"
#include "mrr1.h"
#include "newmemos.h"
#include "winundoc.h"
#include "mrr1glob.h"

/****************************************************************************

    FUNCTION: Mrr1ConfWndProc(HWND, unsigned, WORD, LONG)

    PURPOSE:  Processes messages

    MESSAGES:

        WM_SYSCOMMAND - system menu (About dialog box)
        WM_CREATE     - create window
        WM_DESTROY    - destroy window
        WM_CHAR       - ASCII key value received
        WM_LBUTTONDOWN - left mouse button

    COMMENTS:


****************************************************************************/

long FAR PASCAL Mrr1ConfWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
unsigned message;
WORD wParam;
LONG lParam;
{
    FARPROC lpProcAbout;
    HMENU hMenu;

    PAINTSTRUCT ps;                              /* paint structure          */

    HDC hDC;                                    /* handle to display context */
    HWND hWndView;
    HDC  hDCView;
    RECT Rect;                                  /* selection rectangle      */

    static FARPROC lpfnMrr1CommDlg;
    static HWND    hInstance;

    int xScreen, yScreen;
    int X, Y, nWidth, nHeight;
    char chtimeicon[40];
    int OldSel = FALSE;

    switch (message) {
        case WM_SYSCOMMAND:
            if (wParam == ID_ABOUT) {
                lpProcAbout = MakeProcInstance(About, hInst);
                DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
                FreeProcInstance(lpProcAbout);
                break;
            }
            else
                return (DefWindowProc(hWnd, message, wParam, lParam));

        case WM_CREATE:

            /* Set the timer for five-second intervals */

        /*  idTimer =  SetTimer(hWnd, NULL, 5000, (FARPROC) NULL);  */

            /* Get handle to the hourglass cursor */

            hHourGlass = LoadCursor(hInst, IDC_WAIT);

            hMenu = GetSystemMenu(hWnd, FALSE);
            ChangeMenu(hMenu, NULL, NULL, NULL, MF_APPEND | MF_SEPARATOR);
            ChangeMenu(hMenu, NULL, "A&bout Mrr1...", ID_ABOUT,
                MF_APPEND | MF_STRING);

            hInstance = ((LPCREATESTRUCT) lParam) -> hInstance;
            lpfnMrr1CommDlg = MakeProcInstance(Mrr1CommDlg, hInstance);

            CheckView(hWnd);
            break;

        case WM_CHAR:
            if (wParam == '\r') {
                GetCursorPos(&ptCursor);
                ScreenToClient(hWnd, &ptCursor);
                X = ptCursor.x;
                Y = ptCursor.y;
                goto SelMemoPos;
            }
            break;

        case WM_LBUTTONDOWN:
          /*  Clicking the left button on a file name toggles the
           *  selected/not selected status of that file name.
           *  Currently selected files are displayed in reverse video.
           */
            X = LOWORD(lParam);
            Y = HIWORD(lParam);
          SelMemoPos:;
            if(Y < TopSpace || Y > TopSpace + NMemosVert*LineHeight)  break;
            if(X < SideSpace) {
              break;
            }
            if(((X-SideSpace) % (MEMONAMECHARS*CharWidth)) >=
              MAXMEMONAME*CharWidth) {
                break;
            }
            il = (Y - TopSpace) / LineHeight;
            irow = (X - SideSpace) / (MEMONAMECHARS*CharWidth);
            imemo = irow*NMemosVert + il;
            if(imemo < NMemos) {
              MemoSel[imemo] = !MemoSel[imemo];
            }
            InvalidateRect(hWnd, NULL, FALSE);
            break;

        case WM_TIMER:
            nTimerCount += 5;
            if(IsIconic(hWnd)) {
              sprintf(chtimeicon,"%d",nTimerCount);
              hDC = GetDC(hWnd);
              TextOut(hDC,10,15,chtimeicon,strlen(chtimeicon));
              ReleaseDC(hWnd,hDC);
            } else {
              InvalidateRect(hWnd, NULL, FALSE);
            }
            break;

        case WM_HSCROLL:
        case WM_VSCROLL:
            InvalidateRect(hWnd, NULL, FALSE);
            break;

        case WM_COMMAND:
            switch(wParam) {

              case IDM_EXIT:
                DestroyWindow(hWnd);
                break;

              case IDM_VIEW:
               /*  Selecting the View menu option creates a "View"
                *  window, whose purpose is to display the files
                *  which are currently selected.
                */
                xScreen = GetSystemMetrics(SM_CXSCREEN);
                yScreen = GetSystemMetrics(SM_CYSCREEN);

                hWndView = CreateWindow("Mrr1View",
                "Winc View Window",
                WS_OVERLAPPEDWINDOW /* | WS_VSCROLL */ ,
                0,             /* Initial X pos */
                (int) (yScreen/5)+(NViews*GetSystemMetrics(SM_CYCAPTION)),
                (int) (xScreen),     /* Initial X Width */
                (int) (yScreen * 2 / 3),  /* CW_USEDEFAULT, */
                NULL,
                NULL,
                hInst,
                NULL);

                if (!hWndView)
                    return (NULL);  /* ??? */


                ShowWindow(hWndView, SW_SHOWNORMAL);
                UpdateWindow(hWndView);

                break;


              case IDM_INDEX:
              case IDM_OPEN:
              case IDM_LEAVE:
              case IDM_APPEND:
              case IDM_EDIT:
              case IDM_EXCLUDE:
              case IDM_DELETE:
              case IDM_NEW:
                MessageBox(hWnd, "Command not implemented",
                  "Sorry", MB_OK);
                break;

              case IDM_SELECTALL:
                for(imemo=0; imemo<NMemos; imemo++) {
                  MemoSel[imemo] = TRUE;
                }
                InvalidateRect(hWnd,NULL,FALSE);
                break;

              case IDM_CLEARALL:
                for(imemo=0; imemo<NMemos; imemo++) {
                  MemoSel[imemo] = FALSE;
                }
                InvalidateRect(hWnd,NULL,FALSE);
                break;

              case IDM_VIEWNEW:
                ViewNew = 1;
                CheckView(hWnd);
                break;

              case IDM_VIEWFULL:
                ViewNew = 0;
                CheckView(hWnd);
                break;

              case IDM_SESSION:
               /*  Selecting the Session menu item causes a Terminal
                *  window to be created.
                */
                if(NTerminals <= 0) {

                  xScreen = GetSystemMetrics(SM_CXSCREEN);
                  yScreen = GetSystemMetrics(SM_CYSCREEN);
                  X = (int) (xScreen/12);       /* Initial X pos */
                  Y = (int) (yScreen/7)+(NViews*GetSystemMetrics(SM_CYCAPTION));
                  nWidth = (int) (11*xScreen/12);     /* Initial X Width */
                  nHeight =
                    (int) (LineHeight*24+TopSpace +
                           GetSystemMetrics(SM_CYCAPTION) +
                       /*  GetSystemMetrics(SM_CYMENU) + */
                           2*GetSystemMetrics(SM_CYFRAME));

                  hWndTerm = CreateWindow("Mrr1Term",
                    "IBM Host Session",
                    WS_OVERLAPPEDWINDOW /* | WS_VSCROLL */ ,
                    X, Y, nWidth, nHeight,
                    NULL,
                    NULL,
                    hInst,
                    NULL);

                  if (!hWndTerm)
                      return (NULL);  /* ??? */


                  ShowWindow(hWndTerm, SW_SHOWNORMAL);
                  UpdateWindow(hWndTerm);
                  NTerminals++;
                } else {
                  MessageBox(hWnd,"You already have a terminal window open",
                     "Can't start terminal",MB_OK | MB_ICONASTERISK);
                }

                break;

              case IDM_COMMOPTIONS:
                if(DialogBox(hInstance,"Mrr1Comm",hWnd,lpfnMrr1CommDlg)) {
                  InvalidateRect(hWnd,NULL,TRUE);
                }
                break;
            }
            break;

        case WM_PAINT:
          /*  Display the main conference window, which consists just
           *  of a list of names.  The list of names must be displayed
           *  to properly fit the size and shape of the window.
           *  This display is done in much the same manner as the
           *  MS-DOS Executive window in Windows (the main window that
           *  first comes up when you run Windows).
           *  Whether the code itself is similar, I don't know.
           */
            hDC = BeginPaint (hWnd, &ps);

            OldTextColor = GetTextColor(hDC);
            OldBkColor   = GetBkColor(hDC);
            X = SideSpace;
            Y = TopSpace;

       /*   for(il=0; il<MAXLINES; il++) {
        *     TextOut(hDC,X,Y,&(Lines[il][0]),strlen(&(Lines[il][0])));
        *     Y += LineHeight;
        *   }
        *   PaintCount++;
        *   sprintf(MrrText,"%d Paints",PaintCount);
        *   TextOut(hDC,100,20,MrrText,strlen(MrrText));
        */

            /* Get the client boundaries */

            GetClientRect(hWnd, &Rect);
     /*     NMemosVert = (Rect.bottom - Rect.top - TopSpace) / LineHeight;
            NMemosHorz = (Rect.right - Rect.left - SideSpace) /
                MEMONAMECHARS*CharWidth;
      */
            NMemosVert = 0;
            NMemosHorz = 0;

            X = SideSpace;
            Y = TopSpace;
            il = 0;
            /* Loop through all the file names. */
            for(imemo = 0; imemo < NMemos; imemo++) {
              if(MemoSel[imemo] != OldSel) {
                if(MemoSel[imemo]) {
                  SetTextColor(hDC,OldBkColor);
                  SetBkColor(hDC,OldTextColor);
                  OldSel = TRUE;
                } else {
                  SetTextColor(hDC,OldTextColor);
                  SetBkColor(hDC,OldBkColor);
                  OldSel = FALSE;
                }
              }
              TextOut(hDC,X,Y,NewMemos[imemo],MAXMEMONAME);
              if(il==0) NMemosHorz++;
              Y += LineHeight;
              il++;
              if(il > NMemosVert) NMemosVert = il;
              if(Y > (Rect.bottom - LineHeight)) {
                /* Must start a new column of file names. */
                X += MEMONAMECHARS*CharWidth;
                Y = TopSpace;
                il = 0;
                if(X > Rect.right) break;
              }
            }

            SetTextColor(hDC,OldTextColor);
            SetBkColor(hDC,OldBkColor);
            EndPaint(hWnd, &ps);
            break;

        case WM_ENDSESSION:
            CloseComm(CommDevice);
            break;

        case WM_DESTROY:
            CloseComm(CommDevice);
            PostQuitMessage(0);
            break;

        default:
            return (DefWindowProc(hWnd, message, wParam, lParam));
    }
    return (NULL);
}

/****************************************************************************

    FUNCTION: About(HWND, unsigned, WORD, LONG)

    PURPOSE:  Processes messages for "About" dialog box

    MESSAGES:

        WM_INITDIALOG - initialize dialog box
        WM_COMMAND    - Input received

****************************************************************************/

BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
    switch (message) {
        case WM_INITDIALOG:
            return (TRUE);

        case WM_COMMAND:
            if (wParam == IDOK) {
                EndDialog(hDlg, TRUE);
                return (TRUE);
            }
            break;
    }
    return (FALSE);
}
