home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 3_2004-2005.ISO / Data / Zips / Example_of1799241012004.psc / modAutoCompleteToolTips.bas < prev    next >
Encoding:
BASIC Source File  |  2004-07-18  |  8.6 KB  |  269 lines

  1. Attribute VB_Name = "modAutoCompleteToolTips"
  2. Option Base 1
  3. Option Explicit
  4.  
  5. Global selRange As CodeSenseCtl.IRange
  6. 'globals of CodeSense Control
  7. Global CSGlobals As New CodeSenseCtl.Globals
  8. 'current word (for tooltip)
  9. Global strCurrentWord As String
  10. 'current word function key in udtFuncDesc (see modFunctionDefinitions)
  11. Global intCurrentWordItem As Integer
  12.  
  13. 'CodeList ---
  14. ' TRIGGERED: When AutoComplete key shortcut is pressed
  15. ' PURPOSE: Shows list
  16. ' INPUTS:
  17. '  Control - CodeSense control that caused this
  18. '  ListCtrl - List control assigned to us by
  19. '             the CodeSense control.
  20. ' RETURNS: True (to show list)
  21.  
  22. Function CodeList(ByVal Control As CodeSenseCtl.ICodeSense, ByVal ListCtrl As CodeSenseCtl.ICodeList, ImageList As ImageList) As Boolean
  23. Dim intObjItem As Integer
  24. Dim intTemp As Integer
  25.  
  26. 'set up list properties
  27. 'ListCtrl.BackColor = Control.GetColor(cmClrWindow)
  28. ListCtrl.Font.Name = "Tahoma"
  29. ListCtrl.Font.Size = 10
  30. 'image list for the little pictures to the side
  31. 'on the list
  32. ListCtrl.ImageList = ImageList
  33.  
  34. 'if current word is an object (ie. person typed
  35. ' a dot (.) after object name...
  36. If ObjDefined(Control.CurrentWord) Then
  37.     '...look in autocomplete list for that object
  38.     intObjItem = colObjList(Control.CurrentWord)
  39. 'otherwise
  40. Else
  41.     'use normal autocomplete list
  42.     intObjItem = 1
  43. End If
  44.  
  45. 'go through all autocomplete items...
  46. For intTemp = 1 To UBound(udtObjInfo(intObjItem).strMembers)
  47.     '...and add them to list
  48.     ListCtrl.AddItem udtObjInfo(intObjItem).strMembers(intTemp), udtObjInfo(intObjItem).intMemberType(intTemp)
  49. Next intTemp
  50.  
  51. 'show list
  52. CodeList = True
  53. End Function
  54.  
  55. 'CodeListSelMade ---
  56. ' TRIGGERED: When selection is made in list
  57. ' PURPOSE: To add item chosen to text
  58. ' INPUTS:
  59. '  Control - CodeSense control that triggered this
  60. '  ListCtrl - the list containing AutoComplete items
  61. ' RETURNS: False (to kill list)
  62. Function CodeListSelMade(ByVal Control As CodeSenseCtl.ICodeSense, ByVal ListCtrl As CodeSenseCtl.ICodeList) As Boolean
  63. Dim strItem As String
  64. Dim range As New CodeSenseCtl.range
  65.  
  66. 'get current word in text
  67. strItem = ListCtrl.GetItemText(ListCtrl.SelectedItem)
  68. 'if current word is start of word chosen in box
  69. '(ie. user entered 'Msg', went into AutoComplete,
  70. ' and chose 'MsgBox, it would replace 'Msg' with
  71. ' 'MsgBox' instead of inserting it. If it inserted
  72. ' it, the word would become 'MsgMsgbox')...
  73. If LCase$(Control.CurrentWord) = LCase$(Left$(strItem, Control.CurrentWordLength)) Then
  74.     '...shorten text (if user typed 'Msg', went into
  75.     ' autocomplete and chose MsgBox, this shortens
  76.     ' the item to 'Box' ['Msg' + 'Box' = 'MsgBox'])
  77.     strItem = Mid$(strItem, Control.CurrentWordLength + 1)
  78. End If
  79.  
  80. 'replace selection with this item
  81. Control.ReplaceSel (strItem)
  82.  
  83. 'get cursor position
  84. Set range = Control.GetSel(True)
  85. range.StartColNo = range.StartColNo + Len(strItem)
  86. range.EndColNo = range.StartColNo
  87. range.EndLineNo = range.StartLineNo
  88. 'set cursor position to just after word
  89. Control.SetSel range, True
  90.  
  91. 'kill list
  92. CodeListSelMade = False
  93. End Function
  94.  
  95.  
  96. 'CodeTip ---
  97. ' TRIGGERED: When ToolTip should be shown
  98. ' PURPOSE: To check if it really should be shown
  99. ' INPUTS:
  100. '  Control - You should know by now ;)
  101. ' RETURNS: Type of tip to show (refer to documentation
  102. '          on CodeSense control)
  103. Function CodeTip(ByVal Control As CodeSenseCtl.ICodeSense) As CodeSenseCtl.cmToolTipType
  104. Dim token As CodeSenseCtl.cmTokenType
  105. 'get current token type
  106. token = Control.CurrentToken
  107. 'if current token is text or keyword...
  108. If ((token = cmTokenTypeText) Or (token = cmTokenTypeKeyword)) Then
  109.     '...save current word
  110.     strCurrentWord = Control.CurrentWord
  111.     'if current word is defined...
  112.     If FuncDefined(strCurrentWord) Then
  113.         '...get the index of it...
  114.         intCurrentWordItem = colFuncList(strCurrentWord)
  115.         '...and tell codesource control to show
  116.         'tip.
  117.         CodeTip = cmToolTipTypeMultiFunc
  118.     'if word not defined...
  119.     Else
  120.         '...show no tip
  121.         CodeTip = cmToolTipTypeNone
  122.     End If
  123. 'if in a comment...
  124. Else
  125.     '...show no tip
  126.     CodeTip = cmToolTipTypeNone
  127. End If
  128. End Function
  129.  
  130. 'OK, from now on I am not putting the 'Control'
  131. 'variable in the input description, as it is
  132. 'getting annoying to type it in everytime!
  133. 'And yes, instead of cutting and pasting the
  134. 'header on every function, i type it out again
  135. 'and again and again and again and again ;)
  136.  
  137.  
  138. 'CodeTipInitialize ---
  139. ' TRIGGERED: When ToolTip is initializing
  140. ' PURPOSE: To initialize the tooltio
  141. ' INPUTS:
  142. '  ToolTipCtrl - the tooltip created by the control
  143. ' RETURNS: Nothing
  144. Sub CodeTipInitialize(ByVal Control As CodeSenseCtl.ICodeSense, ByVal ToolTipCtrl As CodeSenseCtl.ICodeTip)
  145. Dim tip As CodeSenseCtl.CodeTipMultiFunc
  146. 'get the control
  147. Set tip = ToolTipCtrl
  148.  
  149. 'set current argument to the first one (this is zero-based)
  150. tip.Argument = 0
  151.  
  152. 'save position
  153. Set selRange = Control.GetSel(True)
  154. selRange.EndColNo = selRange.EndColNo '+ 1
  155.  
  156. 'get definition count for the function
  157. tip.FunctionCount = UBound(udtFuncDesc(intCurrentWordItem).strDef) - 1
  158. 'set current definition to the first one (again, 0-based)
  159. tip.CurrentFunction = 0
  160. 'set tip to the first one
  161. tip.TipText = udtFuncDesc(intCurrentWordItem).strDef(1)
  162.  
  163. 'set font
  164. tip.Font.Name = "Arial"
  165. tip.Font.Size = 10
  166. tip.Font.Italic = True
  167. tip.Font.Bold = False
  168. End Sub
  169.  
  170. 'CodeTipUpdate ---
  171. ' TRIGGERED: When tip should be updated
  172. ' PURPOSE: To update tip
  173. ' INPUTS:
  174. '  ToolTipCtrl - current ToolTip
  175. ' RETURNS: Nothing
  176. Sub CodeTipUpdate(ByVal Control As CodeSenseCtl.ICodeSense, ByVal ToolTipCtrl As CodeSenseCtl.ICodeTip)
  177. Dim iTrim As Integer, j As Integer
  178. Dim bolInQuote As Boolean
  179. Dim tip As CodeSenseCtl.CodeTipMultiFunc
  180. 'get tip
  181. Set tip = ToolTipCtrl
  182.  
  183. Dim range As CodeSenseCtl.IRange
  184. 'get current cursor position
  185. Set range = Control.GetSel(True)
  186. 'if user has moved up/down or before the statement...
  187. If (range.EndLineNo <> selRange.EndLineNo) Or _
  188.    (range.EndColNo < selRange.EndColNo) Then
  189.    '...what do you think?
  190.     tip.Destroy
  191. Else
  192.     Dim iArg, I As Integer
  193.     Dim strLine As String
  194.  
  195.     iArg = 0
  196.     'set i to the line number
  197.     I = selRange.EndLineNo
  198.     'get the line
  199.     strLine = Control.GetLine(I)
  200.     'set iTrim to length of line + 1
  201.     iTrim = Len(strLine) + 1
  202.     'if cursor isn't at end of line...
  203.     If (range.EndColNo < iTrim) Then
  204.         '...then iTrim = current cursor pos
  205.         iTrim = range.EndColNo
  206.     End If
  207.     'get current line, up to iTrim
  208.     strLine = Left(strLine, iTrim)
  209.     bolInQuote = False
  210.     j = 0
  211.     'go through every character in line
  212.     While ((Len(strLine) <> 0) And (j <= Len(strLine)) And (iArg <> -1))
  213.         'check if quote encountered
  214.         If (Mid(strLine, j + 1, 1) = """") Then
  215.             bolInQuote = Not bolInQuote
  216.         'if character is comma...
  217.         ElseIf (Mid(strLine, j + 1, 1) = ",") And bolInQuote = False Then
  218.             '...add 1 to argument count
  219.             iArg = iArg + 1
  220.         'if character is end bracket...
  221.         ElseIf (Mid(strLine, j + 1, 1) = ")") And bolInQuote = False Then
  222.             '...signal to destroy tip
  223.             iArg = -1
  224.         'if character is quote...
  225.         ElseIf (Mid(strLine, j + 1, 1) = "'") And bolInQuote = False Then
  226.             '...set iArg to -1 to destroy tip (since
  227.             'user is starting a comment
  228.             iArg = -1
  229.         End If
  230.         'add one to character count
  231.         j = j + 1
  232.     Wend
  233.     'if tip should be destroyed...
  234.     If (iArg = -1) Then
  235.         '...destroy it, ...
  236.         tip.Destroy
  237.     '...otherwise
  238.     Else
  239.         'set number of current argument
  240.         tip.Argument = iArg
  241.         'set tiptext to current function description
  242.         tip.TipText = udtFuncDesc(intCurrentWordItem).strDef(tip.CurrentFunction + 1)
  243.     End If
  244. End If
  245. End Sub
  246.  
  247. 'KeyPress ---
  248. ' TRIGGERED: When a key is pressed
  249. ' PURPOSE: To see if AutoComplete should be activated
  250. ' INPUTS:
  251. '  KeyAscii - The ASCII code of the key
  252. '  Shift - The KeyMask (eg. shift, alt or ctrl)
  253. ' RETURNS: Nothing
  254. Function KeyPress(ByVal Control As CodeSenseCtl.ICodeSense, ByVal KeyAscii As Long, ByVal Shift As Long) As Boolean
  255.     Select Case KeyAscii
  256.         'if key is starting bracket or space...
  257.         Case (Asc("(")), (Asc(" "))
  258.             '...show AutoComplete
  259.             Control.ExecuteCmd (cmCmdCodeTip)
  260.         'if key is dot...
  261.         Case (Asc("."))
  262.             '...and current word is a defined object...
  263.             If ObjDefined(Control.CurrentWord) Then
  264.                 '...show autocomplete
  265.                 Control.ExecuteCmd cmCmdCodeList
  266.             End If
  267.     End Select
  268. End Function
  269.