;
;   DESKTOP CONTEXT MENU
;   written by Ivan Poddubny
;
;   €¢â®à - ˆ¢ ­ ®¤¤ã¡­ë©
;   e-mail: ivan-yar@bk.ru
;
;   Compile with flat assembler
;
;------------------------------------------------------------------------------
; version:	1.1
; last update:  27/03/2012
; changed by:   Marat Zakiyanov aka Mario79, aka Mario
; changes:      The program uses only 3404 bytes memory is now.
;               Optimisations and code refactoring.
;------------------------------------------------------------------------------
include 'lang.inc'
include '..\..\..\macros.inc'
;------------------------------------------------------------------------------
	use32
	org 0x0
	db 'MENUET01'	; 8 byte id
	dd 0x01		; header version
	dd START	; start of code
	dd IM_END	; size of image
	dd I_END	; memory for app
	dd stack_area	; esp
	dd 0		; boot parameters
	dd 0		; path
;------------------------------------------------------------------------------
START:
; ¯®«ãç¨âì á¨á⥬­ë¥ 梥â 
	mcall	48,3,sc,sizeof.system_colors
; ãáâ ­®¢¨¬ ¬ áªã ᮡë⨩ - ­ á ¨­â¥à¥áã¥â ⮫쪮 ¬ëèì
	mcall	40,100000b
;------------------------------------------------------------------------------
align 4
still:		     ; £« ¢­ë© 横« ®á­®¢­®£® ¯à®æ¥áá 
	mcall	10	; ¦¤ñ¬ ᮡëâ¨ï

	mcall	37,2	; ª ª¨¥ ­ ¦ âë ª¯®¯ª¨?
	cmp	eax,ebx	; ¥á«¨ ­¥ ¯à ¢ ï, ¢®§¢à â
	jne	still
;--------------------------------------
; íâ® ¤«ï ®â« ¤ª¨ - ¥á«¨ ¬ëèì ¢ â®çª¥ (0;0), § ªà®¥¬áï
;	xor	ebx,ebx
;	mcall	37
;	test	eax,eax	; ªãàá®à ¢ â®çª¥ (0;0), â.¥. eax = 0
;	je	exit
;--------------------------------------
; ª®®à¤¨­ âë ªãàá®à 
	xor	ebx,ebx
	mcall	37

	mov	ebx,eax		; eax = cursor_x
	shr	eax,16		; ebx = cursor_y
	and	ebx,0xffff
	mov	[curx1],eax	; curx1 = cursor_x
	mov	[cury1],ebx	; cury1 = cursor_y
; ª®¬ã ¯à¨­ ¤«¥¦¨â â®çª ?
	mcall	34,[curx1],[cury1]
	cmp	al,1 ; 1 - ï¤à®
	jne	still
;--------------------------------------
align 4
@@:		; ¯®¤®¦¤ñ¬, ¯®ª  ¯®«ì§®¢ â¥«ì ­¥ ®â¯ãá⨫ ¯à ¢ãî ª­®¯ªã ¬ëè¨
	mcall	37,2    ;   ­ ¦ âë «¨ ª­®¯ª¨ ¬ëè¨?
	test	eax,ebx ; ¥á«¨ ®â¯ãá⨫, (eax != 2)
	jz	@f	;   ¨¤ñ¬ ¢ ­ ç «® £« ¢­®£® 横« 

	mcall	68,1	; ¨­ ç¥ ¯¥à¥ª«î稬áï ­  á«¥¤ãî騩 ¯®â®ª á¨áâ¥¬ë ¨ ª®£¤ 
	jmp	@b	; ¢ë¯®«­¥­¨¥ ¢¥à­¥âáï í⮬㠯®â®ªã, ¯à®¢¥à¨¬ ¬ëèì ®¯ïâì
;--------------------------------------
align 4	
@@:
; ¥á«¨ 㦥 ¡ë«® ®âªàëâ® ¬¥­î, ­ã¦­® ¯®¤®¦¤ âì, ¯®ª  ®­® § ªà®¥âáï:
	cmp	[menu_opened],0
	je	@f

	mcall	68,1	; ¯¥à¥ª«î稬áï ­  á«¥¤ãî騩 ¯®â®ª á¨á⥬ë 
			; ¡®«¥¥ íä䥪⨢­ë© ᯮᮡ § ¤¥à¦ª¨ 祬 mcall 5
	jmp	@b
;--------------------------------------
align 4
@@:
;   ⥯¥àì ¬®¦­® ᬥ«® § ¯ã᪠âì ¯à®æ¥áá (¯®â®ª) ¬¥­î
	mcall	51,1,start_wnd,stack_wnd
	jmp	still
;------------------------------------------------------------------------------	
align 4
exit_menu:	      ; ¥á«¨ ¢ë室¨¬ ¨§ ¬¥­î, ­ ¤® § ¯¨á âì ¢ [menu_opened] 0
	mov	[menu_opened],0
;--------------------------------------
align 4
exit:		      ; á ¬ë ¨¤ñ¬, ª®£¤  ¢ë室¨¬ ¨§ ®á­®¢­®£® ¯à®æ¥áá 
	or	eax,-1	      ; eax = -1
	mcall
;------------------------------------------------------------------------------	
; §¤¥áì áâ àâã¥â ¯à®æ¥áá ¬¥­î
;------------------------------------------------------------------------------	
align 4
start_wnd:
	mov	[menu_opened],1
; ãáâ ­®¢¨¬ ¬ áªã ¦¥« ¥¬ëå ᮡë⨩: ¬¥­î + ª­®¯ª¨ + ¯¥à¥à¨á®¢ª 
	mcall	40,100101b
;------------------------------------------------------------------------------	
align 4
red:
	call	draw_window
;------------------------------------------------------------------------------	
align 4
still2: 	    ; £« ¢­ë© 横« ¯à®æ¥áá  ¬¥­î
	mcall	10	; ¦¤ñ¬ ᮡëâ¨ï

	cmp	eax,1	    ; ¯¥à¥à¨á®¢ª ?
	je	red
	
	cmp	eax,3	    ; ª­®¯ª ?
	je	button
	
	cmp	eax,6	    ; ¬ëèì?
	je	mouse

	jmp	still2	    ; ¢¥à­ñ¬áï ¢ ­ ç «® £« ¢­®£® 横« 
;------------------------------------------------------------------------------	
align 4
; Ž€Ž’—ˆŠ Œ›˜ˆ
mouse:		  ; ª®£¤  ¯®«ì§®¢ â¥«ì ­ ¦¬ñâ ª­®¯ªã ¬ëè¨, § ªà®¥¬áï
	mcall	37,2	; ª ª¨¥ ª­®¯ª¨ ­ ¦ âë?
	test	eax,eax   ; ­¨ª ª¨¥? - ⮣¤  ¯à¥ªà á­®! ¢¥à­ñ¬áï ¢ £« ¢­ë© 横«
	jz	still2

        mcall   37,0

        mov     esi, eax
        shr     esi, 16
        movzx   edi, ax
        mcall   9, procinfo, -1
	
        mov     eax, [procinfo.box.left]
        cmp     esi, eax
        jl      exit_menu

        add     eax, [procinfo.box.width]
        cmp     esi, eax
        jge     exit_menu

        mov     eax, [procinfo.box.top]
        cmp     edi, eax
        jl      exit_menu

        add     eax, [procinfo.box.height]
        cmp     edi, eax
        jge     exit_menu

        jmp     still2
;------------------------------------------------------------------------------	
align 4
; €†€’€ ŠŽŠ€
button:
	mcall	17	; ¯®«ãç¨âì ¨¤¥­â¨ä¨ª â®à ­ ¦ â®© ª­®¯ª¨

	sub	ah,10	      ; áà ¢­¨¢ ¥¬ á 10
	jl	nofuncbtns    ; ¥á«¨ ¬¥­ìè¥ - § ªà뢠¥¬ ¬¥­î

	movzx	ebx,ah	      ; ¯®«ã稫¨ ­®¬¥à ¯à®£à ¬¬ë ¢ ᯨ᪥ ¢ ebx
	mov	esi,[startapps + ebx*4]
	mov	edi,start_info.path
	cld
;--------------------------------------
align 4
@@:
	lodsb
	stosb
	test	al,al
	jnz	@b
	mcall	70, start_info

;	mov	eax,5         ; ¯®¤®¦¤ñ¬, ¯®ª  ¯à®£à ¬¬  § ¯ãáâ¨âìáï
;	mov	ebx,1         ;   â® ¥ñ ®ª­® ­¥ ¡ã¤¥â ®âà¨á®¢ ­® (¡ £ ¢ ï¤à¥???)
;	mcall          ; à áª®¬¬¥­â¨àã©â¥ í⨠áâப¨, ¥á«¨ ã ¢ á ¯à®¡«¥¬ë
		       ; á ®âà¨á®¢ª®©
;--------------------------------------
align 4
nofuncbtns:	      ; § ªà뢠¥¬ ¬¥­î
	jmp	exit_menu
;------------------------------------------------------------------------------	
_BTNS_		  = 6	  ; ª®«¨ç¥á⢮ ª­®¯®ª ("¯ã­ªâ®¢ ¬¥­î")

if lang eq ru
  font		  = 0x00000000
  string_length   = 20		; ¤«¨­  áâப¨
  wnd_x_size	  = 133 	; è¨à¨­  ®ª­ 
  title_pos	 = 36 shl 16 + 7
else
  font		  = 0x10000000
  string_length   = 12		; ¤«¨­  áâப¨
  wnd_x_size	  = 105 	; è¨à¨­  ®ª­ 
  title_pos	 = 23 shl 16 + 7
end if
;------------------------------------------------------------------------------	
;*******************************
;********  ˆ‘“…Œ ŽŠŽ  ********
;*******************************
draw_window:
	mcall	12,1	; ­ ç¨­ ¥¬ "à¨á®¢ âì"

	mov	eax,[curx1]	 ; ⥪ã騥 ª®®à¤¨­ âë ªãàá®à 
	mov	[curx],eax	 ; § ¯¨è¥¬ ¢ ª®®à¤¨­ âë ®ª­ 
	mov	eax,[cury1]
	mov	[cury],eax
; ⥯¥àì ¡ã¤¥¬ áç¨â âì ª®®à¤¨­ âë ®ª­ , çâ®¡ë ®­® §  ªà © íªà ­  ­¥ ¢ë«¥§«®
	mcall	14		; ¯®«ã稬 à §¬¥à íªà ­ 

	mov	ebx,eax
	shr	eax,16			; ¢ eax - x_screen
	and	ebx,0xffff		; ¢ ebx - y_screen
	add	eax,-wnd_x_size		; eax = [x_screen - è¨à¨­  ®ª­ ]
	add	ebx,-_BTNS_*15-21	; ebx = [y_screen - ¢ëá®â  ®ª­ ]

	cmp	eax,[curx]
	jg	.okx			; ¥á«¨ ®ª­® ᫨誮¬ ¡«¨§ª® ª ¯à ¢®¬ã ªà î,
	add	[curx],-wnd_x_size	; ᤢ¨­¥¬ ¥£® ¢«¥¢® ­  100
;--------------------------------------
align 4
.okx:
	cmp	ebx, [cury]
	jg	.oky			; ¯® ¢¥à⨪ «¨ â®ç­® â ª¦¥
	add	[cury], -_BTNS_*15-21
;--------------------------------------
align 4
.oky:
	xor	eax, eax	   ; äã­ªæ¨ï 0 - ᮧ¤ âì ®ª­®
	mov	ebx, [curx]	   ;  ebx = [ª®®à¤¨­ â  ¯® x] shl 16 + [è¨à¨­ ]
	shl	ebx, 16
	add	ebx, wnd_x_size
	mov	ecx, [cury]	   ;  ecx = [ª®®à¤¨­ â  ¯® y] shl 16 + [¢ëá®â ]
	shl	ecx, 16
	add	ecx, _BTNS_*15+21
	mov	edx, [sc.work]	   ;  梥â à ¡®ç¥© ®¡« áâ¨
	mov	esi, [sc.grab]	   ;  梥⠧ £®«®¢ª 
	or	esi, 0x81000000
	mov	edi, [sc.frame]    ;  梥â à ¬ª¨
	mcall

	mov	eax, 4		   ; § £®«®¢®ª
	mov	ebx, title_pos	  ;  [x] shl 16 + [y]
	mov	ecx, [sc.grab_text];  èà¨äâ ¨ 梥â (á¥àë©)
	or	ecx, 0x10000000

	push	ecx
	push	ecx
	xor	edx,edx
;--------------------------------------
align 4
.dec_color:
	sub	byte [esp+edx], 0x33
	jae	@f
	mov	byte [esp+edx], 0
;--------------------------------------
align 4
@@:
	inc	edx
	jnp	.dec_color
	pop	ecx
	mov	edx, title	  ;   ¤à¥á § £®«®¢ª 
	mov	esi, title.size   ;  ¤«¨­  § £®«®¢ª  ("M E N U")
	mcall
	pop	ecx
	add	ebx, 1 shl 16	   ;  ᤢ¨­¥¬ ¢¯à ¢® ­  1
	mcall

	mov	ebx, 1*65536+wnd_x_size-2  ; ­ ç¨­ ¥¬ ¤¥« âì ª­®¯ª¨
	mov	ecx, 20*65536+15
	mov	edx, 10 or 0x40000000 ; ¡¨â 30 ãáâ ­®¢«¥­ => ª­®¯ª  ­¥ à¨áã¥âáï

	mov	edi,_BTNS_	     ; ª®«¨ç¥á⢮ ª­®¯®ª (áçñâ稪)
;--------------------------------------
align 4
newbtn:		     ; ­ ç «® 横« 
	mcall	8		;  ᮧ¤ ñ¬ ª­®¯ªã

			     ;  ¯¨è¥¬ ⥪áâ ­  ª­®¯ª¥
	pushad		     ;   ᯠᠥ¬ ॣ¨áâàë
	shr	ecx, 16
	and	ebx, 0xffff0000
	add	ebx, ecx	     ;   ebx = [x] shl 16 + [y];
	add	ebx, 10*65536+4      ;   ebx += ᬥ饭¨¥ ®â­®á¨â¥«ì­® ªà ï ª­®¯ª¨;
	mov	ecx, [sc.work_text]  ;   èà¨äâ ¨ 梥â
	or	ecx, font
	add	edx, -10	     ;   edx = ­®¬¥à ª­®¯ª¨;
	imul	edx, string_length   ;   edx *= ¤«¨­  áâப¨;
	add	edx, text	     ;   edx += text;  ⥯¥àì ¢ edx  ¤à¥á áâப¨
	mov	esi, string_length   ;   ¢ esi - ¤«¨­  áâப¨
	mcall	4
	popad

	inc	edx		     ;  ­®¬¥à ª­®¯ª¨++;
	add	ecx,15*65536	     ;  㢥«¨ç¨¬ ᬥ饭¨¥ ¯® y
	dec	edi		     ;  㬥­ì訬 áçñâ稪
	jnz	newbtn		     ; ¥á«¨ ­¥ ­®«ì, ¯®¢â®à¨¬ ¢áñ ¥éñ à §

	mcall	12,2	; § ª®­ç¨«¨ "à¨á®¢ âì"
	ret			     ; ¢®§¢à â
;------------------------------------------------------------------------------	
align 4
; „€›… Žƒ€ŒŒ›

  macro strtbl name, [string]
  {
   common
     label name dword
   forward
     local str
     dd str
   forward
     str db string
  }

  strtbl startapps	 ,\
    <"/sys/PIC4",0>	,\
    <"/sys/DESKTOP",0>	,\
    <"/sys/ICON",0>,\
    <"/sys/SETUP",0>	,\
    <"/sys/DEVELOP/BOARD",0> ,\
    <"/sys/CPU",0> 
    
  sz title, "KolibriOS"

  lsz text,\
    en, 'Background  ',\
    en, 'Desktop     ',\
    en, 'Icon manager',\
    en, 'Device setup',\
    en, 'Debug board ',\
    en, 'Processes   ',\
    \
    ru, 'ƒ¥­¥à â®à ®¡®¥¢     ',\
    ru, ' áâனª  ®ª®­      ',\
    ru, '“¯à ¢«¥­¨¥ ¨ª®­ª ¬¨ ',\
    ru, ' áâனª  ãáâனá⢠',\
    ru, ' ­¥«ì ®â« ¤ª¨      ',\
    ru, 'à®æ¥ááë            '
;------------------------------------------------------------------------------	
align 4
start_info:
	.mode	dd 7
		dd 0
	.params dd 0
		dd 0
		dd 0
		db 0
		dd start_info.path
;------------------------------------------------------------------------------
IM_END:	
align 4
; …ˆˆ–ˆ€‹ˆ‡ˆŽ‚€›… „€›…
  curx1		dd ?	; ª®®à¤¨­ âë ªãàá®à 
  cury1		dd ?
  curx		dd ?	; ª®®à¤¨­ âë ®ª­  ¬¥­î
  cury		dd ?

  menu_opened	db ?	; ®âªàëâ® ¬¥­î ¨«¨ ­¥â? (1-¤ , 0-­¥â)
;------------------------------------------------------------------------------	
align 4
start_info.path	rb 256
;------------------------------------------------------------------------------	
align 4
sc	system_colors	; á¨á⥬­ë¥ 梥â 
;------------------------------------------------------------------------------	
align 4
procinfo process_information	; ¨­ä®à¬ æ¨ï ® ¯à®æ¥áá¥
;------------------------------------------------------------------------------	
align 4
	rb 512			; áâíª ¤«ï ®ª­  ¬¥­î - 墠â¨â ¨ 1 Š¡
stack_wnd:
;------------------------------------------------------------------------------	
align 4
	rb 512
stack_area:
;------------------------------------------------------------------------------	
I_END:
;------------------------------------------------------------------------------	
; ŠŽ…– Žƒ€ŒŒ›
;------------------------------------------------------------------------------