Re: Adding a customer cell renderer to a simplelist




On Aug 29, 2004, at 8:15 PM, Daniel Kasak wrote:

I'm a little unsure of how the columns are numbered ( whether hidden columns count or not ).

SimpleList is designed to keep things Simple by maintaining a one-to-one relationship between the columns of the model and the columns of the view. 'hidden' columns are a bit of a hack; they are simply skipped when iterating over the column info to create view columns. to that end i usually try to keep the hidden columns at the end of the model so the view and model column indices still line up properly.


I started out by assuming that they *do* count, so I used:

    my $renderer = Gtk2::CellRendererSpinButton->new();
...
       $column = Gtk2::TreeViewColumn->new_with_attributes(
"Postcode",
                                                            $renderer,
value => 6

index 6 refers to the 7th column.  your model has only 6 columns.

                                                       );



It still doesn't seem like my new column is being 'registered' as far as the SimpleList is concerned...

i think you're still confusing the model columns and view columns. you've created another column in the view, but the model has not changed.

most importantly, $treeview->append ($column) does not affect your model, it only adds another element to the display. that view column can display data from any of the model columns.


I've just had an idea now: am I supposed to have *another* column with the value to be used for the custom cellrenderer to look at ( ie am I supposed to define the 'PostCode' column when I first create the SimpleList and then just set it's width to zero? )?

that's a thought in the right direction, as it will put another column into your model.

i suggest, however, you try a more elegant approach to get SimpleList to do the work for you:

  # add a new column type that uses a CellRendererSpinButton
  # to display and edit a Glib::Double column
  Gtk2::SimpleList->add_column_type (
                'number',
                type => 'Glib::Double',
                renderer => 'Gtk2::CellRendererSpinButton',
                attr => 'value',
  );

  my $slist = Gtk2::SimpleList->new_from_treeview(
                $prospects->get_widget('Locations_SList'),
                'DanPK'             => 'hidden',
                'LocNo'             => 'hidden',
                'Branch Name'       => 'text',
                'Address'           => 'text',
                'Suburb'            => 'text',
                'State'             => 'text'
                'PostCode'          => 'number'
  );

  # simplelist's set_column_editable() method doesn't know
  # how to handle a 'number' column, so we have to do
  # this one by hand.  note that it's the 7th column in the
  # model, but the *5th* column in the view, so its index
  # is 4.  (this confusion is why i like to keep the hidden
  # columns last.)
  my $numbercell = ($slist->get_column (4)->get_cell_renderers)[0];
  # tell it to allow editing...
  $numbercell->set (mode => "editable");
  # ... and hook up something to handle that editing.
  $numbercell->signal_connect (edited => sub {
        my (undef, $pathstring, $newval) = @_;
        # we'll cheat and use the path string as a stringified row number.
        $slist->{data}[$pathstring+0][2] = $newval;
  });


having said all that, of course, i don't think a double is what you want to use to represent postal codes. you may need to hack up the CellRenderer to use an integer instead of a double.


--
"that's it! you're a genius!" "yes. that's what i think. do you think i deserve a raise?"
        - dialogue from 'Godzilla versus Mothra', 1964




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