Re: [gtkmm] TreeView
- From: "Matthew Walton" <matthew alledora co uk>
- To: "Sebastian Ryszard Kruk" <skruk tlen pl>
- Cc: gtkmm-list gnome org
- Subject: Re: [gtkmm] TreeView
- Date: Tue, 29 Jul 2003 13:24:44 +0100 (BST)
Right, I'm at work at the moment so I can't provide definite code, but I
can tell you what you need to do.
Firstly, add a column to your TreeModel (ListStore or TreeStore in most
circumstances) which contains a Gdk::Color. This will be used to store the
colour that you want the row to be.
Then, when you're adding columns to the TreeView, don't add the column
with the Gdk::Color in it. Any column not explicitly added to the TreeView
(using append_column() or append_column_editable() or a related method)
will not be displayed.
I'm assuming you're using append_column() to add a column which displays
text (or multiple columns, it doesn't really matter in this case, I'll
assume it's column 0). You need to get hold of the CellRenderer made for
that column and tell it to read its background colour from the TreeModel
column which contains the Gdk::Color (which for this case I will assume is
column 5), which will be something like this
p_treeview->get_column(0)->add_attribute(p_treeview->get_column_cell_renderer(0),"background_gdk",5);
Please note that this code may not work at all, I'm just glancing in the
docs and writing down how I think it should be possible.
What you will find if you get this right is that the CellRenderer in each
row looks at the appropriate column in that row and gets its background
colour from it - thus giving you per-row colouring. You can of course get
multiple columns to read their colour from the same place.
If you don't understand how this works, consider that when you use
something like append_column() to add a Glib::ustring or std::string
column to the TreeView, it's actually doing something like this:
- create a Gtk::TreeViewColumn
- create a Gtk::CellRendererText
- tell the column that it is to be rendered using the CellRendererText
just made
- tell the column that the CellRendererText should get its property "text"
from the model column specified by the user
- add the TreeViewColumn to the TreeView
the wonderful thing is that you can then get hold of the column (or indeed
do all these steps manually if you prefer, which I do if I'm mucking about
with several different properties of the same column) and tell it to
obtain other properties of the CellRenderer from other model columns, or
indeed from function calls.
Also note that the columns in the TreeModel bear no relationship at all to
the columns in the TreeView (well, not very much anyway), and several of
them may provide the input for one View column, and others may be used by
no View columns, but instead are just extra data storage for the program
itself.
You might like to look at this file in the CVS of one of my projects:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/towel/towel-gtk2/gui-tracks.cc?rev=1.10&content-type=text/vnd.viewcvs-markup
especially in the TracksWindow constructor, where comments say 'set up
group store columns' and 'construct display columns for items TreeView'.
The former uses append_column()'s automatic column creation, but then
modifies the result. The latter creates two columns entirely manually,
setting them up so the text is in italics if one of the model columns
(which contains a boolean value) is true.
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/towel/towel-gtk2/trackgroupsstore.h?rev=1.5&content-type=text/vnd.viewcvs-markup
and
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/towel/towel-gtk2/trackstore.h?rev=1.2&content-type=text/vnd.viewcvs-markup
contain the class definitions for the two TreeModels being used (they're
subclasses of TreeStore and ListStore) along with their column
definitions, which you might find of some use in understanding the code.
As for Glib::ArrayHandle - don't worry about destroying it, it'll handle
itself quite adequately. To use one when it's the return value of a
function, just assign it to an STL container like vector<T> or list<T>,
the conversion will happen automatically. Then obviously the resulting STL
class is your problem with regards to memory management. Unlike GTK+'s C
API though, gtkmm programmers don't have to explicitly destroy anything at
all that I'm aware of. Certainly not ArrayHandles.
I hope that's helpful. Let me know if there's anything else. I really
should have CCed my initial reply to the gtkmm-list, but I forgot, so I'll
CC this one to it instead. Someone may wish to correct something I've
said.
Matthew
Sebastian Ryszard Kruk said:
> Użytkownik Matthew Walton napisał:
>> Sebastian Ryszard Kruk said:
>>
>>
>>>Hi,
>>>
>>>I have some questions about Gtk::TreeView
>>>1) How to set different colors for row? I tried CellRendererText accesed
>>>via Columns but it colored whole column
>>
>>
>> Off the top of my head: add a column to your TreeModel which contains
>> the
>> colour you want the row to be, then tie the appropriate properties of
>> the
>> CellRenderers to that column so that they read their colour from it.
>>
>
> Sorry - I am newbe to GTKmm - for last couple of years I have done most
> GUI in Java Swing/AWT and C WinAPI. As I presume I should add new column
> - OK I know how to do it, set some colour - once again I am aware (thats
> how I have changed background and foreground colour of all elements in
> Gtk::TreeView). But I dont get how to combine column properties to row?
> What I want to do is to colour only one row - and have the ability to
> change it in runtime. I wanted to do it with get_cell_renderers() but
> even when I set colour to the first one I got it changed colour of the
> whole column.
>
> As I presume the column with colour you have mentioned should be somehow
> hidden?
>
> By the way one more question - how to destroy Glib::ListHandle<T> ? The
> comments got in gtkmm API reference are no good in it either (it is not
> the same as good old j2sdk API JavaDoc) ;-(
>
>
> Thanks
>
> skruk
>
>
>
>
>>
>>>2) Is it possible tu place eg. Gtk::Label or any other widget in cell??
>>
>>
>> No, only subclasses of Gtk::CellRenderer may be placed in a TreeView's
>> display. Current GTK+ (and thus gtkmm) comes with CellRendererText,
>> CellRendererPixbuf (for images) and CellRendererToggle (for boolean
>> values). I believe there is a CellRendererProgressBar under
>> consideration
>> for inclusion in a future version of GTK+. I believe there is also a
>> combo
>> box CellRenderer in the gtkmm2 examples. You may be able to find others
>> in
>> the source code for various GPLed projects, although I can't be certain
>> of
>> that. If you want something like a button, or a radio button or
>> something
>> of that sort, you'll have to write your own I'm afraid.
>>
>
>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]