
---------------------------------------------------------------------
--# GtkDrawingArea - draws pie chart of disk usage
---------------------------------------------------------------------

include GtkEngine.e 
include GtkCairo.e
include std/math.e

enum TOTAL, FREE, USED, NAME

ifdef WINDOWS then
constant diskinfo = disk_size("C:")
elsedef
constant diskinfo = disk_size("/")
end ifdef

-- note: no two disk size programs report anywhere near the same values,
-- so there's no way to know if Euphoria's disk info is more - or less -
-- accurate than any of the others. Eu and df -H reports match the most closely.

constant win = create(GtkWindow,
    "size=300x300,border_width=10,resizable=FALSE,position=1,$destroy=Quit")

constant panel = create(GtkBox,"orientation=VERTICAL,spacing=10")
    add(win,panel)

constant screen = create(GtkDrawingArea) 
    set(screen,"size request",280,200)
    add(panel,screen)
    
constant css = create(GtkCssProvider,"GtkFrame {border-radius: 20px;}")

constant frame = create(GtkFrame)
    add(panel,frame)

constant evb = create(GtkEventBox)
        add(frame,evb)

constant lbl = create(GtkLabel,{
    {"font","10"},
    {"color","blue"},
    {"markup",
	format(`  <b><u>[]</u></b>
	<b>Size:  </b> []
	<span color='red'><b>Used:</b></span> []
	<span color='green'><b>Free: </b></span> []`,
	    {diskinfo[NAME],
		format("[:8.0,,] meg.",diskinfo[TOTAL]/1_000_000),
		format("[:8.0,,] meg.",diskinfo[USED]/1_000_000),
		format("[:8.0,,] meg.",diskinfo[FREE]/1_000_000),
                "                                   "})}})
	add(evb,lbl)

constant btn1 = create(GtkButton,"gtk-quit","Quit")
constant box = create(GtkButtonBox,{
    {"layout",GTK_BUTTONBOX_START},
    {"margin top",10}})
    add(box,btn1)
    pack(panel,-box)

-- following line redraws the pie chart on program start and
-- whenever window is obscured/restored. 
-- it must not be called until all widgets are created
    connect(panel,"draw",call_back(routine_id("DrawPieChart"))) 

show_all(win)
main()

-------------------------------
function DrawPieChart()
-------------------------------
atom dwin = get(screen,"window")
atom cr = create(GdkCairo_t,dwin) 

set(cr,{
    {"source rgba",255,255,255,255},
    {"operator",CAIRO_OPERATOR_SOURCE},
    {"paint"}})

atom xc = 140.0
atom yc = 95.0
atom radius = 50.0

atom percent_used = (diskinfo[USED]/diskinfo[TOTAL])
atom percent_free = (diskinfo[FREE]/diskinfo[TOTAL])
atom degrees_used = 360 * percent_used
atom degrees_free = 360 * percent_free

-- angles are specified in radians; the zero radial is at 3 o'clock.
-- to get a more natural display, we rotate it to start at high noon.
atom offset = 90 * DEGREES_TO_RADIANS 
atom radians_used = (degrees_used * DEGREES_TO_RADIANS)-offset
atom radians_free = (degrees_free - (90 * DEGREES_TO_RADIANS))-offset

set(cr,{ -- fill free space;
    {"color","pale green"},
    {"arc",xc, yc, 90.0, -offset, radians_free},
    {"fill"}})

set(cr,{ -- draw border;
    {"color","blue"},
    {"line width",3},
    {"arc",xc, yc, 90.0, 0, math:TWOPI},
    {"stroke"}})

set(cr,{ -- fill used space;
    {"color","red"},
    {"move to",xc,yc},
    {"arc", xc, yc,88, -offset, radians_used},
    {"fill"}})

set(cr,{-- draw center dot;
    {"color","yellow"},
    {"arc",xc, yc, 4.0, 0, math:TWOPI},
    {"fill"}})

set(cr,"destroy")

return 0 -- important to return 0 here!
end function




