;***************************************
;*********In The End Map Editor*********
;******copyright (c) by Barf 2005*******
;****************Contact:***************
;***************barf@o2.pl**************
;***************************************

format PE GUI 4.0
entry start

include 'C:\Program Files\Flat Asembler\INCLUDE\win32a.inc'

section '.data' data readable writeable

  _title db 'In The End level editor',0 ;Primary variables like title of window,
  _class db 'ITELE',0		        ;Class Name of window
  _btn	 db 'BUTTON',0		        ;And Class Names of Controls
  _stc	 db 'STATIC',0

  btn1	  db 'File',0		        ;Variables containing titles of buttons
  btn11   db 'New',0
  hbtn11  dd ?			        ;And variables in which their handles will be saved.
  btn12   db 'Open',0
  hbtn12  dd ?
  btn13   db 'Save',0
  hbtn13  dd ?
  btn14   db 'Save As...',0
  hbtn14  dd ?
  btn2	  db 'Exit',0

  hWnd	    dd ?		        ;Variables in which handles of main window,
  hInstance dd ?		        ;resouces will be saved
  hWA	    dd ?

  hDC	    dd ?		        ;Variables which will contain handles used by GDI
  hWABrush  dd ?


  Work	    dd 0		        ;flags

  msg  MSG			        ;Special structures
  wc   WNDCLASS
  ps   PAINTSTRUCT

  sWidth    dd ?		        ;Variables in which we`ll save measurements of screen
  sHeight   dd ?

section '.code' code readable executable

  start:
	invoke	GetSystemMetrics, SM_CXSCREEN	     ;In the beginning we take width and height of window and put to variables
	add	EAX, 1				     ;System Metric SM_CXSCREEN i X position, so we have to add 1 to it to get width
	mov     [sWidth], EAX
	invoke	GetSystemMetrics, SM_CYSCREEN
	mov     [sHeight], EAX

	invoke	GetModuleHandle,0
	mov     [hInstance],EAX
	invoke	LoadIcon,0,IDI_APPLICATION
	mov     [wc.hIcon],EAX
	invoke	LoadCursor,0,IDC_ARROW
	mov     [wc.hCursor],EAX
	mov     [wc.style],0
	mov     [wc.lpfnWndProc],WindowProc
	mov     [wc.cbClsExtra],0
	mov     [wc.cbWndExtra],0
	mov	EAX,[hInstance]
	mov     [wc.hInstance],EAX
	mov     [wc.hbrBackground],COLOR_BTNFACE+1
	mov     [wc.lpszMenuName],0
	mov     [wc.lpszClassName],_class
	invoke	RegisterClass,wc		     ;Standard process od registering window class

	mov	EBX, [sWidth]			     ;Our Window will be as wide as screen
	mov	ECX, [sHeight]			     ;We`ll use height of screen to establish height of our window
	sub	ECX, 25 			     ;We substract 25 from height for Start Menu Bar
	invoke	CreateWindowEx,0,_class,_title,WS_VISIBLE+WS_CAPTION+WS_SYSMENU+WS_MINIMIZEBOX,0,0,EBX,ECX,NULL,NULL,[hInstance],NULL
	mov     [hWnd],EAX

  msg_loop:
	invoke	GetMessage,msg,NULL,0,0 	     ;Classic message loop declaration
	or	EAX,EAX
	jz	end_loop
	invoke	TranslateMessage,msg
	invoke	DispatchMessage,msg
	jmp	msg_loop
	  end_loop:
	invoke	ExitProcess,[msg.wParam]

proc WindowProc, hwnd,wmsg,wparam,lparam
	push	ebx esi edi			     ;Standart beginning of Window Controling Procedure
	cmp     [wmsg],WM_DESTROY
	je	onDestroy
  defwndproc:
	invoke	DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
	cmp     [wmsg],WM_CREATE
	je	onCreate
	cmp     [wmsg],WM_COMMAND
	je	onCommand
	cmp     [wmsg], WM_PAINT
	je	onPaint
	jmp	finish
  onDestroy:
	invoke	ReleaseDC
	invoke	PostQuitMessage,0
	xor	EAX,EAX
  onCreate:
	invoke	CreateSolidBrush, 000000h	     ;In the beginning of program we`re creating black brush for a background of Work Area
	mov     [hWABrush], EAX 		     ;It`s saved in hWABrush

	invoke	CreateWindowEx, 0,_btn, btn11, WS_VISIBLE+WS_CHILD, 0,  0,  100,  25, [hwnd], 110, [hInstance], NULL
	mov     [hbtn11], EAX
	invoke	CreateWindowEx, 0,_btn, btn12, WS_VISIBLE+WS_CHILD, 100,0,  100,  25, [hwnd], 120, [hInstance], NULL
	mov     [hbtn12], EAX
	invoke	CreateWindowEx, 0,_btn, btn13, WS_VISIBLE+WS_CHILD, 200,0,  100,  25, [hwnd], 130, [hInstance], NULL
	mov     [hbtn13], EAX
	invoke	CreateWindowEx, 0,_btn, btn14, WS_VISIBLE+WS_CHILD, 300,0,  100,  25, [hwnd], 140, [hInstance], NULL
	mov     [hbtn14], EAX			     ;If user push btn1 it will create 4 new buttons( and save their handles)
	invoke	CreateWindowEx, 0,_btn, btn2, WS_VISIBLE+WS_CHILD, 400,0,  100,  25, [hwnd], 200, [hInstance], NULL

	mov	EBX, [sWidth]			     ;We`ll make work area on STATIC. Well need measurments of screen to establish
	mov	ECX, [sHeight]			     ;a size of work area
	sub	EBX, 10 			     ;Scrollbars makes static a bit wider. We have to make it 10px narrower
	sub	ECX, 100			     ;Static has to be 100px lower. 15px for titlebar, 50px for buttons, 10px for scrollbar
						     ;and 25px for Start Menu Bar
	invoke	CreateWindowEx, 0,_stc, 0, WS_VISIBLE+WS_CHILD+WS_HSCROLL+WS_VSCROLL, 0, 50, EBX, ECX, [hwnd], 0  , [hInstance], NULL
	mov     [hWA], EAX
	invoke	BeginPaint, [hWA], ps		     ;Now we have to create hDC for our static( without we couldn`t draw)
	mov	EAX, [ps.hdc]
	mov     [hDC], EAX			     ;For our comfort we`ll copy hDC from PAINTSTRUCT to separate variable
	jmp	finish
  onCommand:					     ;Messages from buttons.
	cmp     [wparam], 110			     ;If New Button is pushed
	jne	onExitButton			     ;Program doesn`t jump to onExitButton
       ;When btn11 has been pushed, program select pre-made brush for static
	mov     [Work], 1			     ;And Sets Work on 1, what cause start of painting on static.
  onExitButton:
	cmp     [wparam], 200			     ;If btn2 is pushed
	jne	finish
	invoke	PostQuitMessage,0		     ;Program will closed
	jmp	finish
  onPaint:
	cmp     [Work], 1			     ;If edition hadn`t been started
	jne	finish				     ;Program jumps to end
	invoke	Rectangle, [hDC], 0,0,10000,10000

  finish:
	pop	edi esi ebx
	return
endp

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
	  user,'USER32.DLL',\
	  gdi, 'GDI32.DLL'

  import kernel,\
	 GetModuleHandle,'GetModuleHandleA',\
	 ExitProcess,'ExitProcess'

  import user,\
	 RegisterClass,'RegisterClassA',\
	 CreateWindowEx,'CreateWindowExA',\
	 DefWindowProc,'DefWindowProcA',\
	 GetMessage,'GetMessageA',\
	 TranslateMessage,'TranslateMessage',\
	 DispatchMessage,'DispatchMessageA',\
	 LoadCursor,'LoadCursorA',\
	 LoadIcon,'LoadIconA',\
	 DestroyWindow, 'DestroyWindow',\
	 PostQuitMessage,'PostQuitMessage',\
	 GetSystemMetrics, 'GetSystemMetrics',\
	 BeginPaint, 'BeginPaint',\
	 ReleaseDC, 'ReleaseDC',\
	 EndPaint, 'EndPaint'

  import gdi,\
	 CreateSolidBrush, 'CreateSolidBrush',\
	 SelectObject, 'SelectObject',\
	 Rectangle,'Rectangle'
