/*
	dstdize

*!  VERSION 1.2.0  06feb2007

*/

VERSION 10.0

INCLUDE _std_large
INCLUDE _ht310
INCLUDE header
HELP hlp1, view("help dstdize")
RESET res1

DIALOG main, label("dstdize - Direct standardization") tabtitle("Main")
BEGIN
  TEXT     tx_char   _lft      _top      _cwd1     .,		/*
		*/ label("Characteristic variable:")

  VARNAME  vn_char   @         _ss       _vnwd     .,		/*
		*/ label("Characteristic variable")

  TEXT     tx_pop    _lft2     _top      _cwd1     .,		/*
		*/ label("Population variable:")

  VARNAME  vn_pop    @         _ss       _vnwd     .,		/*
		*/ label("Population variable")

  TEXT     tx_strata _lft      _ls       _cwd1     .,		/*
		*/ label("Strata variables:")

  DEFINE   holdy @y

  VARLIST  vl_strata @         _ss       @         .,		/*
		*/ label("Strata variables")

  TEXT     tx_byvars _lft2     holdy     @         .,		/*
		*/ label("Grouping variables:")

  VARLIST  vl_byvars @         _ss       @         .,		/*
		*/ option(by)					/*
		*/ label("Grouping variables")

  RADIO    rb_def    _lft      _xls      _iwd      .,		/*
		*/ first					/*
		*/ clickon("script default_on")			/*
		*/ label("Use standard population from data in memory")

  RADIO    rb_using  @         _ms       @         .,		/*
		*/ clickon("script using_on")			/*
		*/ label("Use standard population from Stata dataset:")

  FILE     fi_using  _indent2  _ss       _ibwd     .,		/*
		*/ option(using)				/*
		*/ error("Use standard population from Stata dataset")/*
		*/ filter("Stata Dataset (*.dta)|*.dta|All Files (*.*)|*.*")	/*
		*/ defext(dta)						/*
		*/ label("Browse...")

  RADIO    rb_base   _lft      _ls       _iwd      .,		/*
		*/ last 					/*
		*/ clickon("script base_on")			/*
	*/ label("Use standard population from a value of grouping variable:")

  TEXT     tx_base   _indent2  _ss       _ibwd     .,		/*
		*/ label("Value:")

  DEFINE holdy @y

  EDIT     ed_base   @         _ss       _vnwd     .,		/*
		*/ option(base)					/*
		*/ label("Value")

  TEXT     tx_byvar  _lft2     holdy     _cwd2     .,		/*
		*/ label("Grouping variable:")

  VARNAME  vn_byvar  @         _ss       _vnwd     .,		/*
		*/ option(by)					/*
		*/ label("Grouping variable")

  DEFINE _x _lft
  DEFINE _cx _spr
  DEFINE _y _xls
  INCLUDE _sp_level
END

SCRIPT base_on
BEGIN
main.fi_using.disable
main.ed_base.enable
main.vn_byvar.enable
main.vl_byvars.disable
END

SCRIPT using_on
BEGIN
main.fi_using.enable
main.ed_base.disable
main.vn_byvar.disable
main.vl_byvars.enable
END

SCRIPT default_on
BEGIN
main.fi_using.disable
main.ed_base.disable
main.vn_byvar.disable
main.vl_byvars.enable
END

INCLUDE ifin


DIALOG opt, tabtitle("Options")
BEGIN
  TEXT     tx_saving _lft      _top      _iwd      .,		/*
		*/ label("Save computed standard population distribution as a Stata dataset:")

  FILE     fi_saving @         _ss       @         .,		/*
		*/ option(saving)				/*
		*/ label("Save As...")				/*
		*/ filter("Stata Dataset (*.dta)|*.dta|All Files (*.*)|*.*")	/*
		*/ defext(dta)						/*
		*/ save

  CHECKBOX ck_format    @	_ls	@	.,		///
	onclickon(script format_on)				///
	onclickoff(script format_off)				///
	label("Overide display format for numbers in final summary table")
  EDIT     ed_format	+20	_ss	200	.,		///
	option(format)						///
	label("Display format")
  BUTTON   bu_format	+205	@	80	.,		///
	label("Create...")					///
	onpush(script showFormatDialog)				///
	tooltip("Create display format")

  CHECKBOX ck_print  _lft      _ls       _iwd      .,		/*
		*/ option(print)				/*
		*/ label("Include table summary of standard population in output")

  GROUPBOX gb_adv    @         _xls      @         _ht3,	/*
  		*/ label("Advanced")
  CHECKBOX ck_nores  _ilft     _ms       _inwd     .,		/*
		*/ option(nores)				/*
		*/ label("Suppress saving results in r()")
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

SCRIPT format_on
BEGIN
  	opt.ed_format.enable
  	opt.bu_format.enable
END

SCRIPT format_off
BEGIN
  	opt.ed_format.disable
	opt.bu_format.disable
END


PROGRAM command
BEGIN
	put "dstdize "
	varlist main.vn_char
	varlist main.vn_pop
	varlist main.vl_strata
	INCLUDE _ifin_pr
	beginoptions
		if main.rb_def {
			require main.vl_byvars
			optionarg main.vl_byvars
		}
		if main.rb_using {
			require main.vl_byvars
			require main.fi_using
			optionarg main.vl_byvars
			optionarg /quoted main.fi_using
		}
		if main.rb_base {
			require main.ed_base
			require main.vn_byvar
			optionarg main.ed_base
			optionarg main.vn_byvar
		}
		if opt.fi_saving {
			repfile opt.fi_saving
			put " " "saving("
			put `"""'
			put opt.fi_saving
			put `"""'
			put ", replace)"
		}
		option opt.ck_print
		optionarg opt.ed_format
		INCLUDE _level_main_pr
		option opt.ck_nores
	endoptions
END
