This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
/
NavBarGroupStateHelper.vb
174 lines (148 loc) · 5.96 KB
/
NavBarGroupStateHelper.vb
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
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraEditors.ViewInfo
Imports DevExpress.XtraEditors.Drawing
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraNavBar
Imports DevExpress.XtraNavBar.ViewInfo
Imports System.Reflection
Namespace WindowsApplication1
Public Class NavBarGroupStateHelper
Inherits Component
Private isLocked As Boolean
Private _CheckIndent As Integer
Public Property CheckIndent() As Integer
Get
Return _CheckIndent
End Get
Set(ByVal value As Integer)
_CheckIndent = value
End Set
End Property
Private _GroupEdit As RepositoryItemCheckEdit
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public Property GroupEdit() As RepositoryItemCheckEdit
Get
Return _GroupEdit
End Get
Set(ByVal value As RepositoryItemCheckEdit)
_GroupEdit = value
End Set
End Property
Private _NavBarControl As NavBarControl
Public Property NavBarControl() As NavBarControl
Get
Return _NavBarControl
End Get
Set(ByVal value As NavBarControl)
UnsubscribeEvents(value)
_NavBarControl = value
SubscribeEvents(value)
End Set
End Property
Public Sub New()
GroupEdit = New RepositoryItemCheckEdit()
End Sub
Private Function IsCustomDrawNeeded() As Boolean
Return GroupEdit IsNot Nothing AndAlso NavBarControl IsNot Nothing AndAlso Not isLocked
End Function
Private Sub SubscribeEvents(ByVal navBarControl As NavBarControl)
If navBarControl IsNot Nothing Then
AddHandler Me.NavBarControl.CustomDrawGroupCaption, AddressOf NavBarControl_CustomDrawGroupCaption
AddHandler Me.NavBarControl.MouseClick, AddressOf NavBarControl_MouseClick
End If
End Sub
Private Sub UnsubscribeEvents(ByVal navBarControl As NavBarControl)
If navBarControl Is Nothing Then
RemoveHandler Me.NavBarControl.CustomDrawGroupCaption, AddressOf NavBarControl_CustomDrawGroupCaption
RemoveHandler Me.NavBarControl.MouseClick, AddressOf NavBarControl_MouseClick
End If
End Sub
Private Function GetCheckBoxWidth() As Integer
Return CheckIndent * 2 + 10
End Function
Private Function GetCaptionBounds(ByVal originalCaptionBounds As Rectangle) As Rectangle
Return New Rectangle(originalCaptionBounds.Location, New Size(originalCaptionBounds.Width - GetCheckBoxWidth(), originalCaptionBounds.Height))
End Function
Private Function GetCheckBoxBounds(ByVal fixedCaptionBounds As Rectangle) As Rectangle
Return New Rectangle(fixedCaptionBounds.Right, fixedCaptionBounds.Top, GetCheckBoxWidth(), fixedCaptionBounds.Height)
End Function
Private Sub NavBarControl_CustomDrawGroupCaption(ByVal sender As Object, ByVal e As DevExpress.XtraNavBar.ViewInfo.CustomDrawNavBarElementEventArgs)
If Not IsCustomDrawNeeded() Then
Return
End If
Try
isLocked = True
Dim painter As BaseNavGroupPainter = NavBarControl.View.CreateGroupPainter(NavBarControl)
Dim info As NavGroupInfoArgs = TryCast(e.ObjectInfo, NavGroupInfoArgs)
Dim originalCaptionBounds As New Rectangle(info.CaptionClientBounds.X,info.CaptionClientBounds.Y,info.CaptionClientBounds.Width-info.ButtonBounds.Width,info.CaptionClientBounds.Height)
Dim fixedCaptionBounds As Rectangle = GetCaptionBounds(originalCaptionBounds)
Dim checkBoxBounds As Rectangle = GetCheckBoxBounds(fixedCaptionBounds)
info.CaptionBounds = fixedCaptionBounds
painter.DrawObject(info)
info.CaptionBounds = originalCaptionBounds
DrawCheckBox(e.Graphics, checkBoxBounds, IsGroupEnabled(info.Group))
e.Handled = True
Finally
isLocked = False
End Try
End Sub
Protected Sub DrawCheckBox(ByVal g As Graphics, ByVal r As Rectangle, ByVal Checked As Boolean)
Dim painter As BaseEditPainter = GroupEdit.CreatePainter()
Dim info As BaseEditViewInfo = GroupEdit.CreateViewInfo()
info.EditValue = Checked
info.Bounds = r
info.CalcViewInfo(g)
Dim args As New ControlGraphicsInfoArgs(info, New DevExpress.Utils.Drawing.GraphicsCache(g), r)
painter.Draw(args)
args.Cache.Dispose()
End Sub
Private hotRectangle As Rectangle
Private hotGroup As NavBarGroup
Private Shared Function GetNavBarView(ByVal NavBar As NavBarControl) As NavBarViewInfo
Dim pi As PropertyInfo = GetType(NavBarControl).GetProperty("ViewInfo", BindingFlags.Instance Or BindingFlags.NonPublic)
Return TryCast(pi.GetValue(NavBar, Nothing), NavBarViewInfo)
End Function
Private Function IsCheckBox(ByVal p As Point) As Boolean
Dim hi As NavBarHitInfo = NavBarControl.CalcHitInfo(p)
If hi.Group Is Nothing Then
Return False
End If
Dim vi As NavBarViewInfo = GetNavBarView(NavBarControl)
vi.Calc(NavBarControl.ClientRectangle)
Dim groupInfo As NavGroupInfoArgs = vi.GetGroupInfo(hi.Group)
Dim originalCaptionBounds As New Rectangle(groupInfo.CaptionClientBounds.X, groupInfo.CaptionClientBounds.Y, groupInfo.CaptionClientBounds.Width - groupInfo.ButtonBounds.Width, groupInfo.CaptionClientBounds.Height)
Dim checkBounds As Rectangle = GetCheckBoxBounds(GetCaptionBounds(originalCaptionBounds))
hotGroup = hi.Group
hotRectangle = checkBounds
Return checkBounds.Contains(p)
End Function
Private Sub NavBarControl_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
If IsCheckBox(e.Location) Then
ToggleGroupState(hotGroup)
End If
End Sub
Private Shared Function IsGroupEnabled(ByVal group As NavBarGroup) As Boolean
Return group.Tag Is Nothing
End Function
Private Sub ToggleGroupState(ByVal group As NavBarGroup)
SetGroupState(group, (Not IsGroupEnabled(group)))
End Sub
Private Sub SetGroupState(ByVal group As NavBarGroup, ByVal enabled As Boolean)
For Each link As NavBarItemLink In group.ItemLinks
link.Item.Enabled = enabled
Next link
If enabled Then
group.Tag = Nothing
Else
group.Tag = 0
End If
NavBarControl.Invalidate(hotRectangle)
End Sub
End Class
End Namespace