#include <windows.h>

#define IDM_NEW    0
#define IDM_OPEN   1
#define IDM_SAVE   2
#define IDM_SAVEAS 3
#define IDM_PRINT  4
#define IDM_EXIT   5

#define IDM_UNDO   10
#define IDM_CUT    11
#define IDM_COPY   12
#define IDM_PASTE  13
#define IDM_DEL    14

#define IDM_WHITE  20
#define IDM_LTGRAY 21
#define IDM_GRAY   22
#define IDM_BLACK  23

#define IDM_START  30
#define IDM_STOP   31

#define IDM_HELP   40
#define IDM_ABOUT  41

static char szAppName[] = "WinMenu" ;
HWND        hwnd ;
HMENU		hMenu,hMenuPopup;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
//int AskConfirmation (HWND) ;

int main ()
{
     //HWND        hwnd ;
     MSG         msg ;
     WNDCLASSEX  wndclass ;

     wndclass.cbSize        = sizeof (wndclass) ;
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = NULL;//hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

     RegisterClassEx (&wndclass) ;

	 hMenu = CreateMenu ();

	 hMenuPopup = CreateMenu ();
	 AppendMenu (hMenuPopup, MF_STRING, IDM_NEW,    "&New");
	 AppendMenu (hMenuPopup, MF_STRING, IDM_OPEN,   "&Open...");
	 AppendMenu (hMenuPopup, MF_STRING, IDM_SAVE,   "&Save");
	 AppendMenu (hMenuPopup, MF_STRING, IDM_SAVEAS, "Save &As...");
	 AppendMenu (hMenuPopup, MF_SEPARATOR, 0,       NULL);
	 AppendMenu (hMenuPopup, MF_STRING, IDM_PRINT,  "&Print...");
	 AppendMenu (hMenuPopup, MF_SEPARATOR, 0,       NULL);
	 AppendMenu (hMenuPopup, MF_STRING, IDM_EXIT,   "E&xit");

	 AppendMenu (hMenu, MF_POPUP, (UINT) hMenuPopup, "&File");

	 hMenuPopup = CreateMenu ();
	 AppendMenu (hMenuPopup, MF_STRING, IDM_UNDO,  "&Undo");
	 AppendMenu (hMenuPopup, MF_SEPARATOR, 0,      NULL);
	 AppendMenu (hMenuPopup, MF_STRING, IDM_CUT,   "Cu&t");
	 AppendMenu (hMenuPopup, MF_STRING, IDM_COPY,  "&Copy");
	 AppendMenu (hMenuPopup, MF_STRING, IDM_PASTE, "&Paste");
	 AppendMenu (hMenuPopup, MF_STRING, IDM_DEL,   "De&lete");

	 AppendMenu (hMenu, MF_POPUP, (UINT) hMenuPopup, "&Edit");

	 hMenuPopup = CreateMenu ();
	 AppendMenu (hMenuPopup, MF_STRING | MF_CHECKED, IDM_WHITE,  "&White");
	 AppendMenu (hMenuPopup, MF_STRING,              IDM_LTGRAY, "&Lt Gray");
	 AppendMenu (hMenuPopup, MF_STRING,              IDM_GRAY,   "&Gray");
	 AppendMenu (hMenuPopup, MF_STRING,              IDM_BLACK,  "&Black");

	 AppendMenu (hMenu, MF_POPUP, (UINT) hMenuPopup, "&Background");

	 hMenuPopup = CreateMenu ();
	 AppendMenu (hMenuPopup, MF_STRING,             IDM_START,  "&Start");
	 AppendMenu (hMenuPopup, MF_STRING | MF_GRAYED, IDM_STOP,   "S&top");

	 AppendMenu (hMenu, MF_POPUP, (UINT) hMenuPopup, "&Timer");

	 hMenuPopup = CreateMenu ();
	 AppendMenu (hMenuPopup, MF_STRING, IDM_HELP,  "&Help...");
	 AppendMenu (hMenuPopup, MF_STRING, IDM_ABOUT, "&About WinMenu...");

	 AppendMenu (hMenu, MF_POPUP, (UINT) hMenuPopup, "&Help");
		

     hwnd = CreateWindowEx (WS_EX_LEFT,
		            szAppName,               // window class name
		            "The WinMenu Program",     // window caption
                    WS_OVERLAPPEDWINDOW,     // window style
                    CW_USEDEFAULT,           // initial x position
                    CW_USEDEFAULT,           // initial y position
                    CW_USEDEFAULT,           // initial x size
                    CW_USEDEFAULT,           // initial y size
                    NULL,                    // parent window handle
                    hMenu,                   // window menu handle
                    //hInstance,             // program instance handle
					NULL,
		            NULL) ;		             // creation parameters

     ShowWindow (hwnd, SW_SHOW) ;
     UpdateWindow (hwnd) ;

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

     return msg.wParam ;

}

int AskConfirmation (HWND hwnd)
{

     return MessageBox (hwnd, "Really want to close PopPad2?",
                        szAppName, MB_YESNO | MB_ICONQUESTION) ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
     static int  iColorID[4] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,
                                 BLACK_BRUSH } ;
     static int  iSelection = IDM_WHITE ;

     switch (iMsg)
     {

		  case WM_COMMAND :

               switch (LOWORD (wParam))
               {

				case IDM_NEW :
				case IDM_OPEN :
				case IDM_SAVE :
				case IDM_SAVEAS :
				case IDM_PRINT :
					MessageBeep (0) ;
					return 0;

				case IDM_EXIT :
					SendMessage (hwnd, WM_CLOSE, 0, 0);
					return 0;

				case IDM_UNDO :
				case IDM_CUT :
				case IDM_COPY :
				case IDM_PASTE :
				case IDM_DEL :
					MessageBeep (0) ;
					return 0;

                case IDM_WHITE :          // Note: Logic below assumes
                case IDM_LTGRAY :         // that IDM_WHITE through IDM_BLACK
                case IDM_GRAY :           // are consecutive numbers in
                case IDM_BLACK :          // the order shown here.

                     CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
                     iSelection = LOWORD (wParam) ;
                     CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
     
                     SetClassLong (hwnd, GCL_HBRBACKGROUND,
                          (LONG) GetStockObject 
							 (iColorID[LOWORD (wParam) - IDM_WHITE])) ;

                     InvalidateRect (hwnd, NULL, TRUE) ;
                     return 0 ;

                case IDM_START :
                     if (SetTimer (hwnd, 1, 1000, NULL))
                     {
                          EnableMenuItem (hMenu, IDM_START, MF_GRAYED) ;
                          EnableMenuItem (hMenu, IDM_STOP,  MF_ENABLED) ;
                     }
                     return 0 ;

                case IDM_STOP :
                     KillTimer (hwnd, 1) ;
                     EnableMenuItem (hMenu, IDM_START, MF_ENABLED) ;
                     EnableMenuItem (hMenu, IDM_STOP,  MF_GRAYED) ;
                     return 0 ;

                case IDM_HELP :
                     MessageBox (hwnd, "Help not yet implemented!",
                                 szAppName, MB_ICONEXCLAMATION | MB_OK) ;
                     return 0 ;

                case IDM_ABOUT :
                     MessageBox (hwnd, "Menu Demonstration Program.",
                                 szAppName, MB_ICONINFORMATION | MB_OK) ;
                     return 0 ;
                }
                break ;

          case WM_TIMER :
               MessageBeep (0) ;
               return 0 ;

          case WM_CLOSE :
               if (IDYES == AskConfirmation (hwnd))
                    DestroyWindow (hwnd) ;
               return 0 ;

          case WM_DESTROY :
               PostQuitMessage (0) ;
               return 0 ;
     }

     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}