// DEFBUTT.PRG
// Example of how to change the state of a button for a default
// Hit CTRL+BREAK to exit program.

set window title to 'Default Button Example'

// Windows API function calls
declare pfunc SendMessage returning integer in dll user.exe
declare pfunc GetFocus returning integer in dll user.exe
declare pfunc SetFocus returning integer in dll user.dll

#define BM_SETSTYLE			1028			// WM_USER + 4
#define BS_PUSHBUTTON		0
#define BS_DEFPUSHBUTTON	1
#define EM_SETPASSWORDCHAR	1052

set window type to 4+32+1024
create window 'Enter Password' from 5,5 to 15,40

create button 'OK' at 1,24 size 1,8
create button 'Cancel' at 3,24 size 1,8 plain 
create button 'Help' at 6,24 size 1,8 plain

@ 1,2 say "Password:"

sEditSave = ''
create control editbox 'Edit01' at 2,2 save to sEditSave size 1,10

// get the handles of the buttons and editbox
hButOK = hwnd('OK')
hButCancel = hwnd('Cancel')
hButHelp = hwnd('Help')
hEdit01 = hwnd('Edit01')

// Set the password character to 42 (decimal), which is '*' in ASCII.
// Password character can be set to almost any character
// Note: This will not work on a multi-line editbox.
SendMessage(toint(hEdit01), toint(EM_SETPASSWORDCHAR), toint(42), tolong(0))

// force focus to the editbox
SetFocus(toint(hEdit01))

hOldFoc = GetFocus()

do while .t.
	myevent = -1
	do while myevent = -1
		// chkevent() must be first
		myevent = chkevent()
		hCurrentFoc = GetFocus()
		// Event 6 will be generated by a button click, and also if ENTER
		// is pressed when the button has focus
		if ((myevent = 6) .or. (myevent = 1))
			if ((hCurrentFoc = hButOK) .or. (myevent = 1))
				if (lastkey() != 27)
					@ 6,0 say 'OK selected    '
				else
					@ 6,0 say 'Cancel selected'
				endif
			else	
				if hCurrentFoc = hButCancel
					@ 6,0 say 'Cancel selected'
				else
					@ 6,0 say 'Help selected  '
				endif				
			endif			
		endif
		if hCurrentFoc <> hOldFoc
			do case
				case hCurrentFoc = hButOK
					// if last parameter is 1, button is redrawn immediately
					SendMessage(toint(hButOK), toint(BM_SETSTYLE), toint(BS_DEFPUSHBUTTON), tolong(1))
					SendMessage(toint(hButCancel), toint(BM_SETSTYLE), toint(BS_PUSHBUTTON), tolong(1))
					SendMessage(toint(hButHelp), toint(BM_SETSTYLE), toint(BS_PUSHBUTTON), tolong(1))
				case hCurrentFoc = hButCancel
					SendMessage(toint(hButOK), toint(BM_SETSTYLE), toint(BS_PUSHBUTTON), tolong(1))
					SendMessage(toint(hButCancel), toint(BM_SETSTYLE), toint(BS_DEFPUSHBUTTON), tolong(1))
					SendMessage(toint(hButHelp), toint(BM_SETSTYLE), toint(BS_PUSHBUTTON), tolong(1))
				case hCurrentFoc = hButHelp
					SendMessage(toint(hButOK), toint(BM_SETSTYLE), toint(BS_PUSHBUTTON), tolong(1))
					SendMessage(toint(hButCancel), toint(BM_SETSTYLE), toint(BS_PUSHBUTTON), tolong(1))
					SendMessage(toint(hButHelp), toint(BM_SETSTYLE), toint(BS_DEFPUSHBUTTON), tolong(1))
				otherwise
					// if focus is on anything other than a button, set the default to 'OK'
					SendMessage(toint(hButOK), toint(BM_SETSTYLE), toint(BS_DEFPUSHBUTTON), tolong(1))
					SendMessage(toint(hButCancel), toint(BM_SETSTYLE), toint(BS_PUSHBUTTON), tolong(1))
					SendMessage(toint(hButHelp), toint(BM_SETSTYLE), toint(BS_PUSHBUTTON), tolong(1))
			endcase
		endif
		hOldFoc = hCurrentFoc
	enddo
enddo


