OSDN Git Service

AppliStation,ハッシュが非整合の時に尋ねるようにする部分の改良
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / ToolStripWeb2LikeTextBox.cs
1 using System;\r
2 using System.ComponentModel;\r
3 using System.Windows.Forms;\r
4 using System.Drawing;\r
5 \r
6 namespace AppliStation.Util\r
7 {\r
8         public class ToolStripWeb2LikeTextBox : ToolStripTextBox\r
9         {\r
10                 public ToolStripWeb2LikeTextBox() : base()\r
11                 {\r
12                         this.Text = string.Empty;\r
13                 }\r
14                                 \r
15                 [Category("Action")]\r
16                 public event EventHandler TextChangedTriggerFired;\r
17                 \r
18                 private int textChangedTriggerInterval = 500;\r
19                 \r
20                 /// <summary>\r
21                 /// Interval time for TextChangedTriggerFired.\r
22                 /// </summary>\r
23                 [Category("Behavior")]\r
24                 [DefaultValue(500)]\r
25                 public virtual int TextChangedTriggerInterval {\r
26                         get { return textChangedTriggerInterval; }\r
27                         set { textChangedTriggerInterval = value; }\r
28                 }\r
29                 \r
30                 \r
31                 private string oldText = null;\r
32                 public void FireTextChangedTrigger(EventArgs e)\r
33                 {\r
34                         textChangeTriggerTimerStop();\r
35                         \r
36                         if (oldText != this.Text) {\r
37                                 if (TextChangedTriggerFired != null) {\r
38                                         TextChangedTriggerFired(this, e);\r
39                                 }\r
40                                 oldText = this.Text;\r
41                         }\r
42                 }\r
43                 public void FireTextChangedTrigger()\r
44                 {\r
45                         FireTextChangedTrigger(EventArgs.Empty);\r
46                 }\r
47                 \r
48                 #region TextChange Timers\r
49                 \r
50                 /// <summary>\r
51                 /// Timer for TextChangedTriggerFired.\r
52                 /// </summary>\r
53                 protected Timer textChangeTriggerTimer = null;\r
54                 \r
55                 private void onTextUpdate(bool force)\r
56                 {\r
57                         textChangeTriggerTimerStop();\r
58                         \r
59                         textChangeTriggerTimer = new Timer();\r
60                         textChangeTriggerTimer.Tick += textChangeTriggerTimerTick;\r
61                         textChangeTriggerTimer.Interval = TextChangedTriggerInterval;\r
62                         textChangeTriggerTimer.Start();\r
63                 }\r
64                                 \r
65                 private void textChangeTriggerTimerTick(object sender, EventArgs e)\r
66                 {\r
67                         FireTextChangedTrigger(e);\r
68                 }\r
69                 \r
70                 private void textChangeTriggerTimerStop()\r
71                 {\r
72                         if (textChangeTriggerTimer != null) {\r
73                                 textChangeTriggerTimer.Stop();\r
74                                 textChangeTriggerTimer = null;\r
75                         }\r
76                 }\r
77                 \r
78                 protected override void OnTextChanged(EventArgs e)\r
79                 {\r
80                         base.OnTextChanged(e);\r
81                         \r
82                         if (Focused) {\r
83                                 onTextUpdate(false);\r
84                         }\r
85                 }\r
86                 \r
87                 #endregion\r
88                 \r
89                 protected bool textIsEmpty = true;\r
90                 \r
91                 public override string Text {\r
92                         get { return textIsEmpty? string.Empty : base.Text; }\r
93                         set {\r
94                                 bool changed = false;\r
95                                 if (string.IsNullOrEmpty(value)) {\r
96                                         if (! textIsEmpty) {\r
97                                                 changed = true;\r
98                                         \r
99                                                 textIsEmpty = true;\r
100                                                 base.Text = EmptyText;\r
101                                         }\r
102                                 } else if (this.Text != value) {\r
103                                         changed = true;\r
104                                         \r
105                                         textIsEmpty = false;\r
106                                         base.Text = value;\r
107                                 }\r
108                                 \r
109                                 if (changed) {\r
110                                         FireTextChangedTrigger(EventArgs.Empty);\r
111                                         base.OnTextChanged(EventArgs.Empty);\r
112                                         \r
113                                         updateForeColorInternal();\r
114                                 }\r
115                         }\r
116                 }\r
117                 \r
118                 private string emptyText = "(none)";\r
119                 \r
120                 /// <summary>\r
121                 /// Gray text when textbox is empty and has not focus\r
122                 /// </summary>\r
123                 [Category("Appearance")]\r
124                 public virtual string EmptyText {\r
125                         get { return emptyText; }\r
126                         set {\r
127                                 emptyText = value;\r
128                                 \r
129                                 if (textIsEmpty) {\r
130                                         base.Text = emptyText;\r
131                                 }\r
132                         }\r
133                 }\r
134                 \r
135                 private Color emptyForeColor = SystemColors.GrayText;\r
136                 \r
137                 /// <summary>\r
138                 /// Gray text color when textbox is empty and has not focus\r
139                 /// </summary>\r
140                 [Category("Appearance")]\r
141                 [DefaultValue(typeof(Color), "GrayText")]\r
142                 public virtual Color EmptyForeColor {\r
143                         get { return emptyForeColor; }\r
144                         set {\r
145                                 emptyForeColor = value;\r
146                                 updateForeColorInternal();\r
147                         }\r
148                 }\r
149                 \r
150                 private Color foreColor = SystemColors.WindowText;\r
151                 \r
152                 public override Color ForeColor {\r
153                         get { return foreColor; }\r
154                         set {\r
155                                 foreColor = value;\r
156                                 updateForeColorInternal();\r
157                         }\r
158                 }\r
159                 \r
160                 protected override void OnEnter(EventArgs e)\r
161                 {\r
162                         if (textIsEmpty) {\r
163                                 base.Text = string.Empty;\r
164                                 textIsEmpty = false;\r
165                                 updateForeColorInternal();\r
166                         }\r
167                         \r
168                         base.OnEnter(e);\r
169                 }\r
170                 \r
171                 protected override void OnLeave(EventArgs e)\r
172                 {\r
173                         if (string.IsNullOrEmpty(base.Text)) {\r
174                                 base.Text = EmptyText;\r
175                                 textIsEmpty = true;\r
176                                 updateForeColorInternal();\r
177                         }\r
178                         \r
179                         base.OnLeave(e);\r
180                 }\r
181                 \r
182                 private void updateForeColorInternal()\r
183                 {\r
184                         base.ForeColor = textIsEmpty? EmptyForeColor : ForeColor;\r
185                 }\r
186         }\r
187 }\r