This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiSelectColumnCustomizationListBox.cs
146 lines (126 loc) · 4.59 KB
/
MultiSelectColumnCustomizationListBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils.Drawing;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid.Views.Grid.Customization;
namespace MultiSelectColumnCustomization
{
public class MultiSelectColumnCustomizationListBox : ColumnCustomizationListBox
{
private int pushedIndex = -1;
List<object> checkedItems = new List<object>();
object focusedItem = null;
public MultiSelectColumnCustomizationListBox(CustomizationForm form)
: base(form)
{
}
protected Rectangle CalcCheckBoxRect(GraphicsCache cache, Rectangle bounds)
{
RepositoryItemCheckEdit checkBox = new RepositoryItemCheckEdit();
CheckEditViewInfo viewInfo = (CheckEditViewInfo)checkBox.CreateViewInfo();
viewInfo.Bounds = bounds;
viewInfo.CalcViewInfo(cache.Graphics);
ControlGraphicsInfoArgs args = new ControlGraphicsInfoArgs(viewInfo, cache, bounds);
BaseCheckObjectInfoArgs ci = ((CheckEditViewInfo)args.ViewInfo).CheckInfo;
Rectangle retValue = new Rectangle(bounds.X, ci.GlyphRect.Y, ci.GlyphRect.Width, ci.GlyphRect.Height);
return retValue;
}
protected override void DrawItemObject(GraphicsCache cache, int index, Rectangle bounds, DrawItemState itemState)
{
Rectangle checkBoxRect = CalcCheckBoxRect(cache, bounds);
bounds.X += checkBoxRect.Width;
bounds.Width -= checkBoxRect.Width;
base.DrawItemObject(cache, index, bounds, itemState);
ButtonState checkState = ButtonState.Normal;
if ( index == pushedIndex )
checkState = ButtonState.Pushed;
if ( checkedItems.Contains(Items[index]) )
checkState = ButtonState.Checked;
cache.Paint.DrawCheckBox(cache.Graphics, checkBoxRect, checkState);
if ( focusedItem != null && Items.IndexOf(focusedItem) == index )
cache.Paint.DrawFocusRectangle(cache.Graphics, checkBoxRect, CustomizationForm.ForeColor, CustomizationForm.BackColor);
}
protected override void OnMouseDown(MouseEventArgs e)
{
Point mousePoint = new Point(e.X, e.Y);
object pointedItem = ItemByPoint(new Point(e.X, e.Y));
int itemIndex = Items.IndexOf(pointedItem);
Rectangle itemRect = GetItemRectangle(itemIndex);
GraphicsInfo.Default.AddGraphics(null);
Rectangle checkBoxRect;
try {
checkBoxRect = CalcCheckBoxRect(GraphicsInfo.Default.Cache, itemRect);
}
finally {
GraphicsInfo.Default.ReleaseGraphics();
}
if ( checkBoxRect.Contains(mousePoint) && e.Button == MouseButtons.Left )
{
pushedIndex = itemIndex;
this.InvalidateObject(pointedItem);
return;
} else if ( !checkBoxRect.Contains(mousePoint) )
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
Point mousePoint = new Point(e.X, e.Y);
object pointedItem = ItemByPoint(new Point(e.X, e.Y));
int itemIndex = Items.IndexOf(pointedItem);
Rectangle itemRect = GetItemRectangle(itemIndex);
GraphicsInfo.Default.AddGraphics(null);
Rectangle checkBoxRect;
try {
checkBoxRect = CalcCheckBoxRect(GraphicsInfo.Default.Cache, itemRect);
}
finally {
GraphicsInfo.Default.ReleaseGraphics();
}
if ( checkBoxRect.Contains(mousePoint) && e.Button == MouseButtons.Left )
{
if ( ModifierKeys == Keys.Shift )
{
int startIndex = Items.IndexOf(focusedItem);
int endIndex = Items.IndexOf(pointedItem);
bool check = !checkedItems.Contains(pointedItem);
if ( endIndex >= startIndex )
for ( int i = startIndex; i <= endIndex; i++ )
{
if ( check && !checkedItems.Contains(Items[i]) )
checkedItems.Add(Items[i]);
else if ( !check && checkedItems.Contains(Items[i]) )
checkedItems.Remove(Items[i]);
} else
for ( int i = endIndex; i < startIndex; i++ )
{
if ( check && !checkedItems.Contains(Items[i]) )
checkedItems.Add(Items[i]);
else if ( !check && checkedItems.Contains(Items[i]) )
checkedItems.Remove(Items[i]);
}
} else if ( ModifierKeys == Keys.None )
{
if ( checkedItems.Contains(pointedItem) )
checkedItems.Remove(pointedItem);
else
checkedItems.Add(pointedItem);
}
focusedItem = pointedItem;
pushedIndex = -1;
this.InvalidateObject(pointedItem);
return;
} else if ( !checkBoxRect.Contains(mousePoint) )
base.OnMouseUp(e);
}
public List<object> CheckedItems
{
get
{
return checkedItems;
}
}
}
}