tangxu
2024-10-22 4d9fe5ed98ceb6b8fe9dc52ebfb80860ad1aee99
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
256
// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/antdui/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620
 
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
 
namespace AntdUI
{
    partial class Table
    {
        #region 编辑模式
 
        bool inEditMode = false;
        void EditModeClose()
        {
            if (inEditMode)
            {
                ScrollBar.OnInvalidate = null;
                Focus();
                inEditMode = false;
            }
        }
 
        void OnEditMode(RowTemplate it, TCell cell, int i_row, int i_col, int sx, int sy)
        {
            if (rows == null) return;
            if (it.AnimationHover)
            {
                it.ThreadHover?.Dispose();
                it.ThreadHover = null;
            }
            TCellColumn column = (TCellColumn)rows[0].cells[i_col];
            bool multiline = column.column.LineBreak;
            if (cell is TCellText cellText)
            {
                object? value = null;
                if (cell.PROPERTY != null && cell.VALUE != null) value = cell.PROPERTY.GetValue(cell.VALUE);
                else if (cell.VALUE is AntItem item) value = item.value;
                else value = cell.VALUE;
 
                bool isok = true;
                if (CellBeginEdit != null) isok = CellBeginEdit(this, new TableEventArgs(value, it.RECORD, i_row, i_col));
                if (!isok) return;
                inEditMode = true;
 
                ScrollBar.OnInvalidate = () => EditModeClose();
                BeginInvoke(new Action(() =>
                {
                    for (int i = 0; i < rows.Length; i++) rows[i].hover = i == i_row;
                    int height = Helper.GDI(g =>
                    {
                        if (multiline) return (int)Math.Ceiling(g.MeasureString(value?.ToString(), Font, cell.rect.Width).Height * 1.4F);
                        return (int)Math.Ceiling(g.MeasureString(Config.NullText, Font).Height * 1.66F);
                    });
                    var edit_input = ShowInput(cell, sx, sy, height, multiline, value, _value =>
                    {
                        bool isok_end = true;
                        if (CellEndEdit != null) isok_end = CellEndEdit(this, new TableEndEditEventArgs(_value, it.RECORD, i_row, i_col));
                        if (isok_end)
                        {
                            cellText.value = _value;
                            if (GetValue(value, _value, out var o))
                            {
                                if (it.RECORD is DataRow datarow) datarow[i_col] = o;
                                else SetValue(cell, o);
                            }
                        }
                    });
                    if (cellText.column.Align == ColumnAlign.Center) edit_input.TextAlign = HorizontalAlignment.Center;
                    else if (cellText.column.Align == ColumnAlign.Right) edit_input.TextAlign = HorizontalAlignment.Right;
                    CellBeginEditInputStyle?.Invoke(this, new TableBeginEditInputStyleEventArgs(value, it.RECORD, i_row, i_col, ref edit_input));
                    Controls.Add(edit_input);
                    edit_input.Focus();
                }));
            }
            else if (cell is Template templates)
            {
                foreach (ITemplate template in templates.value)
                {
                    if (template.Value is CellText text)
                    {
                        object? value = null;
                        if (cell.PROPERTY != null && cell.VALUE != null) value = cell.PROPERTY.GetValue(cell.VALUE);
                        else if (cell.VALUE is AntItem item) value = item.value;
                        else value = cell.VALUE;
                        bool isok = true;
                        if (CellBeginEdit != null) isok = CellBeginEdit(this, new TableEventArgs(value, it.RECORD, i_row, i_col));
                        if (!isok) return;
                        inEditMode = true;
 
                        ScrollBar.OnInvalidate = () => EditModeClose();
                        BeginInvoke(new Action(() =>
                        {
                            for (int i = 0; i < rows.Length; i++)
                            {
                                rows[i].hover = i == i_row;
                            }
                            int height = Helper.GDI(g =>
                            {
                                if (multiline) return (int)Math.Ceiling(g.MeasureString(value?.ToString(), Font, cell.rect.Width).Height * 1.4F);
                                return (int)Math.Ceiling(g.MeasureString(Config.NullText, Font).Height * 1.66F);
                            });
                            var edit_input = ShowInput(cell, sx, sy, height, multiline, value, _value =>
                            {
                                bool isok_end = true;
                                if (CellEndEdit != null) isok_end = CellEndEdit(this, new TableEndEditEventArgs(_value, it.RECORD, i_row, i_col));
                                if (isok_end)
                                {
                                    if (value is CellText text2)
                                    {
                                        text2.Text = _value;
                                        SetValue(cell, text2);
                                    }
                                    else
                                    {
                                        text.Text = _value;
                                        if (GetValue(value, _value, out var o))
                                        {
                                            if (it.RECORD is DataRow datarow) datarow[i_col] = o;
                                            else SetValue(cell, o);
                                        }
                                    }
                                }
                            });
                            CellBeginEditInputStyle?.Invoke(this, new TableBeginEditInputStyleEventArgs(value, it.RECORD, i_row, i_col, ref edit_input));
                            if (template.Value.PARENT != null)
                            {
                                if (template.Value.PARENT.column.Align == ColumnAlign.Center) edit_input.TextAlign = HorizontalAlignment.Center;
                                else if (template.Value.PARENT.column.Align == ColumnAlign.Right) edit_input.TextAlign = HorizontalAlignment.Right;
                            }
                            Controls.Add(edit_input);
                            edit_input.Focus();
                        }));
                        return;
                    }
                }
            }
        }
 
        bool GetValue(object? value, string _value, out object read)
        {
            if (value is int)
            {
                if (int.TryParse(_value, out var v))
                {
                    read = v;
                    return true;
                }
            }
            else if (value is double)
            {
                if (double.TryParse(_value, out var v))
                {
                    read = v;
                    return true;
                }
            }
            else if (value is decimal)
            {
                if (decimal.TryParse(_value, out var v))
                {
                    read = v;
                    return true;
                }
            }
            else if (value is float)
            {
                if (float.TryParse(_value, out var v))
                {
                    read = v;
                    return true;
                }
            }
            else
            {
                read = _value;
                return true;
            }
            read = _value;
            return false;
        }
 
        Input ShowInput(TCell cell, int sx, int sy, int height, bool multiline, object? value, Action<string> call)
        {
            Input input;
            if (value is CellText text2)
            {
                input = new Input
                {
                    Multiline = multiline,
                    Location = new Point(cell.RECT.X - sx, cell.RECT.Y - sy + (cell.RECT.Height - height) / 2),
                    Size = new Size(cell.RECT.Width, height),
                    Text = text2.Text ?? ""
                };
            }
            else
            {
                input = new Input
                {
                    Multiline = multiline,
                    Location = new Point(cell.RECT.X - sx, cell.RECT.Y - sy + (cell.RECT.Height - height) / 2),
                    Size = new Size(cell.RECT.Width, height),
                    Text = value?.ToString() ?? ""
                };
            }
            string old_text = input.Text;
            bool isone = true;
            input.KeyPress += (a, b) =>
            {
                if (a is Input input && isone)
                {
                    if (b.KeyChar == 13)
                    {
                        isone = false;
                        b.Handled = true;
                        ScrollBar.OnInvalidate = null;
                        if (old_text != input.Text) call(input.Text);
                        inEditMode = false;
                        input.Dispose();
                    }
                }
            };
            input.LostFocus += (a, b) =>
            {
                if (a is Input input && isone)
                {
                    isone = false;
                    ScrollBar.OnInvalidate = null;
                    if (old_text != input.Text) call(input.Text);
                    inEditMode = false;
                    input.Dispose();
                }
            };
            return input;
        }
 
        #endregion
    }
}