You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.4 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Threading;
  24. namespace ICSharpCode.ILSpy.Controls
  25. {
  26. public class SearchBox : TextBox
  27. {
  28. static SearchBox()
  29. {
  30. DefaultStyleKeyProperty.OverrideMetadata(
  31. typeof(SearchBox),
  32. new FrameworkPropertyMetadata(typeof(SearchBox)));
  33. }
  34. #region Dependency properties
  35. public static DependencyProperty WatermarkTextProperty = DependencyProperty.Register("WatermarkText", typeof(string), typeof(SearchBox));
  36. public static DependencyProperty WatermarkColorProperty = DependencyProperty.Register("WatermarkColor", typeof(Brush), typeof(SearchBox));
  37. public static DependencyProperty HasTextProperty = DependencyProperty.Register("HasText", typeof(bool), typeof(SearchBox));
  38. public static readonly DependencyProperty UpdateDelayProperty =
  39. DependencyProperty.Register("UpdateDelay", typeof(TimeSpan), typeof(SearchBox),
  40. new FrameworkPropertyMetadata(TimeSpan.FromMilliseconds(200)));
  41. #endregion
  42. #region Public Properties
  43. public string WatermarkText {
  44. get { return (string)GetValue(WatermarkTextProperty); }
  45. set { SetValue(WatermarkTextProperty, value); }
  46. }
  47. public Brush WatermarkColor {
  48. get { return (Brush)GetValue(WatermarkColorProperty); }
  49. set { SetValue(WatermarkColorProperty, value); }
  50. }
  51. public bool HasText {
  52. get { return (bool)GetValue(HasTextProperty); }
  53. private set { SetValue(HasTextProperty, value); }
  54. }
  55. public TimeSpan UpdateDelay {
  56. get { return (TimeSpan)GetValue(UpdateDelayProperty); }
  57. set { SetValue(UpdateDelayProperty, value); }
  58. }
  59. #endregion
  60. #region Handlers
  61. private void IconBorder_MouseLeftButtonUp(object obj, MouseButtonEventArgs e)
  62. {
  63. if (this.HasText)
  64. this.Text = string.Empty;
  65. }
  66. #endregion
  67. #region Overrides
  68. DispatcherTimer timer;
  69. protected override void OnTextChanged(TextChangedEventArgs e)
  70. {
  71. base.OnTextChanged(e);
  72. HasText = this.Text.Length > 0;
  73. if (timer == null)
  74. {
  75. timer = new DispatcherTimer();
  76. timer.Tick += timer_Tick;
  77. }
  78. timer.Stop();
  79. timer.Interval = this.UpdateDelay;
  80. timer.Start();
  81. UpdateWatermarkLabel();
  82. }
  83. private void UpdateWatermarkLabel()
  84. {
  85. Label wl = (Label)GetTemplateChild("WatermarkLabel");
  86. if (wl != null)
  87. wl.Visibility = HasText ? Visibility.Hidden : Visibility.Visible;
  88. }
  89. void timer_Tick(object sender, EventArgs e)
  90. {
  91. timer.Stop();
  92. timer = null;
  93. var textBinding = GetBindingExpression(TextProperty);
  94. if (textBinding != null)
  95. {
  96. textBinding.UpdateSource();
  97. }
  98. }
  99. protected override void OnLostFocus(RoutedEventArgs e)
  100. {
  101. UpdateWatermarkLabel();
  102. base.OnLostFocus(e);
  103. }
  104. protected override void OnGotFocus(RoutedEventArgs e)
  105. {
  106. UpdateWatermarkLabel();
  107. base.OnGotFocus(e);
  108. }
  109. public override void OnApplyTemplate()
  110. {
  111. base.OnApplyTemplate();
  112. Border iconBorder = GetTemplateChild("PART_IconBorder") as Border;
  113. if (iconBorder != null)
  114. {
  115. iconBorder.MouseLeftButtonUp += IconBorder_MouseLeftButtonUp;
  116. }
  117. }
  118. protected override void OnKeyDown(KeyEventArgs e)
  119. {
  120. if (e.Key == Key.Escape && this.Text.Length > 0)
  121. {
  122. this.Text = string.Empty;
  123. e.Handled = true;
  124. }
  125. else
  126. {
  127. base.OnKeyDown(e);
  128. }
  129. }
  130. #endregion
  131. }
  132. }