/*
  gr_ed_bygraph.dlg

*!  VERSION 1.0.3  07may2007

*/

VERSION 10.0
MODAL

INCLUDE _std_medium
DEFINE _dlght 260
INCLUDE header_gr_ed

DEFINE _clwd	15
DEFINE _wd	130
DEFINE _sep	+135
DEFINE _wd2	120
DEFINE _sep2	+125
DEFINE _cbwd	120
DEFINE _cbsep	+125

DEFINE _cbwd2	75
DEFINE _cbsep2	+80

SCRIPT PREINIT
BEGIN
	settitle "Graph properties"
	create STRING G
	create STRING GRAPH
	create ARRAY  ARGS
	create STRING MODE
	create BOOLEAN isPieGraph
	program setupGOBJECT

	create DOUBLE index
	create STRING tmp
	create ARRAY tmpArray

// size
	create STRING aspect
	create STRING aspect_pos
	create STRING xsize
	create STRING ysize
	create STRING scale
	
// advanced
	create DOUBLE visible
	create DOUBLE filled
	create STRING xoffset
	create STRING yoffset

	program get_defaults
END

SCRIPT POSTINIT
BEGIN
	program initialize_controls
	program check_init_tab
END

PROGRAM setupGOBJECT
BEGIN
	if ! __MESSAGE {
		stopbox stop 	///
			"An unexpected error has occurred." "Object not found."
		exit 198
	}
	call __MESSAGE.tokenize ARGS
	call G.setvalue class ARGS[1]
	call MODE.setvalue class ARGS[2]
	call GRAPH.setvalue class ._Gr_Global.edit_graph

	/* deal with pie graphs */
	call G.withvalue isPieGraph.storeClsObjectExists @.graphs[1].piestyles
	if isPieGraph {
		call size.gb_aspect.hide
		call size.tx_aspect.hide
		call size.ed_aspect.hide
		call size.tx_pos.hide
		call size.cb_pos.hide

		call size.settabtitle "Size"
		call size.gb_size.setposition . 10
		call size.tx_ysize.setposition . 30
		call size.ed_ysize.setposition . 30
		call size.tx_xsize.setposition . 57
		call size.ed_xsize.setposition . 57

		call size.gb_scale.setposition . 102
		call size.tx_scale.setposition . 122
		call size.ed_scale.setposition . 122
	}
	else {
		call size.gb_aspect.show
		call size.tx_aspect.show
		call size.ed_aspect.show
		call size.tx_pos.show
		call size.cb_pos.show

		call size.gb_size.setposition . 102
		call size.tx_ysize.setposition . 122
		call size.ed_ysize.setposition . 122
		call size.tx_xsize.setposition . 149
		call size.ed_xsize.setposition . 149

		call size.gb_scale.setposition . 194
		call size.tx_scale.setposition . 214
		call size.ed_scale.setposition . 214
		call size.settabtitle "Aspect/Size"
	}
END

PROGRAM check_init_tab
BEGIN
	if MODE.contains("MODE(") {
		if MODE.contains("region") {
			call overall.setactive
		}
		if MODE.contains("aspect") {
			call size.setactive
			call size.ed_aspect.setfocus
		}
		if MODE.contains("size") {
			call size.setactive
			call size.ed_ysize.setfocus
		}
		if MODE.contains("title") {
			call titles.setactive
		}
		if MODE.contains("organization") {
			call org.setactive
		}
	}
END

INCLUDE gr_ed_regions

DIALOG size, tabtitle("Aspect/Size")
BEGIN
  GROUPBOX gb_aspect	_lft	_top	_iwd	_ht5,		///
	label("Aspect ratio")
  TEXT     tx_aspect	_ilft	_ss	_wd	.,		///
	label("Aspect ratio:") right
  EDIT     ed_aspect	_sep	@	_cbwd	.,		///
	numonly							///
	tooltip("Aspect ratio for plot regions")
  TEXT     tx_pos	_ilft	_mls	_wd	.,		///
	label("Position:") right
  COMBOBOX cb_pos	_sep	@	_cbwd	.,		///
	dropdownlist contents(_compass)

  GROUPBOX gb_size	_lft	+45	_iwd	_ht5,		///
	label("Size")
  TEXT     tx_ysize	_ilft	_ss	_wd	.,		///
	label("Y size (inches):") right
  EDIT     ed_ysize	_sep	@	_cbwd	.,		///
	label("Y size")
  TEXT     tx_xsize	_ilft	_mls	_wd	.,		///
	label("X size (inches):") right
  EDIT     ed_xsize	_sep	@	_cbwd	.,		///
	label("X size")

  GROUPBOX gb_scale	_lft	+45	_iwd	_ht2,		///
	label("Scale text, markers, and line widths")
  TEXT     tx_scale	_ilft	_ss	_wd	.,		///
	label("Scaling factor:") right
  EDIT     ed_scale	_sep	@	_cbwd	.,		///
	label("Scaling factor")					///
	tooltip("Scaling factor for sizing text, markers, and line widths")
END

INCLUDE gr_ed__titles

SCRIPT org_PREINIT
BEGIN
	create STRING layout
	create DOUBLE rows
	create DOUBLE cols
	create DOUBLE colFirst
	create STRING holes

	create STRING by_margin
	create STRING customByMargin
	create STRING customByMarginDefault

	program org_get_defaults
END

SCRIPT org_POSTINIT
BEGIN
	program org_initialize_controls
	program org_checkCustomMargin
END

PROGRAM org_get_defaults
BEGIN
	call G.withvalue rows.setvalue class @.rows
	call G.withvalue cols.setvalue class @.cols
	if cols.isgt(0) {
		call org.cb_layout.setdefault "Columns"
		call org.sp_rowcol.setdefault class cols.value
	}
	else {
		call org.cb_layout.setdefault "Rows"
		call org.sp_rowcol.setdefault class rows.value
	}

	call G.withvalue colFirst.setvalue class @.set_colfirst
	if colFirst {
		call org.cb_colfirst.setdefault "1"
	}
	else {
		call org.cb_colfirst.setdefault "0"
	}

	call G.withvalue index.setvalue class @.style.indiv_margin.styledex
	call index.withvalue by_margin.setvalue class .__STYLES.margin[@]
	call customByMarginDefault.setvalue ""
	if ! by_margin {
		call G.withvalue by_margin.setvalue class @.style.indiv_margin.left
		call by_margin.append " "
		call G.withvalue by_margin.append class @.style.indiv_margin.right
		call by_margin.append " "
		call G.withvalue by_margin.append class @.style.indiv_margin.bottom
		call by_margin.append " "
		call G.withvalue by_margin.append class @.style.indiv_margin.top
		call customByMarginDefault.setvalue class by_margin.value
	}
	call by_margin.withvalue org.cb_margin.setdefault "@"

	call G.withvalue holes.setvalue class @.holes
	call org.ed_holes.setdefault class holes.value
END

PROGRAM org_initialize_controls
BEGIN
	if cols.isgt(0) {
		call org.cb_layout.setvalue "Columns"
		call org.sp_rowcol.setvalue class cols.value
	}
	else {
		call org.cb_layout.setvalue "Rows"
		call org.sp_rowcol.setvalue class rows.value
	}

	if colFirst {
		call org.cb_colfirst.setvalue "1"
	}
	else {
		call org.cb_colfirst.setvalue "0"
	}

	call org.cb_margin.setvalue class by_margin.value

	call org.ed_holes.setvalue class holes.value
END

DIALOG org, tabtitle("Organization")
BEGIN
  GROUPBOX	gb_organization	_lft	_top	_iwd	_ht17,	///
	label("Organization")

  TEXT		tx_layout	_ilft   _ss	_wd	.,	///
	label("Rows/Columns:") right
  COMBOBOX	cb_layout	_sep	@	_cbwd2	.,	///
	dropdownlist 						///
	contents(rows_columns)					///
	onselchangelist(rows_columns_sel)
  SPINNER	sp_rowcol	_cbsep2	@	40	.,	///
	default(1) min(1)
  TEXT		tx_rowcol	+45	@	80	.,	///
	label("Rows")

  TEXT		tx_colfirst	_ilft	_mls	_wd	.,	///
	label("Graph sequence:") right
  COMBOBOX	cb_colfirst	_sep	@	_cbwd	.,	///
	dropdownlist 						///
	contents(gr_sequence) 	values(gr_sequence_val)

  TEXT     tx_margin	_ilft	_mls	_wd	.,		///
	label("Graph margins:") right	
  COMBOBOX cb_margin	_sep	@	_cbwd	.,		///
	dropdownlist contents(_margin)				///
	onselchangelist(org_cb_margin_sel)
  BUTTON   bu_margin	_cbsep	@	_clwd	.,		///
	label("..") tooltip("Custom margin")			///
	onpush(script org_show_margin_dialog)

  TEXT	   tx_holes	_ilft	_mls	_wd	.,		///
	label("Positions to leave blank:") right
  EDIT	ed_holes		_sep	@	_cbwd	.,	///
	tooltip("Cells to skip when placing graphs")

  TEXTBOX tb_note	_ilft	_xls	_ibwd	_tb3,		///
	label(Changes made on this tab rearrange the 		///
		graphs and their components; changes may 	///
		override previous edits and affect whether 	///
		these objects are shown and their positions.)
END

LIST gr_sequence
BEGIN
	"Across first"
	"Down first"
END
LIST gr_sequence_val
BEGIN
	"0"
	"1"
END

LIST rows_columns
BEGIN
	"Rows"
	"Columns"
END
LIST rows_columns_sel
BEGIN
	script row_value_show
	script col_value_show
END
SCRIPT row_col_value_hide
BEGIN
	org.sp_rowcol.hide
	org.tx_rowcol.hide
END
SCRIPT row_value_show
BEGIN
	org.sp_rowcol.show
	org.tx_rowcol.show
	org.tx_rowcol.setlabel "Rows"
END
SCRIPT col_value_show
BEGIN
	org.sp_rowcol.show
	org.tx_rowcol.show
	org.tx_rowcol.setlabel "Columns"
END

LIST org_cb_margin_sel
BEGIN
	org.bu_margin.show
	org.bu_margin.hide
END

SCRIPT org_show_margin_dialog
BEGIN
	create CHILD gr_margin_create AS org_margin_dlg
	org_margin_dlg.setExitString customByMargin
	org_margin_dlg.setExitAction "program org_checkCustomMargin"
END	
PROGRAM org_checkCustomMargin
BEGIN
	if customByMargin {
		call customByMargin.tokenize tmpArray
		call tmp.setvalue "Custom margin "
		call tmp.append "(Left:"
		call tmp.append class tmpArray[1]
		call tmp.append " Right:"
		call tmp.append class tmpArray[2]
		call tmp.append " Bottom:"
		call tmp.append class tmpArray[3]
		call tmp.append " Top:"
		call tmp.append class tmpArray[4]
		call tmp.append ")"
		call tmp.withvalue org.bu_margin.settooltip "@"
	}
	else {
		call org.bu_margin.settooltip "Custom margin (not defined)"
	}
END


PROGRAM org_output
BEGIN
	call program org_get_defaults
	
	if ! org.cb_layout.isdefault() | ! org.sp_rowcol.isdefault() {
		if org.cb_layout.iseq("Columns") {
			put "cmd(.set_cols = "
			put org.sp_rowcol
			put ") "
			put "cmd(.set_rows = 0) "
		}
		else {
			put "cmd(.set_rows = "
			put org.sp_rowcol
			put ") "
			put "cmd(.set_cols =0 ) "
		}
		call hasOrgOutput.settrue
	}

	if ! org.cb_colfirst.isdefault() {
		put "cmd(.set_colfirst = "
		put org.cb_colfirst
		put ") "
		call hasOrgOutput.settrue
	}

	if org.cb_margin.iseq("custom") {
		if customByMargin & customByMargin.isneq(customByMarginDefault) {
			put "style(indiv_margin("
			put customByMargin
			put ")) "
			call hasOrgOutput.settrue
		}
	}
	else {
		if ! org.cb_margin.isdefault() {
			put " style(indiv_margin(" org.cb_margin ")) "
			call hasOrgOutput.settrue
		}
	}

	if ! org.ed_holes.isdefault() {
		put "cmd(.holes = " `"""'
		put org.ed_holes
		put `"""' ") "
		call hasOrgOutput.settrue
	}
END

DIALOG adv, tabtitle("Advanced")
BEGIN
  CHECKBOX ck_hidden	_ilft	_top	_ibwd	.,		///
	label("Hide graph")					///
	onclickon(adv.ck_collapse.enable)			///
	onclickoff(adv.ck_collapse.disable)
  CHECKBOX ck_collapse	@	_ms	@	.,		///
	label("Collapse when hidden")

  GROUPBOX gb_offset	_lft	_ls	_iwd	_ht5,		///
	label("Position offset")
  TEXT     tx_xoffset	_ilft	_ss	_wd	.,		///
	label("X offset:") right
  EDIT	   ed_xoffset	_sep	@	_cbwd	.,

  TEXT     tx_yoffset	_ilft	_mls	_wd	.,		///
	label("Y offset:") right
  EDIT	   ed_yoffset	_sep	@	_cbwd	.,
END

PROGRAM get_defaults
BEGIN
// size
	call G.withvalue aspect.setvalue class @.graphs[1].style.aspect_ratio.val
	call size.ed_aspect.setdefault class aspect.value

	call G.withvalue index.setvalue class @.graphs[1].style.aspect_pos.styledex
	call index.withvalue aspect_pos.setvalue class .__STYLES.compass2dir[@]
	call size.cb_pos.setdefault class aspect_pos.value

	call G.withvalue ysize.setvalue class @.style.declared_ysize.val
	call size.ed_ysize.setdefault class ysize.value
	
	call G.withvalue xsize.setvalue class @.style.declared_xsize.val
	call size.ed_xsize.setdefault class xsize.value
	
	call G.withvalue scale.setvalue class @.gmetric_mult
	call size.ed_scale.setdefault class scale.value

// advanced
	call G.withvalue visible.setvalue class @.draw_view.istrue
	if visible {
		call adv.ck_hidden.setdefault 0
	}
	else {
		call adv.ck_hidden.setdefault 1
	}

	call G.withvalue filled.setvalue class @.fill_if_undrawn.istrue
	if filled {
		call adv.ck_collapse.setdefault 0
	}
	else {
		call adv.ck_collapse.setdefault 1
	}

	call G.withvalue xoffset.setvalue class @.xoffset
	call adv.ed_xoffset.setdefault class xoffset.value

	call G.withvalue yoffset.setvalue class @.yoffset
	call adv.ed_yoffset.setdefault class yoffset.value
END

PROGRAM initialize_controls
BEGIN
// size
	call size.ed_aspect.setvalue class aspect.value
	call size.cb_pos.setvalue class aspect_pos.value
	call size.ed_ysize.setvalue class ysize.value
	call size.ed_xsize.setvalue class xsize.value
	call size.ed_scale.setvalue class scale.value

// advanced
	if visible {
		call adv.ck_hidden.setoff
	}
	else {
		call adv.ck_hidden.seton
	}

	if filled {
		call adv.ck_collapse.setoff
	}
	else {
		call adv.ck_collapse.seton
	}
	call xoffset.withvalue adv.ed_xoffset.setvalue "@"
	call yoffset.withvalue adv.ed_yoffset.setvalue "@"
END

PROGRAM command
BEGIN
	put "." GRAPH ".UndoBegin"
	stata hidden queue

	call program get_defaults
	
	put "_gm_dlg_edits"
	beginoptions
		put " object(" G ") "
		put " " /program region_output
		call program titles_output	// has own output routine

// size
		if ! size.ed_aspect.isdefault() {
			put " cmd(_gm_edit "
			put "." G ".SetAspectRatio "
			put size.ed_aspect
			put ") "
		}
		if ! size.cb_pos.isdefault() {
			put " style(aspect_pos("
			put size.cb_pos
			put ")) "
		}
		if ! size.ed_ysize.isdefault() {
			put " style(declared_ysize("
			put size.ed_ysize
			put ")) "
		}
		if ! size.ed_xsize.isdefault() {
			put " style(declared_xsize("
			put size.ed_xsize
			put ")) "
		}
		if ! size.ed_scale.isdefault() {
			call create DOUBLE scaleValue 
			call scaleValue.setvalue class size.ed_scale.value
			if scaleValue.isle(0) | scaleValue.isge(100) {
				stopbox stop `""Scaling factor" out of range."' ///
					"Value must be greater than 0 and less than 100."
			}
			put " set(gmetric_mult " size.ed_scale ") "
		}

// organization
		call create BOOLEAN hasOrgOutput
		call hasOrgOutput.setfalse
		call program org_output
		if (hasOrgOutput) {
			put " cmd(_gm_edit ." G ".Edit , "
			put " " /program org_output
			put " ) "
		}

// advanced
		if ! adv.ck_hidden.isdefault() {
			if adv.ck_hidden {
				put " set(draw_view no) " 
			}
			else {
				put " set(draw_view yes) " 
			}
		}
		if ! adv.ck_collapse.isdefault() {
			if adv.ck_collapse {
				put " set(fill_if_undrawn no) " 
			}
			else {
				put " set(fill_if_undrawn yes) " 
			}
		}
		if ! adv.ed_xoffset.isdefault() {
			put "set(xoffset " adv.ed_xoffset ") "
		}
		if ! adv.ed_yoffset.isdefault() {
			put "set(yoffset " adv.ed_yoffset ") "
		}
		put " epilog(_gedi toolbar bygraph " G ")"
	endoptions
	stata hidden queue

	put "." GRAPH ".UndoEnd " G
	stata hidden queue
END
