More on embedding combo models in a treestore



Thanks ( many thanks ) to help from Muppet and Torsten, I've finally got
things working ... kinda.

If I edit a cell with a combo and use the keyboard to end editing, or if
I click outside the combo, but still in the *same* row, everything works
fine.

If I edit a cell with a combo and then click into another row, however,
things go screwy. The problem seems to be that by clicking in another
row, I'm 'activating' it *before* my code catches the edit and figures
out what to put back into the cell. ie when I get hold of the combo's
model, I'm actually getting the model of the combo in the *new* row that
I just clicked in, and not the model of the combo in the *current* (
from my point of view anyway ) row.

The strange thing is that while I get hold of the wrong model, the
correct row ( ie the one I edited ) is updated.

Here's the code in question:

sub process_text_editing {
   
    my ( $self, $renderer, $text_path, $new_text ) = @_;
   
    my $column_no = $renderer->{column};
    my $path = Gtk2::TreePath->new_from_string ($text_path);
    my $model = $self->{treeview}->get_model;
    my $iter = $model->get_iter ($path);
   
    # If this is a CellRendererCombo, then we have to look up the ID to
match $new_text
    if ( ref($renderer) eq "Gtk2::CellRendererCombo" ) {
       
       
#####################################################################
        # ******** this next row is the one that gets the wrong model
*******
       
#####################################################################
       
        my $combo_model = $renderer->get("model");
        my $combo_iter = $combo_model->get_iter_first;
        my $found_match = FALSE;
       
        while ($combo_iter) {
           
            if ($combo_model->get($combo_iter, 1) eq $new_text) {
                $found_match = TRUE;
                $new_text = $combo_model->get( $combo_iter, 0 ); # It's
possible that this is a bad idea
                last;
            }
           
            $combo_iter = $combo_model->iter_next($combo_iter);
           
        }
       
        # If we haven't found a match, default to a zero
        if ( !$found_match ) {
            $new_text = 0; # This may also be a bad idea
        }
       
    }
   
    # Test to see if there is *really* a change or whether we've just
received a double-click
    # or something else that hasn't actually changed the data
    my $old_text = $model->get( $iter, $column_no );
   
    if ( $old_text ne $new_text ) {
       
        if ( $self->{fields}->[$column_no - 1]->{validation} ) { # Array
of field defs starts at zero
            if ( ! $self->{fields}->[$column_no - 1]->{validation}(
                                    {
                                        renderer    => $renderer,
                                        text_path    => $text_path,
                                        new_text    => $new_text
                                    }
                                     )
               ) {
                return FALSE; # Error dialog should have already been
produced by validation code
            }
        }
       
        $model->set( $iter, $column_no, $new_text );
       
    }
   
    return FALSE;
   
}

-- 
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: dkasak nusconsulting com au
website: http://www.nusconsulting.com.au



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