Imports System.Drawing
Imports System.Data
Imports System.Linq
Imports FarPoint.Win.Spread
Imports System.Reflection
Imports FarPoint.Win.Spread.DrawingSpace
Imports System.Text
Imports Spread.Common.Features.Spread.Common.Features.Sample

Namespace Features
    Partial Public Class ShapeActions
        Inherits BaseSampleControl
        Public Sub New()
            InitializeComponent()
            Name = "Shape Actions"

            InitSpreadSheet(fpSpread.ActiveSheet)
        End Sub

        Private Shared Function ConvertToHexString(bytes As Byte()) As String
            Dim hex As New StringBuilder()
            For Each b As Byte In bytes
                Dim hexPair As String = Convert.ToString(b, 16)
                If hexPair.Length < 2 Then
                    hexPair = "0" & hexPair
                End If
                hex.Append(hexPair)
            Next
            Return hex.ToString()
        End Function


        ''' <summary>
        ''' Create new instance of FarPoint.Win.Spread.DrawingSpace.<ShapeName>
        ''' </summary>
        ''' <param name="shapeName"></param>
        ''' <returns></returns>
        Friend Shared Function CreateShapeInstance(shapeName As String) As PSShape
            Dim currentAssembly As Assembly = GetType(FpSpread).Assembly
            Dim assemblyName As AssemblyName = currentAssembly.GetName()
            Dim fullname As String = currentAssembly.FullName

            Dim spAssemblyShortName As String = "FarPoint.Win.Spread"
            Dim spAssemblyVersion As String = assemblyName.Version.ToString()
            Dim spAssemblyCulture As String = (fullname.Split(","c))(2).Substring(9)
            Dim spAssemblyPublicKeyTokenBytes As Byte() = assemblyName.GetPublicKeyToken()
            Dim spAssemblyPublicKeyToken As String = ConvertToHexString(spAssemblyPublicKeyTokenBytes)
            Dim spNameTemplate As String = "{0}, Version={1}, Culture={2}, PublicKeyToken={3}"

            Dim spAssemblyFullName As String = String.Format(spNameTemplate, spAssemblyShortName, spAssemblyVersion, spAssemblyCulture, spAssemblyPublicKeyToken)
            Dim shapeInstance As Object = Activator.CreateInstance(spAssemblyFullName, "FarPoint.Win.Spread.DrawingSpace." & shapeName)
            Dim cShape As PSShape = CType(shapeInstance.Unwrap(), PSShape)
            'Return TryCast(Activator.CreateInstance(spAssemblyFullName, "FarPoint.Win.Spread.DrawingSpace." & shapeName).Unwrap(), PSShape)
            Return cShape
        End Function

        Private Sub InitSpreadSheet(sheet As SheetView)
            Dim shapes = Assembly.GetAssembly(GetType(PSShape)).GetTypes().Where(Function(type) type.IsSubclassOf(GetType(PSShape)) AndAlso Not type.IsAbstract AndAlso type.Name.Contains("Shape")).Take(12).ToList()

            Const WIDTH As Integer = 100, HEIGHT As Integer = 100
            Dim row As Integer = 3, col As Integer = 2

            For i As Integer = 0 To shapes.Count - 1
                If Not shapes(i).Name Is Nothing Then
                    Dim shape As PSShape = CreateShapeInstance(shapes(i).Name)
                    shape.Size = New Size(WIDTH, HEIGHT)
                    sheet.AddShape(shape, row, col)

                    col += 5
                    If col > 12 Then
                        row += 10
                        col = 2
                    End If
                End If
            Next
        End Sub
    End Class
End Namespace

'
'ShapeActions.Designer.vb
'

Namespace Features
	Partial Class ShapeActions
		''' <summary> 
		''' Required designer variable.
		''' </summary>
		Private components As System.ComponentModel.IContainer = Nothing

        ''' <summary> 
        ''' Clean up any resources being used.
        ''' </summary>
        ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        Protected Overloads Sub Dispose(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()
			Me.fpSpread = New FarPoint.Win.Spread.FpSpread()
			Me.fpSpread_Sheet1 = New FarPoint.Win.Spread.SheetView()
			Me.pnlMain.SuspendLayout()
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).BeginInit()
			Me.SuspendLayout()
			' 
			' pnlMain
			' 
			Me.pnlMain.Controls.Add(Me.fpSpread)
			' 
			' fpSpread
			' 
			Me.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill
			Me.fpSpread.Location = New System.Drawing.Point(0, 0)
			Me.fpSpread.Name = "fpSpread"
			Me.fpSpread.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me.fpSpread_Sheet1})
			Me.fpSpread.Size = New System.Drawing.Size(477, 487)
			Me.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White
			Me.fpSpread.StatusBarVisible = True
			Me.fpSpread.TabIndex = 0
			' 
			' fpSpread_Sheet1
			' 
			Me.fpSpread_Sheet1.Reset()
			Me.fpSpread_Sheet1.SheetName = "Sheet1"
			' Formulas and custom names must be loaded with R1C1 reference style
			Me.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
			Me.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
			' 
			' ShapeActions
			' 
			Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
			Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
			Me.Name = "ShapeActions"
			Me.pnlMain.ResumeLayout(False)
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).EndInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).EndInit()
			Me.ResumeLayout(False)

		End Sub

		#End Region

		Private fpSpread As FarPoint.Win.Spread.FpSpread
		Private fpSpread_Sheet1 As FarPoint.Win.Spread.SheetView
	End Class
End Namespace
