MirDropDownBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using Client.MirGraphics;
  2. namespace Client.MirControls
  3. {
  4. public class MirDropDownBox : MirControl
  5. {
  6. private MirLabel _label;
  7. private MirButton _DropDownButton;
  8. private MirLabel[] _Option = new MirLabel[5];
  9. private MirButton _ScrollUp, _ScrollDown, _ScrollPosition;
  10. public int _SelectedIndex = -1;
  11. public int _WantedIndex = -1;
  12. public int MinimumOption = 0;
  13. public int ScrollIndex = 0;
  14. public int OrigHeight = 0;
  15. public long LastMouseLeave = 0;
  16. private List<String> _Items = new List<string>();
  17. public event EventHandler ValueChanged;
  18. public int SelectedIndex
  19. {
  20. get
  21. {
  22. return _SelectedIndex;
  23. }
  24. set
  25. {
  26. _SelectedIndex = value;
  27. if (_SelectedIndex >= Items.Count)
  28. _SelectedIndex = -1;
  29. }
  30. }
  31. public List<String> Items
  32. {
  33. get
  34. {
  35. return _Items;
  36. }
  37. set
  38. {
  39. _Items = value;
  40. ItemsChanged();
  41. }
  42. }
  43. #region Back Color
  44. protected override void OnBackColourChanged()
  45. {
  46. base.OnBackColourChanged();
  47. if (_label != null && !_label.IsDisposed)
  48. _label.BackColour = BackColour;
  49. }
  50. #endregion
  51. #region Size
  52. protected override void OnSizeChanged()
  53. {
  54. base.OnSizeChanged();
  55. OrigHeight = OrigHeight == 0 ? Size.Height : OrigHeight;
  56. for (int i = 0; i < _Option.Length; i++)
  57. {
  58. _Option[i].Size = new Size(Size.Width - 16, 16); //Size.Width - 13, 13
  59. }
  60. if (_label != null && !_label.IsDisposed)
  61. _label.Size = new Size(Size.Width - 16, 15); //Size.Width - 16, 12
  62. if (_DropDownButton != null && !_DropDownButton.IsDisposed)
  63. _DropDownButton.Location = new Point(Size.Width - 18, 2);
  64. if (_ScrollUp != null && !_ScrollUp.IsDisposed)
  65. _ScrollUp.Location = new Point(Size.Width - 12, 14);
  66. if (_ScrollDown != null && !_ScrollDown.IsDisposed)
  67. _ScrollDown.Location = new Point(Size.Width - 12, 52);
  68. if (_ScrollPosition != null && !_ScrollPosition.IsDisposed)
  69. _ScrollPosition.Location = new Point(Size.Width - 11, 22);
  70. }
  71. #endregion
  72. #region Enabled
  73. protected override void OnEnabledChanged()
  74. {
  75. base.OnEnabledChanged();
  76. if (_DropDownButton != null && !_DropDownButton.IsDisposed)
  77. {
  78. _DropDownButton.Enabled = Enabled;
  79. if (Enabled)
  80. _DropDownButton.Visible = true;
  81. else
  82. _DropDownButton.Visible = false;
  83. }
  84. }
  85. #endregion
  86. public MirDropDownBox()
  87. {
  88. BackColour = Color.FromArgb(255,6,6,6);
  89. ForeColour = Color.White;
  90. Enabled = false;
  91. _label = new MirLabel
  92. {
  93. Parent = this,
  94. Location = new Point(0, 0),
  95. ForeColour = ForeColour,
  96. BackColour = BackColour,
  97. Font = new Font(Settings.FontName, 8F),
  98. Visible = true,
  99. };
  100. _label.Click += (o, e) =>
  101. {
  102. if (_DropDownButton.Enabled)
  103. DropDownClick();
  104. };
  105. _label.BeforeDraw += (o, e) =>
  106. {
  107. if ((Items.Count > 0) && (Items.Count >= SelectedIndex))
  108. if (SelectedIndex == -1)
  109. {
  110. _label.Text = " ";
  111. }
  112. else
  113. _label.Text = Items[SelectedIndex];
  114. else
  115. _label.Text = "None";
  116. };
  117. for (int i = 0; i < _Option.Length; i++)
  118. {
  119. _Option[i] = new MirLabel
  120. {
  121. Parent = this,
  122. Visible = false,
  123. Location = new Point(0, 15 + (i * 13)),
  124. ForeColour = ForeColour,
  125. BackColour = Color.FromArgb(255,20,20,20),
  126. Font = new Font(Settings.FontName, 8F)
  127. };
  128. int index = i;
  129. _Option[index].MouseEnter += (o, e) => _Option[index].BackColour = Color.FromArgb(255,140,70,0);
  130. _Option[index].MouseLeave += (o, e) => _Option[index].BackColour = Color.FromArgb(255, 20, 20, 20);
  131. _Option[index].MouseDown += (o, e) => _Option[index].BackColour = Color.FromArgb(255, 20, 20, 20);
  132. _Option[index].MouseUp += (o, e) => _Option[index].BackColour = Color.FromArgb(255,20,20,20);
  133. _Option[index].Click += (o, e) => SelectOption(index);
  134. _Option[index].BeforeDraw += (o, e) =>
  135. {
  136. if (Items.Count > (ScrollIndex + index + MinimumOption))
  137. _Option[index].Text = _Items[ScrollIndex + index + MinimumOption];
  138. };
  139. }
  140. _DropDownButton = new MirButton
  141. {
  142. Index = 207,
  143. Library = Libraries.Prguse2,
  144. Location = new Point(Size.Width - 18, 1),
  145. Parent = this,
  146. HoverIndex = 208,
  147. PressedIndex = 209,
  148. Visible = false,
  149. };
  150. _DropDownButton.Click += (o, e) => DropDownClick();
  151. _ScrollUp = new MirButton
  152. {
  153. HoverIndex = 2022,
  154. Index = 2021,
  155. Library = Libraries.Prguse,
  156. Location = new Point(Size.Width - 12, 14),
  157. Parent = this,
  158. PressedIndex = 2023,
  159. Visible = false
  160. };
  161. _ScrollUp.Click += (o, e) => ScrollUp();
  162. _ScrollDown = new MirButton
  163. {
  164. HoverIndex = 2025,
  165. Index = 2024,
  166. Library = Libraries.Prguse,
  167. Location = new Point(Size.Width - 12, 52),
  168. Parent = this,
  169. PressedIndex = 2026,
  170. Visible = false
  171. };
  172. _ScrollDown.Click += (o, e) => ScrollDown();
  173. _ScrollPosition = new MirButton
  174. {
  175. HoverIndex = 2016,
  176. Index = 2015,
  177. Library = Libraries.Prguse,
  178. Location = new Point(Size.Width - 11, 22),
  179. Movable = true,
  180. Parent = this,
  181. PressedIndex = 2017,
  182. Visible = false
  183. };
  184. _ScrollPosition.OnMoving += ScrollPosition;
  185. BeforeDraw += MirDropDownBox_BeforeDraw;
  186. }
  187. void MirDropDownBox_BeforeDraw(object sender, EventArgs e)
  188. {
  189. if (!_Option[0].Visible) return;
  190. if (IsMouseOver(CMain.MPoint)) return;
  191. foreach (MirControl control in Controls)
  192. if (control.IsMouseOver(CMain.MPoint))
  193. return;
  194. CloseDropDown();
  195. }
  196. protected override void OnMouseLeave()
  197. {
  198. if (!IsMouseOver(CMain.MPoint))
  199. CloseDropDown();
  200. base.OnMouseLeave();
  201. }
  202. public void SelectOption(int index)
  203. {
  204. if (ScrollIndex + index + MinimumOption < Items.Count)
  205. {
  206. _WantedIndex = ScrollIndex + index + MinimumOption;
  207. if (ValueChanged != null)
  208. ValueChanged.Invoke(this, EventArgs.Empty);
  209. }
  210. else
  211. Update();
  212. CloseDropDown();
  213. }
  214. public void ItemsChanged()
  215. {
  216. CloseDropDown();
  217. SelectedIndex = -1;
  218. }
  219. public void Update()
  220. {
  221. // Member is not implemented.
  222. }
  223. public void DropDownClick()
  224. {
  225. if (_Option[0].Visible)
  226. CloseDropDown();
  227. else
  228. OpenDropDown();
  229. }
  230. public void OpenDropDown()
  231. {
  232. Size = new Size(Size.Width, OrigHeight + (Math.Max(5, Items.Count) * 15));
  233. ScrollIndex = 0;
  234. if (Items.Count > 5)
  235. ScrollIndex = SelectedIndex > 3 ? SelectedIndex - 2 : 0;
  236. for (int i = 0; i < _Option.Length; i++)
  237. if (Items.Count > i)
  238. _Option[i].Visible = true;
  239. else
  240. _Option[i].Visible = false;
  241. if (Items.Count > 5)
  242. {
  243. _ScrollDown.Visible = true;
  244. _ScrollPosition.Visible = true;
  245. _ScrollUp.Visible = true;
  246. }
  247. else
  248. {
  249. _ScrollDown.Visible = false;
  250. _ScrollPosition.Visible = false;
  251. _ScrollUp.Visible = false;
  252. }
  253. _ScrollDown.Location = new Point(Size.Width - 12, 9 + Math.Min(5, Items.Count) * 13);
  254. }
  255. public void CloseDropDown()
  256. {
  257. Size = new Size(Size.Width, OrigHeight);
  258. for (int i = 0; i < _Option.Length; i++)
  259. _Option[i].Visible = false;
  260. _ScrollDown.Visible = false;
  261. _ScrollPosition.Visible = false;
  262. _ScrollUp.Visible = false;
  263. }
  264. public void ScrollUp()
  265. {
  266. if (ScrollIndex > 0) ScrollIndex--;
  267. Update();
  268. }
  269. public void ScrollDown()
  270. {
  271. if (ScrollIndex < (Items.Count - 5)) ScrollIndex++;
  272. Update();
  273. }
  274. void ScrollPosition(object sender, MouseEventArgs e)
  275. {
  276. int x = Size.Width - 11;
  277. int y = _ScrollPosition.Location.Y;
  278. if (y >= _ScrollDown.Location.Y - 14) y = _ScrollDown.Location.Y - 14;
  279. if (y < 20) y = 20;
  280. int h = _ScrollDown.Location.Y - _ScrollUp.Location.Y + 6;
  281. h = (int)((y - 15) / (h / (float)(Items.Count - 1)));
  282. if (h > Items.Count - 5) h = Math.Max(0, Items.Count - 5);
  283. if (h != ScrollIndex)
  284. {
  285. ScrollIndex = h;
  286. Update();
  287. }
  288. _ScrollPosition.Location = new Point(x, y);
  289. }
  290. #region Disposable
  291. protected override void Dispose(bool disposing)
  292. {
  293. base.Dispose(disposing);
  294. if (!disposing) return;
  295. if (_label != null && !_label.IsDisposed)
  296. _label.Dispose();
  297. _label = null;
  298. if (_DropDownButton != null && !_DropDownButton.IsDisposed)
  299. _DropDownButton.Dispose();
  300. _DropDownButton = null;
  301. if (_ScrollDown != null && !_ScrollDown.IsDisposed)
  302. _ScrollDown.Dispose();
  303. _ScrollDown = null;
  304. if (_ScrollPosition != null && !_ScrollPosition.IsDisposed)
  305. _ScrollPosition.Dispose();
  306. _ScrollPosition = null;
  307. if (_ScrollUp != null && !_ScrollUp.IsDisposed)
  308. _ScrollUp.Dispose();
  309. _ScrollUp = null;
  310. for (int i = 0; i < _Option.Length; i++)
  311. {
  312. if (_Option[i] != null && !_Option[i].IsDisposed)
  313. _Option[i].Dispose();
  314. _Option[i] = null;
  315. }
  316. }
  317. #endregion
  318. }
  319. }