Re: vbox in list TreeView
| From: | jake at lyrehc dot com | Date: | Thu, 12 Apr 2007 03:08:53 +0000 |
| Subject: | Re: vbox in list TreeView | ||
| Groups: | php.gtk.general | ||
| Request: | Send a blank email to php-gtk-general+get-7782@lists.php.net to get a copy of this message | ||
Hi,
The model holds the data you want to store, which is not always the same
as how you want it displayed. You have to set a cell renderer to actually
draw the things you want displayed.
I'm not sure what Gtk::TYPE_OBJECT represents, but it's not just a general
object in the sense you might think of it. If you want to store a
particular type of Gtk+ object you can use the gtype property of the
class. In my earlier example I specify that I want to store a GdkPixbuf
in the first column by passing GdkPixbuf::gtype to the GtkListStore
constructor. If you want to store PHP objects you need to use
Gtk::TYPE_PHP_VALUE.
If you have some custom object in the model, you can set a callback
function to convert it into the format required by the GtkCellRenderer you
selected. You use set_data_cell_func for this purpose.
For example, say you have a very basic PHP class PersonName:
class PersonName
{
public $First;
public $Last;
function __construct($first,$last)
{
$this->First = $first;
$this->Last = $last;
}
}
If you wanted to store these objects in a combo box and display them as a
string of the form "Last, First" you could do the following:
//define a callback function to convert the object to a string
function setPersonNameText($column,$cell,$model,$iterator)
{
$person = $model->get_value($iterator,$column); //get the PersonName
$cell->set_property('text',$person->Last.", ".$person->First); //set
the text to be rendered from it
}
//some people to put in the box
$john = new PersonName("John","Doe");
$jane = new PersonName("Jane","Doe");
//create the combobox and model
$combo_box = new GtkComboBox();
$model = new GtkListStore(Gtk::TYPE_PHP_VALUE); //create a model holding a
php value (in this case instances of PersonName)
//add the names
$model->append(array($john));
$model->append(array($jane));
$combo_box->set_model($model); //set this as the model for the combo box
$cell_renderer = new GtkCellRendererText(); //renders a text string
$combo_box->pack_start($cell_renderer); //add the renderer to the box
//use our function to convert the data to the form required by our renderer
$combo_box->set_cell_data_func($cell_renderer,'setPersonNameText');
If you can be more specific about what data you want to hold and how you
want it displayed I might be able to offer advice for your specific
situation. If none of the existing GtkCellRenderer subclasses fit your
needs you would have to extend GtkCellRenderer with a custom class that
draws your cell display, but that would require working with the Gdk
layer.
-Jake Cobb
On 4/10/07, redsofa@redsofa.net <redsofa@redsofa.net> wrote:
> Hello,
>
> Thanks for your help.
>
> I have a problem : The Xul rich list box can contain more widgets (box,
button,
> more labels ...).
> pix and label is just one example.
>
> I search one issue to insert lot of others widgets.
>
> I see " Gtk::TYPE_BOXED " or " Gtk::TYPE_OBJECT " here :
> http://gtk.php.net/manual/en/gtk.enum.type.php
>
> But i don't know use Gtk::TYPE_OBJECT ListStore.
>
> do you have one idea ?
>
> Thanks a lot.
>
> Thomas.
>
>
>
>
>
> Selon jake@lyrehc.com:
>
> > Hello,
> >
> > Based on the link you provided, you might try a GtkIconView instead
> > of a GtkTreeView. The rich list box in your link looks like a select
> > list with only one item selectable and an icon-text pair for each row.
> > Try something like:
> >
> > $icon_view = new GtkIconView();
> > $model = new GtkListStore(GdkPixbuf::gtype, Gtk::TYPE_STRING);
//icon-text
> > pair
> > $icon_view->set_model($model);
> > $icon_view->set_pixbuf_column(0);
> > $icon_view->set_text_column(1);
> > $icon_view->set_columns(1); //one column, like a select list
> > $icon_view->set_selection_mode(Gtk::SELECTION_BROWSE); //exactly one item
> > selected
> > $icon_view->set_orientation(Gtk::ORIENTATION_HORIZONTAL); //text to right
> > of icon
> >
> > You can then use the model to add/remove rows or see which is selected.
> >
> > Adding rows:
> >
> > $model = $icon_view->get_model();
> >
> > $model->append(array(GdkPixbuf::new_from_file('image.png'),'image.png'));
> >
> > $model->append(array(GdkPixbuf::new_from_file('image2.png'),'image2.png'));
> >
> > Finding the selected information:
> >
> > $model = $icon_view->get_model();
> > $selected = $icon_view->get_selected_items();
> > if(empty($selected))
> > print("Nothing selected.");
> > else
> > {
> > $iter = $model->get_iter($selected[0]); //only one path by selection
> > mode, get iterator for it
> > $text = $model->get_value($iter,1); //get the text column (1) value
> > print("Image $text selected.");
> > }
> >
> > The GtkIconView still uses CellRenderer subclasses, but you don't have to
> > set them up yourself. Hope that's what you're looking for.
> >
> > -Jake
> >
> > >Hi,
> > >
> > >Someone know issue to insert vbox in treeview ?
> > >
> > >I would like make someone like that (without GtkCellRendererPixbuf to
> > insert >lot of widgets) :
> >
> > >>http://developer.mozilla.org/en/docs/Image:Controlguide-richlistbox.png
> > >
> > >Thanks,
> > >
> > >Thomas.
> > >Gul project / gul.redsofa.net
> >
> > --
> > PHP-GTK General Mailing List (http://gtk.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
>
>
>