home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD39053102000.psc / ColorCode.bas next >
Encoding:
BASIC Source File  |  2000-03-10  |  16.9 KB  |  515 lines

  1. Attribute VB_Name = "ColorCode"
  2. '    --------------------------------------------------------------------------
  3. '    EzColorTest HTML Editor Color Coding Test
  4. '    Copyright (C) 2000  Eric Banker
  5. '
  6. '    This program is free software; you can redistribute it and/or modify
  7. '    it under the terms of the GNU General Public License as published by
  8. '    the Free Software Foundation; either version 2 of the License, or
  9. '    (at your option) any later version.
  10. '
  11. '    This program is distributed in the hope that it will be useful,
  12. '    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. '    GNU General Public License for more details.
  15. '
  16. '    You should have received a copy of the GNU General Public License
  17. '    along with this program; if not, write to the Free Software
  18. '    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. '    --------------------------------------------------------------------------
  20.  
  21. Option Explicit
  22.  
  23. ' These hold the color information
  24. Public m_TextCol As String
  25. Public m_AttribCol As String
  26. Public m_TagCol As String
  27. Public m_CommentCol As String
  28. Public m_AspCol As String
  29.  
  30. ' ##########################################################################################
  31. ' These below are the color coding functions. These handle all color coding for the program.
  32. ' ##########################################################################################
  33.  
  34. ' Call this when you load a form with code in it. It color codes the entire document
  35.  
  36. Public Sub HtmlHighlight()
  37. On Error Resume Next
  38.     frmMain.trapUndo = False
  39.     ' Color Html and asp
  40.     HtmlColorCode
  41.     
  42.     ' Move back to the start of the thing
  43.     frmMain.RichTxtBox.SelStart = 0
  44.     frmMain.trapUndo = True
  45. End Sub
  46.  
  47. ' Colorizes HTML while typing
  48. ' --------------------------------------------------------------
  49.  
  50. Public Function KeyPressEvent(KeyAscii As Integer) As Integer
  51.     Static cInAttrib As Boolean, cInTag As Boolean
  52.     Static cInAttribQuote As Boolean, cTypedIn As Boolean
  53.     Static cInComment As Boolean
  54.     Static cInASP As Boolean
  55.     Static cInFunction As Boolean
  56.     
  57.     frmMain.trapUndo = False
  58.     
  59.     Dim cChar As String
  60.  
  61.     With frmMain.RichTxtBox
  62.         cChar = Chr$(KeyAscii)
  63.         
  64.         If cInTag = False And cInAttrib = False And cInComment = False And cInASP = False Then
  65.             .SelColor = m_TextCol
  66.         End If
  67.  
  68.         If cInTag = True And (cInAttrib = True Or cInAttribQuote = True) Then
  69.             .SelColor = m_AttribCol
  70.         End If
  71.  
  72.         If cChar = "<" Then
  73.             .SelColor = m_TagCol
  74.             cInTag = True
  75.             cTypedIn = True
  76.         End If
  77.  
  78.         If cChar = "=" And cInTag = True Then
  79.             cInAttrib = True
  80.         End If
  81.  
  82.         If cChar = Chr$(34) And cInAttrib = True And cInAttribQuote = True Then
  83.             cInAttrib = False
  84.             cInAttribQuote = False
  85.         ElseIf cChar = Chr$(34) And cInAttrib = True And cInAttribQuote = False Then
  86.             cInAttribQuote = True
  87.         End If
  88.  
  89.         If cChar = " " And (cInAttribQuote = False And cInTag = True) Then
  90.             .SelColor = m_TagCol
  91.             cInAttrib = False
  92.         End If
  93.  
  94.         If cChar = "!" And Mid$(.Text, .SelStart, 1) = "<" Then
  95.  
  96.             .SelStart = .SelStart - 1
  97.             .SelLength = 1
  98.             .SelColor = m_CommentCol
  99.             .SelText = "<!--"
  100.  
  101.             cInTag = False
  102.             cInAttrib = False
  103.             cInASP = False
  104.             cInComment = True
  105.  
  106.             KeyAscii = 0
  107.         End If
  108.         
  109.         If cChar = "%" And Mid$(.Text, .SelStart, 1) = "<" Then
  110.  
  111.             .SelStart = .SelStart - 1
  112.             .SelLength = 1
  113.             .SelColor = m_AspCol
  114.             .SelText = "<%"
  115.  
  116.             cInTag = False
  117.             cInAttrib = False
  118.             cInASP = True
  119.             cInComment = False
  120.  
  121.             KeyAscii = 0
  122.         End If
  123.  
  124.         If cChar = ">" Then
  125.             If cInComment = False And cInASP = True Then
  126.                 .SelColor = m_AspCol
  127.             ElseIf cInComment = True And cInASP = False Then
  128.                 .SelColor = m_CommentCol
  129.             ElseIf cInComment = False And cInASP = False Then
  130.                 .SelColor = m_TagCol
  131.             End If
  132.             
  133.             cInTag = False
  134.             cInASP = False
  135.             cInComment = False
  136.             cTypedIn = False
  137.         End If
  138.  
  139.     End With
  140.  
  141.     KeyPressEvent = KeyAscii
  142.     
  143.     frmMain.trapUndo = True
  144. ErrExit:
  145.     Exit Function
  146. End Function
  147.  
  148. ' Insert text w/tag coloring if necessary
  149.  
  150. Public Sub InsertTag(Tag$, StopAsp As Boolean)
  151. Dim S As Long
  152.     'frmMain.trapUndo = False
  153.     S = frmMain.RichTxtBox.SelStart
  154.     If Len(frmMain.RichTxtBox.SelText) > 0 Then frmMain.RichTxtBox.SelText = ""
  155.     frmMain.RichTxtBox.SelText = Tag$
  156.     
  157.     If StopAsp = True Then
  158.         frmMain.trapUndo = False
  159.         HtmlColorCode S, S + Len(Tag), True
  160.         frmMain.trapUndo = True
  161.     Else
  162.         frmMain.trapUndo = False
  163.         HtmlColorCode S, S + Len(Tag), False
  164.         frmMain.trapUndo = True
  165.     End If
  166.     
  167.     'frmMain.trapUndo = True
  168. End Sub
  169.  
  170. ' Insert Asp code with asp coloring
  171.  
  172. Public Sub InsertAspTag(Tag$)
  173. Dim U As Long
  174.     U = frmMain.RichTxtBox.SelStart
  175.     If Len(frmMain.RichTxtBox.SelText) > 0 Then frmMain.RichTxtBox.SelText = ""
  176.     frmMain.RichTxtBox.SelText = Tag$
  177.     
  178.     frmMain.trapUndo = False
  179.     ASPColorCode U, U + Len(Tag)
  180.     frmMain.trapUndo = True
  181. End Sub
  182.  
  183. ' This function determines whether the caret is currently outside a tag. This was a royal pain in the ass.
  184.  
  185. Public Function IsOutsideTag()
  186. On Error Resume Next
  187. Dim LastGT As Long, LastLT As Long, NextGT As Long, NextLT As Long
  188. Dim EndTag As Long, StartTag As Long
  189. Dim txt$, Start As Long, Start2 As Long
  190. Dim InMainTag As Boolean, InEndTag As Boolean
  191.     
  192.     txt = frmMain.RichTxtBox.Text
  193.     Start = frmMain.RichTxtBox.SelStart
  194.     
  195.     If Start = 0 Then
  196.         m_TextCol = vbBlack
  197.         Exit Function
  198.     Else
  199.         EndTag = InStr(Start + 1, txt, ">")
  200.         StartTag = InStr(Start + 1, txt, "<")
  201.  
  202.         If StartTag > EndTag Then
  203.             InMainTag = True
  204.         Else
  205.             InMainTag = False
  206.         End If
  207.         
  208.         LastLT = RevInStr(txt, "<", Start + 1)
  209.         LastGT = RevInStr(txt, ">", Start + 1)
  210.  
  211.         If LastLT < LastGT Then
  212.             InEndTag = True
  213.         Else
  214.             InEndTag = False
  215.         End If
  216.  
  217.         If InMainTag = True Or InEndTag = True Then
  218.             m_TextCol = frmMain.RichTxtBox.SelColor
  219.         Else
  220.             m_TextCol = vbBlack
  221.         End If
  222.     End If
  223. End Function
  224.  
  225. ' ##########################################################################################
  226. ' These are the main color coding functions. These are not called ever by the user.
  227. ' ##########################################################################################
  228.  
  229. ' This is the main color coding function. This does everything html, comments, and attributes. It also calls
  230. ' the ASP color coding function if nessasary
  231.  
  232. Public Function HtmlColorCode(Optional startchar As Long = 1, Optional endchar As Long = -1, Optional StopAsp As Boolean = False)
  233. On Error GoTo ErrHandler
  234.     ' These are the variables for the tags for ColorCoding
  235.     Dim CommentOpenTag As String
  236.     Dim CommentCloseTag As String
  237.  
  238.     Dim oldselstart As Long, oldsellen As Long
  239.     
  240.     ' These are place holders for the color coding
  241.     Dim tag_open As Long
  242.     Dim tag_close As Long
  243.     Dim bef As String
  244.     Dim Curr As String
  245.     
  246.     frmMain.trapUndo = False
  247.     
  248.     ' Find out where the cursor is
  249.     oldselstart = frmMain.RichTxtBox.SelStart
  250.     oldsellen = frmMain.RichTxtBox.SelLength
  251.     
  252.     If endchar = -1 Then endchar = Len(frmMain.RichTxtBox.Text)
  253.     If startchar = 0 Then startchar = 1
  254.  
  255.     ' These are the close tags for colorcoding
  256.     
  257.     tag_close = startchar
  258.     
  259.     ' Lets try to hide the color coding from the user:
  260.     frmMain.RichTxtBox.HideSelection = True
  261.     
  262.     ' Now lets loop through the tags and color code it
  263.     Do
  264.         ' See where the next tag starts. if any
  265.         tag_open = InStr(tag_close, frmMain.RichTxtBox.Text, "<")
  266.         
  267.         'If so, then color it...
  268.         If tag_open <> 0 Then  'Found a tag
  269.             
  270.             'Get everything before the tag we're on...
  271.             bef = Mid$(frmMain.RichTxtBox.Text, 1, tag_open - 1)
  272.             
  273.             'Find the end of the next tag...
  274.             tag_close = InStr(tag_open, frmMain.RichTxtBox.Text, ">")
  275.  
  276.             'Get the current HTML tag...
  277.             Curr = Mid$(frmMain.RichTxtBox.Text, tag_open, tag_close - tag_open + 1)
  278.             
  279.             If tag_close <> 0 Then
  280.                 Select Case Left$(Curr, 3)
  281.                     Case "<!-"
  282.                         ' It's a comment...
  283.                         tag_close = InStr(tag_open, frmMain.RichTxtBox.Text, "->") + 1
  284.                             frmMain.RichTxtBox.SelStart = tag_open - 1
  285.                             frmMain.RichTxtBox.SelLength = tag_close - tag_open + 1
  286.                             frmMain.RichTxtBox.SelColor = m_CommentCol
  287.                     Case Else
  288.                         ' This colors basic Html tags and then colors the attributes
  289.                         cycleAttrib Curr, tag_open, tag_close
  290.                 End Select
  291.             End If
  292.             
  293.             If tag_close = 0 Or tag_close >= endchar Then
  294.                 ' If we are coloring tags and it's over the end tag then
  295.                 ' get me out of this loop and don't color anymore
  296.                 Exit Do
  297.             End If
  298.         Else
  299.             Exit Do
  300.         End If
  301.     Loop
  302.     
  303.     ' Color ASP Stuff only if we need to. We have a special function for coloring ASP tags so we won't
  304.     ' worry if this deals with it or not.
  305.     If StopAsp = False Then
  306.         ASPColorCode startchar, endchar
  307.     End If
  308.     
  309.     frmMain.RichTxtBox.SelStart = oldselstart
  310.     frmMain.RichTxtBox.SelLength = oldsellen
  311.     frmMain.RichTxtBox.HideSelection = False
  312.     frmMain.RichTxtBox.SetFocus
  313.     
  314.     frmMain.trapUndo = True
  315.     Exit Function
  316.     
  317. ErrHandler:
  318.     Exit Function
  319. End Function
  320.  
  321. ' This function colorizes ASP code
  322.  
  323. Private Function ASPColorCode(Optional startchar As Long = 1, Optional endchar As Long = -1)
  324. On Error GoTo ErrHandler
  325.     Dim oldselstart As Long, oldsellen As Long
  326.     
  327.     ' These are place holders for the color coding
  328.     Dim tag_open As Long
  329.     Dim tag_close As Long
  330.     Dim bef As String
  331.     Dim Curr As String
  332.     
  333.     frmMain.trapUndo = False
  334.     
  335.     ' Find out where the cursor is
  336.     oldselstart = frmMain.RichTxtBox.SelStart
  337.     oldsellen = frmMain.RichTxtBox.SelLength
  338.     
  339.     If endchar = -1 Then endchar = Len(frmMain.RichTxtBox.Text)
  340.     If startchar = 0 Then startchar = 1
  341.  
  342.     ' These are the close tags for colorcoding
  343.     
  344.     tag_close = startchar
  345.     
  346.     ' Lets try to hide the color coding from the user:
  347.     frmMain.RichTxtBox.HideSelection = True
  348.     
  349.     ' Now lets loop through the tags and color code it
  350.     Do
  351.         ' See where the next tag starts. if any
  352.         tag_open = InStr(tag_close, frmMain.RichTxtBox.Text, "<%")
  353.         
  354.         'If so, then color it...
  355.         If tag_open <> 0 Then  'Found a tag
  356.             
  357.             'Get everything before the tag we're on...
  358.             bef = Mid$(frmMain.RichTxtBox.Text, 1, tag_open - 1)
  359.             
  360.             'Find the end of the next tag...
  361.             tag_close = InStr(tag_open, frmMain.RichTxtBox.Text, "%>")
  362.  
  363.             'Get the current HTML tag...
  364.             Curr = Mid$(frmMain.RichTxtBox.Text, tag_open, tag_close - tag_open + 1)
  365.             
  366.             If tag_close <> 0 Then
  367.                 Select Case Left$(Curr, 2)
  368.                     Case "<%"
  369.                         ' It's asp
  370.                         tag_close = InStr(tag_open, frmMain.RichTxtBox.Text, "%>") + 1
  371.                             frmMain.RichTxtBox.SelStart = tag_open - 1
  372.                             frmMain.RichTxtBox.SelLength = tag_close - tag_open + 1
  373.                             frmMain.RichTxtBox.SelColor = m_AspCol
  374.                     Case Else
  375.                         ' it's not an asp tag so do nothing
  376.                 End Select
  377.             End If
  378.             
  379.             If tag_close = 0 Or tag_close >= endchar Then
  380.                 ' If we are coloring tags and it's over the end tag then
  381.                 ' get me out of this loop and don't color anymore
  382.                 Exit Do
  383.             End If
  384.         Else
  385.             Exit Do
  386.         End If
  387.     Loop
  388.     
  389.     frmMain.RichTxtBox.SelStart = oldselstart
  390.     frmMain.RichTxtBox.SelLength = oldsellen
  391.     frmMain.RichTxtBox.HideSelection = False
  392.     frmMain.RichTxtBox.SetFocus
  393.     
  394.     frmMain.trapUndo = True
  395.     
  396.     Exit Function
  397.     
  398. ErrHandler:
  399.     Exit Function
  400. End Function
  401.  
  402. ' This cycles through the html and comes back with the right tag colors for the tag and all of it's
  403. ' attributes
  404.  
  405. Private Function cycleAttrib(CurrTag As String, opentag As Long, closetag As Long)
  406.     
  407.     Dim fPos As Long, sPos As Long, qPos As Long, qnPos As Long, aPos As Long, tBeg As Long, tEnd As Long
  408.     Dim isFirstCycle As Boolean
  409.     Dim eTag As String
  410.     Dim sPosTxt As String
  411.     Dim LeftOver As Long
  412.     Dim EndTag As Long, QuotePos As Long, QuoteEndPos As Long
  413.     
  414.     frmMain.trapUndo = False
  415.     
  416.     eTag = CurrTag
  417.     isFirstCycle = True
  418.  
  419.     Do While Len(eTag) > 0
  420.         fPos = InStr(1, eTag, "=")
  421.  
  422.         If (fPos = 0 And isFirstCycle = True) Then
  423.             ' This just checks to see if it's a basic html tag w/ no attributes and if so colors that
  424.             ' without going through the rest of the junk.
  425.             frmMain.RichTxtBox.SelStart = opentag - 1
  426.             frmMain.RichTxtBox.SelLength = closetag - opentag + 1
  427.             frmMain.RichTxtBox.SelColor = m_TagCol
  428.             Exit Function
  429.         ' It looks like we have an attribute. Here comes the hard part...
  430.         ElseIf fPos <> 0 Then 'Put in the color info...
  431.             If Left$(eTag, 1) = "<" Then
  432.                 ' This brings back the entire tag. something like:
  433.                 ' <img src="blah.jpg" onclick="blah">
  434.                 ' and then color codes the entire thing
  435.                 tBeg = opentag
  436.                 tEnd = opentag + fPos
  437.  
  438.                 ' Color Code the entire tag first
  439.                 frmMain.RichTxtBox.SelStart = tBeg - 1
  440.                 frmMain.RichTxtBox.SelLength = closetag - tBeg + 1
  441.                 frmMain.RichTxtBox.SelColor = m_TagCol
  442.  
  443.                 ' This brings back the text that is past the attribute. in the previous example:
  444.                 ' "blah.jpg" onclick="blah">
  445.                 eTag = Mid$(eTag, fPos + 1)
  446.                 LeftOver = closetag - Len(eTag)
  447.             End If
  448.         End If
  449.         
  450.         'Find the first instance of a space in the
  451.         'part of the tag that we have left...
  452.         sPos = InStr(1, eTag, Chr$(32))
  453.  
  454.         'Gets the text up to the next space...
  455.         sPosTxt = Mid$(eTag, 1, sPos)
  456.         
  457.         'Checks to see if there's a quote in the text...
  458.         qPos = InStr(1, sPosTxt, Chr$(34))
  459.  
  460.         'If there's a quote found, then we need to find
  461.         'its end...
  462.         If qPos <> 0 Then
  463.             'Look for the next quote...
  464.             qnPos = InStr(2, eTag, Chr$(34))
  465.  
  466.             If qnPos <> 0 Then
  467.                 sPosTxt = Mid$(eTag, 1, qnPos)
  468.             End If
  469.         End If
  470.  
  471.         LeftOver = closetag - Len(eTag)
  472.         frmMain.RichTxtBox.SelStart = LeftOver
  473.         frmMain.RichTxtBox.SelLength = Len(sPosTxt)
  474.         frmMain.RichTxtBox.SelColor = m_AttribCol
  475.         
  476.         'Truncates the tag so there's no attrib value left...
  477.         eTag = Mid$(eTag, Len(sPosTxt) + 1)
  478.  
  479.         'Find the next position of an equal sign...
  480.         sPos = InStr(1, eTag, "=")
  481.  
  482.         'If there's no =, then we know we're on the last
  483.         'attrib value, so we need to put in some final
  484.         'info...all that's left is something like:
  485.         '"#ffffff">
  486.         If sPos = 0 Then
  487.             'Put in the attrib color before the ">"
  488.             'if it's the last attribute...
  489.             eTag = Mid$(eTag, 1, Len(eTag) - 1)
  490.  
  491.             'Insert the RTF info...
  492.             'bef = bef & infoRTF & AttribInfo & eTag
  493.             frmMain.RichTxtBox.SelStart = LeftOver
  494.             frmMain.RichTxtBox.SelLength = Len(eTag)
  495.             frmMain.RichTxtBox.SelColor = m_AttribCol
  496.  
  497.             'Truncate the end...
  498.             sPos = Len(eTag)
  499.             Exit Do
  500.         End If
  501.  
  502.         'Truncates the tag appropriately...
  503.         eTag = Mid$(eTag, sPos + 1)
  504.         isFirstCycle = False
  505.  
  506.         'If there's nothing left, then we need to exit
  507.         'the loop so it doesn't loop infinitely...
  508.         If sPos = 0 And qPos = 0 Then Exit Do
  509.     Loop
  510.     
  511.     frmMain.trapUndo = True
  512.     Exit Function
  513. End Function
  514.  
  515.