1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using System.Windows.Forms.VisualStyles;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views {
|
---|
28 | public partial class ToolTipComboBox : ComboBox {
|
---|
29 |
|
---|
30 | private ComboBoxState comboBoxState = ComboBoxState.Normal;
|
---|
31 | private bool mouseOver = false;
|
---|
32 |
|
---|
33 | public ToolTipComboBox()
|
---|
34 | : base() {
|
---|
35 | SetStyle(ControlStyles.AllPaintingInWmPaint, true);
|
---|
36 | SetStyle(ControlStyles.UserPaint, true);
|
---|
37 | InitializeComponent();
|
---|
38 | DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
|
---|
39 | DropDownStyle = ComboBoxStyle.DropDownList;
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected override void OnMeasureItem(MeasureItemEventArgs e) {
|
---|
43 | base.OnMeasureItem(e);
|
---|
44 | string text = GetItemText(Items[e.Index]);
|
---|
45 | if (string.IsNullOrEmpty(text)) text = "test";
|
---|
46 | var rect = TextRenderer.MeasureText(text, Font);
|
---|
47 | e.ItemHeight = rect.Height - 2;
|
---|
48 | e.ItemWidth = rect.Width;
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected override void OnDrawItem(DrawItemEventArgs e) {
|
---|
52 | base.OnDrawItem(e);
|
---|
53 | var args = new ToolTipRequiredEventArgs(e.Index, e.Index >= 0 ? Items[e.Index] : null);
|
---|
54 | if (e.Index >= 0) OnToolTipRequired(args);
|
---|
55 | if (!e.State.HasFlag(DrawItemState.ComboBoxEdit) && !string.IsNullOrEmpty(args.ToolTip)) {
|
---|
56 | var ttRect = TextRenderer.MeasureText(args.ToolTip, Font);
|
---|
57 | if (RectangleToScreen(e.Bounds).Right + ttRect.Width > Screen.FromControl(this).Bounds.Right)
|
---|
58 | toolTip.Show(args.ToolTip, this, e.Bounds.Left - ttRect.Width, e.Bounds.Bottom);
|
---|
59 | else toolTip.Show(args.ToolTip, this, e.Bounds.Right, e.Bounds.Bottom);
|
---|
60 | } else toolTip.Hide(this);
|
---|
61 | if (!e.State.HasFlag(DrawItemState.ComboBoxEdit))
|
---|
62 | e.DrawBackground();
|
---|
63 | if (e.Index > -1 && Items.Count > 0) {
|
---|
64 | if (!e.State.HasFlag(DrawItemState.ComboBoxEdit) && e.State.HasFlag(DrawItemState.Selected)) {
|
---|
65 | using (var b = new SolidBrush(BackColor)) {
|
---|
66 | e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, e.Bounds);
|
---|
67 | }
|
---|
68 | } else if (!e.State.HasFlag(DrawItemState.ComboBoxEdit)) {
|
---|
69 | using (var b = new SolidBrush(ForeColor)) {
|
---|
70 | e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, e.Bounds);
|
---|
71 | }
|
---|
72 | } else {
|
---|
73 | using (var b = new SolidBrush(ForeColor)) {
|
---|
74 | Rectangle r = e.Bounds;
|
---|
75 | r.Offset(0, 1);
|
---|
76 | e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, r);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | e.DrawFocusRectangle();
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected override void OnPaint(PaintEventArgs e) {
|
---|
84 | base.OnPaint(e);
|
---|
85 | if (Enabled == false) comboBoxState = ComboBoxState.Disabled;
|
---|
86 | Rectangle clip = Bounds; // using e.ClipRectangle leads to strange behavior when maximizing/unmaximizing, just redraw the entire region all the time
|
---|
87 | clip.Offset(-clip.X, -clip.Y); // x and y need to be 0
|
---|
88 | if (VisualStyleInformation.IsSupportedByOS && VisualStyleInformation.IsEnabledByUser) {
|
---|
89 | PushButtonState pb_State = PushButtonState.Normal;
|
---|
90 | if (comboBoxState == ComboBoxState.Disabled)
|
---|
91 | pb_State = PushButtonState.Disabled;
|
---|
92 | else if (comboBoxState == ComboBoxState.Hot)
|
---|
93 | pb_State = PushButtonState.Hot;
|
---|
94 | else if (comboBoxState == ComboBoxState.Pressed)
|
---|
95 | pb_State = PushButtonState.Pressed;
|
---|
96 |
|
---|
97 | ButtonRenderer.DrawButton(e.Graphics, clip, pb_State);
|
---|
98 | var r = new Rectangle(clip.Right - SystemInformation.VerticalScrollBarWidth + 2,
|
---|
99 | clip.Top,
|
---|
100 | SystemInformation.VerticalScrollBarWidth - 4,
|
---|
101 | clip.Height);
|
---|
102 | DrawArrow(e.Graphics, r);
|
---|
103 | } else {
|
---|
104 | ComboBoxRenderer.DrawTextBox(e.Graphics, clip, comboBoxState);
|
---|
105 | var r = new Rectangle(clip.Right - SystemInformation.VerticalScrollBarWidth,
|
---|
106 | clip.Top,
|
---|
107 | SystemInformation.VerticalScrollBarWidth,
|
---|
108 | clip.Height);
|
---|
109 | ComboBoxRenderer.DrawDropDownButton(e.Graphics, r, comboBoxState);
|
---|
110 | }
|
---|
111 | Rectangle rect = clip;
|
---|
112 | rect.Inflate(-2, -4);
|
---|
113 | rect.Offset(1, 0);
|
---|
114 | if (SelectedIndex > -1 && Items.Count > 0) {
|
---|
115 | using (var b = new SolidBrush(ForeColor)) {
|
---|
116 | e.Graphics.DrawString(GetItemText(Items[SelectedIndex]), Font, b, rect);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void DrawArrow(Graphics g, Rectangle rect) {
|
---|
122 | Point p1 = new Point(rect.X + 3, rect.Y + rect.Height / 2 - 2);
|
---|
123 | Point p2 = new Point(rect.X + rect.Width - 3, rect.Y + rect.Height / 2 - 2);
|
---|
124 | Point p3 = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2 + 2);
|
---|
125 | g.FillPolygon(new SolidBrush(System.Drawing.SystemColors.ControlText), new Point[] { p1, p2, p3 });
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected override void OnDropDown(EventArgs e) {
|
---|
129 | base.OnDropDown(e);
|
---|
130 | comboBoxState = ComboBoxState.Pressed;
|
---|
131 | Invalidate();
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected override void OnDropDownClosed(EventArgs e) {
|
---|
135 | base.OnDropDownClosed(e);
|
---|
136 | toolTip.Hide(this);
|
---|
137 | comboBoxState = ComboBoxState.Normal;
|
---|
138 | Invalidate();
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected override void OnMouseEnter(EventArgs e) {
|
---|
142 | base.OnMouseEnter(e);
|
---|
143 | mouseOver = true;
|
---|
144 | if (comboBoxState == ComboBoxState.Normal) {
|
---|
145 | comboBoxState = ComboBoxState.Hot;
|
---|
146 | Invalidate();
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | protected override void OnMouseHover(EventArgs e) {
|
---|
151 | base.OnMouseHover(e);
|
---|
152 | if (comboBoxState == ComboBoxState.Normal) {
|
---|
153 | comboBoxState = ComboBoxState.Hot;
|
---|
154 | Invalidate();
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | protected override void OnMouseLeave(EventArgs e) {
|
---|
159 | base.OnMouseLeave(e);
|
---|
160 | mouseOver = false;
|
---|
161 | if (comboBoxState == ComboBoxState.Hot) {
|
---|
162 | comboBoxState = ComboBoxState.Normal;
|
---|
163 | Invalidate();
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | protected override void OnMouseDown(MouseEventArgs e) {
|
---|
168 | base.OnMouseDown(e);
|
---|
169 | comboBoxState = ComboBoxState.Pressed;
|
---|
170 | Invalidate();
|
---|
171 | }
|
---|
172 |
|
---|
173 | public event EventHandler<ToolTipRequiredEventArgs> ToolTipRequired;
|
---|
174 | protected virtual void OnToolTipRequired(ToolTipRequiredEventArgs args) {
|
---|
175 | var handler = ToolTipRequired;
|
---|
176 | if (handler != null) handler(this, args);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|