A little Converter

For everything that's not in any way related to PureBasic. General chat etc...
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

A little Converter

Post by Baldrick »

I am at the moment looking at buying myself a new car, so being that it is a Long weekend here in Aus I decided to go for a drive down to Newcastle to take 1 of these little Turbo Subaru Foresters for a test drive.
After copping a puncture on the way down which took me about 1/2 hr to change due to 1 of the wheel nuts having its shoulders stripped off by some1 using a rattlegun. I took this little subaru for a run & am very impressed. Now just got to sell off my old car then I will be ordering 1 I think. :)

http://subaru.com.au/explore/forester/e ... n&grade=XT

Anyway, after doing this I then copped the little prob of lack of accomodation, so just made the 4 hr trip home, so got home a day earlier than I had planned. Being that I am looking at this little car, I have been busy converting Kilowatts to horsepower, etc....
So being as I had a spare day I decided to have a little play with the PB Visual designer which I don't normally bother with & made a little converter which some of you may find handy.

Includefile: save as Converter_Window_Procs.pb

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)

;{ constants
;- Window Constants
;
Enumeration
  #Window_0
  #Window_1
  #Window_2
  #Window_3
EndEnumeration

;- MenuBar Constants
;
Enumeration
  #MenuBar_0
EndEnumeration

Enumeration
  #MENU_1
  #MENU_4
  #Menu_5
  #Menu_6
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Text_0
  #String_0
  #Text_1
  #Text_2
  #Text_3
  #Button_0
  #Combo_0
  #Text_4
  #String_2
  #Text_5
  #String_3
  #Button_1
  #Text_6
  #Button_2
  #List_0
  #Button_3
EndEnumeration

Global AppName.s="Converter"
About$=AppName+" is a freeware program by Baldrick.."+#CRLF$
About$+"Formula's used in this program NOT tested for accuracy. "
About$+"Author accepts no liabilty whatsoever for errors, loss or damages arising "
About$+"from use of this program."
;} Constants

;{ Structures Start
Structure Conversion
Name.s
Multiplier.f
EndStructure 
Global NewList FromTo.Conversion()
;} Structures End

;{ Selectall Start
Procedure SelectAll(textfocus)
  SendMessage_(GadgetID(textfocus),#EM_SETSEL,0,99999) 
  SetActiveGadget(textfocus)
EndProcedure
;} SelectAll end
 
;{ Populate ComboBox
Procedure PopulateComboBox()
  If OpenPreferences(AppName+".ini")
  Repeat
   PreferenceGroup("Converter Name") 
   Set$=ReadPreferenceString(Str(a),"")
   If Set$>""
   AddElement(FromTo())
   FromTo()\Name=Set$
   AddGadgetItem(#Combo_0,-1,FromTo()\Name)
   PreferenceGroup("Multiplier Value")
   Set.f=ReadPreferenceFloat(Str(a),0)
   FromTo()\Multiplier=Set
   EndIf 
   a+1
  Until Set$=""
   a=0
  ClosePreferences()
  EndIf  
EndProcedure 
;} Populate combobox

;{ Test Config
  Procedure TestConfig()
  If OpenPreferences(AppName+".ini")=0
  If CreatePreferences(AppName+".ini")
  PreferenceGroup("Converter Name")
  WritePreferenceString("0","Km's to Miles")
  WritePreferenceString("1","Miles to Km's")
  PreferenceGroup("Multiplier Value")
  WritePreferenceFloat("0",0.62)
  WritePreferenceFloat("1",1.61)
  ClosePreferences()
  MessageRequester(AppName,"Configuration file Not found"+#CRLF$+"Default settings have been applied")
  EndIf
 Else
 ClosePreferences()
EndIf
  EndProcedure 
;} test config

;{ Calculate start
Procedure.f Calculate(Value$,Selection$)
    ResetList(FromTo())
    Repeat 
    NextElement(FromTo())
    Current$=FromTo()\Name
    Until Current$=selection$
    Result.f=ValF(value$)*FromTo()\Multiplier
ProcedureReturn Result
EndProcedure 
;} calculate end

;{ Add Function
Procedure AddFunction(Name$,Mult.f)
If Name$>""
If Mult>0
AddElement(FromTo())
FromTo()\Name=Name$
FromTo()\Multiplier=Mult
AddGadgetItem(#Combo_0,-1,Name$)
quant=CountList(FromTo())
ResetList(FromTo())
CreatePreferences(Appname+".ini")
For a=0 To quant-1
NextElement(FromTo())
PreferenceGroup("Converter Name")
WritePreferenceString(Str(a),FromTo()\Name)
PreferenceGroup("Multiplier Value")
WritePreferenceFloat(Str(a),FromTo()\Multiplier)
Next
ClosePreferences()
EndIf
EndIf 
EndProcedure 
;} add function end

;{ Populate List gadget
Procedure PopulateList()
Count=CountList(FromTo())
SelectElement(FromTo(),1)
If CountList(FromTo())>2
Repeat
NextElement(FromTo())
AddGadgetItem(#List_0,-1,FromTo()\Name)
Index=ListIndex(FromTo())
Until Index=Count-1
EndIf 
EndProcedure 
;} Populate list gadget end

;{ Remove Function start
Procedure RemoveFunction(Text.s)
If Text>""
ResetList(FromTo())
Repeat
NextElement(FromTo())
Names.s=FromTo()\Name
Until Names=Text
DeleteElement(FromTo())
quant=CountList(FromTo())
ResetList(FromTo())
CreatePreferences(Appname+".ini")
For a=0 To quant-1
NextElement(FromTo())
PreferenceGroup("Converter Name")
WritePreferenceString(Str(a),FromTo()\Name)
PreferenceGroup("Multiplier Value")
WritePreferenceFloat(Str(a),FromTo()\Multiplier)
Next
ClosePreferences()
ClearGadgetItemList(#Combo_0) ; clear all, inc fromto list
ClearList(FromTo())
PopulateComboBox()  ; reset the above with fresh settings from updated ini file
SetGadgetState(#Combo_0,0)
EndIf 
EndProcedure 
;} Remove function end
 
;{ Main window 
Procedure Open_Window_0()
    TestConfig()  
  If OpenWindow(#Window_0, 355, 175, 180, 257, AppName,  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
    If CreateMenu(#MenuBar_0, WindowID(#Window_0))
      MenuTitle("&File")
      MenuItem(#Menu_5,"Add &Conversion")
      MenuItem(#Menu_6,"Remove Con&version")
      MenuBar()
      MenuItem(#MENU_1, "&Exit")
      AddKeyboardShortcut(#Window_0,#PB_Shortcut_Escape,#MENU_1)
      MenuTitle("&About")
      MenuItem(#MENU_4, "Abou&t")
      EndIf

      If CreateGadgetList(WindowID(#Window_0))
        TextGadget(#Text_0, 10, 25, 60, 20, "Input Value")
        StringGadget(#String_0, 10, 45, 155, 20, "")
        TextGadget(#Text_1, 10, 85, 80, 20, "Conversion Type")
        ComboBoxGadget(#Combo_0, 10, 105, 155, 120)
        TextGadget(#Text_2, 10, 145, 70, 20, "Output Result")
        TextGadget(#Text_3, 10, 165, 155, 20, "", #PB_Text_Border)
        ButtonGadget(#Button_0, 55, 200, 70, 20, "Calculate")
       PopulateComboBox()
      SetGadgetState(#Combo_0,0)
      SetActiveGadget(#String_0)
      EndIf
    EndIf
EndProcedure
;} Main window

;{ Add conversion window
Procedure Open_Window_1()
  If OpenWindow(#Window_1, 356, 215, 180, 154, AppName+" Add New Conversion",  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_WindowCentered,WindowID(#Window_0) )
    If CreateGadgetList(WindowID(#Window_1))
      TextGadget(#Text_4, 10, 20, 95, 20, "Conversion Name:")
      StringGadget(#String_2, 10, 40, 155, 20, "")
      TextGadget(#Text_5, 10, 75, 100, 20, "Conversion Multipiler:")
      StringGadget(#String_3, 10, 95, 155, 20, "")
      ButtonGadget(#Button_1, 65, 120, 50, 20, "Add")
    EndIf
  EndIf
EndProcedure
;} Add conversion window

;{ About window
Procedure Open_Window_2(About$)
  If OpenWindow(#Window_2, 356, 254, 180, 230, "About "+AppName,  #PB_Window_TitleBar| #PB_Window_WindowCentered,WindowID(#Window_0) )
    If CreateGadgetList(WindowID(#Window_2))
      TextGadget(#Text_6, 5, 10, 170, 180, About$, #PB_Text_Border)
      ButtonGadget(#Button_2, 65, 200, 50, 20, "OK")
    EndIf
  EndIf
EndProcedure
;} About window

;{ Remove item window
Procedure Open_Window_3()
  If OpenWindow(#Window_3, 356, 215, 180, 154, AppName+" Remove Conversion",  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_WindowCentered,WindowID(#Window_0) )
    If CreateGadgetList(WindowID(#Window_3))
      ListViewGadget(#List_0,10, 10, 155, 100)
      ButtonGadget(#Button_3, 65, 125, 50, 20, "Remove")
      PopulateList()
    EndIf
  EndIf
EndProcedure 
;} Remove item window end
Main File

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)

;{ includes
IncludeFile "Converter_Window_Procs.pb"
;IncludeFile #PB_Compiler_Home+"My Projects\ErrorHandle.pbi"
Open_Window_0()
;} includes

;{ Main loop
Repeat
  Event=WaitWindowEvent(1)
  WindowID=EventWindow():GadgetID=EventGadget():EventType=EventType():MenuID=EventMenu()
  
   If EventType=#PB_EventType_Focus
    Select GadgetID
     Case #String_0
      SelectAll(#String_0)
     Case #String_2
      SelectAll(#String_2)
     Case #String_3
      SelectAll(#String_3)
    EndSelect 
   EndIf 
  
  If Event = #PB_Event_Menu 
    Select MenuID
      Case #MENU_1
       Quit=1
      Case #MENU_4
       Open_Window_2(About$)
      Case #Menu_5
       Open_Window_1()
      Case #Menu_6
       Open_Window_3()
    EndSelect 
  EndIf
  
  If Event = #PB_Event_Gadget
    Select GadgetID
      Case #Button_0
       selection$=GetGadgetText(#Combo_0)
       value$=GetGadgetText(#String_0)
       Res.f=Calculate(Value$,Selection$)
       SetGadgetText(#Text_3,StrF(Res,2))
      Case #Button_1
       Name$=GetGadgetText(#String_2)
       Mult.f=ValF(GetGadgetText(#String_3))
       AddFunction(Name$,Mult.f)
       SetGadgetText(#String_2,"")
       SetGadgetText(#String_3,"")
      Case #Button_2
       CloseWindow(#Window_2)
       SetActiveWindow(#Window_0)
      Case #Button_3
       state=GetGadgetState(#List_0)
       Text.s=GetGadgetItemText(#List_0,state,0)
       RemoveFunction(Text)
       ClearGadgetItemList(#List_0)
       PopulateList()
    EndSelect 
  EndIf
  
  If Event = #PB_Event_CloseWindow
    Select WindowID
      Case #Window_0
       Quit=1
      Case #Window_1
       CloseWindow(#Window_1)
       SetActiveWindow(#Window_0)
      Case #Window_3
       CloseWindow(#Window_3)
       SetActiveGadget(#Window_0)
    EndSelect 
  EndIf 
  
  Until Quit
End
;} Main loop
When you 1st run this program it will make a little ini file for you, but if you like, you might like to manually make 1 and paste the below detail into it.

[Converter Name]
0 = Km's to Miles
1 = Miles to Km's
2 = Cm to Inch
3 = Inch to Cm
4 = Cubic Cm to Cubic Inch
5 = Cubic Inch to Cubic Cm
6 = NewtonMetres to FtLbs
7 = FtLbs to NewtonMetres
8 = Kpa to PSi
9 = PSi to Kpa
10 = Metres to Feet
11 = Feet to Metres
12 = Kw to Hp
13 = Hp to Kw
14 = Litres to Gallons {Imperial}
15 = Gallons {Imperial} to litres
[Multiplier Value]
0 = 0.620000
1 = 1.610000
2 = 0.393700
3 = 2.540000
4 = 0.061020
5 = 16.387199
6 = 0.740000
7 = 1.360000
8 = 0.145000
9 = 6.980000
10 = 3.280800
11 = 0.304800
12 = 1.340000
13 = 0.746000
14 = 0.220264
15 = 4.540000
Anyway, if you find it usefull, you are welcome, otherwise, Oh well :roll:
Have fun!!
Regards,
Baldrick
Character
Enthusiast
Enthusiast
Posts: 337
Joined: Mon Aug 07, 2006 3:51 pm
Location: Netherlands

Post by Character »

Nice! A self building little tool.

I saved it in my PureBasic collection. Might be handy some day :lol:



Image
Cessante causa cessat effectus
Post Reply