/* dlg2.cpp, uses global variables to get data from */
/* a dialog box with edit and list box controls     */

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage, 
                                        WPARAM wParam, LPARAM lParam);

BOOL CALLBACK InfoDlgProc (HWND hDlg, UINT wMessage, 
                                                WPARAM wParam, LPARAM lParam);

int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
    int nCmdShow)
{
    HWND        hWnd ;      /* the window's "handle" */
    MSG         msg ;       /* a message structure */
    WNDCLASS    wndclass ;  /* window class structure */

        wndclass.style          = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc    = WndProc ;
        wndclass.cbClsExtra     = 0 ;
        wndclass.cbWndExtra     = 0 ;
        wndclass.hInstance      = hInstance ;
        wndclass.hIcon          = NULL ;
        wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName   = "MYMENU" ;
        wndclass.lpszClassName  = "MyClass" ;
                        /* register the parent window class */
        if (!RegisterClass (&wndclass))
            return (0) ;

    hWnd = CreateWindow ("MyClass","Dialog Box Example 2", 
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
        CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;
    ShowWindow (hWnd, nCmdShow) ;   /* display the window */    
    UpdateWindow (hWnd) ;

    while (GetMessage (&msg, NULL, 0, 0))   /* message loop */
    {
            TranslateMessage (&msg) ;   /* translate keyboard messages */
            DispatchMessage (&msg) ;    /* send message to WndProc() */
    }
    return (msg.wParam) ;
}

/* Global variables */
    char name[]="                    ";     /* name entered by user */
    int nCurSel;    /* salary range selected by user from list box */

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage, 
                                        WPARAM wParam, LPARAM lParam)
{
    HINSTANCE   hInstance ;
    static int  nRetVal = 0 ;
    char        cBuf[30];       
    /* A blank string to erase strings displayed earlier */
    char        blanks[]="                                                  ";
    HDC         hDC;
    
    switch (wMessage)           /* process windows messages */
    {
        case WM_COMMAND:            /* menu items */
            switch (LOWORD(wParam))
            {
                case IDM_GET:     /* run the information dialog procedure */
                    hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
                    nRetVal = DialogBox (hInstance,"INFODLG",hWnd,(DLGPROC)InfoDlgProc) ;
                        hDC=GetDC(hWnd);
                        if (nRetVal==1)
                        {       
                                lstrcat(cBuf,0);        /* Null terminate the buffer */  
                                switch (nCurSel)        /* position in list box */
                                {
                                case 0:
                                        lstrcpy(cBuf,"0-10K: Poverty Level       ");
                                        break;
                                case 1:
                                        lstrcpy(cBuf,"10K-50K: Doing OK          ");
                                        break;
                                case 2:
                                        lstrcpy(cBuf,"50K-100K: Quite Comfortable");
                                        break;
                                case 3:
                                        lstrcpy(cBuf,">100K: You got it made     ");
                                        break;
                                }

                                TextOut(hDC,10,10,"Name: ",6); 
                                TextOut(hDC,60,10,blanks,lstrlen(blanks));
                                TextOut(hDC,60,10,name,lstrlen(name));
                                TextOut(hDC,10,50,blanks,lstrlen(blanks));
                                TextOut(hDC,10,50,cBuf,lstrlen(cBuf));
                        }
                        else
                        {
                                TextOut(hDC,10,10,blanks,lstrlen(blanks));
                                TextOut(hDC,10,50,blanks,lstrlen(blanks));
                                TextOut(hDC,10,10,"No response!",12);
                        }
                        ReleaseDC(hWnd,hDC);
                    break ;
                case IDM_QUIT:
                    DestroyWindow (hWnd) ;  /* destory window, */
                    break ;         /* terminating application */
            }
            break ;

        case WM_DESTROY:            /* stop application */
            PostQuitMessage (0) ;
            break ;
        default:            /* default windows message processing */
            return DefWindowProc (hWnd, wMessage, wParam, lParam) ;
    }
    return (0) ;
}

/* The information-retrieving dialog box procedure */
BOOL CALLBACK InfoDlgProc (HWND hDlg, UINT wMessage, 
                                                        WPARAM wParam, LPARAM lParam)
{
        char salary[4][20]={"0-10K  ","10K-50K ","50K-100K","> 100K  "};
        char    cBuf[]="                   ";
        int     i;
    
        switch (wMessage)       /* process messages */
    {
        case WM_INITDIALOG:
            SendDlgItemMessage(hDlg, IDC_LISTSALARY, LB_RESETCONTENT, 0, 0);
            for (i=0; i<4; i++) /* Fill the list box with salary ranges */
                        SendDlgItemMessage(hDlg, IDC_LISTSALARY, LB_ADDSTRING,0,(LPARAM)salary[i]); 
            return (TRUE);
        case WM_COMMAND:    /* controls in dialog box activated */
            switch (LOWORD(wParam))
            {
                case IDOK:                                  /* OK button */
                    SendDlgItemMessage (hDlg,IDC_EDITNAME,EM_GETLINE,0,(LPARAM)cBuf);
                    lstrcpy(name,cBuf);
                    nCurSel = (int) SendDlgItemMessage(hDlg,IDC_LISTSALARY,LB_GETCURSEL,0,0);
                    EndDialog (hDlg, 1) ;       /* exit the dialog box */
                    return (TRUE) ;
                case IDCANCEL:                          /* Cancel button */
                    EndDialog (hDlg, 0) ;   /* exit the dialog box */
                    return (TRUE) ;
            }
            break ;
    }
    return (FALSE) ;    /* return FALSE to signify message not processed */
}