CompletionForm.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using EsPy.Python.Jedi;
  2. using ScintillaNET;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace EsPy.Components
  13. {
  14. public partial class CompletionForm : Form
  15. {
  16. public event EventHandler FormDisposing = null;
  17. public CompletionForm()
  18. {
  19. InitializeComponent();
  20. //this.Capture = true;
  21. this.listBox.SelectedIndexChanged += ListBox_SelectedIndexChanged;
  22. }
  23. protected override bool ShowWithoutActivation
  24. {
  25. get { return true; }
  26. }
  27. protected override CreateParams CreateParams
  28. {
  29. get
  30. {
  31. CreateParams baseParams = base.CreateParams;
  32. const int WS_EX_NOACTIVATE = 0x08000000;
  33. const int WS_EX_TOOLWINDOW = 0x00000080;
  34. baseParams.ExStyle |= (int)(WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
  35. return baseParams;
  36. }
  37. }
  38. protected override void Dispose(bool disposing)
  39. {
  40. if (this.ToolTip != null)
  41. {
  42. this.ToolTip.Dispose();
  43. }
  44. Globals.MainForm.Deactivate -= MainForm_Deactivate;
  45. Globals.MainForm.Resize -= MainForm_Resize;
  46. if (this.FormDisposing != null)
  47. this.FormDisposing(this, new EventArgs());
  48. if (disposing && (components != null))
  49. {
  50. components.Dispose();
  51. }
  52. base.Dispose(disposing);
  53. }
  54. private Scintilla FScintilla = null;
  55. public Scintilla Scintilla
  56. {
  57. get { return this.FScintilla; }
  58. set
  59. {
  60. this.FScintilla = value;
  61. if (this.FScintilla != null)
  62. {
  63. this.listBox.Font = this.Scintilla.Font;
  64. }
  65. }
  66. }
  67. public void Clear()
  68. {
  69. this.listBox.Items.Clear();
  70. }
  71. public void Add(Completion item)
  72. {
  73. this.listBox.Items.Add(item);
  74. }
  75. public void AddRange(List<Completion> list)
  76. {
  77. this.listBox.Items.AddRange(list.ToArray());
  78. }
  79. public int Count
  80. { get { return this.listBox.Items.Count; } }
  81. public int SelectedIndex
  82. {
  83. get { return this.listBox.SelectedIndex; }
  84. set {
  85. if (this.ToolTip != null)
  86. {
  87. this.ToolTip.Dispose();
  88. this.ToolTip = null;
  89. }
  90. this.listBox.SelectedIndex = value;
  91. }
  92. }
  93. //public void Home()
  94. //{
  95. // this.SelectedIndex = 0;
  96. //}
  97. //public void End()
  98. //{
  99. // this.SelectedIndex = this.listBox.Items.Count - 1;
  100. //}
  101. public BaseDefinition SelectedItem
  102. { get { return this.listBox.SelectedItem as BaseDefinition; } }
  103. public void SelectPrevious()
  104. {
  105. if (this.SelectedIndex < 0)
  106. {
  107. this.SelectedIndex = 0;
  108. }
  109. else if (this.SelectedIndex > 0)
  110. {
  111. this.SelectedIndex--;
  112. }
  113. }
  114. public void SelectNext()
  115. {
  116. if (this.SelectedIndex < 0)
  117. {
  118. this.SelectedIndex = 0;
  119. }
  120. else if (this.SelectedIndex < this.listBox.Items.Count - 1)
  121. {
  122. this.SelectedIndex++;
  123. }
  124. }
  125. //public void PageUp()
  126. //{
  127. //}
  128. //public void PageDown()
  129. //{
  130. //}
  131. private ToolTip ToolTip = null;
  132. private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
  133. {
  134. if (this.listBox.SelectedItem != null)
  135. {
  136. BaseDefinition def = (this.listBox.SelectedItem as BaseDefinition);
  137. string doc = string.IsNullOrEmpty(def.docstring) ? "No documentation" : def.docstring;
  138. string module = string.IsNullOrEmpty(def.module_name) ? "" : "module: " + def.module_name + "\r\n";
  139. string type = string.IsNullOrEmpty(def.type) ? "" : def.type + " ";
  140. string parm = def.parameters != null ? "(" + string.Join(", ", def.parameters) + ")" : "";
  141. string title = type + def.name + parm;
  142. if (ToolTip == null)
  143. {
  144. //ToolTip.Hide(this);
  145. //ToolTip.Dispose();
  146. this.ToolTip = new ToolTip();
  147. }
  148. //ToolTip = new ToolTip();
  149. if (!ToolTip.Equals(ToolTip.Tag))
  150. {
  151. ToolTip.ShowAlways = true;
  152. ToolTip.ToolTipTitle = title;
  153. Point p = this.Scintilla.PointToClient(new Point(this.Right + 2, this.Top + 2));
  154. ToolTip.Tag = this.listBox.SelectedItem;
  155. ToolTip.Show(module + doc, this.Scintilla, p);
  156. }
  157. }
  158. else if (this.ToolTip != null)
  159. {
  160. this.ToolTip.Dispose();
  161. this.ToolTip = null;
  162. }
  163. }
  164. //private void listBox_MouseDown(object sender, MouseEventArgs e)
  165. //{
  166. //}
  167. private void CompletionForm_Shown(object sender, EventArgs e)
  168. {
  169. Globals.MainForm.Deactivate += MainForm_Deactivate;
  170. Globals.MainForm.Resize += MainForm_Resize;
  171. }
  172. private void MainForm_Resize(object sender, EventArgs e)
  173. {
  174. this.Close();
  175. }
  176. private void MainForm_Deactivate(object sender, EventArgs e)
  177. {
  178. this.Close();
  179. }
  180. private void CompletionForm_Load(object sender, EventArgs e)
  181. {
  182. this.Height = this.listBox.Height + 2;
  183. }
  184. }
  185. }