Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Controls.Primitives
Imports System.Windows.Media
Imports O2S.Components.PDFView4NET
Imports O2S.Components.PDFView4NET.Forms

Partial Public Class MainPage
    Inherits UserControl
    ''' <summary>
    ''' Initializes a new instance of the <see cref="MainPage"/> class.
    ''' </summary>
    Public Sub New()
        InitializeComponent()
        DataContext = Me

        AddHandler PdfControl.BeforeFieldAdd, New System.EventHandler(Of BeforeFieldAddEventArgs)(AddressOf PdfControl_BeforeFieldAdd)
        AddHandler PdfControl.FieldContextMenu, New System.EventHandler(Of FieldContextMenuEventArgs)(AddressOf PdfControl_FieldContextMenu)
        AddHandler PdfControl.PageClick, New System.EventHandler(Of PageClickEventArgs)(AddressOf PdfControl_PageClick)
    End Sub

    ''' <summary>
    ''' Handles the PageClick event of the PdfControl control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="O2S.Components.PDFView4NET.PageClickEventArgs"/> instance containing the event data.</param>
    Private Sub PdfControl_PageClick(ByVal sender As Object, ByVal e As PageClickEventArgs)
        RemovePopupMenuFromUI(False)
    End Sub

    Private _sender As Object
    Private _selectedField As PDFField
    Private _selectedWidget As PDFFieldWidget
    Private _contextMenuPopup As New Popup()

    ''' <summary>
    ''' Handles the FieldContextMenu event of the PdfControl control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="O2S.Components.PDFView4NET.FieldContextMenuEventArgs"/> instance containing the event data.</param>
    Private Sub PdfControl_FieldContextMenu(ByVal sender As Object, ByVal e As FieldContextMenuEventArgs)
        If PdfControl.WorkMode = UserInteractiveWorkMode.EditAnnotations Then
            _selectedField = e.Field
            _selectedWidget = e.FieldWidget

            Dim content As New Grid() With { _
             .Background = New SolidColorBrush(Color.FromArgb(225, 225, 225, 225)) _
            }
            content.RowDefinitions.Add(New RowDefinition())
            content.RowDefinitions.Add(New RowDefinition())

            Dim border01 As New Border() With { _
             .BorderBrush = New SolidColorBrush(Colors.Black), _
             .BorderThickness = New Thickness(1) _
            }
            AddHandler border01.MouseEnter, New System.Windows.Input.MouseEventHandler(AddressOf HoverEffect_MouseEnter)
            AddHandler border01.MouseLeave, New System.Windows.Input.MouseEventHandler(AddressOf HoverEffect_MouseLeave)
            Dim textBlock01 As New TextBlock() With { _
             .Text = "  Properties", _
             .Name = "Properties" _
            }
            AddHandler textBlock01.MouseLeftButtonDown, New System.Windows.Input.MouseButtonEventHandler(AddressOf content_MouseLeftButtonDown)
            border01.Child = textBlock01
            border01.SetValue(Grid.RowProperty, 0)

            Dim border02 As New Border() With { _
             .BorderBrush = New SolidColorBrush(Colors.Black), _
             .BorderThickness = New Thickness(1) _
            }
            AddHandler border02.MouseEnter, New System.Windows.Input.MouseEventHandler(AddressOf HoverEffect_MouseEnter)
            AddHandler border02.MouseLeave, New System.Windows.Input.MouseEventHandler(AddressOf HoverEffect_MouseLeave)
            Dim textBlock02 As New TextBlock() With { _
             .Text = "  Delete field  ", _
             .Name = "Delete" _
            }
            AddHandler textBlock02.MouseLeftButtonDown, New System.Windows.Input.MouseButtonEventHandler(AddressOf content_MouseLeftButtonDown)
            border02.Child = textBlock02
            border01.SetValue(Grid.RowProperty, 1)

            content.Children.Add(border01)
            content.Children.Add(border02)

            _contextMenuPopup.Child = content

            ' Considered mouse pointer size: 20x30. Added the mouse size to that the popup appears below it.
            _contextMenuPopup.HorizontalOffset = e.MousePosition.X + 20
            _contextMenuPopup.VerticalOffset = e.MousePosition.Y + 30
            _contextMenuPopup.IsOpen = True

            _sender = sender
        End If
    End Sub

    ''' <summary>
    ''' Handles the MouseLeave event of the control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
    Private Sub HoverEffect_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
        DirectCast(sender, Border).Background = New SolidColorBrush(Color.FromArgb(225, 225, 225, 225))
    End Sub

    ''' <summary>
    ''' Handles the MouseEnter event of the control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
    Private Sub HoverEffect_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
        DirectCast(sender, Border).Background = New SolidColorBrush(Color.FromArgb(225, 225, 100, 100))
    End Sub

    ''' <summary>
    ''' Handles the MouseLeftButtonDown event of the content control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
    Private Sub content_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        If TypeOf sender Is TextBlock AndAlso _selectedField IsNot Nothing AndAlso _selectedWidget IsNot Nothing Then
            If DirectCast(sender, TextBlock).Name = "Delete" Then
                ' Delete current field
                RemovePopupMenuFromUI(True)
            Else
                ' Show properties
                RemovePopupMenuFromUI(False)
                ShowPropertiesWindowForSelectedField()
            End If

            e.Handled = True
        End If
    End Sub

    ''' <summary>
    ''' Removes the popup menu from UI.
    ''' </summary>
    ''' <param name="deleteAnnotation">if set to <c>true</c> [delete annotation].</param>
    Private Sub RemovePopupMenuFromUI(ByVal deleteAnnotation As Boolean)
        If _contextMenuPopup.Child IsNot Nothing AndAlso TypeOf _contextMenuPopup.Child Is Grid AndAlso _selectedField IsNot Nothing AndAlso _selectedWidget IsNot Nothing Then
            RemoveHandler DirectCast(_contextMenuPopup.Child, Grid).MouseLeftButtonDown, New System.Windows.Input.MouseButtonEventHandler(AddressOf content_MouseLeftButtonDown)
            RemoveHandler DirectCast(_contextMenuPopup.Child, Grid).MouseEnter, New System.Windows.Input.MouseEventHandler(AddressOf HoverEffect_MouseEnter)
            RemoveHandler DirectCast(_contextMenuPopup.Child, Grid).MouseLeave, New System.Windows.Input.MouseEventHandler(AddressOf HoverEffect_MouseLeave)

            _contextMenuPopup.Child = Nothing
            _contextMenuPopup.IsOpen = False

            If deleteAnnotation Then
                DeleteCurrentField()
            End If
        End If
    End Sub

    ''' <summary>
    ''' Deletes the current field.
    ''' </summary>
    Private Sub DeleteCurrentField()
        If _selectedField IsNot Nothing AndAlso _selectedWidget IsNot Nothing Then
            _selectedWidget.Page.Fields.Remove(_selectedField)
            _selectedField = Nothing
        End If
    End Sub

    ''' <summary>
    ''' Shows the properties window for selected field.
    ''' </summary>
    Private Sub ShowPropertiesWindowForSelectedField()
        Select Case _selectedField.Type
            Case PDFFieldType.CheckBox
                Dim cbpf As New CheckBoxPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                cbpf.CheckBoxField = TryCast(_selectedField, PDFCheckBoxField)
                cbpf.CheckBoxWidget = TryCast(TryCast(_selectedField, PDFCheckBoxField).Widgets(0), PDFCheckBoxWidget)
                cbpf.IsCancelable = False
                popupTool.Child = cbpf
                AddHandler cbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.DropDownList
                Dim ddlpf As New DropDownListPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                ddlpf.DropDownListField = TryCast(_selectedField, PDFDropDownListField)
                ddlpf.DropDownListWidget = TryCast(TryCast(_selectedField, PDFDropDownListField).Widgets(0), PDFDropDownListWidget)
                ddlpf.IsCancelable = False
                popupTool.Child = ddlpf
                AddHandler ddlpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.ListBox
                Dim lbpf As New ListBoxPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                lbpf.ListBoxField = TryCast(_selectedField, PDFListBoxField)
                lbpf.ListBoxWidget = TryCast(TryCast(_selectedField, PDFListBoxField).Widgets(0), PDFListBoxWidget)
                lbpf.IsCancelable = False
                popupTool.Child = lbpf
                AddHandler lbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.PushButton
                Dim pbpf As New PushButtonPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                pbpf.PushButtonField = TryCast(_selectedField, PDFPushButtonField)
                pbpf.PushButtonWidget = TryCast(TryCast(_selectedField, PDFPushButtonField).Widgets(0), PDFPushButtonWidget)
                pbpf.IsCancelable = False
                popupTool.Child = pbpf
                AddHandler pbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.RadioButton
                Dim rbpf As New RadioButtonPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                rbpf.RadioButtonField = TryCast(_selectedField, PDFRadioButtonListField)
                rbpf.RadioButtonWidget = TryCast(TryCast(_selectedField, PDFRadioButtonListField).Widgets(0), PDFRadioButtonItem)
                rbpf.IsCancelable = False
                popupTool.Child = rbpf
                AddHandler rbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.Signature
                Dim spf As New SignaturePropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                spf.SignatureField = TryCast(_selectedField, PDFSignatureField)
                spf.SignatureWidget = TryCast(TryCast(_selectedField, PDFSignatureField).Widgets(0), PDFSignatureWidget)
                spf.IsCancelable = False
                popupTool.Child = spf
                AddHandler spf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.TextBox
                Dim tbpf As New TextBoxPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                tbpf.TextBoxField = TryCast(_selectedField, PDFTextBoxField)
                tbpf.TextBoxWidget = TryCast(TryCast(_selectedField, PDFTextBoxField).Widgets(0), PDFTextBoxWidget)
                tbpf.IsCancelable = False
                popupTool.Child = tbpf
                AddHandler tbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
        End Select
    End Sub

    Private popupTool As New Popup()

    ''' <summary>
    ''' Handles the BeforeFieldAdd event of the PdfControl control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="O2S.Components.PDFView4NET.BeforeFieldAddEventArgs"/> instance containing the event data.</param>
    Private Sub PdfControl_BeforeFieldAdd(ByVal sender As Object, ByVal e As BeforeFieldAddEventArgs)
        Select Case e.Field.Type
            Case PDFFieldType.CheckBox
                Dim cbpf As New CheckBoxPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                cbpf.CheckBoxField = TryCast(e.Field, PDFCheckBoxField)
                cbpf.CheckBoxWidget = TryCast(TryCast(e.Field, PDFCheckBoxField).Widgets(0), PDFCheckBoxWidget)
                _selectedField = cbpf.CheckBoxField
                _selectedWidget = cbpf.CheckBoxWidget
                cbpf.IsCancelable = True
                popupTool.Child = cbpf
                AddHandler cbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.DropDownList
                Dim ddlpf As New DropDownListPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                ddlpf.DropDownListField = TryCast(e.Field, PDFDropDownListField)
                ddlpf.DropDownListWidget = TryCast(TryCast(e.Field, PDFDropDownListField).Widgets(0), PDFDropDownListWidget)
                _selectedField = ddlpf.DropDownListField
                _selectedWidget = ddlpf.DropDownListWidget
                ddlpf.IsCancelable = True
                popupTool.Child = ddlpf
                AddHandler ddlpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.ListBox
                Dim lbpf As New ListBoxPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                lbpf.ListBoxField = TryCast(e.Field, PDFListBoxField)
                lbpf.ListBoxWidget = TryCast(TryCast(e.Field, PDFListBoxField).Widgets(0), PDFListBoxWidget)
                _selectedField = lbpf.ListBoxField
                _selectedWidget = lbpf.ListBoxWidget
                lbpf.IsCancelable = True
                popupTool.Child = lbpf
                AddHandler lbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.PushButton
                Dim pbpf As New PushButtonPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                pbpf.PushButtonField = TryCast(e.Field, PDFPushButtonField)
                pbpf.PushButtonWidget = TryCast(TryCast(e.Field, PDFPushButtonField).Widgets(0), PDFPushButtonWidget)
                _selectedField = pbpf.PushButtonField
                _selectedWidget = pbpf.PushButtonWidget
                pbpf.IsCancelable = True
                popupTool.Child = pbpf
                AddHandler pbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.RadioButton
                Dim rbpf As New RadioButtonPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                rbpf.RadioButtonField = TryCast(e.Field, PDFRadioButtonListField)
                rbpf.RadioButtonWidget = TryCast(TryCast(e.Field, PDFRadioButtonListField).Widgets(0), PDFRadioButtonItem)
                _selectedField = rbpf.RadioButtonField
                _selectedWidget = rbpf.RadioButtonWidget
                rbpf.IsCancelable = True
                popupTool.Child = rbpf
                AddHandler rbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.Signature
                Dim spf As New SignaturePropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                spf.SignatureField = TryCast(e.Field, PDFSignatureField)
                spf.SignatureWidget = TryCast(TryCast(e.Field, PDFSignatureField).Widgets(0), PDFSignatureWidget)
                _selectedField = spf.SignatureField
                _selectedWidget = spf.SignatureWidget
                spf.IsCancelable = True
                popupTool.Child = spf
                AddHandler spf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
            Case PDFFieldType.TextBox
                Dim tbpf As New TextBoxPropertiesForm() With { _
                 .Width = Me.ActualWidth, _
                 .Height = Me.ActualHeight _
                }
                tbpf.TextBoxField = TryCast(e.Field, PDFTextBoxField)
                tbpf.TextBoxWidget = TryCast(TryCast(e.Field, PDFTextBoxField).Widgets(0), PDFTextBoxWidget)
                _selectedField = tbpf.TextBoxField
                _selectedWidget = tbpf.TextBoxWidget
                tbpf.IsCancelable = True
                popupTool.Child = tbpf
                AddHandler tbpf.Closed, AddressOf popupTool_Closed
                popupTool.IsOpen = True
                Exit Select
        End Select
    End Sub

    ''' <summary>
    ''' Handles the Closed event of the popupTool control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    Private Sub popupTool_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        If TypeOf sender Is CheckBoxPropertiesForm Then
            RemoveHandler DirectCast(sender, CheckBoxPropertiesForm).Closed, AddressOf popupTool_Closed

            If Not DirectCast(sender, CheckBoxPropertiesForm).AcceptChanges Then
                DeleteCurrentField()
            End If
        ElseIf TypeOf sender Is DropDownListPropertiesForm Then
            RemoveHandler DirectCast(sender, DropDownListPropertiesForm).Closed, AddressOf popupTool_Closed

            If Not DirectCast(sender, DropDownListPropertiesForm).AcceptChanges Then
                DeleteCurrentField()
            End If
        ElseIf TypeOf sender Is ListBoxPropertiesForm Then
            RemoveHandler DirectCast(sender, ListBoxPropertiesForm).Closed, AddressOf popupTool_Closed

            If Not DirectCast(sender, ListBoxPropertiesForm).AcceptChanges Then
                DeleteCurrentField()
            End If
        ElseIf TypeOf sender Is PushButtonPropertiesForm Then
            RemoveHandler TryCast(sender, PushButtonPropertiesForm).Closed, AddressOf popupTool_Closed

            If Not DirectCast(sender, PushButtonPropertiesForm).AcceptChanges Then
                DeleteCurrentField()
            End If
        ElseIf TypeOf sender Is RadioButtonPropertiesForm Then
            RemoveHandler DirectCast(sender, RadioButtonPropertiesForm).Closed, AddressOf popupTool_Closed

            If Not DirectCast(sender, RadioButtonPropertiesForm).AcceptChanges Then
                DeleteCurrentField()
            End If
        ElseIf TypeOf sender Is SignaturePropertiesForm Then
            RemoveHandler DirectCast(sender, SignaturePropertiesForm).Closed, AddressOf popupTool_Closed

            If Not DirectCast(sender, SignaturePropertiesForm).AcceptChanges Then
                DeleteCurrentField()
            End If
        ElseIf TypeOf sender Is TextBoxPropertiesForm Then
            RemoveHandler DirectCast(sender, TextBoxPropertiesForm).Closed, AddressOf popupTool_Closed

            If Not DirectCast(sender, TextBoxPropertiesForm).AcceptChanges Then
                DeleteCurrentField()
            End If
        End If

        popupTool.IsOpen = False
    End Sub

    Private _zoomValues As New List(Of Double)(New Double() { _
             25, _
             50, _
             75, _
             100, _
             125, _
             150, _
             200, _
             300, _
             400, _
             500, _
             600, _
             700, _
             800, _
             900, _
             1000 _
            })

    ''' <summary>
    ''' Gets the zoom values.
    ''' </summary>
    Public ReadOnly Property ZoomValues() As List(Of Double)
        Get
            Return _zoomValues
        End Get
    End Property

    ''' <summary>
    ''' Handles the Click event of the OpenFile control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub OpenFile_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim browseFile As New OpenFileDialog()
        browseFile.Multiselect = False
        browseFile.Filter = "PDF Files (*.pdf)|*.pdf"

        Dim result As System.Nullable(Of Boolean) = browseFile.ShowDialog()
        Dim boolResult As Boolean

        If Boolean.TryParse(result, boolResult) Then
            PdfControl.Document = New PDFDocument()
            PdfControl.Document.Load(browseFile.File.OpenRead())
        End If
    End Sub

    ''' <summary>
    ''' Handles the Click event of the SaveFile control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub SaveFile_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If PdfControl.Document IsNot Nothing AndAlso PdfControl.Document.PageCount > 0 Then
            Dim saveDialog As New SaveFileDialog()
            saveDialog.Filter = "PDF Files (*.pdf)|*.pdf"
            Dim result As System.Nullable(Of Boolean) = saveDialog.ShowDialog()
            Dim boolResult As Boolean

            If Boolean.TryParse(result, boolResult) Then
                Dim stream As System.IO.Stream = saveDialog.OpenFile()
                PdfControl.Document.Save(stream)
                stream.Close()
            End If
        Else
            MessageBox.Show("Nothing to save.", "Message", MessageBoxButton.OK)
        End If
    End Sub

    Private _selectedButton As Button

    ''' <summary>
    ''' Gets or sets the selected button.
    ''' </summary>
    ''' <value>
    ''' The selected button.
    ''' </value>
    Private Property SelectedButton() As Button
        Get
            Return _selectedButton
        End Get
        Set(ByVal value As Button)
            If _selectedButton IsNot Nothing Then
                ' Unselect previous button
                _selectedButton.BorderBrush = New SolidColorBrush(Colors.Transparent)
            End If

            _selectedButton = value

            If _selectedButton IsNot Nothing Then
                ' Select button
                _selectedButton.BorderBrush = New SolidColorBrush(Colors.Blue)
            End If
        End Set
    End Property

    ''' <summary>
    ''' Sets the work mode.
    ''' </summary>
    ''' <param name="pressedButton">The pressed button.</param>
    ''' <param name="workMode">The work mode.</param>
    Private Sub SetWorkMode(ByVal pressedButton As Button, ByVal workMode As UserInteractiveWorkMode)
        PdfControl.WorkMode = workMode
        SelectedButton = TryCast(pressedButton, Button)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the PanAndScanClick control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub PanAndScan_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.PanAndScan)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the EditAnnotations control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub EditAnnotations_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.EditAnnotations)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the TextBoxField control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub TextBoxField_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.AddTextBoxField)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the CheckBoxField control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub CheckBoxField_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.AddCheckBoxField)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the RadioButtonField control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub RadioButtonField_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.AddRadioButtonField)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the ListBoxField control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub ListBoxField_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.AddListBoxField)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the ComboBoxField control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub ComboBoxField_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.AddDropDownListField)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the ButtonField control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub ButtonField_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.AddPushButtonField)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the DigitalSignatureField control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub DigitalSignatureField_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SetWorkMode(TryCast(sender, Button), UserInteractiveWorkMode.AddSignatureField)
    End Sub
End Class