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 |
|
---|
32 | public ToolTipComboBox()
|
---|
33 | : base() {
|
---|
34 | SetStyle(ControlStyles.AllPaintingInWmPaint, true);
|
---|
35 | SetStyle(ControlStyles.UserPaint, true);
|
---|
36 | InitializeComponent();
|
---|
37 | DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
|
---|
38 | DropDownStyle = ComboBoxStyle.DropDownList;
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected override void OnDrawItem(DrawItemEventArgs e) {
|
---|
42 | base.OnDrawItem(e);
|
---|
43 | var args = new ToolTipRequiredEventArgs(e.Index, e.Index >= 0 ? Items[e.Index] : null);
|
---|
44 | if (e.Index >= 0) OnToolTipRequired(args);
|
---|
45 | if (!e.State.HasFlag(DrawItemState.ComboBoxEdit) && !string.IsNullOrEmpty(args.ToolTip)) {
|
---|
46 | var ttRect = TextRenderer.MeasureText(args.ToolTip, Font);
|
---|
47 | if (RectangleToScreen(e.Bounds).Right + ttRect.Width > Screen.FromControl(this).Bounds.Right)
|
---|
48 | toolTip.Show(args.ToolTip, this, e.Bounds.Left - ttRect.Width, e.Bounds.Bottom);
|
---|
49 | else toolTip.Show(args.ToolTip, this, e.Bounds.Right, e.Bounds.Bottom);
|
---|
50 | } else toolTip.Hide(this);
|
---|
51 | if (!e.State.HasFlag(DrawItemState.ComboBoxEdit))
|
---|
52 | e.DrawBackground();
|
---|
53 | if (e.Index > -1 && Items.Count > 0) {
|
---|
54 | if (!e.State.HasFlag(DrawItemState.ComboBoxEdit) && e.State.HasFlag(DrawItemState.Selected)) {
|
---|
55 | using (var b = new SolidBrush(BackColor)) {
|
---|
56 | e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, e.Bounds);
|
---|
57 | }
|
---|
58 | } else if (!e.State.HasFlag(DrawItemState.ComboBoxEdit)) {
|
---|
59 | using (var b = new SolidBrush(ForeColor)) {
|
---|
60 | e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, e.Bounds);
|
---|
61 | }
|
---|
62 | } else {
|
---|
63 | using (var b = new SolidBrush(ForeColor)) {
|
---|
64 | var rect = new Rectangle(2, 4, Size.Width - 2, Size.Height - 4);
|
---|
65 | e.Graphics.DrawString(GetItemText(Items[e.Index]), Font, b, rect);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 | e.DrawFocusRectangle();
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected override void OnPaint(PaintEventArgs e) {
|
---|
73 | base.OnPaint(e);
|
---|
74 | if (Enabled == false) comboBoxState = ComboBoxState.Disabled;
|
---|
75 | Rectangle clip = new Rectangle(new Point(0, 0), Size);
|
---|
76 | clip.Inflate(0, 1);
|
---|
77 | if (VisualStyleInformation.IsSupportedByOS && VisualStyleInformation.IsEnabledByUser) {
|
---|
78 | PushButtonState pb_State = PushButtonState.Normal;
|
---|
79 | if (comboBoxState == ComboBoxState.Disabled)
|
---|
80 | pb_State = PushButtonState.Disabled;
|
---|
81 | else if (comboBoxState == ComboBoxState.Hot)
|
---|
82 | pb_State = PushButtonState.Hot;
|
---|
83 | else if (comboBoxState == ComboBoxState.Pressed)
|
---|
84 | pb_State = PushButtonState.Pressed;
|
---|
85 |
|
---|
86 | ButtonRenderer.DrawButton(e.Graphics, clip, pb_State);
|
---|
87 | var r = new Rectangle(clip.Right - SystemInformation.VerticalScrollBarWidth + 2,
|
---|
88 | clip.Top,
|
---|
89 | SystemInformation.VerticalScrollBarWidth - 4,
|
---|
90 | clip.Height);
|
---|
91 | DrawArrow(e.Graphics, r);
|
---|
92 | } else {
|
---|
93 | ComboBoxRenderer.DrawTextBox(e.Graphics, clip, comboBoxState);
|
---|
94 | var r = new Rectangle(clip.Right - SystemInformation.VerticalScrollBarWidth,
|
---|
95 | clip.Top,
|
---|
96 | SystemInformation.VerticalScrollBarWidth,
|
---|
97 | clip.Height);
|
---|
98 | ComboBoxRenderer.DrawDropDownButton(e.Graphics, r, comboBoxState);
|
---|
99 | }
|
---|
100 | var rect = new Rectangle(2, 4, Size.Width - 2, Size.Height - 4);
|
---|
101 | if (SelectedIndex > -1 && Items.Count > 0) {
|
---|
102 | using (var b = new SolidBrush(ForeColor)) {
|
---|
103 | e.Graphics.DrawString(GetItemText(Items[SelectedIndex]), Font, b, rect);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void DrawArrow(Graphics g, Rectangle rect) {
|
---|
109 | Point p1 = new Point(rect.X + 3, rect.Y + rect.Height / 2 - 2);
|
---|
110 | Point p2 = new Point(rect.X + rect.Width - 3, rect.Y + rect.Height / 2 - 2);
|
---|
111 | Point p3 = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2 + 2);
|
---|
112 | g.FillPolygon(new SolidBrush(System.Drawing.SystemColors.ControlText), new Point[] { p1, p2, p3 });
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected override void OnDropDown(EventArgs e) {
|
---|
116 | base.OnDropDown(e);
|
---|
117 | comboBoxState = ComboBoxState.Pressed;
|
---|
118 | Invalidate();
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected override void OnDropDownClosed(EventArgs e) {
|
---|
122 | base.OnDropDownClosed(e);
|
---|
123 | toolTip.Hide(this);
|
---|
124 | comboBoxState = ComboBoxState.Normal;
|
---|
125 | Invalidate();
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected override void OnMouseEnter(EventArgs e) {
|
---|
129 | base.OnMouseEnter(e);
|
---|
130 | if (comboBoxState == ComboBoxState.Normal) {
|
---|
131 | comboBoxState = ComboBoxState.Hot;
|
---|
132 | Invalidate();
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected override void OnMouseHover(EventArgs e) {
|
---|
137 | base.OnMouseHover(e);
|
---|
138 | if (comboBoxState == ComboBoxState.Normal) {
|
---|
139 | comboBoxState = ComboBoxState.Hot;
|
---|
140 | Invalidate();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | protected override void OnMouseLeave(EventArgs e) {
|
---|
145 | base.OnMouseLeave(e);
|
---|
146 | if (comboBoxState == ComboBoxState.Hot) {
|
---|
147 | comboBoxState = ComboBoxState.Normal;
|
---|
148 | Invalidate();
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | protected override void OnMouseDown(MouseEventArgs e) {
|
---|
153 | base.OnMouseDown(e);
|
---|
154 | if (e.Button == MouseButtons.Left) {
|
---|
155 | comboBoxState = ComboBoxState.Pressed;
|
---|
156 | Invalidate();
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | public event EventHandler<ToolTipRequiredEventArgs> ToolTipRequired;
|
---|
161 | protected virtual void OnToolTipRequired(ToolTipRequiredEventArgs args) {
|
---|
162 | var handler = ToolTipRequired;
|
---|
163 | if (handler != null) handler(this, args);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|