Re: EuGTK Convert Units

new topic     » goto parent     » topic index » view thread      » older message » newer message

OK, I would change the added routine in convert.e to take an integer parameter for simplicty:

-- additional routines added by Kenneth Rhodes  
-- index of categories  
global constant index = {"Length", "Time", "Velocity", "Angles",  "Angular Velocity","Area", "Volume",  "Weight", "Pressure", "Energy"}  
  
--obtain sequence of category unit labels  from index sequence  
global function SelectUnit(integer i)  
object cs = {} 
   for a = 1  to length(Unit[i]) by 2 do  
      cs = append(cs, Unit[i][a])   
   end for  
return cs  
end function  

Then convert.ex might be:

 
include GtkEngine.e  
include convert.e  
  
constant docs = "Unit Catgory"  
  
constant   
    win = create(GtkWindow,"border_width=10,position=1,$destroy=Quit"),  
    panel = create(GtkBox,"orientation=HORIZONTAL,spacing=10"),  
    lbl = create(GtkLabel,docs),  
    cb1 = create(GtkComboBoxText),   
    V1 = create(GtkEntry,"placeholder text=Value to convert"),   
    cb2 = create(GtkComboBoxText,"$changed=Convert"),   
    cb3 = create(GtkComboBoxText,"$changed=Convert"),    
    V2 = create(GtkEntry,"placeholder text=Results"),  
      
    btnbox = create(GtkButtonBox,"spacing=5"),  
    btn1 = create(GtkButton,"gtk-ok","Convert"),  
    btn2 = create(GtkButton,"gtk-quit","Quit")  
      
    add(win,panel)  
    add(panel,{lbl,cb1,V1,cb2,cb3,V2})   
    add(panel, V2)  
    pack_end(panel,btnbox)  
    add(btnbox,{btn1,btn2})  
     
    for i = 1 to length(index) do -- load first drop-down box with units; 
        set(cb1,"append text",index[i]) 
    end for 
    connect(cb1,"changed","Update") 
     
show_all(win)  
main()  
  
--------------------------- 
global function Update() -- update both in and out units drop down lists; 
--------------------------- 
integer u = get(cb1,"active") 
object units = SelectUnit(u) 
 set(cb2,"remove all")  
 set(cb3,"remove all") 
 for j = 1 to length(units) do  
    set(cb2,"append text",units[j]) 
    set(cb3,"append text",units[j]) 
 end for 
 set(cb2,"active",1) 
 set(cb3,"active",1) 
return 1  
end function  
 
---------------------------- 
global function Convert() -- call Jim Roberts' convert routine in convert.e 
---------------------------- 
atom val = to_number(get(V1,"text"))   
object from_units = get(cb2,"active text") 
object to_units = get(cb3,"active text") 
if val = 0 then return 1 end if  
if atom(from_units) or atom(to_units) then return 1 end if 
object results = convert_unit(val,from_units, to_units) 
results[1] = from_units 
results[2] = to_units 
results = append(results,val) 
set(V2,"tooltip text",text:format("[4] [1] = [3] [2]",results)) 
set(V2,"text",text:format("[4] [1] = [3:,,.4] [2]",results))       
return 1 
end function  
 

Note1: there's no need to check for invalid units, as there are no invalid options presented
Note2: your layout looks better than the one in my demos.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu