/*
  gr_ed_arrow.dlg

*!  VERSION 1.0.1  09mar2007

*/

VERSION 10.0
MODAL

INCLUDE _std_vsmall
DEFINE _dlght 310
INCLUDE header_gr_ed

DEFINE _clwd	15
DEFINE _wd	110
DEFINE _sep	+115
DEFINE _wd2	100
DEFINE _sep2	+105
DEFINE _cbwd	130
DEFINE _cbsep	+135

SCRIPT PREINIT
BEGIN	
	create STRING G
	program setupGOBJECT
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

SCRIPT main_PREINIT
BEGIN
	create STRING color
	create STRING rgb
	create STRING width
	create STRING pattern
	create DOUBLE index
	program main_get_defaults
END

DIALOG main, title("Added line properties") tabtitle("Line")
BEGIN
  GROUPBOX gb_lines	_lft	_top	_iwd	_ht8,		///
	label("Line properties")
  TEXT     tx_clr	_ilft	_ss	_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)
END

SCRIPT main_POSTINIT
BEGIN
	program main_initialize_controls
END

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

PROGRAM main_get_defaults
BEGIN	
	call G.withvalue index.setvalue class @.style.linestyle.color.styledex
	call index.withvalue color.setvalue class .__STYLES.color[@]
	call main.cb_clr.setdefault class color.value
	
	call G.withvalue rgb.setvalue class @.style.linestyle.color.rgb
	call main.cl_clr.setdefault class rgb.value

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

	call G.withvalue index.setvalue class @.style.linestyle.pattern.styledex
	call index.withvalue pattern.setvalue class .__STYLES.linepattern[@]
	call main.cb_pattern.setdefault class pattern.value
END

PROGRAM main_initialize_controls
BEGIN
	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
END

PROGRAM main_output
BEGIN
	if ! main.cl_clr.isvisible() & ! main.cb_clr.isdefault() {
		put "style(linestyle(color(" 
		put main.cb_clr 
		put "))) "
	}
	if main.cl_clr.isvisible() & ! main.cl_clr.isdefault() {
		put "style(linestyle(color("  `"""'
		put main.cl_clr
		put `"""'
		put "))) "
	}
	if ! main.cb_width.isdefault() {
		put "style(linestyle(width("
		put main.cb_width
		put "))) "
	}
	if ! main.cb_pattern.isdefault() {
		put "style(linestyle(pattern("
		put main.cb_pattern
		put "))) "
	}
END

//*****************************************************************************

DIALOG arrow, tabtitle("Arrowheads")
BEGIN
  CHECKBOX ck_head	_lft	_top	_iwd	.,		///
	label("Place arrowhead at ending point")		///
	onclickon(program arrow_check_status)			///
	onclickoff(program arrow_check_status)			///
	nomemory
  CHECKBOX ck_tail	_lft	_ss	_iwd	.,		///
	label("Place arrowhead at beginning point")		///
	onclickon(program arrow_check_status)			///
	onclickoff(program arrow_check_status)			///
	nomemory

  GROUPBOX gb_basic	_lft	_ms	_iwd	_ht21,		///
	label("Arrowhead properties")
	
  TEXT     tx_size	_ilft	_ss	_wd	.,		///
	label("Size:") right
  COMBOBOX cb_size	_sep	@	_cbwd	.,		///
	dropdown						///
	contents(_symbolsizes)
  
  TEXT     tx_bsize	_ilft	_mls	_wd	.,		///
	label("Barb size:") right
  COMBOBOX cb_bsize	_sep	@	_cbwd	.,		///
	dropdown 						///
	contents(_symbolsizes)

  TEXT     tx_angle	_ilft	_mls	_wd	.,		///
	label("Angle:") right
  COMBOBOX cb_angle	_sep	@	_cbwd	.,		///
	dropdown						///
	contents(_angles)					///
	nomemory

  TEXT     tx_clr	_ilft	_mls	_wd	.,		///
	label("Color:") right
  COMBOBOX cb_clr	_sep	@	_cbwd	.,		///
	dropdownlist contents(_colors) 				///
	onselchangelist(arrow_cl_clr_sel)
  COLOR    cl_clr	_cbsep	@	_clwd	.,

  CHECKBOX ck_outline	_ilft	+35	_ibwd	_ht6,		///
	groupbox						///
	label("Different outline color")			///
	onclickon(script arrow_outline_on)			///
	onclickoff(script arrow_outline_off) nomemory
  
  TEXT     tx_oclr	_iilft	_ms	_wd2	.,		///
	label("Outline color:") right
  COMBOBOX cb_oclr	_sep2	@	_cbwd	.,		///
	dropdownlist contents(_colors) 				///
	onselchangelist(arrow_cl_oclr_sel)
  COLOR    cl_oclr	_cbsep	@	_clwd	.,

  TEXT     tx_owidth	_iilft	_mls	_wd2	.,		///
	label("Outline width:") right
  COMBOBOX cb_owidth	_sep2	@	_cbwd	.,		///
	dropdown 						///
	contents(_linewidths)
END

PROGRAM arrow_check_status
BEGIN
	if arrow.ck_head | arrow.ck_tail {
		call arrow.gb_basic.enable
		call arrow.tx_size.enable
		call arrow.cb_size.enable
		call arrow.tx_bsize.enable
		call arrow.cb_bsize.enable
		call arrow.tx_angle.enable
		call arrow.cb_angle.enable
		call arrow.tx_clr.enable
		call arrow.cb_clr.enable
		call arrow.cl_clr.enable
		call arrow.ck_outline.enable
		if arrow.ck_outline {
			call script arrow_outline_on
		}
	}
	else {
		call arrow.gb_basic.disable
		call arrow.tx_size.disable
		call arrow.cb_size.disable
		call arrow.tx_bsize.disable
		call arrow.cb_bsize.disable
		call arrow.tx_angle.disable
		call arrow.cb_angle.disable
		call arrow.tx_clr.disable
		call arrow.cb_clr.disable
		call arrow.cl_clr.disable
		call arrow.ck_outline.disable
		call script arrow_outline_off
	}
END

SCRIPT arrow_outline_on
BEGIN
	arrow.tx_oclr.enable
	arrow.cb_oclr.enable
	arrow.cl_oclr.enable
	arrow.tx_owidth.enable
	arrow.cb_owidth.enable
END

SCRIPT arrow_outline_off
BEGIN
	arrow.tx_oclr.disable
	arrow.cb_oclr.disable
	arrow.cl_oclr.disable
	arrow.tx_owidth.disable
	arrow.cb_owidth.disable
END

LIST arrow_cl_clr_sel
BEGIN
	arrow.cl_clr.show
	arrow.cl_clr.hide
END

LIST arrow_cl_oclr_sel
BEGIN
	arrow.cl_oclr.show
	arrow.cl_oclr.hide
END

SCRIPT arrow_PREINIT
BEGIN
	create STRING  arrowDir
	create BOOLEAN hasHead
	create BOOLEAN hasTail
	create STRING  arrowSize
	create STRING  arrowBSize
	create STRING  arrowAngle
	create STRING  arrowColor
	create STRING  arrowRGB
	create STRING  arrowOColor
	create STRING  arrowORGB
	create STRING  arrowOWidth
	program arrow_get_defaults
END

SCRIPT arrow_POSTINIT
BEGIN
	program arrow_initialize_controls
END

PROGRAM arrow_get_defaults
BEGIN	
	call G.withvalue index.setvalue class @.style.headpos.styledex
	call index.withvalue arrowDir.setvalue class .__STYLES.arrowdir[@]
	call hasHead.setfalse
	call hasTail.setfalse
	call arrow.ck_head.setdefault 0
	call arrow.ck_tail.setdefault 0
	if arrowDir.iseq("head") | arrowDir.iseq("both") {
		call hasHead.settrue
		call arrow.ck_head.setdefault 1
	}
	if arrowDir.iseq("tail") | arrowDir.iseq("both") {
		call hasTail.settrue
		call arrow.ck_tail.setdefault 1
	}

	call G.withvalue index.setvalue class @.style.headstyle.size.styledex
	call index.withvalue arrowSize.setvalue class .__STYLES.symbolsize[@]
	if ! arrowSize {
		call G.withvalue arrowSize.setvalue class @.style.headstyle.size.val
	}
	call arrow.cb_size.setdefault class arrowSize.value
	
	call G.withvalue index.setvalue class @.style.headstyle.backsize.styledex
	call index.withvalue arrowBSize.setvalue class .__STYLES.symbolsize[@]
	if ! arrowBSize {
		call G.withvalue arrowBSize.setvalue class @.style.headstyle.backsize.val
	}
	call  arrow.cb_bsize.setdefault class arrowBSize.value

	call G.withvalue index.setvalue class @.style.headstyle.angle.styledex
	call index.withvalue arrowAngle.setvalue class .__STYLES.anglestyle[@]
	if ! arrowAngle {
		call G.withvalue arrowAngle.setvalue class @.style.headstyle.angle.val
	}
	call arrow.cb_angle.setdefault class arrowAngle.value

	call G.withvalue index.setvalue class @.style.headstyle.fillcolor.styledex
	call index.withvalue arrowColor.setvalue class .__STYLES.color[@]
	call arrow.cb_clr.setdefault class arrowColor.value
	
	call G.withvalue arrowRGB.setvalue class @.style.headstyle.fillcolor.rgb
	call arrow.cl_clr.setdefault class arrowRGB.value

	call G.withvalue index.setvalue class @.style.headstyle.linestyle.color.styledex
	call index.withvalue arrowOColor.setvalue class .__STYLES.color[@]
	call arrow.cb_oclr.setdefault class arrowOColor.value

	call G.withvalue arrowORGB.setvalue class @.style.headstyle.linestyle.color.rgb
	call arrow.cl_oclr.setdefault class arrowORGB.value

	call G.withvalue index.setvalue class @.style.headstyle.linestyle.width.styledex
	call index.withvalue arrowOWidth.setvalue class .__STYLES.linewidth[@]
	if ! arrowOWidth {
		call G.withvalue arrowOWidth.setvalue class @.style.headstyle.linestyle.width.val
	}
	call arrow.cb_owidth.setdefault class arrowOWidth.value
END

PROGRAM arrow_initialize_controls
BEGIN
	if hasHead {
		call arrow.ck_head.seton
	}
	else {
		call arrow.ck_head.setoff
	}
	if hasTail {
		call arrow.ck_tail.seton
	}
	else {
		call arrow.ck_tail.setoff
	}
	call arrow.cb_size.setvalue class arrowSize.value
	call arrow.cb_bsize.setvalue class arrowBSize.value
	call arrow.cb_angle.setvalue class arrowAngle.value
	if arrowColor {
		call arrow.cb_clr.setvalue class arrowColor.value
	}
	else {
		call arrow.cb_clr.setvalue "custom"
	}
	call arrow.cl_clr.setvalue class arrowRGB.value
	if arrowOColor {
		call arrow.cb_oclr.setvalue class arrowOColor.value
	}
	else {
		call arrow.cb_oclr.setvalue "custom"
	}
	call arrow.cl_oclr.setvalue class arrowORGB.value
	call arrow.cb_owidth.setvalue class arrowOWidth.value
	
	if arrowRGB.iseq(arrowORGB) {
		call arrow.ck_outline.setoff
	}
	else {
		call arrow.ck_outline.seton
	}

	call program arrow_check_status
END

PROGRAM arrow_output
BEGIN
	if ! arrow.ck_head.isdefault() | ! arrow.ck_tail.isdefault() {
		if arrow.ck_head & arrow.ck_tail {
			put "style(headpos(both)) "
		}
		if arrow.ck_head & ! arrow.ck_tail {
			put "style(headpos(head)) "
		}
		if ! arrow.ck_head & arrow.ck_tail {
			put "style(headpos(tail)) "
		}
		if ! arrow.ck_head & ! arrow.ck_tail {
			put "style(headpos(neither)) "
		}
	}

	if ! arrow.ck_head & ! arrow.ck_tail {
		exit
	}
		
	if ! arrow.cb_size.isdefault() {
		put "style(headstyle(size("
		put arrow.cb_size
		put "))) "
	}

	if ! arrow.cb_bsize.isdefault() {
		put "style(headstyle(backsize("
		put arrow.cb_bsize
		put "))) "
	}

	if ! arrow.cb_angle.isdefault() {
		put "style(headstyle(angle("
		put arrow.cb_angle
		put "))) "
	}


	// always output fill color when not the default
	if ! arrow.cl_clr.isvisible() & ! arrow.cb_clr.isdefault() {
		put "style(headstyle(fillcolor(" 
		put arrow.cb_clr 
		put "))) "
	}
	if arrow.cl_clr.isvisible() & ! arrow.cl_clr.isdefault() {
		put "style(headstyle(fillcolor("  `"""'
		put arrow.cl_clr
		put `"""'
		put "))) "
	}
	
	// determine when outline color should be output
	if ! arrow.ck_outline {
		// force the outline color to the same when necessary
		if ! arrow.cb_clr.iseq(arrowOColor) & ! arrow.cl_clr.isvisible() {
			// they are different... force them to be the same
			put "style(headstyle(linestyle(color(" 
			put arrow.cb_clr 
			put ")))) "
		}
		if ! arrow.cl_clr.iseq(arrowORGB) & arrow.cl_clr.isvisible() {
			// they are different... force them to be the same
			put "style(headstyle(linestyle(color("  `"""'
			put arrow.cl_clr
			put `"""'
			put ")))) "
		}
		
	}
	else {
		// request that outline and fill are different
		if ! arrow.cl_oclr.isvisible() & ! arrow.cb_oclr.isdefault() {
			put "style(headstyle(linestyle(color(" 
			put arrow.cb_oclr 
			put ")))) "
		}
		if arrow.cl_oclr.isvisible() & ! arrow.cl_oclr.isdefault() {
			put "style(headstyle(linestyle(color("  `"""'
			put arrow.cl_oclr
			put `"""'
			put ")))) "			
		}
	}
	
	if ! arrow.cb_owidth.isdefault() {
		put "style(headstyle(linestyle(width("
		put /hidden arrow.cb_owidth
		put ")))) "
	}
END

SCRIPT adv_PREINIT
BEGIN
	create DOUBLE drawview
	program adv_get_defaults
END

SCRIPT adv_POSTINIT
BEGIN
	program adv_initialize_controls
END

PROGRAM adv_get_defaults
BEGIN
	call G.withvalue drawview.setvalue class @.draw_view.istrue
	if drawview {
		call adv.ck_hide.setdefault 0
	}
	else {
		call adv.ck_hide.setdefault 1
	}
END

PROGRAM adv_initialize_controls
BEGIN
	if drawview {
		call adv.ck_hide.setoff
	}
	else {
		call adv.ck_hide.seton
	}
	if G.startsWith("_Gr_Global") {
		call adv.bu_reset.show
		call adv.ck_hide.hide
	}
	else {
		call adv.bu_reset.hide
		call adv.ck_hide.show
	}
END

DIALOG adv, tabtitle("Advanced")
BEGIN
  CHECKBOX ck_hide	_lft	_top	_iwd	.,		///
	label("Hide added line")

  BUTTON   bu_reset	_lft	@	130	.,		///
	label("Reset Defaults")					///
	onpush(program reset_defaults)
END

PROGRAM reset_defaults
BEGIN
	call create STRING graph_name
	call graph_name.setvalue class _Gr_Global.edit_graph
	put "." graph_name ".ResetPrototypeDefaults " G
	stata hidden immediate
	
	call program main_get_defaults
	call program main_initialize_controls
	call program arrow_get_defaults
	call program arrow_initialize_controls
	call program adv_get_defaults
	call program adv_initialize_controls

	stopbox note "Defaults have been reset."
END

PROGRAM adv_output
BEGIN
	if ! adv.ck_hide.isdefault() {
		if adv.ck_hide {
			put "set(draw_view no) "
		}
		else {
			put "set(draw_view yes) "
		}
	}
END

PROGRAM command
BEGIN
	call program main_get_defaults
	call program arrow_get_defaults
	call program adv_get_defaults

	put "_gm_dlg_edits"
	beginoptions
		put " object(" G ") "
		put " " /program main_output
		put " " /program arrow_output
		put " " /program adv_output
		put " epilog(_gedi toolbar arrow " G ")"
	endoptions
END
