;;-----------------------------------------------------------------------------
;; PURPOSE:
;;	A combo widget to create a password entry field. This text field 
;;	displays asterixes instead of the actual password. Note that the clear
;;	text password is stored in the widget so it's not the most secure 
;;	solution
;;
;; CALLING SEQUENCE:
;;	id = DICW_pwdField(parent, VALUE=value, UVALUE=uvalue, UNAME=uname,
;;		/SENSITIVE, XSIZE=xsize)
;;
;; REQUIRED INPUTS:
;;	parent - partent of the widget
;;
;; OUTPUTS:
;;	The widget ID of the combo widget
;;		
;; OPTIONAL INPUT KEYWORDS:
;;	VALUE - Initial value of the password
;;	UVALUE - User defined value
;;	UNAME - User name for the widget
;;	SENSITIVE - Is the field sensitive to user interations (default=1)
;;	XSIZE - Size in characters of the text field
;;
;; EXAMPLE:
;;      IDL> id = DICW_pwdField(parent)
;;
;; EVENTS PRODUCED:
;;	All WIDGET_TEXT* events
;;
;; PROCEDURES USED (i.e. called directly!):
;;
;; MODIFICATION HISTORY:
;;   	2004-09-24  M. Desnoyer - Created
;;
;;-----------------------------------------------------------------------------

;;--------------
;; Event handler
;;--------------
FUNCTION DICW_pwdField_event, ev

;; Get the value of the password field
widget_control, ev.ID, get_uvalue=val, /NO_COPY

CASE ev.type OF

	;; Character added
	0: BEGIN
		val = strmid(val,0,ev.offset)+string(ev.ch)+$
			strmid(val,ev.offset)
		cursor=ev.offset+1
	END

	;; String added
	1: BEGIN
		val = strmid(val,0,ev.offset)+ev.str+strmid(val,ev.offset)
		cursor=ev.offset+strlen(ev.str)
	END

	;; Text deleted
	2: BEGIN
		val = strmid(val,0,ev.offset)+strmid(val,ev.offset+ev.length)
		cursor=ev.offset
	END

	;; Selection move
	3: cursor=[ev.offset, ev.length]

ELSE: BREAK
ENDCASE

widget_control, ev.HANDLER, set_value=val, /NO_COPY
widget_control, ev.ID, set_text_select=cursor

ev.ID = ev.HANDLER

RETURN, ev
END

;;--------------
;; Set value handler
;;--------------
PRO DICW_pwdField_setVal, id, val

;; Get the field
fld = widget_info(id, /child)

;; Create the display
len = strlen(val)
disp = (len GT 0)?strjoin(replicate('*',len)):''

;; Update the widget
widget_control, fld, set_value=disp, set_uvalue=val

END

;;--------------
;; Get value handler
;;--------------
FUNCTION DICW_pwdField_getVal, id

;; Get the field
fld = widget_info(id, /child)

widget_control, fld, get_uvalue=value

RETURN, value
END

;;--------------
;; Actual creation routine
;;--------------
FUNCTION DICW_pwdField, parent, VALUE=value, UVALUE=uvalue, UNAME=uname,$
	SENSITIVE=sensitive, XSIZE=xsize

IF n_params() NE 1 THEN message, 'Must specify a parent'
IF NOT keyword_set(value) THEN value=''

;; Create a container to hold the password field
base = widget_base(parent, uvalue=uvalue, uname=uname, sensitive=sensitive, $
	func_get_value='DICW_pwdField_getVal', $
	pro_set_value='DICW_pwdField_setVal', $
	event_func='DICW_pwdField_event')

;; Create the text field
len = strlen(value)
disp = (len GT 0)?strjoin(replicate('*',len)):''
fld = widget_text(base, /all_events, /sensitive, value=disp, $
	uvalue=value, xsize=xsize)

;; Turn on the event handling
;;xmanager, 'dicw_pwdfield', base, event_handler='DICW_pwdField_event', /no_block

RETURN, base
END