143 lines
4.1 KiB
Elixir
143 lines
4.1 KiB
Elixir
|
|
------------------------------------------------------------------------------
|
|
--# Formatting floats in GtkListView cells
|
|
------------------------------------------------------------------------------
|
|
|
|
include GtkEngine.e
|
|
|
|
include std/locale.e -- for 'money' format
|
|
|
|
-- Note: when you use locale.e, you will have to use the
|
|
-- gtk: namespace for gtk calls, since locale.e also has
|
|
-- add(), set() and get() functions
|
|
|
|
constant docs = `<u><b>Floating-point numbers</b></u>
|
|
|
|
This shows how to display them correctly using a
|
|
custom formatting function.
|
|
|
|
You can edit the raw numbers or the formatted ones.
|
|
Click to open the edit space, when finished, click elsewhere
|
|
or hit <enter>
|
|
|
|
<span color='red'><b>Note:</b> doesn't work on Windows. Don't know why.</span>
|
|
`
|
|
constant win = create(GtkWindow,{
|
|
{"border width",10},
|
|
{"position",GTK_WIN_POS_CENTER},
|
|
{"icon","thumbnails/mongoose.png"},
|
|
{"signal","destroy","Quit"}})
|
|
|
|
constant panel = create(GtkBox,VERTICAL)
|
|
gtk:add(win,panel)
|
|
|
|
constant lbl = create(GtkLabel,{{"markup",docs}})
|
|
gtk:add(panel,lbl)
|
|
|
|
constant scroller = create(GtkScrolledWindow,{
|
|
{"min content height",150},
|
|
{"min content width",290}})
|
|
pack(panel,scroller,TRUE,TRUE)
|
|
|
|
enum NAME,AGE,AMT
|
|
|
|
constant ren1 = create(GtkCellRendererText,{
|
|
{"padding",10},
|
|
{"font","bold"}})
|
|
|
|
constant col1 = create(GtkTreeViewColumn,{
|
|
{"title","Name"},
|
|
{"pack start",ren1},
|
|
{"sort column id",1},
|
|
{"add attribute",ren1,"text",NAME}})
|
|
|
|
constant ren2 = create(GtkCellRendererText)
|
|
constant col2 = create(GtkTreeViewColumn,{
|
|
{"title","Age"},
|
|
{"pack start",ren2},
|
|
{"add attribute",ren2,"text",AGE}})
|
|
|
|
constant ren3 = create(GtkCellRendererText,{
|
|
{"editable",TRUE},
|
|
{"signal","edited",call_back(routine_id("UpdateValue3"))}})
|
|
|
|
constant col3 = create(GtkTreeViewColumn,{
|
|
{"title","Raw Val"},
|
|
{"pack start",ren3},
|
|
{"add attribute",ren3,"text",AMT}})
|
|
|
|
constant ren4 = create(GtkCellRendererText,{
|
|
{"alignment",1,0},
|
|
{"editable",TRUE},
|
|
{"signal","edited",call_back(routine_id("UpdateValue4"))}})
|
|
|
|
constant col4 = create(GtkTreeViewColumn,{
|
|
{"title","Formatted"},
|
|
{"sort column id",3},
|
|
{"pack start",ren4},
|
|
{"add attribute",ren4,"text",AMT},
|
|
{"cell data func",ren4,call_back(routine_id("MyCellFmt"))}})
|
|
-- func above is called for each cell in col4 to format the value
|
|
|
|
constant tv = create(GtkTreeView,{
|
|
{"rules hint",TRUE},
|
|
{"reorderable",TRUE},
|
|
{"grid lines",GTK_TREE_VIEW_GRID_LINES_BOTH},
|
|
{"append columns",{col1,col2,col3,col4}}})
|
|
gtk:add(scroller,tv)
|
|
|
|
constant selection = gtk:get(tv,"selection")
|
|
|
|
constant mdl = create(GtkListStore,{gSTR,gINT,gFLT})
|
|
gtk:set(tv,"model",mdl)
|
|
|
|
constant list =
|
|
{
|
|
{"Sam Smith",45,19.95},
|
|
{"Sue Jones",23,98.33},
|
|
{"Frank Farkel",64,-4.00},
|
|
{"Joe Schmoe",49,1056.50},
|
|
$
|
|
}
|
|
gtk:set(mdl,"data",list)
|
|
|
|
show_all(win)
|
|
main()
|
|
|
|
---------------------------------------------------------------
|
|
function MyCellFmt(atom layout, atom rend, atom mdl, atom iter)
|
|
---------------------------------------------------------------
|
|
-- this function 'fixes' the display of floating-point numbers
|
|
-- in a list/tree view. money is formatted per locale,
|
|
-- and credit balances are shown in red;
|
|
|
|
atom val = gtk:get(mdl,"col data from iter",iter,AMT)
|
|
|
|
if val < 0 then
|
|
gtk:set(rend,"foreground","red")
|
|
else
|
|
gtk:set(rend,"foreground","black")
|
|
end if
|
|
gtk:set(rend,"property","markup",locale:money(val)) -- must specify "property";
|
|
|
|
return 1
|
|
end function
|
|
|
|
---------------------------------------------------------
|
|
function UpdateValue3(atom ctl, atom rend, object newval)
|
|
---------------------------------------------------------
|
|
atom row = gtk:get(selection,"selected row")
|
|
newval = to_number(peek_string(newval))
|
|
gtk:set(mdl,"col data",row,3,newval)
|
|
return 1
|
|
end function
|
|
|
|
---------------------------------------------------------
|
|
function UpdateValue4(atom ctl, atom rend, object newval)
|
|
---------------------------------------------------------
|
|
atom row = gtk:get(selection,"selected row")
|
|
newval = to_number(peek_string(newval))
|
|
gtk:set(mdl,"col data",row,3,newval)
|
|
return 1
|
|
end function
|