philw Posted February 19, 2008 Share Posted February 19, 2008 I'm having trouble with a simple search function. It's the usual sort of thing, a window opens, you enter a search term and check a box if you want the search to be case sensitive. Clicking the "search" button closes the window, scrolls down to the first instance and highlights it. Pressing f3 finds the next instance, and so on. For some reason, the initial search, although it scrolls to the first instance, doesn't highlight it. Subsequent calls using f3 work fine. This is a pared-down version of the script. The relevant functions start at line 293. expandcollapse popup#include <GuiConstants.au3> #include <GuiEdit.au3> #include <File.au3> #include <Array.au3> #include <String.au3> #include <Misc.au3> ; =============================================== ; set mode ; =============================================== Opt("GUIOnEventMode",1) ; =============================================== ; set up variables ; =============================================== Dim $filedata[6] Dim $select[2] Global $winflag=1 Global $docflag=0 Global $fileflag Global $dstr Global $drive Global $directory Global $name Global $type Global $d Global $file Global $fileh Global $filepath Global $defaultpath Global $filename Global $selected Global $offset Global $fstr Global $flen Global $case Global $fct ; =============================================== ; create main window ;=============================================== $hWindow=GUICreate("",750,460,-1,-1,$WS_OVERLAPPEDWINDOW+$WS_MAXIMIZE) WinSetTitle($hWindow,"","Gedit") GUISetOnEvent($GUI_EVENT_CLOSE,"CloseWindow",$hWindow) ; =============================================== ; create find window ; =============================================== $hFindWindow=GUICreate("Find",440,145,-1,-1,$WS_BORDER+$WS_DISABLED,-1,$hWindow) GUISetBkColor(0xEEEEEE) ; =============================================== ; create find controls ; =============================================== $FindLabel1=GUICtrlCreateLabel("Find:",20,20,100,20) $FindInput1=GUICtrlCreateInput("",20,45,400,20) $FindCheckbox1=GUICtrlCreateCheckBox("Case sensitive",20,85,150,20,$BS_AUTOCHECKBOX) $FindButton1=GUICtrlCreateButton("Find",310,85,50,20,$BS_DEFPUSHBUTTON) $FindButton2=GUICtrlCreateButton("Cancel",370,85,50,20) GUICtrlSetOnEvent($FindButton1,"SearchFindText") GUICtrlSetOnEvent($FindButton2,"SearchFindClose") ; =============================================== ; show main window ; =============================================== GUISetState(@SW_SHOW,$hWindow) GUISetState(@SW_ENABLE,$hWindow) ; =============================================== ; create text field ; =============================================== $hEdit=GUICtrlCreateEdit("",0,0,750,460,$ES_MULTILINE+$WS_VSCROLL+$WS_MAXIMIZE+$ES_WANTRETURN) If @OSTYPE="WIN32_NT" Then GUICtrlSendMsg($hEdit,$EM_SETLIMITTEXT,4000000,0) EndIf GUICtrlSetState($hEdit,$GUI_ENABLE) GUICtrlSetState($hEdit,$GUI_FOCUS) ; =============================================== ; set up file menu ; =============================================== $filemenu=GUICtrlCreateMenu("&File") $fileopen=GUICtrlCreateMenuItem("Open ctrl-o",$filemenu) GUICtrlSetOnEvent($fileopen,"FileOpenFile") ; =============================================== ; set up search menu ; =============================================== $searchmenu=GUICtrlCreateMenu("&Search") $searchfind=GUICtrlCreateMenuitem("Find ctrl-f",$searchmenu) $searchnext=GUICtrlCreateMenuitem("Find next f3",$searchmenu) GUICtrlSetOnEvent($searchfind,"SearchFindOpen") GUICtrlSetOnEvent($searchnext,"SearchFindInstance") ; =============================================== ; set up hotkeys ; =============================================== EnableHotKeys() ; =============================================== ; set up menu messaging ; =============================================== GUIRegisterMsg($WM_INITMENUPOPUP,"ProcessMsg") GUIRegisterMsg($WM_UNINITMENUPOPUP,"ProcessMsg") ; =============================================== ; general functions ; =============================================== Func ProcessMsg($h,$msg,$w,$l) If $msg=279 Then DisableReturn() EndIf If $msg=293 Then EnableReturn() EndIf EndFunc ; ----------------------------------------------- Func EnableReturn() HotKeySet("{enter}","EditReturn") EndFunc ; ----------------------------------------------- Func DisableReturn() HotKeySet("{enter}") EndFunc ; ----------------------------------------------- Func EnableHotKeys() HotKeySet("^f","SearchFindOpen") HotKeySet("^o","FileOpenFile") HotKeySet("{f3}","SearchFindInstance") EndFunc ; ----------------------------------------------- Func DisableHotKeys() HotKeySet("^f") HotKeySet("^o") HotKeySet("{f3}") EndFunc ; ----------------------------------------------- Func Selected() $select=_GUICtrlEdit_GetSel($hEdit) If $select[0]<>$select[1] Then Return true Else Return false EndIf EndFunc ; ----------------------------------------------- Func EditGetOffset() $d=_GUICtrlEdit_LineIndex($hEdit,-1)+_GUICtrlEdit_LineLength($hEdit,-1) $dstr=GUICtrlRead($hEdit) $dstr=StringLeft($dstr,$d) $darray=StringSplit($dstr,@CRLF,1) $d=$darray[0] $dstr=$darray[$d] $offset=StringRegExpReplace($dstr,"\S+\s*","") EndFunc ; ----------------------------------------------- Func EditReturn() EditGetOffset() _GUICtrlEdit_ReplaceSel($hEdit,@CRLF&$offset) EndFunc ; ----------------------------------------------- Func CloseWindow() If FileSaveChanges() Then Exit EndIf EndFunc ; =============================================== ; file functions ; =============================================== Func SplitFile() $FileArray=_PathSplit($file,$drive,$directory,$name,$type) $filepath=$drive&$directory $filename=$name&$type EndFunc ; ----------------------------------------------- Func FileOpenFile() FileReadFile() If $dstr Then If @OSTYPE<>"WIN32_WINDOWS" OR StringLen($dstr)<30000 Then FileDisplayFile() Else DisableReturn() MsgBox(0,"","File has been opened but is too large to display on this system.") EnableReturn() WinSetTitle($hWindow,"","Gedit ") $docflag=0 EndIf Else DisableReturn() MsgBox(0,"","Unable to open file") EnableReturn() EndIf EndFunc ; ----------------------------------------------- Func FileReadFile() If $defaultpath<>"" Then $filepath=$defaultpath EndIf If $filepath="" Then $filepath="C:\" EndIf FileChangeDir($filepath) DisableReturn() $file=FileOpenDialog("Open file...",@WorkingDir,"All (*.*)|Text (*.txt)|Html (*.html)",2) EnableReturn() If @error<>1 Then $fileh=FileOpen($file,0) If $fileh<>-1 Then SplitFile() $dstr=FileRead($fileh) If StringInStr($dstr,@CRLF)=0 Then $dstr=StringAddCR($dstr) EndIf $dstr=StringReplace($dstr,@TAB," ") FileClose($fileh) EndIf Else $dstr="" EndIf EndFunc ; ----------------------------------------------- Func FileDisplayFile() If FileSaveChanges() Then GUICtrlSetData($hEdit,$dstr) Send("^{HOME}") WinSetTitle($hWindow,"","Gedit "&$file) $docflag=1 EndIf EndFunc ; ----------------------------------------------- Func FileSaveChanges() Local $response If _GUICtrlEdit_GetModify($hEdit) Then DisableReturn() $response=MsgBox(260,"Warning!","Changes have not been saved. Close the current file anyway?") EnableReturn() GUICtrlSetState($hEdit,$GUI_FOCUS) If $response=7 Then Return 0 Else _GUICtrlEdit_SetModify($hEdit,false) Return 1 EndIf EndIf Return 1 EndFunc ; =============================================== ; search functions ; =============================================== Func SearchFindOpen() DisableReturn() GUISetState(@SW_SHOW,$hFindWindow) GUISetState(@SW_ENABLE,$hFindWindow) GUICtrlSetState($FindCheckbox1,$GUI_UNCHECKED) GUICtrlSetData($FindInput1,"") GUICtrlSetState($FindInput1,$GUI_FOCUS) EndFunc ; ----------------------------------------------- Func SearchFindClose() GUISetState(@SW_HIDE,$hFindWindow) GUISetState(@SW_ENABLE,$hWindow) GUICtrlSetState($hEdit,$GUI_FOCUS) EnableReturn() EndFunc ; ----------------------------------------------- Func SearchFindText() SearchFindClose() Send("^{home}") $fstr=GUICtrlRead($FindInput1) If $fstr Then $flen=StringLen($fstr) If BitAnd(GUICtrlRead($FindCheckbox1),$GUI_CHECKED) Then $case=1 Else $case=0 EndIf $fct=1 SearchFindInstance() Else DisableReturn() MsgBox(0,"","Enter search string") EnableReturn() EndIf EndFunc ; ----------------------------------------------- Func SearchFindInstance() $dstr=GUICtrlRead($hEdit) $d=StringInStr($dstr,$fstr,$case,$fct) If $d>0 Then $d=$d-1 _GUICtrlEdit_SetSel($hEdit,$d,$d+$flen) _GUICtrlEdit_Scroll($hEdit,$SB_SCROLLCARET) $fct=$fct+1 Else $fct=1 Send("^{home}") DisableReturn() MsgBox(0,"","Finished searching file") EnableReturn() EndIf EndFunc ; =============================================== ; idle loop ; =============================================== While 1 Sleep(100) If WinActive($hWindow)=1 AND $winflag=0 Then GUICtrlSetState($hEdit,$GUI_FOCUS) EnableHotKeys() $winflag=1 ElseIf WinActive($hWindow)=0 AND $winflag=1 Then DisableHotKeys() $winflag=0 EndIf WEnd ; ----------------------------------------------- Any help with this would be greatly appreciated! Link to comment Share on other sites More sharing options...
philw Posted February 27, 2008 Author Share Posted February 27, 2008 While messing around, trying to get this script to work, I discovered by chance that if I add MsgBox(0,"","") after line 325, the first occurence of the search term is highlighted as soon as the Message Box window is closed. HOWEVER, if it is added after line 342, the first occurence is highlighted but subsequent occurences are not, although the program does scroll down to display them. In other words, my original problem is completely reversed. Now totally mystified...! Link to comment Share on other sites More sharing options...
philw Posted March 13, 2008 Author Share Posted March 13, 2008 Problem solved. The line GUICtrlSetState($hEdit,$GUI_FOCUS) at the end of the script was unselecting the text previously selected. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now