1. EuGTK - GtkListStore - color?
- Posted by Jerry_Story Sep 07, 2010
- 1761 views
There doesn't seem to be any way in GtkRoutines.e to color a row in a GtkListStore. A search on 'color' does not find it. A search on 'colour' finds nothing.
But this tutorial says it is possible in GTK.
http://scentric.net/tutorial/sec-treeview-col-whole-row.html
My reason for wanting to put color in rows is color means information.
2. Re: EuGTK - GtkListStore - color?
- Posted by irv Sep 09, 2010
- 1648 views
Yes, you can certainly do that, and I had a demo somewhere, can't find it at the moment. Basically, you add a column to your list (which does not necessarily have to be shown) which contains the color for that row.
For example: Name Age [color] Bill 23 "blue" Jane 55 "red" etc.
Then you set the color of the textrenderer text or background. I'll dig around or write a new demo showing how that works.
3. Re: EuGTK - GtkListStore - color?
- Posted by irv Sep 09, 2010
- 1618 views
To color an entire row, the following works. I had to edit one line in GtkRoutines.e to accomodate it, but it seems like this is the accepted way to do it.
First, edit GtkRoutines.e - line 2353 or so, to read:
{"insert_column_with_attributes",{P,I,S,P,S,I,S,I,S,I,S,I},I},
Then the following will color each line with whatever color is specified with that name.
include GtkEngine.e include std/machine.e include std/console.e constant names = { {"Jerry S. Smith","green"}, {"Jonnie B. Good","yellow"}, {"Jason Jones","blue"}, {"Susan Black","black"}, {"Fred Flintstone","brown"} } enum COLOR=2 object selection function Foo(object view) Info(0,"Info","You selected",get(view,"text",1)) return 1 end function constant foo = call_back(routine_id("Foo")) constant win = create(GtkWindow) connect(win,"destroy",quit) set(win,"title","Test 33 - List View") set(win,"position",GTK_WIN_POS_CENTER) set(win,"border width",10) set(win,"default size",400,400) constant panel = create(GtkVBox) add(win,panel) constant tv = create(GtkTreeView) set(tv,"reorderable",TRUE) add(panel,tv) constant lbl = create(GtkLabel) set(lbl,"markup","<span size='small'>Double click on a row to retrieve info\nDrag items to re-order!</span>") pack(panel,-lbl) object renderer1 = create(GtkCellRendererText) enum NUM=0,NAME,SCORE,COLOUR object store = create(GtkListStore,gINT,gSTR,gINT,gSTR) set(tv,"model",store) set(tv,"insert column with attributes",-1,"#",renderer1,"text",NUM,"background",COLOUR) set(tv,"insert column with attributes",-1,"Name",renderer1,"text",NAME) set(tv,"insert column with attributes",-1,"Score",renderer1,"text",SCORE) set(tv,"insert column with attributes",-1,"Color",renderer1,"text",COLOUR) object iter = allocate(32) set(tv,"model",store) for i = 1 to length(names) do set(store,"append",iter) set(store,"set",iter,NUM,i,-1) set(store,"set",iter,NAME,allocate_string(names[i][NAME]),-1) set(store,"set",iter,SCORE,rand(100),-1) set(store,"set",iter,COLOUR,allocate_string(names[i][COLOR]),-1) end for connect(tv,"row-activated",foo) show_all(win) main()
PS: If you don't want to see the color names displayed on your list, just comment out the line:
--set(tv,"insert column with attributes",-1,"Color",renderer1,"text",COLOUR)
PPS: If you want different colors - or no colors - for some columns - IOW, the whole row isn't one color, you need to set up additional renderers for those columns. You can have a separate renderer for each column if you wish.
4. Re: EuGTK - GtkListStore - color?
- Posted by Jerry_Story Sep 11, 2010
- 1590 views
To color an entire row, the following works. I had to edit one line in GtkRoutines.e to accomodate it, but it seems like this is the accepted way to do it.
First, edit GtkRoutines.e - line 2353 or so, to read:
{"insert_column_with_attributes",{P,I,S,P,S,I,S,I,S,I,S,I},I},
Then the following will color each line with whatever color is specified with that name.
include GtkEngine.e include std/machine.e include std/console.e constant names = { {"Jerry S. Smith","green"}, {"Jonnie B. Good","yellow"}, {"Jason Jones","blue"}, {"Susan Black","black"}, {"Fred Flintstone","brown"} } enum COLOR=2 object selection function Foo(object view) Info(0,"Info","You selected",get(view,"text",1)) return 1 end function constant foo = call_back(routine_id("Foo")) constant win = create(GtkWindow) connect(win,"destroy",quit) set(win,"title","Test 33 - List View") set(win,"position",GTK_WIN_POS_CENTER) set(win,"border width",10) set(win,"default size",400,400) constant panel = create(GtkVBox) add(win,panel) constant tv = create(GtkTreeView) set(tv,"reorderable",TRUE) add(panel,tv) constant lbl = create(GtkLabel) set(lbl,"markup","<span size='small'>Double click on a row to retrieve info\nDrag items to re-order!</span>") pack(panel,-lbl) object renderer1 = create(GtkCellRendererText) enum NUM=0,NAME,SCORE,COLOUR object store = create(GtkListStore,gINT,gSTR,gINT,gSTR) set(tv,"model",store) set(tv,"insert column with attributes",-1,"#",renderer1,"text",NUM,"background",COLOUR) set(tv,"insert column with attributes",-1,"Name",renderer1,"text",NAME) set(tv,"insert column with attributes",-1,"Score",renderer1,"text",SCORE) set(tv,"insert column with attributes",-1,"Color",renderer1,"text",COLOUR) object iter = allocate(32) set(tv,"model",store) for i = 1 to length(names) do set(store,"append",iter) set(store,"set",iter,NUM,i,-1) set(store,"set",iter,NAME,allocate_string(names[i][NAME]),-1) set(store,"set",iter,SCORE,rand(100),-1) set(store,"set",iter,COLOUR,allocate_string(names[i][COLOR]),-1) end for connect(tv,"row-activated",foo) show_all(win) main()
PS: If you don't want to see the color names displayed on your list, just comment out the line:
--set(tv,"insert column with attributes",-1,"Color",renderer1,"text",COLOUR)
PPS: If you want different colors - or no colors - for some columns - IOW, the whole row isn't one color, you need to set up additional renderers for those columns. You can have a separate renderer for each column if you wish.
Thanks. The example works. But now I have the problem of understanding the code.
5. Re: EuGTK - GtkListStore - color?
- Posted by irv Sep 12, 2010
- 1636 views
Thanks. The example works. But now I have the problem of understanding the code.
You're not the only one who can't understand this stuff - hence the lack of tutorials on using TreeView/ListView controls. It's a case of letting computer science theory override practicality.
In Theory - the M/V/C (Model/View/Controller) architecture allows you to display and manipulate *one* set of data in many different ways. See: http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
In Practice - programmers seldom need or want to do that, and when they do need to do so, they already have better methods available.
It's sort of like:
In Theory - if we put wings and floats on cars, they would be able to go more places, fly over traffic jams, and be safer if we fell into the water.
In Practice - you'd need a pilot's license, a driver's license, and a sea captain's ticket to use the thing, and it would take an hour to get the million $ car started and get clearance to leave your driveway.
So, seeing as how they (the GTK folks) decided to go this route instead of implementing something simple and usable - what do we do?
We can either work within the framework GTK provides, which is what I did in the code above, or we can write a Euphoria 'wrapper' to handle the sticky details. I'm trying different ways of doing that now.
6. Re: EuGTK - GtkListStore - color?
- Posted by irv Sep 12, 2010
- 1627 views
We can simplify the list view greatly with the use of a small include file. Here's a simple list program, following it is the include:
include GtkEngine.e include ListView.e as List include machine.e enum NAME, AGE, PET object names = { {"Jerry S. Smith",21,"Rover"}, {"Jonnie B. Goode",44,"Fluffy"}, {"Susan Black",33,"none"}, {"Fred Flintstone",55,"Dino"}, {"George Burns",112,"cigar"} } constant win = create(GtkWindow) connect(win,"destroy",quit) constant panel = create(GtkVBox) add(win,panel) constant tv = List:View({"#","Name","Age","Pet name"}) -- {column headings} add(panel,tv) object store = List:Store(gINT,gSTR,gINT,gSTR) -- (column data types) set(tv,"model",store) for i = 1 to length(names) do -- load the list store with data List:Row(store,i & names[i]) end for show_all(win) main()
Code below handles the dirty work:
-- listview.e include GtkEngine.e object renderer = create(GtkCellRendererText) object iter = allocate(32) export function Column(atom tv, sequence title, integer datacolumn) set(tv,"insert column with attributes",-1,title,renderer,"text",datacolumn) return 1 end function export function View(object cols) atom tv = create(GtkTreeView) for i = 1 to length(cols) do Column(tv,cols[i],i-1) end for return tv end function export function Store(atom p1=0, atom p2=0, atom p3=0, atom p4=0, atom p5=0, atom p6=0, atom p7=0, atom p8=0) atom store = create(GtkListStore,p1,p2,p3,p4,p5,p6,p7,p8) return store end function export function Row(atom store, object data) set(store,"insert",iter,99999) for i = 1 to length(data) do set(store,"set",iter,i-1,data[i],-1) end for return 1 end function
The above works fine as long as there is only one list in operation - too bad we don't have some object-oriented abilities, so we could "include ListView.e as ..." more than one instance. As you may remember, some years ago, this accidentally worked, but was soon removed.
7. Re: EuGTK - GtkListStore - color?
- Posted by Jerry_Story Sep 17, 2010
- 1503 views
We can simplify the list view greatly with the use of a small include file. Here's a simple list program, following it is the include:
include GtkEngine.e include ListView.e as List include machine.e enum NAME, AGE, PET object names = { {"Jerry S. Smith",21,"Rover"}, {"Jonnie B. Goode",44,"Fluffy"}, {"Susan Black",33,"none"}, {"Fred Flintstone",55,"Dino"}, {"George Burns",112,"cigar"} } constant win = create(GtkWindow) connect(win,"destroy",quit) constant panel = create(GtkVBox) add(win,panel) constant tv = List:View({"#","Name","Age","Pet name"}) -- {column headings} add(panel,tv) object store = List:Store(gINT,gSTR,gINT,gSTR) -- (column data types) set(tv,"model",store) for i = 1 to length(names) do -- load the list store with data List:Row(store,i & names[i]) end for show_all(win) main()
Code below handles the dirty work:
-- listview.e include GtkEngine.e object renderer = create(GtkCellRendererText) object iter = allocate(32) export function Column(atom tv, sequence title, integer datacolumn) set(tv,"insert column with attributes",-1,title,renderer,"text",datacolumn) return 1 end function export function View(object cols) atom tv = create(GtkTreeView) for i = 1 to length(cols) do Column(tv,cols[i],i-1) end for return tv end function export function Store(atom p1=0, atom p2=0, atom p3=0, atom p4=0, atom p5=0, atom p6=0, atom p7=0, atom p8=0) atom store = create(GtkListStore,p1,p2,p3,p4,p5,p6,p7,p8) return store end function export function Row(atom store, object data) set(store,"insert",iter,99999) for i = 1 to length(data) do set(store,"set",iter,i-1,data[i],-1) end for return 1 end function
The above works fine as long as there is only one list in operation - too bad we don't have some object-oriented abilities, so we could "include ListView.e as ..." more than one instance. As you may remember, some years ago, this accidentally worked, but was soon removed.
In my current translation project I need 5 lists. Does this mean that I should have 5 instances of ListView.e?
include ListView1 as List1 include ListView2 as List2 include ListView3 as List3 include ListView4 as List4 include ListView5 as List5
8. Re: EuGTK - GtkListStore - color?
- Posted by irv Sep 18, 2010
- 1442 views
Some years ago, for a short while, someone made a mistake and you could include a file more than once, with different "as" clauses, and it would create an independent set of variables each time. That was too good to last, so it was removed quickly. As far as I could tell, it worked fine. :(
The only way to do multiple lists now would be to do as you suggest, copy ListView.e under different names and include them 'as' xxx.
The reason for this is that the ListView code holds some variables & pointers which you don't want to overwrite. With a LOT of coding, I could probably figure out a way to associate different variables & pointers with different lists, but it seems like a lot of trouble with little benefit.
9. Re: EuGTK - GtkListStore - color?
- Posted by irv Sep 19, 2010
- 1364 views
Umm... you'll notice that the ListView.e contains only a few lines of code. No doubt the other code you'll need to write to handle all the other aspects of each list will be quite a bit longer.
So, why not write separate include files for each of those lists, and just paste the routines from ListView into those files? This would also give you the option to make individual mods, like colors.
Of course, you would want to make the functions local, not export them.
10. Re: EuGTK - GtkListStore - color?
- Posted by jimcbrown (admin) Sep 19, 2010
- 1425 views
The above works fine as long as there is only one list in operation - too bad we don't have some object-oriented abilities, so we could "include ListView.e as ..." more than one instance. As you may remember, some years ago, this accidentally worked, but was soon removed.
Am I missing something obvious? Why wouldn't the following work (at least as a quick&dirty hack) ?
include GtkEngine.e include ListView.e as List include machine.e enum NAME, AGE, PET object names = { {"Jerry S. Smith",21,"Rover"}, {"Jonnie B. Goode",44,"Fluffy"}, {"Susan Black",33,"none"}, {"Fred Flintstone",55,"Dino"}, {"George Burns",112,"cigar"} } constant win = create(GtkWindow) connect(win,"destroy",quit) constant panel = create(GtkVBox) add(win,panel) constant lv = List:CreateLV() constant tv = List:View(lv, {"#","Name","Age","Pet name"}) -- {column headings} add(panel,tv) object store = List:Store(gINT,gSTR,gINT,gSTR) -- (column data types) set(tv,"model",store) for i = 1 to length(names) do -- load the list store with data List:Row(lv, store,i & names[i]) end for show_all(win) main()
Code below handles the dirty work:
-- listview.e include GtkEngine.e export function CreateLV() object renderer = create(GtkCellRendererText) object iter = allocate(32) return {renderer, iter} end function export function Column(sequence lv, atom tv, sequence title, integer datacolumn) object renderer = lv[1] set(tv,"insert column with attributes",-1,title,render,"text",datacolumn) return 1 end function export function View(sequence lv, object cols) atom tv = create(GtkTreeView) for i = 1 to length(cols) do Column(lv,tv,cols[i],i-1) end for return tv end function export function Store(atom p1=0, atom p2=0, atom p3=0, atom p4=0, atom p5=0, atom p6=0, atom p7=0, atom p8=0) atom store = create(GtkListStore,p1,p2,p3,p4,p5,p6,p7,p8) return store end function export function Row(sequence lv, atom store, object data) object iter = lv[2] set(store,"insert",iter,99999) for i = 1 to length(data) do set(store,"set",iter,i-1,data[i],-1) end for return 1 end function
11. Re: EuGTK - GtkListStore - color?
- Posted by irv Sep 19, 2010
- 1351 views
include GtkEngine.e enum RENDERER, ITER export function CreateLV() object renderer = create(GtkCellRendererText) object iter = allocate(32) return {renderer, iter} end function function Column(sequence lv, atom tv, sequence title, integer datacolumn) set(tv,"insert column with attributes",-1,title,lv[RENDERER],"text",datacolumn) return 1 end function export function View(sequence lv, object cols) atom tv = create(GtkTreeView) for i = 1 to length(cols) do Column(lv,tv,cols[i],i-1) end for return tv end function export function Store(object p) while length(p) < 8 do p &= 0 end while return create(GtkListStore,p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8]) end function export function Row(sequence lv, atom store, object data, integer position=99999) set(store,"insert",lv[ITER],position) for i = 1 to length(data) do set(store,"set",lv[ITER],i-1,data[i],-1) end for return 1 end function
Yep, with slight modifications, that works for simple text lists. It will need to be expanded if you plan to use images, progressbars, or any other kinds of controls that can be put into a list/tree view, or if you plan to change the colors of some lines/columns. To do that, you need additional, distinct renderers.
I changed the calling parameters slightly:
object lv1 = CreateLV() constant tv1 = List:View(lv1,{"#","Name","Age","Pet"}) -- {create column headings} object store1 = List:Store({gINT,gSTR,gINT,gSTR}) -- {column data types} set(tv1,"model",store1) for i = 1 to length(names1) do -- load the list store with data List:Row(lv1,store1,i & names1[i]) end for
12. Re: EuGTK - GtkListStore - color?
- Posted by irv Sep 19, 2010
- 1421 views
- Last edited Sep 20, 2010
OK!
I have, with only 12 hours work, figured out how to make colored rows and/or columns, change fonts, etc. in ListViews. It's easy - for you. Simple one-liners like this will work:
setCol(3,tv,lv,"background",BCOLOR) -- get color from this cell in store setCol(2,tv,lv,"foreground",FCOLOR) setCol(2,tv,lv,"background","#FF00FF") setRow({2,3},tv,lv,"background","pink") -- use color name setRow({1,4},tv,lv,"font",FONT) -- use font from cell in store setRow(2,tv,lv,"font","Georgia Bold 16") setCol(3,tv,lv,"background","gray")
Screenshot WikiSpaces SourceForge EuGTK
Download EuGTK_4.2.2 from any of the sites listed above that you can get to, and try it out.