/*
  gr_ed_rline.idlg

*!  VERSION 1.0.1  18apr2007

*/


SCRIPT rline_PREINIT
BEGIN
	create DOUBLE plotCustomLabel
	create DOUBLE plotCustomStyle

	create STRING color
	create STRING rgb
	create STRING width
	create STRING pattern
	create STRING cmissing
	create DOUBLE drawview

	program rline_get_defaults
END

DIALOG rline, tabtitle("Lines")
BEGIN
  GROUPBOX gb_line	_lft	_top	_iwd	_ht11,		///
	label("Line properties")
  TEXT     tx_clr	_ilft	_ss	_wd	.,		///
	label("Color:") right
  COMBOBOX cb_clr	_sep	@	_cbwd	.,		///
	dropdownlist contents(_colors) 				///
	onselchangelist(rline_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_cmissing	_ilft	 _mls	_wd	.,		///
	label("Fill missing values:") right
  COMBOBOX cb_cmissing	_sep	@	_cbwd	.,		///
	dropdownlist						///
	contents(_yesno)

  // public scope (do not change name)
  CHECKBOX ck_hide	_lft	+50	_iwd	.,		///
	label("Hide plot")

  BUTTON   bu_sort	_rj60	@	60	.,		///
	label("Sort") tooltip("Resort on x variable")		///
	onpush(program sort_on_x)
END

PROGRAM sort_on_x
BEGIN
	call G.withvalue plotCustomStyle.storeClsArraySize @.obs_styles
	call G.withvalue plotCustomLabel.storeClsArraySize @.obs_labels	
	if plotCustomStyle.iseq(0) & plotCustomLabel.iseq(0) {
		put "_gm_dlg_edits, "
		put "object(" G ") "
		put "cmd(" "." G ".SortOnX" ") "
		stata hidden queue
	}
	else {
		stopbox stop "Sort is not allowed on plots with custom styles or labels"
	}
END

SCRIPT rline_POSTINIT
BEGIN
	program rline_initialize_controls
END

LIST rline_cl_clr_sel
BEGIN
	rline.cl_clr.show
	rline.cl_clr.hide
END

PROGRAM rline_get_defaults
BEGIN
	call Gstyle.withvalue index.setvalue class @.area.linestyle.color.styledex
	call index.withvalue color.setvalue class .__STYLES.color[@]
	call rline.cb_clr.setdefault class color.value
	
	call Gstyle.withvalue rgb.setvalue class @.area.linestyle.color.rgb
	call rline.cl_clr.setdefault class rgb.value

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

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

	call G.withvalue index.setvalue class @.style.connect_missings.styledex
	call index.withvalue cmissing.setvalue class .__STYLES.yesno[@]
	call rline.cb_cmissing.setdefault class cmissing.value

	call G.withvalue drawview.setvalue class @.draw_view.istrue
	if drawview {
		call rline.ck_hide.setdefault 0
	}
	else {
		call rline.ck_hide.setdefault 1
	}
END

PROGRAM rline_initialize_controls
BEGIN
	if color {
		call rline.cb_clr.setvalue class color.value
	}
	if ! color {
		call rline.cb_clr.setvalue "custom"
	}
	call rline.cl_clr.setvalue class rgb.value

	call rline.cb_width.setvalue class width.value
	call rline.cb_pattern.setvalue class pattern.value
	call rline.cb_cmissing.setvalue class cmissing.value

	if drawview {
		call rline.ck_hide.setoff
	}
	else {
		call rline.ck_hide.seton
	}
END

PROGRAM rline_output
BEGIN
	call program rline_get_defaults

	if ! rline.cl_clr.isvisible() & ! rline.cb_clr.isdefault() {
		put "style(area(linestyle(color(" 
		put rline.cb_clr 
		put ")))) "
	}
	if rline.cl_clr.isvisible() & ! rline.cl_clr.isdefault() {
		put "style(area(linestyle(color("  `"""'
		put rline.cl_clr
		put `"""'
		put ")))) "
	}
	if ! rline.cb_width.isdefault() {
		put "style(area(linestyle(width("
		put rline.cb_width
		put ")))) "
	}
	if ! rline.cb_pattern.isdefault() {
		put "style(area(linestyle(pattern("
		put rline.cb_pattern
		put ")))) "
	}
	if ! rline.cb_cmissing.isdefault() {
		put "style(connect_missings("
		put rline.cb_cmissing
		put ")) "
	}

	if ! rline.ck_hide.isdefault() {
		if rline.ck_hide {
			put "set(draw_view no) "
		}
		else {
			put "set(draw_view yes) "
		}
	}
END
