/*
  gr_ed_barregion.dlg

*!  VERSION 1.0.0  30jan2007

*/

VERSION 10.0
MODAL

INCLUDE _std_medium
DEFINE _dlght 420
INCLUDE header_gr_ed

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

DEFINE GBWD	_iwd

SCRIPT PREINIT
BEGIN
	settitle "Bar region properties"
	create STRING G
	create STRING GRAPH
	create STRING MODE
	create ARRAY  ARGS
	create STRING barKey
	program setupGOBJECT

	create DOUBLE index
	create STRING tmp
	create STRING child__MESSAGE
	create STRING unused

// organization
	create DOUBLE horizontal
	create DOUBLE percentages
	create DOUBLE include0
	create DOUBLE stackBars
	create DOUBLE missing
	create DOUBLE reverse
	create STRING varGap
	create STRING groupGap
	create STRING sgroupGap
	create STRING outerGap

// labels
	create STRING labelMethod
	create STRING labelPosition
	create STRING labelGap
	create STRING labelFormat
	create STRING labelFormatResult
	
// advanced
	create DOUBLE visible
	create DOUBLE filled

	program get_defaults
END

SCRIPT POSTINIT
BEGIN
	program initialize_controls
	program check_init_tab
END

PROGRAM check_init_tab
BEGIN
	if MODE.contains("MODE(") {
		if MODE.contains("refline") {
			call reflines.setactive
		}
	}
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
	
	call G.withvalue barKey.setvalue class @.bargraph.bargraph
END

DIALOG org, tabtitle("Organization")
BEGIN
  GROUPBOX gb_orient	_lft	_top	GBWD	_ht2,		///
	label("Orientation")
  RADIO    rb_horiz	_ilft	_ss	120	., 		///
	first							///
	label("Horizontal") nomemory
  RADIO    rb_vert	+125	@	@	.,		///
	last							///
	label("Vertical") nomemory

  CHECKBOX ck_stack	_lft	+45	_cwd1	.,		///
	label("Stack bars")
	
DEFINE y @y

  CHECKBOX ck_percent	@	_ms	@	.,		///
	label("Graph percentages")

  CHECKBOX ck_include0	@	_ms	@	.,		///
	label("Include zero in scale")

  CHECKBOX ck_reverse	@	_ms	@	.,		///
	label("Reverse scale")

  CHECKBOX ck_missing	@	_ms	@	.,		///
	label("Include missing categories")

  BUTTON   bu_rotate	_rj120	y	120	.,		///
	label("Rotate categories")				///
	onpush(program rotateCategories)			///
	tooltip("Cycle through over categories including having one category in the legend")

  BUTTON   bu_sort	_rj120	_ls	80	.,		///
	label("Sort bars")					///
	tooltip("Sort bars based on height")			///
	onpush(program sortBars)
  CHECKBOX ck_desc	@	_ms	120	.,		///
	label("Sort descending")

  GROUPBOX gb_gaps	_lft	+70	GBWD	_ht11,		///
	label("Bar spacing")

  TEXT     tx_varGap	_ilft	_ss	_wd	.,		///
	label("Variables:") right
  COMBOBOX cb_varGap	_sep	@	_cbwd	.,		///
	dropdown 						///
	contents(.__STYLES_LABEL.relsize) values(.__STYLES.relsize)

  TEXT     tx_grpGap	_ilft	_mls	_wd	.,		///
	label("Categories:") right
  COMBOBOX cb_grpGap	_sep	@	_cbwd	.,		///
	dropdown 						///
	contents(.__STYLES_LABEL.relsize) values(.__STYLES.relsize)

  TEXT     tx_sgrpGap	_ilft	_mls	_wd	.,		///
	label("Super categories:") right
  COMBOBOX cb_sgrpGap	_sep	@	_cbwd	.,		///
	dropdown 						///
	contents(.__STYLES_LABEL.relsize) values(.__STYLES.relsize)

  TEXT     tx_outerGap	_ilft	_mls	_wd	.,		///
	label("Outer:") right
  COMBOBOX cb_outerGap	_sep	@	_cbwd	.,		///
	dropdown 						///
	contents(.__STYLES_LABEL.relsize) values(.__STYLES.relsize)

  GROUPBOX gb_note	_lft	+45	_iwd	_ht3,		///
	label("Important note")
  TEXTBOX  tb_note	_ilft	_ss	_ibwd	_tb2,		///
	label("Changes made on this tab will reset any customizations which have been made to individual bars.")
END

PROGRAM sortBars
BEGIN
	put "_gm_dlg_edits, "
	put "object(" barKey ") "
	put "cmd(_gm_edit "
		put "." G ".GraphEdit, cmd(_set_sort_height "
		if org.ck_desc {
			put "0"
		}
		else {
			put "1"
		}			
		put ")) "
	stata hidden immediate
END

PROGRAM rotateCategories
BEGIN
	put "_gm_dlg_edits, "
	put "object(" barKey ") "
	put "cmd(_gm_edit "
		put "." G ".GraphEdit, cmd(_set_rotate"
	put ")) "
	stata hidden immediate
END

DIALOG labels, tabtitle("Labels")
BEGIN
  GROUPBOX gb_label	_lft	_top	_iwd	_ht11,		///
	label("Bar label properties")

  TEXT     tx_label	_ilft	_ss	_wd	.,		///
	label("Label:") right
  COMBOBOX cb_label	_sep	@	_cbwd	.,		///
	dropdownlist						///
	contents(.__STYLES_LABEL.barlabelstyle) 		///
	values(.__STYLES.barlabelstyle)

  TEXT     tx_labPos	_ilft	_mls	_wd	.,		///
	label("Position:") right
  COMBOBOX cb_labPos	_sep	@	_cbwd	.,		///
	dropdownlist						///
	contents(.__STYLES_LABEL.barlabelpos) 			///
	values(.__STYLES.barlabelpos)

  TEXT     tx_labGap	_ilft	_mls	_wd	.,		///
	label("Gap:") right
  COMBOBOX cb_labGap	_sep	@	_cbwd	.,		///
	dropdown						///
	contents(_textsizes)

  TEXT     tx_labFormat	_ilft	_mls	_wd	.,		///
	label("Format:") right
  EDIT     ed_labFormat	_sep	@	_cbwd	.,		///
	label("Format")
  BUTTON   bu_labFormat	_cbsep	@	_clwd	.,		///
	label("..") onpush(script label_GetFormat)		///
	tooltip("Create format")
END

SCRIPT label_GetFormat
BEGIN
	create CHILD format_chooser
	format_chooser.setExitAction "labels.ed_labFormat.setvalue class labelFormatResult.value"
	format_chooser.setExitString labelFormatResult
END

INCLUDE gr_ed_regions

DIALOG reflines, tabtitle("Reference lines")
BEGIN
  BUTTON   bu_href	_ilft	20	180	.,		///
	label("Add horizontal reference line")			///
	onpush(script show_hrefline_dialog)			///
	tooltip("Add a horizontal reference line at values on the y axis")
  BUTTON   bu_vref	_ilft	_ls	180	.,		///
	label("Add vertical reference line")			///
	onpush(script show_vrefline_dialog)			///
	tooltip("Add a vertical reference line at values on the x axis")
END

SCRIPT show_hrefline_dialog
BEGIN
	child__MESSAGE.setvalue "yaxis"
	create STRING hrefline_result
	create CHILD gr_ed_axis_refline AS gr_ed_h_refline
	gr_ed_h_refline.settitle "Add horizontal reference line"
	gr_ed_h_refline.setExitAction "program get_hrefline_result"
	gr_ed_h_refline.setExitString hrefline_result
END
PROGRAM get_hrefline_result
BEGIN
	if hrefline_result {
		put "_gm_dlg_edits , "
		put "object(" G ") "
		put "cmd(" 
			put "." G ".add_log_refline "
			put "y "
			put `" "" "'
			put hrefline_result
		put ") "
		stata hidden queue
	}
END

SCRIPT show_vrefline_dialog
BEGIN
	child__MESSAGE.setvalue "xaxis"
	create STRING vrefline_result
	create CHILD gr_ed_axis_refline AS gr_ed_v_refline
	gr_ed_v_refline.settitle "Add vertical reference line"
	gr_ed_v_refline.setExitAction "program get_vrefline_result"
	gr_ed_v_refline.setExitString vrefline_result
END
PROGRAM get_vrefline_result
BEGIN
	if vrefline_result {
		put "_gm_dlg_edits , "
		put "object(" G ") "
		put "cmd(" 
			put "." G ".add_log_refline "
			put "x "
			put `" "" "'
			put vrefline_result
		put ") "
		stata hidden queue
	}
END

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

PROGRAM get_defaults
BEGIN
// organization
	call barKey.withvalue horizontal.setvalue class @.horizontal.istrue
	if horizontal {
		call org.rb_horiz.setdefault 1
		call org.rb_vert.setdefault 0
	}
	else {
		call org.rb_vert.setdefault 1
		call org.rb_horiz.setdefault 0
	}

	call barKey.withvalue percentages.setvalue class @.percentages.istrue
	if percentages {
		call org.ck_percent.setdefault 1
	}
	else {
		call org.ck_percent.setdefault 0
	}

	call barKey.withvalue include0.setvalue class @.include0.istrue
	if include0 {
		call org.ck_include0.setdefault 1
	}
	else {
		call org.ck_include0.setdefault 0
	}

	call barKey.withvalue stackBars.setvalue class @.stack.istrue
	if stackBars {
		call org.ck_stack.setdefault 1
	}
	else {
		call org.ck_stack.setdefault 0
	}

	call barKey.withvalue missing.setvalue class @.fill.istrue
	if missing {
		call org.ck_missing.setdefault 1
	}
	else {
		call org.ck_missing.setdefault 0
	}

	call barKey.withvalue reverse.setvalue class @.reverse_scale.istrue
	if reverse {
		call org.ck_reverse.setdefault 1
	}
	else {
		call org.ck_reverse.setdefault 0
	}

	call barKey.withvalue index.setvalue class @.graphstyle.gap.styledex
	call index.withvalue varGap.setvalue class .__STYLES.relsize[@]
	if ! varGap {
		call barKey.withvalue varGap.setvalue class @.graphstyle.gap.val
	}
	call org.cb_varGap.setdefault class varGap.value

	call barKey.withvalue index.setvalue class @.graphstyle.group_gap.styledex
	call index.withvalue groupGap.setvalue class .__STYLES.relsize[@]
	if ! groupGap {
		call barKey.withvalue groupGap.setvalue class @.graphstyle.group_gap.val
	}
	call org.cb_grpGap.setdefault class groupGap.value

	call barKey.withvalue index.setvalue class @.graphstyle.supgroup_gap.styledex
	call index.withvalue sgroupGap.setvalue class .__STYLES.relsize[@]
	if ! sgroupGap {
		call barKey.withvalue sgroupGap.setvalue class @.graphstyle.supgroup_gap.val
	}
	call org.cb_sgrpGap.setdefault class sgroupGap.value

	call barKey.withvalue index.setvalue class @.graphstyle.outer_gap.styledex
	call index.withvalue outerGap.setvalue class .__STYLES.relsize[@]
	if ! outerGap {
		call barKey.withvalue outerGap.setvalue class @.graphstyle.outer_gap.val
	}
	call org.cb_outerGap.setdefault class outerGap.value

// labels
	call barKey.withvalue index.setvalue class @.barlabel_method.styledex
	call index.withvalue labelMethod.setvalue class .__STYLES.barlabelstyle[@]
	call labels.cb_label.setdefault class labelMethod.value

	call barKey.withvalue index.setvalue class @.barlabel_pos.styledex
	call index.withvalue labelPosition.setvalue class .__STYLES.barlabelpos[@]
	call labels.cb_labPos.setdefault class labelPosition.value

	call barKey.withvalue index.setvalue class @.graphstyle.barlabel_gap.styledex
	call index.withvalue labelGap.setvalue class .__STYLES.gsize[@]
	if ! labelGap {
		call barKey.withvalue labelGap.setvalue class @.graphstyle.barlabel_gap.val
	}
	call labels.cb_labGap.setdefault class labelGap.value

	call barKey.withvalue labelFormat.setvalue class @.graphstyle.barlabel_format
	call labels.ed_labFormat.setdefault class labelFormat.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
	}
END


PROGRAM initialize_controls
BEGIN
// organization
	if horizontal.iseq(1) {
		call org.rb_horiz.seton
	}
	else {
		call org.rb_vert.seton
	}

	if percentages {
		call org.ck_percent.seton
	}
	else {
		call org.ck_percent.setoff
	}

	if include0 {
		call org.ck_include0.seton
	}
	else {
		call org.ck_include0.setoff
	}

	if stackBars {
		call org.ck_stack.seton
	}
	else {
		call org.ck_stack.setoff
	}

	if missing {
		call org.ck_missing.seton
	}
	else {
		call org.ck_missing.setoff
	}

	if reverse {
		call org.ck_reverse.seton
	}
	else {
		call org.ck_reverse.setoff
	}

	call org.cb_varGap.setvalue class varGap.value
	call org.cb_grpGap.setvalue class groupGap.value
	call org.cb_sgrpGap.setvalue class sgroupGap.value
	call org.cb_outerGap.setvalue class outerGap.value

// labels
	call labels.cb_label.setvalue class labelMethod.value
	call labels.cb_labPos.setvalue class labelPosition.value
	call labels.cb_labGap.setvalue class labelGap.value
	call labels.ed_labFormat.setvalue class labelFormat.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
	}
END

PROGRAM org_output
BEGIN
	put "_gm_dlg_edits"
	beginoptions
		put " object(" barKey ") "

		if org.rb_horiz & ! org.rb_horiz.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_horizontal "
			put "yes"			
			put ")) "
		}
		if org.rb_vert & ! org.rb_vert.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_horizontal "
			put "no"			
			put ")) "
		}
		if ! org.ck_percent.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_percentages "
			if org.ck_percent {
				put "yes"			
			}
			else {
				put "no"
			}
			put ")) "
		}
		if ! org.ck_include0.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_include0 "
			if org.ck_include0 {
				put "yes"			
			}
			else {
				put "no"
			}
			put ")) "
		}
		if ! org.ck_stack.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_stack "
			if org.ck_stack {
				put "yes"			
			}
			else {
				put "no"
			}
			put ")) "
		}
		if ! org.ck_missing.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_fill "
			if org.ck_missing {
				put "yes"			
			}
			else {
				put "no"
			}
			put ")) "
		}
		if ! org.ck_reverse.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_reverse_scale "
			if org.ck_reverse {
				put "yes"			
			}
			else {
				put "no"
			}
			put ")) "
		}
		if ! org.cb_varGap.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_graphstyle_remake "
			put "gap " org.cb_varGap
			put ")) "
		}
		if ! org.cb_grpGap.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_graphstyle_remake "
			put "group_gap " org.cb_grpGap
			put ")) "
		}
		if ! org.cb_sgrpGap.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_graphstyle_remake "
			put "supgroup_gap " org.cb_sgrpGap
			put ")) "
		}
		if ! org.cb_outerGap.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_graphstyle_remake "
			put "outer_gap " org.cb_outerGap
			put ")) "
		}

// labels	
		if ! labels.cb_label.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_barlabel_method "
			put labels.cb_label
			put ")) "
		}
		if ! labels.cb_labPos.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_barlabel_pos "
			put labels.cb_labPos
			put ")) "
		}
		if ! labels.cb_labGap.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_graphstyle_remake "
			put " barlabel_gap " labels.cb_labGap
			put ")) "
		}
		if ! labels.ed_labFormat.isdefault() {
			put "cmd(_gm_edit "
			put "." G ".GraphEdit, cmd(_set_graphstyle_remake "
			put " barlabel_format " labels.ed_labFormat
			put ")) "
		}
	
		put " nodraw "
	endoptions
	stata hidden queue
END

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

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

		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) " 
			}
		}
		put " epilog(_gedi toolbar barregion " G ")"
	endoptions
	stata hidden queue

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