-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColumnInplaceEditorHelper.cs
148 lines (137 loc) · 4.69 KB
/
ColumnInplaceEditorHelper.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Drawing;
using DevExpress.Utils;
namespace WindowsApplication1
{
public class ColumnInplaceEditorHelper
{
private RepositoryItem _Item;
private GridColumn _Column;
GridView view;
public ColumnInplaceEditorHelper(GridColumn column, RepositoryItem inplaceEditor)
{
_Column = column;
_Item = inplaceEditor;
view = column.View as GridView;
view.CustomDrawColumnHeader += view_CustomDrawColumnHeader;
view.MouseDown += view_MouseDown;
view.Layout += new EventHandler(view_Layout);
}
void view_Layout(object sender, EventArgs e)
{
CloseEditor();
}
private object _EditValue;
public object EditValue
{
get { return _EditValue; }
set
{
_EditValue = value;
}
}
private BaseEdit _ActiveEditor;
public BaseEdit ActiveEditor
{
get { return _ActiveEditor; }
set { _ActiveEditor = value; }
}
void view_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == _Column)
{
e.Info.Caption = string.Empty;
e.Painter.DrawObject(e.Info);
e.Handled = true;
DrawEditorHelper.DrawColumnInplaceEditor(e, _Item, EditValue, GetRightIndent());
}
}
public int GetRightIndent()
{
return _Column.OptionsColumn.AllowSort != DevExpress.Utils.DefaultBoolean.False || _Column.OptionsFilter.AllowFilter ? 25 : 0;
}
void view_MouseDown(object sender, MouseEventArgs e)
{
CloseEditor();
Rectangle editorBounds;
if (ClickInEditor(e, out editorBounds))
{
ShowEditor(editorBounds);
DXMouseEventArgs.GetMouseArgs(e).Handled = true;
}
}
private bool ClickInEditor(MouseEventArgs e, out Rectangle editorBounds)
{
editorBounds = Rectangle.Empty;
GridViewInfo vi = view.GetViewInfo() as GridViewInfo;
GridColumnInfoArgs columnInfo = CalcColumnHitInfo(e.Location, vi.ColumnsInfo);
if (columnInfo != null && columnInfo.Column == _Column)
{
editorBounds = DrawEditorHelper.GetEditorBounds(columnInfo.Bounds, GetRightIndent());
return editorBounds.Contains(e.Location);
}
return false;
}
protected virtual GridColumnInfoArgs CalcColumnHitInfo(Point pt, GridColumnsInfo cols)
{
foreach (GridColumnInfoArgs ci in cols)
{
if (!ci.Bounds.IsEmpty && IntInRange(pt.X, ci.Bounds.Left, ci.Bounds.Right))
{
if (ci.Type == GridColumnInfoType.EmptyColumn) continue;
return ci;
}
}
return null;
}
protected bool IntInRange(int x, int left, int right)
{
if (right < left)
{
int temp = left;
left = right;
right = temp;
}
return (x >= left && x < right);
}
private void ShowEditor(Rectangle bounds)
{
ActiveEditor = _Item.CreateEditor();
ActiveEditor.Properties.LockEvents();
ActiveEditor.Parent = view.GridControl;
ActiveEditor.Properties.Assign(_Item);
ActiveEditor.Properties.AutoHeight = false;
ActiveEditor.Location = bounds.Location;
ActiveEditor.Size = bounds.Size;
ActiveEditor.CreateControl();
ActiveEditor.EditValue = EditValue;
ActiveEditor.SendMouse(ActiveEditor.PointToClient(Control.MousePosition), Control.MouseButtons);
ActiveEditor.Properties.UnLockEvents();
}
private void CloseEditor()
{
if (ActiveEditor != null)
{
EditValue = ActiveEditor.EditValue;
ActiveEditor.Dispose();
ActiveEditor = null;
}
}
void editor_Leave(object sender, EventArgs e)
{
CloseEditor();
}
}
}