Re: CellRendererCombo

On May 19, 2005, at 9:34 PM, Dan wrote:

Back at it again ...

From the man page, there are only 3 properties:

- has-entry
- model
- text-column

But don't forget that CellRendererCombo inherits a whole slew of other properties from its parent class, CellRendererText.

Is the renderer supposed to automatically select the right row by
comparing the value in the treestore's model with the value in the
combo's model, or am I supposed to write something that does that? I'm a
little lost at the moment.

No. Until the user starts editing the cell, a CellRendererCombo is no different from a CellRendererText. To choose the text to display in the cell, set the "text" property. The additional functionality of CellRendererCombo isn't even enabled unless you set "editable" => TRUE, and then the model only gives you a list of things from which to choose.

An tiny example?

#!/usr/bin/perl -w

use strict;
use Gtk2 -init;
use Glib ':constants';

# some things from which to choose...
my @choices = qw(one two three four five six seven eight nine ten);

# in a format that CellRendererCombo can understand:
my $popupmodel = Gtk2::ListStore->new ('Glib::String');
foreach (@choices) {
        $popupmodel->set ($popupmodel->append, 0, $_);

# and now some random data for the treeview:
my $model = Gtk2::ListStore->new ('Glib::String');
foreach (1..5) {
$model->set ($model->append, 0, $choices[ int rand scalar @choices ]);

# a combo cell:
my $renderer = Gtk2::CellRendererCombo->new;
$renderer->set (editable => TRUE,     # user can choose... without this,
# the rest of the settings are ignored.
                model => $popupmodel, # this list of choices
                text_column => 0,     # in this column of $popupmodel
                has_entry => FALSE);  # and cannot use write-in choices
# if we want the user's selection from the combo to stick, we have to hook
# things up to make that happen.
$renderer->signal_connect (edited => sub {
        my ($renderer, $text_path, $new_string) = @_;
$model->set ($model->get_iter_from_string ($text_path), 0, $new_string);

# we treat the renderer like a text renderer when adding it to the view.
# tell the view column to fetch the text to display in the cell from column
# 0 of the model.
my $treeview = Gtk2::TreeView->new ($model);
$treeview->insert_column_with_attributes (-1, 'Stuff', $renderer, text => 0);

# for illustration, allow the user to toggle whether the cell allows
# write-in votes.
my $toggle = Gtk2::CheckButton->new ('has-entry');
$toggle->signal_connect (toggled => sub {
        $renderer->set (has_entry => $toggle->get_active);

# the rest is boilerplate.
my $vbox = Gtk2::VBox->new;
$vbox->add ($treeview);
$vbox->pack_start ($toggle, FALSE, FALSE, 0);

my $window = Gtk2::Window->new;
$window->signal_connect (destroy => sub { Gtk2->main_quit });
$window->add ($vbox);


If I lived in Teletubby Land, the homicide rate would be four.
  -- elysse

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]