/*
  gr_ed_axis_grid.idlg

*!  VERSION 1.0.1  08jun2007

*/

INCLUDE _std_small
DEFINE _dlght 245
INCLUDE header_gr_ed

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

SCRIPT local_init
BEGIN
	create STRING G
	create STRING parent_name

	// Get the Graphics object name (G) from the parent...
	parent_name.setvalue class PARENT._dlg_rsrcname
	parent_name.withvalue G.setvalue class @.G.value

	create STRING tstyle
	create STRING Gstyle
	program setStyleType

	create DOUBLE hasGrid
	create STRING color
	create STRING rgb
	create STRING width
	create STRING pattern
	create DOUBLE extendlow
	create DOUBLE extendhigh

	create DOUBLE index
	create STRING tmp

	program get_defaults
END

SCRIPT POSTINIT
BEGIN
	program initialize_controls
END

PROGRAM setStyleType
BEGIN
	if styleType.iseq("major") {
		// dereference dlg_major and setup object path
		call Gstyle.setvalue class G.value			// initialize to G
		call G.withvalue tstyle.setvalue class @.dlg_major 	// -> tstyle | majornl
		call tstyle.append "style"				// -> majorstyle | majornlstyle
		call tstyle.withvalue Gstyle.append ".style.@"		// -> .Graph.xaxis1.style.majorstyle
	}
	if styleType.iseq("minor") {
		// dereference dlg_minor and setup object path
		call Gstyle.setvalue class G.value			// initialize to G
		call G.withvalue tstyle.setvalue class @.dlg_minor 	// -> minor | minornl
		call tstyle.append "style"				// -> minorstyle | minornlstyle
		call tstyle.withvalue Gstyle.append ".style.@"		// -> .Graph.xaxis1.style.minorstyle
	}
END

DIALOG main, title("Grid properties") tabtitle("Main")
BEGIN
  CHECKBOX ck_show	_lft	20	_iwd	_ht18,		///
	label("Show grid")					///
	groupbox						///
	onclickon(program check_show)				///
	onclickoff(program check_show)
  TEXT     tx_clr	_ilft	_mls	_wd	.,		///
	label("Color:") right
  COMBOBOX cb_clr	_sep	@	_cbwd	.,		///
	dropdownlist contents(_colors) 				///
	onselchangelist(main_cl_clr_sel)
  COLOR    cl_clr	_cbsep	@	_clwd	.,

  TEXT     tx_width	_ilft	_mls	_wd	.,		///
	label("Width:") right
  COMBOBOX cb_width	_sep	@	_cbwd	.,		///
	dropdown 						///
	contents(_linewidths)

  TEXT     tx_pattern	_ilft	_mls	_wd	.,		///
	label("Pattern:") right
  COMBOBOX cb_pattern	_sep	@	_cbwd	.,		///
	dropdownlist						///
	contents(_linepatterns)

  TEXT     tx_showmin	_ilft	_mls	_wd	.,		///
	label("Grid lines at minimum:") right
  COMBOBOX cb_showmin	_sep	@	_cbwd	.,		///
	dropdownlist nomemory					///
	contents(grid_min_contents) values(grid_min_values)

  TEXT     tx_showmax	_ilft	_mls	_wd	.,		///
	label("Grid lines at maximum:") right
  COMBOBOX cb_showmax	_sep	@	_cbwd	.,		///
	dropdownlist nomemory					///
	contents(grid_max_contents) values(grid_max_values)

  CHECKBOX ck_extend	_ilft	+35	_iibwd	.,		///
	label("Extend grid lines into plot region margin")
END

LIST grid_min_contents
BEGIN
	No change
	Show
	Hide
END
LIST grid_min_values
BEGIN
	""
	show
	hide
END

LIST grid_max_contents
BEGIN
	No change
	Show
	Hide
END
LIST grid_max_values
BEGIN
	""
	show
	hide
END



PROGRAM check_show
BEGIN
	if main.ck_show {
		call main.tx_clr.enable
		call main.cb_clr.enable
		call main.cl_clr.enable

		call main.tx_width.enable
		call main.cb_width.enable

		call main.tx_pattern.enable
		call main.cb_pattern.enable

		call main.cb_showmin.enable
		call main.tx_showmin.enable
		call main.cb_showmax.enable
		call main.tx_showmax.enable
		call main.ck_extend.enable
	}
	else {
		call main.tx_clr.disable
		call main.cb_clr.disable
		call main.cl_clr.disable

		call main.tx_width.disable
		call main.cb_width.disable

		call main.tx_pattern.disable
		call main.cb_pattern.disable

		call main.cb_showmin.disable
		call main.tx_showmin.disable
		call main.cb_showmax.disable
		call main.tx_showmax.disable
		call main.ck_extend.disable
	}
END

LIST main_cl_clr_sel
BEGIN
	main.cl_clr.show
	main.cl_clr.hide
END

PROGRAM get_defaults
BEGIN
	call tmp.setvalue class G.value
	call tmp.append ".style.draw_"
	call tmp.append class styleType.value
	call tmp.append "_grid"
	call tmp.withvalue hasGrid.setvalue class @.istrue
	call hasGrid.withvalue main.ck_show.setdefault @

	call Gstyle.withvalue index.setvalue class @.gridstyle.linestyle.color.styledex
	call index.withvalue color.setvalue class .__STYLES.color[@]
	call main.cb_clr.setdefault class color.value
	
	call Gstyle.withvalue rgb.setvalue class @.gridstyle.linestyle.color.rgb
	call main.cl_clr.setdefault class rgb.value

	call Gstyle.withvalue index.setvalue class @.gridstyle.linestyle.width.styledex
	call index.withvalue width.setvalue class .__STYLES.linewidth[@]
	if ! width {
		call Gstyle.withvalue width.setvalue class @.gridstyle.linestyle.width.val
	}
	call main.cb_width.setdefault class width.value

	call Gstyle.withvalue index.setvalue class @.gridstyle.linestyle.pattern.styledex
	call index.withvalue pattern.setvalue class .__STYLES.linepattern[@]
	call main.cb_pattern.setdefault class pattern.value

	call Gstyle.withvalue extendlow.setvalue class @.gridstyle.extend_low.istrue
	call Gstyle.withvalue extendhigh.setvalue class @.gridstyle.extend_high.istrue
	if extendlow | extendhigh {
		call main.ck_extend.setdefault 1
	}
	else {
		call main.ck_extend.setdefault 0
	}
END

PROGRAM initialize_controls
BEGIN
	if hasGrid {
		call main.ck_show.seton
	}
	else {
		call main.ck_show.setoff
	}

	if color {
		call main.cb_clr.setvalue class color.value
	}
	if ! color {
		call main.cb_clr.setvalue "custom"
	}
	call main.cl_clr.setvalue class rgb.value

	call main.cb_width.setvalue class width.value
	call main.cb_pattern.setvalue class pattern.value

	if extendlow | extendhigh {
		call main.ck_extend.seton
	}
	else {
		call main.ck_extend.setoff
	}
END

PROGRAM command
BEGIN
	call program get_defaults
	put "_gm_dlg_edits"
	beginoptions
		put " object(" G ") "

		if ! main.ck_show.isdefault() {
			if main.ck_show {
				put "style(draw_" styleType "_grid(" 
				put "yes"
				put ")) "
			}
			else {
				put "style(draw_" styleType "_grid(" 
				put "no"
				put ")) "
			}
		}
		if main.ck_show {
			if ! main.cl_clr.isvisible() & ! main.cb_clr.isdefault() {
				put "style(" tstyle "(gridstyle(linestyle(color(" 
				put main.cb_clr 
				put "))))) "
			}
			if main.cl_clr.isvisible() & ! main.cl_clr.isdefault() {
				put "style(" tstyle "(gridstyle(linestyle(color("   `"""'
				put main.cl_clr
				put `"""'
				put "))))) "
			}
			if ! main.cb_width.isdefault() {
				put "style(" tstyle "(gridstyle(linestyle(width("
				put main.cb_width
				put "))))) "
			}
			if ! main.cb_pattern.isdefault() {
				put "style(" tstyle "(gridstyle(linestyle(pattern("
				put main.cb_pattern
				put "))))) "
			}
			if main.cb_showmin {
				if main.cb_showmin.iseq("show") {
					put "style(" tstyle "(gridstyle(force_nomin("
					put "no"
					put ")))) "
					put "style(" tstyle "(gridstyle(draw_min("
					put "yes"
					put ")))) "
				}
				else {
					put "style(" tstyle "(gridstyle(force_nomin("
					put "yes"
					put ")))) "
					put "style(" tstyle "(gridstyle(draw_min("
					put "no"
					put ")))) "
				}
			}
			if main.cb_showmax {
				if main.cb_showmax.iseq("show") {
					put "style(" tstyle "(gridstyle(force_nomax("
					put "no"
					put ")))) "
					put "style(" tstyle "(gridstyle(draw_max("
					put "yes"
					put ")))) "
				}
				else {
					put "style(" tstyle "(gridstyle(force_nomax("
					put "yes"
					put ")))) "
					put "style(" tstyle "(gridstyle(draw_max("
					put "no"
					put ")))) "
				}
			}
			if ! main.ck_extend.isdefault() {
				if main.ck_extend {
					put "style(" tstyle "(gridstyle(extend_low("
					put "yes"
					put ")))) "
					put "style(" tstyle "(gridstyle(extend_high("
					put "yes"
					put ")))) "
				}
				else {
					put "style(" tstyle "(gridstyle(extend_low("
					put "no"
					put ")))) "
					put "style(" tstyle "(gridstyle(extend_high("
					put "no"
					put ")))) "
				}
			}
		}
		put " epilog(_gedi toolbar axis " G ")"
	endoptions
	stata hidden queue
END
