Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Printing
Imports System.ComponentModel
Imports C1.Silverlight.FlexGrid

Namespace FlexPrinting
    Partial Public Class MainPage
        Inherits UserControl
        Private _data As ICollectionView = Product.GetProducts(100)

        Public Sub New()
            InitializeComponent()

            ' populate grid
            _flex.ItemsSource = _data
            _flex.Columns("Line").AllowMerging = True
            _flex.Columns("Color").AllowMerging = True

            ' for testing
            _flex.AllowResizing = AllowResizing.Both
        End Sub

#Region "** event handlers"

        ' toggle grouping
        Private Sub _chkGroup_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Using _data.DeferRefresh()
                Dim gd = _data.GroupDescriptions
                gd.Clear()
                If _chkGroup.IsChecked.Value Then
                    _data.GroupDescriptions.Add(New System.Windows.Data.PropertyGroupDescription("Line"))
                    _data.GroupDescriptions.Add(New System.Windows.Data.PropertyGroupDescription("Color"))
                End If
            End Using
        End Sub

        ' toggle merging
        Private Sub _chkMerge_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            _flex.AllowMerging = If(_chkMerge.IsChecked.Value, AllowMerging.Cells, AllowMerging.None)
        End Sub

        ' toggle freezing
        Private Sub _chkFreeze_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            If _chkFreeze.IsChecked.Value Then
                _flex.Rows.Frozen = Math.Min(12, _flex.Selection.Row)
                _flex.Columns.Frozen = Math.Min(2, _flex.Selection.Column)

            Else
                _flex.Rows.Frozen = 0
                _flex.Columns.Frozen = 0
            End If
        End Sub
#End Region

#Region "** printing"

        ' basic printing
        Private Sub _btnBasicPrinting_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' get scale mode and margin
            Dim scaleMode = If(_cmbZoom.SelectedIndex = 0, C1.Silverlight.FlexGrid.ScaleMode.ActualSize, If(_cmbZoom.SelectedIndex = 1, C1.Silverlight.FlexGrid.ScaleMode.PageWidth, C1.Silverlight.FlexGrid.ScaleMode.SinglePage))
            Dim margin = If(_cmbMargins.SelectedIndex = 0, 0, If(_cmbMargins.SelectedIndex = 1, 96.0 / 2, 96.0))

            ' print the grid
            _flex.Print("C1FlexGrid", scaleMode, New Thickness(margin), 20)
        End Sub

        ' advanced printing
        Private Sub _btnAdvancedPrinting_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim pd = New System.Windows.Printing.PrintDocument()
            AddHandler pd.BeginPrint, AddressOf pd_BeginPrint
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            AddHandler pd.EndPrint, AddressOf pd_EndPrint
            pd.Print("C1FlexGrid")
        End Sub

        Private _pages As List(Of FrameworkElement)
        Private _margin As Double
        Private _scaleMode As ScaleMode
        Private _currentPage As Integer
        Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As BeginPrintEventArgs)
            _pages = Nothing

            ' get margins, scale mode
            _margin = If(_cmbMargins.SelectedIndex = 0, 0, If(_cmbMargins.SelectedIndex = 1, 96.0 / 2, 96.0))
            _scaleMode = If(_cmbZoom.SelectedIndex = 0, ScaleMode.ActualSize, If(_cmbZoom.SelectedIndex = 1, ScaleMode.PageWidth, ScaleMode.SinglePage))
        End Sub
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
            If _pages Is Nothing Then
                ' calculate page size, discounting margins
                Dim sz = e.PrintableArea
                sz.Width -= 2 * _margin
                sz.Height -= 2 * _margin

                ' generate images for every page
                _currentPage = 0
                _pages = _flex.GetPageImages(_scaleMode, sz, 100)
            End If

            ' create page element
            Dim pageTemplate_Renamed = New PageTemplate()

            ' set margins
            pageTemplate_Renamed.SetPageMargin(New Thickness(_margin))

            ' set content
            pageTemplate_Renamed.PageContent.Child = _pages(_currentPage)
            pageTemplate_Renamed.PageContent.Stretch = If(_scaleMode = ScaleMode.ActualSize, System.Windows.Media.Stretch.None, System.Windows.Media.Stretch.Uniform)

            ' set footer text
            pageTemplate_Renamed.FooterRight.Text = String.Format("Page {0} of {1}", _currentPage + 1, _pages.Count)

            ' render this page
            e.PageVisual = pageTemplate_Renamed

            ' move on to next page
            _currentPage += 1
            e.HasMorePages = _currentPage < _pages.Count
        End Sub
        Private Sub pd_EndPrint(ByVal sender As Object, ByVal e As EndPrintEventArgs)
            _pages = Nothing
        End Sub
#End Region
    End Class
End Namespace
