Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Code Converter - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Data
  2. Imports System.Text.RegularExpressions
  3. Imports TallComponents.PDF.Layout
  4. Imports Code_Converter.CodeFormatting
  5. Public Class frmMain
  6.     Public Delegate Function ConfirmFunction() As Boolean
  7.     Private Delegate Sub Converter(ByVal R As KeywordRegex, ByRef S As String)
  8.  
  9.     Private Enum ConvertFormat
  10.         Html
  11.         HtmlCss
  12.         Rtf
  13.         Pdf
  14.     End Enum
  15.  
  16.     Private Function Step2_SelectFormat() As Boolean
  17.         Dim Done As Boolean = True
  18.         If rdbHtml.Checked Then
  19.             SelectedFormat = ConvertFormat.Html
  20.         ElseIf rdbHtmlCss.Checked Then
  21.             SelectedFormat = ConvertFormat.HtmlCss
  22.         ElseIf rdbRtf.Checked Then
  23.             SelectedFormat = ConvertFormat.Rtf
  24.             If MessageBox.Show("Il formato RTF, a volte, può essere letto in modi leggermente differenti a seconda del " & _
  25.             "programma utilizzato per la lettura. Microsoft Word e WordPad, ad esempio, differiscono per l'uso degli " & _
  26.             "indici nel sistema dei colori. Al fine di non compromettere il documento in modo vistoso, seleziona Sì nel caso " & _
  27.             "usassi Microsoft Word, o No per WordPad.", "Code Converter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
  28.                 OpenWithMSWord = True
  29.             Else
  30.                 OpenWithMSWord = False
  31.             End If
  32.         ElseIf rdbPdf.Checked Then
  33.             SelectedFormat = ConvertFormat.Pdf
  34.         Else
  35.             ErrorMessage = "Completare tutti i campi richiesti!"
  36.             Done = False
  37.         End If
  38.         Return Done
  39.     End Function
  40.  
  41.     Private Function Step3_GetCode() As Boolean
  42.         Dim Done As Boolean = True
  43.  
  44.         If rdbFromCode.Checked Then
  45.             If txtCode.Text = "" Or txtCode.Text = Nothing Then
  46.                 ErrorMessage = "Nessun codice inserito!"
  47.                 Done = False
  48.             Else
  49.                 Source = txtCode.Text
  50.             End If
  51.         ElseIf rdbFromFile.Checked Then
  52.             If txtFile.Text = "" Or txtFile.Text = Nothing Then
  53.                 Done = False
  54.             Else
  55.                 If IO.File.Exists(txtFile.Text) Then
  56.                     Source = IO.File.ReadAllText(txtFile.Text)
  57.                 Else
  58.                     ErrorMessage = "File inesistente!"
  59.                     Done = False
  60.                 End If
  61.             End If
  62.         Else
  63.             ErrorMessage = "Completare tutti i campi richiesti!"
  64.             Done = False
  65.         End If
  66.  
  67.         Return Done
  68.     End Function
  69.  
  70.     Private Function Step4_SelectLanguage() As Boolean
  71.         For Each C As Control In pnlStep4.Controls
  72.             If TypeOf C Is RadioButton Then
  73.                 If DirectCast(C, RadioButton).Checked Then
  74.                     SelectedLanguage = DirectCast(C, RadioButton).Tag
  75.                     Return True
  76.                 End If
  77.             End If
  78.         Next
  79.         ErrorMessage = "Nessun linguaggio selezionato!"
  80.         Return False
  81.     End Function
  82.  
  83.     Private Function Step5_GetFormat() As Boolean
  84.         SourceFont.Font = fcbSource.SelectedFont
  85.         SourceFont.Color = fcbSource.SelectedColor
  86.         KeyFont.Font = fcbKeyword.SelectedFont
  87.         KeyFont.Color = fcbKeyword.SelectedColor
  88.         CommentFont.Font = fcbComment.SelectedFont
  89.         CommentFont.Color = fcbComment.SelectedColor
  90.         Return True
  91.     End Function
  92.  
  93.     Private Sub Step6_Convert(ByVal FileName As String)
  94.         cmdNext.Enabled = False
  95.         cmdPrev.Enabled = False
  96.         lblProg.Text = "Conversione in corso..."
  97.         Application.DoEvents()
  98.  
  99.         Dim Output As BaseConverter
  100.  
  101.         Select Case SelectedFormat
  102.             Case ConvertFormat.Html
  103.                 Output = New HtmlConverter(SourceFont, CommentFont, KeyFont)
  104.             Case ConvertFormat.HtmlCss
  105.                 Output = New HtmlCssConverter(SourceFont, CommentFont, KeyFont)
  106.             Case ConvertFormat.Rtf
  107.                 Output = New RtfConverter(SourceFont, CommentFont, KeyFont)
  108.                 CType(Output, RtfConverter).OpenWithMSWord = Me.OpenWithMSWord
  109.             Case ConvertFormat.Pdf
  110.                 Output = New PdfConverter(SourceFont, CommentFont, KeyFont)
  111.         End Select
  112.  
  113.         Output.Source = Source
  114.         Output.LoadCCL(Languages(SelectedLanguage))
  115.  
  116.         Output.Create(FileName)
  117.         Output.Convert(prgProg)
  118.         prgProg.Value = 100
  119.         Output.Write()
  120.         Output.Close()
  121.  
  122.         Application.DoEvents()
  123.         lblProg.Text = "Operazione completata"
  124.  
  125.         cmdNext_Click(Me, EventArgs.Empty)
  126.     End Sub
  127.  
  128.     Private Sub InitializeFile(ByVal File As Object, Optional ByVal FileName As String = "")
  129.         Select Case SelectedFormat
  130.          
  131.             Case ConvertFormat.Pdf
  132.                 Dim Pdf As Document = CType(File, Document)
  133.                 With Pdf.DocumentInfo
  134.                     .Author = "Totem"
  135.                     .Creator = "Code Converter"
  136.                     .Subject = "Codice sorgente"
  137.                 End With
  138.                 Pdf.Sections.Add(New Section)
  139.         End Select
  140.     End Sub
  141.  
  142.     Private Function GetConfirmFunction() As ConfirmFunction
  143.         Select Case CurrentPanel
  144.             Case 1
  145.                 Return Nothing
  146.             Case 2
  147.                 Return AddressOf Step2_SelectFormat
  148.             Case 3
  149.                 Return AddressOf Step3_GetCode
  150.             Case 4
  151.                 Return AddressOf Step4_SelectLanguage
  152.             Case 5
  153.                 Return AddressOf Step5_GetFormat
  154.         End Select
  155.     End Function
  156.  
  157.     Private ReadOnly Property PanelName() As String
  158.         Get
  159.             Return "pnlStep" & CurrentPanel
  160.         End Get
  161.     End Property
  162.  
  163.     Private ReadOnly Property CanGoForward() As Boolean
  164.         Get
  165.             Return (CurrentPanel <> PanelCount)
  166.         End Get
  167.     End Property
  168.  
  169.     Private ReadOnly Property CanGoBack() As Boolean
  170.         Get
  171.             Return (CurrentPanel > 1)
  172.         End Get
  173.     End Property
  174.  
  175.     Private CurrentPanel As Byte = 1
  176.     Private PanelCount As Byte = 7
  177.     Private Languages As New List(Of String)
  178.  
  179.     Private SelectedFormat As ConvertFormat
  180.     Private SelectedLanguage As Byte = 0
  181.     Private Source, OutputFile As String
  182.     Private ErrorMessage As String
  183.     Private SourceFont, KeyFont, CommentFont As ColoredFont
  184.     Private OpenWithMSWord As Boolean
  185.  
  186.     Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  187.         Dim Rdb As RadioButton
  188.         Dim Y As Int32 = 82
  189.         For Each File As String In IO.Directory.GetFiles(Application.StartupPath & "\Languages")
  190.             Languages.Add(File)
  191.             Rdb = New RadioButton
  192.             Rdb.Checked = False
  193.             Rdb.Location = New Point(20, Y)
  194.             Rdb.AutoSize = True
  195.             Rdb.Text = IO.Path.GetFileNameWithoutExtension(File)
  196.             Rdb.Tag = Languages.Count - 1
  197.             Y += 20
  198.             pnlStep4.Controls.Add(Rdb)
  199.         Next
  200.  
  201.         Dim r As New Regex("\s+(?<key>As)\s+")
  202.         Dim s As New Regex("")
  203.         'MsgBox(r.Replace("Public PublicVar, VarPublicAs String, E As Int16", "<k>${key}</k>"))
  204.     End Sub
  205.  
  206.     Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
  207.         Dim Scan As ConfirmFunction = GetConfirmFunction()
  208.  
  209.         If Scan IsNot Nothing Then
  210.             If Not Scan() Then
  211.                 MessageBox.Show(ErrorMessage, "Code Converter", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  212.                 Exit Sub
  213.             End If
  214.         End If
  215.  
  216.         Me.Controls(Me.PanelName).Visible = False
  217.         CurrentPanel += 1
  218.         Me.Controls(Me.PanelName).Visible = True
  219.  
  220.         If Not Me.CanGoForward Then
  221.             cmdExit.Text = "Fine"
  222.             cmdNext.Enabled = False
  223.         Else
  224.             cmdExit.Text = "Esci"
  225.             cmdNext.Enabled = True
  226.         End If
  227.  
  228.         cmdPrev.Enabled = Me.CanGoBack
  229.     End Sub
  230.  
  231.     Private Sub cmdPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrev.Click
  232.         Me.Controls(Me.PanelName).Visible = False
  233.         CurrentPanel -= 1
  234.         Me.Controls(Me.PanelName).Visible = True
  235.  
  236.         cmdPrev.Enabled = Me.CanGoBack
  237.         cmdNext.Enabled = Me.CanGoForward
  238.     End Sub
  239.  
  240.  
  241.     Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
  242.         If cmdExit.Text = "Esci" Then
  243.             If MessageBox.Show("Sei sicuro di voler uscire?", "Code Converter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
  244.                 Me.Close()
  245.             End If
  246.         Else
  247.             If MessageBox.Show("Vuoi convertire un altro sorgente?", "Code Converter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
  248.                 Me.Controls(Me.PanelName).Visible = False
  249.                 CurrentPanel = 1
  250.                 Me.Controls("pnlStep1").Visible = True
  251.                 cmdNext.Enabled = True
  252.                 cmdPrev.Enabled = False
  253.                 cmdExit.Text = "Esci"
  254.             Else
  255.                 Me.Close()
  256.             End If
  257.         End If
  258.     End Sub
  259.  
  260.     Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
  261.         Dim Open As New OpenFileDialog
  262.         Open.Filter = "Codice sorgente|*.*"
  263.         If Open.ShowDialog = Windows.Forms.DialogResult.OK Then
  264.             txtFile.Text = Open.FileName
  265.         End If
  266.     End Sub
  267.  
  268.     Private Sub rdbFromCode_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbFromCode.CheckedChanged
  269.         txtCode.Enabled = rdbFromCode.Checked
  270.         txtFile.Enabled = Not rdbFromCode.Checked
  271.         cmdBrowse.Enabled = txtFile.Enabled
  272.     End Sub
  273.  
  274.     Private Sub pnlStep6_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pnlStep6.VisibleChanged
  275.         If pnlStep6.Visible Then
  276.             Dim Save As New SaveFileDialog
  277.             Select Case SelectedFormat
  278.                 Case ConvertFormat.Html
  279.                     Save.Filter = "Pagina web compilata|*.html"
  280.                 Case ConvertFormat.HtmlCss
  281.                     Save.Filter = "Pagina web compilata|*.html"
  282.                 Case ConvertFormat.Pdf
  283.                     Save.Filter = "Documento PDF|*.pdf"
  284.                 Case ConvertFormat.Rtf
  285.                     Save.Filter = "Documento RTF|*.rtf"
  286.             End Select
  287.             If Save.ShowDialog = Windows.Forms.DialogResult.OK Then
  288.                 Step6_Convert(Save.FileName)
  289.             End If
  290.         End If
  291.     End Sub
  292. End Class