-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupEditProvider.cs
255 lines (232 loc) · 9.37 KB
/
GroupEditProvider.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using System.Drawing;
using System.Collections;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
namespace DXSample {
public class GroupEditProvider
{
GridView view;
Dictionary<Type, BaseEdit> editors;
Dictionary<Type, BaseEditPainter> painters;
Dictionary<Type, BaseEditViewInfo> info;
Rectangle hotTrackRectangle;
BaseEdit groupEdit;
bool showGroupEditorOnMouseHover = false;
bool singleClick = false;
public GroupEditProvider(GridView view)
{
this.view = view;
painters = new Dictionary<Type, BaseEditPainter>();
editors = new Dictionary<Type, BaseEdit>();
info = new Dictionary<Type, BaseEditViewInfo>();
hotTrackRectangle = Rectangle.Empty;
}
public bool ShowGroupEditorOnMouseHover
{
get { return showGroupEditorOnMouseHover; }
set
{
if (showGroupEditorOnMouseHover != value)
showGroupEditorOnMouseHover = value;
}
}
public bool SingleClick
{
get { return singleClick; }
set
{
if (singleClick != value)
singleClick = value;
}
}
Rectangle HotTrackRectangle
{
get { return hotTrackRectangle; }
set
{
if (hotTrackRectangle == value)
return;
view.InvalidateRect(hotTrackRectangle);
view.InvalidateRect(value);
hotTrackRectangle = value;
}
}
public void EnableGroupEditing()
{
view.CustomDrawGroupRow += OnCustomDrawGroupRow;
view.MouseDown += OnMouseDown;
view.MouseMove += OnMouseMove;
view.TopRowChanged += OnTopRowChanged;
view.GridControl.Resize += OnGridControlResize;
}
void OnGridControlResize(object sender, EventArgs e)
{
HideGroupEditor(groupEdit, false);
}
void OnTopRowChanged(object sender, EventArgs e)
{
HideGroupEditor(groupEdit, false);
}
void OnMouseMove(object sender, MouseEventArgs e)
{
GridHitInfo hitInfo = view.CalcHitInfo(e.Location);
if (hitInfo.HitTest == GridHitTest.Row && view.IsGroupRow(hitInfo.RowHandle))
{
GridViewInfo viewInfo = view.GetViewInfo() as GridViewInfo;
GridGroupRowInfo rowInfo = viewInfo.GetGridRowInfo(hitInfo.RowHandle) as GridGroupRowInfo;
GridColumn col = viewInfo.GetNearestColumn(e.Location);
GridColumnInfoArgs args = viewInfo.ColumnsInfo[col];
if (col.VisibleIndex == 0)
HotTrackRectangle = new Rectangle(rowInfo.ButtonBounds.Right + 2, rowInfo.Bounds.Y,
args.Bounds.Width + args.Bounds.X - rowInfo.ButtonBounds.Right - 2, rowInfo.Bounds.Height);
else
HotTrackRectangle = new Rectangle(args.Bounds.X + 2, rowInfo.Bounds.Y,
args.Bounds.Width - 2, rowInfo.Bounds.Height);
}
else
HotTrackRectangle = Rectangle.Empty;
}
void OnMouseDown(object sender, MouseEventArgs e)
{
GridHitInfo hitInfo = view.CalcHitInfo(e.Location);
if (e.Button == MouseButtons.Left && HotTrackRectangle.Contains(e.Location))
{
if ((SingleClick && e.Clicks == 1) || (!SingleClick && e.Clicks == 2))
{
view.FocusedRowHandle = hitInfo.RowHandle;
GridViewInfo viewInfo = view.GetViewInfo() as GridViewInfo;
GridColumn col = viewInfo.GetNearestColumn(e.Location);
ShowGroupEditor(col);
DXMouseEventArgs.GetMouseArgs(e).Handled = true;
}
}
}
private void ShowGroupEditor(GridColumn column)
{
groupEdit = GetGroupEditor(column);
groupEdit.Visible = true;
groupEdit.Bounds = HotTrackRectangle;
groupEdit.Tag = column.FieldName;
groupEdit.Select();
}
private BaseEdit GetGroupEditor(GridColumn column)
{
Type editorType = column.RealColumnEdit.GetType();
if (editors.ContainsKey(editorType)) return editors[editorType];
BaseEdit groupEdit = column.RealColumnEdit.CreateEditor();
groupEdit.Parent = view.GridControl;
if (groupEdit is CheckEdit)
(groupEdit as CheckEdit).Properties.GlyphAlignment = HorzAlignment.Center;
groupEdit.KeyDown += OnGroupEditKeyDown;
groupEdit.Validated += OnGroupEditValidated;
editors.Add(editorType, groupEdit);
return groupEdit;
}
void OnGroupEditKeyDown(object sender, KeyEventArgs e)
{
BaseEdit edit = sender as BaseEdit;
if (e.KeyData == Keys.Enter)
{
HideGroupEditor(edit, true);
e.Handled = true;
}
if (e.KeyData == Keys.Escape)
{
HideGroupEditor(edit, false);
e.Handled = true;
}
}
private void HideGroupEditor(BaseEdit edit, bool updateValues)
{
if (edit == null || !edit.Visible) return;
if (edit.IsModified && edit.EditValue != null && updateValues)
SetChildRowValues(edit);
edit.EditValue = null;
edit.Visible = false;
}
void OnGroupEditValidated(object sender, EventArgs e)
{
BaseEdit edit = sender as BaseEdit;
HideGroupEditor(edit, true);
}
private void SetChildRowValues(BaseEdit edit)
{
int groupRowHandle = view.FocusedRowHandle;
int childRowCount = view.GetChildRowCount(groupRowHandle);
string fieldName = edit.Tag.ToString();
for (int i = 0; i < childRowCount; i++)
{
int childRowHandle = view.GetChildRowHandle(groupRowHandle, i);
view.SetRowCellValue(childRowHandle, fieldName, edit.EditValue);
}
}
void OnCustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
{
if (ShowGroupEditorOnMouseHover && HotTrackRectangle != Rectangle.Empty
&& e.Info.Bounds.Contains(HotTrackRectangle.Location)) {
DrawGroupRow(e);
DrawGroupEditor(e);
e.Handled = true;
}
}
private void DrawGroupEditor(DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
{
GridViewInfo viewInfo = view.GetViewInfo() as GridViewInfo;
GridColumn col = viewInfo.GetNearestColumn(HotTrackRectangle.Location);
BaseEditPainter groupEditPainter = GetGroupEditPainter(col);
BaseEditViewInfo editViewInfo = GetGroupEditViewInfo(col);
editViewInfo.Bounds = HotTrackRectangle;
editViewInfo.CalcViewInfo(e.Graphics);
e.Cache.FillRectangle(viewInfo.PaintAppearance.Row.GetBackBrush(e.Cache), HotTrackRectangle);
groupEditPainter.Draw(new ControlGraphicsInfoArgs(editViewInfo, e.Cache, HotTrackRectangle));
}
private void DrawGroupRow(DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
{
e.Appearance.FillRectangle(e.Cache, e.Bounds);
e.Painter.DrawObject(e.Info);
}
private BaseEditPainter GetGroupEditPainter(GridColumn col)
{
Type editorType = col.RealColumnEdit.GetType();
if (painters.ContainsKey(editorType)) return painters[editorType];
painters.Add(editorType, col.RealColumnEdit.CreatePainter());
return painters[editorType];
}
private BaseEditViewInfo GetGroupEditViewInfo(GridColumn col)
{
Type editorType = col.RealColumnEdit.GetType();
if (info.ContainsKey(editorType)) return info[editorType];
info.Add(editorType, col.RealColumnEdit.CreateViewInfo());
return info[editorType];
}
public void DisableGroupEditing(){
foreach (BaseEdit edit in editors.Values)
{
edit.Parent = null;
edit.Validated -= OnGroupEditValidated;
edit.KeyDown -= OnGroupEditKeyDown;
edit.Dispose();
}
hotTrackRectangle = Rectangle.Empty;
editors.Clear();
painters.Clear();
info.Clear();
view.CustomDrawGroupRow -= OnCustomDrawGroupRow;
view.MouseDown -= OnMouseDown;
view.MouseMove -= OnMouseMove;
view.TopRowChanged -= OnTopRowChanged;
view.GridControl.Resize -= OnGridControlResize;
}
}
}