﻿Imports GrapeCity.CalcEngine
Imports GrapeCity.Spreadsheet
Imports Spread.Common.Features.Spread.Common.Features.Sample

Public Class QRCodeFunctionVisualization
     Inherits BaseSampleControl
     Sub New()

          ' This call is required by the designer.
          InitializeComponent()

          ' Add any initialization after the InitializeComponent() call.
          QRCodeFunction()
     End Sub

     Private Sub QRCodeFunction()
          fpSpread1.AddCustomFunction(New VisualFunction("QRCode", 1, 2, FunctionAttributes.[Variant], New QRCodeFunctionVisualizer()))
          Dim worksheet As IWorksheet = fpSpread1.AsWorkbook().ActiveSheet
          worksheet.Cells("B1").Value = "https://developer.mescius.com/spread"
          worksheet.Cells("C1").Value = "L"
          worksheet.Cells("A1").Formula = "VF.QRCode(B1, C1)"
          worksheet.Columns(0).ColumnWidth = 200
          worksheet.Rows(0).RowHeight = 200
     End Sub
End Class

'
'QRCodeFunctionVisualizer.vb
'

Imports FarPoint.Win.Spread.Visualization
Imports GrapeCity.CalcEngine
Imports GrapeCity.Spreadsheet
Imports QRCoder
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports QRCoder.QRCodeGenerator

Public Class QRCodeFunctionVisualizer
     Inherits FunctionVisualizer
     Protected Overrides Sub PaintCell(graphics As Graphics, rect As Rectangle, cellValue As Object, ByRef styleFormat As StyleFormat, worksheet As IWorksheet)
          Dim ECCLevel As ECCLevel = ECCLevel.L
          Dim forceUtf8 As Boolean = False
          Dim utf8BOM As Boolean = False
          Dim eciMode As EciMode = CType(0, EciMode)
          Dim requestedVersion As Integer = -1

          Dim visualizationData As VisualizationData = CType(cellValue, VisualizationData)
          If (Not IsNothing(visualizationData)) Then
               Dim context As IEvaluationContext = worksheet.Workbook.WorkbookSet.CalculationEngine.EvaluationContext
               Dim level As String = visualizationData.Parameters(0).GetText(context)
               If level = "L" Then
                    ECCLevel = 0
               ElseIf level = "M" Then
                    ECCLevel = 1
               ElseIf level = "Q" Then
                    ECCLevel = 2
               Else
                    ECCLevel = 3
               End If

               Using qrGenerator As New QRCodeGenerator()
                    Using QRCodeData As QRCodeData = qrGenerator.CreateQrCode(visualizationData.Value.GetText(context), ECCLevel, forceUtf8, utf8BOM, eciMode, requestedVersion)
                         Using qrCode As New QRCode(QRCodeData)
                              Using image As Bitmap = qrCode.GetGraphic(20, System.Drawing.Color.Black, System.Drawing.Color.White, Nothing, rect.Width)
                                   graphics.DrawImage(image, rect)
                              End Using
                         End Using
                    End Using
               End Using
          End If
     End Sub
End Class

'
'CustomDataVisualization.Designer.vb
'

Imports Spread.Common.Features.Spread.Common.Features.Sample

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class QRCodeFunctionVisualization
     Inherits BaseSampleControl

     'UserControl overrides dispose to clean up the component list.
     <System.Diagnostics.DebuggerNonUserCode()>
     Protected Shadows Sub Dispose(ByVal disposing As Boolean)
          Try
               If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
               End If
          Finally
               MyBase.Dispose(disposing)
          End Try
     End Sub

     'Required by the Windows Form Designer
     Private components As System.ComponentModel.IContainer

     'NOTE: The following procedure is required by the Windows Form Designer
     'It can be modified using the Windows Form Designer.  
     'Do not modify it using the code editor.
     <System.Diagnostics.DebuggerStepThrough()> _
     Private Sub InitializeComponent()
          Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(QRCodeFunctionVisualization))
          Me.fpSpread1 = New FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, CType(resources.GetObject("pnlMain.Controls"), Object))
          Me.fpSpread1_Sheet1 = Me.fpSpread1.GetSheet(0)
          Me.formulaTextBox1 = New FarPoint.Win.Spread.FormulaTextBox()
          Me.pnlMain.SuspendLayout()
          CType(Me.fpSpread1, System.ComponentModel.ISupportInitialize).BeginInit()
          Me.SuspendLayout()
          '
          'pnlMain
          '
          Me.pnlMain.Controls.Add(Me.fpSpread1)
          Me.pnlMain.Controls.Add(Me.formulaTextBox1)
          '
          'fpSpread1
          '
          Me.fpSpread1.AccessibleDescription = "fpSpread1, Sheet1, Row 0, Column 0"
          Me.fpSpread1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
          Me.fpSpread1.BackColor = System.Drawing.SystemColors.Control
          Me.fpSpread1.Font = New System.Drawing.Font("Calibri", 11.0!)
          Me.fpSpread1.Location = New System.Drawing.Point(0, 23)
          Me.fpSpread1.Name = "fpSpread1"
          Me.fpSpread1.RightToLeft = System.Windows.Forms.RightToLeft.No
          Me.fpSpread1.Size = New System.Drawing.Size(474, 461)
          Me.fpSpread1.TabIndex = 2
          '
          'formulaTextBox1
          '
          Me.formulaTextBox1.Dock = System.Windows.Forms.DockStyle.Top
          Me.formulaTextBox1.Location = New System.Drawing.Point(0, 0)
          Me.formulaTextBox1.Name = "formulaTextBox1"
          Me.formulaTextBox1.Size = New System.Drawing.Size(477, 23)
          Me.formulaTextBox1.TabIndex = 3
          'Attach formulaTextBox1 to fpSpread1
          Me.formulaTextBox1.Attach(Me.fpSpread1)
          '
          'QRCodeFunctionVisualization
          '
          Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
          Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
          Me.Name = "Custom Data Visualization"
          Me.pnlMain.ResumeLayout(False)
          CType(Me.fpSpread1, System.ComponentModel.ISupportInitialize).EndInit()
          Me.ResumeLayout(False)

     End Sub

     Private WithEvents formulaTextBox1 As FarPoint.Win.Spread.FormulaTextBox
     Private WithEvents fpSpread1 As FarPoint.Win.Spread.FpSpread
     Friend WithEvents fpSpread1_Sheet1 As FarPoint.Win.Spread.SheetView
End Class
