Imports System.Data
Imports System.Text.RegularExpressions
Imports TallComponents.PDF.Layout
Imports Code_Converter.CodeFormatting
Public Class frmMain
Public Delegate Function ConfirmFunction() As Boolean
Private Delegate Sub Converter(ByVal R As KeywordRegex, ByRef S As String)
Private Enum ConvertFormat
Html
HtmlCss
Rtf
Pdf
End Enum
Private Function Step2_SelectFormat() As Boolean
Dim Done As Boolean = True
If rdbHtml.Checked Then
SelectedFormat = ConvertFormat.Html
ElseIf rdbHtmlCss.Checked Then
SelectedFormat = ConvertFormat.HtmlCss
ElseIf rdbRtf.Checked Then
SelectedFormat = ConvertFormat.Rtf
If MessageBox.Show("Il formato RTF, a volte, può essere letto in modi leggermente differenti a seconda del " & _
"programma utilizzato per la lettura. Microsoft Word e WordPad, ad esempio, differiscono per l'uso degli " & _
"indici nel sistema dei colori. Al fine di non compromettere il documento in modo vistoso, seleziona Sì nel caso " & _
"usassi Microsoft Word, o No per WordPad.", "Code Converter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
OpenWithMSWord = True
Else
OpenWithMSWord = False
End If
ElseIf rdbPdf.Checked Then
SelectedFormat = ConvertFormat.Pdf
Else
ErrorMessage = "Completare tutti i campi richiesti!"
Done = False
End If
Return Done
End Function
Private Function Step3_GetCode() As Boolean
Dim Done As Boolean = True
If rdbFromCode.Checked Then
If txtCode.Text = "" Or txtCode.Text = Nothing Then
ErrorMessage = "Nessun codice inserito!"
Done = False
Else
Source = txtCode.Text
End If
ElseIf rdbFromFile.Checked Then
If txtFile.Text = "" Or txtFile.Text = Nothing Then
Done = False
Else
If IO.
File.
Exists(txtFile.
Text) Then
Source
= IO.
File.
ReadAllText(txtFile.
Text)
Else
ErrorMessage = "File inesistente!"
Done = False
End If
End If
Else
ErrorMessage = "Completare tutti i campi richiesti!"
Done = False
End If
Return Done
End Function
Private Function Step4_SelectLanguage() As Boolean
For Each C As Control In pnlStep4.Controls
If TypeOf C Is RadioButton Then
If DirectCast(C, RadioButton).Checked Then
SelectedLanguage = DirectCast(C, RadioButton).Tag
Return True
End If
End If
Next
ErrorMessage = "Nessun linguaggio selezionato!"
Return False
End Function
Private Function Step5_GetFormat() As Boolean
SourceFont.Font = fcbSource.SelectedFont
SourceFont.Color = fcbSource.SelectedColor
KeyFont.Font = fcbKeyword.SelectedFont
KeyFont.Color = fcbKeyword.SelectedColor
CommentFont.Font = fcbComment.SelectedFont
CommentFont.Color = fcbComment.SelectedColor
Return True
End Function
Private Sub Step6_Convert(ByVal FileName As String)
cmdNext.Enabled = False
cmdPrev.Enabled = False
lblProg.Text = "Conversione in corso..."
Application.DoEvents()
Dim Output As BaseConverter
Select Case SelectedFormat
Case ConvertFormat.Html
Output = New HtmlConverter(SourceFont, CommentFont, KeyFont)
Case ConvertFormat.HtmlCss
Output = New HtmlCssConverter(SourceFont, CommentFont, KeyFont)
Case ConvertFormat.Rtf
Output = New RtfConverter(SourceFont, CommentFont, KeyFont)
CType(Output, RtfConverter).OpenWithMSWord = Me.OpenWithMSWord
Case ConvertFormat.Pdf
Output = New PdfConverter(SourceFont, CommentFont, KeyFont)
End Select
Output.Source = Source
Output.LoadCCL(Languages(SelectedLanguage))
Output.Create(FileName)
Output.Convert(prgProg)
prgProg.Value = 100
Output.Write()
Output.Close()
Application.DoEvents()
lblProg.Text = "Operazione completata"
cmdNext_Click(Me, EventArgs.Empty)
End Sub
Private Sub InitializeFile
(ByVal File As Object,
Optional ByVal FileName
As String = "")
Select Case SelectedFormat
Case ConvertFormat.Pdf
Dim Pdf
As Document
= CType(File, Document
)
With Pdf.DocumentInfo
.Author = "Totem"
.Creator = "Code Converter"
.Subject = "Codice sorgente"
End With
Pdf.Sections.Add(New Section)
End Select
End Sub
Private Function GetConfirmFunction() As ConfirmFunction
Select Case CurrentPanel
Case 1
Return Nothing
Case 2
Return AddressOf Step2_SelectFormat
Case 3
Return AddressOf Step3_GetCode
Case 4
Return AddressOf Step4_SelectLanguage
Case 5
Return AddressOf Step5_GetFormat
End Select
End Function
Private ReadOnly Property PanelName() As String
Get
Return "pnlStep" & CurrentPanel
End Get
End Property
Private ReadOnly Property CanGoForward() As Boolean
Get
Return (CurrentPanel <> PanelCount)
End Get
End Property
Private ReadOnly Property CanGoBack() As Boolean
Get
Return (CurrentPanel > 1)
End Get
End Property
Private CurrentPanel As Byte = 1
Private PanelCount As Byte = 7
Private Languages As New List(Of String)
Private SelectedFormat As ConvertFormat
Private SelectedLanguage As Byte = 0
Private Source, OutputFile As String
Private ErrorMessage As String
Private SourceFont, KeyFont, CommentFont As ColoredFont
Private OpenWithMSWord As Boolean
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Rdb As RadioButton
Dim Y As Int32 = 82
For Each File As String In IO.
Directory.
GetFiles(Application.
StartupPath & "\Languages")
Rdb = New RadioButton
Rdb.Checked = False
Rdb.Location = New Point(20, Y)
Rdb.AutoSize = True
Rdb.
Text = IO.
Path.
GetFileNameWithoutExtension(File)
Rdb.Tag = Languages.Count - 1
Y += 20
pnlStep4.Controls.Add(Rdb)
Next
Dim r As New Regex("\s+(?<key>As)\s+")
Dim s As New Regex("")
'MsgBox(r.Replace("Public PublicVar, VarPublicAs String, E As Int16", "<k>${key}</k>"))
End Sub
Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
Dim Scan As ConfirmFunction = GetConfirmFunction()
If Scan IsNot Nothing Then
If Not Scan() Then
MessageBox.Show(ErrorMessage, "Code Converter", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
End If
Me.Controls(Me.PanelName).Visible = False
CurrentPanel += 1
Me.Controls(Me.PanelName).Visible = True
If Not Me.CanGoForward Then
cmdExit.Text = "Fine"
cmdNext.Enabled = False
Else
cmdExit.Text = "Esci"
cmdNext.Enabled = True
End If
cmdPrev.Enabled = Me.CanGoBack
End Sub
Private Sub cmdPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrev.Click
Me.Controls(Me.PanelName).Visible = False
CurrentPanel -= 1
Me.Controls(Me.PanelName).Visible = True
cmdPrev.Enabled = Me.CanGoBack
cmdNext.Enabled = Me.CanGoForward
End Sub
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
If cmdExit.Text = "Esci" Then
If MessageBox.Show("Sei sicuro di voler uscire?", "Code Converter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
Else
If MessageBox.Show("Vuoi convertire un altro sorgente?", "Code Converter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Controls(Me.PanelName).Visible = False
CurrentPanel = 1
Me.Controls("pnlStep1").Visible = True
cmdNext.Enabled = True
cmdPrev.Enabled = False
cmdExit.Text = "Esci"
Else
Me.Close()
End If
End If
End Sub
Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
Dim Open As New OpenFileDialog
Open.Filter = "Codice sorgente|*.*"
If Open.ShowDialog = Windows.Forms.DialogResult.OK Then
txtFile.Text = Open.FileName
End If
End Sub
Private Sub rdbFromCode_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbFromCode.CheckedChanged
txtCode.Enabled = rdbFromCode.Checked
txtFile.Enabled = Not rdbFromCode.Checked
cmdBrowse.Enabled = txtFile.Enabled
End Sub
Private Sub pnlStep6_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pnlStep6.VisibleChanged
If pnlStep6.Visible Then
Dim Save As New SaveFileDialog
Select Case SelectedFormat
Case ConvertFormat.Html
Save.Filter = "Pagina web compilata|*.html"
Case ConvertFormat.HtmlCss
Save.Filter = "Pagina web compilata|*.html"
Case ConvertFormat.Pdf
Save.Filter = "Documento PDF|*.pdf"
Case ConvertFormat.Rtf
Save.Filter = "Documento RTF|*.rtf"
End Select
If Save.ShowDialog = Windows.Forms.DialogResult.OK Then
Step6_Convert(Save.FileName)
End If
End If
End Sub
End Class