namespace IndexFormula.Finance.Win
{
using IndexFormula.Finance;
using MuchInfo.Chart.Data.EnumTypes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows.Forms;
///
/// 当前数据公式应用窗口
/// ShowForm()后,直接取FormulaManager.CurrentFormulas来获取得到的公式
///
[ToolboxItem(false)]
public partial class FormulaManager : Form
{
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnDown;
private System.Windows.Forms.Button btnMore;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnSource;
private System.Windows.Forms.Button btnUp;
private System.Windows.Forms.CheckBox cbSecondYAxis;
private IContainer components;
private System.Windows.Forms.ComboBox ddlFavoriteFormula;
private bool DisableChangeEvent;
private System.Windows.Forms.GroupBox gbCurrent;
private System.Windows.Forms.GroupBox gbParam;
public System.Windows.Forms.ImageList ilFormula;
private System.Windows.Forms.ImageList ilIcon;
private System.Windows.Forms.ListBox lbFormulaList;
private string[] ListedFormulas = new string[] {
"MAIN", "MainArea", "VOLMA", "MACD", "RSI", "CMF", "TRIX", "CCI", "FastSTO", "SlowSTO", "ATR", "OBV", "ULT", "DPO", "WR", "PPO",
"PVO", "StochRSI", "AD"
};
private string[] OverlayFormulas = new string[] {
"MAIN", "MainArea", "HL", "MA", "EMA", "BB", "AreaBB", "SAR", "ZIGLABEL", "ZIG", "ZIGICON", "ZIGW", "ZIGSR", "SR", "SRAxisY", "COMPARE",
"COMPARE2", "Fibonnaci", "LinRegr", "TradingIcon"
};
private System.Windows.Forms.ToolTip tpBtnDown;
private System.Windows.Forms.ToolTip tpBtnUp;
private System.Windows.Forms.ToolTip tpDelete;
private System.Windows.Forms.ToolTip tpSource;
public FormulaManager()
{
this.InitializeComponent();
switch (SetLanguage.currentLanguage)
{
case ChartLanguageType.SimplifiedChinese:
this.tpBtnUp.SetToolTip(this.btnUp, "上移");
this.tpBtnDown.SetToolTip(this.btnDown, "下移");
this.tpDelete.SetToolTip(this.btnDelete, "删除");
this.tpSource.SetToolTip(this.btnSource, "源代码");
break;
case ChartLanguageType.TraditionalChinese:
this.tpBtnUp.SetToolTip(this.btnUp, "上移");
this.tpBtnDown.SetToolTip(this.btnDown, "下移");
this.tpDelete.SetToolTip(this.btnDelete, "刪除");
this.tpSource.SetToolTip(this.btnSource, "源代碼");
break;
case ChartLanguageType.English:
this.tpBtnUp.SetToolTip(this.btnUp, "Move Up");
this.tpBtnDown.SetToolTip(this.btnDown, "Move Down");
this.tpDelete.SetToolTip(this.btnDelete, "Delete");
this.tpSource.SetToolTip(this.btnSource, "Source Code");
break;
}
//SetLanguage.CurrentUICulture();
}
private void AddFavorite(string[] Formulas)
{
this.ddlFavoriteFormula.Items.Clear();
switch (SetLanguage.currentLanguage)
{
case ChartLanguageType.SimplifiedChinese:
this.ddlFavoriteFormula.Items.Add("选择要添加新的公式");
break;
case ChartLanguageType.TraditionalChinese:
this.ddlFavoriteFormula.Items.Add("選擇要添加新的公式");
break;
case ChartLanguageType.English:
this.ddlFavoriteFormula.Items.Add("Select to add new formula");
break;
}
foreach (string str in Formulas)
{
FormulaBase formulaByName = FormulaBase.GetFormulaByName(str);
foreach (object obj2 in this.ddlFavoriteFormula.Items)
{
if (obj2.GetType() == formulaByName.GetType())
{
continue;
}
}
this.ddlFavoriteFormula.Items.Add(formulaByName);
}
this.ddlFavoriteFormula.SelectedIndex = 0;
}
private void AddFormulas()
{
FormulaBase selectedItem = (FormulaBase)this.ddlFavoriteFormula.SelectedItem;
if (selectedItem != null)
{
this.AddFormulas(selectedItem.CreateName);
}
}
private void AddFormulas(string Name)
{
this.lbFormulaList.Items.Add(Name);
this.SelectLastFormula();
}
private void btnDelete_Click(object sender, EventArgs e)
{
this.DeleteFormulas();
}
private void btnDown_Click(object sender, EventArgs e)
{
this.MoveFormula(1);
}
private void btnMore_Click(object sender, EventArgs e)
{
string name = new SelectFormula().Select(null, null, false);
if (name != null)
{
this.AddFormulas(name);
}
}
private void btnSource_Click(object sender, EventArgs e)
{
int selectedIndex = this.lbFormulaList.SelectedIndex;
if (selectedIndex >= 0)
{
EditFormula(FormulaBase.GetFormulaByName((string)this.lbFormulaList.Items[selectedIndex]));
}
}
///
/// 编辑公式
///
///
///
public static bool EditFormula(FormulaBase fb)
{
string formulaFile = PluginManager.GetFormulaFile(fb);
if (formulaFile != null)
{
string directoryName = Path.GetDirectoryName(formulaFile);
formulaFile = Path.GetFileNameWithoutExtension(formulaFile).Replace('_', '.');
formulaFile = directoryName + @"\" + formulaFile;
if (File.Exists(formulaFile))
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(SetLanguage.languageName);
FormulaSourceEditor.Open(formulaFile, fb.GetType().ToString());
return true;
}
}
return false;
}
private void btnUp_Click(object sender, EventArgs e)
{
this.MoveFormula(-1);
}
private void cbSecondYAxis_CheckedChanged(object sender, EventArgs e)
{
this.DisableChangeEvent = true;
try
{
if (this.lbFormulaList.SelectedItem != null)
{
string str = this.lbFormulaList.SelectedItem.ToString();
this.lbFormulaList.Items[this.lbFormulaList.SelectedIndex] = str.TrimEnd(new char[] { '!' }) + (this.cbSecondYAxis.Checked ? "!" : "");
}
}
finally
{
this.DisableChangeEvent = false;
}
}
private void cbSecondYAxis_Click(object sender, EventArgs e)
{
}
private void ddlFavoriteFormula_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ddlFavoriteFormula.SelectedIndex > 0)
{
this.AddFormulas();
this.ddlFavoriteFormula.SelectedIndex = 0;
}
}
private void DeleteFormulas()
{
this.DisableChangeEvent = true;
int selectedIndex = this.lbFormulaList.SelectedIndex;
if (selectedIndex >= 0)
{
try
{
this.lbFormulaList.Items.RemoveAt(selectedIndex);
}
finally
{
this.DisableChangeEvent = false;
}
this.SetSelectFormula(selectedIndex);
}
}
protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormulaManager));
this.ddlFavoriteFormula = new System.Windows.Forms.ComboBox();
this.btnCancel = new System.Windows.Forms.Button();
this.gbCurrent = new System.Windows.Forms.GroupBox();
this.cbSecondYAxis = new System.Windows.Forms.CheckBox();
this.btnDown = new System.Windows.Forms.Button();
this.btnUp = new System.Windows.Forms.Button();
this.btnSource = new System.Windows.Forms.Button();
this.lbFormulaList = new System.Windows.Forms.ListBox();
this.gbParam = new System.Windows.Forms.GroupBox();
this.btnDelete = new System.Windows.Forms.Button();
this.ilIcon = new System.Windows.Forms.ImageList(this.components);
this.btnOK = new System.Windows.Forms.Button();
this.btnMore = new System.Windows.Forms.Button();
this.ilFormula = new System.Windows.Forms.ImageList(this.components);
this.tpBtnUp = new System.Windows.Forms.ToolTip(this.components);
this.tpBtnDown = new System.Windows.Forms.ToolTip(this.components);
this.tpDelete = new System.Windows.Forms.ToolTip(this.components);
this.tpSource = new System.Windows.Forms.ToolTip(this.components);
this.gbCurrent.SuspendLayout();
this.SuspendLayout();
//
// ddlFavoriteFormula
//
this.ddlFavoriteFormula.DisplayMember = "CombineName";
this.ddlFavoriteFormula.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
resources.ApplyResources(this.ddlFavoriteFormula, "ddlFavoriteFormula");
this.ddlFavoriteFormula.Name = "ddlFavoriteFormula";
this.ddlFavoriteFormula.SelectedIndexChanged += new System.EventHandler(this.ddlFavoriteFormula_SelectedIndexChanged);
//
// btnCancel
//
resources.ApplyResources(this.btnCancel, "btnCancel");
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Name = "btnCancel";
//
// gbCurrent
//
resources.ApplyResources(this.gbCurrent, "gbCurrent");
this.gbCurrent.Controls.Add(this.cbSecondYAxis);
this.gbCurrent.Controls.Add(this.btnDown);
this.gbCurrent.Controls.Add(this.btnUp);
this.gbCurrent.Controls.Add(this.btnSource);
this.gbCurrent.Controls.Add(this.lbFormulaList);
this.gbCurrent.Controls.Add(this.gbParam);
this.gbCurrent.Controls.Add(this.btnDelete);
this.gbCurrent.Name = "gbCurrent";
this.gbCurrent.TabStop = false;
//
// cbSecondYAxis
//
resources.ApplyResources(this.cbSecondYAxis, "cbSecondYAxis");
this.cbSecondYAxis.Name = "cbSecondYAxis";
this.cbSecondYAxis.CheckedChanged += new System.EventHandler(this.cbSecondYAxis_CheckedChanged);
this.cbSecondYAxis.Click += new System.EventHandler(this.cbSecondYAxis_Click);
//
// btnDown
//
resources.ApplyResources(this.btnDown, "btnDown");
this.btnDown.Image = global::IndexFormula.Finance.Win.Properties.Resources.arrow_down;
this.btnDown.Name = "btnDown";
this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
//
// btnUp
//
resources.ApplyResources(this.btnUp, "btnUp");
this.btnUp.Image = global::IndexFormula.Finance.Win.Properties.Resources.arrow_up;
this.btnUp.Name = "btnUp";
this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
//
// btnSource
//
resources.ApplyResources(this.btnSource, "btnSource");
this.btnSource.Image = global::IndexFormula.Finance.Win.Properties.Resources.application_edit;
this.btnSource.Name = "btnSource";
this.btnSource.Click += new System.EventHandler(this.btnSource_Click);
//
// lbFormulaList
//
resources.ApplyResources(this.lbFormulaList, "lbFormulaList");
this.lbFormulaList.DisplayMember = "Description";
this.lbFormulaList.Name = "lbFormulaList";
this.lbFormulaList.ValueMember = "Name";
this.lbFormulaList.SelectedIndexChanged += new System.EventHandler(this.lbOverlayList_SelectedIndexChanged);
//
// gbParam
//
resources.ApplyResources(this.gbParam, "gbParam");
this.gbParam.Name = "gbParam";
this.gbParam.TabStop = false;
//
// btnDelete
//
resources.ApplyResources(this.btnDelete, "btnDelete");
this.btnDelete.Image = global::IndexFormula.Finance.Win.Properties.Resources.Delete;
this.btnDelete.Name = "btnDelete";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// ilIcon
//
this.ilIcon.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
resources.ApplyResources(this.ilIcon, "ilIcon");
this.ilIcon.TransparentColor = System.Drawing.Color.Transparent;
//
// btnOK
//
resources.ApplyResources(this.btnOK, "btnOK");
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Name = "btnOK";
//
// btnMore
//
resources.ApplyResources(this.btnMore, "btnMore");
this.btnMore.Name = "btnMore";
this.btnMore.Click += new System.EventHandler(this.btnMore_Click);
//
// ilFormula
//
this.ilFormula.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
resources.ApplyResources(this.ilFormula, "ilFormula");
this.ilFormula.TransparentColor = System.Drawing.Color.Transparent;
//
// FormulaManager
//
this.AcceptButton = this.btnOK;
resources.ApplyResources(this, "$this");
this.CancelButton = this.btnCancel;
this.Controls.Add(this.btnMore);
this.Controls.Add(this.ddlFavoriteFormula);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.gbCurrent);
this.Controls.Add(this.btnOK);
this.Name = "FormulaManager";
this.gbCurrent.ResumeLayout(false);
this.ResumeLayout(false);
}
private void lbOverlayList_SelectedIndexChanged(object sender, EventArgs e)
{
if (!this.DisableChangeEvent)
{
string str;
this.ParamToTextBox((string)this.lbFormulaList.SelectedItem, out str);
}
}
private void MoveFormula(int Delta)
{
this.DisableChangeEvent = true;
try
{
int selectedIndex = this.lbFormulaList.SelectedIndex;
int num2 = selectedIndex + Delta;
if (((selectedIndex >= 0) && (num2 >= 0)) && (num2 < this.lbFormulaList.Items.Count))
{
object item = this.lbFormulaList.Items[selectedIndex];
this.lbFormulaList.Items.RemoveAt(selectedIndex);
this.lbFormulaList.Items.Insert(selectedIndex + Delta, item);
this.lbFormulaList.SelectedIndex = num2;
}
}
finally
{
this.DisableChangeEvent = false;
}
}
public void ParamToTextBox(string NameAndParam, out string FormulaName)
{
this.cbSecondYAxis.Checked = NameAndParam.EndsWith("!");
FormulaBase formulaByName = FormulaBase.GetFormulaByName(NameAndParam.TrimEnd(new char[] { '!' }));
FormulaName = formulaByName.FormulaName;
SelectFormula.AddParamToGroupBox(this.gbParam, this.ilFormula, formulaByName, new EventHandler(this.tbP4_Leave));
}
private void SelectFirstFormula()
{
this.SetSelectFormula(0);
}
private void SelectLastFormula()
{
this.SetSelectFormula(this.lbFormulaList.Items.Count - 1);
}
private void SetSelectFormula(int Index)
{
if (Index < 0)
{
Index = 0;
}
if (Index >= this.lbFormulaList.Items.Count)
{
Index = this.lbFormulaList.Items.Count - 1;
}
if ((Index >= 0) && (Index < this.lbFormulaList.Items.Count))
{
this.lbFormulaList.SelectedIndex = Index;
}
}
public DialogResult ShowForm()
{
this.AddFavorite(this.ListedFormulas);
return this.ShowForm(null, string.Empty);
}
///
/// 弹出编辑当前公式对话框
///
/// 当前图表用到的公式集
/// 当前选中图表的应用公式
/// 返回对话框
public DialogResult ShowForm(string[] formulas, string selectedFormula)
{
if (formulas != null)
{
string[] strArray = formulas;
if (strArray != null)
{
this.CurrentFormulas = string.Join("#", strArray);
string createName = "";
if (selectedFormula != null)
{
createName = selectedFormula;
}
this.SelectedFormula = createName;
ArrayList list = new ArrayList();
list.AddRange(strArray);
//以下稍作修改,合并2个常用的公式供一般性选择
list.AddRange(CombinationArray(this.OverlayFormulas, this.ListedFormulas));
//if (fa.IsMain()) //是否为基本主要的。这个函数需要界面数据,暂时不作实现
//{
// list.AddRange(this.OverlayFormulas);
//}
//else
//{
// list.AddRange(this.ListedFormulas);
//}
this.AddFavorite((string[])list.ToArray(typeof(string)));
}
}
return base.ShowDialog();
}
public DialogResult ShowForm(FormulaArea fa, FormulaBase SelectedFormula)
{
if (fa != null)
{
string[] strArray = fa.FormulaToStrings();
if (strArray != null)
{
this.CurrentFormulas = string.Join("#", strArray);
string createName = "";
if (SelectedFormula != null)
{
createName = SelectedFormula.CreateName;
}
this.SelectedFormula = createName;
ArrayList list = new ArrayList();
list.AddRange(strArray);
if (fa.IsMain())
{
list.AddRange(this.OverlayFormulas);
}
else
{
list.AddRange(this.ListedFormulas);
}
this.AddFavorite((string[])list.ToArray(typeof(string)));
}
}
return base.ShowDialog();
}
///
/// 合并两组字符串
///
/// 字符串1
/// 字符串2
/// 返回合并后字符串
private string[] CombinationArray(string[] array1, string[] array2)
{
List comArray = new List();
comArray.AddRange(array1);
foreach (string str in array2)
{
if (!comArray.Contains(str))
{
comArray.Add(str);
}
}
return comArray.ToArray();
}
public bool IsMain(int AxisYIndex)
{
//以下主要判断数据的类型
//for (int i = 0; i < this.FormulaDataArray.Count; i++)
//{
// if ((((this[i].RenderType == FormulaRenderType.STOCK) || (this[i].RenderType == FormulaRenderType.MONOSTOCK)) || (string.Compare(this[i].Name, "Main", true) == 0)) && ((AxisYIndex < 0) || (this[i].AxisYIndex == AxisYIndex)))
// {
// return true;
// }
//}
//return false;
return true;
}
private void tbP4_Leave(object sender, EventArgs e)
{
string str = this.TextBoxToParam((string)this.lbFormulaList.SelectedItem);
if (str != "")
{
this.DisableChangeEvent = true;
try
{
this.lbFormulaList.Items[this.lbFormulaList.SelectedIndex] = str;
}
finally
{
this.DisableChangeEvent = false;
}
}
}
private string TextBoxToParam(string FormulaName)
{
int index = FormulaName.IndexOf('(');
if (index >= 0)
{
FormulaName = FormulaName.Substring(0, index);
}
string param = SelectFormula.GetParam(this.gbParam);
if (FormulaBase.GetFormulaByName(FormulaName) != null)
{
return (FormulaName + param);
}
return "";
}
public string CurrentFormulas
{
get
{
string str = "";
foreach (string str2 in this.lbFormulaList.Items)
{
if (str != "")
{
str = str + "#";
}
str = str + str2;
}
return str;
}
set
{
this.lbFormulaList.Items.Clear();
if ((value != "") && (value != null))
{
foreach (string str in value.Split(new char[] { '#' }))
{
this.AddFormulas(str);
}
this.SelectFirstFormula();
}
}
}
public string SelectedFormula
{
get
{
if (this.lbFormulaList.SelectedIndex > 0)
{
return this.lbFormulaList.Items[this.lbFormulaList.SelectedIndex].ToString();
}
return "";
}
set
{
int index = this.lbFormulaList.Items.IndexOf(value);
if (index >= 0)
{
this.lbFormulaList.SelectedIndex = index;
}
}
}
}
}