Jump to content
Sign in to follow this  
gcue

smartest way to store journal entries?

Recommended Posts

gcue

hello.

i have a gui with a listview box each line item would be journal entry

first column = preview of the journal entry (first 100 chars or whatever)

second column = modified date

whats the smartest way to store each of these entries to later be able to read them?

(keep in mind that an entry can have @CRLFs also)

seperate files for each entry would seem a bit overkill, possibility to save each entry into one file and later be able to quickly reference each entry?

hope im making sense..

Edited by gcue

Share this post


Link to post
Share on other sites
Dolemite50

I like the individual file approach because it's convenient to easily identify the entry by filename when populating a list or just browsing the dir.

Otherwise you can use an ini, here is a fine example to the tune of Rob Saunder's QuickScript:

#cs
****************************************************************************
Author:         Rob Saunders - www.therks.com
Language:       AutoIt v3.2.13.8 - www.autoitscript.com
Description:
    A simple text window that will run and/or compile AutoIt3 code.
    Using the compiled version of this script you should be able to run and
    compile AutoIt code on machines that do not even have AutoIt installed.
    
    Program settings are stored, by default, in the current user's application data
    folder (ie: C:\Documents and Settings\Rob\Application Data\quickscript.ini).
    HOWEVER, if you create a quickscript.ini in the same folder as the program
    it will use that file instead when saving/loading program settings.
    
    To run the code in the content area, choose Options > Run, or press Ctrl+R.
    To stop code execution (in the case of an infinite loop, for example) press Ctrl+Q
    or try to close the QuickScript window (note that unless you used #NoTrayIcon, or
    otherwise hid the tray icon, the icon will remain in your system tray, just like
    every other program that hasn't closed properly, just mouse over the icon and it
    should disappear).
    
    Snippets are chunks of code that you can store for quick use, it's a good place
    to store a piece of code used in common tests like a GUI template or an
    infinite loop. A few snippets come pre-loaded. You can add your own by using
    the Snippet menu, or manually editing the settings file.
    
    For more information about AutoIt check online at http://www.autoitscript.com
    
    Important Notes:
        A)  To use include files, when QuickScript is compiled, they must be
            placed in an "include" subfolder where the .exe is stored.
            Example:
                EXE path = D:\Files\QuickScript.exe
                String.au3 path MUST = D:\Files\Include\String.au3
                or else #include <String.au3> will not work.
                #include "D:\Files\String.au3" will work though, obviously.
        B)  If you wish to compile this script yourself you may need to alter the lines
            that pertain to the location of the AutoIt3 compiler files. Look for the
            function "_CompilerGetFiles" and edit the FileInstall lines appropriately.
        C)  The include files (String.au3, File.au3, GUIConstants.au3, etc..) are not
            installed by this script, whether it's compiled or not. Just too many to
            add. You will have to get those yourself.
    
    Version History:
        1.0)    First numbered version.
****************************************************************************
#ce


#NoTrayIcon
#include <Constants.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ScrollBarConstants.au3>
#include <GUIEdit.au3>
#include <File.au3>
#include <Misc.au3>

Opt('GUIOnEventMode', 1)
Opt('GUICloseOnESC', 0)


Global Const $sAppTitle = 'QuickScript'
Global Const $sAppVersion = '1.0'
Global $sSingleKey = 'AUTOIT3APPTRACK' & $sAppTitle & $sAppVersion
_CheckDoubleRun($sSingleKey)

#region - Variable setup
Global Const $EDIT_MAX_LIMIT = 2^31-1 ; 0x7fffffff, 2147483647

Global $sMainTitle = $sAppTitle & ' v' & $sAppVersion
If @Compiled Then
    $sMainTitle &= ' (Compiled v' & @AutoItVersion & ')'
Else
    $sMainTitle &= ' (AutoIt v' & @AutoItVersion & ' ยป ' & @AutoItExe & ')'
EndIf

Global $sIniFile = @AppDataDir & '\' & $sAppTitle & '.ini'
If FileExists(@ScriptDir & '\' & $sAppTitle & '.ini') Then $sIniFile = @ScriptDir & '\' & $sAppTitle & '.ini'

Global $sTempFilePath = @TempDir & '\tmp.au3'

Global $sLastFile = IniRead($sIniFile, 'Settings', 'LastFile', '')
If Not FileExists($sLastFile) Then $sLastFile = $sTempFilePath
Global $sDefaultContents = FileRead($sTempFilePath)

Global $sEditorCmd = IniRead($sIniFile, 'Settings', 'Editor', '"' & @SystemDir & '\Notepad.exe" "%1"')
IniWrite($sIniFile, 'Settings', 'Editor', '"' & $sEditorCmd & '"')

Global $aSaveWinPos, $aLoadWinPos = StringSplit(IniRead($sIniFile, 'Settings', 'LastWinPos', '-1|-1|-1|-1'), '|')

Enum $FONT_NAME = 1, $FONT_SIZE, $FONT_WEIGHT, $FONT_ATTS
Global $aContentFont = StringSplit(IniRead($sIniFile, 'Settings', 'ContentFont', 'Courier New|9|400|0'), '|') ; Name|Size|Weight|Attributes
ReDim $aContentFont[5]
Global $aConsoleFont = StringSplit(IniRead($sIniFile, 'Settings', 'ConsoleFont', 'Courier New|8|400|0'), '|')
ReDim $aConsoleFont[5]

Global $iSizerHeight = 5
Global $iSizerTopLimit = 40
Global $iSizerBottomLimit = 35 + 20

Global $aMinMaxSize_Main[4]
    $aMinMaxSize_Main[0] = @DesktopWidth
    $aMinMaxSize_Main[1] = @DesktopHeight
    $aMinMaxSize_Main[2] = 200
    $aMinMaxSize_Main[3] = 150

Global $iRunTimer
Global $iScriptPID
Global $bConsoleShow = False

Global $aSnippetsData[1], $aSnippetsMenuItems[1]
Global $mi_SnippetSep, $mi_SnippetRefresh

Global $aEditSel[2], $aEditSelMem[2] = [ 1, -1 ]
Global $aTabStops4Letter[1] = [ 4*4 ]

;~ NOT Constants - These need to be alterable
Global $IGNORE_WM_MOVE = False
Global $IGNORE_WM_PAINT = False

Opt('OnExitFunc', '_OnAutoItExit')
#endregion

#region - GUI construction
$hGUIMain = GUICreate($sMainTitle, 400, 320, -1, -1, $WS_OVERLAPPEDWINDOW, $WS_EX_ACCEPTFILES)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_Generic')

    #region - Accelerator dummies   
    $dm_AccelTabShift = GUICtrlCreateDummy()
        GUICtrlSetOnEvent(-1, '_Generic')
    $dm_AccelTabCtrlShift = GUICtrlCreateDummy()
        GUICtrlSetOnEvent(-1, '_Generic')
    $dm_AccelTabCtrl = GUICtrlCreateDummy()
        GUICtrlSetOnEvent(-1, '_Generic')
    $dm_AccelTab = GUICtrlCreateDummy()
        GUICtrlSetOnEvent(-1, '_Generic')
    $dm_AccelEscape = GUICtrlCreateDummy()
        GUICtrlSetOnEvent(-1, '_Generic')
    #endregion  
    $dm_Stop = GUICtrlCreateDummy()
        GUICtrlSetOnEvent(-1, '_ScriptRun')
    #Region - Menu
    $me_File = GUICtrlCreateMenu('&File')
        $mi_FileOpen = GUICtrlCreateMenuItem('&Open file    Ctrl+O', $me_File)
            GUICtrlSetOnEvent(-1, '_Generic')
        $mi_FileSave = GUICtrlCreateMenuItem('&Save as *.au3    Ctrl+S', $me_File)
            GUICtrlSetOnEvent(-1, '_ScriptSave')
        $mi_FileCompile = GUICtrlCreateMenuItem('Save &as *.exe/*.a3x   Ctrl+Alt+S', $me_File)
            GUICtrlSetOnEvent(-1, '_ScriptCompile')
        $mi_FileSettings = GUICtrlCreateMenuItem('&Edit settings file   Ctrl+E', $me_File)
            GUICtrlSetOnEvent(-1, '_Generic')
        GUICtrlCreateMenuItem('', $me_File)
        $mi_FileExit = GUICtrlCreateMenuItem('E&xit', $me_File)
            GUICtrlSetOnEvent(-1, '_Generic')
    $me_Snippets = GUICtrlCreateMenu('Sn&ippets')
        $mi_SnippetAdd = GUICtrlCreateMenuItem('&Add snippet    Ctrl+Shift+A', $me_Snippets)
            GUICtrlSetOnEvent(-1, '_SnippetsHandler')
        $mi_SnippetRemove = GUICtrlCreateMenuItem('Re&move snippet  Ctrl+Shift+R', $me_Snippets)
            GUICtrlSetOnEvent(-1, '_SnippetsHandler')
        $mi_SnippetRefresh = GUICtrlCreateMenuItem('&Refresh snippet list', $me_Snippets)
            GUICtrlSetOnEvent($mi_SnippetRefresh, '_SnippetsHandler')
        $me_SnippetInsert = GUICtrlCreateMenu('&Insert snippet', $me_Snippets)
    $me_Options = GUICtrlCreateMenu('&Options')
        $mi_OptionsRun = GUICtrlCreateMenuItem('&Run    Ctrl+R', $me_Options)
            GUICtrlSetOnEvent(-1, '_ScriptRun')
        $mi_OptionsStop = GUICtrlCreateMenuItem('&Stop Running  Ctrl+Q', $me_Options)
            GUICtrlSetState(-1, $GUI_DISABLE)
            GUICtrlSetOnEvent(-1, '_ScriptRun')
        $mi_OptionsSelectAll = GUICtrlCreateMenuItem('Select &All   Ctrl+A', $me_Options)
            GUICtrlSetOnEvent(-1, '_Generic')
        $mi_OptionsConsole = GUICtrlCreateMenuItem('Toggle &Console Alt+C', $me_Options)
            GUICtrlSetOnEvent(-1, '_Generic')
        $mi_OptionsGoTo = GUICtrlCreateMenuItem('Go To Line...  Ctrl+G', $me_Options)
            GUICtrlSetOnEvent(-1, '_Generic')
        $mi_OptionsCompilerExport = GUICtrlCreateMenuItem('E&xport Compiler Files', $me_Options)
            GUICtrlSetOnEvent(-1, '_CompilerExport')
            If Not @Compiled Then GUICtrlDelete($mi_OptionsCompilerExport)
        $mi_OptionsContentFont = GUICtrlCreateMenuItem('Content &Font...', $me_Options)
            GUICtrlSetOnEvent(-1, '_FontSettings')
        $mi_OptionsConsoleFont = GUICtrlCreateMenuItem('Console &Font...', $me_Options)
            GUICtrlSetOnEvent(-1, '_FontSettings')
        
    $me_Help = GUICtrlCreateMenu('&Help')
        $mi_HelpReadme = GUICtrlCreateMenuItem('View &ReadMe', $me_Help)
            GUICtrlSetOnEvent(-1, '_HelpMenu')
        $mi_HelpSource = GUICtrlCreateMenuItem('View &Source', $me_Help)
            GUICtrlSetOnEvent(-1, '_HelpMenu')
        $mi_HelpAbout = GUICtrlCreateMenuItem('About ' & $sAppTitle, $me_Help)
            GUICtrlSetOnEvent(-1, '_HelpMenu')
    #endregion <- Menu
    
$lb_ContentFrame = GUICtrlCreateLabel('ContentFrame', 0, 0, 400, 280, $SS_SUNKEN)
    GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
    GUICtrlSetState(-1, $GUI_HIDE)

$lb_SizerFrame = GUICtrlCreateLabel('SizerFrame', 0, 180, 400, $iSizerHeight, $SS_SUNKEN)
    GUICtrlSetResizing(-1, $GUI_DOCKSTATEBAR+$GUI_DOCKRIGHT+$GUI_DOCKLEFT)
    GUICtrlSetState(-1, $GUI_HIDE)

$lb_ConsoleFrame = GUICtrlCreateLabel('ConsoleFrame', 0, 185, 400, 95, $SS_SUNKEN)
    GUICtrlSetResizing(-1, $GUI_DOCKSTATEBAR+$GUI_DOCKRIGHT+$GUI_DOCKLEFT)
    GUICtrlSetState(-1, $GUI_HIDE)

$lb_Status = GUICtrlCreateLabel('', 0, 280, 400, 20, BitOR($SS_CENTERIMAGE, $SS_SUNKEN))
    GUICtrlSetResizing(-1, $GUI_DOCKSTATEBAR+$GUI_DOCKRIGHT+$GUI_DOCKLEFT)
#endregion

#region - Sub Frames
$hGUIFrameTop = GUICreate('ContentFrameWin', 100, 100, 0, 0, $WS_CHILD, Default, $hGUIMain)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_Generic')
    GUISetOnEvent($GUI_EVENT_DROPPED, '_Generic')
    $ed_Lines = GUICtrlCreateEdit('1', 0, 0, 50, 100, BitOR($ES_READONLY, $ES_RIGHT))
        GUICtrlSetResizing(-1, BitXOR($GUI_DOCKBORDERS, $GUI_DOCKRIGHT, $GUI_DOCKWIDTH))
        GUICtrlSetFont(-1, $aContentFont[$FONT_SIZE], $aContentFont[$FONT_WEIGHT], $aContentFont[$FONT_ATTS], $aContentFont[$FONT_NAME])
        _GUICtrlEdit_SetLimitText($ed_Lines, $EDIT_MAX_LIMIT)
    $ed_Content = GUICtrlCreateEdit($sDefaultContents, 50, 0, 50, 100)
        GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
        GUICtrlSetState(-1, $GUI_DROPACCEPTED)
        GUICtrlSetFont(-1, $aContentFont[$FONT_SIZE], $aContentFont[$FONT_WEIGHT], $aContentFont[$FONT_ATTS], $aContentFont[$FONT_NAME])
        _GUICtrlEdit_SetabStops($ed_Content, $aTabStops4Letter)
        _GUICtrlEdit_SetMargins($ed_Content, $EC_LEFTMARGIN, 10)
        _GUICtrlEdit_SetLimitText($ed_Content, $EDIT_MAX_LIMIT)
GUISetState()

$hGUIFrameSize = GUICreate('SizerFrameWin', 400, $iSizerHeight, 0, 180, $WS_CHILD, Default, $hGUIMain)
    GUISetCursor(11, 1)
    GUICtrlCreateLabel('', -5, 0, 410, $iSizerHeight, Default, $GUI_WS_EX_PARENTDRAG)
        GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
GUISetState()

$hGUIFrameBot = GUICreate('ConsoleFrameWin', 400, 90, 0, 190, $WS_CHILD, Default, $hGUIMain)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_Generic')
    $ed_Console = GUICtrlCreateEdit('', 0, 0, 400, 90)
        GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
        GUICtrlSetFont(-1, $aConsoleFont[$FONT_SIZE], $aConsoleFont[$FONT_WEIGHT], $aConsoleFont[$FONT_ATTS], $aConsoleFont[$FONT_NAME])
        _GUICtrlEdit_SetabStops($ed_Console, $aTabStops4Letter)
        _GUICtrlEdit_SetMargins($ed_Console, $EC_LEFTMARGIN, 10)
        _GUICtrlEdit_SetLimitText($ed_Console, 2^31-1) ; 0x7fffffff, 2147483647
#endregion <- Sub frames

#region - Compile wait window
$hGUICompile = GUICreate('Compiling...', 200, 75, Default, Default, BitOR($WS_POPUP, $WS_BORDER), Default, $hGUIMain)
$lb_CompileStatus = GUICtrlCreateLabel('', 0, 0, 200, 75, BitOR($SS_CENTERIMAGE, $SS_CENTER))
#endregion <- Compile wait window

GUIRegisterMsg($WM_GETMINMAXINFO, 'WM_GETMINMAXINFO')
GUIRegisterMsg($WM_SIZE, 'WM_SIZE')
GUIRegisterMsg($WM_MOVE, 'WM_MOVE')
GUIRegisterMsg($WM_PAINT, 'WM_PAINT')
GUIRegisterMsg($WM_CTLCOLOREDIT, 'WM_CTLCOLOREDIT')

_SnippetLoad()
GUISetState(@SW_SHOW, $hGUIMain)
If $aLoadWinPos[0] = 4 And $aLoadWinPos[1] <> -1 And $aLoadWinPos[2] <> -1 Then
    WinMove($hGUIMain, '', $aLoadWinPos[1], $aLoadWinPos[2], $aLoadWinPos[3], $aLoadWinPos[4])
EndIf

Global $aSaveWinPos = WinGetPos($hGUIMain)
Global $aCtrlGroupRun[3] = [ $me_File, $me_Snippets, $mi_OptionsSelectAll ]
Global $aCtrlGroupCompile[6] = [ $ed_Content, $me_File, $me_Snippets, $mi_OptionsSelectAll, $mi_OptionsRun, $mi_OptionsConsole ]

Global $aGUIAccelerators[16][2] = [ _
    ['^o', $mi_FileOpen], _
    ['^s', $mi_FileSave], _
    ['^e', $mi_FileSettings], _
    ['^a', $mi_OptionsSelectAll], _
    ['^r', $mi_OptionsRun], _
    ['^q', $mi_OptionsStop], _
    ['^g', $mi_OptionsGoTo], _
    ['!c', $mi_OptionsConsole], _
    ['^!s', $mi_FileCompile], _
    ['^+a', $mi_SnippetAdd], _
    ['^+r', $mi_SnippetRemove], _
    ['{esc}', $dm_AccelEscape], _
    ['{tab}', $dm_AccelTab], _
    ['+{tab}', $dm_AccelTabShift], _
    ['^{tab}', $dm_AccelTabCtrl], _
    ['^+{tab}', $dm_AccelTabCtrlShift]]
GUISetAccelerators($aGUIAccelerators, $hGUIMain)

ControlFocus($hGUIMain, '', $ed_Content)
_GUICtrlEdit_SetSel($ed_Content, 0, 0)

#region - Program idle
While 1
    If AutoItWinGetTitle() == 'SHOW' Then
        AutoItWinSetTitle($sSingleKey)
        WinActivate($hGUIMain)
    EndIf
    
    If $iScriptPID Then
        $iStdoutRead = StdoutRead($iScriptPID)
        If Not @error Then
            If @extended Then
                If Not $bConsoleShow Then
                    _ToggleConsole($bConsoleShow)
                EndIf
                _GUICtrlEdit_AppendText($ed_Console, StringRegExpReplace($iStdoutRead, '([^\r])\n', '\1' & @CRLF))
            EndIf
        Else
            $iScriptPID = False
            _GUICtrlEdit_AppendText($ed_Console, @CRLF & 'Run time: ' & Round(TimerDiff($iRunTimer) / 1000, 3) & 's')
            GUICtrlSetState($mi_OptionsRun, $GUI_ENABLE)
            GUICtrlSetState($mi_OptionsStop, $GUI_DISABLE)
            WinSetTitle($hGUIMain, '', $sMainTitle)
            ControlFocus($hGUIFrameTop, '', $ed_Content)
        EndIf
    EndIf
        
    $aEditSel = _GUICtrlEdit_GetSel($ed_Content)
    If $aEditSel[0] <> $aEditSelMem[0] Or $aEditSel[1] <> $aEditSelMem[1] Then
        $sContent = GUICtrlRead($ed_Content)
        If StringRegExp($sContent, '[^\r]\n') Then
            $sContent = StringStripCR($sContent)
            $sContent = StringAddCR($sContent)
            GUICtrlSetData($ed_Content, $sContent)
        EndIf

        $iLineFromChar = _GUICtrlEdit_LineFromChar($ed_Content, -1)
        $iCharacter = $aEditSel[0] - _GUICtrlEdit_LineIndex($ed_Content, $iLineFromChar)
        $sStatusLine = StringFormat('[ Line: %d/%d ][ Char: %d ][ Selection: %d/%d chars ]', _
            $iLineFromChar + 1, _
            _GUICtrlEdit_GetLineCount($ed_Content), _
            $iCharacter+1, _
            Abs($aEditSel[0] - $aEditSel[1]), _
            _GUICtrlEdit_GetTextLen($ed_Content))
        GUICtrlSetData($lb_Status, $sStatusLine)

        $aEditSelMem = $aEditSel
    EndIf
    Sleep(10)
WEnd
#endregion

;~ GUI Event Functions -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=>

Func _Generic()
    Switch @GUI_CtrlId
        Case $mi_OptionsGoTo
            Local $sGoTo = InputBox('Go to', 'Enter the line and character number separated by non-numbers (character is optional):' & @LF & _
                '(Example: "25" or "12,21" or "32 19", etc)', '', '', 250, 150, Default, Default, Default, $hGUIMain)
            If $sGoTo <> '' Then
                Local $aMatches = StringRegExp($sGoTo, '([0-9]+)(?:[^0-9]+([0-9]+))?', 1)
                If Not @error Then
                    If UBound($aMatches) > 1 Then
                        _GoTo($aMatches[0], $aMatches[1])
                    Else
                        _GoTo($aMatches[0])
                    EndIf
                EndIf
            EndIf
        Case $mi_FileOpen
            $sFileOpen = FileOpenDialog('Open', _PathSplitWrap(IniRead($sIniFile, 'Settings', 'LastFile', '')), 'AutoIt (*.au3)|All files (*.*)', 1, '', $hGUIMain)
            If Not @error Then
                _LoadFileContents($sFileOpen)
                If Not @error Then
                    IniWrite($sIniFile, 'Settings', 'LastFile', $sFileOpen)
                EndIf
            EndIf
        Case $dm_AccelTab, $dm_AccelTabShift
            Local $hFocusCtrl = _GUIGetFocus($hGUIMain)
            If $hFocusCtrl = $ed_Content OR $hFocusCtrl = $ed_Console Then
                _GUICtrlEdit_BeginUpdate($ed_Content)
                $aEditSel = _GUICtrlEdit_GetSel($ed_Content)
                $aEditSel[0] = _GUICtrlEdit_LineFromChar($ed_Content, $aEditSel[0])
                $aEditSel[1] = _GUICtrlEdit_LineFromChar($ed_Content, $aEditSel[1])
                If $aEditSel[0] <> $aEditSel[1] Then
                    $iLineLen = _GUICtrlEdit_LineLength($ed_Content, $aEditSel[1])
                    $aEditSel[0] = _GUICtrlEdit_LineIndex ($ed_Content, $aEditSel[0])
                    $aEditSel[1] = _GUICtrlEdit_LineIndex ($ed_Content, $aEditSel[1]) + $iLineLen
                    _GUICtrlEdit_SetSel($ed_Content, $aEditSel[0], $aEditSel[1])
                    $sSelText = StringMid(_GUICtrlEdit_GetText($ed_Content), $aEditSel[0]+1, ($aEditSel[1]-$aEditSel[0]))
                    If @GUI_CtrlId = $dm_AccelTab Then
                        $sNewText = StringRegExpReplace($sSelText, '(?m)^(.*)', @TAB & '\1')
                    Else
                        $sNewText = StringRegExpReplace($sSelText, '(?m)^\t(.*)', '\1')
                    EndIf
                    _GUICtrlEdit_ReplaceSel($ed_Content, $sNewText, True)
                    _GUICtrlEdit_SetSel($ed_Content, $aEditSel[0], $aEditSel[0]+StringLen($sNewText))
                ElseIf @GUI_CtrlId = $dm_AccelTab Then
                    _GUICtrlEdit_ReplaceSel($ed_Content, @TAB, True)
                EndIf
                _GUICtrlEdit_EndUpdate($ed_Content)
            EndIf
        Case $dm_AccelTabCtrlShift, $dm_AccelTabCtrl
            If $bConsoleShow Then
                Local $hFocusCtrl = _GUIGetFocus($hGUIMain)
                If $hFocusCtrl = $ed_Console Then
                    ControlFocus($hGUIMain, '', $ed_Content)
                Else
                    ControlFocus($hGUIMain, '', $ed_Console)
                EndIf
            EndIf
        Case $mi_OptionsSelectAll
            Local $hFocusCtrl = _GUIGetFocus($hGUIMain)
            If $hFocusCtrl = $ed_Console Or $hFocusCtrl = $ed_Content Then _GUICtrlEdit_SetSel($hFocusCtrl, 0, -1)
        Case $mi_OptionsConsole
            _ToggleConsole($bConsoleShow)
        Case $mi_FileSettings
            _RunEditor($sIniFile)
        Case $GUI_EVENT_DROPPED
            $sFileOpen = StringStripWS(@GUI_DragFile, 3)
            _LoadFileContents($sFileOpen)
            If Not @error Then
                IniWrite($sIniFile, 'Settings', 'LastFile', $sFileOpen)
            EndIf
        Case $GUI_EVENT_CLOSE, $mi_FileExit, $dm_AccelEscape
            If $iScriptPID Then
                GUICtrlSendToDummy($dm_Stop, 1)
            Else
                If @GUI_CtrlId = $dm_AccelEscape Then
                    If MsgBox(0x42024, 'Confirm', 'Exit ' & $sAppTitle & '?') = 6 Then Exit 1
                Else
                    Exit 2
                EndIf
            EndIf
    EndSwitch
EndFunc

Func _FontSettings()
    Local $iItalic, $iUnderline, $iStrike
    Local $aFontSett, $sFontSett
    If @GUI_CtrlId = $mi_OptionsContentFont Then
        $aFontSett = $aContentFont
    Else
        $aFontSett = $aConsoleFont
    EndIf
    
    $iItalic = BitAND($aFontSett[$FONT_ATTS], 2)
    $iUnderline = BitAND($aFontSett[$FONT_ATTS], 4)
    $iStrike = BitAND($aFontSett[$FONT_ATTS], 8)
    $aFontSett = _ChooseFont($aFontSett[$FONT_NAME], $aFontSett[$FONT_SIZE], 0, _
        $aFontSett[$FONT_WEIGHT], $iItalic, $iUnderline, $iStrike, $hGUIMain)
    If Not IsArray($aFontSett) Then Return 0
    
    $sFontSett = $aFontSett[2] & '|' & $aFontSett[3] & '|' & $aFontSett[4] & '|' & $aFontSett[1]
    
    If @GUI_CtrlId = $mi_OptionsContentFont Then
        IniWrite($sIniFile, 'Settings', 'ContentFont', $sFontSett)
        $aContentFont = StringSplit($sFontSett, '|')
        GUICtrlSetFont($ed_Lines, $aContentFont[$FONT_SIZE], $aContentFont[$FONT_WEIGHT], $aContentFont[$FONT_ATTS], $aContentFont[$FONT_NAME])
        GUICtrlSetFont($ed_Content, $aContentFont[$FONT_SIZE], $aContentFont[$FONT_WEIGHT], $aContentFont[$FONT_ATTS], $aContentFont[$FONT_NAME])
        _GUICtrlEdit_SetabStops($ed_Content, $aTabStops4Letter)
        _GUICtrlEdit_SetMargins($ed_Content, $EC_LEFTMARGIN, 10)
    Else
        IniWrite($sIniFile, 'Settings', 'ConsoleFont', $sFontSett)
        $aConsoleFont = StringSplit($sFontSett, '|')
        GUICtrlSetFont($ed_Console, $aConsoleFont[$FONT_SIZE], $aConsoleFont[$FONT_WEIGHT], $aConsoleFont[$FONT_ATTS], $aConsoleFont[$FONT_NAME])
        _GUICtrlEdit_SetabStops($ed_Console, $aTabStops4Letter)
        _GUICtrlEdit_SetMargins($ed_Console, $EC_LEFTMARGIN, 10)
    EndIf
EndFunc

Func _SnippetsHandler()
    Switch @GUI_CtrlId
        Case $mi_SnippetAdd
            $sContent = GUICtrlRead($ed_Content)
            If $sContent <> '' Then
                $sSnippetName = InputBox($sAppTitle, 'Enter a name for the snippet you are saving.' & @LF & _
                    '* NOTE: If you use the same name as an existing snippet, it will be overwritten.', '', ' m', _
                    300, 150, Default, Default, Default, $hGUIMain)
                If $sSnippetName <> '' Then
                    $sSnippetContent = _StringToFormat($sContent)
                    IniWrite($sIniFile, 'Snippets', $sSnippetName, $sSnippetContent)
                EndIf
                _SnippetLoad()
            Else
                MsgBox(0x2030, 'Notice', 'The content area is empty.')
            EndIf
        Case $mi_SnippetRemove
            $sSnippetName = InputBox($sAppTitle, 'Enter the name of the snippet you wish to remove.' & @LF & _
                '* NOTE: This operation cannot be undone.', '', ' m', 300, 130, Default, Default, Default, $hGUIMain)
            If $sSnippetName <> '' Then
                IniDelete($sIniFile, 'Snippets', $sSnippetName)
            EndIf
            _SnippetLoad()
        Case $mi_SnippetRefresh
            _SnippetLoad()
        Case $aSnippetsMenuItems[1] to $aSnippetsMenuItems[$aSnippetsData[0][0]]
            For $i = 1 to $aSnippetsData[0][0]
                If @GUI_CtrlId = $aSnippetsMenuItems[$i] Then
                    _GUICtrlEdit_ReplaceSel($ed_Content, $aSnippetsData[$i][1], True)
                    ExitLoop
                EndIf
            Next
    EndSwitch
EndFunc

Func _ScriptRun()
    If $iScriptPID Then
        If MsgBox(0x42024, 'Confirm', 'Halt the running script?') = 6 Then
            ProcessClose($iScriptPID)
            _GUICtrlEdit_AppendText($ed_Console, 'Script terminated. ')
        EndIf
    Else
        $sContent = GUICtrlRead($ed_Content)
        If $sContent <> '' Then
            $iRunTimer = TimerInit()
            GUICtrlSetData($ed_Console, '')
            
            $hFile = FileOpen($sTempFilePath, 2)
            FileWrite($hFile, $sContent)
            FileClose($hFile)
            
            WinSetTitle($hGUIMain, '', 'Running script...')
            GUICtrlSetState($mi_OptionsRun, $GUI_DISABLE)
            GUICtrlSetState($mi_OptionsStop, $GUI_ENABLE)
            $iScriptPID = Run(FileGetShortName(@AutoItExe) & ' /ErrorStdOut /AutoIt3ExecuteScript ' & FileGetShortName($sTempFilePath), @ScriptDir, @SW_HIDE, $STDOUT_CHILD)
            WinSetTitle($hGUIMain, '', 'Running script... (PID: ' & $iScriptPID & ')')
        EndIf
    EndIf
EndFunc

Func _ScriptSave()
    $sContent = GUICtrlRead($ed_Content)
    If $sContent <> '' Then
        $aFilePath = _PathSplitWrap(IniRead($sIniFile, 'Settings', 'LastFile', ''), 1)
        If Not $aFilePath[0] Then $aFilePath[0] = @ScriptDir
        If Not ($aFilePath[1] & $aFilePath[2]) Then $aFilePath[1] = _UniqueFile($aFilePath[0], 'au3', 'autoit_', 4)
        $sFileName = FileSaveDialog('Save as...', $aFilePath[0], 'AutoIt3 Files (*.au3)', 2+16, $aFilePath[1] & $aFilePath[2], $hGUIMain)
        If Not @error Then
            IniWrite($sIniFile, 'Settings', 'LastFile', $sFileName)
            $hFile = FileOpen($sFileName, 2)
            If $hFile = -1 Then
                MsgBox(16, 'Error', 'Could not open file for saving:' & @LF & $sFileName)
            Else
                FileWrite($hFile, $sContent)
                FileClose($hFile)
                If MsgBox(0x42024, 'Confirm', 'Open with external editor?') = 6 Then
                    If _RunEditor($sFileName) Then
                        If MsgBox(0x42024, 'Confirm', 'Close ' & $sAppTitle & '?') = 6 Then
                            Exit 3
                        EndIf
                    Else
                        MsgBox(0x42030, 'Notice', 'Unable to open external editor.')
                    EndIf
                EndIf
            EndIf
        EndIf       
    EndIf
EndFunc

Func _ScriptCompile()
    $sContent = GUICtrlRead($ed_Content)
    If $sContent <> '' Then
        $aFilePath = _PathSplitWrap(IniRead($sIniFile, 'Settings', 'LastFile', ''), 1)
        If Not $aFilePath[0] Then $aFilePath[0] = @ScriptDir
        If Not $aFilePath[1] Then $aFilePath[1] = _UniqueFile($aFilePath[0], '', 'autoit_', 4)
        $sFileName = FileSaveDialog('Save as...', $aFilePath[0], 'Executables (*.exe)|Encoded script file (*.a3x)', 2+16, $aFilePath[1] & '.exe', $hGUIMain)
        If Not @error Then
            $aFilePath = _PathSplitWrap($sFileName, 1)
            IniWrite($sIniFile, 'Settings', 'LastFile', $aFilePath[0] & $aFilePath[1] & '.au3')
            WinSetTitle($hGUIMain, '', 'Compiling script...')
            GUICtrlSetData($lb_CompileStatus, '')
            GUISetState(@SW_SHOW, $hGUICompile)
            _CompileScript($sContent, $sFileName, $aFilePath[0])
            GUISetState(@SW_HIDE, $hGUICompile)
            WinSetTitle($hGUIMain, '', $sMainTitle)
        EndIf
    EndIf
EndFunc

Func _CompilerExport()
    Local $iMsgBox = MsgBox(0x2144, 'Export Compiler Files', _
        'This will export the AutoIt3 compiler files so you can use them manually.' & @LF & _
        'They will be exported to the folder where ' & $sAppTitle & ' is currently located so' & @LF & _
        'be sure that folder is writable.' & @LF & _
        'Also, if ' & $sAppTitle & ' detects the files in it''s directory when you choose' & @LF & _
        'File > Save as *.exe/*.a3x then it will use those files. This means you can compile' & @LF & _
        'using include files if they are placed in this same folder.' & @LF & 'Continue?')
    If $iMsgBox = 6 Then
        Local $aFiles = _CompilerGetFiles(@ScriptDir)
        If @error Then
            MsgBox(0x2010, 'Error', 'There was an error trying to extract the files, directory may not be writable.' & @LF & _
                'Place ' & $sAppTitle & ' in a writable directory and try again.')
        Else
            MsgBox(0x2040, 'Success', 'The files appear to have extracted properly.' & @LF & _
                'You can find them in the following folder:' & @LF & @ScriptDir)
        EndIf
    EndIf
EndFunc

Func _HelpMenu()
    If @GUI_CtrlId = $mi_HelpAbout Then
        Local $sCompileMsg = 'You are running an uncompiled version of ' & $sAppTitle & ' v' & $sAppVersion & @LF & 'with AutoIt version ' & @AutoItVersion & '.'
        If @Compiled Then
            $sCompileMsg = 'You are running a compiled version of ' & $sAppTitle & ' v' & $sAppVersion & '.' & @LF & 'It was compiled with AutoIt version ' & @AutoItVersion & '.'
        EndIf
        MsgBox(0x2040, 'About ' & $sAppTitle & ' v' & $sAppVersion, 'A simple, portable AutoIt3 editor/interpreter/compiler.' & @LF & _
            $sCompileMsg & @LF & @LF & _
            'Written by Rob Saunders <www.therks.com>' & @LF & _
            'Written with the AutoIt3 language <www.autoit3.com>' & @LF & @LF & _
            'Choose Help > View ReadMe for more details.')
    Else
        Local $sSourcePath = _UniqueFile(@TempDir)
        FileInstall('QuickScript.au3', $sSourcePath, 1)
        If @GUI_CtrlId = $mi_HelpReadme Then
            Local $bPrint, $sLine, $hFile = FileOpen($sSourcePath, 0)
            If $hFile <> -1 Then
                GUICtrlSetData($ed_Content, '')
                Do
                    $sLine = FileReadLine($hFile)
                    If StringInStr($sLine, '*****') Then
                        If $bPrint = 1 Then ExitLoop
                        $bPrint = 1
                        ContinueLoop
                    EndIf
                    If $bPrint Then
                        GUICtrlSetData($ed_Content, $sLine & @CRLF, 1)
                    EndIf
                Until False
                FileClose($hFile)
            EndIf
        Else
            _LoadFileContents($sSourcePath)
        EndIf
        FileDelete($sSourcePath)
    EndIf
EndFunc

;~ Other Functions -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=>

Func _RunEditor($sFile)
    $sRunEditor = StringReplace($sEditorCmd, '%1', $sFile)
    If @extended Then
        Return Run($sRunEditor)
    Else
        Return Run($sRunEditor & ' ' & $sFile)
    EndIf
EndFunc

Func _ToggleConsole(ByRef $bConsoleShow, $iAction = 0)
    Local $aClient = WinGetClientSize($hGUIMain)
    
    $bConsoleShow = Not $bConsoleShow
    If $bConsoleShow Then
        $IGNORE_WM_MOVE = False
        Local $aSizerPos = ControlGetPos($hGUIMain, '', $lb_SizerFrame)
        GUISetState(@SW_SHOWNA, $hGUIFrameBot)
        GUISetState(@SW_SHOWNA, $hGUIFrameSize)
        GUICtrlSetPos($lb_ContentFrame, 0, 0, $aClient[0], $aSizerPos[1])
    Else
        $IGNORE_WM_MOVE = True
        GUICtrlSetPos($lb_ContentFrame, 0, 0, $aClient[0], $aClient[1] - 20)
        GUISetState(@SW_HIDE, $hGUIFrameBot)
        GUISetState(@SW_HIDE, $hGUIFrameSize)
        ControlFocus($hGUIMain, '', $ed_Content)
    EndIf
EndFunc

Func _SnippetLoad()
    Global $aSnippetsData = IniReadSection($sIniFile, 'Snippets')
    If @error Then
        Global $aSnippetsData[6][2] = [ [ 5 ], [ '&MsgBox' ], [ '&Endless Loop/HotKey Test' ], [ '&GUI Sample' ], [ 'G&UI Sample (OnEventMode)' ], [ '&ConsoleWrite Wrapper' ] ]
        $aSnippetsData[1][1] = 'MsgBox(0, '''', '''')\n'
        $aSnippetsData[2][1] = 'HotKeySet(''{esc}'', ''_Exit'')\n;HotKeySet(''#!^+'', ''_HotKey'')\n\nFunc _HotKey()\n\tMsgBox(0x40, ''HotKey:'', @HotKeyPressed)\nEndFunc\n\nWhile 1\n\tToolTip(''Looping...'')\n\tSleep(10)\nWEnd\n\nFunc _Exit()\n\tExit\nEndFunc\n'
        $aSnippetsData[3][1] = '$gui = GUICreate('''', 200, 200)\n;GUICtrlCreate\nGUISetState()\n\nWhile 1\n\t$gm = GUIGetMsg()\n\tSwitch $gm\n\t\tCase -3 ; $GUI_EVENT_CLOSE\n\t\t\tExitLoop\n\t\tCase Else\n\t\t\tIf $gm > 0 Then\n\t\t\t\tToolTip($gm & '':'' & GUICtrlRead($gm))\n\t\t\tEndIf\n\tEndSwitch\nWEnd\n'
        $aSnippetsData[4][1] = 'Opt(''GUIOnEventMode'', 1)\n$gui = GUICreate('''', 200, 200)\n\tGUISetOnEvent(-3, ''_Handler'') ; $GUI_EVENT_CLOSE\n;GUICtrlCreate\n;\tGUICtrlSetOnEvent(-1, ''_Handler'')\nGUISetState()\n\nWhile 1\n\tSleep(1)\nWEnd\n\nFunc _Handler()\n\tSwitch @GUI_CtrlId\n\t\tCase -3 ; $GUI_EVENT_CLOSE\n\t\t\tExit\n\t\tCase Else\n\t\t\tIf $gm > 0 Then\n\t\t\t\tToolTip($gm & '':'' & GUICtrlRead($gm))\n\t\t\tEndIf\n\tEndSwitch\nEndFunc\n'
        $aSnippetsData[5][1] = 'Func CW($sMsg)\n\tConsoleWrite($sMsg)\n\tIf Not StringRegExp($sMsg, ''\\n$'') Then ConsoleWrite(@CRLF)\nEndFunc\n'
        
        IniWriteSection($sIniFile, 'Snippets', $aSnippetsData, 1)
    EndIf
    
    If $aSnippetsMenuItems[0] > 0 Then
        For $i = 1 to $aSnippetsMenuItems[0]
            GUICtrlDelete($aSnippetsMenuItems[$i])
        Next
    EndIf
    Global $aSnippetsMenuItems[$aSnippetsData[0][0] + 1] = [ $aSnippetsData[0][0] ]
    For $i = 1 to $aSnippetsData[0][0]
        $aSnippetsData[$i][1] = StringReplace($aSnippetsData[$i][1], '%', '%%')
        $aSnippetsData[$i][1] = StringFormat($aSnippetsData[$i][1])
        $aSnippetsData[$i][1] = StringAddCR(StringStripCR($aSnippetsData[$i][1]))
        $aSnippetsMenuItems[$i] = GUICtrlCreateMenuItem($aSnippetsData[$i][0], $me_SnippetInsert)
        GUICtrlSetOnEvent($aSnippetsMenuItems[$i], '_SnippetsHandler')
    Next
EndFunc

Func _LoadFileContents($sFile)
    Local $hFile = FileOpen($sFile, 0)
    If $hFile <> -1 Then
        Local $sContents = FileRead($hFile)
        FileClose($hFile)
        GUICtrlSetData($ed_Content, $sContents)
        Return 1
    Else
        MsgBox(0x2030, 'Notice', 'Unable to load file:' & @CRLF & $sFile)
        Return SetError(1, 0, 0)
    EndIf
EndFunc

Func _StringToFormat($sString)
    $sString = StringReplace($sString, '\', '\\')
    $sString = StringReplace($sString, @CR, '')
    $sString = StringReplace($sString, @LF, '\n')
    $sString = StringReplace($sString, @TAB, '\t')
    Return $sString
EndFunc

Func _CompileScript($sScriptContent, $sOutFile, $sOutFolder)
    Local $iCompilePID, $iCompileTimer, $sCompileCmd, $aCompilerFiles, $bCompileSuccess = True
    Local $sTempFile = _UniqueFile($sOutFolder)
    Local $hTempFile = FileOpen($sTempFile, 2)
    If $hTempFile <> -1 Then
        Do
            FileWrite($hTempFile, $sScriptContent)
            FileClose($hTempFile)
            
            If @Compiled Then
                If FileExists(@ScriptDir & '\Aut2Exe.exe') And FileExists(@ScriptDir & '\upx.exe') And FileExists(@ScriptDir & '\AutoItSC.bin') Then
                    $sCompileCmd = StringFormat('"%s" /in "%s" /out "%s"', @ScriptDir & '\Aut2Exe.exe', $sTempFile, $sOutFile)
                Else
                    $aCompilerFiles = _CompilerGetFiles($sOutFolder)
                    If @error Then
                        MsgBox(0x42030, 'Notice', 'Unable to extract compiler files to location:' & @LF & $sOutFolder & @LF & 'Please ensure folder is writable.')
                        ExitLoop
                    EndIf
                    $sCompileCmd = StringFormat('"%s" /in "%s" /out "%s"', $aCompilerFiles[0], $sTempFile, $sOutFile)
                EndIf
            Else
                Local $sAut2ExePath = _PathSplitWrap(@AutoItExe) & 'Aut2Exe\Aut2Exe.exe'
                If Not FileExists($sAut2ExePath) Then
                    MsgBox(0x42030, 'Notice', 'Unable to find compiler. File does not exist at expected location:' & @LF & $sAut2ExePath)
                    ExitLoop
                EndIf
                $sCompileCmd = StringFormat('"%s" /in "%s" /out "%s"', $sAut2ExePath, $sTempFile, $sOutFile)
            EndIf
            
            $iCompileTimer = TimerInit()
            $iCompilePID = Run($sCompileCmd, $sOutFolder)
            
            If $iCompilePID Then
                While ProcessExists($iCompilePID)
                    GUICtrlSetData($lb_CompileStatus, 'Compiling... Time elapsed: ' & Round(TimerDiff($iCompileTimer) / 1000, 2) & 's')
                    
                    If WinExists('Aut2Exe Error') Then
                        If WinGetProcess('[LAST]') = $iCompilePID Then
                            $bCompileSuccess = False
                            ExitLoop
                        EndIf
                    EndIf
                    Sleep(100)
                WEnd
                If $bCompileSuccess Then
                    If MsgBox(0x42144, 'Compile', 'Compile completed. Navigate to file?') = 6 Then
                        Run(@WindowsDir & '\explorer.exe /select,"' & $sOutFile & '"')
                    EndIf
                Else
                    MsgBox(0x42030, 'Notice', 'Compile ended.')
                EndIf
            Else
                MsgBox(0x42010, 'Error', 'Error starting compiler.')
            EndIf           
        Until True
        If @Compiled Then _CompilerCleanupFiles($aCompilerFiles)
        FileDelete($sTempFile)
    Else
        MsgBox(0x2030, 'Notice', 'Could not write temporary script files, ensure folder is writable.')
    EndIf
EndFunc

Func _CompilerGetFiles($sFolder, $bOverwrite = 1)
    Local $aCompilerFiles[3], $bReturn = 1
    
    Do
        $bReturn *= FileInstall('C:\Program Files\AutoIt3\Aut2Exe\Aut2exe.exe', $sFolder & '\Aut2exe.exe', $bOverwrite)
        If Not $bReturn Then ExitLoop
        $aCompilerFiles[0] = $sFolder & '\Aut2exe.exe'
        
        $bReturn *= FileInstall('C:\Program Files\AutoIt3\Aut2Exe\upx.exe', $sFolder & '\upx.exe', $bOverwrite)
        If Not $bReturn Then ExitLoop
        $aCompilerFiles[1] = $sFolder & '\upx.exe'
    
        $bReturn *= FileInstall('C:\Program Files\AutoIt3\Aut2Exe\AutoItSC.bin', $sFolder & '\AutoItSC.bin', $bOverwrite)
        If Not $bReturn Then ExitLoop
        $aCompilerFiles[2] = $sFolder & '\AutoItSC.bin'
    Until True
        
    If Not $bReturn Then
        _CompilerCleanupFiles($aCompilerFiles)
        SetError(1)
    EndIf
    Return $aCompilerFiles
EndFunc

Func _CompilerCleanupFiles(ByRef $aCompilerFiles)
    If IsArray($aCompilerFiles) Then
        For $i = 0 to UBound($aCompilerFiles) - 1
            If $aCompilerFiles[$i] Then FileDelete($aCompilerFiles[$i])
        Next
    EndIf
EndFunc

Func _PathSplitWrap($sFilePath, $bReturnAll = False)
    Local $v, $v = _PathSplit($sFilePath, $v, $v, $v, $v)
    If $bReturnAll Then
        Local $r[3] = [ $v[1] & $v[2], $v[3], $v[4] ]
        Return $r
    Else
        Return $v[1] & $v[2]
    EndIf
EndFunc

Func _GUICtrlGroupSetState($aCtrlGroup, $iState)
    Local $hCtrl
    Local $iUBound = UBound($aCtrlGroup) - 1
    For $i = 0 To $iUBound
        $iPipe = StringInStr($aCtrlGroup[$i], '|')
        If $iPipe Then
            $hCtrl = StringLeft($aCtrlGroup[$i], $iPipe - 1)
            $iState = StringTrimLeft($aCtrlGroup[$i], $iPipe)
            GUICtrlSetState($hCtrl, $iState)
        Else
            GUICtrlSetState($aCtrlGroup[$i], $iState)
        EndIf
    Next
EndFunc

Func _UniqueFile($sDirectory, $sExtension = 'tmp', $sPrefix = '~', $iLen = 8, $iTimeout = 1000)
    Local $sFileName
    If $sExtension <> '' Then $sExtension = '.' & $sExtension   

    Local $iTimer = TimerInit()
    Do
        If TimerDiff($iTimer) > $iTimeout Then
            Return SetError(1, 0, False)
        EndIf
        $sFileName = $sPrefix
        For $i = 1 to $iLen
            $sFileName &= StringLower(Hex(Random(0, 15, 1), 1))
        Next
        $sFileName &= $sExtension
    Until Not FileExists($sDirectory & '\' & $sFileName)

    Return $sFileName
EndFunc

Func _GoTo($iLine, $iCol = 0)
    $iCol -= 1
    If $iCol < 1 Then $iCol = 0
    ControlFocus($hGUIMain, '', $ed_Content)
    Local $iGoTo = _GUICtrlEdit_LineIndex($ed_Content, $iLine-1) + $iCol
    _GUICtrlEdit_SetSel($ed_Content, $iGoTo, $iGoTo)
    _GUICtrlEdit_Scroll($ed_Content, $SB_SCROLLCARET)
EndFunc

Func _CheckDoubleRun($sSingleKey)
    $iSingleton = _Singleton($sSingleKey, 1)
    If Not $iSingleton Then
        Local $iMsgBox = MsgBox(0x2024, $sAppTitle, StringFormat('An instance of %s is already running.\r\nAre you sure you''d like to start another?', $sAppTitle))
        If $iMsgBox <> 6 Then
            WinSetTitle($sSingleKey, '', 'SHOW')
            Exit
        EndIf
    EndIf
    AutoItWinSetTitle($sSingleKey)
EndFunc

;~ Window Message Functions -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=>

Func WM_SIZE($hWnd, $iMsg, $iWParam, $iLParam)
    If $hWnd = $hGUIMain Then
        If Not BitAND(WinGetState($hGUIMain), 16 + 32) Then
            $aSaveWinPos = WinGetPos($hGUIMain)
        EndIf
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Func WM_MOVE($hWnd, $iMsg, $iWParam, $iLParam)
    If $IGNORE_WM_MOVE Then Return $GUI_RUNDEFMSG

    If $hWnd = $hGUIMain Then
        If Not BitAND(WinGetState($hGUIMain), 16 + 32) Then
            $aSaveWinPos = WinGetPos($hGUIMain)
        EndIf
    ElseIf $hWnd = $hGUIFrameSize Then
        $aClient = WinGetClientSize($hGUIMain)
        $iYPos = BitShift($iLParam, 16)
        $iTopEdge = $iSizerTopLimit
        $iBottomEdge = $aClient[1] - $iSizerBottomLimit - 10 ; -10 to account for the height of the sizebar
        If $iYPos <= $iTopEdge Then $iYPos = $iTopEdge
        If $iYPos >= $iBottomEdge Then $iYPos = $iBottomEdge

        WinMove($hGUIFrameSize, '', 0, $iYPos, $aClient[0], $iSizerHeight)
        GUICtrlSetPos($lb_SizerFrame, 0, $iYPos, $aClient[0], $iSizerHeight)
        GUICtrlSetPos($lb_ContentFrame, 0, 0, $aClient[0], $iYPos)
        GUICtrlSetPos($lb_ConsoleFrame, 0, $iYPos + $iSizerHeight, $aClient[0], $aClient[1] - 20 - $iSizerHeight - $iYPos)
    EndIf
EndFunc

Func WM_PAINT($hWnd = 0, $iMsg = 0)
    If $IGNORE_WM_PAINT Then Return $GUI_RUNDEFMSG
    
    If $hWnd = $hGUIMain Then
        ___WinFollowControl($hGUIMain, $lb_ContentFrame, $hGUIFrameTop)
        If $bConsoleShow Then
            ___WinFollowControl($hGUIMain, $lb_SizerFrame, $hGUIFrameSize)
            ___WinFollowControl($hGUIMain, $lb_ConsoleFrame, $hGUIFrameBot)
        EndIf
    EndIf
EndFunc

Func WM_GETMINMAXINFO($hWnd, $iMsg, $iWParam, $iLParam)
    Local $tMinMaxInfo = DllStructCreate('int;int;int;int;int;int;int;int;int;int', $iLParam)
    If $hWnd = $hGUIMain Then
        DllStructSetData($tMinMaxInfo, 7, $aMinMaxSize_Main[2]); min width
        DllStructSetData($tMinMaxInfo, 8, $aMinMaxSize_Main[3]); min height
        DllStructSetData($tMinMaxInfo, 9, $aMinMaxSize_Main[0]); max width
        DllStructSetData($tMinMaxInfo, 10, $aMinMaxSize_Main[1]); max height
        Return 0
    EndIf
EndFunc

Func WM_CTLCOLOREDIT($hWnd, $iMsg, $iWParam, $iLParam)
    If $iLParam = GUICtrlGetHandle($ed_Content) Then
        If Not IsDeclared('iLineCount_Mem') Then Global $iLineCount_Mem = -1
        Local $iLineCount = _GUICtrlEdit_GetLineCount($ed_Content)
        If $iLineCount <> $iLineCount_Mem then
            Local $sLineCount = ''
            For $i = 1 to $iLineCount
                $sLineCount &= $i & @CRLF
            Next
            GUICtrlSetData($ed_Lines, $sLineCount)
            $iLineCount_Mem = $iLineCount
        EndIf
        Local $iLineVis = _GUICtrlEdit_GetFirstVisibleLine($ed_Content)
        
        _GUICtrlEdit_LineScroll($ed_Lines, 0, -$iLineCount)
        _GUICtrlEdit_LineScroll($ed_Lines, 0, $iLineVis)
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Func ___WinFollowControl($hCtrlWnd, $iCtrl, $hMoveWnd)
    Local $aControl = ControlGetPos($hCtrlWnd, '', $iCtrl)
    WinMove($hMoveWnd, '', $aControl[0], $aControl[1], $aControl[2], $aControl[3])
EndFunc

;~ -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=> -=>

Func _OnAutoItExit()
    ;~ Local $sLastFile = IniRead($sIniFile, 'Settings', 'LastFile', '')
    ;~ If $sLastFile <> $sTempFilePath Then
        ;~ FileCopy($sLastFile, $sTempFilePath, 1)
    ;~ EndIf
    If IsArray($aSaveWinPos) And UBound($aSaveWinPos) >= 3 Then
        IniWrite($sIniFile, 'Settings', 'LastWinPos', $aSaveWinPos[0] & '|' & $aSaveWinPos[1] & '|' & $aSaveWinPos[2] & '|' & $aSaveWinPos[3])
    EndIf
    GUIDelete($hGUIMain)
    GUIDelete($hGUIFrameTop)
    GUIDelete($hGUIFrameSize)
    GUIDelete($hGUIFrameBot)
    Exit 4
EndFunc


#region - GUIGetFocus.au3
;===============================================================================
;
; Description:      _GUIGetFocus - Retrieves the classname of a window.
; Syntax:           _GUIGetFocus ( $hGUI [, $vUser32Dll = 'user32.dll' ] )
; Parameter(s):     $hGUI - Handle of GUI to check for control focus.
;                   $vUser32Dll - Optional handle to user32.dll.
; Return Value(s):  Success: Returns the id of focused control.
;                   Failure: Returns False, sets @error to 1, sets @extended to DllCall error.
; Author(s):        Rob Saunders (admin@therks.com)
;
;===============================================================================

Func _GUIGetFocus($hGUI, $vUser32Dll = 'user32.dll')
    Local $sFocus, $hCtrl, $aCtrlID, $sClassname, $hParent
    If Not IsHWnd($hGUI) Then
        Return SetError(1, 0, 0)
    EndIf
    
    $sFocus = ControlGetFocus($hGUI, '')
    $hCtrl = ControlGetHandle($hGUI, '', $sFocus)
    $sClassname = ___GCOP($hCtrl, $vUser32Dll, 1)
    If @error Then Return SetError(1, @error, 0)
    If $sClassname = 'COMBOBOX' Then
        $aCtrlID = DllCall($vUser32Dll, 'int', 'GetDlgCtrlID', 'hwnd', $hCtrl)
    ElseIf $sClassname = 'EDIT' Then
        $hParent = ___GCOP($hCtrl, $vUser32Dll)
        If @error Then Return SetError(2, @error, 0)
        $sClassname = ___GCOP($hParent, $vUser32Dll, 1)
        If @error Then Return SetError(3, @error, 0)
        If $sClassname = 'COMBOBOX' Then
            $aCtrlID = DllCall($vUser32Dll, 'int', 'GetDlgCtrlID', 'hwnd', $hParent)
        Else
            $aCtrlID = DllCall($vUser32Dll, 'int', 'GetDlgCtrlID', 'hwnd', $hCtrl)
        EndIf
    Else
        $aCtrlID = DllCall($vUser32Dll, 'int', 'GetDlgCtrlID', 'hwnd', $hCtrl)
    EndIf
    
    If @error Then Return SetError(4, @error, 0)
    Return $aCtrlID[0]
EndFunc

Func ___GCOP($hWnd, ByRef $vUser32Dll, $iSwitch = 0) ; GetClassOrParent
    Local $aReturn
    If $iSwitch = 0 Then
        $aReturn = DllCall($vUser32Dll, 'hwnd', 'GetParent', 'hwnd', $hWnd)
        If @error Then
            Return SetError(@error, 0, 0)
        Else
            Return $aReturn[0]
        EndIf
    Else
        $aReturn = DllCall($vUser32Dll, 'int', 'GetClassName', 'hwnd', $hWnd, 'str', '', 'int', 255)
        If @error Then
            Return SetError(@error, 0, 0)
        Else
            Return $aReturn[2]
        EndIf
    EndIf
EndFunc
#endregion

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.