Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.Persistent.BaseImpl
Imports System.IO

Public Class RTFCopy_VC
    Inherits DevExpress.ExpressApp.ViewController

    '-----------------------Csak LISTVIEW-n működik!-------------------

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        ' Frame.GetController(Of RTFCopy_VC).aGetGridToRTFText.Active.SetItemValue("", False)
    End Sub
    Private Sub aGetGridToRTFText_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGetGridToRTFText.Execute
        Dim _ListView As ListView = TryCast(View, ListView)
        If _ListView IsNot Nothing Then
            Dim _GridListEditor As GridListEditor = TryCast(_ListView.Editor, GridListEditor)
            If _GridListEditor IsNot Nothing Then
                Dim _GridView As GridView = TryCast(_GridListEditor.GridView, GridView)
                If _GridView IsNot Nothing Then

                    Dim _FileName As String = Path.GetTempPath + "Grid.html"
                    _GridView.ExportToHtml(_FileName)

                    Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
                    Dim _UserCreated As User = SecuritySystem.CurrentUser
                    Dim _RTFCopy As RTFCopy = _onew.FindObject(Of RTFCopy)(DevExpress.Data.Filtering.CriteriaOperator.Parse("UserCreated=?", _onew.GetObject(_UserCreated)))
                    If _RTFCopy Is Nothing Then
                        _RTFCopy = New RTFCopy(_onew.Session)
                    End If
                    _RTFCopy.RTFString = File.ReadAllText(_FileName)

                    _onew.CommitChanges()
                End If
            End If
        End If
    End Sub
    Function RTFTableCreator(_Grid As GridView, Optional _FontSize As String = "8", Optional _FontType As String = "Tahoma") As String

        Dim _RTFtext As New StringBuilder

        Dim _HeaderFillColor As Int32 ' -- Current table header fill color index in colot table
        _HeaderFillColor = 2


        If _Grid.DataRowCount > 0 Then
            Dim _FontSizeNUM As Int32 = (Convert.ToInt32(_FontSize) * 2) ' -- Font size in half-points (the default is 16). => Size 8!!!

            Dim _RowNumFull As Integer = _Grid.RowCount
            Dim _RowNum As Integer = _Grid.DataRowCount
            Dim _ColumnsNum As Integer = _Grid.Columns.Count
            Dim irowIndex As Int32 = 0
            Dim _LastCellIndex As Int32 = 0

            '--Header to RTF format--
            '=================================================================================
            _RTFtext.Append("\pard\plain\ql\par\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10" & _
                            "\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")

            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\clvertalt\clcbpat2\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10" &
                                    "\cltxlrtb\clftsWidth3\clwWidth3302\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _ik)
                If _LastCellIndex < _ik Then
                    _LastCellIndex = _ik
                End If
            Next
            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\pard\plain\ql\intbl{\b\fs" & _FontSizeNUM & "\cf0 " & _Grid.Columns.View.VisibleColumns(_ik).Caption & "}\b\cell")
            Next

            _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10" & _
                            "\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")
            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\clvertalt\clcbpat" & _HeaderFillColor & "\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10" &
                                    "\cltxlrtb\clftsWidth3\clwWidth3302\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _ik)
            Next
            irowIndex += 1
            '' --Data to RTF format--
            '=================================================================================
            For _ik As Int16 = 0 To _RowNumFull - 2
                If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_ik)) = True Then
                    _RTFtext.Append("\row")

                    _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetGroupRowDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_ik)).ToString) & "}\cell")


                    _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                    "\trleft-108\trftsWidth1\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108\tblindtype3\tblind0")

                    _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth9619" & _
                                             "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _LastCellIndex)

                    irowIndex += 1
                Else
                    _RTFtext.Append("\row")

                    For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                        If _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Double" And _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Boolean" Then

                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetRowCellDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk))) & "}\cell")
                        ElseIf _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName = "System.Boolean" Then
                            If _Grid.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk)) = True Then
                                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Van" & "}\cell")
                            Else
                                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Nincs" & "}\cell")
                            End If
                        Else
                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0\qr " & LTrim(RTrim(Format(_Grid.Columns.View.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk)), "#,##0.00"))) & "}\qr\cell")

                        End If
                    Next

                    _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                    "\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")

                    For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1

                        _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth3302" & _
                                        "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _jk)
                    Next
                    irowIndex += 1
                End If
            Next
            '--Last row --
            '=================================================================================
            If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1)) = True Then
                _RTFtext.Append("\row")
                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetGroupRowDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1)).ToString) & "}\cell")

                _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                "\trleft-108\trftsWidth1\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108\tblindtype3\tblind0")

                _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth9619" & _
                                         "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _LastCellIndex)
            Else
                _RTFtext.Append("\row")
                For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                    If _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Double" And _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Boolean" Then

                        _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.Columns.View.GetRowCellDisplayText(_RowNum - 1, _Grid.Columns.View.VisibleColumns(_jk))) & "}\cell")
                    ElseIf _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName = "System.Boolean" Then
                        If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1)) = True Then

                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Van" & "}\cell")
                        Else
                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Nincs" & "}\cell")
                        End If
                    Else
                        _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0\qr " & LTrim(RTrim(Format(_Grid.Columns.View.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1), _Grid.Columns.View.VisibleColumns(_jk)), "#,##0.00"))) & "}\qr\cell")
                    End If
                Next

                _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\lastrow\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                   "\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")
                ''---
                For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1

                    _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth3302" & _
                                    "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _jk)
                Next
            End If

            _RTFtext.Append("\row\pard\plain\ql")
        End If
        Return _RTFtext.ToString
    End Function
End Class
