
------------------------------------------------------------------------
--# Demos use of ini or settings files, with optional data spaces;
------------------------------------------------------------------------
-- RUN THIS FROM AN X-TERM to see the ini contents

-- To use settings, you must give a unique name to each control you 
-- wish to monitor/set. This allows colors, fonts, etc. to be 
-- specified on startup, but unlike CSS, modified during the run, 
-- and more importantly, allows control *states* and *values*,
-- not just appearance, to be preserved from run to run. 

-- The ini code is plain text, and easier to edit than CSS.

include GtkEngine.e
include GtkSettings.e -- for saving and loading settings
include GtkFontSelector.e -- for font filter

constant ini = canonical_path("~/demos/resources/test153.ini")

create(GtkWindow,"name=MainWindow,icon=face-cool,$delete-event=BailOut")

add("MainWindow",create(GtkBox,"name=panel,orientation=1"))

add("panel",{
    create(GtkLabel,"name=Label1"),
    create(GtkCalendar,"name=MyCalendar"),
    create(GtkButtonBox,"name=Box1"),
    create(GtkButtonBox,"name=Box2")})

add("Box1",{
    create(GtkButton,"stock_calendar#_Today",_("SetToday")),
    create(GtkFontButton,{
	{"name","FontButton"},  
	{"filter func",fontselector:filter}, -- filter out unusable fonts
	{"signal","font-set",_("SetCalFont")}})})

add("Box2",{
    create(GtkButton,"gtk-quit",_("BailOut")),	
    create(GtkButton,"gtk-save",_("SaveState")),
    create(GtkColorButton,{
	{"name","ColorChooserButton"},
	{"signal","color-set",_("SetBkgnd")}})})

-- Below is the list of the active controls we want to save and load:	
   object list = {"MainWindow","ColorChooserButton","FontButton","MyCalendar"}

settings:Load(ini) -- get settings from prior run, 1 = display on terminal, optional;

show_all("MainWindow")

main()

-----------------------
function SaveState() -- save controls on list, don't exit;
-----------------------
 settings:Save(ini,list,1) -- 1 means display on terminal, optional;
 set("MainWindow","icon","face-cool")
return 1
end function

------------------------------
function SetBkgnd(atom ctl) -- change and save window background color
------------------------------
object color = get(ctl,"rgba",1) -- 1 gets color as hex string 
    set("MainWindow","background",color)
    settings:Add(ini,"MainWindow","background",color) -- see note[1]
 return 1
end function

--------------------------------
function SetCalFont(atom ctl) -- change and save calendar font
--------------------------------
object fnt = get(ctl,"font")
    set("MyCalendar","font",fnt)
    settings:Add(ini,"MyCalendar","font",fnt) -- see note[2]
return 1
end function

----------------------
function SetToday() -- set calendar to current computer date, save to ini
----------------------
    set("MyCalendar","date",date())
    set("MyCalendar","mark day",get("MyCalendar","day"))
return 1
end function

---------------------
global function BailOut() -- save all settings for next run
--------------------- 
-- show how to retrieve named 'data' items attached to a widget,
-- normally not necessary;
object title = get("MainWindow","data","Foobar")
object msg = get("MainWindow","data","Message")

-- save the default value for the active controls on the list,
-- this is the standard one-liner that is all that is needed for
-- most programs;
  settings:Save(ini,list)
  
  Info(,title,"BailOut",msg)

return Quit()
end function

-- note [1] 
-- this is shown for demo purposes, an alternative would be to just set the 
-- window background by reading the ColorChooserButton's color property after 
-- loading the ini.

-- note [2]
-- this is also shown for demo purposes, normally it would do to set the calendar
-- font from the FontButton.font property. You might use this if you were setting
-- fonts in some other manner (text entry, perhaps?)


