MirInputBox.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Client.MirGraphics;
  2. namespace Client.MirControls
  3. {
  4. public sealed class MirInputBox : MirImageControl
  5. {
  6. public readonly MirLabel CaptionLabel;
  7. public readonly MirButton OKButton, CancelButton;
  8. public readonly MirTextBox InputTextBox;
  9. public MirInputBox(string message)
  10. {
  11. Modal = true;
  12. Movable = false;
  13. Index = 660;
  14. Library = Libraries.Prguse;
  15. Location = new Point((Settings.ScreenWidth - Size.Width) / 2, (Settings.ScreenHeight - Size.Height) / 2);
  16. CaptionLabel = new MirLabel
  17. {
  18. DrawFormat = TextFormatFlags.WordBreak,
  19. Location = new Point(25, 25),
  20. Size = new Size(235, 40),
  21. Parent = this,
  22. Text = message,
  23. };
  24. InputTextBox = new MirTextBox
  25. {
  26. Parent = this,
  27. Border = true,
  28. BorderColour = Color.Lime,
  29. Location = new Point(23, 86),
  30. Size = new Size(240, 19),
  31. MaxLength = 50,
  32. };
  33. InputTextBox.SetFocus();
  34. InputTextBox.TextBox.KeyPress += MirInputBox_KeyPress;
  35. OKButton = new MirButton
  36. {
  37. HoverIndex = 201,
  38. Index = 200,
  39. Library = Libraries.Title,
  40. Location = new Point(60, 123),
  41. Parent = this,
  42. PressedIndex = 202,
  43. };
  44. CancelButton = new MirButton
  45. {
  46. HoverIndex = 204,
  47. Index = 203,
  48. Library = Libraries.Title,
  49. Location = new Point(160, 123),
  50. Parent = this,
  51. PressedIndex = 205,
  52. };
  53. CancelButton.Click += DisposeDialog;
  54. }
  55. void MirInputBox_KeyPress(object sender, KeyPressEventArgs e)
  56. {
  57. if (e.KeyChar == (char)Keys.Enter)
  58. {
  59. if (OKButton != null && !OKButton.IsDisposed)
  60. OKButton.InvokeMouseClick(EventArgs.Empty);
  61. e.Handled = true;
  62. }
  63. else if (e.KeyChar == (char)Keys.Escape)
  64. {
  65. if (CancelButton != null && !CancelButton.IsDisposed)
  66. CancelButton.InvokeMouseClick(EventArgs.Empty);
  67. e.Handled = true;
  68. }
  69. }
  70. void DisposeDialog(object sender, EventArgs e)
  71. {
  72. Dispose();
  73. }
  74. public override void OnKeyDown(KeyEventArgs e)
  75. {
  76. base.OnKeyDown(e);
  77. e.Handled = true;
  78. }
  79. public override void OnKeyUp(KeyEventArgs e)
  80. {
  81. base.OnKeyUp(e);
  82. e.Handled = true;
  83. }
  84. public override void OnKeyPress(KeyPressEventArgs e)
  85. {
  86. base.OnKeyPress(e);
  87. if (e.KeyChar == (char)Keys.Escape)
  88. {
  89. if (CancelButton != null && !CancelButton.IsDisposed)
  90. CancelButton.InvokeMouseClick(EventArgs.Empty);
  91. }
  92. else if (e.KeyChar == (char)Keys.Enter)
  93. {
  94. if (OKButton != null && !OKButton.IsDisposed)
  95. OKButton.InvokeMouseClick(EventArgs.Empty);
  96. }
  97. e.Handled = true;
  98. }
  99. public override void Show()
  100. {
  101. if (Parent != null) return;
  102. Parent = MirScene.ActiveScene;
  103. Highlight();
  104. for (int i = 0; i < Program.Form.Controls.Count; i++)
  105. {
  106. TextBox T = Program.Form.Controls[i] as TextBox;
  107. if (T != null && T.Tag != null && T.Tag != null)
  108. ((MirTextBox)T.Tag).DialogChanged();
  109. }
  110. }
  111. #region Disposable
  112. protected override void Dispose(bool disposing)
  113. {
  114. base.Dispose(disposing);
  115. if (!disposing) return;
  116. for (int i = 0; i < Program.Form.Controls.Count; i++)
  117. {
  118. TextBox T = (TextBox)Program.Form.Controls[i];
  119. if (T != null && T.Tag != null && T.Tag != null)
  120. ((MirTextBox)T.Tag).DialogChanged();
  121. }
  122. }
  123. #endregion
  124. }
  125. }