/* button.cpp   Creating button controls */

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

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" ;
        if (!RegisterClass (&wndclass))
            return 0 ;

    hWnd = CreateWindow ("MyClass", "Button Control Example", 
        WS_OVERLAPPEDWINDOW, 
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
        NULL, NULL, hInstance, NULL) ;
    ShowWindow (hWnd, nCmdShow) ;    
    UpdateWindow(hWnd) ;

    while (GetMessage (&msg, NULL, 0, 0)) 
    {
        TranslateMessage (&msg) ; 
        DispatchMessage (&msg) ;  
    }
    return (msg.wParam) ;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage, 
                           WPARAM wParam, LPARAM lParam)
{
    static  HWND    hGroup, hRad1, hRad2, hCheck, hBut1, hBut2 ;
    HINSTANCE       hInstance ;
    BOOL            bIsChecked, bTopRadioOn ; /* button status variables */
    char            cBuf [128] ;
    
    switch (wMessage)       /* process windows messages */
    {
        case WM_CREATE:     /* program is just starting */
                            /* get the instance handle */
            hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ; 

                            /* create button controls */

            hGroup = CreateWindow ("BUTTON", "Group Box", 
                    WS_CHILD | BS_GROUPBOX, 
                    10, 10, 120, 100,
                    hWnd, NULL, hInstance, NULL) ;
   
            hRad1 = CreateWindow ("BUTTON", "Radio 1",
                    WS_CHILD | BS_RADIOBUTTON, 
                    20, 40, 80, 20,
                    hWnd, (HMENU)RADIO1, hInstance, NULL) ;

            hRad2 = CreateWindow ("BUTTON", "Radio 2",
                    WS_CHILD | BS_RADIOBUTTON,
                    20, 70, 80, 20,
                    hWnd, (HMENU)RADIO2, hInstance, NULL) ;

            hCheck = CreateWindow ("BUTTON", "Check Box",
                    WS_CHILD | BS_AUTO3STATE,
                    20, 120, 110, 20,
                    hWnd, (HMENU)CHECK_BOX, hInstance, NULL) ;

            hBut1 = CreateWindow ("BUTTON", "Show Status",
                    WS_CHILD | BS_PUSHBUTTON,
                    140, 20, 120, 40,
                    hWnd, (HMENU)STATUS_BUTTON, hInstance, NULL) ;

            hBut2 = CreateWindow ("BUTTON", "Done",
                    WS_CHILD | BS_DEFPUSHBUTTON,
                    140, 110, 100, 40,
                    hWnd, (HMENU)DONE_BUTTON, hInstance, NULL) ;

                        /* display the buttons */
            ShowWindow (hGroup, SW_SHOWNORMAL) ;
            ShowWindow (hRad1, SW_SHOWNORMAL) ;
            ShowWindow (hRad2, SW_SHOWNORMAL) ;
            ShowWindow (hCheck, SW_SHOWNORMAL) ;
            ShowWindow (hBut1, SW_SHOWNORMAL) ;
            ShowWindow (hBut2, SW_SHOWNORMAL) ;
                 /* start with radio button 1 checked */
            SendMessage (hRad1, BM_SETCHECK, TRUE, 0) ;
            break ;

        case WM_COMMAND:
            switch (LOWORD(wParam)) /* check Button control ID */
            {

                case RADIO1:        /* top radio button clicked */
                    SendMessage (hRad1, BM_SETCHECK, TRUE, 0) ;
                    SendMessage (hRad2, BM_SETCHECK, FALSE, 0) ;
                    break ;

                case RADIO2:        /* bottom radio button clicked */
                    SendMessage (hRad1, BM_SETCHECK, FALSE, 0) ;
                    SendMessage (hRad2, BM_SETCHECK, TRUE, 0) ;
                    break ;

                case CHECK_BOX:     /* auto check box */
                    break ;         /* no need to process message */

                case STATUS_BUTTON:
                    bIsChecked = SendMessage (hCheck, BM_GETCHECK, 0, 0) ;
                    bTopRadioOn = SendMessage (hRad1, BM_GETCHECK, 0, 0) ;
                    wsprintf (cBuf, 
                        "Top Radio Button = %d, Check box = %d",
                        bTopRadioOn, bIsChecked) ;
                    MessageBox (hWnd, cBuf, "Current Button Status", MB_OK) ;
                    break ;

                case DONE_BUTTON:
                    DestroyWindow (hWnd) ;  /* terminate application */
                    break ; 

                    /* The action taken for the IDM_TEST menu item is the */
                    /* same as that for the STATUS_BUTTON control, so just */
                    /* simulate the same message as if the control had */
                    /* been activated (by sending a WM_COMMAND message). */
                case IDM_TEST:
                    SendMessage (hWnd, WM_COMMAND, STATUS_BUTTON, 0) ;
                    break ;

                case IDM_QUIT:
                    DestroyWindow (hWnd) ;  /* destroy 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) ;
}