 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
fures
Joined: 31 Jan 2008 Posts: 30
|
Posted: Sun Apr 20, 2008 8:26 am Post subject: Auto-Setup GUI for changing any standard INI-file data |
|
|
Automatically generated Gui for a standard INI file
Auto-save any changes on the fly
Auto reload all params after closing the Gui
Usage:
IniGui_IniFile = <your_ini_file.ini>
GoSub, IniGui_Setup
; user code here
Return
#Include IniGui.ahk
Features:
Handle sections in the INI file
Handle param types:
- Boolean (commented line under param starting with ;type=Boolean)
- Parsing List (text separated by | or ;type=ParsingList)
- Short Text or Num (If text shorter than 30 char)
- Long Text (any other)
Shows default values for params (commented line under param starting with Default=)
Shows help texts for params (any other commented lines under param)
Example INI structure:
| Code: |
[Section Name]
Param1=0
;Default=default value
;Type=Boolean
;This is the help text - 1st line
;This text appears also in the Gui for user help - 2nd line
Param2=item1|item2|item3
;This is the help text for this Parse List param
;Type=Parsing (this is not necessary as the text contains "|")
;Default=item1|item2
Param3=bla-bla-bla
...
|
And the code is:
| Code: |
/*
IniGui
Automaticly generated Gui for a standard INI file
Auto-save any changes
Auto reload all params after closing the Gui
Usage:
IniGui_IniFile = <your_ini_file.ini>
GoSub, IniGui_Setup
; user code here
Return
#Include IniGui.ahk
Name space: IniGui_ (for Subrutines and Vars)
Features:
Handle sections in the INI file
Handle param types: (commented line under param starting with type=boolean) Boolean (True/False), Short Text or Num, Long Text, Parsing List (separated by |)
Shows default values for params (commented line under param starting with Default=)
Shows help texts for params (any other commented lines under param)
Example INI structure:
[Section Name]
Param1 = any value
;Default=default value
;Type=Boolean
;This is the help text.
;This text appears also in the Gui for user help.
Param2 = True
...
*/
;IniGui_IniFile:="320MPHX.ini"
;GoSub, IniGui_Setup
;Return
IniGui_Setup:
If (!IniGui_IniFile)
Return
IfNotExist,%IniGui_IniFile%
Return
IniGui_Hiba =
Gui, 99:+LastFound
IniGui_mywin := WinExist()
Gui, 99:+Resize
Gui, 99:Default
Gui, 99:Add, TreeView, vIniGui_MyTree x0 y0 h500 w200 gIniGui_MyTree
Gui, 99:Add, Text, w400 x210 y20 h105, ;Dummy for type description and Help
Gui, Font, Bold
Gui, 99:Add, Text, w400 x210 y130, Value:
Gui, 99:Add, Text, w400 x210 y330, Default:
Gui, Font
Gui, 99:Add, Edit, w400 r10 x210 y150 -Wrap HScroll gIniGui_DoEdit1 vIniGui_Ed1,
Gui, 99:Add, Edit, w200 r1 x210 y150 gIniGui_DoEdit2 vIniGui_Ed2,
Gui, 99:Add, Checkbox, w400 x210 y150 gIniGui_DoButton1,
Gui, 99:Add, Edit, w400 x210 y350 h100, ;Dummy for Default val text
Gui, 99: Add, Button, x460 y460 w150 h25 gIniGui_DoExit, Close
GoSub, IniGui_AddTreeItems
Gui, 99:Show,w620 h500,%A_ScriptName% Setup
Return
IniGui_AddTreeItems:
Loop, Read, %IniGui_IniFile%
{
If (!A_LoopReadLine or SubStr(A_LoopReadLine,1,1)=";")
Continue
If (SubStr(A_LoopReadLine,1,1)="[") ;Section
If not IniGui_Expand
{
StringReplace,IniGui_Sect,A_LoopReadLine,[,,All
StringReplace,IniGui_Sect,IniGui_Sect,],,All
IniGui_SID := TV_Add(IniGui_Sect,"","Bold Expand")
IniGui_Expand = 1
}
else
{
StringReplace,IniGui_Sect,A_LoopReadLine,[,,All
StringReplace,IniGui_Sect,IniGui_Sect,],,All
IniGui_SID := TV_Add(IniGui_Sect,"","Bold")
}
else ;Param
{
StringSplit,IniGui_Item,A_LoopReadLine,=
IniGui_Par = %IniGui_Item1%
IniGui_IID := TV_Add(IniGui_Par,IniGui_SID)
}
}
Return
IniGui_MyTree: ; This subroutine handles user actions (such as clicking).
if A_GuiEvent <> S ; i.e. an event other than "select new tree item".
Return ; Do nothing.
IniGui_CurrTV := A_EventInfo
TV_GetText(IniGui_ValName,IniGui_CurrTV)
IniGui_PID := TV_GetParent(IniGui_CurrTV)
TV_GetText(IniGui_PName,IniGui_PID)
IniRead,IniGui_Val,%IniGui_IniFile%,%IniGui_PName%,%IniGui_ValName%
;Tooltip, IniRead IniGui_Val "IniGui_IniFile" %IniGui_PName% %IniGui_ValName%
;Get Help and Default if any
IniGui_Help = `r`n
IniGui_Default =
IniGui_Type =
IniGui_Kell =
Loop, Read, %IniGui_IniFile%
{
;IfNotInString,A_LoopReadLine,%IniGui_ValName%
If (SubStr(A_LoopReadLine,1,StrLen(IniGui_ValName))<>IniGui_ValName)
{
If not IniGui_Kell
Continue
}
else
{
IniGui_Kell = 1
Continue
}
;MsgBox, %A_LoopReadLine%
If (SubStr(A_LoopReadLine,1,8)=";default") ;Default
IniGui_Default := SubStr(A_LoopReadLine,InStr(A_LoopReadLine,"=")+1)
else If (SubStr(A_LoopReadLine,1,5)=";type") ;Type
IniGui_Type := SubStr(A_LoopReadLine,InStr(A_LoopReadLine,"=")+1)
else If (SubStr(A_LoopReadLine,1,1)=";") ;Help
IniGui_Help := IniGui_Help . "`r`n" . SubStr(A_LoopReadLine,2)
else If (!A_LoopReadLine) ;Empty row
Continue
else ;New section or Param
Break
Continue
}
Control,Hide,,Static1,ahk_id %IniGui_mywin%
Control,Hide,,Static2,ahk_id %IniGui_mywin%
Control,Hide,,Static3,ahk_id %IniGui_mywin%
Control,Hide,,Edit1,ahk_id %IniGui_mywin%
Control,Hide,,Edit2,ahk_id %IniGui_mywin%
Control,Hide,,Edit3,ahk_id %IniGui_mywin%
Control,Hide,,Button1,ahk_id %IniGui_mywin%
If (IniGui_Val=="" or IniGui_Val="ERROR")
Return
; StringReplace,IniGui_Help,IniGui_Help,|,`r`n,All
; StringReplace,IniGui_Default,IniGui_Default,|,`r`n,All
Control,Show,,Static1,ahk_id %IniGui_mywin%
Control,Show,,Static2,ahk_id %IniGui_mywin%
;If IniGui_Val in false,true ;Boolean
If (InStr(IniGui_Type,"bool")) ;Boolean
{
ControlSetText,Static1,%IniGui_ValName% (type: Boolean) %IniGui_Help%
If IniGui_Default
{
Control,Show,,Edit3,ahk_id %IniGui_mywin%
Control,Show,,Static3,ahk_id %IniGui_mywin%
ControlSetText,Edit3,%IniGui_Default%
}
Control,Show,,Button1,ahk_id %IniGui_mywin%
If (IniGui_Val)
Control,Check,,Button1,ahk_id %IniGui_mywin%
else
Control,UnCheck,,Button1,ahk_id %IniGui_mywin%
ControlFocus,SysTreeView321,ahk_id %IniGui_mywin%
}
else If (InStr(IniGui_Type,"parse") or (InStr(IniGui_Val,"|") and StrLen(IniGui_Val)>1)) ;Parse list
{
ControlSetText,Static1,%IniGui_ValName% (type: Parse List) %IniGui_Help%
If IniGui_Default
{
Control,Show,,Edit3,ahk_id %IniGui_mywin%
Control,Show,,Static3,ahk_id %IniGui_mywin%
StringReplace,IniGui_Default,IniGui_Default,|,`r`n,All
ControlSetText,Edit3,%IniGui_Default%
}
Control,Show,,Edit1,ahk_id %IniGui_mywin%
StringReplace,IniGui_Val,IniGui_Val,|,`r`n,All
ControlSetText,Edit1,% IniGui_Val,ahk_id %IniGui_mywin%
}
else If (StrLen(IniGui_Val)<30) ;Short text
{
ControlSetText,Static1,%IniGui_ValName% (type: Short Text or Num) %IniGui_Help%
If IniGui_Default
{
Control,Show,,Edit3,ahk_id %IniGui_mywin%
Control,Show,,Static3,ahk_id %IniGui_mywin%
ControlSetText,Edit3,%IniGui_Default%
}
Control,Show,,Edit2,ahk_id %IniGui_mywin%
ControlSetText,Edit2,% IniGui_Val,ahk_id %IniGui_mywin%
}
else ;Long text noWrap
{
ControlSetText,Static1,%IniGui_ValName% (type: Long Text) %IniGui_Help%
If IniGui_Default
{
Control,Show,,Edit3,ahk_id %IniGui_mywin%
Control,Show,,Static3,ahk_id %IniGui_mywin%
ControlSetText,Edit3,%IniGui_Default%
}
Control,Show,,Edit1,ahk_id %IniGui_mywin%
ControlSetText,Edit1,% IniGui_Val,ahk_id %IniGui_mywin%
}
Return
IniGui_DoWriteIni(IniGui_CurrTV,IniGui_Val)
{
Global
IniGui_ID := TV_GetText(IniGui_Par,IniGui_CurrTV)
IniGui_PID := TV_GetParent(IniGui_ID)
TV_GetText(IniGui_PName,IniGui_PID)
;Tooltip, % "IniWrite: " IniGui_Par "=" IniGui_Val " Section:" IniGui_PName
IniWrite, %IniGui_Val%, %IniGui_IniFile%, %IniGui_PName%, %IniGui_Par%
Return
}
IniGui_DoEdit1:
ControlGetText,IniGui_Ed1,Edit1,ahk_id %IniGui_mywin%
StringReplace,IniGui_Val,IniGui_Ed1,`r`n,|,All
StringReplace,IniGui_Val,IniGui_Val,||,|,All
StringReplace,IniGui_Val,IniGui_Val,||,|,All
StringReplace,IniGui_Val,IniGui_Val,||,|,All
IniGui_DoWriteIni(IniGui_CurrTV,IniGui_Val)
Return
IniGui_DoEdit2:
ControlGetText,IniGui_Ed2,Edit2,ahk_id %IniGui_mywin%
IniGui_Val := IniGui_Ed2
IniGui_DoWriteIni(IniGui_CurrTV,IniGui_Val)
Return
IniGui_DoButton1:
ControlGet, IniGui_Bu1, Checked,, Button1, ahk_id %IniGui_mywin%
IniGui_Val = 0
If IniGui_Bu1
IniGui_Val = 1
IniGui_DoWriteIni(IniGui_CurrTV,IniGui_Val)
Return
IniGui_DoExit:
99GuiClose:
99GuiEscape:
Gui, 99:Destroy
Gui, 1:Default
Tooltip
GoSub, IniGui_Load
Return
IniGui_Load:
If (!IniGui_IniFile)
{
MsgBox, IniGui_IniFile is not defined.
Return
}
IfNotExist,%IniGui_IniFile%
{
MsgBox, %IniGui_IniFile% not exists.
Return
}
IniGui_Hiba =
Loop, Read, %IniGui_IniFile%
{
If (!A_LoopReadLine or SubStr(A_LoopReadLine,1,1)=";")
Continue
If (SubStr(A_LoopReadLine,1,1)="[")
{
StringReplace,IniGui_Sect,A_LoopReadLine,[,,All
StringReplace,IniGui_Sect,IniGui_Sect,],,All
}
else
{
StringSplit,IniGui_Item,A_LoopReadLine,=
IniGui_Par = %IniGui_Item1%
IniRead,%IniGui_Par%,%IniGui_IniFile%,%IniGui_Sect%,%IniGui_Par%
;MsgBox, IniRead-%IniGui_Item1%-%IniGui_IniFile%-%IniGui_Sect%-%IniGui_Item1%
}
}
Return
; 99GuiSize: ; Expand/shrink the ListView and TreeView in response to user's resizing of window.
; if A_EventInfo = 1 ; The window has been minimized. No action needed.
; return
; GuiControl, Move, IniGui_MyTree, % "H" . (A_GuiHeight - 0) ; -30 for StatusBar and margins.
; GuiControl, Move, IniGui_MyList, % "H" . (A_GuiHeight - 0) . " W" . (A_GuiWidth - 200 - 10)
; Return
|
And yes, I'm tired of manually creating Setup Guis for my scripts, and I don't want users messing around in the ini files. That's why.
I hope it helps some...
best,
Fures
Last edited by fures on Sun Apr 20, 2008 6:03 pm; edited 3 times in total |
|
| Back to top |
|
 |
monkey Guest
|
Posted: Sun Apr 20, 2008 12:20 pm Post subject: |
|
|
Look intersting and has possibilities, but...
if you | Code: | | #Include IniGui.ahk | at top of your script, the 1st lines executed are: | Code: | IniGui_Setup:
If (!IniGui_IniFile)
Return |
which I am sure is not what you intended
You either need to make IniGui_Setup a function or move the include
to after the user code, like:
| Code: | Usage:
IniGui_IniFile = <your_ini_file.ini>
GoSub, IniGui_Setup
; user code here
return
#Include IniGui.ahk
|
Keep working at it, I think it will be very useful  |
|
| Back to top |
|
 |
fures
Joined: 31 Jan 2008 Posts: 30
|
Posted: Sun Apr 20, 2008 2:16 pm Post subject: |
|
|
Of course, you include at the end of user code.
Thanks for the note.
Function? Yes, I started to work on it as a function, but was surprised by some AHK messages claiming the some of the vars should be public or private etc... I did not have the mood to go thru all the docs to find out what should be public and what not. It was easier to make it subroutine-like and use a namespace to exclude conflicts.
I assume it to be ready at this stage, so I do not keep working on it, unless there's any good idea...
Cheers,
fures |
|
| Back to top |
|
 |
keyboarder Guest
|
Posted: Sun Apr 20, 2008 10:58 pm Post subject: |
|
|
| nice. great idea. I have error on inifile if value is hotkey? for example ^c or #c? |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|