Sign in to follow this  
Followers 0
philw

Button in child window not responding when While loop is running

13 posts in this topic

I'm having a bit of a problem with a window-closing script. The main function contains a For loop. Under certain conditions it should open another window, then pause until a button in the new window is clicked. Then the child window should close and the main loop continue.

I'm using a While loop to pause the For loop. However, when this runs it seems to deactivate the button in the child window.

The child window is opening OK. And, if I remove the While loop from the main function, the button in the new window closes it. If I leave it in, however, clicking the button does nothing - the child window doesn't close.

Any advice as to where I'm going wrong gratefully received!

Global $d


$hWindow=GUICreate("",750,460,-1,-1,BitOR($WS_OVERLAPPEDWINDOW,$WS_MAXIMIZE))


$hEditWindow=GUICreate("",360,345,-1,-1,BitOR($WS_BORDER,$WS_DISABLED),-1,$hWindow)
$EditButton1=GUICtrlCreateButton("OK",160,290,50,20)
GUICtrlSetOnEvent($EditButton1,"EditAccept")


Func MainFunction()
  For $ct=1 to whatever
    If (test is met) Then
      $d=0
      EditOpen()
      While $d=0
        Sleep(100)
      Wend
    EndIf
    (function continues)
  Next
EndFunc


Func EditOpen()
  GUISetState(@SW_SHOW,$hEditWindow) 
  GUISetState(@SW_ENABLE,$hEditWindow)
EndFunc


Func EditAccept()
  $d=1
  GUISetState(@SW_HIDE,$hEditWindow)
  GUISetState(@SW_DISABLE,$hEditWindow)
  GUISetState(@SW_ENABLE,$hWindow)
EndFunc

Share this post


Link to post
Share on other sites



Take a look at GUISwitch in the help file.

Thanks for the suggestion, Uten. However, as far as I can see the Help file example checks whether the window close button has been clicked, whereas I want to use buttons I have created in the window to trigger scripts that will hide / deactivate the window (among other things). Unfortunately, for some reason these scripts don't seem to be executing when the buttons are clicked.

Share this post


Link to post
Share on other sites

Guess search page is a good place to start to get more samples using GuiSwitch..;)

Hi Uten,

Did as you suggested. However, the examples all seem to be in MessageLoop Mode, whereas my script is in OnEvent Mode.

This is a pared-down version of the problem script (probably should have posted it before). If you open a text file, then select Edit > Check For errors, then click OK twice, a window with the title Remove line break? appears. It's the OK button in this window that's not working. Spent hours staring at the script, but I still can't work out how to use GUISwitch in this context!

Thanks!

#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <ProgressConstants.au3>
#include <ListboxConstants.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <GuiConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiEdit.au3>
#include <String.au3>
#include <Array.au3>
#include <File.au3>
#include <Misc.au3>

; ===============================================
; set mode
; ===============================================

Opt("GUIOnEventMode",1)

; ===============================================
; set up variables
; ===============================================

Dim $darray[10000000]
Dim $select[2]

Global $winflag=1
Global $fileflag
Global $selectall
Global $dstr
Global $tempstr
Global $d
Global $vct
Global $firstline
Global $file=""
Global $drive
Global $directory
Global $name
Global $type
Global $fileh
Global $filepath
Global $filename=""
Global $response
Global $startpos
Global $endpos
Global $lbflag=0

; ===============================================
; set up hotkeys
; ===============================================

EnableHotKeys()

; ===============================================
; create main window
;===============================================

$hWindow=GUICreate("",750,460,-1,-1,BitOR($WS_OVERLAPPEDWINDOW,$WS_MAXIMIZE))
GUISetState(@SW_ENABLE,$hWindow)
WinSetTitle($hWindow,"","Checkit")
GUISetOnEvent($GUI_EVENT_CLOSE,"CloseWindow",$hWindow)

; ===============================================
; create options window
; ==============================================

$hOptionsWindow=GUICreate("Options",280,330,-1,-1,BitOR($WS_BORDER,$WS_DISABLED),-1,$hWindow)
GUISetBkColor(0xEEEEEE)
GUISetState(@SW_ENABLE,$hOptionsWindow)
GUISetState(@SW_HIDE,$hOptionsWindow)

; ===============================================
; create options controls
; ===============================================

$OptionsLabel1=GUICtrlCreateLabel("Remove line breaks:",20,20,300,20)

$OptionsCheckbox1=GUICtrlCreateCheckBox("Manual",20,40,70,20)

$OptionsButton1=GUICtrlCreateButton("OK",140,275,50,20,$BS_DEFPUSHBUTTON)

GUICtrlSetState($OptionsCheckbox1,$GUI_CHECKED)

GUICtrlSetOnEvent($OptionsButton1,"OptionsClose")

; ===============================================
; create text edit window
; ===============================================

$hEditWindow=GUICreate("",360,345,-1,-1,BitOR($WS_BORDER,$WS_DISABLED),-1,$hWindow)
GUISetBkColor(0xEEEEEE)
GUISetState(@SW_ENABLE,$hEditWindow)
GUISetState(@SW_HIDE,$hEditWindow)

; ===============================================
; create text edit controls
; ===============================================

$hEdit1=GUICtrlCreateEdit("",10,40,340,240,BitOR($ES_MULTILINE,$WS_VSCROLL,$ES_WANTRETURN))

$EditButton1=GUICtrlCreateButton("OK",160,290,50,20)

GUICtrlSetOnEvent($EditButton1,"EditAccept")

; ===============================================
; show main window
; ===============================================

GUISetState(@SW_SHOW,$hWindow)

; ===============================================
; create edit control
; ===============================================

$winsize=WinGetClientSize($hWindow)
$winw=$winsize[0]
$winh=$winsize[1]

$hEdit=GUICtrlCreateEdit("",0,0,$winw,$winh-16,BitOR($ES_MULTILINE,$WS_VSCROLL,$ES_WANTRETURN))
If @OSTYPE="WIN32_NT" Then
  GUICtrlSendMsg($hEdit,$EM_SETLIMITTEXT,4000000,0)
Else
  GUICtrlSendMsg($hEdit,$EM_SETLIMITTEXT,65535,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 edit menu
; ===============================================

$editmenu=GUICtrlCreateMenu("&Edit")

$editcheck=GUICtrlCreateMenuitem("Check for errors  ctrl-shift e",$editmenu)
GUICtrlSetOnEvent($editcheck,"OptionsOpen")

; ===============================================
; set up hotkeys
; ===============================================

EnableHotKeys()

; ===============================================
; general functions
; ===============================================

Func EnableHotKeys()
  HotKeySet("^o","FileOpenFile")
  HotKeySet("^+e","OptionsOpen")  
EndFunc

; -----------------------------------------------

Func DisableHotKeys()  
  HotKeySet("^o")
  HotKeySet("^+e")  
EndFunc

; -----------------------------------------------

Func Selected()
  $select=_GUICtrlEdit_GetSel($hEdit)
  If $select[0]<>$select[1] Then
    $startpos=$select[0]
    $endpos=$select[1]
    EditCopy()
    $dstr=ClipGet()
    Return true
  Else
    Return false
  EndIf
EndFunc

; -----------------------------------------------

Func Message($type,$caption,$message)
  $response=MsgBox($type,$caption,$message)
EndFunc

; -----------------------------------------------

Func SplitString($splitstr)
  $darray=StringSplit($splitstr,@CRLF,1)
EndFunc

; -----------------------------------------------

Func CombineString()
  Local $combinestr=_ArrayToString($darray,@CRLF,1,$darray[0])
  Return $combinestr
EndFunc

; -----------------------------------------------

Func CloseWindow()   
  If _GUICtrlEdit_GetModify($hEdit) Then
    Message(260,"Warning","Changes have not been saved. Exit Checkit anyway?")
    GUICtrlSetState($hEdit,$GUI_FOCUS)
    If $response=7 Then
      Return
    EndIf
  EndIf
  Exit
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)<60000 Then
      FileDisplayFile() 
    Else
      Message(0,"","File is too large to display on this system.")
      WinSetTitle($hWindow,"","Checkit   ")
    EndIf
  EndIf
EndFunc

; -----------------------------------------------

Func FileReadFile()
  If $filepath="" Then
    $filepath="C:\"
  EndIf
  FileChangeDir($filepath)
  $file=FileOpenDialog("Open file...",@WorkingDir,"All (*.*)|Html (*.html)|Css(*.css)",2)  
  If @error<>0 Then
    $dstr=""
    Return
  Else
    $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,"  ")
      $dstr=StringRegExpReplace($dstr," +"," ")
      $dstr=StringRegExpReplace($dstr,"(\r\n) ",@CRLF)
      $dstr=StringRegExpReplace($dstr," (\r\n)",@CRLF)
      $dstr=StringRegExpReplace($dstr,"(\r\n){3,}",@CRLF&@CRLF)
      $dstr=StringRegExpReplace($dstr,"\A(\r\n)+","")
      $dstr=StringRegExpReplace($dstr,"(\r\n)+\Z","")
      FileClose($fileh)
    EndIf
  EndIf
EndFunc

; -----------------------------------------------

Func FileDisplayFile()
  If FileSaveChanges() Then
    GUICtrlSetData($hEdit,$dstr)
    WinSetTitle($hWindow,"","Checkit   "&$file)
  EndIf
EndFunc

; -----------------------------------------------

Func FileSaveChanges()
  If _GUICtrlEdit_GetModify($hEdit) Then
    Message(260,"Warning!","Changes have not been saved. Close the current file anyway?")
    GUICtrlSetState($hEdit,$GUI_FOCUS)
    If $response=7 Then
      Return 0
    Else
      _GUICtrlEdit_SetModify($hEdit,false)
      Return 1
    EndIf
  EndIf
  Return 1
EndFunc

; ===============================================
; edit functions
; ===============================================

Func OptionsOpen()
  If GUICtrlRead($hEdit)="" Then
    Return
  EndIf
    $selectall=false
  $response=Selected()
  If $response=false Then
    Message(1,"","No text selected. Search whole file?")
    If $response=1 Then
            $selectall=true
      $select[0]=0
      $dstr=GUICtrlRead($hEdit)
    Else
      Return
    EndIf
  EndIf  
  $firstline=_GUICtrlEdit_LineFromChar($hEdit,$select[0])
  GUISetState(@SW_SHOW,$hOptionsWindow)
EndFunc

; -----------------------------------------------

Func OptionsClose()
  If BitAnd(GUICtrlRead($OptionsCheckbox1),$GUI_CHECKED) Then
    $lbflag=2
  EndIf 
  GUISetState(@SW_HIDE,$hOptionsWindow)
  EditCheck()
EndFunc

; -----------------------------------------------

Func EditCheck()
  SplitString($dstr)
  For $ct=1 to $darray[0]
    $tempstr=$darray[$ct]
    $vct=$firstline+$ct
    If $lbflag<>0 Then
      If $ct<$darray[0] Then
        If StringRegExp($tempstr,"[^.?!]\Z") Then
          If $lbflag=2 Then
            _GUICtrlEdit_LineScroll($hEdit,0,$vct-_GUICtrlEdit_GetFirstVisibleLine($hEdit)-2)
            EditOpen("Remove line break?")
            If $d=1 Then
              $darray[$ct+1]="~#*"
              _GUICtrlEdit_SetModify($hEdit,true)
            EndIf
          EndIf
        EndIf
      EndIf
    EndIf
  Next
  $dstr=CombineString()
    If $selectall Then
        GUICtrlSetData($hEdit,$dstr)
    Else
        _GUICtrlEdit_ReplaceSel($hEdit,$dstr)
        $d=StringLen($dstr)
        _GUICtrlEdit_SetSel($hEdit,$select[0],$select[0]+$d)
    EndIf
EndFunc

; -----------------------------------------------

Func EditOpen($title)
  $d=0
  GUISetState(@SW_SHOW,$hEditWindow) 
  WinSetTitle($hEditWindow,"",$title)
  GUICtrlSetData($hEdit1,$tempstr)
  While $d=0
    Sleep(100)
  Wend
EndFunc

; -----------------------------------------------

Func EditAccept()
  $d=1
  GUISetState(@SW_HIDE,$hEditWindow)
  _GUICtrlEdit_SetSel($hEdit,$select[0],$select[1])
EndFunc

; ===============================================
; idle loop
; ===============================================

While 1
  Sleep(100)
  If WinActive($hWindow)=1 AND $winflag=0 Then  
    EnableHotKeys()
    $winflag=1
  ElseIf WinActive($hWindow)=0 AND $winflag=1 Then
    DisableHotKeys()
    $winflag=0
  EndIf
WEnd

; -----------------------------------------------

Share this post


Link to post
Share on other sites

Did as you suggested. However, the examples all seem to be in MessageLoop Mode, whereas my script is in OnEvent Mode

The difference is just that the script engine calls a function rather than wait for us to call GuiGetMsg() when a message comes along.

Played a bit with the help file samples. Test it and see if this clarify anything or if you can describe your situation by modifying this sample

#include <GUIConstantsEx.au3>
Global $mainwindow
Func testHelpSampleAdvanced()
    Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 
    $mainwindow = GUICreate("Hello World", 200, 100)
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
    GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
    $okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
    GUICtrlSetOnEvent($okbutton, "OKButton")

    $dummywindow = GUICreate("Dummy window for testing ", 200, 100)
    $okbutton2 = GUICtrlCreateButton("OK", 70, 50, 60)
    GUICtrlSetOnEvent($okbutton2, "OKButton")   
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

    GUISwitch($mainwindow)
    GUISetState(@SW_SHOW, $dummywindow)
    GUISetState(@SW_SHOW, $mainwindow)

    While 1
      Sleep(1000)  ; Idle around
    WEnd
EndFunc
Func OKButton()
  ;Note: at this point @GUI_CTRLID would equal $okbutton
  MsgBox(0, "GUI Event", "You pressed OK! @GUI_WINHANDLE:=" & @GUI_WINHANDLE & ",  @GUI_CtrlId:=" & @GUI_CtrlId)
EndFunc

Func CLOSEClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
  ;@GUI_WINHANDLE will be either $mainwindow or $dummywindow
  If @GUI_WINHANDLE = $mainwindow Then 
    MsgBox(0, "GUI Event", "You clicked CLOSE in the main window! Exiting...")
    Exit
  EndIf 
EndFunc
;====================00
testHelpSampleAdvanced()

PS: The code you posted gave me a bit of a scare as it wanted (indicated) it would modify files on my computer.

Share this post


Link to post
Share on other sites

I played around with GUISwitch, but I didn't get anywhere with it either. The problem is resulting in nothing 'listening' for the OK button being pressed. I think the correct way you should fix it is to redesign your For/Next loop... that said, you can get around it this way, its not particularly clean but hey it works.

Func EditOpen($title)
  $d=0
  GUISetState(@SW_SHOW,$hEditWindow) 
  WinSetTitle($hEditWindow,"",$title)
  GUICtrlSetData($hEdit1,$tempstr)
  Opt("GUIOnEventMode",0)
  While $d=0
    $msg = GUIGetMsg()
    If $msg = $EditButton1 Then 
        Opt("GUIOnEventMode",1)
        EditAccept()
        EndIf
    Sleep(100)
  Wend
EndFunc

Like I said it ain't pretty, but this might work out for you. There could also be an easier way to do it but I don't work too much with on event mode.


While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

Have not played much with OnEvent mode in AutoIt. When I started, after the kids went to bed, I got kind of stuck in the challenge you have met philw.

Now see if this sample can help you out.

#include <GUIConstantsEx.au3>
opt("MustDeclareVars",1)
Global $hWin1, $hWin2, $btn1, $btn2
testTwoWindows()
Func dbg($msg, $line =@ScriptLineNumber, $err=@error, $ext=@extended)
    If not @Compiled Then 
        If StringLeft($msg, 1) = "(" Then
            ConsoleWrite($msg & "==>> (" & $line & ") := (" & $err & ")(" & $ext & ") " &  @CRLF )
        Else
            ConsoleWrite("(" & $line & ") := (" & $err & ")(" & $ext & ") " & $msg & @CRLF )
        EndIf
    EndIf
    If $err <> 0 Then SetError($err,$ext)
endFunc
Func OnEvent_btnChild()
    dbg("OnEvent_btnChild::GuiDelete():=" & GUIDelete(@GUI_WINHANDLE))
    ;GuiSwitch($hWin1)
EndFunc
Func OnEvent_btnMaster()
    dbg("OnEvent_btnMaster")
    $hWin2 = CreateChildGui()
    dbg("$hWin2:=" & $hWin2)
    GUISetState(@SW_SHOW, $hWin2)
    
    ;GuiSwitch($hWin2)
    
    ;WinWaitClose($hWin2)
    dbg("OnEvent_btnMaster::GuiSwitch")
    ;GuiSwitch($hWin1)
EndFunc
Func CreateChildGui()
    Local $win=GUICreate("$hWin2",75,75,-1,-1);,BitOR($WS_BORDER,$WS_DISABLED),-1, $hWin1)
    GuiSetOnEvent($GUI_EVENT_CLOSE, "OnEvent_CloseWin2")
    GUICtrlCreateButton("OK", 5,5,40,20)
    GUICtrlSetOnEvent(-1,"OnEvent_btnChild")
    Return $win
EndFunc
Func OnEvent_CloseWin()
    dbg("OnEvent_CloseWin")
    GUIDelete(@GUI_WINHANDLE)
    Exit
EndFunc
Func OnEvent_CloseWin2()
    dbg("OnEvent_CloseWin2")
    GUIDelete(@GUI_WINHANDLE)
    ;GuiSwitch($hWin1)
EndFunc
Func testTwoWindows()
    Opt("GUIOnEventMode", 1)

    $hWin1=GUICreate("$hWin1",100,100,-1,-1);,BitOR($WS_OVERLAPPEDWINDOW,$WS_MAXIMIZE))
    GUICtrlCreateButton("OK",10,10,50,20)
    GUICtrlSetOnEvent(-1,"OnEvent_btnMaster")
    GuiSetOnEvent($GUI_EVENT_CLOSE, "OnEvent_CloseWin")
    GUISetState()

    While 1
        sleep(100)
    Wend
EndFunc

Notice how OnEvent_CloseWin2 is newer called after a GuiDelete($h) call? I find that odd. Maybe we should do a _PostMessage to get it called. Or maybe I have goofed in my code, as I often do..;)

Uten

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I played around with GUISwitch, but I didn't get anywhere with it either. The problem is resulting in nothing 'listening' for the OK button being pressed. I think the correct way you should fix it is to redesign your For/Next loop... that said, you can get around it this way, its not particularly clean but hey it works.

Func EditOpen($title)
  $d=0
  GUISetState(@SW_SHOW,$hEditWindow) 
  WinSetTitle($hEditWindow,"",$title)
  GUICtrlSetData($hEdit1,$tempstr)
  Opt("GUIOnEventMode",0)
  While $d=0
    $msg = GUIGetMsg()
    If $msg = $EditButton1 Then 
        Opt("GUIOnEventMode",1)
        EditAccept()
        EndIf
    Sleep(100)
  Wend
EndFunc

Like I said it ain't pretty, but this might work out for you. There could also be an easier way to do it but I don't work too much with on event mode.

Thanks very much for the suggestion, "someone". Works fine! Seems to be the only way to get the button to respond - at least, the way the code is at the moment.

BTW, when you say redesign the For/Next loop, how would I do that? Do you know where the problem might lie?

Thanks too for your further suggestion, Uten. I'll need to spend a bit of time getting my head round it, when I get home from work.

I don't know where the warning message you mentioned above came from. I'm certainly not tying to design malicious software to take over the world - just a program to clean up text files!

Edited by philw

Share this post


Link to post
Share on other sites

Don't know where that message came from, Uten. I'm certainly not tying to design malicious software to take over the world - just a program to clean up text files!

Did not think you were. It just gave me a rush when one of my files were listed and I was supposed to give a Ok on removing all CRLF's or something like that.. ;)

Happy to see that someone found a working solution for you.

Uten

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Thanks very much for the suggestion, "someone". Works fine! Seems to be the only way to get the button to respond - at least, the way the code is at the moment.

BTW, when you say redesign the For/Next loop, how would I do that? Do you know where the problem might lie?

Like I said I don't use on event that much, but the problem is your for/next loop shouldn't be set on run-away train mode unless you code it to stop, you know what I mean? I tried to code an example out of your script, but I couldn't get it working. Maybe tomorrow. It might not be worth it to go back change your code. Ultimately, if it works thats what matters.

What I would do is code your for loop to only do one step per function call. You have to rework your code for it to work but define $ct beforehand, and maybe on each EditAccept do a $ct += 1. The goal there is to let the script return to the main idle loop to continue 'listening' for GUI messages (in this case from $EditButton1). Not keeping that idea in the back of your head is going to make things harder as your GUIs get more complicated.

Hope this helps!

PS Uten your avatar is messing with my mind. I swear I saw the baby do #1 before Now I've watched it for minutes and nothing.... I don't know....

Edited by someone

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

@philw

I don't know if you need this or have moved on, but others who read this

may benefit from an example to compare to.

I've broken up your Edit loop code into separate functions.

The trick is as 'someone' mentioned to save the

current position of the loop, then return from

the function and wait for an event.

On event restart the loop from the saved position advanced by one.

Edit: corrected some errors in array handling causing dimension range exceeding

#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <ProgressConstants.au3>
#include <ListboxConstants.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <GuiConstantsEx.au3>
#include <EditConstants.au3>
#include <GuiEdit.au3>
#include <String.au3>
#include <Array.au3>
#include <File.au3>
#include <Misc.au3>

; ===============================================
; set mode
; ===============================================

Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)

; ===============================================
; set up variables
; ===============================================

Global $darray[10000000]
Global $select[2]

Global $iLoopCnt = 1
Global $winflag = 1
Global $fileflag
Global $selectall
Global $dstr
Global $tempstr
Global $d
Global $vct
Global $firstline
Global $file = ""
Global $drive
Global $directory
Global $name
Global $type
Global $fileh
Global $filepath
Global $filename = ""
Global $response
Global $startpos
Global $endpos
Global $lbflag = 0
Global $hWindow, $winsize, $winw, $winh, $hEdit, $filemenu, $fileopen, $editmenu, $editcheck
Global $hOptionsWindow, $OptionsLabel1, $OptionsCheckbox1, $OptionsButton1
Global $hEditWindow, $hEdit1, $EditButton1
; ===============================================
; set up hotkeys
; ===============================================

EnableHotKeys()

; ===============================================
; create main window
;===============================================

$hWindow = GUICreate("Checkit", 750, 460, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_MAXIMIZE))
;GUISetState(@SW_ENABLE, $hWindow)
;WinSetTitle($hWindow, "", "Checkit")
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseWindow", $hWindow)

; ===============================================
; create main window edit control
; ===============================================

$winsize = WinGetClientSize($hWindow)
$winw = $winsize[0]
$winh = $winsize[1]

$hEdit = GUICtrlCreateEdit("", 0, 0, $winw, $winh - 16, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_WANTRETURN))
If @OSTYPE = "WIN32_NT" Then
    GUICtrlSendMsg($hEdit, $EM_SETLIMITTEXT, 4000000, 0)
Else
    GUICtrlSendMsg($hEdit, $EM_SETLIMITTEXT, 65535, 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 edit menu
; ===============================================

$editmenu = GUICtrlCreateMenu("&Edit")
$editcheck = GUICtrlCreateMenuItem("Check for errors  ctrl-shift e", $editmenu)
GUICtrlSetOnEvent($editcheck, "OptionsOpen")


; ===============================================
; create options window
; ==============================================

$hOptionsWindow = GUICreate("Options", 280, 330, -1, -1, $WS_BORDER, -1, $hWindow)
GUISetBkColor(0xEEEEEE)
GUISwitch($hOptionsWindow)

; ===============================================
; create options controls
; ===============================================

$OptionsLabel1 = GUICtrlCreateLabel("Remove line breaks:", 20, 20, 300, 20)
$OptionsCheckbox1 = GUICtrlCreateCheckbox("Manual", 20, 40, 70, 20)
$OptionsButton1 = GUICtrlCreateButton("OK", 140, 275, 50, 20, $BS_DEFPUSHBUTTON)

GUICtrlSetState($OptionsCheckbox1, $GUI_CHECKED)
GUICtrlSetOnEvent($OptionsButton1, "EditCheck")

GUISetState(@SW_DISABLE, $hOptionsWindow)
;GUISetState(@SW_ENABLE, $hOptionsWindow)
;GUISetState(@SW_HIDE, $hOptionsWindow)

; ===============================================
; create text edit window
; ===============================================

$hEditWindow = GUICreate("", 360, 345, -1, -1, $WS_BORDER, -1, $hWindow)
GUISetBkColor(0xEEEEEE)
GUISwitch($hEditWindow)

; ===============================================
; create text edit controls
; ===============================================

$hEdit1 = GUICtrlCreateEdit("", 10, 40, 340, 240, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_WANTRETURN))
$EditButton1 = GUICtrlCreateButton("OK", 160, 290, 50, 20)
GUICtrlSetOnEvent($EditButton1, "EditAccept")

GUISetState(@SW_DISABLE, $hEditWindow)
;GUISetState(@SW_ENABLE, $hEditWindow)
;GUISetState(@SW_HIDE, $hEditWindow)

; ===============================================
; show main window
; ===============================================

GUISwitch($hWindow)
GUISetState(@SW_SHOW, $hWindow)

; ===============================================
; set up hotkeys
; ===============================================

EnableHotKeys()

; ===============================================
; general functions
; ===============================================

Func EnableHotKeys()
    HotKeySet("^o", "FileOpenFile")
    HotKeySet("^+e", "OptionsOpen")
    Return
EndFunc   ;==>EnableHotKeys

; -----------------------------------------------

Func DisableHotKeys()
    HotKeySet("^o")
    HotKeySet("^+e")
    Return
EndFunc   ;==>DisableHotKeys

; -----------------------------------------------

Func Selected()
    $select = _GUICtrlEdit_GetSel($hEdit)
    If $select[0] <> $select[1] Then
        $startpos = $select[0]
        $endpos = $select[1]
        ;EditCopy()
        $dstr = ClipGet()
        Return True
    Else
        Return False
    EndIf
EndFunc   ;==>Selected

; -----------------------------------------------

Func Message($type, $caption, $message)
    $response = MsgBox($type, $caption, $message)
    Return
EndFunc   ;==>Message

; -----------------------------------------------

Func SplitString($splitstr)
    $darray = StringSplit($splitstr, @CRLF, 1)
    Return
EndFunc   ;==>SplitString

; -----------------------------------------------

Func CombineString()
    Local $combinestr = _ArrayToString($darray, @CRLF, 1, $darray[0])
    Return $combinestr
EndFunc   ;==>CombineString

; -----------------------------------------------

Func CloseWindow()
    If _GUICtrlEdit_GetModify($hEdit) Then
        Message(260, "Warning", "Changes have not been saved. Exit Checkit anyway?")
        GUICtrlSetState($hEdit, $GUI_FOCUS)
        If $response = 7 Then
            Return
        EndIf
    EndIf
    Exit
EndFunc   ;==>CloseWindow

; ===============================================
; file functions
; ===============================================

Func SplitFile()
    Local $FileArray = _PathSplit($file, $drive, $directory, $name, $type)
    $filepath = $drive & $directory
    $filename = $name & $type
    Return
EndFunc   ;==>SplitFile

; -----------------------------------------------

Func FileOpenFile()
    FileReadFile()
    If $dstr Then
        If @OSTYPE <> "WIN32_WINDOWS" Or StringLen($dstr) < 60000 Then
            FileDisplayFile()
        Else
            Message(0, "", "File is too large to display on this system.")
            WinSetTitle($hWindow, "", "Checkit   ")
        EndIf
    EndIf
    Return
EndFunc   ;==>FileOpenFile

; -----------------------------------------------

Func FileReadFile()
    If $filepath = "" Then
        $filepath = "C:\"
    EndIf
    FileChangeDir($filepath)
    $file = FileOpenDialog("Open file...", @WorkingDir, "All (*.*)|Html (*.html)|Css(*.css)", 2)
    If @error <> 0 Then
        $dstr = ""
        Return
    Else
        $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, "  ")
            $dstr = StringRegExpReplace($dstr, " +", " ")
            $dstr = StringRegExpReplace($dstr, "(\r\n) ", @CRLF)
            $dstr = StringRegExpReplace($dstr, " (\r\n)", @CRLF)
            $dstr = StringRegExpReplace($dstr, "(\r\n){3,}", @CRLF & @CRLF)
            $dstr = StringRegExpReplace($dstr, "\A(\r\n)+", "")
            $dstr = StringRegExpReplace($dstr, "(\r\n)+\Z", "")
            FileClose($fileh)
        EndIf
    EndIf
    Return
EndFunc   ;==>FileReadFile

; -----------------------------------------------

Func FileDisplayFile()
    If FileSaveChanges() Then
        GUICtrlSetData($hEdit, $dstr)
        WinSetTitle($hWindow, "", "Checkit   " & $file)
    EndIf
    Return
EndFunc   ;==>FileDisplayFile

; -----------------------------------------------

Func FileSaveChanges()
    If _GUICtrlEdit_GetModify($hEdit) Then
        Message(260, "Warning!", "Changes have not been saved. Close the current file anyway?")
        GUICtrlSetState($hEdit, $GUI_FOCUS)
        If $response = 7 Then
            Return 0
        Else
            _GUICtrlEdit_SetModify($hEdit, False)
            Return 1
        EndIf
    EndIf
    Return 1
EndFunc   ;==>FileSaveChanges

; ===============================================
; edit functions
; ===============================================

Func OptionsOpen()
    If GUICtrlRead($hEdit) = "" Then
        Return
    EndIf
    $selectall = False
    $response = Selected()
    If $response = False Then
        Message(1, "", "No text selected. Search whole file?")
        If $response = 1 Then
            $selectall = True
            $select[0] = 0
            $dstr = GUICtrlRead($hEdit)
        Else
            Return
        EndIf
    EndIf
    $firstline = _GUICtrlEdit_LineFromChar($hEdit, $select[0])
    GUISetState(@SW_ENABLE, $hOptionsWindow)
    GUISetState(@SW_SHOW, $hOptionsWindow)
    GUISwitch($hOptionsWindow)
    Return
EndFunc   ;==>OptionsOpen

; -----------------------------------------------

Func EditCheck() ; hide Options Gui and show Edit GUI
    If BitAND(GUICtrlRead($OptionsCheckbox1), $GUI_CHECKED) Then
        $lbflag = 2
    EndIf
    GUISetState(@SW_DISABLE, $hOptionsWindow)
    GUISetState(@SW_HIDE, $hOptionsWindow)
    GUISwitch($hEditWindow)
    WinSetTitle($hEditWindow, "", "Remove line break?")
    GUISetState(@SW_ENABLE, $hEditWindow)
    GUISetState(@SW_SHOW, $hEditWindow)
    SplitString($dstr)
    Return _EditStep() ; run EditStep loop
EndFunc   ;==>EditCheck

Func EditAccept()
    _GUICtrlEdit_SetSel($hEdit, $select[0], $select[1])
    $darray[$iLoopCnt] = "~#*"
    _GUICtrlEdit_SetModify($hEdit, True)
    $iLoopCnt += 1 ; increment loop start position
    If $iLoopCnt = $darray[0] Then Return _EditExit() ; ** exit before processing last line
    Return _EditStep()
EndFunc   ;==>EditAccept

Func _EditStep()
    For $ct = $iLoopCnt To $darray[0]
        $tempstr = $darray[$ct]
        $vct = $firstline + $ct
        If $lbflag <> 0 Then
            If $ct < $darray[0] Then
                If StringRegExp($tempstr, "[^.?!]\Z") Then
                    If $lbflag = 2 Then
                        _GUICtrlEdit_LineScroll($hEdit, 0, $vct - _GUICtrlEdit_GetFirstVisibleLine($hEdit) - 2)
                        GUICtrlSetData($hEdit1, $tempstr)
                        ConsoleWrite('-$iLoopCnt = ' & $iLoopCnt &@crlf)
                        $iLoopCnt = $ct ; save current position in loop to $iLoopCnt
                        Return ; return to waiting for OK button click (EditAccept())
                    EndIf
                EndIf
            Else
                Return _EditExit() ; ** exit before processing last line
            EndIf
        EndIf
    Next
    Return
EndFunc

Func _EditExit() ; Exit editor, hide Edit Gui, update main Edit control, switch to Main Gui
    $dstr = CombineString()
    If $selectall Then
        GUICtrlSetData($hEdit, $dstr)
    Else
        _GUICtrlEdit_ReplaceSel($hEdit, $dstr)
        $d = StringLen($dstr)
        _GUICtrlEdit_SetSel($hEdit, $select[0], $select[0] + $d)
    EndIf
    GUISetState(@SW_DISABLE, $hEditWindow)
    GUISetState(@SW_HIDE, $hEditWindow)
    GUISwitch($hWindow)
    Return
EndFunc

; -----------------------------------------------

; ===============================================
; idle loop
; ===============================================

While 1
    Sleep(100)
    If WinActive($hWindow) = 1 And $winflag = 0 Then
        EnableHotKeys()
        $winflag = 1
    ElseIf WinActive($hWindow) = 0 And $winflag = 1 Then
        DisableHotKeys()
        $winflag = 0
    EndIf
WEnd
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

Thanks to everyone for their help on this one. It was very much appreciated!

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0