Imports Microsoft.Win32
Imports System.IO

Class MainWindow
  Private Enum ResultadoDlg
    Aceptar
    Cancelar
    [Error]
  End Enum

  Public Sub New()
    InitializeComponent()

    ' Se admiten tres intentos para introducir la palabra de paso
    Dim nCuenta As Integer = 0
    Dim vr As ResultadoDlg
    ' Aceptar, Cancelar o Error
    Do
      nCuenta += 1
      vr = contraseña()
    Loop While vr = ResultadoDlg.[Error] AndAlso nCuenta < 3
    If vr = ResultadoDlg.Cancelar OrElse nCuenta = 3 Then
      Close()
    End If
  End Sub

  Private Function contraseña() As ResultadoDlg
    Dim sPalabraDePaso As String
    ' Crear la caja de diálogo de entrada
    Dim Dlg As New DlgPalabraDePaso()

    ' Mostrar el diálogo
    If Dlg.ShowDialog() = True Then
      ' Obtener los datos
      sPalabraDePaso = Dlg.ctPalabraDePaso.Password
    Else
      Return ResultadoDlg.Cancelar
    End If

    ' Verificar si la palabra de paso es correcta
    Dim pp As String = MySettings.Default.PalabraDePaso
    If sPalabraDePaso.CompareTo(pp) <> 0 Then
      MessageBox.Show("La palabra de paso no es correcta", "Error",
                      MessageBoxButton.OK, MessageBoxImage.[Error])
      Return ResultadoDlg.[Error]
    End If
    Return ResultadoDlg.Aceptar
  End Function

  Private Sub ArchivoSalir_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Close()
  End Sub

  Private Sub AyudaAcercaDe_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New AcercaDe()
    dlg.ShowDialog()
  End Sub

  Private Sub DialogoCasillaV_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgCasillaVerificacion()
    dlg.ShowDialog()
  End Sub

  Private Sub DialogoBtOpcion_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgBasesNum()
    dlg.ShowDialog()
  End Sub

  Private Sub DialogoListaSimple_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgListaSimple()
    dlg.ShowDialog()
  End Sub

  Private Sub DialogoListaDesplegable_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgListaDesplegable()
    dlg.ShowDialog()
  End Sub

  Private Sub DialogoRangos_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgRangos()
    dlg.ShowDialog()
  End Sub

  Private Sub Calendario_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgCalendario()
    dlg.ShowDialog()
  End Sub

  Private Sub DialogoListView_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgListView()
    dlg.ShowDialog()
  End Sub

  Private Sub DialogoTreeView_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgTreeView()
    dlg.ShowDialog()
  End Sub

  Private Sub DialogoDataGrid_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgDataGrid()
    dlg.ShowDialog()
  End Sub

  Private Sub ArchivoAbrir_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim DlgAbrir As New OpenFileDialog()

    DlgAbrir.Filter = "ficheros xaml (*.xaml)|*.xaml" & "|ficheros rtf (*.rtf)|*.rtf|Todos (*.*)|*.*"
    DlgAbrir.FilterIndex = 2
    ' Mostrar el diálogo Abrir
    If DlgAbrir.ShowDialog() = True Then
      Dim flowDocument As New FlowDocument()
      Dim textRange As New TextRange(flowDocument.ContentStart, flowDocument.ContentEnd)
      Dim fStream As New FileStream(DlgAbrir.FileName, FileMode.Open)
      If DlgAbrir.FileName.ToUpper().EndsWith(".XAML") Then
        textRange.Load(fStream, DataFormats.Xaml)
      Else
        textRange.Load(fStream, DataFormats.Rtf)
      End If
      fStream.Close()
      richTextBox.Document = flowDocument
    End If
  End Sub

  Private Sub ArchivoGuardar_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim DlgGuardar As New SaveFileDialog()

    DlgGuardar.Filter = "ficheros xaml (*.xaml)|*.xaml" & "|ficheros rtf (*.rtf)|*.rtf|Todos (*.*)|*.*"
    DlgGuardar.FilterIndex = 2

    If DlgGuardar.ShowDialog() = True Then
      Dim fStream As FileStream
      If richTextBox.Document IsNot Nothing Then
        Dim selección As New TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd)
        fStream = New FileStream(DlgGuardar.FileName, FileMode.Create)
        If DlgGuardar.FileName.ToUpper().EndsWith(".XAML") Then
          selección.Save(fStream, DataFormats.Xaml)
        Else
          selección.Save(fStream, DataFormats.Rtf)
        End If
        fStream.Close()
      End If
    End If
  End Sub

  Private Sub DialogoColor_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

  End Sub

  Private Sub DialogoDocDinamico_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim dlg As New DlgDocDinamico()
    dlg.ShowDialog()
  End Sub

  Private Sub ArchivoImprimir_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
    Dim descripcionDoc As String = "Documento"
    Dim DlgImprimir As New PrintDialog()
    If DlgImprimir.ShowDialog() = True Then
      '' Imprimir el documento paginado existente
      'Dim docFuente As IDocumentPaginatorSource = richTextBox.Document
      'DlgImprimir.PrintDocument(docFuente.DocumentPaginator, descripcionDoc)
      Dim docFuente As FlowDocument = richTextBox.Document

      ' Guardar la configuración actual                                
      Dim pageWidth As Double = docFuente.PageWidth
      Dim pageHeight As Double = docFuente.PageHeight
      Dim columnGap As Double = docFuente.ColumnGap
      Dim columnWidth As Double = docFuente.ColumnWidth
      Dim pagePadding As Thickness = docFuente.PagePadding

      ' Hacer coincidir la página FlowDocument con la página impresa
      docFuente.PageHeight = DlgImprimir.PrintableAreaHeight
      docFuente.PageWidth = DlgImprimir.PrintableAreaWidth
      docFuente.PagePadding = New Thickness(50)

      ' Utilizar dos columnas
      docFuente.ColumnGap = 24
      docFuente.ColumnWidth = (docFuente.PageWidth - docFuente.ColumnGap - docFuente.PagePadding.Left - docFuente.PagePadding.Right) / 2

      Dim docFuentePag As IDocumentPaginatorSource = docFuente
      DlgImprimir.PrintDocument(docFuentePag.DocumentPaginator, descripcionDoc)

      ' Restablecer la configuración inicial
      docFuente.PageWidth = pageWidth
      docFuente.PageHeight = pageHeight
      docFuente.ColumnGap = columnGap
      docFuente.ColumnWidth = columnWidth
      docFuente.PagePadding = pagePadding
    End If
  End Sub
End Class
