Re: 7GUIs

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

Here's the third. Can someone suggest a better way of validating input dates? The rest require more work - not to write the code, but to understand the specs. Lunch takes priority!

Screenshots here: https://photos.app.goo.gl/EzuzcIIp8jCLusgn2

include GtkEngine.e 
include std/datetime.e 
 
constant css = create(GtkCssProvider,"GtkEntry#invalid {background: red;}") 
 
constant 
    win = create(GtkWindow,"size=150x-1,title=Book Flight,border=10,$destroy=Quit"), 
    pan = create(GtkBox,"orientation=VERTICAL,spacing=5") 
    gtk:add(win,pan) 
   
constant 
    opt = create(GtkComboBoxText) 
    gtk:add(opt,{"One-way flight","Return flight"}) 
    gtk:set(opt,"active",1) 
    connect(opt,"changed","UpdateT2") 
    gtk:add(pan,opt) 
 
constant  
    T1 = create(GtkEntry,"name=valid,text=01/1/2018,$activate=ValidDate"),  
    T2 = create(GtkEntry,"name=valid,text=01/01/2018,$activate=ValidDate,sensitive=FALSE"),  
    B1 = create(GtkButton,"Book","BookFlight") 
    gtk:add(pan,{T1,T2, B1})     
     
show_all(win)  
main() 
 
----------------------------------- 
global function ValidDate(atom ctl) 
----------------------------------- 
object d1 = get(ctl,"text") 
object d2 = retain_all("0123456789/",d1) 
object dt = -1 
 
if equal(d1,d2) then 
    dt = datetime:parse(d2,"%d/%m/%Y") 
end if 
 
if atom(dt) then 
    set(ctl,"name","invalid") 
    set(B1,"sensitive",FALSE) 
    return FALSE 
else 
    set(ctl,"name","valid") 
    set(B1,"sensitive",TRUE) 
end if 
 
 if get(opt,"active") = 2 then 
    set(B1,"sensitive",ValidDateRange()) 
 end if 
  
 return TRUE  
end function 
 
----------------------------------- 
global function ValidDateRange() 
----------------------------------- 
object dt1 = datetime:parse(get(T1,"text"),"%d/%m/%Y") 
object dt2 = datetime:parse(get(T2,"text"),"%d/%m/%Y") 
return datetime:diff(dt1,dt2) >= 0 
end function 
 
------------------------------ 
global function UpdateT2() 
------------------------------ 
  set(T2,"sensitive",get(opt,"active")-1)    
  return 1 
end function 
 
--------------------------------- 
global function BookFlight() 
--------------------------------- 
if ValidDate(T1) and ValidDate(T2) and ValidDateRange() then 
  switch get(opt,"active") do 
 
    case 1 then Info(win,"Congrats", 
        "You have booked a one-way flight", 
        sprintf("Leaving %s",{get(T1,"text")})) 
         
    case else Info(win,"Congrats","You have booked a round-trip flight", 
        sprintf("Leaving %s and returning %s",{get(T1,"text"),get(T2,"text")})) 
 
  end switch 
end if 
return 1 
end function 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu