File size: 13,156 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO
Imports System.ComponentModel
Imports System.Reflection

Namespace PropertyGridEx

    <Designer(GetType(System.Windows.Forms.Design.ControlDesigner)), _
     ToolboxBitmap(GetType(PropertyGridEx))> _
    Public Class PropertyGridEx
        Inherits PropertyGrid

#Region "Protected variables and objects"

        ' CustomPropertyCollection assigned to MyBase.SelectedObject
        Protected oCustomPropertyCollection As CustomPropertyCollection
        Protected bShowCustomProperties As Boolean

        ' CustomPropertyCollectionSet assigned to MyBase.SelectedObjects
        Protected oCustomPropertyCollectionSet As CustomPropertyCollectionSet
        Protected bShowCustomPropertiesSet As Boolean

        ' Internal PropertyGrid Controls
        Protected oPropertyGridView As Object
        Protected oHotCommands As Object
        Protected oDocComment As Object
        Protected oToolStrip As ToolStrip

        ' Internal PropertyGrid Fields
        Protected oDocCommentTitle As Label
        Protected oDocCommentDescription As Label
        Protected oPropertyGridEntries As FieldInfo

        ' Properties variables
        Protected bAutoSizeProperties As Boolean
        Protected bDrawFlatToolbar As Boolean

#End Region

#Region "Public Functions"
        Public Sub New()
            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            Me.DoubleBuffered = True

            ' Initialize collections
            oCustomPropertyCollection = New CustomPropertyCollection
            oCustomPropertyCollectionSet = New CustomPropertyCollectionSet

            If Not Type.GetType("Mono.Runtime") Is Nothing Then
                ' Attach internal controls
                oPropertyGridView = MyBase.GetType.BaseType.InvokeMember("property_grid_view", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)
                oHotCommands = Nothing 'MyBase.GetType.BaseType.InvokeMember("hotcommands", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)
                oToolStrip = MyBase.GetType.BaseType.InvokeMember("toolbar", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)
                oDocComment = MyBase.GetType.BaseType.InvokeMember("help_panel", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)

                ' Attach DocComment internal fields
                If oDocComment IsNot Nothing Then
                    oDocCommentTitle = MyBase.GetType.BaseType.InvokeMember("help_title_label", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)
                    oDocCommentDescription = MyBase.GetType.BaseType.InvokeMember("help_description_label", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)
                    'oDocCommentTitle = oDocComment.GetType.InvokeMember("help_title_label", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, oDocComment, Nothing)
                    'oDocCommentDescription = oDocComment.GetType.InvokeMember("help_description_label", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, oDocComment, Nothing)
                End If

                ' Attach PropertyGridView internal fields
                If oPropertyGridView IsNot Nothing Then
                    oPropertyGridEntries = oPropertyGridView.GetType.GetField("grid_items", BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
                End If
            Else
                ' Attach internal controls
                oPropertyGridView = MyBase.GetType.BaseType.InvokeMember("gridView", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)
                oHotCommands = MyBase.GetType.BaseType.InvokeMember("hotcommands", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)
                oToolStrip = MyBase.GetType.BaseType.InvokeMember("toolStrip", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)
                oDocComment = MyBase.GetType.BaseType.InvokeMember("doccomment", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, Me, Nothing)

                ' Attach DocComment internal fields
                If oDocComment IsNot Nothing Then
                    oDocCommentTitle = oDocComment.GetType.InvokeMember("m_labelTitle", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, oDocComment, Nothing)
                    oDocCommentDescription = oDocComment.GetType.InvokeMember("m_labelDesc", BindingFlags.NonPublic Or BindingFlags.GetField Or BindingFlags.Instance, Nothing, oDocComment, Nothing)
                End If

                ' Attach PropertyGridView internal fields
                If oPropertyGridView IsNot Nothing Then
                    oPropertyGridEntries = oPropertyGridView.GetType.GetField("allGridEntries", BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
                End If
            End If


            ' Apply Toolstrip style
            If oToolStrip IsNot Nothing Then
                ApplyToolStripRenderMode(bDrawFlatToolbar)
            End If

        End Sub

        Public Sub MoveSplitterTo(ByVal x As Integer)
            oPropertyGridView.GetType().InvokeMember("MoveSplitterTo", BindingFlags.InvokeMethod Or BindingFlags.NonPublic Or BindingFlags.Instance, Nothing, oPropertyGridView, New Object() {x})
        End Sub

        Public Overrides Sub Refresh()
            If bShowCustomPropertiesSet Then
                MyBase.SelectedObjects = oCustomPropertyCollectionSet.ToArray
            End If
            MyBase.Refresh()
            If bAutoSizeProperties Then AutoSizeSplitter()
        End Sub

        Public Sub SetComment(ByVal title As String, ByVal description As String)
            oDocComment.SetComment(title, description)
        End Sub

#End Region

#Region "Protected Functions"
        Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
            MyBase.OnResize(e)
            If bAutoSizeProperties Then AutoSizeSplitter()
        End Sub

        Protected Sub AutoSizeSplitter(Optional ByVal RightMargin As Integer = 32)

            Dim oItemCollection As GridItemCollection = oPropertyGridEntries.GetValue(oPropertyGridView)
            If oItemCollection Is Nothing Then Exit Sub
            Dim oGraphics As Drawing.Graphics = Drawing.Graphics.FromHwnd(Me.Handle)
            Dim CurWidth As Integer = 0
            Dim MaxWidth As Integer = 0

            For Each oItem As GridItem In oItemCollection
                If oItem.GridItemType = GridItemType.Property Then
                    CurWidth = oGraphics.MeasureString(oItem.Label, Me.Font).Width + RightMargin
                    If CurWidth > MaxWidth Then
                        MaxWidth = CurWidth
                    End If
                End If
            Next

            MoveSplitterTo(MaxWidth)
        End Sub
        Protected Sub ApplyToolStripRenderMode(ByVal value As Boolean)
            If value Then
                oToolStrip.Renderer = New ToolStripSystemRenderer
            Else
                Dim Renderer As New ToolStripProfessionalRenderer(New CustomColorScheme())
                Renderer.RoundedEdges = False
                oToolStrip.Renderer = Renderer
            End If
        End Sub
#End Region

#Region "Properties"

        <Category("Behavior"), _
         DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
         DescriptionAttribute("Set the collection of the CustomProperty. Set ShowCustomProperties to True to enable it."), _
         RefreshProperties(RefreshProperties.Repaint)> _
        Public ReadOnly Property Item() As CustomPropertyCollection
            Get
                Return oCustomPropertyCollection
            End Get
        End Property

        <Category("Behavior"), _
         DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
         DescriptionAttribute("Set the CustomPropertyCollectionSet. Set ShowCustomPropertiesSet to True to enable it."), _
         RefreshProperties(RefreshProperties.Repaint)> _
        Public ReadOnly Property ItemSet() As CustomPropertyCollectionSet
            Get
                Return oCustomPropertyCollectionSet
            End Get
        End Property

        <Category("Behavior"), _
         DefaultValue(False), _
         DescriptionAttribute("Move automatically the splitter to better fit all the properties shown.")> _
        Public Property AutoSizeProperties() As Boolean
            Get
                Return bAutoSizeProperties
            End Get
            Set(ByVal Value As Boolean)
                bAutoSizeProperties = Value
                If Value Then AutoSizeSplitter()
            End Set
        End Property

        <Category("Behavior"), _
         DefaultValue(False), _
         DescriptionAttribute("Use the custom properties collection as SelectedObject."), _
         RefreshProperties(RefreshProperties.All)> _
        Public Property ShowCustomProperties() As Boolean
            Get
                Return bShowCustomProperties
            End Get
            Set(ByVal value As Boolean)
                If value = True Then
                    bShowCustomPropertiesSet = False
                    MyBase.SelectedObject = oCustomPropertyCollection
                End If
                bShowCustomProperties = value
            End Set
        End Property

        <Category("Behavior"), _
         DefaultValue(False), _
         DescriptionAttribute("Use the custom properties collections as SelectedObjects."), _
         RefreshProperties(RefreshProperties.All)> _
        Public Property ShowCustomPropertiesSet() As Boolean
            Get
                Return bShowCustomPropertiesSet
            End Get
            Set(ByVal value As Boolean)
                If value = True Then
                    bShowCustomProperties = False
                    MyBase.SelectedObjects = oCustomPropertyCollectionSet.ToArray
                End If
                bShowCustomPropertiesSet = value
            End Set
        End Property

        <Category("Appearance"), _
         DefaultValue(False), _
         DescriptionAttribute("Draw a flat toolbar")> _
        Public Overloads Property DrawFlatToolbar() As Boolean
            Get
                Return bDrawFlatToolbar
            End Get
            Set(ByVal value As Boolean)
                bDrawFlatToolbar = value
                ApplyToolStripRenderMode(bDrawFlatToolbar)
            End Set
        End Property

        <Category("Appearance"), _
         DisplayName("Toolstrip"), _
         DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
         DescriptionAttribute("Toolbar object"), _
         Browsable(True)> _
        Public ReadOnly Property ToolStrip() As ToolStrip
            Get
                Return oToolStrip
            End Get
        End Property

        <Category("Appearance"), _
         DisplayName("Help"), _
         DescriptionAttribute("DocComment object. Represent the comments area of the PropertyGrid."), _
         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), _
         Browsable(False)> _
        Public ReadOnly Property DocComment() As Control
            Get
                Return oDocComment
            End Get
        End Property

        <Category("Appearance"), _
         DisplayName("HelpTitle"), _
         DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
         DescriptionAttribute("Help Title Label."), _
         Browsable(True)> _
        Public ReadOnly Property DocCommentTitle() As Label
            Get
                Return oDocCommentTitle
            End Get
        End Property

        <Category("Appearance"), _
         DisplayName("HelpDescription"), _
         DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
         DescriptionAttribute("Help Description Label."), _
         Browsable(True)> _
        Public ReadOnly Property DocCommentDescription() As Label
            Get
                Return oDocCommentDescription
            End Get
        End Property

        <Category("Appearance"), _
         DisplayName("HelpImageBackground"), _
         DescriptionAttribute("Help Image Background.")> _
        Public Property DocCommentImage() As Image
            Get
                Return oDocComment.BackgroundImage
            End Get
            Set(ByVal value As Image)
                oDocComment.BackgroundImage = value
            End Set
        End Property

#End Region

    End Class

End Namespace