TextBoxEx.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. using System.Text.RegularExpressions;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace OpenSEMI.Ctrlib.Controls
  7. {
  8. public class TextBoxEx : TextBox
  9. {
  10. private string m_strOldValue = string.Empty;
  11. public static readonly DependencyProperty EditBoxModeProperty;
  12. public static readonly DependencyProperty NormalColorProperty;
  13. public static readonly DependencyProperty ChangedColorProperty;
  14. public static readonly DependencyProperty WarningColorProperty;
  15. private bool m_AllowEmpty = true;
  16. private bool m_AllowBackgroundChange = true;
  17. public static readonly DependencyProperty MaxValueProperty;
  18. public static readonly DependencyProperty MinValueProperty;
  19. public static readonly DependencyProperty AccuracyProperty;
  20. public static readonly DependencyProperty TextSavedProperty;
  21. public static readonly DependencyProperty ScrollToEndProperty;
  22. public static readonly DependencyProperty IsTextboxFocusedProperty;
  23. public EditBoxMode EditBoxMode
  24. {
  25. get
  26. {
  27. return (EditBoxMode)GetValue(EditBoxModeProperty);
  28. }
  29. set
  30. {
  31. SetValue(EditBoxModeProperty, value);
  32. }
  33. }
  34. public Brush NormalColor
  35. {
  36. get
  37. {
  38. return (Brush)GetValue(NormalColorProperty);
  39. }
  40. set
  41. {
  42. SetValue(NormalColorProperty, value);
  43. }
  44. }
  45. public Brush ChangedColor
  46. {
  47. get
  48. {
  49. return (Brush)GetValue(ChangedColorProperty);
  50. }
  51. set
  52. {
  53. SetValue(ChangedColorProperty, value);
  54. }
  55. }
  56. public Brush WarningColor
  57. {
  58. get
  59. {
  60. return (Brush)GetValue(WarningColorProperty);
  61. }
  62. set
  63. {
  64. SetValue(WarningColorProperty, value);
  65. }
  66. }
  67. public bool AllowEmpty
  68. {
  69. get
  70. {
  71. return m_AllowEmpty;
  72. }
  73. set
  74. {
  75. m_AllowEmpty = value;
  76. }
  77. }
  78. public bool AllowBackgroundChange
  79. {
  80. get
  81. {
  82. return m_AllowBackgroundChange;
  83. }
  84. set
  85. {
  86. m_AllowBackgroundChange = value;
  87. }
  88. }
  89. public double MaxValue
  90. {
  91. get
  92. {
  93. return (double)GetValue(MaxValueProperty);
  94. }
  95. set
  96. {
  97. SetValue(MaxValueProperty, value);
  98. }
  99. }
  100. public double MinValue
  101. {
  102. get
  103. {
  104. return (double)GetValue(MinValueProperty);
  105. }
  106. set
  107. {
  108. SetValue(MinValueProperty, value);
  109. }
  110. }
  111. public int Accuracy
  112. {
  113. get
  114. {
  115. return (int)GetValue(AccuracyProperty);
  116. }
  117. set
  118. {
  119. SetValue(AccuracyProperty, value);
  120. }
  121. }
  122. public bool TextSaved
  123. {
  124. get
  125. {
  126. return (bool)GetValue(TextSavedProperty);
  127. }
  128. set
  129. {
  130. SetValue(TextSavedProperty, value);
  131. }
  132. }
  133. public bool IsScrollToEnd
  134. {
  135. get
  136. {
  137. return (bool)GetValue(ScrollToEndProperty);
  138. }
  139. set
  140. {
  141. SetValue(ScrollToEndProperty, value);
  142. }
  143. }
  144. public bool IsTextboxFocused
  145. {
  146. get
  147. {
  148. return (bool)GetValue(IsTextboxFocusedProperty);
  149. }
  150. set
  151. {
  152. SetValue(IsTextboxFocusedProperty, value);
  153. }
  154. }
  155. public event KeyEventHandler TextBoxExEnterKeyDown;
  156. public override void OnApplyTemplate()
  157. {
  158. base.OnApplyTemplate();
  159. }
  160. static TextBoxEx()
  161. {
  162. EditBoxModeProperty = DependencyProperty.Register("EditBoxMode", typeof(EditBoxMode), typeof(TextBoxEx), new UIPropertyMetadata(EditBoxMode.Default));
  163. NormalColorProperty = DependencyProperty.Register("NormalColor", typeof(Brush), typeof(TextBoxEx));
  164. ChangedColorProperty = DependencyProperty.Register("ChangedColor", typeof(Brush), typeof(TextBoxEx));
  165. WarningColorProperty = DependencyProperty.Register("WarningColor", typeof(Brush), typeof(TextBoxEx));
  166. MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(TextBoxEx), new UIPropertyMetadata(double.NaN));
  167. MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(TextBoxEx), new UIPropertyMetadata(double.NaN));
  168. AccuracyProperty = DependencyProperty.Register("Accuracy", typeof(int), typeof(TextBoxEx), new UIPropertyMetadata(4));
  169. TextSavedProperty = DependencyProperty.Register("TextSaved", typeof(bool), typeof(TextBoxEx), new UIPropertyMetadata(true, TextSavedChangedCallBack));
  170. ScrollToEndProperty = DependencyProperty.Register("IsScrollToEnd", typeof(bool), typeof(TextBoxEx), new UIPropertyMetadata(false));
  171. IsTextboxFocusedProperty = DependencyProperty.Register("IsTextboxFocused", typeof(bool), typeof(TextBoxEx), new UIPropertyMetadata(false));
  172. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxEx), new FrameworkPropertyMetadata(typeof(TextBoxEx)));
  173. }
  174. protected override void OnPreviewKeyDown(KeyEventArgs e)
  175. {
  176. switch (EditBoxMode)
  177. {
  178. case EditBoxMode.Email:
  179. break;
  180. case EditBoxMode.SignInteger:
  181. AllowInteger(e);
  182. break;
  183. case EditBoxMode.UnSignInteger:
  184. AllowUnsignInteger(e);
  185. break;
  186. case EditBoxMode.Decimal:
  187. AllowDecimal(e);
  188. break;
  189. case EditBoxMode.UnSignDecimal:
  190. AllowUnSignDecimal(e);
  191. break;
  192. case EditBoxMode.Time:
  193. AllowTime(e);
  194. break;
  195. case EditBoxMode.FileName:
  196. AllowFileName(e);
  197. break;
  198. }
  199. }
  200. private void AllowInteger(KeyEventArgs e)
  201. {
  202. int num;
  203. if ((Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Shift) && e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Insert && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Tab && e.Key != Key.Next && e.Key != Key.Prior && e.Key != Key.Return && e.Key != Key.Return && e.Key != Key.Escape && e.Key != Key.Home)
  204. {
  205. num = ((e.Key == Key.End) ? 1 : 0);
  206. goto IL_00ae;
  207. }
  208. num = 1;
  209. goto IL_00ae;
  210. IL_00ae:
  211. bool flag = (byte)num != 0;
  212. bool flag2 = e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9;
  213. bool flag3 = e.Key >= Key.D0 && e.Key <= Key.D9;
  214. if ((flag3 | flag2) && Keyboard.Modifiers != 0)
  215. {
  216. e.Handled = true;
  217. return;
  218. }
  219. if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
  220. {
  221. if (Keyboard.Modifiers != ModifierKeys.Shift)
  222. {
  223. if (base.Text.IndexOfAny(new char[1]
  224. {
  225. '-'
  226. }, 0) == -1 && base.CaretIndex == 0)
  227. {
  228. flag = true;
  229. goto IL_017c;
  230. }
  231. e.Handled = true;
  232. return;
  233. }
  234. flag = false;
  235. }
  236. goto IL_017c;
  237. IL_017c:
  238. e.Handled = (!flag && !flag3 && !flag2);
  239. }
  240. private void AllowUnsignInteger(KeyEventArgs e)
  241. {
  242. bool flag = e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9;
  243. bool flag2 = e.Key >= Key.D0 && e.Key <= Key.D9;
  244. if ((flag2 | flag) && Keyboard.Modifiers != 0)
  245. {
  246. e.Handled = true;
  247. return;
  248. }
  249. int num;
  250. if ((Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Shift) && e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Insert && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Tab && e.Key != Key.Next && e.Key != Key.Prior && e.Key != Key.Return && e.Key != Key.Return && e.Key != Key.Escape && e.Key != Key.Home)
  251. {
  252. num = ((e.Key == Key.End) ? 1 : 0);
  253. goto IL_0106;
  254. }
  255. num = 1;
  256. goto IL_0106;
  257. IL_0106:
  258. bool flag3 = (byte)num != 0;
  259. e.Handled = (!flag3 && !flag2 && !flag);
  260. }
  261. private void AllowDecimal(KeyEventArgs e)
  262. {
  263. int num;
  264. if ((Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Shift) && e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Insert && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Tab && e.Key != Key.Next && e.Key != Key.Prior && e.Key != Key.Return && e.Key != Key.Return && e.Key != Key.Escape && e.Key != Key.Home)
  265. {
  266. num = ((e.Key == Key.End) ? 1 : 0);
  267. goto IL_00ae;
  268. }
  269. num = 1;
  270. goto IL_00ae;
  271. IL_00ae:
  272. bool flag = (byte)num != 0;
  273. bool flag2 = e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9;
  274. bool flag3 = e.Key >= Key.D0 && e.Key <= Key.D9;
  275. if ((flag3 | flag2) && Keyboard.Modifiers != 0)
  276. {
  277. e.Handled = true;
  278. return;
  279. }
  280. if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
  281. {
  282. if (Keyboard.Modifiers != ModifierKeys.Shift)
  283. {
  284. if (base.Text.IndexOfAny(new char[1]
  285. {
  286. '-'
  287. }, 0) == -1 && base.CaretIndex == 0)
  288. {
  289. flag = true;
  290. goto IL_0181;
  291. }
  292. e.Handled = true;
  293. return;
  294. }
  295. flag = false;
  296. }
  297. goto IL_0181;
  298. IL_0181:
  299. if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
  300. {
  301. if (Keyboard.Modifiers != ModifierKeys.Shift)
  302. {
  303. if (base.Text.IndexOfAny(new char[1]
  304. {
  305. '.'
  306. }, 0) == -1 && base.CaretIndex > 0 && base.CaretIndex + Accuracy >= base.Text.Length)
  307. {
  308. flag = true;
  309. goto IL_0213;
  310. }
  311. e.Handled = true;
  312. return;
  313. }
  314. flag = false;
  315. }
  316. goto IL_0213;
  317. IL_0213:
  318. int num2 = base.Text.IndexOf('.');
  319. if (num2 > -1 && base.CaretIndex > num2 && base.Text.Length - num2 > Accuracy)
  320. {
  321. flag3 = (flag2 = false);
  322. }
  323. e.Handled = (!flag && !flag3 && !flag2);
  324. }
  325. private void AllowUnSignDecimal(KeyEventArgs e)
  326. {
  327. bool flag = e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9;
  328. bool flag2 = e.Key >= Key.D0 && e.Key <= Key.D9;
  329. if ((flag2 | flag) && Keyboard.Modifiers != 0)
  330. {
  331. e.Handled = true;
  332. return;
  333. }
  334. int num;
  335. if ((Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Shift) && e.Key != Key.Back && e.Key != Key.Delete && e.Key != Key.Insert && e.Key != Key.Down && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Tab && e.Key != Key.Next && e.Key != Key.Prior && e.Key != Key.Return && e.Key != Key.Return && e.Key != Key.Escape && e.Key != Key.Home)
  336. {
  337. num = ((e.Key == Key.End) ? 1 : 0);
  338. goto IL_0108;
  339. }
  340. num = 1;
  341. goto IL_0108;
  342. IL_0108:
  343. bool flag3 = (byte)num != 0;
  344. if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
  345. {
  346. if (Keyboard.Modifiers != ModifierKeys.Shift)
  347. {
  348. if (base.Text.IndexOfAny(new char[1]
  349. {
  350. '.'
  351. }, 0) == -1 && base.CaretIndex > 0 && base.CaretIndex + Accuracy >= base.Text.Length)
  352. {
  353. flag3 = true;
  354. goto IL_019b;
  355. }
  356. e.Handled = true;
  357. return;
  358. }
  359. flag3 = false;
  360. }
  361. goto IL_019b;
  362. IL_019b:
  363. int num2 = base.Text.IndexOf('.');
  364. if (num2 > -1 && base.CaretIndex > num2 && base.Text.Length - num2 > Accuracy)
  365. {
  366. flag2 = (flag = false);
  367. }
  368. e.Handled = (!flag3 && !flag2 && !flag);
  369. }
  370. private void AllowTime(KeyEventArgs e)
  371. {
  372. AllowDecimal(e);
  373. if (e.Key == Key.Oem1 && Keyboard.Modifiers == ModifierKeys.Shift)
  374. {
  375. e.Handled = false;
  376. }
  377. if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
  378. {
  379. e.Handled = true;
  380. }
  381. }
  382. private void AllowFileName(KeyEventArgs e)
  383. {
  384. if (base.Text.Length >= 128)
  385. {
  386. e.Handled = true;
  387. }
  388. else if (Keyboard.Modifiers == ModifierKeys.Shift && (e.Key == Key.OemPeriod || e.Key == Key.OemComma || e.Key == Key.D8 || e.Key == Key.Oem1 || e.Key == Key.Oem7))
  389. {
  390. e.Handled = true;
  391. }
  392. else if (e.Key == Key.Oem2 || e.Key == Key.Oem5)
  393. {
  394. e.Handled = true;
  395. }
  396. }
  397. public bool IsOutOfRange()
  398. {
  399. if (!double.IsNaN(MinValue) && !double.IsNaN(MaxValue))
  400. {
  401. double result;
  402. bool flag = double.TryParse(base.Text, out result);
  403. if (result > MaxValue)
  404. {
  405. return true;
  406. }
  407. if (result < MinValue)
  408. {
  409. return true;
  410. }
  411. return false;
  412. }
  413. return false;
  414. }
  415. protected override void OnLostFocus(RoutedEventArgs e)
  416. {
  417. base.OnLostFocus(e);
  418. IsTextboxFocused = false;
  419. if (base.Text.Length == 0)
  420. {
  421. if (!AllowEmpty)
  422. {
  423. base.Text = m_strOldValue;
  424. }
  425. }
  426. else if (EditBoxMode == EditBoxMode.UnSignDecimal || EditBoxMode == EditBoxMode.Decimal || EditBoxMode == EditBoxMode.SignInteger || EditBoxMode == EditBoxMode.UnSignInteger)
  427. {
  428. if (IsOutOfRange())
  429. {
  430. base.Background = Brushes.Red;
  431. base.ToolTip = MinValue + "-" + MaxValue;
  432. }
  433. else
  434. {
  435. SetBGColor(this);
  436. }
  437. }
  438. }
  439. protected override void OnKeyDown(KeyEventArgs e)
  440. {
  441. if (e.Key == Key.Tab)
  442. {
  443. SelectAll();
  444. }
  445. KeyEventHandler textBoxExEnterKeyDown = this.TextBoxExEnterKeyDown;
  446. if (textBoxExEnterKeyDown != null && e.Key == Key.Return)
  447. {
  448. if (IsOutOfRange())
  449. {
  450. base.Background = Brushes.Red;
  451. base.ToolTip = MinValue + "-" + MaxValue;
  452. }
  453. else
  454. {
  455. textBoxExEnterKeyDown(this, e);
  456. }
  457. }
  458. if (e.Key == Key.Return || e.Key == Key.Return)
  459. {
  460. if (IsOutOfRange())
  461. {
  462. base.Background = Brushes.Red;
  463. base.ToolTip = MinValue + "-" + MaxValue;
  464. }
  465. else
  466. {
  467. SetBGColor(this);
  468. }
  469. }
  470. }
  471. protected override void OnTextChanged(TextChangedEventArgs e)
  472. {
  473. base.OnTextChanged(e);
  474. if (base.IsInitialized)
  475. {
  476. if (m_strOldValue != base.Text && base.IsFocused && AllowBackgroundChange)
  477. {
  478. TextSaved = false;
  479. }
  480. else
  481. {
  482. m_strOldValue = base.Text;
  483. TextSaved = true;
  484. }
  485. if (EditBoxMode == EditBoxMode.FileName)
  486. {
  487. Regex regex = new Regex("\\*|\\\\|\\/|\\?|\"|:|\\<|\\>|\\|");
  488. base.Text = regex.Replace(base.Text, string.Empty);
  489. }
  490. }
  491. if (IsScrollToEnd)
  492. {
  493. ScrollToEnd();
  494. }
  495. }
  496. protected override void OnMouseDown(MouseButtonEventArgs e)
  497. {
  498. base.OnMouseDown(e);
  499. if (e.ClickCount == 1)
  500. {
  501. SelectAll();
  502. }
  503. }
  504. protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
  505. {
  506. base.OnGotKeyboardFocus(e);
  507. IsTextboxFocused = true;
  508. }
  509. private static void TextSavedChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
  510. {
  511. TextBoxEx textBoxEx = d as TextBoxEx;
  512. if (textBoxEx != null)
  513. {
  514. if (textBoxEx.IsOutOfRange())
  515. {
  516. textBoxEx.Background = Brushes.Red;
  517. textBoxEx.ToolTip = textBoxEx.MinValue + "-" + textBoxEx.MaxValue;
  518. }
  519. else
  520. {
  521. SetBGColor(textBoxEx);
  522. }
  523. }
  524. }
  525. private static void SetBGColor(TextBoxEx tb)
  526. {
  527. if (tb.TextSaved)
  528. {
  529. tb.m_strOldValue = tb.Text;
  530. if (tb.NormalColor != null)
  531. {
  532. tb.Background = tb.NormalColor;
  533. }
  534. }
  535. else if (tb.ChangedColor != null)
  536. {
  537. tb.Background = tb.ChangedColor;
  538. }
  539. }
  540. public void SaveText()
  541. {
  542. TextSaved = true;
  543. }
  544. }
  545. }