﻿Imports FarPoint.Win.Spread
Imports GrapeCity.Spreadsheet
Imports Spread.Common.Features.Spread.Common.Features.Sample
Imports Spread.Worksheet.Properties
Imports System.IO
Imports System.Windows.Forms

Partial Public Class ProtectionDialog
    Inherits BaseSampleControl

    Private _isEmptyPasswordProtected As Boolean

    Public Sub New()
        InitializeComponent()
        Name = "Protection Dialog"
        SetupSpread()
    End Sub

    Private Sub SetupSpread()
        _fpSpread.Features.EnhancedShapeEngine = True

        Using stream As MemoryStream = New MemoryStream(My.Resources.SalesOrders)
            _fpSpread.OpenExcel(stream)
        End Using

        AddHandler _fpSpread.ActiveSheet.PropertyChanged, AddressOf OnPropertyChanged
        _fpSpread.Features.AutoCreateHyperlink = True
        _fpSpread.Features.ExcelCompatibleKeyboardShortcuts = True
        _fpSpread.TabStrip.Editable = True
        _isEmptyPasswordProtected = False
    End Sub

    Private Sub OnProtectSheetMenuItemClick(ByVal sender As Object, ByVal e As System.EventArgs)
        If _fpSpread.ActiveSheet Is Nothing Then Return

        If Not _isEmptyPasswordProtected Then
            Dim protectSheetDialog As FarPoint.Win.Spread.Dialogs.ProtectSheetDialog = New FarPoint.Win.Spread.Dialogs.ProtectSheetDialog(_fpSpread)
            protectSheetDialog.ShowDialog(Me)
        Else
            _fpSpread.AsWorkbook().ActiveSheet.Unprotect(String.Empty)
        End If
    End Sub

    Private Sub OnProtectTabStripMenuItemClick(ByVal sender As Object, ByVal e As System.EventArgs)
        _fpSpread.TabStrip.Editable = Not _fpSpread.TabStrip.Editable

        If Not _fpSpread.TabStrip.Editable Then
            _fpSpread.Focus()
            _protectTabStripMenuItem.Text = "Un&protect TabStrip"
        Else
            _protectTabStripMenuItem.Text = "&Protect TabStrip"
        End If
    End Sub

    Private Sub OnFormatCellsMenuItemClick(ByVal sender As Object, ByVal e As System.EventArgs)
        ShowFormatCellsDialog()
    End Sub

    Private Sub ShowFormatCellsDialog()
        If _fpSpread.ActiveSheet Is Nothing Then Return
        Dim workSheet As IWorksheet = _fpSpread.ActiveSheet.AsWorksheet()

        If Not _fpSpread.ActiveSheet.Protect OrElse (_fpSpread.ActiveSheet.Protect AndAlso workSheet.Protection.AllowFormattingCells) Then
            Dim dialog = FarPoint.Win.Spread.Dialogs.BuiltInDialogs.FormatCells(workSheet.Selection)
            dialog.ShowDialog()
        End If
    End Sub

    Private Sub OnPropertyChanged(ByVal sender As Object, ByVal e As SheetViewPropertyChangeEventArgs)
        _isEmptyPasswordProtected = False

        If _fpSpread.ActiveSheet.AsWorksheet().ProtectionMode = GrapeCity.Spreadsheet.ProtectionMode.[Protected] Then
            _isEmptyPasswordProtected = True
        End If

        If _fpSpread.ActiveSheet.Protect Then
            _protectSheetMenuItem.Text = "Un&protect Sheet"
        Else
            _protectSheetMenuItem.Text = "&Protect Sheet"
        End If
    End Sub
End Class

//
// ProtectionDialog.Designer.vb
//


Partial Class ProtectionDialog
        ''' <summary> 
        ''' Required designer variable.
        ''' </summary>
        Private components As ComponentModel.IContainer = Nothing

        ''' <summary> 
        ''' Clean up any resources being used.
        ''' </summary>
        ''' <paramname="disposing">true if managed resources should be disposed; otherwise, false.</param>
        Protected Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

#Region "Component Designer generated code"

        ''' <summary> 
        ''' Required method for Designer support - do not modify 
        ''' the contents of this method with the code editor.
        ''' </summary>
        Private Sub InitializeComponent()
            components = New ComponentModel.Container()
            Dim resources As ComponentModel.ComponentResourceManager = New ComponentModel.ComponentResourceManager(GetType(ProtectionDialog))
            contextMenuStrip1 = New Windows.Forms.ContextMenuStrip(components)
            menuStrip1 = New Windows.Forms.MenuStrip()
            _protectSheetMenuItem = New Windows.Forms.ToolStripMenuItem()
            _protectTabStripMenuItem = New Windows.Forms.ToolStripMenuItem()
            _formatCellsMenuItem = New Windows.Forms.ToolStripMenuItem()
            splitContainer1 = New Windows.Forms.SplitContainer()
            splitContainer2 = New Windows.Forms.SplitContainer()
            nameBox1 = New FarPoint.Win.Spread.NameBox()
            _fpSpread = New FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, resources.GetObject("splitContainer2.Panel1.Controls"))
            _fpSpread_Sheet1 = _fpSpread.GetSheet(0)
            panel1 = New Windows.Forms.Panel()
            formulaTextBox1 = New FarPoint.Win.Spread.FormulaTextBox()
            pnlMain.SuspendLayout()
            menuStrip1.SuspendLayout()
            CType(splitContainer1, ComponentModel.ISupportInitialize).BeginInit()
            splitContainer1.Panel1.SuspendLayout()
            splitContainer1.Panel2.SuspendLayout()
            splitContainer1.SuspendLayout()
            CType(splitContainer2, ComponentModel.ISupportInitialize).BeginInit()
            splitContainer2.Panel1.SuspendLayout()
            splitContainer2.Panel2.SuspendLayout()
            splitContainer2.SuspendLayout()
            CType(_fpSpread, ComponentModel.ISupportInitialize).BeginInit()
            panel1.SuspendLayout()
            SuspendLayout()
            ' 
            ' pnlMain
            ' 
            pnlMain.Controls.Add(splitContainer1)
            pnlMain.Controls.Add(menuStrip1)
            ' 
            ' pnlControls
            ' 
            pnlControls.Size = New Drawing.Size(210, 599)
            ' 
            ' contextMenuStrip1
            ' 
            contextMenuStrip1.ImageScalingSize = New Drawing.Size(20, 20)
            contextMenuStrip1.Name = "contextMenuStrip1"
            contextMenuStrip1.Size = New Drawing.Size(61, 4)
            ' 
            ' menuStrip1
            ' 
            menuStrip1.ImageScalingSize = New Drawing.Size(20, 20)
            menuStrip1.Items.AddRange(New Windows.Forms.ToolStripItem() {_protectSheetMenuItem, _protectTabStripMenuItem, _formatCellsMenuItem})
            menuStrip1.Location = New Drawing.Point(0, 0)
            menuStrip1.Name = "menuStrip1"
            menuStrip1.Size = New Drawing.Size(636, 28)
            menuStrip1.TabIndex = 0
            menuStrip1.Text = "menuStrip1"
            ' 
            ' _protectSheetMenuItem
            ' 
            _protectSheetMenuItem.Name = "_protectSheetMenuItem"
            _protectSheetMenuItem.Size = New Drawing.Size(111, 24)
            _protectSheetMenuItem.Text = "&Protect Sheet"
            AddHandler _protectSheetMenuItem.Click, New EventHandler(AddressOf OnProtectSheetMenuItemClick)
            ' 
            ' _protectTabStripMenuItem
            ' 
            _protectTabStripMenuItem.Name = "_protectTabStripMenuItem"
            _protectTabStripMenuItem.Size = New Drawing.Size(128, 24)
            _protectTabStripMenuItem.Text = "Protect TabStrip"
            AddHandler _protectTabStripMenuItem.Click, New EventHandler(AddressOf OnProtectTabStripMenuItemClick)
            ' 
            ' _formatCellsMenuItem
            ' 
            _formatCellsMenuItem.Name = "_formatCellsMenuItem"
            _formatCellsMenuItem.Size = New Drawing.Size(105, 24)
            _formatCellsMenuItem.Text = "Format Cells"
            AddHandler _formatCellsMenuItem.Click, New EventHandler(AddressOf OnFormatCellsMenuItemClick)
            ' 
            ' splitContainer1
            ' 
            splitContainer1.Dock = Windows.Forms.DockStyle.Fill
            splitContainer1.IsSplitterFixed = True
            splitContainer1.Location = New Drawing.Point(0, 28)
            splitContainer1.Name = "splitContainer1"
            splitContainer1.Orientation = Windows.Forms.Orientation.Horizontal
            ' 
            ' splitContainer1.Panel1
            ' 
            splitContainer1.Panel1.Controls.Add(splitContainer2)
            ' 
            ' splitContainer1.Panel2
            ' 
            splitContainer1.Panel2.Controls.Add(_fpSpread)
            splitContainer1.Size = New Drawing.Size(636, 571)
            splitContainer1.SplitterDistance = 25
            splitContainer1.TabIndex = 1
            ' 
            ' splitContainer2
            ' 
            splitContainer2.Dock = Windows.Forms.DockStyle.Fill
            splitContainer2.IsSplitterFixed = True
            splitContainer2.Location = New Drawing.Point(0, 0)
            splitContainer2.Name = "splitContainer2"
            ' 
            ' splitContainer2.Panel1
            ' 
            splitContainer2.Panel1.Controls.Add(nameBox1)
            ' 
            ' splitContainer2.Panel2
            ' 
            splitContainer2.Panel2.Controls.Add(panel1)
            splitContainer2.Size = New Drawing.Size(636, 25)
            splitContainer2.SplitterDistance = 149
            splitContainer2.TabIndex = 0
            ' 
            ' nameBox1
            ' 
            nameBox1.Dock = Windows.Forms.DockStyle.Fill
            nameBox1.Location = New Drawing.Point(0, 0)
            nameBox1.MaxLength = 255
            nameBox1.Name = "nameBox1"
            nameBox1.Size = New Drawing.Size(149, 24)
            nameBox1.TabIndex = 0
            ' Attach nameBox1 to _fpSpread
            nameBox1.Attach(_fpSpread)
            ' 
            ' _fpSpread
            ' 
            _fpSpread.AccessibleDescription = "Book1, Sheet1, Row 0, Column 0"
            _fpSpread.Dock = Windows.Forms.DockStyle.Fill
            _fpSpread.Font = New Drawing.Font("Calibri", 11.0F)
            _fpSpread.Location = New Drawing.Point(0, 0)
            _fpSpread.Name = "_fpSpread"
            _fpSpread.Size = New Drawing.Size(636, 542)
            _fpSpread.TabIndex = 0
            ' 
            ' panel1
            ' 
            panel1.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
            panel1.Controls.Add(formulaTextBox1)
            panel1.Dock = Windows.Forms.DockStyle.Fill
            panel1.Location = New Drawing.Point(0, 0)
            panel1.Name = "panel1"
            panel1.Size = New Drawing.Size(483, 25)
            panel1.TabIndex = 0
            ' 
            ' formulaTextBox1
            ' 
            formulaTextBox1.AcceptsTab = True
            formulaTextBox1.BorderStyle = Windows.Forms.BorderStyle.None
            formulaTextBox1.Dock = Windows.Forms.DockStyle.Fill
            formulaTextBox1.Location = New Drawing.Point(0, 0)
            formulaTextBox1.Margin = New Windows.Forms.Padding(0)
            formulaTextBox1.Name = "formulaTextBox1"
            formulaTextBox1.RightToLeft = Windows.Forms.RightToLeft.No
            formulaTextBox1.ScrollBars = Windows.Forms.RichTextBoxScrollBars.None
            formulaTextBox1.Size = New Drawing.Size(481, 23)
            formulaTextBox1.TabIndex = 0
            ' Attach formulaTextBox1 to _fpSpread
            formulaTextBox1.Attach(_fpSpread)
            ' 
            ' ProtectionDialog
            ' 
            AutoScaleDimensions = New Drawing.SizeF(8.0F, 16.0F)
            AutoScaleMode = Windows.Forms.AutoScaleMode.Font
            Name = "ProtectionDialog"
            pnlMain.ResumeLayout(False)
            pnlMain.PerformLayout()
            menuStrip1.ResumeLayout(False)
            menuStrip1.PerformLayout()
            splitContainer1.Panel1.ResumeLayout(False)
            splitContainer1.Panel2.ResumeLayout(False)
            CType(splitContainer1, ComponentModel.ISupportInitialize).EndInit()
            splitContainer1.ResumeLayout(False)
            splitContainer2.Panel1.ResumeLayout(False)
            splitContainer2.Panel2.ResumeLayout(False)
            CType(splitContainer2, ComponentModel.ISupportInitialize).EndInit()
            splitContainer2.ResumeLayout(False)
            CType(_fpSpread, ComponentModel.ISupportInitialize).EndInit()
            panel1.ResumeLayout(False)
            ResumeLayout(False)

        End Sub

#End Region

        Private contextMenuStrip1 As Windows.Forms.ContextMenuStrip
        Private menuStrip1 As Windows.Forms.MenuStrip
        Private _protectSheetMenuItem As Windows.Forms.ToolStripMenuItem
        Private splitContainer1 As Windows.Forms.SplitContainer
        Private splitContainer2 As Windows.Forms.SplitContainer
        Private nameBox1 As FarPoint.Win.Spread.NameBox
        Private panel1 As Windows.Forms.Panel
        Private formulaTextBox1 As FarPoint.Win.Spread.FormulaTextBox
        Private _fpSpread As FarPoint.Win.Spread.FpSpread
        Private _fpSpread_Sheet1 As FarPoint.Win.Spread.SheetView
        Private _protectTabStripMenuItem As Windows.Forms.ToolStripMenuItem
        Private _formatCellsMenuItem As Windows.Forms.ToolStripMenuItem
    End Class
