/* 

	Filename - intro2GUI2.c
	Date - Februrary 23, 2008

	This program is an example program for the Intro to GUI Programming on neworder.box.sk
*/

#include <windows.h>

#define szAppName TEXT ("intro2GUI")

// Child Controls
#define ID_ED_DATAIN 1
#define ID_STATIC 2
#define ID_PUSH 3

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM)  ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{

	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				= 	LoadIcon (NULL, IDI_APPLICATION) ;
	wndclass.hCursor			= 	LoadCursor (NULL, IDC_ARROW) ;
	wndclass.hbrBackground 	= 	(HBRUSH) GetStockObject (WHITE_BRUSH) ;
	wndclass.lpszMenuName	= 	NULL ;
	wndclass.lpszClassName 	= 	szAppName ;

	if(!RegisterClass(&wndclass)){
		MessageBox(NULL, TEXT("This program requires Windows NT!"),
			szAppName, MB_ICONERROR) ;
		return 0;
	}

	hwnd = CreateWindow (szAppName, szAppName, 
						WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
						NULL, NULL, hInstance, NULL);

	ShowWindow (hwnd, iCmdShow) ;
	UpdateWindow (hwnd) ;
	
	BOOL bGotMessage ;

	while( (bGotMessage = GetMessage( &msg, NULL, 0, 0 )) != 0){ 
	    if (bGotMessage == -1) {
        // handle the error and possibly exit
    	} else {
     	   TranslateMessage(&msg) ; 
       	  DispatchMessage(&msg) ; 
   		}
	}

	return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC				hdc;
	PAINTSTRUCT		ps;
	RECT				rect;


	static HWND hwndEditDataIn, hwndButton ;
	static TCHAR szDataIn[512] ;
	static TCHAR szDataInLabel[] = TEXT("Data in:     __________") ;

	switch (message){
		case WM_CREATE:
			// When we create our window we need to make sure we create the child controls.
			hwndEditDataIn = CreateWindow(TEXT("edit"), NULL,
					WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_CENTER,
					70, 10, 100, 20, hwnd, (HMENU) ID_ED_DATAIN,
					((LPCREATESTRUCT) lParam)->hInstance, NULL ) ;

			// We are going to set a limit of 10 characters for our Edit Box. Always control the input.
			SendMessage( hwndEditDataIn, EM_LIMITTEXT, 10 * sizeof( char), 0L ) ;

			hwndButton = CreateWindow(TEXT("button"), TEXT("Push Me!"),
					WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, 
					70, 50, 100, 20, hwnd, (HMENU) ID_PUSH,
					((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
			return 0 ;

    	case WM_COMMAND :

			if(LOWORD(wParam) == ID_ED_DATAIN) {

				if (HIWORD ( wParam) == EN_MAXTEXT ) {
						//Make sure the control did not exceed our limit of ten characters.
						MessageBox (hwnd, TEXT ("We are only accepting ten characters!"),
								szAppName, MB_OK | MB_ICONSTOP) ;
						return 0 ;
					}
				}

			if(LOWORD(wParam) == ID_PUSH) {

				if ( GetWindowText(hwndEditDataIn, szDataIn, 511)  )
					MessageBox(hwnd, szDataIn, szAppName, MB_OK) ;

				InvalidateRect(hwnd, NULL, TRUE) ;

			}

			return 0 ;

	case WM_SETFOCUS :
			SetFocus(hwndEditDataIn) ;

			return 0 ;

		case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);

			GetClientRect(hwnd, &rect);

			DrawText (hdc, TEXT("Hello neworder!"), -1, &rect,
				DT_SINGLELINE | DT_CENTER | DT_VCENTER);

			 TextOut (hdc, 10, 15, szDataInLabel, lstrlen (szDataInLabel)) ;       

			EndPaint (hwnd, &ps);
			return 0;

		case WM_DESTROY:
			PostQuitMessage (0);
			return 0;

		default:
			return DefWindowProc (hwnd, message, wParam, lParam);
		}
}
