;include 'FUNDAMENTALS\MACROS\symbols.inc'
include 'FUNDAMENTALS\EQUATES\FUNDAMENTALS.INC'
include 'ABI\WIN\win32a.inc'

format PE GUI ;rename binary as 'MINIPAD.EXE'
entry start   ;launch CMD line '%OUTPUT%'

IDR_ICON = 17
IDR_GICON = 1
IDR_MENU = 37

IDM_NEW   = 101
IDM_EXIT  = 102
IDM_ABOUT = 901


section '.text' code readable executable

  start:

	invoke	GetModuleHandle,0
	mov	[wc.hInstance],eax

	invoke	LoadIcon,eax,IDR_ICON
	mov	[wc.hIcon],eax
	invoke	LoadCursor,0,IDC_ARROW
	mov	[wc.hCursor],eax
	invoke	RegisterClass,wc
	test	eax,eax
	jz	error

	invoke	LoadMenu,[wc.hInstance],IDR_MENU
	invoke	CreateWindowEx,0,_class,_title,WS_VISIBLE+WS_OVERLAPPEDWINDOW,144,128,656,256,NULL,eax,[wc.hInstance],NULL
	test	eax,eax
	jz	error

  msg_loop:
	invoke	GetMessage,msg,NULL,0,0
	cmp	eax,1
	jb	end_loop
	jne	msg_loop
	invoke	TranslateMessage,msg
	invoke	DispatchMessage,msg
	jmp	msg_loop

  error:
	invoke	MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK

  end_loop:
	invoke	ExitProcess,0

proc WindowProc uses ebx esi edi, hwnd,wmsg,wparam,lparam
	mov	eax,[wmsg]
	cmp	eax,WM_CREATE
	je	.wmcreate
	cmp	eax,WM_SIZE
	je	.wmsize
	cmp	eax,WM_SETFOCUS
	je	.wmsetfocus
	cmp	eax,WM_COMMAND
	je	.wmcommand
	cmp	eax,WM_DESTROY
	je	.wmdestroy
  .defwndproc:
	invoke	DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
	jmp	.finish
  .wmcreate:
	invoke	GetClientRect,[hwnd],client
	invoke	CreateWindowEx,WS_EX_CLIENTEDGE,_edit,0,WS_VISIBLE+WS_CHILD+WS_HSCROLL+WS_VSCROLL+ES_AUTOHSCROLL+ES_AUTOVSCROLL+ES_MULTILINE,[client.left],[client.top],[client.right],[client.bottom],[hwnd],0,[wc.hInstance],NULL
	or	eax,eax
	jz	.failed
	mov	[edithwnd],eax
	invoke	CreateFont,16,0,0,0,0,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_RASTER_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FIXED_PITCH+FF_DONTCARE,NULL
	or	eax,eax
	jz	.failed
	mov	[editfont],eax
	invoke	SendMessage,[edithwnd],WM_SETFONT,eax,FALSE
	xor	eax,eax
	jmp	.finish
      .failed:
	or	eax,-1
	jmp	.finish
  .wmsize:
	invoke	GetClientRect,[hwnd],client
	invoke	MoveWindow,[edithwnd],[client.left],[client.top],[client.right],[client.bottom],TRUE
	xor	eax,eax
	jmp	.finish
  .wmsetfocus:
	invoke	SetFocus,[edithwnd]
	xor	eax,eax
	jmp	.finish
  .wmcommand:
	mov	eax,[wparam]
	and	eax,0FFFFh
	cmp	eax,IDM_NEW
	je	.new
	cmp	eax,IDM_ABOUT
	je	.about
	cmp	eax,IDM_EXIT
	je	.wmdestroy
	jmp	.defwndproc
      .new:
	invoke	SendMessage,[edithwnd],WM_SETTEXT,0,0
	jmp	.finish
      .about:
	invoke	MessageBox,[hwnd],_about_text,_about_title,MB_OK
	jmp	.finish
  .wmdestroy:
	invoke	DeleteObject,[editfont]
	invoke	PostQuitMessage,0
	xor	eax,eax
  .finish:
	ret
endp

section '.data' data readable writeable

  _title TCHAR 'Menu_v0 example',0
  _about_title TCHAR 'About MiniPad',0
  _about_text TCHAR 'This is Win32 example program created with flat assembler.',0
  _error TCHAR 'Startup failed.',0

  _class TCHAR 'MINIPAD32',0
  _edit TCHAR 'EDIT',0

  wc WNDCLASS style:0, lpfnWndProc:WindowProc, hbrBackground:COLOR_BTNFACE+1, lpszClassName:_class

  edithwnd dd ?
  editfont dd ?

  msg MSG
  client RECT

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
	  user32,'USER32.DLL',\
	  gdi32,'GDI32.DLL'

  include 'ABI\WIN\API\kernel32.inc'
  include 'ABI\WIN\API\user32.inc'
  include 'ABI\WIN\API\gdi32.inc'

section '.reloc' fixups data readable discardable	; needed for Win32s

macro menu ex,helpID,extra&
  local items
  match ,ex
    dw 0
  else
    dw 1
  end match
  dw items -$-2
  match any,ex helpID
    dd helpID+0
  end match
  match , extra
  else
    iterate elem,extra
      if elem eqtype ''
	db elem,0
	align 4
      else
	dd elem
      end if
    end iterate
  end match
  items:
end macro

macro menuitem2 string,id,option
  dw option
  if ~(option and MF_POPUP)
    dw id+0
  end if
  du string,0
end macro

macro menuitem string,id,resinfo,status,type
   dd MFT_STRING or type+0,status+0,id
   dw resinfo+0
   du string,0
   align 4
   match any,resinfo
    if resinfo and MFR_POPUP
     dd 0
    end if
   end match
end macro

macro menuseparator resinfo
   dd MFT_SEPARATOR,0,0
   dw resinfo+0,0
end macro

macro menubar option,ID,string
  dw (option+0) and $FFF7 ; 8 has no effect; and $60 = next row
  if option and MF_POPUP
  else
    dw ID+0
  end if
  du string,0
end macro

macro menuitemseparator option,ID,string
  if option and $F0
    dw option
  else
    dw 0 ; other flags ignored if no one of $80,$40,$20,$10 is set
  end if
  dw 0 ; with most of (maybe wit all of) options this is ignored
  dw 0
end macro

section '.rsrc' resource data readable

  resource_directory

    resource_data RT_MENU,IDR_MENU,LANG_ENGLISH+SUBLANG_DEFAULT
      ;menu ex,,0,0,0
      menu
      ;dw 1,4
      ;dd 0; helpID
       du $0010,"First",0
	 du $0200,IDM_NEW,"1st subitem, Let It be ""New""",0
	 du $0800,$FF25,"sEpArAtOr , anycase you don`t see this text",0 ; direct separator
	 du $0010,0 ; indirect separator with descendants
	   du $0000,$1,"This item never be shown - it is separator child",0
	   du $0080,$2,"This never shown too and this last child of separator - flag $80",0
	 du $F60C,$3,"This is 4th subitem",0
	 du $0010,"5th subitem with descendants",0
	   du $0000,$FF26,0 ; this is separator
	   du $0080,$4,"last sub of 5th subitem",0
	 du $00A0,IDM_EXIT,"First ender, Let it be ""Exit""",0
       du $0070,"That is displayed in 2nd row at left",0
	 du $0080,$6,"only 1 descendant",0
       du $0074,"3rd row at right, let it be ""Help""",0
	 du $0080,IDM_ABOUT,"1st descendant ""&About...""",0
       du $0000,-1,"dummy item",0
       du $0080,-1,"dummy item",0
; rest will never be shown
       menubar 0018h+MFR_END+$8000,,"HELLO"
       du MF_BITMAP+MFR_POPUP,0,'&File',0
       ;menuitem2 '&File',0,MF_BITMAP+MFR_POPUP
	 ;dw 0FFFFh,IDR_ICON
	 ;menuitem2 '&File',,0;MFR_END
	 du $866C,'F','&File',0
       menuitem '&File',0,MFR_POPUP
		;du $D414,'F','&File',0
		;menuitemseparator $FF0F,300,0
		;du $68,0,"New"
		menuitem 'New',0;IDM_NEW
		menuseparator
		menuitem 'E&xit',IDM_EXIT,MFR_END
       menuitem '&Help',0,MFR_POPUP + MFR_END
		menuitem '&About...',IDM_ABOUT,MFR_END
    end resource_data

    resource_data RT_ICON,IDR_ICON,LANG_NEUTRAL
      virtual at 0
       file 'minipad.ico':6,16
       load size:dword from 8
       load position:dword from 12
      end virtual
      file 'minipad.ico':position,size
    end resource_data

    resource_data RT_GROUP_ICON,IDR_GICON,LANG_NEUTRAL
      dw 0,1,1
      file 'minipad.ico':6,12
      dw IDR_ICON
    end resource_data

    ;resource_data RT_VERSION,1,LANG_NEUTRAL
      ;I was lazy to implement that
    ;end resource_data
  end resource_directory
;dw
;MF_GRAYED	    = 0001h
;MF_?		    = 0002h  (no seen disabling)
;MF_BITMAP	    = 0004h (looks like MF_RIGHTJUSTIFY)
;MF_CHECKED	    = 0008h (no seen difference)
;