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 | var args = new ToolTipRequiredEventArgs(e.Index, Items[e.Index]);
|
---|
43 | OnToolTipRequired(args);
|
---|
44 | if (!e.State.HasFlag(DrawItemState.ComboBoxEdit) && !string.IsNullOrEmpty(args.ToolTip)) {
|
---|
45 | var ttRect = e.Graphics.MeasureString(args.ToolTip, Font);
|
---|
46 | if (RectangleToScreen(e.Bounds).Right + ttRect.Width > Screen.FromControl(this).Bounds.Right)
|
---|
47 | toolTip.Show(args.ToolTip, this, e.Bounds.Left - (int)Math.Ceiling(ttRect.Width), e.Bounds.Bottom);
|
---|
48 | else toolTip.Show(args.ToolTip, this, e.Bounds.Right, e.Bounds.Bottom);
|
---|
49 | } else toolTip.Hide(this);
|
---|
50 | e.DrawBackground();
|
---|
51 | DrawItemText(e.Graphics, e.Bounds, e.Index);
|
---|
52 | e.DrawFocusRectangle();
|
---|
53 | base.OnDrawItem(e);
|
---|
54 | }
|
---|
55 | protected override void OnDropDownClosed(EventArgs e) {
|
---|
56 | base.OnDropDownClosed(e);
|
---|
57 | toolTip.Hide(this);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void DrawItemText(Graphics graphics, Rectangle rectangle, int index) {
|
---|
61 | if (index == -1 || Items.Count == 0)
|
---|
62 | return;
|
---|
63 | using (var b = new SolidBrush(ForeColor)) {
|
---|
64 | graphics.DrawString(GetItemText(Items[index]), Font, b, rectangle);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void OnPaint(PaintEventArgs e) {
|
---|
69 | base.OnPaint(e);
|
---|
70 | if (Enabled == false) comboBoxState = ComboBoxState.Disabled;
|
---|
71 | if (VisualStyleInformation.IsSupportedByOS && VisualStyleInformation.IsEnabledByUser) {
|
---|
72 | PushButtonState pb_State = PushButtonState.Normal;
|
---|
73 | if (comboBoxState == ComboBoxState.Disabled)
|
---|
74 | pb_State = PushButtonState.Disabled;
|
---|
75 | else if (comboBoxState == ComboBoxState.Hot)
|
---|
76 | pb_State = PushButtonState.Hot;
|
---|
77 | else if (comboBoxState == ComboBoxState.Pressed)
|
---|
78 | pb_State = PushButtonState.Pressed;
|
---|
79 | Rectangle r = e.ClipRectangle;
|
---|
80 | r.Inflate(1, 1);
|
---|
81 | ButtonRenderer.DrawButton(e.Graphics, r, pb_State);
|
---|
82 | r = new Rectangle(e.ClipRectangle.Right - SystemInformation.VerticalScrollBarWidth + 2,
|
---|
83 | e.ClipRectangle.Top,
|
---|
84 | SystemInformation.VerticalScrollBarWidth - 4,
|
---|
85 | e.ClipRectangle.Height);
|
---|
86 | DrawArrow(e.Graphics, r);
|
---|
87 | } else {
|
---|
88 | ComboBoxRenderer.DrawTextBox(e.Graphics, e.ClipRectangle, comboBoxState);
|
---|
89 | Rectangle r = new Rectangle(e.ClipRectangle.Right - SystemInformation.VerticalScrollBarWidth,
|
---|
90 | e.ClipRectangle.Top,
|
---|
91 | SystemInformation.VerticalScrollBarWidth,
|
---|
92 | e.ClipRectangle.Height);
|
---|
93 | ComboBoxRenderer.DrawDropDownButton(e.Graphics, r, comboBoxState);
|
---|
94 | }
|
---|
95 | Rectangle rect = e.ClipRectangle;
|
---|
96 | rect.Inflate(-2, -4);
|
---|
97 | rect.Offset(1, 0);
|
---|
98 | DrawItemText(e.Graphics, rect, SelectedIndex);
|
---|
99 | }
|
---|
100 |
|
---|
101 | private void DrawArrow(Graphics g, Rectangle rect) {
|
---|
102 | Point p1 = new Point(rect.X + 3, rect.Y + rect.Height / 2 - 2);
|
---|
103 | Point p2 = new Point(rect.X + rect.Width - 3, rect.Y + rect.Height / 2 - 2);
|
---|
104 | Point p3 = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2 + 2);
|
---|
105 | g.FillPolygon(new SolidBrush(System.Drawing.SystemColors.ControlText), new Point[] { p1, p2, p3 });
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected override void OnMouseEnter(EventArgs e) {
|
---|
109 | base.OnMouseEnter(e);
|
---|
110 | comboBoxState = ComboBoxState.Hot;
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected override void OnMouseLeave(EventArgs e) {
|
---|
114 | base.OnMouseLeave(e);
|
---|
115 | comboBoxState = ComboBoxState.Normal;
|
---|
116 | }
|
---|
117 |
|
---|
118 | protected override void OnMouseDown(MouseEventArgs e) {
|
---|
119 | base.OnMouseDown(e);
|
---|
120 | comboBoxState = ComboBoxState.Pressed;
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected override void OnMouseUp(MouseEventArgs e) {
|
---|
124 | base.OnMouseUp(e);
|
---|
125 | if (Top <= e.Y && Bottom >= e.Y && Left < e.X && Right > e.X)
|
---|
126 | comboBoxState = ComboBoxState.Hot;
|
---|
127 | else comboBoxState = ComboBoxState.Normal;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public event EventHandler<ToolTipRequiredEventArgs> ToolTipRequired;
|
---|
131 | protected virtual void OnToolTipRequired(ToolTipRequiredEventArgs args) {
|
---|
132 | var handler = ToolTipRequired;
|
---|
133 | if (handler != null) handler(this, args);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|