/* combo.c   Creating combo box controls */

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

int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                     LPSTR lpszCmdLine, int nCmdShow)
{
    HWND        hWnd ; 
    MSG         msg ; 
    WNDCLASS    wndclass ;

        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", "Combo Box 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    hCombo ;
    HINSTANCE       hInstance ;
    int             nSel ;
    char            cBuf [256], cSelBuf [128] ; /* used to get/display */
                                                /* Combo Box items */

    switch (wMessage)       /* process windows messages */
    {
        case WM_CREATE:     /* program is just starting */
                            /* get the instance handle */
                hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
                            /* Create combo box control */
                            /* Two styles are shown here */
                            /* First a simple combo box */
                hCombo = CreateWindow ("COMBOBOX", "",
                    WS_CHILD | CBS_SIMPLE | CBS_SORT | WS_VSCROLL,
                    10, 10, 180, 80, hWnd, (HMENU)COMBO_ID, hInstance, NULL) ; 
                            /* Second a dropdown combo box */                             
             /* hCombo = CreateWindow ("COMBOBOX", "",
                    WS_CHILD | CBS_DROPDOWN | CBS_SORT | WS_VSCROLL,
                    10, 10, 180, 80, hWnd, (HMENU)COMBO_ID, hInstance, NULL) ; */
                
                ShowWindow (hCombo, SW_SHOWNORMAL) ;
                break ;

        case WM_COMMAND:
            switch (LOWORD(wParam))         /* menu item or control ID's */
            {
                case COMBO_ID:  /* user did something to combo */
                    if (HIWORD (wParam) == CBN_SELCHANGE)
                    {               /* if user selected an item */
                                    /* get number of selected item */
                        nSel = (int) SendMessage (hCombo, CB_GETCURSEL, 0, 0) ;
                                    /* load sel. string into cSelBuf */
                        SendMessage (hCombo, CB_GETLBTEXT, nSel,
                            (LPARAM) cSelBuf) ;
                                    /* build a readable string */
                        wsprintf (cBuf, "The selected item = %s.", (LPSTR) cSelBuf) ;
                                    /* show string in a message box */
                        MessageBox (hWnd, cBuf, "Selection", MB_OK) ;
                    }
                    break ; 

                case IDM_FILL:      /* fill combo with strings */
                    SendMessage (hCombo, CB_RESETCONTENT, 0, 0) ;
                    SendMessage (hCombo, CB_ADDSTRING, 0,
                        (LPARAM) "First string added.") ;
                    SendMessage (hCombo, CB_ADDSTRING, 0,
                        (LPARAM) "Second string added.") ;
                    SendMessage (hCombo, CB_ADDSTRING, 0,
                        (LPARAM) "Third string added.") ;
                    SendMessage (hCombo, CB_ADDSTRING, 0,
                        (LPARAM) "Another string added.") ;
                    SendMessage (hCombo, CB_ADDSTRING, 0,
                        (LPARAM) "Yet another string added.") ;
                    SendMessage (hCombo, CB_ADDSTRING, 0,
                        (LPARAM) "Last string added.") ;
                                    /* start with first item selected */
                    SendMessage (hCombo, CB_SETCURSEL, 0, 0) ;
                    break ;
                case IDM_QUIT:
                    DestroyWindow (hWnd) ; 
                    break ;  
            }
            break ;

        case WM_DESTROY: 
            PostQuitMessage (0) ;
            break ;

        default:   
            return DefWindowProc (hWnd, wMessage, wParam, lParam) ;
    }
    return (0) ;
}