SelectFormula.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. namespace IndexFormula.Finance.Win
  2. {
  3. using IndexFormula.Finance;
  4. using IndexFormula.Finance.DataProvider;
  5. using System;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.Resources;
  9. using System.Windows.Forms;
  10. [ToolboxItem(false)]
  11. public class SelectFormula : Form
  12. {
  13. private Button btnCancel;
  14. private Button btnEdit;
  15. private Button btnOK;
  16. private IContainer components;
  17. private static int Delta;
  18. private string[] FilterPrefixes;
  19. private GroupBox gbParam;
  20. public ImageList ilFormula;
  21. private ListBox lbLines;
  22. private Label lFullName;
  23. private const int OneDelta = 20;
  24. private Panel pnClient;
  25. private string Result;
  26. private bool SelectLine;
  27. private Splitter sp1;
  28. private TextBox tbDesc;
  29. private TreeView tvFormula;
  30. public SelectFormula()
  31. {
  32. this.InitializeComponent();
  33. }
  34. public static void AddParamToGroupBox(GroupBox gbParam, ImageList ilFormula, FormulaBase fb, EventHandler ehLeave)
  35. {
  36. gbParam.Controls.Clear();
  37. if (fb != null)
  38. {
  39. int num = 10;
  40. int top = 20;
  41. int num3 = 30;
  42. int num4 = 0;
  43. for (int i = 0; i < fb.Params.Count; i++)
  44. {
  45. Label label = new Label();
  46. label.AutoSize = true;
  47. label.Text = fb.Params[i].Name + "=";
  48. label.Left = num;
  49. label.Top = top + 3;
  50. label.Parent = gbParam;
  51. num4 = Math.Max(num4, label.Width);
  52. top += num3;
  53. }
  54. top = 20;
  55. num += num4 + 6;
  56. for (int j = 0; j < fb.Params.Count; j++)
  57. {
  58. FormulaParam param = fb.Params[j];
  59. TextBox tb = new TextBox();
  60. tb.Left = num;
  61. tb.Top = top;
  62. tb.Text = param.Value;
  63. tb.Parent = gbParam;
  64. tb.Leave += ehLeave;
  65. int num7 = (num + tb.Width) + 6;
  66. if (param.ParamType == FormulaParamType.Double)
  67. {
  68. Label label2 = new Label();
  69. label2.AutoSize = true;
  70. label2.Text = "(" + param.MinValue + "--" + param.MaxValue + ")";
  71. label2.Left = num7;
  72. label2.Top = top + 3;
  73. label2.Parent = gbParam;
  74. num7 += label2.Right;
  75. }
  76. switch (fb.Params[j].ParamType)
  77. {
  78. case FormulaParamType.Symbol:
  79. CreateSelectButton(gbParam, ilFormula, num7 + 6, top, 2, tb, new EventHandler(SelectFormula.SelectSymbol_Click));
  80. break;
  81. case FormulaParamType.Indicator:
  82. CreateSelectButton(gbParam, ilFormula, num7 + 6, top, 1, tb, new EventHandler(SelectFormula.SelectFormula_Click));
  83. break;
  84. }
  85. top += num3;
  86. }
  87. }
  88. }
  89. private void btnEdit_Click(object sender, EventArgs e)
  90. {
  91. //if ((this.tvFormula.SelectedNode != null) && (this.tvFormula.SelectedNode.Tag != null))
  92. //{
  93. // ChartWinControl.EditFormula((FormulaBase) this.tvFormula.SelectedNode.Tag);
  94. //}
  95. }
  96. private void btnOK_Click(object sender, EventArgs e)
  97. {
  98. TreeNode selectedNode = this.tvFormula.SelectedNode;
  99. this.Result = null;
  100. if (selectedNode != null)
  101. {
  102. FormulaBase tag = (FormulaBase) selectedNode.Tag;
  103. if (tag != null)
  104. {
  105. this.Result = tag.TypeName;
  106. if (this.Result != null)
  107. {
  108. this.Result = this.Result + GetParam(this.gbParam);
  109. }
  110. if (this.SelectLine)
  111. {
  112. string selectedItem = (string) this.lbLines.SelectedItem;
  113. if (selectedItem != null)
  114. {
  115. if (!selectedItem.StartsWith("["))
  116. {
  117. selectedItem = "[" + selectedItem + "]";
  118. }
  119. this.Result = this.Result + selectedItem;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. private void CreateNode(FormulaBase fb)
  126. {
  127. int num;
  128. string str = fb.GetType().ToString();
  129. if (str.StartsWith("FML."))
  130. {
  131. str = str.Substring(4);
  132. }
  133. if (str.IndexOf('.') < 0)
  134. {
  135. str = "Basic." + str;
  136. }
  137. TreeNode node = this.tvFormula.Nodes[0];
  138. while ((num = str.IndexOf('.')) > 0)
  139. {
  140. string text = str.Substring(0, num);
  141. str = str.Substring(num + 1);
  142. TreeNode node2 = null;
  143. for (int i = 0; i < node.Nodes.Count; i++)
  144. {
  145. if (node.Nodes[i].Text == text)
  146. {
  147. node2 = node.Nodes[i];
  148. break;
  149. }
  150. }
  151. if (node2 == null)
  152. {
  153. node.Nodes.Add(node2 = new TreeNode(text, 0, 0));
  154. }
  155. node = node2;
  156. }
  157. TreeNode node3 = new TreeNode(fb.CombineName, 1, 1);
  158. node3.Tag = fb;
  159. node.Nodes.Add(node3);
  160. }
  161. private static void CreateSelectButton(GroupBox gbParam, ImageList ilFormula, int Left, int Top, int ImageIndex, TextBox tb, EventHandler Click)
  162. {
  163. Button button = new Button();
  164. button.Width = 20;
  165. button.Height = 20;
  166. button.FlatStyle = FlatStyle.Popup;
  167. button.Left = Left;
  168. button.Top = Top;
  169. button.ImageList = ilFormula;
  170. button.ImageIndex = ImageIndex;
  171. button.Parent = gbParam;
  172. button.Click += Click;
  173. button.Tag = tb;
  174. }
  175. protected override void Dispose(bool disposing)
  176. {
  177. if (disposing && (this.components != null))
  178. {
  179. this.components.Dispose();
  180. }
  181. base.Dispose(disposing);
  182. }
  183. public static string GetParam(GroupBox gbParam)
  184. {
  185. string str = "";
  186. foreach (Control control in gbParam.Controls)
  187. {
  188. if (control is TextBox)
  189. {
  190. if (str != "")
  191. {
  192. str = str + ",";
  193. }
  194. string text = (control as TextBox).Text;
  195. if (text.IndexOf(',') >= 0)
  196. {
  197. text = "\"" + text + "\"";
  198. }
  199. str = str + text;
  200. }
  201. }
  202. return ("(" + str + ")");
  203. }
  204. private void InitializeComponent()
  205. {
  206. this.components = new System.ComponentModel.Container();
  207. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SelectFormula));
  208. this.tvFormula = new System.Windows.Forms.TreeView();
  209. this.ilFormula = new System.Windows.Forms.ImageList(this.components);
  210. this.sp1 = new System.Windows.Forms.Splitter();
  211. this.pnClient = new System.Windows.Forms.Panel();
  212. this.btnEdit = new System.Windows.Forms.Button();
  213. this.lbLines = new System.Windows.Forms.ListBox();
  214. this.tbDesc = new System.Windows.Forms.TextBox();
  215. this.lFullName = new System.Windows.Forms.Label();
  216. this.gbParam = new System.Windows.Forms.GroupBox();
  217. this.btnOK = new System.Windows.Forms.Button();
  218. this.btnCancel = new System.Windows.Forms.Button();
  219. this.pnClient.SuspendLayout();
  220. this.SuspendLayout();
  221. //
  222. // tvFormula
  223. //
  224. this.tvFormula.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  225. resources.ApplyResources(this.tvFormula, "tvFormula");
  226. this.tvFormula.FullRowSelect = true;
  227. this.tvFormula.HideSelection = false;
  228. this.tvFormula.Name = "tvFormula";
  229. this.tvFormula.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvFormula_AfterSelect);
  230. this.tvFormula.DoubleClick += new System.EventHandler(this.tvFormula_DoubleClick);
  231. //
  232. // ilFormula
  233. //
  234. this.ilFormula.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ilFormula.ImageStream")));
  235. this.ilFormula.TransparentColor = System.Drawing.Color.Transparent;
  236. this.ilFormula.Images.SetKeyName(0, "");
  237. this.ilFormula.Images.SetKeyName(1, "");
  238. this.ilFormula.Images.SetKeyName(2, "");
  239. //
  240. // sp1
  241. //
  242. resources.ApplyResources(this.sp1, "sp1");
  243. this.sp1.Name = "sp1";
  244. this.sp1.TabStop = false;
  245. //
  246. // pnClient
  247. //
  248. this.pnClient.Controls.Add(this.btnEdit);
  249. this.pnClient.Controls.Add(this.lbLines);
  250. this.pnClient.Controls.Add(this.tbDesc);
  251. this.pnClient.Controls.Add(this.lFullName);
  252. this.pnClient.Controls.Add(this.gbParam);
  253. this.pnClient.Controls.Add(this.btnOK);
  254. this.pnClient.Controls.Add(this.btnCancel);
  255. resources.ApplyResources(this.pnClient, "pnClient");
  256. this.pnClient.Name = "pnClient";
  257. //
  258. // btnEdit
  259. //
  260. resources.ApplyResources(this.btnEdit, "btnEdit");
  261. this.btnEdit.Name = "btnEdit";
  262. this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
  263. //
  264. // lbLines
  265. //
  266. resources.ApplyResources(this.lbLines, "lbLines");
  267. this.lbLines.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  268. this.lbLines.Name = "lbLines";
  269. this.lbLines.DoubleClick += new System.EventHandler(this.lbLines_DoubleClick);
  270. //
  271. // tbDesc
  272. //
  273. resources.ApplyResources(this.tbDesc, "tbDesc");
  274. this.tbDesc.BackColor = System.Drawing.Color.Beige;
  275. this.tbDesc.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  276. this.tbDesc.Name = "tbDesc";
  277. this.tbDesc.ReadOnly = true;
  278. //
  279. // lFullName
  280. //
  281. this.lFullName.ForeColor = System.Drawing.Color.Blue;
  282. resources.ApplyResources(this.lFullName, "lFullName");
  283. this.lFullName.Name = "lFullName";
  284. //
  285. // gbParam
  286. //
  287. resources.ApplyResources(this.gbParam, "gbParam");
  288. this.gbParam.Name = "gbParam";
  289. this.gbParam.TabStop = false;
  290. //
  291. // btnOK
  292. //
  293. resources.ApplyResources(this.btnOK, "btnOK");
  294. this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
  295. this.btnOK.Name = "btnOK";
  296. this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
  297. //
  298. // btnCancel
  299. //
  300. resources.ApplyResources(this.btnCancel, "btnCancel");
  301. this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  302. this.btnCancel.Name = "btnCancel";
  303. //
  304. // SelectFormula
  305. //
  306. this.AcceptButton = this.btnOK;
  307. resources.ApplyResources(this, "$this");
  308. this.CancelButton = this.btnCancel;
  309. this.Controls.Add(this.sp1);
  310. this.Controls.Add(this.pnClient);
  311. this.Controls.Add(this.tvFormula);
  312. this.Name = "SelectFormula";
  313. this.Closed += new System.EventHandler(this.SelectFormula_Closed);
  314. this.Load += new System.EventHandler(this.SelectFormula_Load);
  315. this.pnClient.ResumeLayout(false);
  316. this.pnClient.PerformLayout();
  317. this.ResumeLayout(false);
  318. }
  319. private void lbLines_DoubleClick(object sender, EventArgs e)
  320. {
  321. this.btnOK.PerformClick();
  322. }
  323. private void RefreshTree()
  324. {
  325. this.tvFormula.BeginUpdate();
  326. try
  327. {
  328. this.tvFormula.ImageList = this.ilFormula;
  329. this.tvFormula.Nodes.Clear();
  330. this.tvFormula.Nodes.Add("Root");
  331. this.tvFormula.Nodes[0].Nodes.Add("Basic");
  332. foreach (FormulaBase base2 in FormulaBase.GetAllFormulas())
  333. {
  334. this.CreateNode(base2);
  335. }
  336. this.tvFormula.Nodes[0].Expand();
  337. this.tvFormula.Nodes[0].Nodes[0].Expand();
  338. this.gbParam.Controls.Clear();
  339. if (this.tvFormula.Nodes[0].Nodes[0].Nodes.Count > 0)
  340. {
  341. this.tvFormula.SelectedNode = this.tvFormula.Nodes[0].Nodes[0].Nodes[0];
  342. }
  343. }
  344. catch (Exception ex)
  345. {
  346. MessageBox.Show(ex.Message);
  347. }
  348. finally
  349. {
  350. this.tvFormula.EndUpdate();
  351. }
  352. }
  353. public string Select(string Default, string[] FilterPrefixes, bool SelectLine)
  354. {
  355. if (FilterPrefixes != null)
  356. {
  357. for (int i = 0; i < FilterPrefixes.Length; i++)
  358. {
  359. FilterPrefixes[i] = FilterPrefixes[i].ToUpper();
  360. }
  361. }
  362. this.SelectLine = SelectLine;
  363. this.FilterPrefixes = FilterPrefixes;
  364. if (base.ShowDialog() == DialogResult.OK)
  365. {
  366. return this.Result;
  367. }
  368. return null;
  369. }
  370. private static void SelectFormula_Click(object sender, EventArgs e)
  371. {
  372. TextBox tag = (TextBox) ((Button) sender).Tag;
  373. //tag.Text = ChartWinControl.DoSelectFormula(tag.Text, null, true);
  374. tag.Focus();
  375. }
  376. private void SelectFormula_Closed(object sender, EventArgs e)
  377. {
  378. Delta -= 20;
  379. }
  380. private void SelectFormula_Load(object sender, EventArgs e)
  381. {
  382. base.Location = new Point(base.Left + Delta, base.Top + Delta);
  383. Delta += 20;
  384. this.btnEdit.Enabled = !FormulaSourceEditor.EditorVisible;
  385. this.lbLines.Visible = this.SelectLine;
  386. this.RefreshTree();
  387. }
  388. private static void SelectSymbol_Click(object sender, EventArgs e)
  389. {
  390. TextBox tag = (TextBox) ((Button) sender).Tag;
  391. //tag.Text = ChartWinControl.DoSelectSymbol(tag.Text);
  392. tag.Focus();
  393. }
  394. private void tvFormula_AfterSelect(object sender, TreeViewEventArgs e)
  395. {
  396. FormulaBase tag = (FormulaBase) e.Node.Tag;
  397. this.lbLines.Items.Clear();
  398. if (tag != null)
  399. {
  400. this.lFullName.Text = tag.LongName;
  401. this.tbDesc.Text = tag.Description;
  402. try
  403. {
  404. FormulaPackage package = tag.Run(CommonDataProvider.Empty);
  405. for (int i = 0; i < package.Count; i++)
  406. {
  407. FormulaData data = package[i];
  408. if (data.Name == null)
  409. {
  410. this.lbLines.Items.Add("[" + i + "]");
  411. }
  412. else
  413. {
  414. this.lbLines.Items.Add(data.Name);
  415. }
  416. }
  417. }
  418. catch
  419. {
  420. }
  421. }
  422. AddParamToGroupBox(this.gbParam, this.ilFormula, tag, null);
  423. }
  424. private void tvFormula_DoubleClick(object sender, EventArgs e)
  425. {
  426. this.btnOK.PerformClick();
  427. }
  428. }
  429. }