Code: Select all
;/ PureBasic 3.94
;/ Droopy 15/01/06
Structure Pref
Label.s
Key.s
Value.s
LabelId.i
ValueId.i
Type.i
EndStructure
ProcedureDLL PrefInit(IniFile.s,Section.s) ; Initialise the Pref Libraries / Return 1 if the iniFile exist
Shared PrefIniFile.s,PrefSection.s
PrefIniFile=IniFile
PrefSection=Section
Static Flag
If Flag=0
Global NewList Pref.Pref()
Flag=1
Else
ClearList(Pref())
EndIf
If FileSize(PrefIniFile)>0
ProcedureReturn 1
EndIf
ProcedureReturn 0
EndProcedure
ProcedureDLL PrefAdd(Label.s,Key.s,Type) ; Add a preference key : type = ( #PB_String_Password | #PB_String_Numeric | #PB_String_LowerCase | #PB_String_UpperCase )
AddElement(Pref())
Pref()\Label=Label
Pref()\Key=Key
Pref()\Type=Type
EndProcedure
ProcedureDLL PrefLoad() ; Load pref from file (ini)
Shared PrefIniFile.s,PrefSection.s
ForEach Pref()
retour.s=Space(512)
GetPrivateProfileString_(@PrefSection,@Pref()\Key,@vide.s,@retour,512,@PrefIniFile)
Pref()\Value=retour
Next
EndProcedure
ProcedureDLL PrefSave() ; Save pref to file (ini)
Shared PrefIniFile.s,PrefSection.s
ForEach Pref()
WritePrivateProfileString_(@PrefSection,@Pref()\Key,@Pref()\Value,@PrefIniFile)
Next
EndProcedure
ProcedureDLL.s PrefGet(Key.s) ; Retrieve a pref value
ForEach Pref()
If Key=Pref()\Key
ProcedureReturn Pref()\Value
EndIf
Next
EndProcedure
ProcedureDLL PrefSet(Key.s,Value.s) ; Set a pref value( Type = #PB_String_Password | #PB_String_Numeric | #PB_String_LowerCase | #PB_String_UpperCase )
ForEach Pref()
If Key=Pref()\Key
Pref()\Value=Value
EndIf
Next
EndProcedure
ProcedureDLL PrefComplete() ; Return 1 if all value are filled
ForEach Pref()
If LTrim(Pref()\Value)=""
ProcedureReturn 0
EndIf
Next
ProcedureReturn 1
EndProcedure
ProcedureDLL PrefShow(Title.s,ButtonText.s,LabelWidth,ValueWidth) ; Show a window to view / modify the memory preference
Shared PrefIniFile.s,PrefSection.s
;/ Open the Window & calculate the Window Width
Width=LabelWidth+ValueWidth+30
Hwnd=OpenWindow(#PB_Any,0,0,Width,50+ListSize(Pref())*30,Title,#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
;/ Add Labels & Preferences
ForEach Pref()
Pref()\LabelId=TextGadget(#PB_Any,10,10+ListIndex(Pref())*30,LabelWidth,20,Pref()\Label)
Pref()\ValueId=StringGadget(#PB_Any,20+LabelWidth,10+ListIndex(Pref())*30,ValueWidth,20,Pref()\Value,Pref()\Type)
Next
;/ Add the Button
ButtonId=ButtonGadget(#PB_Any,10,40+(ListSize(Pref())-1)*30,Width-20,30,ButtonText)
;/ Event Management
Repeat
event=WaitWindowEvent()
If event=#PB_Event_Gadget And EventGadget()=ButtonId And EventType()=#PB_EventType_LeftClick
;/ Button Click --> Save Pref to the Linked List
ForEach Pref()
Pref()\Value=GetGadgetText(Pref()\ValueId)
Next
event=#PB_Event_CloseWindow ;/ Force quit & close the Window
EndIf
Until event=#PB_Event_CloseWindow
;/ Free all gadgets
FreeGadget(ButtonId)
ForEach Pref()
FreeGadget(Pref()\LabelId)
FreeGadget(Pref()\ValueId)
Next
CloseWindow(Hwnd)
EndProcedure
;/ Test
Exist=PrefInit("c:\test.ini","pref")
;/ Set Preferences
PrefAdd("Server :","Server",0)
PrefAdd("Port :","port",#PB_String_Numeric)
PrefAdd("User Name :","UserName",0)
PrefAdd("Password :","Password",#PB_String_Password)
If Exist
;/ Load value from the INI File
PrefLoad()
Else
;/ Set default value
PrefSet("Server","pop.wanadoo.fr")
PrefSet("port","110")
PrefSet("UserName","droopy")
PrefSet("Password","123456789")
EndIf
;/ Show & let user modify the preferences / Save the preferences to INI File
PrefShow("Configuration","OK",80,100)
PrefSave()
;/ Tell if all parameters is set.
If PrefComplete()
MessageRequester("Parameters","Complete",#MB_ICONINFORMATION)
Else
MessageRequester("Parameters","Not Set",#MB_ICONWARNING)
EndIf