ToolTip.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // COMPLETE
  27. using System.Collections;
  28. using System.ComponentModel;
  29. using System.Drawing;
  30. namespace System.Windows.Forms {
  31. [ProvideProperty ("ToolTip", typeof(System.Windows.Forms.Control))]
  32. [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
  33. public
  34. #if !NET_2_0
  35. sealed
  36. #endif
  37. class ToolTip : System.ComponentModel.Component, System.ComponentModel.IExtenderProvider {
  38. #region Local variables
  39. internal bool is_active;
  40. internal int automatic_delay;
  41. internal int autopop_delay;
  42. internal int initial_delay;
  43. internal int re_show_delay;
  44. internal bool show_always;
  45. internal ToolTipWindow tooltip_window; // The actual tooltip window
  46. internal Hashtable tooltip_strings; // List of strings for each control, indexed by control
  47. internal ArrayList controls;
  48. internal Control active_control; // Control for which the tooltip is currently displayed
  49. internal Control last_control; // last control the mouse was in
  50. internal Timer timer; // Used for the various intervals
  51. #endregion // Local variables
  52. #region ToolTipWindow Class
  53. internal class ToolTipWindow : Control {
  54. #region ToolTipWindow Class Local Variables
  55. internal StringFormat string_format;
  56. #endregion // ToolTipWindow Class Local Variables
  57. #region ToolTipWindow Class Constructor
  58. internal ToolTipWindow() {
  59. string_format = new StringFormat();
  60. string_format.LineAlignment = StringAlignment.Center;
  61. string_format.Alignment = StringAlignment.Center;
  62. string_format.FormatFlags = StringFormatFlags.NoWrap;
  63. Visible = false;
  64. Size = new Size(100, 20);
  65. ForeColor = ThemeEngine.Current.ColorInfoText;
  66. BackColor = ThemeEngine.Current.ColorInfo;
  67. VisibleChanged += new EventHandler(ToolTipWindow_VisibleChanged);
  68. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  69. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  70. }
  71. #endregion // ToolTipWindow Class Constructor
  72. #region ToolTipWindow Class Protected Instance Methods
  73. protected override void OnCreateControl() {
  74. base.OnCreateControl ();
  75. XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, true);
  76. }
  77. protected override CreateParams CreateParams {
  78. get {
  79. CreateParams cp;
  80. cp = base.CreateParams;
  81. cp.Style = (int)WindowStyles.WS_POPUP;
  82. cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
  83. cp.ExStyle = (int)(WindowExStyles.WS_EX_TOOLWINDOW | WindowExStyles.WS_EX_TOPMOST);
  84. return cp;
  85. }
  86. }
  87. protected override void OnPaint(PaintEventArgs pevent) {
  88. // We don't do double-buffering on purpose:
  89. // 1) we'd have to meddle with is_visible, it destroys the buffers if !visible
  90. // 2) We don't draw much, no need to double buffer
  91. ThemeEngine.Current.DrawToolTip(pevent.Graphics, ClientRectangle, this);
  92. base.OnPaint(pevent);
  93. }
  94. protected override void Dispose(bool disposing) {
  95. if (disposing) {
  96. this.string_format.Dispose();
  97. }
  98. base.Dispose (disposing);
  99. }
  100. protected override void WndProc(ref Message m) {
  101. if (m.Msg == (int)Msg.WM_SETFOCUS) {
  102. if (m.WParam != IntPtr.Zero) {
  103. XplatUI.SetFocus(m.WParam);
  104. }
  105. }
  106. base.WndProc (ref m);
  107. }
  108. #endregion // ToolTipWindow Class Protected Instance Methods
  109. #region ToolTipWindow Class Private Methods
  110. private void ToolTipWindow_VisibleChanged(object sender, EventArgs e) {
  111. Control control = (Control)sender;
  112. if (control.is_visible) {
  113. XplatUI.SetTopmost(control.window.Handle, IntPtr.Zero, true);
  114. } else {
  115. XplatUI.SetTopmost(control.window.Handle, IntPtr.Zero, false);
  116. }
  117. }
  118. #endregion // ToolTipWindow Class Protected Instance Methods
  119. public void Present (Control control, string text)
  120. {
  121. if (IsDisposed)
  122. return;
  123. Size display_size;
  124. XplatUI.GetDisplaySize (out display_size);
  125. Size size = ThemeEngine.Current.ToolTipSize (this, text);
  126. Width = size.Width;
  127. Height = size.Height;
  128. Text = text;
  129. int cursor_w, cursor_h, hot_x, hot_y;
  130. XplatUI.GetCursorInfo (control.Cursor.Handle, out cursor_w, out cursor_h, out hot_x, out hot_y);
  131. Point loc = Control.MousePosition;
  132. loc.Y += (cursor_h - hot_y);
  133. if ((loc.X + Width) > display_size.Width)
  134. loc.X = display_size.Width - Width;
  135. if ((loc.Y + Height) > display_size.Height)
  136. loc.Y = Control.MousePosition.Y - Height - hot_y;
  137. Location = loc;
  138. Visible = true;
  139. }
  140. }
  141. #endregion // ToolTipWindow Class
  142. #region Public Constructors & Destructors
  143. public ToolTip() {
  144. // Defaults from MS
  145. is_active = true;
  146. automatic_delay = 500;
  147. autopop_delay = 5000;
  148. initial_delay = 500;
  149. re_show_delay = 100;
  150. show_always = false;
  151. tooltip_strings = new Hashtable(5);
  152. controls = new ArrayList(5);
  153. tooltip_window = new ToolTipWindow();
  154. tooltip_window.MouseLeave += new EventHandler(control_MouseLeave);
  155. timer = new Timer();
  156. timer.Enabled = false;
  157. timer.Tick +=new EventHandler(timer_Tick);
  158. }
  159. public ToolTip(System.ComponentModel.IContainer cont) : this() {
  160. cont.Add (this);
  161. }
  162. ~ToolTip() {
  163. }
  164. #endregion // Public Constructors & Destructors
  165. #region Public Instance Properties
  166. [DefaultValue (true)]
  167. public bool Active {
  168. get {
  169. return is_active;
  170. }
  171. set {
  172. if (is_active != value) {
  173. is_active = value;
  174. if (tooltip_window.Visible) {
  175. tooltip_window.Visible = false;
  176. active_control = null;
  177. }
  178. }
  179. }
  180. }
  181. [DefaultValue (500)]
  182. [RefreshProperties (RefreshProperties.All)]
  183. public int AutomaticDelay {
  184. get {
  185. return automatic_delay;
  186. }
  187. set {
  188. if (automatic_delay != value) {
  189. automatic_delay = value;
  190. autopop_delay = automatic_delay * 10;
  191. initial_delay = automatic_delay;
  192. re_show_delay = automatic_delay / 5;
  193. }
  194. }
  195. }
  196. [RefreshProperties (RefreshProperties.All)]
  197. public int AutoPopDelay {
  198. get {
  199. return autopop_delay;
  200. }
  201. set {
  202. if (autopop_delay != value) {
  203. autopop_delay = value;
  204. }
  205. }
  206. }
  207. [RefreshProperties (RefreshProperties.All)]
  208. public int InitialDelay {
  209. get {
  210. return initial_delay;
  211. }
  212. set {
  213. if (initial_delay != value) {
  214. initial_delay = value;
  215. }
  216. }
  217. }
  218. [RefreshProperties (RefreshProperties.All)]
  219. public int ReshowDelay {
  220. get {
  221. return re_show_delay;
  222. }
  223. set {
  224. if (re_show_delay != value) {
  225. re_show_delay = value;
  226. }
  227. }
  228. }
  229. [DefaultValue (false)]
  230. public bool ShowAlways {
  231. get {
  232. return show_always;
  233. }
  234. set {
  235. if (show_always != value) {
  236. show_always = value;
  237. }
  238. }
  239. }
  240. #endregion // Public Instance Properties
  241. #region Public Instance Methods
  242. public bool CanExtend(object target) {
  243. return false;
  244. }
  245. [Localizable (true)]
  246. [DefaultValue ("")]
  247. public string GetToolTip(Control control) {
  248. string tooltip = (string)tooltip_strings[control];
  249. if (tooltip == null)
  250. return "";
  251. return tooltip;
  252. }
  253. public void RemoveAll() {
  254. tooltip_strings.Clear();
  255. controls.Clear();
  256. }
  257. public void SetToolTip(Control control, string caption) {
  258. tooltip_strings[control] = caption;
  259. // no need for duplicates
  260. if (!controls.Contains(control)) {
  261. control.MouseEnter += new EventHandler(control_MouseEnter);
  262. control.MouseMove += new MouseEventHandler(control_MouseMove);
  263. control.MouseLeave += new EventHandler(control_MouseLeave);
  264. controls.Add(control);
  265. }
  266. // if SetToolTip is called from a control and the mouse is currently over that control,
  267. // make sure that tooltip_window.Text gets updated
  268. if (caption != null && last_control == control) {
  269. Size size = ThemeEngine.Current.ToolTipSize(tooltip_window, caption);
  270. tooltip_window.Width = size.Width;
  271. tooltip_window.Height = size.Height;
  272. tooltip_window.Text = caption;
  273. }
  274. }
  275. public override string ToString() {
  276. return base.ToString() + " InitialDelay: " + initial_delay + ", ShowAlways: " + show_always;
  277. }
  278. #endregion // Public Instance Methods
  279. #region Protected Instance Methods
  280. protected override void Dispose(bool disposing) {
  281. if (disposing) {
  282. // Mop up the mess; or should we wait for the GC to kick in?
  283. timer.Stop();
  284. timer.Dispose();
  285. // Not sure if we should clean up tooltip_window
  286. tooltip_window.Dispose();
  287. tooltip_strings.Clear();
  288. controls.Clear();
  289. }
  290. }
  291. #endregion // Protected Instance Methods
  292. enum TipState {
  293. Initial,
  294. Show,
  295. Down
  296. }
  297. TipState state = TipState.Initial;
  298. #region Private Methods
  299. private void control_MouseEnter(object sender, EventArgs e)
  300. {
  301. last_control = (Control)sender;
  302. // Whatever we're displaying right now, we don't want it anymore
  303. tooltip_window.Visible = false;
  304. timer.Stop();
  305. state = TipState.Initial;
  306. // if we're in the same control as before (how'd that happen?) or if we're not active, leave
  307. if (!is_active || (active_control == (Control)sender)) {
  308. return;
  309. }
  310. if (!show_always) {
  311. IContainerControl cc = last_control.GetContainerControl ();
  312. if ((cc == null) || (cc.ActiveControl == null)) {
  313. return;
  314. }
  315. }
  316. string text = (string)tooltip_strings[sender];
  317. if (text != null && text.Length > 0) {
  318. if (active_control == null) {
  319. timer.Interval = initial_delay;
  320. } else {
  321. timer.Interval = re_show_delay;
  322. }
  323. active_control = (Control)sender;
  324. timer.Start ();
  325. }
  326. }
  327. private void timer_Tick(object sender, EventArgs e) {
  328. timer.Stop();
  329. switch (state) {
  330. case TipState.Initial:
  331. if (active_control == null)
  332. return;
  333. tooltip_window.Present (active_control, (string)tooltip_strings[active_control]);
  334. state = TipState.Show;
  335. timer.Interval = autopop_delay;
  336. timer.Start();
  337. break;
  338. case TipState.Show:
  339. tooltip_window.Visible = false;
  340. state = TipState.Down;
  341. break;
  342. default:
  343. throw new Exception ("Timer shouldn't be running in state: " + state);
  344. }
  345. }
  346. private bool MouseInControl(Control control) {
  347. Point m;
  348. Point c;
  349. Size cw;
  350. if (control == null) {
  351. return false;
  352. }
  353. m = Control.MousePosition;
  354. c = new Point(control.Bounds.X, control.Bounds.Y);
  355. if (control.parent != null) {
  356. c = control.parent.PointToScreen(c);
  357. }
  358. cw = control.ClientSize;
  359. if (c.X<=m.X && m.X<(c.X+cw.Width) &&
  360. c.Y<=m.Y && m.Y<(c.Y+cw.Height)) {
  361. return true;
  362. }
  363. return false;
  364. }
  365. private void control_MouseLeave(object sender, EventArgs e) {
  366. timer.Stop();
  367. if (!MouseInControl(tooltip_window) && !MouseInControl(active_control)) {
  368. active_control = null;
  369. tooltip_window.Visible = false;
  370. }
  371. if (last_control == (Control)sender)
  372. last_control = null;
  373. }
  374. private void control_MouseMove(object sender, MouseEventArgs e) {
  375. if (state != TipState.Down) {
  376. timer.Stop();
  377. timer.Start();
  378. }
  379. }
  380. #endregion // Private Methods
  381. }
  382. }