Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace VBSCAN
   Public Class frmVbScan : Inherits System.Windows.Forms.Form
      Private Dim _disposed As Boolean = False
      <System.Diagnostics.DebuggerNonUserCode()> _
      Public Sub New()
         MyBase.New()
         'This call is required by the Windows Form Designer.
         InitializeComponent()
         'Add any initialization after the InitializeComponent() call
         If m_vb6FormDefInstance Is Nothing Then
            If m_InitializingDefInstance Then
               m_vb6FormDefInstance = Me
            Else
               Try
                  'For the start-up form, the first instance created is the default instance.
                  If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
                     m_vb6FormDefInstance = Me
                  End If
               Catch
               End Try
            End If
         End If
         If (DesignMode) Then Exit Sub
         optDtlView = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Forms.RadioButton)
         optDtlView.Add(1, _optDtlView_1)
         optDtlView.Add(0, _optDtlView_0)
         Form_Load(Nothing, Nothing)
         Form_Resize(Nothing, Nothing)

      End Sub
      'Form overrides dispose to clean up the component list.
      <System.Diagnostics.DebuggerNonUserCode()> _
      Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
         If _disposed Then
            Return
         End If
         If Disposing Then
            If Not components Is Nothing Then
               components.Dispose()
            End If
         End If
         _disposed = True
         MyBase.Dispose(Disposing)
      End Sub
      Private Sub chkICase_Click(ByVal sender As Object, ByVal e As EventArgs)
         modVBSCAN.giCase = chkICase.CheckState
      End Sub
      Private Sub chkiCmnts_Click(ByVal sender As Object, ByVal e As EventArgs)
         modVBSCAN.giCmnts = chkiCmnts.CheckState
      End Sub
      Private Sub cmdGO_Click(ByVal sender As Object, ByVal e As EventArgs)
         Dim rc As Integer = 0

         rc = SetSrch()
         If (rc = 0) Then
            Interaction.Beep()
            System.Windows.Forms.MessageBox.Show("Please enter search strings.", "", MessageBoxButtons.OK)
            txtSrch.Focus()
            Exit Sub
         End If

         If (modVBSCAN.gsMAKName = "") Then
            Interaction.Beep()
            mnuOpen_Click(Nothing,New EventArgs())
            If (modVBSCAN.gsMAKName = "") Then
               Exit Sub
            End If
         End If

         modVBSCAN.gNumHits = 0
         modVBSCAN.giStopScan = 0
         OL1.Focus()
         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
         cmdGo.Enabled = False

         rc = modVBSCAN.rdPrjFile(modVBSCAN.gsMAKName)

         cmdGo.Enabled = True
         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default

         If (modVBSCAN.giStopScan = 1) Then
            System.Windows.Forms.MessageBox.Show("Scan Aborted by User", "", MessageBoxButtons.OK)
            Err.Raise(17,"Scan Aborted by User",String.Empty)
         Else
            If (rc = 0) Then
               OL1.AddItem("[Scan completed successfully. " & modVBSCAN.gNumHits & " hits.]")
               OL1.Indent(OL1.ListCount - 1) = 1
               OL1.ItemData(OL1.ListCount - 1) = -1
            Else
               Err.Raise(17,"Scan failed. Some modules could not be read.",String.Empty)
            End If
         End If
      End Sub
      Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)
         modVBSCAN.giCase = chkICase.CheckState

         modVBSCAN.giCmnts = chkiCmnts.CheckState

         If (_optDtlView_0.Checked) Then
            modVBSCAN.gsDtlView = _optDtlView_0.Text
         Else
            modVBSCAN.gsDtlView = _optDtlView_1.Text
         End If

      End Sub
      Private Sub Form_Resize(ByVal sender As Object, ByVal e As EventArgs)

         On Error Resume Next
         OL1.Width = Me.ClientRectangle.Width - OL1.Left - 10
         OL1.Height = Me.ClientRectangle.Height - OL1.Top - 10

      End Sub
      Private Sub mnuDoc_Click(ByVal sender As Object, ByVal e As EventArgs)
         On Error Goto ErrorHandler

         txtSrch.Text = modVBSCAN.gsDocComment
         chkiCmnts.CheckState = System.Windows.Forms.CheckState.Unchecked
         chkICase.CheckState = System.Windows.Forms.CheckState.Checked

         If (OL1.ListCount < 1) Then
            cmdGO_Click(cmdGo,New EventArgs())
         End If

         If (modVBSCAN.gsDocFile = "") Then
            Interaction.Beep()
            mnuDocOpt_Click(Nothing,New EventArgs())
            If (modVBSCAN.gsDocFile = "") Then
               Exit Sub
            End If
         End If

         Dim sFile As String = ""
         sFile = modVBSCAN.gsDocFile
         modVBSCAN.MakeHtm(sFile)
         System.Windows.Forms.MessageBox.Show("HTML saved in " & sFile, "", MessageBoxButtons.OK)


      ErrorHandler:
         If (Err.Number <> 0) Then
            System.Windows.Forms.MessageBox.Show(Err.Description, "", MessageBoxButtons.OK)
         End If
      End Sub
      Private Sub mnuDocOpt_Click(ByVal sender As Object, ByVal e As EventArgs)
         frmOptions.DefInstance.ShowDialog()
      End Sub
      Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As EventArgs)
         Environment.Exit(0)
      End Sub
      Private Sub mnuHelp_Click(ByVal sender As Object, ByVal e As EventArgs)
         Dim sMsg As String = ""

         sMsg = ""
         sMsg = sMsg & "Open a VB project file (*.mak); enter the pattern(s) to search for; and click 'scan' to start search (ESC to abort)." & Strings.Chr(10) & Strings.Chr(10)
         sMsg = sMsg & "The modules containing the pattern will be flagged with a plus sign. Click on a plus to see more detail" & Strings.Chr(10) & Strings.Chr(10)
         sMsg = sMsg & "Double click on a function or subroutine to inspect its code."

         System.Windows.Forms.MessageBox.Show(sMsg, "", MessageBoxButtons.OK)
      End Sub
      Private Sub mnuOpen_Click(ByVal sender As Object, ByVal e As EventArgs)

         CMD1.Filter = "Project Files|*.MAK;*.VBP"
         CMD1.DialogTitle = "Open VB Project File"
         CMD1.FilterIndex = 1
         CMD1.FileName = ""
         CMD1.CancelError = False
         CMD1.ShowOpen()
         If (CMD1.FileName <> "") Then
            modVBSCAN.gsMAKName = CMD1.FileName
            frmVbScan.DefInstance.Text = "VB VBScan: " & modVBSCAN.gsMAKName
            modVBSCAN.gsMAKPath = Strings.Left(modVBSCAN.gsMAKName,modVBSCAN.RInStr(modVBSCAN.gsMAKName,"\"))
         End If
      End Sub
      Private Sub mnuSave_Click(ByVal sender As Object, ByVal e As EventArgs)
         Dim sFile As String = ""
         sFile = "c:\temp\vbscan.txt"

         If (modVBSCAN.SaveCSR(sFile) <> 0) Then
            System.Windows.Forms.MessageBox.Show("Error saving VBScan Report", "", MessageBoxButtons.OK)
         Else
            Interaction.Shell("notepad " & sFile,AppWinStyle.NormalFocus)
         End If

      End Sub
      Private Sub OL1_Click(ByVal sender As Object, ByVal e As EventArgs)
         Debug.WriteLine("Click ListIndex=" & OL1.ListIndex & "  -- " & OL1.FullPath(OL1.ListIndex))
      End Sub
      Private Sub OL1_DblClick(ByVal sender As Object, ByVal e As EventArgs)
         Dim rc As Integer = 0
         Dim i As Integer = 0
         i = OL1.ListIndex
         Dim O As Integer = 0
         Dim l As Integer = 0
         Dim sf As String = ""
         sf = Strings.Left(OL1.List(i),3)
         Dim sMod As String = ""
         Dim sRtn As String = ""


         If (OL1.ItemData(i) = -1) Then
            Exit Sub
         End If

         sf = ""
         For O = i To 0 Step -1
            If (OL1.ItemData(O) > 0) Then
               sf = modVBSCAN.gsModPath(OL1.ItemData(O)) & "\" & OL1.List(i)
               Exit For
            End If
         Next


         If (OL1.Indent(i) = 1) Then
            sRtn = "General"
         ElseIf (OL1.Indent(i) = 2) Then
            sRtn = OL1.List(i)
         Else
            For O = i To 0 Step -1
               If (OL1.Indent(O) = 2) Then
                  sRtn = OL1.List(O)
                  Exit For
               End If
            Next
         End If

         l = Strings.Len(sf)
         i = modVBSCAN.RInStr(sf,"\")
         If (i > 0) Then
            sMod = Strings.Left(sf,i - 1)
         End If

         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

         If (sRtn = "General") Then
            modVBSCAN.rdGeneral(sMod)
         Else
            modVBSCAN.rdRoutine(sMod,sRtn)
         End If

         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
         If (rc = 0) Then
            l = Strings.Len(sMod)
            i = modVBSCAN.RInStr(sMod,"\")
            If (i > 0) Then
               sMod = Strings.Right(sMod,l - i)
            End If
            If (modVBSCAN.gsDtlView = "List") Then
               frmRtnLst.DefInstance.Text = "VB VBScan: " & sMod & "\" & sRtn
               frmRtnLst.DefInstance.Show()
            Else
               frmRtnOL.DefInstance.Text = "VB VBScan: " & sMod & "\" & sRtn
               frmRtnOL.DefInstance.Show()
            End If

         Else
            System.Windows.Forms.MessageBox.Show("Unable to Load " & sf, "", MessageBoxButtons.OK)
         End If

      End Sub
      Private Sub OL1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
         Dim keyCode As Integer
         keyCode = e.KeyValue
         If (keyCode = 27) Then
            modVBSCAN.giStopScan = 1
         End If
      End Sub
      Private Function SetSrch() As Integer
         SetSrch = 0
         On Error Goto setsrch_err
         SetSrch = 0
         Dim l As Integer = 0
         Dim i As Integer = 0
         Dim sTxt As String = ""
         Dim sbuf As String = ""
         Dim c As String = ""
         Dim wc As Integer = 0

         sTxt = Strings.Trim(txtSrch.Text)
         l = Strings.Len(sTxt)
         If (l = 0) Then
            Exit Function
         End If

         For i = 1 To l + 1 Step 1
            c = Mid(sTxt,i,1)
            If (c <> Strings.Chr(13) And i <= l) Then
               sbuf = sbuf & c
            Else
               If (Strings.Len(sbuf) > 0 And Strings.Left(sbuf,1) <> Strings.Chr(13)) Then
                  i = i + 1
                  wc = wc + 1
                  Redim Preserve modVBSCAN.gsSrch(wc)
                  modVBSCAN.gsSrch(wc) = sbuf
                  sbuf = ""
               End If
            End If
         Next

         SetSrch = wc
         Exit Function

      setsrch_err:
         System.Windows.Forms.MessageBox.Show("Error [SetSrch] " & Err.Description & " #" & Err.Number, "", MessageBoxButtons.OK)
      End Function
      Private Sub optDtlView_Click(ByVal sender As Object, ByVal e As EventArgs)
         Dim optDtlView As System.Windows.Forms.RadioButton
         optDtlView = CType(sender,System.Windows.Forms.RadioButton)
         Dim Index As Integer = Me.optDtlView.Keys(Me.optDtlView.IndexOfValue(optDtlView))
         If Not CType(sender, RadioButton).Checked Then Return
         modVBSCAN.gsDtlView = optDtlView.Text
      End Sub
   End Class
End Namespace
