namespace IndexFormula.Finance.Win { using IndexFormula.Finance; using IndexFormula.Finance.DataProvider; using System; using System.ComponentModel; using System.Drawing; using System.Resources; using System.Windows.Forms; [ToolboxItem(false)] public class SelectFormula : Form { private Button btnCancel; private Button btnEdit; private Button btnOK; private IContainer components; private static int Delta; private string[] FilterPrefixes; private GroupBox gbParam; public ImageList ilFormula; private ListBox lbLines; private Label lFullName; private const int OneDelta = 20; private Panel pnClient; private string Result; private bool SelectLine; private Splitter sp1; private TextBox tbDesc; private TreeView tvFormula; public SelectFormula() { this.InitializeComponent(); } public static void AddParamToGroupBox(GroupBox gbParam, ImageList ilFormula, FormulaBase fb, EventHandler ehLeave) { gbParam.Controls.Clear(); if (fb != null) { int num = 10; int top = 20; int num3 = 30; int num4 = 0; for (int i = 0; i < fb.Params.Count; i++) { Label label = new Label(); label.AutoSize = true; label.Text = fb.Params[i].Name + "="; label.Left = num; label.Top = top + 3; label.Parent = gbParam; num4 = Math.Max(num4, label.Width); top += num3; } top = 20; num += num4 + 6; for (int j = 0; j < fb.Params.Count; j++) { FormulaParam param = fb.Params[j]; TextBox tb = new TextBox(); tb.Left = num; tb.Top = top; tb.Text = param.Value; tb.Parent = gbParam; tb.Leave += ehLeave; int num7 = (num + tb.Width) + 6; if (param.ParamType == FormulaParamType.Double) { Label label2 = new Label(); label2.AutoSize = true; label2.Text = "(" + param.MinValue + "--" + param.MaxValue + ")"; label2.Left = num7; label2.Top = top + 3; label2.Parent = gbParam; num7 += label2.Right; } switch (fb.Params[j].ParamType) { case FormulaParamType.Symbol: CreateSelectButton(gbParam, ilFormula, num7 + 6, top, 2, tb, new EventHandler(SelectFormula.SelectSymbol_Click)); break; case FormulaParamType.Indicator: CreateSelectButton(gbParam, ilFormula, num7 + 6, top, 1, tb, new EventHandler(SelectFormula.SelectFormula_Click)); break; } top += num3; } } } private void btnEdit_Click(object sender, EventArgs e) { //if ((this.tvFormula.SelectedNode != null) && (this.tvFormula.SelectedNode.Tag != null)) //{ // ChartWinControl.EditFormula((FormulaBase) this.tvFormula.SelectedNode.Tag); //} } private void btnOK_Click(object sender, EventArgs e) { TreeNode selectedNode = this.tvFormula.SelectedNode; this.Result = null; if (selectedNode != null) { FormulaBase tag = (FormulaBase) selectedNode.Tag; if (tag != null) { this.Result = tag.TypeName; if (this.Result != null) { this.Result = this.Result + GetParam(this.gbParam); } if (this.SelectLine) { string selectedItem = (string) this.lbLines.SelectedItem; if (selectedItem != null) { if (!selectedItem.StartsWith("[")) { selectedItem = "[" + selectedItem + "]"; } this.Result = this.Result + selectedItem; } } } } } private void CreateNode(FormulaBase fb) { int num; string str = fb.GetType().ToString(); if (str.StartsWith("FML.")) { str = str.Substring(4); } if (str.IndexOf('.') < 0) { str = "Basic." + str; } TreeNode node = this.tvFormula.Nodes[0]; while ((num = str.IndexOf('.')) > 0) { string text = str.Substring(0, num); str = str.Substring(num + 1); TreeNode node2 = null; for (int i = 0; i < node.Nodes.Count; i++) { if (node.Nodes[i].Text == text) { node2 = node.Nodes[i]; break; } } if (node2 == null) { node.Nodes.Add(node2 = new TreeNode(text, 0, 0)); } node = node2; } TreeNode node3 = new TreeNode(fb.CombineName, 1, 1); node3.Tag = fb; node.Nodes.Add(node3); } private static void CreateSelectButton(GroupBox gbParam, ImageList ilFormula, int Left, int Top, int ImageIndex, TextBox tb, EventHandler Click) { Button button = new Button(); button.Width = 20; button.Height = 20; button.FlatStyle = FlatStyle.Popup; button.Left = Left; button.Top = Top; button.ImageList = ilFormula; button.ImageIndex = ImageIndex; button.Parent = gbParam; button.Click += Click; button.Tag = tb; } protected override void Dispose(bool disposing) { if (disposing && (this.components != null)) { this.components.Dispose(); } base.Dispose(disposing); } public static string GetParam(GroupBox gbParam) { string str = ""; foreach (Control control in gbParam.Controls) { if (control is TextBox) { if (str != "") { str = str + ","; } string text = (control as TextBox).Text; if (text.IndexOf(',') >= 0) { text = "\"" + text + "\""; } str = str + text; } } return ("(" + str + ")"); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SelectFormula)); this.tvFormula = new System.Windows.Forms.TreeView(); this.ilFormula = new System.Windows.Forms.ImageList(this.components); this.sp1 = new System.Windows.Forms.Splitter(); this.pnClient = new System.Windows.Forms.Panel(); this.btnEdit = new System.Windows.Forms.Button(); this.lbLines = new System.Windows.Forms.ListBox(); this.tbDesc = new System.Windows.Forms.TextBox(); this.lFullName = new System.Windows.Forms.Label(); this.gbParam = new System.Windows.Forms.GroupBox(); this.btnOK = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); this.pnClient.SuspendLayout(); this.SuspendLayout(); // // tvFormula // this.tvFormula.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; resources.ApplyResources(this.tvFormula, "tvFormula"); this.tvFormula.FullRowSelect = true; this.tvFormula.HideSelection = false; this.tvFormula.Name = "tvFormula"; this.tvFormula.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvFormula_AfterSelect); this.tvFormula.DoubleClick += new System.EventHandler(this.tvFormula_DoubleClick); // // ilFormula // this.ilFormula.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilFormula.ImageStream"))); this.ilFormula.TransparentColor = System.Drawing.Color.Transparent; this.ilFormula.Images.SetKeyName(0, ""); this.ilFormula.Images.SetKeyName(1, ""); this.ilFormula.Images.SetKeyName(2, ""); // // sp1 // resources.ApplyResources(this.sp1, "sp1"); this.sp1.Name = "sp1"; this.sp1.TabStop = false; // // pnClient // this.pnClient.Controls.Add(this.btnEdit); this.pnClient.Controls.Add(this.lbLines); this.pnClient.Controls.Add(this.tbDesc); this.pnClient.Controls.Add(this.lFullName); this.pnClient.Controls.Add(this.gbParam); this.pnClient.Controls.Add(this.btnOK); this.pnClient.Controls.Add(this.btnCancel); resources.ApplyResources(this.pnClient, "pnClient"); this.pnClient.Name = "pnClient"; // // btnEdit // resources.ApplyResources(this.btnEdit, "btnEdit"); this.btnEdit.Name = "btnEdit"; this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click); // // lbLines // resources.ApplyResources(this.lbLines, "lbLines"); this.lbLines.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lbLines.Name = "lbLines"; this.lbLines.DoubleClick += new System.EventHandler(this.lbLines_DoubleClick); // // tbDesc // resources.ApplyResources(this.tbDesc, "tbDesc"); this.tbDesc.BackColor = System.Drawing.Color.Beige; this.tbDesc.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.tbDesc.Name = "tbDesc"; this.tbDesc.ReadOnly = true; // // lFullName // this.lFullName.ForeColor = System.Drawing.Color.Blue; resources.ApplyResources(this.lFullName, "lFullName"); this.lFullName.Name = "lFullName"; // // gbParam // resources.ApplyResources(this.gbParam, "gbParam"); this.gbParam.Name = "gbParam"; this.gbParam.TabStop = false; // // btnOK // resources.ApplyResources(this.btnOK, "btnOK"); this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; this.btnOK.Name = "btnOK"; this.btnOK.Click += new System.EventHandler(this.btnOK_Click); // // btnCancel // resources.ApplyResources(this.btnCancel, "btnCancel"); this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.Name = "btnCancel"; // // SelectFormula // this.AcceptButton = this.btnOK; resources.ApplyResources(this, "$this"); this.CancelButton = this.btnCancel; this.Controls.Add(this.sp1); this.Controls.Add(this.pnClient); this.Controls.Add(this.tvFormula); this.Name = "SelectFormula"; this.Closed += new System.EventHandler(this.SelectFormula_Closed); this.Load += new System.EventHandler(this.SelectFormula_Load); this.pnClient.ResumeLayout(false); this.pnClient.PerformLayout(); this.ResumeLayout(false); } private void lbLines_DoubleClick(object sender, EventArgs e) { this.btnOK.PerformClick(); } private void RefreshTree() { this.tvFormula.BeginUpdate(); try { this.tvFormula.ImageList = this.ilFormula; this.tvFormula.Nodes.Clear(); this.tvFormula.Nodes.Add("Root"); this.tvFormula.Nodes[0].Nodes.Add("Basic"); foreach (FormulaBase base2 in FormulaBase.GetAllFormulas()) { this.CreateNode(base2); } this.tvFormula.Nodes[0].Expand(); this.tvFormula.Nodes[0].Nodes[0].Expand(); this.gbParam.Controls.Clear(); if (this.tvFormula.Nodes[0].Nodes[0].Nodes.Count > 0) { this.tvFormula.SelectedNode = this.tvFormula.Nodes[0].Nodes[0].Nodes[0]; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { this.tvFormula.EndUpdate(); } } public string Select(string Default, string[] FilterPrefixes, bool SelectLine) { if (FilterPrefixes != null) { for (int i = 0; i < FilterPrefixes.Length; i++) { FilterPrefixes[i] = FilterPrefixes[i].ToUpper(); } } this.SelectLine = SelectLine; this.FilterPrefixes = FilterPrefixes; if (base.ShowDialog() == DialogResult.OK) { return this.Result; } return null; } private static void SelectFormula_Click(object sender, EventArgs e) { TextBox tag = (TextBox) ((Button) sender).Tag; //tag.Text = ChartWinControl.DoSelectFormula(tag.Text, null, true); tag.Focus(); } private void SelectFormula_Closed(object sender, EventArgs e) { Delta -= 20; } private void SelectFormula_Load(object sender, EventArgs e) { base.Location = new Point(base.Left + Delta, base.Top + Delta); Delta += 20; this.btnEdit.Enabled = !FormulaSourceEditor.EditorVisible; this.lbLines.Visible = this.SelectLine; this.RefreshTree(); } private static void SelectSymbol_Click(object sender, EventArgs e) { TextBox tag = (TextBox) ((Button) sender).Tag; //tag.Text = ChartWinControl.DoSelectSymbol(tag.Text); tag.Focus(); } private void tvFormula_AfterSelect(object sender, TreeViewEventArgs e) { FormulaBase tag = (FormulaBase) e.Node.Tag; this.lbLines.Items.Clear(); if (tag != null) { this.lFullName.Text = tag.LongName; this.tbDesc.Text = tag.Description; try { FormulaPackage package = tag.Run(CommonDataProvider.Empty); for (int i = 0; i < package.Count; i++) { FormulaData data = package[i]; if (data.Name == null) { this.lbLines.Items.Add("[" + i + "]"); } else { this.lbLines.Items.Add(data.Name); } } } catch { } } AddParamToGroupBox(this.gbParam, this.ilFormula, tag, null); } private void tvFormula_DoubleClick(object sender, EventArgs e) { this.btnOK.PerformClick(); } } }