yangyin
2025-02-24 b3b65a002fe68b1383314098790f5a153b87765f
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Docking.Crown;
using DPumpHydr.WinFrmUI.RLT.Enum.Crown;
using DPumpHydr.WinFrmUI.RLT.Forms;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.CrownHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Native
{
    #region DockContentDragFilterNative
 
    public class DockContentDragFilter : IMessageFilter
    {
        #region Field Region
 
        private readonly CrownDockPanel _dockPanel;
 
        private CrownDockContent _dragContent;
 
        private readonly CrownTranslucentForm _highlightForm;
 
        private bool _isDragging = false;
        private CrownDockRegion _targetRegion;
        private CrownDockGroup _targetGroup;
        private DockInsertType _insertType = DockInsertType.None;
 
        private Dictionary<CrownDockRegion, DockDropArea> _regionDropAreas = new();
        private Dictionary<CrownDockGroup, DockDropCollection> _groupDropAreas = new();
 
        #endregion
 
        #region Constructor Region
 
        public DockContentDragFilter(CrownDockPanel dockPanel)
        {
            _dockPanel = dockPanel;
 
            _highlightForm = new CrownTranslucentForm(ThemeProvider.Theme.Colors.BlueSelection);
        }
 
        #endregion
 
        #region IMessageFilter Region
 
        public bool PreFilterMessage(ref Message m)
        {
            // Exit out early if we're not dragging any content
            if (!_isDragging)
            {
                return false;
            }
 
            // We only care about mouse events
            if (m.Msg is not (((int)WM.MOUSEMOVE) or ((int)WM.LBUTTONDOWN) or ((int)WM.LBUTTONUP) or ((int)WM.LBUTTONDBLCLK) or ((int)WM.RBUTTONDOWN) or ((int)WM.RBUTTONUP) or ((int)WM.RBUTTONDBLCLK)))
            {
                return false;
            }
 
            // Move content
            if (m.Msg == (int)WM.MOUSEMOVE)
            {
                HandleDrag();
                return false;
            }
 
            // Drop content
            if (m.Msg == (int)WM.LBUTTONUP)
            {
                if (_targetRegion != null)
                {
                    _dockPanel.RemoveContent(_dragContent);
                    _dragContent.DockArea = _targetRegion.DockArea;
                    _dockPanel.AddContent(_dragContent);
                }
                else if (_targetGroup != null)
                {
                    _dockPanel.RemoveContent(_dragContent);
 
                    switch (_insertType)
                    {
                        case DockInsertType.None:
                            _dockPanel.AddContent(_dragContent, _targetGroup);
                            break;
                        case DockInsertType.Before:
                        case DockInsertType.After:
                            _dockPanel.InsertContent(_dragContent, _targetGroup, _insertType);
                            break;
                    }
                }
 
                StopDrag();
                return false;
            }
 
            return true;
        }
 
        #endregion
 
        #region Method Region
 
        public void StartDrag(CrownDockContent content)
        {
            _regionDropAreas = new Dictionary<CrownDockRegion, DockDropArea>();
            _groupDropAreas = new Dictionary<CrownDockGroup, DockDropCollection>();
 
            // Add all regions and groups to the drop collections
            foreach (CrownDockRegion region in _dockPanel.Regions.Values)
            {
                if (region.DockArea == DockArea.Document)
                {
                    continue;
                }
 
                // If the region is visible then build drop areas for the groups.
                if (region.Visible)
                {
                    foreach (CrownDockGroup group in region.Groups)
                    {
                        DockDropCollection collection = new(_dockPanel, group);
                        _groupDropAreas.Add(group, collection);
                    }
                }
                // If the region is NOT visible then build the drop area for the region itself.
                else
                {
                    DockDropArea area = new(_dockPanel, region);
                    _regionDropAreas.Add(region, area);
                }
            }
 
            _dragContent = content;
            _isDragging = true;
        }
 
        private void StopDrag()
        {
            Cursor.Current = Cursors.Default;
 
            _highlightForm.Hide();
            _dragContent = null;
            _isDragging = false;
        }
 
        private void UpdateHighlightForm(Rectangle rect)
        {
            Cursor.Current = Cursors.SizeAll;
 
            _highlightForm.SuspendLayout();
 
            _highlightForm.Size = new(rect.Width, rect.Height);
            _highlightForm.Location = new(rect.X, rect.Y);
 
            _highlightForm.ResumeLayout();
 
            if (!_highlightForm.Visible)
            {
                _highlightForm.Show();
                _highlightForm.BringToFront();
            }
        }
 
        private void HandleDrag()
        {
            Point location = Cursor.Position;
 
            _insertType = DockInsertType.None;
 
            _targetRegion = null;
            _targetGroup = null;
 
            // Check all region drop areas
            foreach (DockDropArea area in _regionDropAreas.Values)
            {
                if (area.DropArea.Contains(location))
                {
                    _insertType = DockInsertType.None;
                    _targetRegion = area.DockRegion;
                    UpdateHighlightForm(area.HighlightArea);
                    return;
                }
            }
 
            // Check all group drop areas
            foreach (DockDropCollection collection in _groupDropAreas.Values)
            {
                bool sameRegion = false;
                bool sameGroup = false;
                bool groupHasOtherContent = false;
 
                if (collection.DropArea.DockGroup == _dragContent.DockGroup)
                {
                    sameGroup = true;
                }
 
                if (collection.DropArea.DockGroup.DockRegion == _dragContent.DockRegion)
                {
                    sameRegion = true;
                }
 
                if (_dragContent.DockGroup.ContentCount > 1)
                {
                    groupHasOtherContent = true;
                }
 
                // If we're hovering over the group itself, only allow inserting before/after if multiple content is tabbed.
                if (!sameGroup || groupHasOtherContent)
                {
                    bool skipBefore = false;
                    bool skipAfter = false;
 
                    // Inserting before/after other content might cause the content to be dropped on to its own location.
                    // Check if the group above/below the hovered group contains our drag content.
                    if (sameRegion && !groupHasOtherContent)
                    {
                        if (collection.InsertBeforeArea.DockGroup.Order == _dragContent.DockGroup.Order + 1)
                        {
                            skipBefore = true;
                        }
 
                        if (collection.InsertAfterArea.DockGroup.Order == _dragContent.DockGroup.Order - 1)
                        {
                            skipAfter = true;
                        }
                    }
 
                    if (!skipBefore)
                    {
                        if (collection.InsertBeforeArea.DropArea.Contains(location))
                        {
                            _insertType = DockInsertType.Before;
                            _targetGroup = collection.InsertBeforeArea.DockGroup;
                            UpdateHighlightForm(collection.InsertBeforeArea.HighlightArea);
                            return;
                        }
                    }
 
                    if (!skipAfter)
                    {
                        if (collection.InsertAfterArea.DropArea.Contains(location))
                        {
                            _insertType = DockInsertType.After;
                            _targetGroup = collection.InsertAfterArea.DockGroup;
                            UpdateHighlightForm(collection.InsertAfterArea.HighlightArea);
                            return;
                        }
                    }
                }
 
                // Don't allow content to be dragged on to itself
                if (!sameGroup)
                {
                    if (collection.DropArea.DropArea.Contains(location))
                    {
                        _insertType = DockInsertType.None;
                        _targetGroup = collection.DropArea.DockGroup;
                        UpdateHighlightForm(collection.DropArea.HighlightArea);
                        return;
                    }
                }
            }
 
            // Not hovering over anything - hide the highlight
            if (_highlightForm.Visible)
            {
                _highlightForm.Hide();
            }
 
            // Show we can't drag here
            Cursor.Current = Cursors.No;
        }
 
        #endregion
    }
 
    #endregion
}