/*
   rotate_opt_opts.idlg

*!  VERSION 1.0.2  09feb2009

	// SCRIPTS and PROGRAMS for external use
		* SCRIPT rotate_opt_opts_enable
		* SCRIPT rotate_opt_opts_disable
		* SCRIPT rotate_opts_reloadMatrix
		* PROGRAM rotate_opt_opts_output
*/

SCRIPT rotate_opts_reloadMatrix
BEGIN
	create STRING tmpMatrixName
	tmpMatrixName.setvalue class rotate_opt_opts.cb_init.value
	rotate_opt_opts.cb_init.repopulate
	rotate_opt_opts.cb_init.setvalue class tmpMatrixName.value
END

DIALOG rotate_opt_opts, tabtitle("Optimization")
BEGIN
  SPINNER  sp_iter	_lft	_top		_spwd	.,		///
	label("Maximum iterations")			 		///
	min(0) max(16000) default(1000)					///
	option(iterate)
  TEXT     tx_iter	_spsep	@		_spr2b	.,		///
	label("Maximum iterations")

  GROUPBOX gb_log	_lft	_ls		_iwd	_ht6,		///
	label("Iteration log")
  RADIO	   rb_nolog	_indent	_ss		_inwd	.,		///
	label("No iteration log (default)") first option(NONE)
  RADIO	   rb_log	@	_ss		@	.,		///
	label("Display an iteration log") option(log)
  RADIO    rb_trace	@	_ss		@	.,		///
	label("Display rotation and rotated matrix at each iteration")	///
	option(trace) last

  GROUPBOX gb_tol	_lft	_xls		_iwd	_ht6,		///
	label("Convergence tolerances")
  EDIT     ed_tol	_ilft	_ms		_en7wd	.,		///
	label("Rotation matrix")					///
	option(tolerance) default(1e-6)
  DEFINE holdy @y
  TEXT     tx_tol	_en7sep	@		_en7r2b	.,		///
	label("Rotation matrix")
  EDIT     ed_ltol	_lft2	holdy		_en7wd	.,		///
	label("Projected gradient")					///
	option(ltolerance) default(1e-6)
  TEXT     tx_ltol	_en7sep	@		_en7r2b	.,		///
	label("Projected gradient")
  EDIT     ed_gtol	_ilft	_ls		_en7wd	.,		///
	label("Optimization criterion")					///
	option(gtolerance) default(1e-6)
  TEXT     tx_gtol	_en7sep	@		_en7r2b	.,		///
	label("Optimization criterion")

  CHECKBOX ck_protect	_lft	+45		_cwd1	.,		///
	onclickon("program _protect_on")				///
	onclickoff("program _protect_off")				///
	label("Number of different initial positions")
  DEFINE holdy @y
  SPINNER  sp_protect	_cksep	_ss		_spwd	.,		///
	min(1) default(50) max(100000)					///
	option(protect)							///
	label("Number of iteration trials")

  CHECKBOX ck_init	_lft2	holdy		_cwd2	.,		///
  	onclickon(program _ck_init_on)					///
  	onclickoff(program _ck_init_off)				///
	label("Initial rotation matrix for iteration:")
  COMBOBOX cb_init     _indent2	_ss		_vnwd	.,		///
	label("Initial rotation matrix for iteration") option(init) 	///
	dropdown 							///
	contents(matrix)

  CHECKBOX ck_maxstep	_lft	_ms		_cwd1	.,		///
	onclickon(script _maxstep_on)					///
	onclickoff(script _maxstep_off)					///
	label("Maximum number of step halvings")
  DEFINE holdy @y
  SPINNER  sp_maxstep	_cksep	_ss		_spwd	.,		///
	min(1) default(20) max(100000)					///
	option(maxstep)							///
	label("Maximum number of step halvings")
  TEXT     tx_maxstep	_en7sep	@		150	.,		///
	label("#")

  CHECKBOX ck_random	_lft2	holdy		_cwd2	.,           	///
	option(random)							///
  	onclickon(program _ck_random_on)				///
  	onclickoff(program _ck_random_off)				///
	label("Start from random initial rotation")
END

SCRIPT rotate_opt_opts_disable	// for external use
BEGIN
	program _opts_disable_pr
END
PROGRAM _opts_disable_pr
BEGIN
	call rotate_opt_opts.sp_iter.disable
	call rotate_opt_opts.tx_iter.disable
	call rotate_opt_opts.gb_log.disable
	call rotate_opt_opts.rb_nolog.disable
	call rotate_opt_opts.rb_log.disable
	call rotate_opt_opts.rb_trace.disable
	call rotate_opt_opts.gb_tol.disable
	call rotate_opt_opts.ed_tol.disable
	call rotate_opt_opts.tx_tol.disable
	call rotate_opt_opts.ed_ltol.disable
	call rotate_opt_opts.tx_ltol.disable
	call rotate_opt_opts.ed_gtol.disable
	call rotate_opt_opts.tx_gtol.disable
	call rotate_opt_opts.ck_protect.disable
	if rotate_opt_opts.ck_protect {
		call rotate_opt_opts.sp_protect.disable
	}
	call rotate_opt_opts.ck_init.disable
	if rotate_opt_opts.ck_init {
		call rotate_opt_opts.cb_init.disable
	}
	call rotate_opt_opts.ck_maxstep.disable
	if rotate_opt_opts.ck_maxstep {
		call rotate_opt_opts.sp_maxstep.disable
	}
	call rotate_opt_opts.ck_random.disable
END

SCRIPT rotate_opt_opts_enable	// for external use
BEGIN
	program _opts_enable_pr
END
PROGRAM _opts_enable_pr
BEGIN
	call rotate_opt_opts.sp_iter.enable
	call rotate_opt_opts.tx_iter.enable
	call rotate_opt_opts.gb_log.enable
	call rotate_opt_opts.rb_nolog.enable
	call rotate_opt_opts.rb_log.enable
	call rotate_opt_opts.rb_trace.enable
	call rotate_opt_opts.gb_tol.enable
	call rotate_opt_opts.ed_tol.enable
	call rotate_opt_opts.tx_tol.enable
	call rotate_opt_opts.ed_ltol.enable
	call rotate_opt_opts.tx_ltol.enable
	call rotate_opt_opts.ed_gtol.enable
	call rotate_opt_opts.tx_gtol.enable
	call rotate_opt_opts.ck_protect.enable
	if rotate_opt_opts.ck_protect {
		call rotate_opt_opts.sp_protect.enable
	}
	if !rotate_opt_opts.ck_protect & !rotate_opt_opts.ck_random {
		call rotate_opt_opts.ck_init.enable
		if rotate_opt_opts.ck_init {
			call rotate_opt_opts.cb_init.enable
		}
	}
	call rotate_opt_opts.ck_maxstep.enable
	if rotate_opt_opts.ck_maxstep {
		call rotate_opt_opts.sp_maxstep.enable
	}
	if !rotate_opt_opts.ck_protect & !rotate_opt_opts.ck_init {
		call rotate_opt_opts.ck_random.enable
	}
END

PROGRAM _protect_on
BEGIN
  call rotate_opt_opts.sp_protect.enable
  if !H(rotate_opt_opts.ck_init) {
  	if rotate_opt_opts.ck_init {
  		call rotate_opt_opts.cb_init.disable
  	}
  	call rotate_opt_opts.ck_init.disable
  }
  if !H(rotate_opt_opts.ck_random) {
  	call rotate_opt_opts.ck_random.disable
  }
END

PROGRAM _protect_off
BEGIN
  call rotate_opt_opts.sp_protect.disable
  if !(rotate_opt_opts.ck_random) {
  	call rotate_opt_opts.ck_init.enable
  	if rotate_opt_opts.ck_init {
  		call rotate_opt_opts.cb_init.enable
  	}
  }
  if !(rotate_opt_opts.ck_init) {
  	call rotate_opt_opts.ck_random.enable
  }
END

SCRIPT _maxstep_on
BEGIN
	rotate_opt_opts.sp_maxstep.enable
	rotate_opt_opts.tx_maxstep.enable
END

SCRIPT _maxstep_off
BEGIN
	rotate_opt_opts.sp_maxstep.disable
	rotate_opt_opts.tx_maxstep.disable
END

PROGRAM _ck_init_on
BEGIN
	call rotate_opt_opts.cb_init.enable
	if rotate_opt_opts.ck_init {
		call rotate_opt_opts.ck_random.disable
	}
END

PROGRAM _ck_init_off
BEGIN
	call rotate_opt_opts.cb_init.disable
	if !rotate_opt_opts.ck_init {
		call rotate_opt_opts.ck_random.enable
	}
END

PROGRAM _ck_random_on
BEGIN
	if rotate_opt_opts.ck_random {
		call rotate_opt_opts.ck_init.disable
	}
END

PROGRAM _ck_random_off
BEGIN
	if !rotate_opt_opts.ck_random {
		call rotate_opt_opts.ck_init.enable
	}
END

PROGRAM rotate_opt_opts_output
BEGIN
	optionarg /hidedefault rotate_opt_opts.sp_iter
	option rotate_opt_opts.rb_log
	option rotate_opt_opts.rb_trace
	option rotate_opt_opts.rb_nolog
	optionarg /hidedefault rotate_opt_opts.ed_tol
	optionarg /hidedefault rotate_opt_opts.ed_ltol
	optionarg /hidedefault rotate_opt_opts.ed_gtol
	if rotate_opt_opts.ck_protect {
		optionarg rotate_opt_opts.sp_protect
	}
	if rotate_opt_opts.ck_maxstep {
		require rotate_opt_opts.sp_maxstep
		optionarg rotate_opt_opts.sp_maxstep
	}
	if rotate_opt_opts.ck_init {
		require rotate_opt_opts.cb_init
		optionarg rotate_opt_opts.cb_init
	}
	option rotate_opt_opts.ck_random
END
