/*
  gr_ed_axis.dlg

*!  VERSION 1.0.2  31apr2007

*/

VERSION 10.0
MODAL

INCLUDE _std_mlarge
DEFINE _dlght 430
INCLUDE header_gr_ed

DEFINE _clwd	15
DEFINE _wd	140
DEFINE _sep	+145
DEFINE _cbwd	130
DEFINE _cbsep	+135

SCRIPT PREINIT
BEGIN
	create STRING G
	program setupGOBJECT
	create STRING child__MESSAGE
	
	create STRING axisType
	create STRING tmajor
	create STRING tminor
	create STRING tmp
	create DOUBLE index
END

PROGRAM POSTINIT_PROGRAM
BEGIN
	call major.tb_rule.setlabel			///
		"The axis rule determines the number of ticks and their relative positions."
	call minor.tb_rule.setlabel			///
		"The axis rule determines the number of ticks and their relative positions."
	call major.tb_gprop.setlabel 			///
		"Global properties apply to all noncustom ticks and labels as well as all axis components that are unique. Individual ticks and labels may be customized using the button below."
	call minor.tb_gprop.setlabel 			///
		"Global properties apply to all noncustom ticks and labels as well as all axis components that are unique. Individual ticks and labels may be customized using the button below."

	// dereference dlg_major
	call G.withvalue tmajor.setvalue class @.dlg_major 	// -> major | majornl
	// dereference dlg_minor
	call G.withvalue tminor.setvalue class @.dlg_minor	// -> minor | minornl
	if ! tmajor { // should not happen
		stopbox stop "An unexpected error has occurred"	///
			"(tmajor is not defined)"
	}
	if ! tminor { // should not happen
		stopbox stop "An unexpected error has occurred"	///
			"(tminor is not defined)"
	}
END


PROGRAM setupGOBJECT
BEGIN
	if ! __MESSAGE {
		stopbox stop 	///
			"An unexpected error has occurred." "Object not found."
		exit 198
	}
	call G.setvalue class __MESSAGE.value
END

DIALOG major, title("Axis properties") tabtitle("Major")
BEGIN
  GROUPBOX gb_rule	_lft	_top	_iwd	_ht13,		///
	label("Axis rule")
  RADIO    rb_default	_ilft	_ss	180	.,		///
	first							///
	label("Use existing rule")				///
	onclickon(program check_rule_type)			///
	nomemory
  RADIO    rb_suggest	@	_ss	@	.,		///
	label("Suggest # of ticks")				///
	onclickon(program check_rule_type)			///
	nomemory
  DEFINE y @y
  RADIO    rb_delta	@	_ss	@	.,		///
	label("Range/Delta")					///
	onclickon(program check_rule_type)			///
	nomemory
  RADIO    rb_minmax	@	_ss	@	.,		///
	label("Min Max")					///
	onclickon(program check_rule_type)			///
	nomemory
  RADIO    rb_norule	@	_ss	@	.,		///
	last							///
	label("None")						///
	onclickon(program check_rule_type)			///
	nomemory

  SPINNER  sp_suggest	200	y	_en7wd	.,		///
	min(1) default(5) max(10000)
  TEXT     tx_suggest	_en7sep @	100	.,		///
	label("Ticks")

  EDIT     ed_min	200	y	_en7wd	.,		///
	label("Minimum value") numonly
  TEXT     tx_min	_en7sep @	100	.,		///
	label("Minimum value")
  EDIT     ed_max	200	_ms	_en7wd	.,		///
	label("Maximum value") numonly
  TEXT     tx_max	_en7sep @	100	.,		///
	label("Maximum value")
  EDIT     ed_delta	200	_ms	_en7wd	.,		///
	numonly							///
	label("Delta")
  TEXT     tx_delta	_en7sep @	100	.,		///
	label("Delta")

  TEXTBOX  tb_rule	_ilft	_xls	_ibwd	_tb1,


  GROUPBOX gb_global	_lft	_xls	_iwd	_ht16h,		///
	label("Global properties")
  BUTTON   bu_label	_ilft	_ms	110	.,		///
	label("Label properties")				///
	tooltip("Modify the default rendition of labels")	///
	onpush(script show_label_dialog)
  BUTTON   bu_tick	170	@	110	.,		///
	label("Tick properties")				///
	tooltip("Modify the default rendition of ticks")	///
	onpush(script show_tick_dialog)

  BUTTON   bu_scale	_ilft	_mls	110	.,		///
	label("Scale")						///
	tooltip("Log, reverse, or extend axis scale")		///
	onpush(script show_axis_scale_dialog)
  BUTTON   bu_line	170	@	110	.,		///
	label("Axis line")					///
	tooltip("Modify rendition of axis line") 		///
	onpush(script show_axis_line_dialog)

  BUTTON   bu_grid	_ilft	_mls	110	.,		///
	label("Grid lines")					///
	tooltip("Add and modify grid lines")			///
	onpush(script show_axis_grid_dialog)
  BUTTON   bu_ref	170	@	110	.,		///
	label("Reference line")					///
	onpush(program show_axis_refline_dialog)		///
	tooltip("Add reference line at a value on the axis")

  BUTTON   bu_titles	_ilft	_mls	110	.,		///
	label("Title")						///
	onpush(script show_axis_title_dialog)			///
	tooltip("Modify the text or rendition of the axis title")

  BUTTON   bu_advanced	170	@	110	.,		///
	label("Advanced")					///
	onpush(script show_axis_adv_dialog)			///
	tooltip("Advanced axis properties")

  TEXTBOX  tb_gprop	_ilft	+35	_ibwd	_tb3,

  BUTTON   bu_addticks	_lft	+70	220	.,		///
	label("Edit or add individual ticks and labels")	///
	onpush(script show_axis_major_custom_dialog)		///
	tooltip("Add or customize individual ticks and labels")

END

SCRIPT show_label_dialog
BEGIN
	create CHILD gr_ed_axis_label_major, allowsubmit
	gr_ed_axis_label_major.settitle "Axis label properties (major)"
END

SCRIPT show_tick_dialog
BEGIN
	create CHILD gr_ed_axis_tick_major, allowsubmit
	gr_ed_axis_tick_major.settitle "Axis tick properties (major)"
END

SCRIPT show_axis_scale_dialog
BEGIN
	create CHILD gr_ed_axis_scale, allowsubmit
	gr_ed_axis_scale.settitle "Axis scale properties"
END

SCRIPT show_axis_line_dialog
BEGIN
	create CHILD gr_ed_axis_line, allowsubmit
	gr_ed_axis_line.settitle "Axis line properties"
END

SCRIPT show_axis_grid_dialog
BEGIN
	create CHILD gr_ed_axis_grid_major, allowsubmit
	gr_ed_axis_grid_major.settitle "Axis grid properties (major)"
END

PROGRAM show_axis_refline_dialog
BEGIN
	call child__MESSAGE.setvalue ""

	/* look at axis.position and if it is left or right we have a yaxis 
	 * and if it is top or bottom we have an xaxis ...
	 * 	if neither fall back on the name of the object
	 */
	call G.withvalue index.setvalue class @.position.styledex
	call index.withvalue tmp.setvalue class .__STYLES.relative_posn[@]
	if tmp.contains("left") | tmp.contains("right") {
		call child__MESSAGE.setvalue "yaxis"
		call axisType.setvalue "y"
	}
	else {
		if tmp.contains("above") | tmp.contains("below") {
			call child__MESSAGE.setvalue "xaxis"
			call axisType.setvalue "x"
		}
		else {
			/* did not resolve position */
			if G.contains("xaxis") {
				call child__MESSAGE.setvalue "xaxis"
				call axisType.setvalue "x"
			}
			if G.contains("yaxis") {
				call child__MESSAGE.setvalue "yaxis"
				call axisType.setvalue "y"
			}
		}
	}

	call create STRING refline_result
	call create CHILD gr_ed_axis_refline
	call gr_ed_axis_refline.settitle "Add reference line"
	call gr_ed_axis_refline.setExitAction "program get_refline_result"
	call gr_ed_axis_refline.setExitString refline_result
END
PROGRAM get_refline_result
BEGIN
	if refline_result {
		put "_gm_dlg_edits , "
		put "object(" G ") "
		put "cmd(" 
			put "." G ".plotregion.add_log_refline "
			put axisType " "
			put "plotregion "
			put refline_result
		put ") "
		stata hidden queue
	}
END

SCRIPT show_axis_title_dialog
BEGIN
	child__MESSAGE.setvalue class G.value
	child__MESSAGE.append ".title"
	create CHILD gr_ed_textbox AS axisTitle, allowsubmit
	axisTitle.settitle "Axis title properties"
END

SCRIPT show_axis_adv_dialog
BEGIN
	create CHILD gr_ed_axis_advanced, allowsubmit
	gr_ed_axis_advanced.settitle "Advanced properties"
END

SCRIPT show_axis_major_custom_dialog
BEGIN
	child__MESSAGE.setvalue "__MAJOR__"
	create CHILD gr_ed_axis_custom AS custom_major_dlg, allowsubmit
	custom_major_dlg.settitle "Add or customize individual ticks and labels (major)"
END

PROGRAM check_rule_type
BEGIN
	if major.rb_default {
		call major.sp_suggest.hide
		call major.tx_suggest.hide
		call major.ed_min.hide
		call major.tx_min.hide
		call major.ed_max.hide
		call major.tx_max.hide
		call major.ed_delta.hide
		call major.tx_delta.hide
	}
	if major.rb_suggest {
		call major.sp_suggest.show
		call major.tx_suggest.show

		call major.ed_min.hide
		call major.tx_min.hide
		call major.ed_max.hide
		call major.tx_max.hide
		call major.ed_delta.hide
		call major.tx_delta.hide
	}
	if major.rb_delta {
		call major.sp_suggest.hide
		call major.tx_suggest.hide

		call major.ed_min.show
		call major.tx_min.show
		call major.ed_max.show
		call major.tx_max.show
		call major.ed_delta.show
		call major.tx_delta.show
	}
	if major.rb_minmax {
		call major.sp_suggest.hide
		call major.tx_suggest.hide
		call major.ed_delta.hide
		call major.tx_delta.hide

		call major.ed_min.hide
		call major.tx_min.hide
		call major.ed_max.hide
		call major.tx_max.hide
	}
	if major.rb_norule {
		call major.sp_suggest.hide
		call major.tx_suggest.hide
		call major.ed_min.hide
		call major.tx_min.hide
		call major.ed_max.hide
		call major.tx_max.hide
		call major.ed_delta.hide
		call major.tx_delta.hide
	}
END

PROGRAM major_output
BEGIN
	put "cmd( _gm_edit ." G ".reset_rule "
	if major.rb_suggest {
		put major.sp_suggest
		put ", "
		put "tickset(" tmajor ") "
		put "ruletype(suggest) "
	}
	if major.rb_delta {
		require major.ed_min
		require major.ed_max
		require major.ed_delta
		put major.ed_min " " major.ed_max " " major.ed_delta " "
		put ", "
		put "tickset(" tmajor ") "
		put "ruletype(range) "
	}
	if major.rb_minmax {
		put ", "
		put "tickset(" tmajor ") "
		put "ruletype(minmax) "
	}
	if major.rb_norule {
		put ", "
		put "tickset(" tmajor ") "
		put "ruletype(none) "
	}
	put ") "
END

DIALOG minor, tabtitle("Minor")
BEGIN
  GROUPBOX gb_rule	_lft	_top	_iwd	_ht13,		///
	label("Axis rule")
  RADIO    rb_default	_ilft	_ss	180	.,		///
	first							///
	label("Use existing rule")				///
	onclickon(program check_minorRule_type)			///
	nomemory
  RADIO    rb_suggest	_ilft	_ss	195	.,		///
	label("Suggest # between major ticks")			///
	onclickon(program check_minorRule_type)			///
	nomemory
  DEFINE y @y
  RADIO    rb_delta	@	_ss	@	.,		///
	label("Range/Delta")					///
	onclickon(program check_minorRule_type)			///
	nomemory
  RADIO    rb_minmax	@	_ss	@	.,		///
	label("Min Max")					///
	onclickon(program check_minorRule_type)			///
	nomemory
  RADIO    rb_norule	@	_ss	@	.,		///
	last							///
	label("None")						///
	onclickon(program check_minorRule_type)			///
	nomemory

  SPINNER  sp_suggest	215	y	_en7wd	.,		///
	min(1) default(10) max(10000)
  TEXT     tx_suggest	_en7sep @	95	.,		///
	label("Ticks")

  EDIT     ed_min	215	y	_en7wd	.,		///
	label("Minimum value") numonly
  TEXT     tx_min	_en7sep @	95	.,		///
	label("Minimum value")
  EDIT     ed_max	215	_ms	_en7wd	.,		///
	label("Maximum value") numonly
  TEXT     tx_max	_en7sep @	95	.,		///
	label("Maximum value")
  EDIT     ed_delta	215	_ms	_en7wd	.,		///
	numonly							///
	label("Delta")
  TEXT     tx_delta	_en7sep @	95	.,		///
	label("Delta")

  TEXTBOX  tb_rule	_ilft	_xls	_ibwd	_tb1,


  GROUPBOX gb_global	_lft	_xls	_iwd	_ht11,		///
	label("Global properties")
  BUTTON   bu_label	_ilft	_ms	110	.,		///
	label("Label properties")				///
	tooltip("Modify the default rendition of labels")	///
	onpush(script show_minor_label_dialog)
  BUTTON   bu_tick	170	@	110	.,		///
	label("Tick properties")				///
	tooltip("Modify the default rendition of ticks")	///
	onpush(script show_minor_tick_dialog)

  BUTTON   bu_grid	_ilft	_mls	110	.,		///
	label("Grid lines")					///
	tooltip("Add and modify grid lines")			///
	onpush(script show_minor_grid_dialog)

  TEXTBOX  tb_gprop	_ilft	+35	_ibwd	_tb3,


  BUTTON   bu_addticks	_lft	+70	220	.,		///
	label("Edit or add individual ticks and labels")	///
	onpush(script show_axis_minor_custom_dialog)		///
	tooltip("Add or customize individual ticks and labels")
END

SCRIPT show_minor_label_dialog
BEGIN
	create CHILD gr_ed_axis_label_minor, allowsubmit
	gr_ed_axis_label_minor.settitle "Axis label properties (minor)"
END

SCRIPT show_minor_tick_dialog
BEGIN
	create CHILD gr_ed_axis_tick_minor, allowsubmit
	gr_ed_axis_tick_minor.settitle "Axis tick properties (minor)"
END

SCRIPT show_minor_grid_dialog
BEGIN
	create CHILD gr_ed_axis_grid_minor, allowsubmit
	gr_ed_axis_grid_minor.settitle "Axis grid properties (minor)"
END

SCRIPT show_axis_minor_custom_dialog
BEGIN
	child__MESSAGE.setvalue "__MINOR__"
	create CHILD gr_ed_axis_custom AS custom_minor_dlg, allowsubmit
	custom_minor_dlg.settitle "Add or customize individual ticks and labels (minor)"
END

PROGRAM check_minorRule_type
BEGIN
	if minor.rb_default {
		call minor.sp_suggest.hide
		call minor.tx_suggest.hide
		call minor.ed_min.hide
		call minor.tx_min.hide
		call minor.ed_max.hide
		call minor.tx_max.hide
		call minor.ed_delta.hide
		call minor.tx_delta.hide
	}
	if minor.rb_suggest {
		call minor.sp_suggest.show
		call minor.tx_suggest.show

		call minor.ed_min.hide
		call minor.tx_min.hide
		call minor.ed_max.hide
		call minor.tx_max.hide
		call minor.ed_delta.hide
		call minor.tx_delta.hide
	}
	if minor.rb_delta {
		call minor.sp_suggest.hide
		call minor.tx_suggest.hide

		call minor.ed_min.show
		call minor.tx_min.show
		call minor.ed_max.show
		call minor.tx_max.show
		call minor.ed_delta.show
		call minor.tx_delta.show
	}
	if minor.rb_minmax {
		call minor.sp_suggest.hide
		call minor.tx_suggest.hide
		call minor.ed_delta.hide
		call minor.tx_delta.hide

		call minor.ed_min.hide
		call minor.tx_min.hide
		call minor.ed_max.hide
		call minor.tx_max.hide
	}
	if minor.rb_norule {
		call minor.sp_suggest.hide
		call minor.tx_suggest.hide
		call minor.ed_min.hide
		call minor.tx_min.hide
		call minor.ed_max.hide
		call minor.tx_max.hide
		call minor.ed_delta.hide
		call minor.tx_delta.hide
	}
END

PROGRAM minor_output
BEGIN
	put "cmd( _gm_edit ." G ".reset_rule "
	if minor.rb_suggest {
		put minor.sp_suggest
		put ", "
		put "tickset(" tminor ") "
		put "ruletype(suggest_between) "
	}
	if minor.rb_delta {
		require minor.ed_min
		require minor.ed_max
		require minor.ed_delta
		put minor.ed_min " " minor.ed_max " " minor.ed_delta " "
		put ", "
		put "tickset(" tminor ") "
		put "ruletype(range) "
	}
	if minor.rb_minmax {
		put ", "
		put "tickset(" tminor ") "
		put "ruletype(minmax) "
	}
	if minor.rb_norule {
		put ", "
		put "tickset(" tminor ") "
		put "ruletype(none) "
	}
	put ") "
END

PROGRAM command
BEGIN
	if ! major.rb_default | ! minor.rb_default {
		put "_gm_dlg_edits"
		beginoptions
			put " object(" G ") "
			if ! major.rb_default {
				put " " /program major_output
			}
			if ! minor.rb_default {
				put " " /program minor_output
			}
			put " epilog(_gedi toolbar axis " G ")"
		endoptions
	}
END
