'========================================================== ' ' ' Auto complete words Example by Andy. ' ' ' ' Here we will auto comple IW key words in the edit box. ' ' ' ' Try typing - do or until or for etc.. in the edit box ' ' ' '========================================================== $INCLUDE "codetools.inc" 'You will need this include file. 'All variables, controls, arrays etc are defind in the "codetools.inc" file. ReadKeyWords() 'Read in IW key words. 'Auto complete keyword colours. color1 = rgb(255,204,204) 'Background colour (Pink - highlight) color3 = rgb(0,0,0) 'Text colour color4 = rgb(255,255,255) 'Background colour (White - normal) OPENWINDOW w7,0,0,500,400,@MINBOX|@MAXBOX|@SIZE|@CAPTION,0,"Auto complete - a working example by Andy.",&handler CONTROL w7,@STATIC,"Type some key words && press or click the list view to select word",20,80,480,25,@CTEDITLEFT,STATIC_1 CONTROL w7,@EDIT,"",20,110,440,25,0,WordSearch1 'Our edit box to type in words... CONTROLCMD w7,WordSearch1,@EDSETLIMITTEXT,255 SETCONTROLNOTIFY(w7,WordSearch1,1,1) CONTROL w7,@LISTVIEW,"",20,230,440,100,@BORDER|@LVSNOCOLUMNHEADER|@LVSSHOWSELALWAYS|@LVSREPORT|@VSCROLL,ListView SENDMESSAGE w7,LVM_SETEXTENDEDLISTVIEWSTYLE,0,LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_FLATSB|LBS_WANTKEYBOARDINPUT|LVS_EX_ONECLICKACTIVATE,ListView CONTROLCMD w7,ListView,@LVINSERTCOLUMN,0,"" CONTROLCMD w7,ListView,@LVSETCOLWIDTH,0,440 CONTROLCMD w7,ListView,@LVINSERTITEM,0,"" SETFONT w7,"Arial",10,500,0,STATIC_1 SETCONTROLCOLOR w7,STATIC_1,RGB(0,0,0),RGB(255,255,255) SETFONT w7,"Arial",10,500,0,WordSearch1 SETCONTROLCOLOR w7,WordSearch1,RGB(0,0,0),RGB(255,255,255) SETFONT w7,"Arial",10,500,0,ListView SETCONTROLCOLOR w7,ListView,RGB(0,0,0),RGB(255,255,255) 'Load words into our list view... CONTROLCMD w7,ListView,@LVINSERTITEM,0," " for a = 1 to 1661 CONTROLCMD w7,ListView,@LVINSERTITEM,a,#p[a].words next a 'Now let's hide the list view... showwindow w7,@SWHIDE,ListView 'And begin... setfocus w7,WordSearch1 WAITUNTIL IsWindowClosed(w7) 'Tidy up and close... Finish() SUB handler(),INT SELECT @MESSAGE CASE @IDCREATE CENTERWINDOW w7 CASE @IDCLOSEWINDOW CLOSEWINDOW w7 CASE @IDCONTROL SELECT @CONTROLID CASE WordSearch1 'Out edit box. IF @NOTIFYCODE = @ENENTERKEY 'Enter key pressed... OnWord = 1 AddTheKeyWord() 'Add the selected key word to the edit box... return 0 endif IF @NOTIFYCODE = @ENCHANGE 'Do something if something is typed... 'Get the position in the edit box where you started typing... CONTROLCMD w7,WordSearch1,@EDGETSELECTION,varStart,varEnd OnWord = 1 IF LEN(GETCONTROLTEXT w7,WordSearch1) = 0 SendMessage(w7,@LVM_ENSUREVISIBLE,0,0,ListView) showwindow w7,@SWHIDE,ListView return 0 ELSE SearchAndHighlight(1,WordSearch1) 'Search and highlight matching key word or part word... endif endif CASE ListView SELECT @NOTIFYCODE CASE NM_CUSTOMDRAW '<--- Do something when list view is being re-drawn RETURN ColourListViewSearch(w7.hWnd,@LPARAM) '<--- Color the list view items CASE @NMCLICK 'Click on listview LVpos =*@LPARAM.iItem CONTROLCMD(w7,ListView,@LVGETTEXT,LVpos,0,InfoA) AddTheKeyWord() 'Add the clicked key word to the edit box... endselect ENDSELECT ENDSELECT RETURN 0 ENDSUB 'Sub routine to search for a word you have typed against the key words... 'I have added "SETSIZE" here, because you might want the list view to appear near another edit box... sub SearchAndHighlight(int ListViewPos,int WordIn) SearchFor = GETCONTROLTEXT w7,WordIn if mid$(SearchFor,len(SearchFor),1) = " " or len(SearchFor) = 0 SendMessage(w7,@LVM_ENSUREVISIBLE,0,0,ListView) showwindow w7,@SWHIDE,ListView else showwindow w7,@SWSHOW,ListView if ListViewPos = 1 SETSIZE w7, 20,155,440,60,ListView CONTROLCMD w7,ListView,@LVSETCOLWIDTH,0,350 endif if ListViewPos = 2 SETSIZE w7, 890,170,305,60,ListView CONTROLCMD w7,ListView,@LVSETCOLWIDTH,0,285 endif endif 'Sub routine "GetWords" will put all your typed words into a array of words. int NumberOfWords = GetWords(SearchFor) int TempPos = 0 WordPosCount = 0 'Sub routine "GetWordX" retrieves each typed word and gets it's position in your edit box... 'I do this because you might not just be typing at the end of the edit box, you could be 'typing in between of words already typed... 'Here we can determine which typed word we need to match against... for a = 1 to NumberOfWords SearchFor = GetWordX(a) if TempPos <> instr((GETCONTROLTEXT w7,WordIn),SearchFor) WordPosCount ++ WordPos[WordPosCount] = instr((GETCONTROLTEXT w7,WordIn),SearchFor) endif next a CheckThisWord = 0 'Here we can determine where in the edit box we started typing... for a = 1 to WordPosCount if varStart < WordPos[a] 'Now we know which word to try to match against, and the starting position of it... CheckThisWord = a - 1 showwindow w7,@SWSHOW,ListView 'Show the list view with the key words in it... breakfor endif CheckThisWord = a next a SearchFor = GetWordX(CheckThisWord) 'String "SearchFor" is the word we need to search for in our key words... ClearWords() 'Let's tidy up as well, this clears the array we store our typed words in... if len(SearchFor) < 2 'No key word is less than two characters... SendMessage(w7,@LVM_ENSUREVISIBLE,0,0,ListView) showwindow w7,@SWHIDE,ListView return 0 endif 'If the word typed starts with a non-alphabetic character, check it... int AscValue = asc(mid$(SearchFor,1,1)) if AscValue < 64 and AscValue <> 36 SendMessage(w7,@LVM_ENSUREVISIBLE,0,0,ListView) showwindow w7,@SWHIDE,ListView return 0 endif if AscValue >= 91 and AscValue <= 96 and AscValue <> 95 SendMessage(w7,@LVM_ENSUREVISIBLE,0,0,ListView) showwindow w7,@SWHIDE,ListView return 0 endif if AscValue = 81 or AscValue = 113 or AscValue = 88 or AscValue = 89 or AscValue = 90 or AscValue = 120 or AscValue = 121 or AscValue = 122 SendMessage(w7,@LVM_ENSUREVISIBLE,0,0,ListView) showwindow w7,@SWHIDE,ListView return 0 endif 'Our starting position in the key word list view control 'You could add ' if mid$(SearchFor,1,1) = "A" then aa = X ' if mid$(SearchFor,1,1) = "B" then aa = Y ' if mid$(SearchFor,1,1) = "C" then aa = Z ' X, Y, and Z would be different starting positions. 'As nearly half of the key words start with "@" I'm checking for it. int aa = 0 if mid$(SearchFor,1,1) = "@" 'Search word begins with an "@". aa = 0 SearchLoopTo = 805 else 'Search word doesn't start with an "@", so start from 806, after all the @ keywords... aa = 806 SearchLoopTo = CONTROLCMD(w7,ListView,@LVGETCOUNT) -1 endif for SearchLoop = aa to SearchLoopTo 'Search our key words... InvalidateRect(GETCONTROLHANDLE(w7,ListView),NULL,0) 'This simply sets the list view back to white background... CONTROLCMD(w7,ListView,@LVGETTEXT,SearchLoop,0,InfoA) if mid$(ucase$(InfoA),1,1) = mid$(ucase$(SearchFor),1,1) if instr(ucase$(InfoA),ucase$(SearchFor)) 'Closest match of typed word and key word... SendMessage(w7,@LVM_ENSUREVISIBLE,SearchLoop,0,ListView) LastPosFound = SearchLoop breakfor 'No need to keep searching... endif endif next SearchLoop return endsub 'Sub routine to add our selected key word to the edit box in the correct position... sub AddTheKeyWord(),int int WhichWord = 0 if OnWord = 1 WhichWord = WordSearch1 'We are only concentrating on this edit box, but you might waht to check other edit boxes... endif if OnWord = 2 WhichWord = WordSearch2 endif if OnWord = 3 WhichWord = WordSearch3 endif if len(getcontroltext(w7,WhichWord)) = 0 then return 0 int NumberOfWords = GetWords(getcontroltext(w7,WhichWord)) SearchFor = GetWordX(NumberOfWords) 'Here we delete the typed word (even if it's not a full word yet, e.g. "unt" where we intended to type "until" 'and place the full key word in it's place... 'String "InfoA" has the selected ley word stored in it. if NumberOfWords = CheckThisWord 'Replace last word typed... int DeleteThisWord = instr(getcontroltext(w7,WhichWord),SearchFor) TempText2 = mid$(getcontroltext(w7,WhichWord),1,DeleteThisWord-1) setcontroltext(w7,WhichWord,TempText2 + InfoA) AppendEdit(w7,WhichWord,"",0) 'Jump to the end of the edit box... else 'Replace word not at the end of the edit box... DeleteThisWord = instr(getcontroltext(w7,WhichWord),GetWordX(CheckThisWord)) int ItsLen = len(GetWordX(CheckThisWord)) string NewSearchBox1 = mid$(getcontroltext(w7,WhichWord),1,DeleteThisWord -1) string NewSearchBox2 = mid$(getcontroltext(w7,WhichWord),DeleteThisWord + ItsLen,len(getcontroltext(w7,WhichWord))) setcontroltext(w7,WhichWord,NewSearchBox1 + InfoA + NewSearchBox2) AppendEdit(w7,WhichWord,"",0) 'Jump to the end of the edit box... endif setfocus w7,WhichWord 'Set focus back to our edit box... ClearWords() 'Clear our typed words array... return 0 endsub 'Sub routine to store our typed words... SUB GetWords(STRING BlockIn),int string StoreSubName = "" BlockIn = ltrim$(BlockIn) BlockIn = Rtrim$(BlockIn) BlockSearch = "" if BlockIn = "" then return 0 FOR a = 1 TO LEN(BlockIn) STRING bs = MID$(BlockIn,a,1) BlockSearch = BlockSearch + bs NEXT a WordIn = "" WordCount = 0 FindWords = 0 FindDels = 0 FoundWords = 0 FoundDels = 0 WordCount = 0 y = 0 'Here we are breaking down what's been typed into words 'and storing each word in an array called "FindWord". DO INT amatch = 0 INT smatch = 0 y ++ Letter = MID$(BlockSearch,y,1) IF LEN(BlockSearch) = 0 BREAK ENDIF IF INSTR(CheckLetter,Letter) > 0 'Look for a delimiter, i.e. a character that can separate words... amatch = 1 if y = 1 and Letter = "@" or Letter = "$" or Letter = "_" 'These are exceptions to the rule for key words... Amatch = 0 endif ENDIF 'No delimiter found, so add to the word. IF amatch = 0 WordIn = WordIn + MID$(BlockSearch,y,1) ELSE 'End of word. IF WordIn <> "" AND WordIn <> " " FindWords ++ FindWord[FindWords] = WordIn WordCount ++ WordIn = "" ENDIF IF MID$(BlockSearch,y,1) <> "" AND MID$(BlockSearch,y,1) <> " " FindDels ++ FindDel[FindDels] = MID$(BlockSearch,y,1) ENDIF ENDIF UNTIL y = LEN(BlockSearch) y = 0 'After the end of each line, add the final word. WordCount ++ IF WordIn <> "" 'AND WordIn <> "" FindWords ++ FindWord[FindWords] = WordIn ENDIF 'Our typed words are now stored in an array called "FindWord". RETURN WordCount 'Returns how many words you have typed in the edit box. ENDSUB 'Sub routine to get a specific typed word. sub GetWordX(int WordNo),string return FindWord[WordNo] return "" endsub 'Sub routine to clear out our type words array. sub ClearWords() FOR a = 1 TO FindWords SaveWord[a] = FindWord[a] FindWord[a] = "" NEXT a FOR a = 1 TO FindDels FindDel[a] = "" NEXT a return endsub 'Sub routine to colour list view entries. SUB ColourListViewSearch(hWnd:UINT,lParam:UINT),UINT string tempola int row row = *lParam.nmcd.dwItemSpec if row = LastPosFound 'Found a search match in the constant names currentcolor = color1 'Highlight the line else currentcolor = color4 'Otherwise colour the line's background white (normal) endif if row = 0 currentcolor = color4 'Colour the line's background white (normal) endif CONTROLCMD w7,ListView,@LVGETTEXT,row,0,tempola SELECT *lParam.nmcd.dwDrawStage CASE CDDS_PREPAINT rv = CDRF_NOTIFYITEMDRAW CASE CDDS_ITEMPREPAINT rv = CDRF_NOTIFYSUBITEMDRAW CASE CDDS_SUBITEMPREPAINT *lParam.clrText = color3 *lParam.clrTextBk = currentcolor rv = CDRF_NEWFONT DEFAULT rv = CDRF_DODEFAULT ENDSELECT RETURN rv ENDSUB '========================================================== ' ' ' End of Auto complete words Example by Andy. ' ' ' '==========================================================