( gp EPZ 07/5/01 12:14 )

"events"
\ "safe"
"forms"
"fields"
"case"
"ipif"
"mkpass"

\ using this variable to point
\ whether make standalone prc-file
\ (true) or run under 
\ DragonForth (false)
 
true value ?make-prc

\ Minimal length of password
4 constant MinLength

\ User interface's constants
1000 constant MainForm
1010 constant PassLabel
1021 constant PassField
1030 constant LessButton
1031 constant MoreButton
1025 constant LengthField
1200 constant GenButton
1210 constant CopyButton
1000 constant MBAR1000
2001 constant AboutItem
2003 constant CopyItem
2004 constant GenItem
1000 constant AboutForm

\ shows password's length
\ in field
\ transforming it to the string before
: show-length ( --  )
pass-Size s>d <# #s #>
 LengthField >field
;

API: PrefSetAppPreferencesV10
\ stores app preferences in 
\ palm's preferences database
\ example of using word HERE
\ as a temporary storage
: >store ( -- )
pass-Size here !
\ save preferences with ID EzAL
\ version 1 and lentgth 4 bytes 
'EzAL' 1 here  >abs cell
PrefSetAppPreferencesV10
;

API: PrefGetAppPreferencesV10
\ retrieves app preferences from 
\ palm's preferences database
\ if cannot retrieve data from 
\ database, set password's length
\ to 8
: >get ( -- )
'EzAL' 1 here >abs cell
PrefGetAppPreferencesV10 if
here  @
else  8  then
to pass-size
;

\ handling decreasing of password's
\ length with limits checking
: DecLength ( -- )
Pass-Size 1- 
MinLength  max
to Pass-Size 
show-length
;

\ handling increasing of password's
\ length with limits checking
: IncLength ( -- )
Pass-Size 1+ 
MaxLength min
to Pass-Size 
show-length
;

\ declare using of API functions
API: FldCopy
API: FldSetSelection
API: FrmAlert

\ copies password to clipboard
:  CopyPass ( -- )
PassField FormObjectPtr dup 
\ selects field
0  pass-size  FldSetSelection
\ copy it in clipboard 
FldCopy
\ deselect field - simply
\ set selection from symbol 1 
\ to symbol 1
PassField FormObjectPtr 1 1 FldSetSelection
;

: doMenu ( -- ) \ handle menu events
   event itemid
case
   AboutItem of \ show about info
   AboutForm FrmAlert drop endof
   \ copy pass to clipboard
   CopyItem of  CopyPass endof
    
    \ generate password and show it
   GenItem of 
      doit
       0 pass pass-size PassField >field
endof
endcase
;


\ handing buttons
: doButton ( -- )
event itemID
case 
GenButton of 
doit
0 pass pass-size PassField >field 
endof
\ handling of incresing and 
\ decreasing 
\ the length of password
MoreButton of IncLength endof
LessButton of DecLength endof

\ copy password to clipboard
CopyButton of CopyPass endof
endcase
;

\ application's main event's loop
: EventLoop
  begin
    ekey
    case 
\ important! we need to process 
\ WinEnterEvent - otherwise we'll
\ get a blank space in password's 
\ length field!
        WinEnterEvent of 
        >get  
        show-length endof
        MenuEvent of doMenu endof
\ process buttons
        ctlEnterEvent 
        of doButton endof
    endcase
  again
;

: go
\ show main form 
 MainForm FrmGotoForm
['] EventLoop catch
  drop  
\ call >store procedure
>store ;

\ build or run in DragonForth
?make-prc
[if] 
\ main procedure - GO
 ' go to PilotMain
\ build standalone app
\ with name GP and EzAL creator's ID
 s" gp" 'EzAL'  build

\ use resource with EzA3 creator's ID 
'rsrc' 'EzA3' use-resource

\ copy resources we need
'tFRM' 1000 CopyRes
'MBAR' 1000 CopyRes
'tSTR' 1000 CopyRes
'Talt' 1000 CopyRes
'tAIB' 1000 CopyRes
'tAIB' 1001 CopyRes
\ version of application
'tver' 1000 CopyRes
bye
[else]
\ use resource with EzA3 creator's ID 
'rsrc'  'EzA3' use-resource
\ run GO word and exit
go bye
[then] 