Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.RichEdit
Imports DevExpress.XtraRichEdit.Export
Imports DevExpress.XtraRichEdit.Export.Html
Imports DevExpress.XtraRichEdit.Commands
Imports DevExpress.Office.Services
Public Class dxwEditor
    Public Delegate Sub Guardar(ms As IO.Stream)
    Public Delegate Sub GuardarYExportarHTML(ms As IO.Stream, msHTML As IO.Stream)
    Private DlGuardar As Guardar
    Private DlGuardarYExportarHTML As GuardarYExportarHTML
    Public Delegate Sub CargarDatosMailMerge(dxrte As RichEditControl)
    Private DlCargarDatosMailMerge As CargarDatosMailMerge
    Private _FormatoOrigen As DevExpress.XtraRichEdit.DocumentFormat
    Private _FormatoDestino As DevExpress.XtraRichEdit.DocumentFormat
    Private _SoloLectura As Boolean



    Private Sub biGuardar_ItemClick(sender As Object, e As DevExpress.Xpf.Bars.ItemClickEventArgs)
        Dim ms As New IO.MemoryStream
        'If _Formato = DevExpress.XtraRichEdit.DocumentFormat.Html Then          
        '    Dim options As New HtmlDocumentExporterOptions()
        '    options.ExportRootTag = ExportRootTag.Html
        '    options.CssPropertiesExportType = CssPropertiesExportType.Inline
        '    options.EmbedImages = True
        '    Dim exporter = New HtmlExporter(Me.recEditor.Model, options)            
        '    Dim s As String = exporter.Export()
        '    ms = New IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes(s))
        'Else
        Me.recEditor.SaveDocument(ms, _FormatoDestino)
        'End If
        If Me.DlGuardar IsNot Nothing Then
            Me.DlGuardar.Invoke(ms)
        End If
        If Me.DlGuardarYExportarHTML IsNot Nothing Then
            Dim options As New HtmlDocumentExporterOptions()
            options.ExportRootTag = ExportRootTag.Html
            options.CssPropertiesExportType = CssPropertiesExportType.Inline
            options.EmbedImages = True
            Dim exporter = New HtmlExporter(Me.recEditor.Model, options)
            Dim s As String = exporter.Export()
            Dim mshtml = New IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(s))
            Me.DlGuardarYExportarHTML(ms, mshtml)
        End If



    End Sub
    Private Sub biGuardarYSalir_ItemClick(sender As Object, e As DevExpress.Xpf.Bars.ItemClickEventArgs)
        biGuardar_ItemClick(sender, e)
        Me.Close()
    End Sub

    Public Sub New(Documento As IO.Stream, DelegadoGuardar As Guardar, Optional DelegadoCargaMailMerge As CargarDatosMailMerge = Nothing, Optional SoloLectura As Boolean = False)
        _FormatoOrigen = DevExpress.XtraRichEdit.DocumentFormat.OpenXml
        _FormatoDestino = DevExpress.XtraRichEdit.DocumentFormat.OpenXml
        _SoloLectura = SoloLectura
        Nuevo(Documento, DelegadoGuardar, Nothing, DelegadoCargaMailMerge)

    End Sub
    Public Sub New(Documento As IO.Stream, FormatoOrigen As DevExpress.XtraRichEdit.DocumentFormat, FormatoDestino As DevExpress.XtraRichEdit.DocumentFormat, Optional DelegadoGuardarYExportarHTML As GuardarYExportarHTML = Nothing, Optional DelegadoCargaMailMerge As CargarDatosMailMerge = Nothing, Optional SoloLectura As Boolean = False)
        _FormatoOrigen = FormatoOrigen
        _FormatoDestino = FormatoDestino
        _SoloLectura = SoloLectura
        Nuevo(Documento, Nothing, DelegadoGuardarYExportarHTML, DelegadoCargaMailMerge)
    End Sub

    Public Sub New(Documento As IO.Stream, FormatoOrigen As DevExpress.XtraRichEdit.DocumentFormat, FormatoDestino As DevExpress.XtraRichEdit.DocumentFormat, DelegadoGuardar As Guardar, Optional DelegadoCargaMailMerge As CargarDatosMailMerge = Nothing, Optional SoloLectura As Boolean = False)
        _FormatoOrigen = FormatoOrigen
        _FormatoDestino = FormatoDestino
        _SoloLectura = SoloLectura
        Nuevo(Documento, DelegadoGuardar, Nothing, DelegadoCargaMailMerge)
    End Sub
    Private Sub Nuevo(Documento As IO.Stream, Optional DelegadoGuardar As Guardar = Nothing, Optional DelegadoGuardaryExportarHTML As GuardarYExportarHTML = Nothing, Optional DelegadoCargaMailMerge As CargarDatosMailMerge = Nothing)
        Try
            DXSplashScreen.Show(Of tsWPF.SplashScreenTecnosis)()
            DXSplashScreen.SetState("Cargando Documento ...")

            ' Llamada necesaria para el diseñador.
            InitializeComponent()
            DlGuardar = DelegadoGuardar
            If _SoloLectura OrElse DlGuardar Is Nothing Then Me.biGuardar.IsEnabled = False
            DlGuardarYExportarHTML = DelegadoGuardaryExportarHTML

            DlCargarDatosMailMerge = DelegadoCargaMailMerge
            If Documento IsNot Nothing Then
                Me.recEditor.LoadDocument(Documento, _FormatoOrigen)
            Else
                Me.recEditor.Document.Sections(0).Page.PaperKind = System.Drawing.Printing.PaperKind.A4
            End If
        Catch ex As Exception
            If DXSplashScreen.IsActive Then DXSplashScreen.Close()
            DXMessageBox.Show(ex.Message, "Error Cargando Documento")
        Finally

            If DXSplashScreen.IsActive Then DXSplashScreen.Close()
        End Try
        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub

    Private Sub dxwEditor_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        If Me.DlCargarDatosMailMerge IsNot Nothing Then
            Me.DlCargarDatosMailMerge.Invoke(Me.recEditor)
            'Else
            '    Me.pageMailMerge.IsVisible = False
        End If
    End Sub

End Class
