/*
 contract

*! VERSION 1.2.1  01jun2011

*/

VERSION 10.0

INCLUDE _std_mlarge
INCLUDE _ht380
INCLUDE header
HELP hlp1, view("help contract")
RESET res1

DIALOG main, tabtitle("Main")						///
	label("contract - Make dataset of frequencies and percentages")

BEGIN
  TEXT     tx_var            _lft      _top      _iwd      .,		///
		label("Variables:")
  VARLIST  vl_var            @         _ss       _iwd      .,		///
  		label("Variables")
END

INCLUDE ifin
INCLUDE weights_f

DIALOG opt, tabtitle("Options")
BEGIN
  TEXT     tx_freq           _lft      _top      _cwd2     .,		///
  		label("Name of frequency variable:")
  EDIT     ed_freq           @         _ss       _vnwd     .,		///
  		label("Name of frequency variable")			///
  		max(32)							///
  		option("freq")						///
  		default(_freq)

  CHECKBOX ck_cfreq          @         _ls       _iwd     .,		///
  		label("Create cumulative frequency variable:")		///
  		onclickon(opt.ed_cfreq.enable)				///
  		onclickoff(opt.ed_cfreq.disable)
  EDIT     ed_cfreq          +20       _ss       _vnwd     .,		///
  		label("Cumulative frequency variable")			///
  		option(cfreq)

  GROUPBOX gb_percent        _lft      _ls       _iwd      _ht17,	///
	label("New percentage variables")
  CHECKBOX ck_percent        _ilft     _ss       _ibwd     .,		///
  		label("Create percentage variable:")			///
  		onclickon(program opt_percent_on)			///
  		onclickoff(program opt_percent_off)
  EDIT     ed_percent        +20       _ss       _vnwd     .,		///
  		label("Percentage variable")				///
  		option(percent)

  CHECKBOX ck_cpercent       _ilft     _ls       _ibwd     .,		///
  		label("Create cumulative percentage variable:")		///
  		onclickon(program opt_cpercent_on)			///
  		onclickoff(program opt_cpercent_off)
  EDIT     ed_cpercent       +20       _ss       _vnwd     .,		///
  		label("Cumulative percentage variable")			///
  		option(cpercent)

  CHECKBOX ck_float          _ilft     _ls       _ibwd     .,		///
  		label("Generate percentage variables as type float")	///
  		option(float)
  CHECKBOX ck_format         @         _ms       @         .,		///
		onclickon(program opt_ck_format_check)			///
		onclickoff(program opt_ck_format_check)			///
  		label("Override display format for new percentage variables")
  EDIT     ed_format         +20       _ss       200       .,		///
  		label("Display format for new percentage variables")	///
  		option(format)
  BUTTON   bu_format         +205      @         80        .,		///
	label("Create...")						///
	onpush(script showFormatDialog)					///
	tooltip("Create display format")

  CHECKBOX ck_zero           _lft      _xxls     _iwd      .,		///
  		label("Include combinations with frequency zero")	///
  		option("zero")
  CHECKBOX ck_nomiss         @         _ms       @         .,		///
  		label("Drop observations with missing values")		///
  		option("nomiss")
END

SCRIPT showFormatDialog
BEGIN
	create STRING formatString
	create CHILD format_chooser
	format_chooser.setExitString formatString
	format_chooser.setExitAction "opt.ed_format.setvalue class formatString.value"
END

PROGRAM opt_ck_format_check
BEGIN
	if opt.ck_format & opt.ck_format.isenabled() {
		call opt.ed_format.enable
		call opt.bu_format.enable
	}
	else {
		call opt.ed_format.disable
		call opt.bu_format.disable
	}
END

PROGRAM opt_percent_on
BEGIN
	call opt.ed_percent.enable
	call opt.ck_float.enable
	call opt.ck_format.enable
	call program opt_ck_format_check
END

PROGRAM opt_cpercent_on
BEGIN
	call opt.ed_cpercent.enable
	call opt.ck_float.enable
	call opt.ck_format.enable
	call program opt_ck_format_check
END

PROGRAM opt_percent_off
BEGIN
	call opt.ed_percent.disable
	if ! opt.ck_cpercent {
		call opt.ck_float.disable
		call opt.ck_format.disable
		call opt.ed_format.disable
		call opt.bu_format.disable
	}
END

PROGRAM opt_cpercent_off
BEGIN
	call opt.ed_cpercent.disable
	if ! opt.ck_percent {
		call opt.ck_float.disable
		call opt.ck_format.disable
		call opt.ed_format.disable
		call opt.bu_format.disable
	}
END

PROGRAM command
BEGIN
	put "contract "
	varlist main.vl_var
	INCLUDE _weights_pr
	INCLUDE _ifin_pr
	beginoptions
		optionarg /hidedefault opt.ed_freq
  		if opt.ck_cfreq {
			require opt.ed_cfreq
			optionarg opt.ed_cfreq
		}
  		if opt.ck_percent {
			require opt.ed_percent
			optionarg opt.ed_percent
		}
  		if opt.ck_cpercent {
			require opt.ed_cpercent
			optionarg opt.ed_cpercent
		}
		option opt.ck_float
		optionarg opt.ed_format
		option opt.ck_zero
		option opt.ck_nomiss
	endoptions
END
