home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / sgwnd10 / codebloc.cls < prev    next >
Encoding:
Visual Basic class definition  |  1998-08-15  |  9.2 KB  |  268 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CodeBlock"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
  11. Attribute VB_Ext_KEY = "Top_Level" ,"No"
  12. Attribute VB_Ext_KEY = "Member0" ,"Location"
  13. Attribute VB_Ext_KEY = "Member1" ,"Parameters"
  14. Option Explicit
  15.  
  16. Private mvarName As String
  17. Private mvarLocation As Location
  18. Private mvarTemplate As String
  19. Private mvarParameters As Parameters
  20.  
  21.  
  22. Const skeyCodeBlock = "{CodeBlock}"
  23. Const skeyEndCode = "{EndCode}"
  24. Const skeyCode = "{Code}"
  25. Const skeyName = "{Name}"
  26. Const skeyLocation = "{Location}"
  27. Const sparLeft = "["
  28. Const sparRight = "]"
  29.  
  30.  
  31. Private Sub ExpandString(ByRef s$)
  32.     If Not (mvarParameters Is Nothing) Then
  33.         Dim par As Parameter
  34.         For Each par In mvarParameters
  35.             Dim sPar$
  36.             sPar = sparLeft & par.name & sparRight
  37.             Str_ReplaceAll s, sPar, par.Value
  38.         Next
  39.     End If
  40. End Sub
  41.  
  42. Private Function ItemValue(sText$, sName$)
  43.     ItemValue = ""
  44.     
  45.     Dim pos&
  46.     pos = InStr(sText, sName)
  47.     If (pos > 0) Then
  48.         pos = InStr(pos, sText, "=")
  49.         If (pos > 0) Then
  50.             Dim endPos&
  51.             endPos = InStr(pos + 1, sText, vbCrLf)
  52.             ItemValue = Str_Trim(Mid(sText, pos + 1, endPos - pos - 1))
  53.         End If
  54.     End If
  55. End Function
  56.  
  57. Friend Sub Initialize(sCodeBlock$, params As Parameters)
  58.  
  59.     Set mvarLocation = Nothing
  60.     Set mvarLocation = New Location
  61.     Set mvarParameters = params
  62.     
  63.     ' Find block name and location
  64.     mvarName = ItemValue(sCodeBlock, skeyName)
  65.     Dim sLocation$
  66.     sLocation = ItemValue(sCodeBlock, skeyLocation)
  67.     ExpandString sLocation
  68.     mvarLocation.Initialize sLocation
  69.     
  70.     ' Find code
  71.     mvarTemplate = ""
  72.     Dim nCodeStart&, nCodeEnd&
  73.     nCodeStart = InStr(1, sCodeBlock, skeyCode)
  74.     If (nCodeStart = 0) Then Exit Sub
  75.     nCodeStart = nCodeStart + Len(skeyCode)
  76.     nCodeEnd = InStr(nCodeStart, sCodeBlock, skeyEndCode)
  77.     If (nCodeEnd < nCodeStart) Then Exit Sub
  78.     nCodeEnd = nCodeEnd - 1
  79.     mvarTemplate = Str_TrimEx(Mid(sCodeBlock, nCodeStart, nCodeEnd - nCodeStart), Chr(10) & Chr(13))
  80. End Sub
  81.  
  82. Private Sub Class_Initialize()
  83.     Set mvarParameters = Nothing
  84.     Set mvarLocation = New Location
  85. End Sub
  86.  
  87. Private Sub Class_Terminate()
  88.     Set mvarParameters = Nothing
  89.     Set mvarLocation = Nothing
  90. End Sub
  91.  
  92. Public Property Get Template() As String
  93. Attribute Template.VB_Description = "Original temlate code"
  94.     Template = mvarTemplate
  95. End Property
  96.  
  97. Public Property Get Location() As Object
  98. Attribute Location.VB_Description = "Location of the code block"
  99.     Set Location = mvarLocation
  100. End Property
  101.  
  102. Public Property Get name() As String
  103. Attribute name.VB_Description = "Code block name"
  104.     name = mvarName
  105. End Property
  106.  
  107. Public Function ExpandTemplate() As String
  108.     ExpandTemplate = mvarTemplate
  109.     ExpandString ExpandTemplate
  110. End Function
  111.  
  112. Public Property Get InsertPosition(code As CodeModule) As Long
  113.     On Error GoTo Error_
  114.     
  115.     InsertPosition = 0
  116.     If (code Is Nothing) Then Exit Property
  117.     
  118.     With mvarLocation
  119.         Select Case .Section
  120.             Case sectGlobal
  121.                 InsertPosition = 1
  122.                 Select Case .Position
  123.                     Case posEnd: InsertPosition = code.CountOfDeclarationLines + 1
  124.                     Case posAbsolute: InsertPosition = .LineNumber
  125.                 End Select
  126.                 
  127.             Case sectProc
  128.                 On Error Resume Next
  129.                 InsertPosition = 0
  130.                 InsertPosition = code.ProcBodyLine(.ProcName, vbext_pk_Proc)
  131.                 On Error GoTo Error_
  132.                 If (InsertPosition > 0) Then
  133.                     Select Case .Position
  134.                         Case posBegin: InsertPosition = InsertPosition + 1
  135.                         Case posEnd: InsertPosition = InsertPosition + code.ProcCountLines(.ProcName, vbext_pk_Proc) - 2
  136.                         Case posAbsolute: InsertPosition = InsertPosition + .LineNumber
  137.                     End Select
  138.                 Else
  139.                     ' Procedure does not exist.
  140.                 End If
  141.                 
  142.             Case sectEvent
  143.                 Dim sProcName
  144.                 sProcName = .ObjectName & "_" & .EventName
  145.                 On Error Resume Next
  146.                 InsertPosition = 0
  147.                 InsertPosition = code.ProcBodyLine(sProcName, vbext_pk_Proc)
  148.                 On Error GoTo Error_
  149.                 If (InsertPosition > 0) Then
  150.                     Select Case .Position
  151.                         Case posBegin: InsertPosition = InsertPosition + 1
  152.                         Case posEnd: InsertPosition = InsertPosition + code.ProcCountLines(sProcName, vbext_pk_Proc) - 2
  153.                         Case posAbsolute: InsertPosition = InsertPosition + .LineNumber
  154.                     End Select
  155.                 Else
  156.                     ' Procedure does not exist.
  157.                 End If
  158.                 
  159.             Case sectPropGet, sectPropLet, sectPropSet
  160.                 Dim kind As vbext_ProcKind
  161.                 Select Case .Section
  162.                     Case sectPropGet: kind = vbext_pk_Get
  163.                     Case sectPropLet: kind = vbext_pk_Let
  164.                     Case sectPropSet: kind = vbext_pk_Set
  165.                 End Select
  166.                 
  167.                 On Error Resume Next
  168.                 InsertPosition = 0
  169.                 InsertPosition = code.ProcBodyLine(.ProcName, kind)
  170.                 On Error GoTo Error_
  171.                 If (InsertPosition > 0) Then
  172.                     Select Case .Position
  173.                         Case posBegin: InsertPosition = InsertPosition + 1
  174.                         Case posEnd: InsertPosition = InsertPosition + code.ProcCountLines(sProcName, vbext_pk_Proc) - 2
  175.                         Case posAbsolute: InsertPosition = InsertPosition + .LineNumber
  176.                     End Select
  177.                 Else
  178.                     ' Procedure does not exist.
  179.                 End If
  180.         End Select
  181.     End With
  182.     
  183.     Exit Property
  184. Error_:
  185.     Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
  186. End Property
  187.  
  188. Public Sub EnsureProcExist(code As CodeModule)
  189.     On Error GoTo Error_
  190.     
  191.     ' Get procedure name
  192.     Dim sProcName$
  193.     Dim kind As vbext_ProcKind
  194.     sProcName = ""
  195.     With mvarLocation
  196.         Select Case .Section
  197.             Case sectGlobal
  198.                 kind = vbext_pk_Proc
  199.             Case sectProc:
  200.                 kind = vbext_pk_Proc
  201.                 sProcName = .ProcName
  202.             Case sectEvent:
  203.                 kind = vbext_pk_Proc
  204.                 If (.ObjectName <> "") And (.EventName <> "") Then
  205.                     sProcName = .ObjectName & "_" & .EventName
  206.                 End If
  207.             Case sectPropGet
  208.                 kind = vbext_pk_Get
  209.                 sProcName = .ProcName
  210.             Case sectPropLet
  211.                 kind = vbext_pk_Let
  212.                 sProcName = .ProcName
  213.             Case sectPropSet
  214.                 kind = vbext_pk_Set
  215.                 sProcName = .ProcName
  216.         End Select
  217.  
  218.         If (sProcName <> "") Then
  219.             On Error Resume Next
  220.             Dim nLine&
  221.             nLine = 0
  222.             nLine = code.ProcBodyLine(sProcName, kind)
  223.             On Error GoTo Error_
  224.             If (nLine = 0) Then
  225.                 ' Procedure does not exist. Create it
  226.                 Dim sCode$
  227.                 sCode = ""
  228.                 Select Case .Section
  229.                     Case sectGlobal
  230.                     Case sectEvent:
  231.                         code.CreateEventProc .EventName, .ObjectName
  232.                     Case sectProc:
  233.                         code.AddFromString "Public Sub " & sProcName & "()" & vbCrLf & "End Sub"
  234.                     Case sectPropGet
  235.                         code.AddFromString "Public Property Get " & sProcName & "() As Variant" & vbCrLf & "End Property"
  236.                     Case sectPropLet
  237.                         code.AddFromString "Public Property Let " & sProcName & "(par)" & vbCrLf & "End Property"
  238.                     Case sectPropSet
  239.                         code.AddFromString "Public Property Set " & sProcName & "(par)" & vbCrLf & "End Property"
  240.                 End Select
  241.             End If
  242.         End If
  243.     End With
  244.     
  245.     Exit Sub
  246. Error_:
  247.    If Err.Number = 57017 Then
  248.       ' Event handler is invalid.
  249.       ' SG Wiindow is not referenced in the VB references list
  250.       Err.Description = "SG Window is not referenced!" & vbCrLf & _
  251.          "Open Project menu and click on the References..." & vbCrLf & _
  252.          "command to add SG Window to the list of the referenced components" & vbCrLf & vbCrLf & _
  253.          "Wizard did not insert all nececcery code!"
  254.  
  255.    End If
  256.    Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
  257. End Sub
  258.  
  259. Public Sub InsertCode(code As CodeModule)
  260.     Dim nLine&
  261.     
  262.     EnsureProcExist code
  263.     nLine = InsertPosition(code)
  264.     If (nLine > 0) Then
  265.         code.InsertLines nLine, ExpandTemplate
  266.     End If
  267. End Sub
  268.