List Info

Thread: CellRendererSpinButton in a List




CellRendererSpinButton in a List
user name
2006-03-05 20:55:09
Hi, 
I've been beating my head against a wall
for awhile, so I better ask muppet....where the buck stops.


I'm trying to setup a basic example for myself, putting
different cellrenderers into a list.
I have the text, combos, and toggle setup ok, but
the spinbutton is giving me fits. 

I've used the basic CellRendererSpinButton package
which has been mentioned here before. The problem is that
I can't seem to get the spin box to update to it's new
value
in the list. I can spin them up and down, and when I set
them,
with a click or enter, they revert to 0.

If I manually edit the cell, and press enter, the liststore
seems
to get the value, but it does not appear in the spinbox
cell, which
remains 0.00.  

In the interest of simplicity, I put a super simple example
below.

#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
use Gtk2::Gdk::Keysyms;

package Gtk2::CellRendererSpinButton;
use POSIX qw(DBL_MAX UINT_MAX);
use constant x_padding => 2;
use constant y_padding => 3;

use Glib::Object::Subclass "Gtk2::CellRenderer",
 signals => {
   edited => {
      flags       => [ qw(run-last) ],
      param_types => [ qw(Glib::String Glib:ouble)
],
   },
 },
 properties => [
   Glib::ParamSpec->double(
      "xalign", "Horizontal
Alignment", "Where am i?", 0.0, 1.0, 1.0,
[ qw(readable writable) ]
   ),
   Glib::ParamSpec->boolean(
      "editable", "Editable",
"Can I change that?", 0, [ qw(readable writable)
]
   ),
   Glib::ParamSpec->uint(
      "digits", "Digits", "How
picky are you?", 0, UINT_MAX, 2, [ qw(readable
writable) ]
   ),
   map {
      Glib::ParamSpec->double(
         $_->[ 0 ],
         $_->[ 1 ],
         $_->[ 2 ],
         0.0, DBL_MAX,
         $_->[ 3 ],
         [ qw(readable writable) ]
       )
    } (
      [ "value", "Value", "How
much is the fish?",    0.0 ],
      [ "min",   "Min",   "No
way, I have to live!",  0.0 ],
      [ "max",   "Max",   "Ah,
you're too generous.", 100.0 ],
      [ "step",  "Step", 
"Okay.",                    5.0 ]
    )
 ];

sub INIT_INSTANCE {
   my $self = shift;
   $self-> = 0;
   $self->   = 2;
   $self->    = 0.0;
   $self->      = 0.0;
   $self->      = 100.0;
   $self->     = 5.0;
   $self->   = 1.0;
}

sub calc_size {
   my ( $cell, $layout, $area ) = _;
   my ( $width, $height ) = $layout->get_pixel_size();

   return (
        $area
      ? $cell-> * ( $area->width - ( $width
+ 3 * x_padding ) )
      : 0,
      0,
      $width + x_padding * 2,
      $height + y_padding * 2
   );
}

sub format_text {
   my $cell = shift;
   my $format = sprintf '%%.%df', $cell->;
   sprintf $format, $cell->;
}

sub GET_SIZE {
   my ( $cell, $widget, $area ) = _;

   my $layout = $cell->get_layout( $widget );
   $layout->set_text( $cell->format_text );

   return $cell->calc_size( $layout, $area );
}

sub get_layout {
   my ( $cell, $widget ) = _;

   return $widget->create_pango_layout( "" );
}

sub RENDER {
   my ( $cell, $window, $widget, $background_area,
$cell_area, $expose_area,
      $flags )
    = _;
   my $state;

   if ( $flags & 'selected' ) {
      $state =
       $widget->has_focus()
       ? 'selected'
       : 'active';
   }
   else {
      $state =
       $widget->state() eq 'insensitive'
       ? 'insensitive'
       : 'normal';
   }

   my $layout = $cell->get_layout( $widget );
   $layout->set_text( $cell->format_text );

   my ( $x_offset, $y_offset, $width, $height ) =
    $cell->calc_size( $layout, $cell_area );
   $widget->get_style->paint_layout(
      $window,
      $state,
      1,
      $cell_area,
      $widget,
      "cellrenderertext",
      $cell_area->x() + $x_offset + x_padding,
      $cell_area->y() + $y_offset + y_padding,
      $layout
   );
}

sub START_EDITING {
   my ( $cell, $event, $view, $path, $background_area,
$cell_area, $flags ) =
    _;
   my $spin_button =
    Gtk2::SpinButton->new_with_range( $cell->get(
qw(min max step) ) );

   $spin_button->set_value( $cell->get(
"value" ) );
   $spin_button->set_digits( $cell->get(
"digits" ) );

   $spin_button->grab_focus();

   $spin_button->signal_connect(
      key_press_event => sub {
         my ( $event_box, $event ) = _;

         if (  $event->keyval == $Gtk2::Gdk::Keysyms
            || $event->keyval == $Gtk2::Gdk::Keysyms )
         {
            $spin_button->update();
            $cell->signal_emit( edited => $path,
$spin_button->get_value() );
            $spin_button->destroy();
            return 1;
         }
         elsif ( $event->keyval == $Gtk2::Gdk::Keysyms ) {
            $spin_button->spin( 'step-forward',
               ( $spin_button->get_increments() )[ 0 ] );
            return 1;
         }
         elsif ( $event->keyval == $Gtk2::Gdk::Keysyms ) {
            $spin_button->spin( 'step-backward',
               ( $spin_button->get_increments() )[ 0 ] );
            return 1;
         }

         return 0;
      }
   );

   $spin_button->show_all();

   return $spin_button;
}
1;

############################################################
####
package main;

my $window = Gtk2::Window->new( "toplevel" );
$window->set_title( "CellRendererSpinButton"
);
$window->signal_connect( delete_event => sub {
Gtk2->main_quit(); } );

my $model = Gtk2::ListStore->new( qw(Glib:ouble)
);
my $view  = Gtk2::TreeView->new( $model );

#setup init value 
$model->set( $model->append(), 0 => 42.42 );

my $renderer = Gtk2::CellRendererSpinButton->new();

$renderer->set(
   mode   => "editable",
   min    => 0,
   max    => 1000,
   step   => 2,
   digits => 1
);

$renderer->signal_connect( edited =>
\&cell_edited );

my $column =
 Gtk2::TreeViewColumn->new_with_attributes( "1
digit", $renderer,
   value => 0 );

$view->append_column( $column );

my $vbox = Gtk2::VBox->new( 0, 5 );

$vbox->pack_start( $view, 1, 1, 0 );

my $button = Gtk2::Button->new( "Show Values"
);
$button->signal_connect( "clicked" => sub
{ &show_values(_); } );
$vbox->pack_start( $button, 0, 0, 0 );
$window->add( $vbox );
$window->show_all();

#use Devel::Symdump;
#my $obj =
Devel::Symdump->new('Gtk2::CellRendererSpinButton');
#print $obj->as_string;

Gtk2->main();
############################################################
########
sub cell_edited {
   my ( $cell, $path, $new_value ) = _;

   $model->set( $model->get_iter(
Gtk2::TreePath->new_from_string( $path ) ),
      0 => $new_value );
}
############################################################
########
sub show_values {
    
    my $treeselection = $view->get_selection;
    my $iter = $treeselection->get_selected;

   print  $model->get( $iter, 0 ),"\n" ;
    
}


__END__

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.
html
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
CellRendererSpinButton in a List
user name
2006-03-05 21:45:59
zentara wrote:
> I've used the basic CellRendererSpinButton package
> which has been mentioned here before. The problem is
that
> I can't seem to get the spin box to update to it's
new value
> in the list. I can spin them up and down, and when I
set them,
> with a click or enter, they revert to 0.
>
> If I manually edit the cell, and press enter, the
liststore seems
> to get the value, but it does not appear in the spinbox
cell, which
> remains 0.00.  
>
>   
This sounds astonishingly similar to a problem I'm having
with 
CellRendererCombo:
http
://bugzilla.gnome.org/show_bug.cgi?id=317387 ( see above
post )

Basically, CellRendererText is set up so that when the focus
leaves the 
editable things, an 'edited' signal is fired. This means
that a user can 
type stuff in a cell, and then immediately click outside the
TreeView, 
and the changes are detected ( ie the edited signal is fired
).

Unfortunately, this doesn't happen ( automatically ) in
other renderers, 
for example CellRendererCombo, and your
CellRendererSpinButton. In these 
cases, you have to force an edited signal by pressing the
Enter key ... 
or by clicking somewhere else inside the TreeView ( but
outside the 
current cell ). In your case, since you only have one cell
in the 
TreeView, there's nothing else to click into, so your only
way of 
triggering the 'edited' signal is to use the keyboard. If
there is some 
way to *make* this happen in code, then I'm all ears  This issue
is 
going to present a large problem for me soon too ...

-- 
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: dkasaknusconsulting.com.au
website: http://www.nusconsult
ing.com.au
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
CellRendererSpinButton in a List
user name
2006-03-05 22:17:40
On Mon, Mar 06, 2006 at 08:45:59AM +1100, Daniel Kasak
wrote:
> zentara wrote:
> >I've used the basic CellRendererSpinButton package
> >which has been mentioned here before. The problem
is that
> >I can't seem to get the spin box to update to
it's new value
> >in the list. I can spin them up and down, and when
I set them,
> >with a click or enter, they revert to 0.
> >
> >If I manually edit the cell, and press enter, the
liststore seems
> >to get the value, but it does not appear in the
spinbox cell, which
> >remains 0.00.  
saw this problem passing though, checked my own apps, and
indeed, same
problem here....

-- 
ciao bboett
============================================================
==
bboettadlp.org
http://inforezo.
u-strasbg.fr/~bboett
============================================================
===
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
CellRendererSpinButton in a List
user name
2006-03-06 03:35:54
On Mar 5, 2006, at 4:45 PM, Daniel Kasak wrote:

> Basically, CellRendererText is set up so that when the
focus leaves  
> the editable things, an 'edited' signal is fired.
This means that a  
> user can type stuff in a cell, and then immediately
click outside  
> the TreeView, and the changes are detected ( ie the
edited signal  
> is fired ).
>
> Unfortunately, this doesn't happen ( automatically )
in other  
> renderers, for example CellRendererCombo, and your  
> CellRendererSpinButton. In these cases, you have to
force an edited  
> signal by pressing the Enter key ... or by clicking
somewhere else  
> inside the TreeView ( but outside the current cell ).
In your case,  
> since you only have one cell in the TreeView, there's
nothing else  
> to click into, so your only way of triggering the
'edited' signal  
> is to use the keyboard. If there is some way to *make*
this happen  
> in code, then I'm all ears  This issue
is going to present a  
> large problem for me soon too ...

You've answered your own question ---- add code to your
START_EDITING  
to hook up a handler to the new editable's
"focus-out-event" signal.



--
I believe that if music companies are going to set examples
they need  
to do it to appropriate people and not dead people.
   --  Robin Chianumba

_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
CellRendererSpinButton in a List
user name
2006-03-06 17:34:55
On Sun, 5 Mar 2006 22:35:54 -0500
muppet <scottasofyet.org> wrote:

>You've answered your own question ---- add code to your
START_EDITING  
>to hook up a handler to the new editable's
"focus-out-event" signal.

Yeah, I'm starting to play around with signals in the
SpinCellRenderer
and am getting results. 
But I still cannot alter the text which appears in the
column after
the Spin Button disaappears. I can get the value which I
want to set there,
but I can't get it in.

Is there a way I can keep the spincell widget displayed
constantly? 
That would be preferable for me anyways, I like seeing that
widget
sitting there.

Thanks.

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.
html
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
CellRendererSpinButton in a List
user name
2006-03-06 19:48:57
On Mon, 6 Mar 2006 12:34:55 -0500
zentara <zentara1sbcglobal.net> wrote:

>On Sun, 5 Mar 2006 22:35:54 -0500
>muppet <scottasofyet.org> wrote:
>
>>You've answered your own question ---- add code to
your START_EDITING  
>>to hook up a handler to the new editable's
"focus-out-event" signal.
>
>Yeah, I'm starting to play around with signals in the
SpinCellRenderer
>and am getting results. 
>But I still cannot alter the text which appears in the
column after
>the Spin Button disaappears. I can get the value which I
want to set there,
>but I can't get it in.
>
>Is there a way I can keep the spincell widget displayed
constantly? 
>That would be preferable for me anyways, I like seeing
that widget
>sitting there.
>

Well sorry Muppet, I don't want to waste your time, but I
think I found the
problem.

It's only preliminary, but I'm sure I have a handle on it.

The problem seems to be related to the
CellRendererSpinButton
package needs a section like

sub SET_PROPERTY {
    my ($self, $pspec, $val) = _;

    if ($pspec->get_name eq 'value') {
        $self-> = $val;
     }
}

That lets the initial value set in the ListStore's cells
when you
load the list store with initial values.

I have to work out the exact details of the SET_PROPERTY
sub, to let all the settings thru, but I'm sure the problem
is solved.

I'll have an example soon. 

zentara


-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.
html
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
CellRendererSpinButton in a List
user name
2006-03-07 03:44:32
On Mar 6, 2006, at 12:34 PM, zentara wrote:

> On Sun, 5 Mar 2006 22:35:54 -0500
> muppet <scottasofyet.org> wrote:
>
>> You've answered your own question ---- add code to
your START_EDITING
>> to hook up a handler to the new editable's
"focus-out-event" signal.
>
> Yeah, I'm starting to play around with signals in the
SpinCellRenderer
> and am getting results.
> But I still cannot alter the text which appears in the
column after
> the Spin Button disaappears. I can get the value which
I want to  
> set there,
> but I can't get it in.

After seeing the code you sent out in the
CellRendererSpinButtonZ  
thread, it appears that  you were leaving out the model in
the model- 
view-controller setup.

You were using a global variable to store the value for
editing, and  
to update it for later.  This looks like you were
controlling what  
value shows up in the cell renderer, but not what was
actually in the  
model.

The idea of the cell editor is not that it directly
manipulates the  
model.  The model holds the data.  The view displays the
data.  The  
cell renderer does the actual drawing of the data.  The cell
renderer  
lets you start editing, but that editing is merely a service
to get  
input, which the aggregating layer (main program,
application code,  
or whatever layer set up the whole treeview) responds to by
altering  
the model.

That is, the cell emits the "edited" signal to
let other code update  
the model.  The cell should not alter the model.


> Is there a way I can keep the spincell widget displayed
constantly?
> That would be preferable for me anyways, I like seeing
that widget
> sitting there.

I think that's a different beast.  From what i understand,
the  
TreeView is set up to expect one cell to be edited at a
time.  Having  
a whole bunch of editor widgets up in a treeview would be
more like a  
spreadsheet widget than a treeview.

You could write your own RENDER that draws the cell with
decorations  
that look like a SpinButton if you really like the look of
it.


--
In some newer operating systems, time_t has been widened to
64 bits.  
In the negative direction, this goes back more than twenty
times the  
age of the universe, and so suffices. In the positive
direction,  
whether this range is sufficient to represent all possible
times  
depends on the ultimate fate of the universe, but it can be
expected  
to postpone overflow long enough for such implementation
limits to be  
abolished.
   -- Wikipedia, on UNIX time.

_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
CellRendererSpinButton in a List
user name
2006-03-07 03:38:16
On Mar 6, 2006, at 2:48 PM, zentara wrote:

> Well sorry Muppet, I don't want to waste your time,
but I think I  
> found the problem.

Indeed i talk a lot, but there's more people on this list
than just  
me, you know.  


> It's only preliminary, but I'm sure I have a handle
on it.
>
> The problem seems to be related to the
CellRendererSpinButton
> package needs a section like
>
> sub SET_PROPERTY {
>     my ($self, $pspec, $val) = _;
>
>     if ($pspec->get_name eq 'value') {
>         $self-> = $val;
>      }
> }

This is exactly the same as the XS implementation that is
used as a  
fallback if you don't supply a SET_PROPERTY  (since 1.02x? 
i don't  
remember).  I doubt this is actually what fixed your
problem.

> That lets the initial value set in the ListStore's
cells when you
> load the list store with initial values.

I don't grok that.


--
elysse (in labor): is the head the biggest part?
midwife: yes.
elysse: oh, good.

_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
[1-8]

about | contact  Other archives ( Real Estate discussion Medical topics )