home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / componen / vsocx / demo / vsawk.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-04-04  |  17.7 KB  |  528 lines

  1. VERSION 2.00
  2. Begin Form frmVSAwk 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "VSAwk Demo"
  5.    ClientHeight    =   5325
  6.    ClientLeft      =   975
  7.    ClientTop       =   1560
  8.    ClientWidth     =   8220
  9.    Height          =   6015
  10.    Icon            =   VSAWK.FRX:0000
  11.    Left            =   915
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   5325
  14.    ScaleWidth      =   8220
  15.    Top             =   930
  16.    Width           =   8340
  17.    Begin ListBox lstFonts 
  18.       Height          =   1590
  19.       Left            =   4980
  20.       Sorted          =   -1  'True
  21.       TabIndex        =   3
  22.       Top             =   720
  23.       Width           =   3075
  24.    End
  25.    Begin VideoSoftAwk VSAwk2 
  26.       FS              =   " ,    "
  27.       Left            =   7260
  28.       Top             =   480
  29.    End
  30.    Begin CommandButton btnPrint 
  31.       Caption         =   "&Print"
  32.       Height          =   375
  33.       Left            =   4140
  34.       TabIndex        =   2
  35.       Top             =   1920
  36.       Width           =   735
  37.    End
  38.    Begin CommandButton btnView 
  39.       Caption         =   "&View"
  40.       Height          =   375
  41.       Left            =   3360
  42.       TabIndex        =   1
  43.       Top             =   1920
  44.       Width           =   735
  45.    End
  46.    Begin VideoSoftAwk VSAwk1 
  47.       FS              =   " ,    "
  48.       Left            =   7080
  49.       Top             =   480
  50.    End
  51.    Begin TextBox txtCode 
  52.       FontBold        =   0   'False
  53.       FontItalic      =   0   'False
  54.       FontName        =   "Fixedsys"
  55.       FontSize        =   9
  56.       FontStrikethru  =   0   'False
  57.       FontUnderline   =   0   'False
  58.       Height          =   2715
  59.       Left            =   180
  60.       MultiLine       =   -1  'True
  61.       ScrollBars      =   3  'Both
  62.       TabIndex        =   4
  63.       Top             =   2460
  64.       Width           =   7815
  65.    End
  66.    Begin ListBox lstFileParts 
  67.       Height          =   1590
  68.       Left            =   180
  69.       Sorted          =   -1  'True
  70.       TabIndex        =   0
  71.       Top             =   720
  72.       Width           =   3075
  73.    End
  74.    Begin CommonDialog CMDialog1 
  75.       Left            =   7500
  76.       Top             =   420
  77.    End
  78.    Begin Label lblInfo 
  79.       Alignment       =   2  'Center
  80.       BackStyle       =   0  'Transparent
  81.       Caption         =   "Open a .FRM or .BAS file, then select subroutines to view or print."
  82.       Height          =   1035
  83.       Left            =   3360
  84.       TabIndex        =   9
  85.       Top             =   720
  86.       Width           =   1515
  87.    End
  88.    Begin Label lblFonts 
  89.       Alignment       =   2  'Center
  90.       BackStyle       =   0  'Transparent
  91.       Caption         =   "Printer Fonts"
  92.       ForeColor       =   &H00000000&
  93.       Height          =   195
  94.       Left            =   4980
  95.       TabIndex        =   8
  96.       Top             =   480
  97.       Width           =   3075
  98.    End
  99.    Begin Label lblContents 
  100.       Alignment       =   2  'Center
  101.       BackStyle       =   0  'Transparent
  102.       Caption         =   "Contents"
  103.       ForeColor       =   &H00000000&
  104.       Height          =   195
  105.       Left            =   180
  106.       TabIndex        =   7
  107.       Top             =   480
  108.       Width           =   3075
  109.    End
  110.    Begin Label lblFileName 
  111.       BackColor       =   &H00C0C0C0&
  112.       Caption         =   "[none]"
  113.       ForeColor       =   &H00000080&
  114.       Height          =   225
  115.       Left            =   720
  116.       TabIndex        =   6
  117.       Top             =   120
  118.       Width           =   7275
  119.    End
  120.    Begin Label Label1 
  121.       Alignment       =   1  'Right Justify
  122.       BackColor       =   &H00C0C0C0&
  123.       Caption         =   "File:"
  124.       ForeColor       =   &H00000000&
  125.       Height          =   195
  126.       Left            =   120
  127.       TabIndex        =   5
  128.       Top             =   120
  129.       Width           =   495
  130.    End
  131.    Begin Menu mnuFile 
  132.       Caption         =   "&File"
  133.       Begin Menu mnuFileOpen 
  134.          Caption         =   "&Open"
  135.       End
  136.       Begin Menu mnuFileSep1 
  137.          Caption         =   "-"
  138.       End
  139.       Begin Menu mnuFileExit 
  140.          Caption         =   "E&xit"
  141.       End
  142.    End
  143.    Begin Menu mnuEdit 
  144.       Caption         =   "&Edit"
  145.       Begin Menu mnuCopy 
  146.          Caption         =   "&Copy"
  147.       End
  148.       Begin Menu mnuCopyAll 
  149.          Caption         =   "Copy &All"
  150.       End
  151.    End
  152.    Begin Menu mnuHelp 
  153.       Caption         =   "&Help"
  154.       Begin Menu mnuHelpAbout 
  155.          Caption         =   "&About"
  156.       End
  157.    End
  158. Option Explicit
  159. '--------------------------------------------------
  160. ' Constants and Variables used in VSAwk demo.
  161. '--------------------------------------------------
  162. ' Color Constants
  163. Const DARK_GRAY = &H808080
  164. Const WHITE = &HFFFFFF
  165. Const BLACK = &H0
  166. ' Awk Action constants
  167. Const AWK_SCANFILE = 0
  168. Const AWK_NEXTLINE = 1
  169. Const AWK_CLOSEFILE = 2
  170. ' Holds the section of basic code
  171. Dim CodeString As String
  172. ' Boolean used while looking for [declarations] section
  173. Dim FoundDecl As Integer
  174. ' Are we printing or just viewing?
  175. Dim ActionType As Integer
  176. ' Used to indicate declarations section of code.
  177. Const DECLARE_STRING = "(declarations)"
  178. ' WindowState constant
  179. Const MINIMIZED = 1
  180. ' Cursor shape constants
  181. Const CSR_NORMAL = 0
  182. Const CSR_HOURGLASS = 11
  183. ' ActionType constants
  184. Const GO_VIEW = 0
  185. Const GO_PRINT = 1
  186. ' Default margin constants
  187. Const LR_MARGIN = 5
  188. Const TOP_MARGIN = 3
  189. Const MB_OK = 0                 ' OK button only
  190. Const MB_ICONEXCLAMATION = 48   ' Warning message
  191. Sub btnPrint_Click ()
  192. '--------------------------------------------------
  193. ' Print the routine selected in the list box.
  194. '--------------------------------------------------
  195.     If lstFileParts.ListIndex < 0 Then Exit Sub
  196.     Screen.MousePointer = CSR_HOURGLASS
  197.     VSAwk2.FileName = CMDialog1.Filename
  198.     ActionType = GO_PRINT
  199.     VSAwk2.Action = AWK_SCANFILE
  200.     Screen.MousePointer = CSR_NORMAL
  201. End Sub
  202. Sub btnView_Click ()
  203. '--------------------------------------------------
  204. ' View the routine selected in the list box.
  205. '--------------------------------------------------
  206.     If lstFileParts.ListIndex < 0 Then Exit Sub
  207.     Screen.MousePointer = CSR_HOURGLASS
  208.     VSAwk2.FileName = CMDialog1.Filename
  209.     ActionType = GO_VIEW
  210.     VSAwk2.Action = AWK_SCANFILE
  211.     Screen.MousePointer = CSR_NORMAL
  212. End Sub
  213. Sub ClearForm ()
  214. '--------------------------------------------------
  215. ' Get rid of any old data in the controls.
  216. '--------------------------------------------------
  217.      lstFileParts.Clear
  218.      TxtCode.Text = ""
  219. End Sub
  220. Sub Form_Load ()
  221. '--------------------------------------------------
  222. ' Position the form and load Printer Font list box.
  223. '--------------------------------------------------
  224. Dim i As Integer
  225.     mnuHelp.Caption = Chr$(8) & mnuHelp.Caption & "  "
  226.     For i = 0 To Printer.FontCount - 1
  227.         lstFonts.AddItem Printer.Fonts(i)
  228.     Next
  229.     For i = 0 To lstFonts.ListCount - 1
  230.         If i = 0 Then lstFonts.ListIndex = i
  231.         If lstFonts.List(i) = "Courier New" Then lstFonts.ListIndex = i
  232.     Next
  233.     Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
  234. End Sub
  235. Sub Form_Paint ()
  236. '--------------------------------------------------
  237. ' Repaint 3D effect where necessary.
  238. '--------------------------------------------------
  239.     Set3DControls
  240. End Sub
  241. Sub Form_Resize ()
  242. '--------------------------------------------------
  243. ' Adjust the size of the text control and the
  244. ' File Name label when window size changes.
  245. '--------------------------------------------------
  246. Dim NewWidth As Integer, NewHeight As Integer
  247. Dim Margin As Integer
  248.     On Error Resume Next
  249.     If WindowState = MINIMIZED Then Exit Sub
  250.     Me.Cls
  251.     Margin = lstFileParts.Left
  252.     NewWidth = (Me.ScaleWidth - TxtCode.Left - Margin)
  253.     NewHeight = (Me.ScaleHeight - TxtCode.Top - Margin)
  254.     TxtCode.Move TxtCode.Left, TxtCode.Top, NewWidth, NewHeight
  255.     lblFilename.Width = Me.ScaleWidth - lblFilename.Left - Margin
  256.     ' Draw 3D effect around selected controls
  257.     Set3DControls
  258. End Sub
  259. Function IsTextFile (ByVal AFileName As String) As Integer
  260. '--------------------------------------------------
  261. ' A quick little check to see if this is a text
  262. ' file or not.  Not 100% accurate, but better
  263. ' than nothing.
  264. '--------------------------------------------------
  265. Dim fnum As Integer
  266. Dim i As Integer
  267. Dim ch As String
  268.     On Error GoTo IsTextFile_Error
  269.     IsTextFile = True
  270.     fnum = FreeFile
  271.     Open AFileName For Input As fnum
  272.     For i = 1 To 25
  273.         ch = Input$(1, fnum)
  274.         ' if it's not lower ASCII then its probably
  275.         ' not a text file.
  276.         If Asc(ch) > 127 Then
  277.             IsTextFile = False
  278.             Exit For
  279.         End If
  280.         If EOF(fnum) Then Exit For
  281.     Next
  282. Exit Function
  283. IsTextFile_Error:
  284.     IsTextFile = False
  285.     Exit Function
  286. End Function
  287. Sub lstFileParts_DblClick ()
  288. '--------------------------------------------------
  289. ' Double-clicking on list box is the same as
  290. ' pressing the View button.
  291. '--------------------------------------------------
  292.     btnView.Value = 1
  293. End Sub
  294. Sub Make3D (pic As Form, ctl As Control)
  295. '--------------------------------------------------
  296. ' Wrap a 3D effect around a control on a form.
  297. '--------------------------------------------------
  298. Dim AdjustX As Integer, AdjustY As Integer
  299. Dim RightSide As Single
  300.     AdjustX = Screen.TwipsPerPixelX
  301.     AdjustY = Screen.TwipsPerPixelY
  302.     ' Set the top shading line.
  303.     pic.Line (ctl.Left - AdjustX, ctl.Top - AdjustY)-(ctl.Left + ctl.Width, ctl.Top - AdjustY), DARK_GRAY
  304.     pic.Line -(ctl.Left + ctl.Width, ctl.Top + ctl.Height), WHITE
  305.     pic.Line -(ctl.Left - AdjustX, ctl.Top + ctl.Height), WHITE
  306.     pic.Line -(ctl.Left - AdjustX, ctl.Top - AdjustY), DARK_GRAY
  307. End Sub
  308. Sub mnuCopy_Click ()
  309.     If TxtCode.SelText <> "" Then
  310.         ClipBoard.SetText TxtCode.SelText
  311.         TxtCode.SetFocus
  312.     End If
  313. End Sub
  314. Sub mnuCopyAll_Click ()
  315. Dim SavePos As Integer
  316.     SavePos = TxtCode.SelStart
  317.     TxtCode.SelStart = 0
  318.     TxtCode.SelLength = Len(TxtCode.Text)
  319.     If TxtCode.SelText <> "" Then
  320.         ClipBoard.SetText TxtCode.SelText
  321.     End If
  322.     TxtCode.SelStart = SavePos
  323.     TxtCode.SelLength = 0
  324.     TxtCode.SetFocus
  325. End Sub
  326. Sub mnuFileExit_Click ()
  327. '--------------------------------------------------
  328. ' End the program.
  329. '--------------------------------------------------
  330.     Unload Me
  331. End Sub
  332. Sub mnuFileOpen_Click ()
  333. '--------------------------------------------------
  334. ' Open a Visual Basic source code file.
  335. '--------------------------------------------------
  336.     CMDialog1.DialogTitle = "Open a Visual Basic Source File"
  337.     CMDialog1.Filter = "VB Source Code (.FRM,.BAS)|*.frm;*.bas"
  338.     CMDialog1.Action = 1
  339.     If CMDialog1.Filename = "" Then Exit Sub
  340.     If Not IsTextFile(CMDialog1.Filename) Then
  341.         MsgBox "Source files must be saved as text.", MB_OK Or MB_ICONEXCLAMATION
  342.         Exit Sub
  343.     End If
  344.     ClearForm
  345.     lblFilename = CMDialog1.Filename
  346.     VSAwk1.FileName = CMDialog1.Filename
  347.     Screen.MousePointer = CSR_HOURGLASS
  348.     VSAwk1.Action = AWK_SCANFILE
  349.     Screen.MousePointer = CSR_NORMAL
  350. End Sub
  351. Sub mnuHelpAbout_Click ()
  352.     frmAbout.Show 1
  353. End Sub
  354. Sub PrintHeading ()
  355. '--------------------------------------------------
  356. ' Print heading at top of a printer page.
  357. '--------------------------------------------------
  358. Dim i As Integer
  359.     On Error Resume Next
  360.     Printer.CurrentY = 0
  361.     Printer.CurrentX = 0
  362.     For i = 1 To TOP_MARGIN
  363.         Printer.Print
  364.     Next
  365.     Printer.FontBold = True
  366.     Printer.Print Space$(LR_MARGIN) & "File:    "; CMDialog1.Filename;
  367.     PrintRightAlign "Page " & Format$(Printer.Page)
  368.     Printer.Print
  369.     Printer.Print Space$(LR_MARGIN) & "Printed: " & Format$(Now, "MM/DD/YYYY")
  370.     Printer.CurrentX = Printer.TextWidth(Space$(LR_MARGIN))
  371.     Printer.CurrentY = Printer.CurrentY + (Printer.TextHeight("W") \ 2)
  372.     Printer.DrawWidth = 3
  373.     Printer.Line -(Printer.ScaleWidth - Printer.TextWidth(Space$(LR_MARGIN)), Printer.CurrentY), RGB(0, 0, 0)
  374.     Printer.CurrentY = Printer.CurrentY + (Printer.TextHeight("W") \ 2)
  375.     Printer.CurrentX = 0
  376.     Printer.FontBold = False
  377. End Sub
  378. Sub PrintLine (ByVal ALine As String)
  379. '--------------------------------------------------
  380. ' Print a single line to the printer.  Break up
  381. ' long lines and pass them to PrintLine recursively.
  382. '--------------------------------------------------
  383. Dim indent As Integer
  384. Dim i As Integer, j As Integer
  385. Dim LeftMargin As String
  386. Dim ATab As String
  387.     On Error Resume Next
  388.     ATab = Chr$(9)
  389.     ALine = RTrim$(ALine)
  390.     indent = 0
  391.     For i = 1 To Len(ALine)
  392.         If Mid$(ALine, i, 1) = " " Then
  393.             indent = indent + 1
  394.         ' This converts tabs to spaces
  395.         ElseIf Mid$(ALine, i, 1) = ATab Then
  396.             indent = indent + 8
  397.         Else
  398.             Exit For
  399.         End If
  400.     Next
  401.     ALine = Space$(indent) & Mid$(ALine, i)
  402.     LeftMargin = Space$(LR_MARGIN)
  403.     ' Check if we're at the end of the page.
  404.     If (Printer.CurrentY + Printer.TextHeight(ALine)) >= Printer.ScaleHeight Then
  405.         Printer.NewPage
  406.         PrintHeading
  407.     End If
  408.     ' Check if we can fit Aline on a single line.
  409.     If Printer.TextWidth(LeftMargin & ALine & LeftMargin) <= Printer.ScaleWidth Then
  410.         If Left$(Trim$(ALine), 1) = "'" Then Printer.FontItalic = True
  411.         Printer.Print LeftMargin & ALine
  412.         Printer.FontItalic = False
  413.     Else
  414.         For i = 1 To Len(ALine)
  415.             If Printer.TextWidth(LeftMargin & Left$(ALine, i) & LeftMargin) > Printer.ScaleWidth Then
  416.                 Exit For
  417.             End If
  418.         Next
  419.         ' Try to adjust for a word break nearby.
  420.         For j = i To (j - 12) Step -1
  421.             If InStr(" :()", Mid$(ALine, j, 1)) > 0 Then
  422.                 i = j + 1
  423.                 Exit For
  424.             End If
  425.         Next
  426.         Printer.Print LeftMargin & Left$(ALine, i - 1)
  427.         PrintLine Space$(indent) & ">> " & Mid$(ALine, i)
  428.     End If
  429. End Sub
  430. Sub PrintRightAlign (ByVal Astr As String)
  431. '--------------------------------------------------
  432. ' Print a string a the far right of the page.
  433. '--------------------------------------------------
  434.     On Error Resume Next
  435.     Printer.CurrentX = Printer.ScaleWidth - Printer.TextWidth(Astr) - Printer.TextWidth(Space$(LR_MARGIN))
  436.     Printer.Print Astr;
  437. End Sub
  438. Sub Set3DControls ()
  439. '--------------------------------------------------
  440. ' Draw 3D effect around selected controls.
  441. '--------------------------------------------------
  442.     Make3D frmVSAwk, lstFileParts
  443.     Make3D frmVSAwk, TxtCode
  444.     Make3D frmVSAwk, lblFilename
  445.     Make3D frmVSAwk, lblContents
  446.     Make3D frmVSAwk, lblFonts
  447.     Make3D frmVSAwk, lstFonts
  448.     Make3D frmVSAwk, lblInfo
  449. End Sub
  450. Sub VSAwk1_Begin ()
  451. '--------------------------------------------------
  452. ' Check whether we need to skip the "form definition"
  453. ' stuff at the beginning of a .FRM file.
  454. '--------------------------------------------------
  455.     If Right$(CMDialog1.Filename, 4) <> ".FRM" Then
  456.         lstFileParts.AddItem DECLARE_STRING
  457.         lstFileParts.ItemData(lstFileParts.NewIndex) = VSAwk1.CurrPos
  458.     Else
  459.         FoundDecl = False
  460.     End If
  461. End Sub
  462. Sub VSAwk1_Scan ()
  463. '--------------------------------------------------
  464. ' Build the list box with sub, function and
  465. ' declaration names.
  466. '--------------------------------------------------
  467.     If VSAwk1.NF = 0 Then Exit Sub
  468.     If (Right$(CMDialog1.Filename, 4) = ".FRM") And (Not FoundDecl) And (VSAwk1.L = "End") Then
  469.         FoundDecl = True
  470.         lstFileParts.AddItem DECLARE_STRING
  471.         lstFileParts.ItemData(lstFileParts.NewIndex) = VSAwk1.CurrPos + Len(VSAwk1.L) + 2
  472.     End If
  473.     If (VSAwk1.F(1) = "Sub") Or (VSAwk1.F(1) = "Function") Then
  474.         lstFileParts.AddItem Trim$(VSAwk1.F(2))
  475.         lstFileParts.ItemData(lstFileParts.NewIndex) = VSAwk1.CurrPos
  476.     End If
  477. End Sub
  478. Sub VSAwk2_Begin ()
  479. '--------------------------------------------------
  480. ' Prepare to view (and optionally print) a sub,
  481. ' function or declaration section.
  482. '--------------------------------------------------
  483.     VSAwk2.CurrPos = lstFileParts.ItemData(lstFileParts.ListIndex)
  484.     CodeString = ""
  485.     ' Define printer font and print first page header.
  486.     If ActionType = GO_PRINT Then
  487.         If lstFonts.ListIndex >= 0 Then
  488.             Printer.FontName = lstFonts.List(lstFonts.ListIndex)
  489.         End If
  490.         Printer.FontSize = 8.25
  491.         Printer.FontBold = False
  492.         Printer.FontItalic = False
  493.         Printer.FontUnderline = False
  494.         PrintHeading
  495.     End If
  496. End Sub
  497. Sub VSAwk2_End ()
  498. '--------------------------------------------------
  499. ' Load VB code into text control and, if necessary,
  500. ' end the print job.
  501. '--------------------------------------------------
  502.     TxtCode = CodeString
  503.     If ActionType = GO_PRINT Then Printer.EndDoc
  504. End Sub
  505. Sub VSAwk2_Scan ()
  506. '--------------------------------------------------
  507. ' Process a source code line.
  508. '--------------------------------------------------
  509. Dim ShutDown As Integer
  510.     ' Parsing [Declarations] section
  511.     If lstFileParts.List(lstFileParts.ListIndex) = DECLARE_STRING Then
  512.         If ((VSAwk2.F(1) = "Sub") Or (VSAwk2.F(1) = "Function")) Then
  513.             VSAwk2.Action = AWK_CLOSEFILE
  514.             Exit Sub
  515.         End If
  516.     ' Parsing sub or function
  517.     Else
  518.         If ((VSAwk2.L = "End Sub") Or (VSAwk2.L = "End Function")) Then
  519.             ShutDown = True
  520.         End If
  521.     End If
  522.     CodeString = CodeString & VSAwk2.L & Chr$(13) & Chr$(10)
  523.     If ActionType = GO_PRINT Then
  524.         PrintLine VSAwk2.L
  525.     End If
  526.     If ShutDown Then VSAwk2.Action = AWK_CLOSEFILE
  527. End Sub
  528.