Re: EXTERNAL: Re: Gtk2::SpinButton For Hex
A short hint to those who need a "pedestrian implementation" of a hex spinbox under GTK3: there things are substantially easier packing the various ingredients into a GRID widget. Dont hesitate to ask for a corresponding code example. Juergen ___ gtk-perl-list mailing list gtk-perl-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-perl-list
RE: EXTERNAL: Re: Gtk2::SpinButton For Hex
> Looks like twisting the implementation of the standard Spinbutton widget is > not quite easy to be achieved - maybe my alternative is quicker to put to > effective use. Yeah, I've toyed with doing something along these lines, using other widgets to create a poor man's spin button. My hex inputs use Gtk2::CellRendererSpins in a Gtk2::TreeView, but I think the same thing could be done. It's good to see an implementation if this idea, and I wasn't aware of the icon attributes Gtk2::Entry provides. Thanks for posting your implementation. Jim ___ gtk-perl-list mailing list gtk-perl-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-perl-list
Re: EXTERNAL: Re: Gtk2::SpinButton For Hex
Hi, Looks like twisting the implementation of the standard Spinbutton widget is not quite easy to be achieved - maybe my alternative is quicker to put to effective use. I append code that I reduced from a gtk2 application I did a couple of years ago, that implements a hex Spinbutton as a set of procedures. I am leaving for 8 days of vacation tomorrow morning, did this very rapidly without doing much testing. However, I realized some things that need improving: use fixed format + size of entry box, parametrize the number of digits (presently hard-wired to 4 digits - as needed in my old application): this code is an example of a an approach that works, but needs to be improved to become a library procedure. Not included in the example, but easy to implement, a "get_value" and a "set_value" procedure, and possibly a "step" parameter. I also stuck to keeping this a set of procedures, rather than implementing a widget. Juergen demo code of a hex spin-button implementation == for adequate display, set editor tabs to 4 == #! /usr/bin/perl use strict; use warnings; use diagnostics; use Gtk2'-init'; use Glibqw (TRUE FALSE ); use constant SPIN_HEIGHT => 16; # height of arrow pixmap (must # correspond to pixmap data) use constant SPIN_MIDDLE => ( SPIN_HEIGHT / 2 ); use constant VERT_MARGIN=> 3;# top and bottom margin of widget use constant REPEAT_LATENCY => 700; # msec before first auto repeat use constant REPEAT_INTERVAL => 30; # msec before following auto repeat # Pixmap for arrow item of hex pseudo spinbox # --- my $arrow_xpm = Gtk2::Gdk::Pixbuf->new_from_xpm_data ( "11 16 3 1", " c None", "+ c Black", "- c Gray80", "-+-", " -+++- ", " -++-++- ", " -++- -++- ", "-++- -++-", "--- ---", " ", " ", " ", " ", "--- ---", "-++- -++-", " -++- -++- ", " -++-++- ", " -+++- ", "-+- "); # Arrow-button event in pseudo-spinbox entry widget # = # Argument #0 : entry widget (pseudo spinbutton) # #1 : entry-item-position step: +1 or -1 # #2 : event '' # #3 : threshold y-coordinate between up and down zone sub SpinarrowHit { my ( $p_box, $p_pos, $p_event, $p_middle ) = @_; my ( $x_step, $time_out ); if ( ${$p_box}{'REPEAT'} ne '' ) { # Spin arrow button released: cancel repeat timer Glib::Source->remove ( ${$p_box}{'REPEAT'} ); ${$p_box}{'REPEAT'} = ''; } if ( ref ($p_event) =~ /Button/ ) { # Spin arrow button pressed: step the value, set repeat timer if ( index ($p_event->type, 'release' ) > 0 ) { unless ( ${$p_box}{'REPEAT'} eq '' ) { Glib::Source->remove ( ${$p_box}{'REPEAT'} ); ${$p_box}{'REPEAT'} = ''; } return; } my $pos_y = $p_event->y; if ( $pos_y <= $p_middle - 1 ) { $x_step = 1; } elsif ( $pos_y > $p_middle + 1 ) { $x_step = -1; } else { return; } $time_out = REPEAT_LATENCY; } else { # Repeat timer struck: step and re-launch the repeat timer $x_step = $p_pos; $time_out = REPEAT_INTERVAL; } my $x_value = $p_box->get_text (); $x_value =~ s/\s*//; unless ( $x_value =~ /^[0-9a-f]+$/i ) { return; } $x_value= hex ( $x_value ); if ( ( $x_step == 1 ) && ( $x_value >= ${$p_box}{'MAX'} ) ) { return; } if ( ( $x_step == -1 ) && ($x_value <= ${$p_box}{'MIN'} ) ) { return; } ${$p_box}{'VALUE'} = $x_value + $x_step; $p_box->set_text ( sprintf ( "%04x", $x_value + $x_step ) ); ${$p_box}{'REPEAT'} = Glib::Timeout->add ( $time_out, sub { SpinarrowHit ( $p_box, $x_step, '', $p_middle );
RE: EXTERNAL: Re: Gtk2::SpinButton For Hex
> > >> How can I use a Gtk2::SpinButton to prompt for an integer expressed in > > >> hex? I've tried the following, but it fails. > > >> > > >> ... > > >> > > >> The text appears to be correct while I hold either arrow button down; I > > >> see hex values incrementing. However, single clicks of an arrow button > > >> fail if the displayed text contains A-F. Hitting the Enter key also > > >> fails on the same values. In both cases, the text changes to a decimal > > >> integer. > > > > > > What do the '$value's look like in the outputCB (either in a debugger or > > > ...). > > > > With single clicks of the up arrow button, the values in outputCB() > > increment from 0 to 10. 10 correctly displays as A. One more click of the > > up arrow calls outputCB() twice for some reason, the first time with a > > value of 0, and the second with a value of 1. So visibly, 10 wraps to 1 > > instead of 11, or B. If I manually type 'FF' and hit the Enter key, > > outputCB() is called with a value of 0. I've played with callbacks on the > > 'input' and 'changed' signals too, but nothing has worked so far. > > > > I learned something else. If I change the sprintf() to use '0x%X' instead > > of '%X', it seems to work. It also works in octal with '0%o' and binary > > with '0b%b'. > > > > Unfortunately, the '0x' prefix is unacceptable to my users. Grr. So I'm > > still in search of a way to spin a hex value, but with no '0x'. Knowing > > this about the prefix, though, still may be useful to others wanting > > something similar. > > Hi Jim > > If that is the case can you not use chain the sprintf through substr? > > e.g. > > > $spin->set_text(substr (sprintf '0x%X',$value), 2); > > which will drop the first 2 characters of the string that is printed > by > sprintf? That's the same as what I had originally, which failed. $spin->set_text(sprintf('%X',$value)); Gtk2::SpinButton seems to spin hex values fine if the "0x" is in the string. Your substr() version and my original fail, I think, because the "0x" is missing. My users, of course, don't want to type the "0x" since the input is always hex, and it's really a programming convention; not something for a user interface. Thanks. Jim ___ gtk-perl-list mailing list gtk-perl-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-perl-list
Re: EXTERNAL: Re: Gtk2::SpinButton For Hex
On Wed, May 20, 2015 at 05:23:53PM +, Williams, James P2 wrote: > >> How can I use a Gtk2::SpinButton to prompt for an integer expressed in > >> hex? I've tried the following, but it fails. > >> > >> ... > >> > >> The text appears to be correct while I hold either arrow button down; I > >> see hex values incrementing. However, single clicks of an arrow button > >> fail if the displayed text contains A-F. Hitting the Enter key also fails > >> on the same values. In both cases, the text changes to a decimal integer. > > > > What do the '$value's look like in the outputCB (either in a debugger or > > ...). > > With single clicks of the up arrow button, the values in outputCB() increment > from 0 to 10. 10 correctly displays as A. One more click of the up arrow > calls outputCB() twice for some reason, the first time with a value of 0, and > the second with a value of 1. So visibly, 10 wraps to 1 instead of 11, or B. > If I manually type 'FF' and hit the Enter key, outputCB() is called with a > value of 0. I've played with callbacks on the 'input' and 'changed' signals > too, but nothing has worked so far. > > I learned something else. If I change the sprintf() to use '0x%X' instead of > '%X', it seems to work. It also works in octal with '0%o' and binary with > '0b%b'. > > Unfortunately, the '0x' prefix is unacceptable to my users. Grr. So I'm > still in search of a way to spin a hex value, but with no '0x'. Knowing this > about the prefix, though, still may be useful to others wanting something > similar. Hi Jim If that is the case can you not use chain the sprintf through substr? e.g. > $spin->set_text(substr (sprintf '0x%X',$value), 2); which will drop the first 2 characters of the string that is printed by sprintf? Bob > On Tue, May 19, 2015 at 10:22:51PM +, Williams, James P2 wrote: > > How can I use a Gtk2::SpinButton to prompt for an integer expressed in hex? > > I've tried the following, but it fails. > > > >use strict; > >use warnings; > > > >use Glib qw(TRUE FALSE); > >use Gtk2 qw(-init); > > > >my($spin)=Gtk2::SpinButton->new_with_range(0,1000,1); > >$spin->set_numeric(FALSE); > >$spin->signal_connect(output => \&outputCB); > > > >my($win)=new Gtk2::Window(); > >$win->add($spin); > >$win->show_all(); > > > >Gtk2->main(); > > > >sub outputCB > >{ > > my($spin)=@_; > > my($value)=$spin->get_adjustment()->get_value(); > > > > $spin->set_text(sprint '%X',$value); > >} > > > > The text appears to be correct while I hold either arrow button down; I see > > hex values incrementing. However, single clicks of an arrow button fail if > > the displayed text contains A-F. Hitting the Enter key also fails on the > > same values. In both cases, the text changes to a decimal integer. > > > > Thanks. > > > > Jim > Hi Jim > > What do the '$value's look like in the outputCB (either in a debugger or ...). > > sprint - should be sprintf? > > Maybe try set_value or set_digits rather than set_text? > > How are any of these impacted by the set_numeric statement? > > Bob > ___ > gtk-perl-list mailing list > gtk-perl-list@gnome.org > https://mail.gnome.org/mailman/listinfo/gtk-perl-list ___ gtk-perl-list mailing list gtk-perl-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-perl-list
RE: EXTERNAL: Re: Gtk2::SpinButton For Hex
>> How can I use a Gtk2::SpinButton to prompt for an integer expressed in hex? >> I've tried the following, but it fails. >> >> ... >> >> The text appears to be correct while I hold either arrow button down; I see >> hex values incrementing. However, single clicks of an arrow button fail if >> the displayed text contains A-F. Hitting the Enter key also fails on the >> same values. In both cases, the text changes to a decimal integer. > > What do the '$value's look like in the outputCB (either in a debugger or ...). With single clicks of the up arrow button, the values in outputCB() increment from 0 to 10. 10 correctly displays as A. One more click of the up arrow calls outputCB() twice for some reason, the first time with a value of 0, and the second with a value of 1. So visibly, 10 wraps to 1 instead of 11, or B. If I manually type 'FF' and hit the Enter key, outputCB() is called with a value of 0. I've played with callbacks on the 'input' and 'changed' signals too, but nothing has worked so far. I learned something else. If I change the sprintf() to use '0x%X' instead of '%X', it seems to work. It also works in octal with '0%o' and binary with '0b%b'. Unfortunately, the '0x' prefix is unacceptable to my users. Grr. So I'm still in search of a way to spin a hex value, but with no '0x'. Knowing this about the prefix, though, still may be useful to others wanting something similar. > sprint - should be sprintf? Sorry about the "sprint". We're on a closed network; that was a transcription typo. > Maybe try set_value or set_digits rather than set_text? I think I have to call set_text() to see the hex version in the entry. I have tried calling set_value() too, both before and after set_text(). That gives me the same behavior: spinning seems to work, but single clicks wrap to 0 or 1 as soon as I reach a number containing [A-F]+. On set_digits(), I'm spinning integers, so I've left it at the default, 0. It wouldn't make sense being any other value, if I understand its purpose. > How are any of these impacted by the set_numeric statement? I've set this to FALSE, since I have to allow A-F to be typed into the entry. Otherwise, those key events are ignored. Thanks for any other ideas you may have. Jim -Original Message- From: Robert Wilkinson [mailto:b...@fourtheye.org] Sent: Wednesday, May 20, 2015 3:12 AM To: Williams, James P2 Cc: 'gtk-perl-list@gnome.org' Subject: EXTERNAL: Re: Gtk2::SpinButton For Hex On Tue, May 19, 2015 at 10:22:51PM +, Williams, James P2 wrote: > How can I use a Gtk2::SpinButton to prompt for an integer expressed in hex? > I've tried the following, but it fails. > >use strict; >use warnings; > >use Glib qw(TRUE FALSE); >use Gtk2 qw(-init); > >my($spin)=Gtk2::SpinButton->new_with_range(0,1000,1); >$spin->set_numeric(FALSE); >$spin->signal_connect(output => \&outputCB); > >my($win)=new Gtk2::Window(); >$win->add($spin); >$win->show_all(); > >Gtk2->main(); > >sub outputCB >{ > my($spin)=@_; > my($value)=$spin->get_adjustment()->get_value(); > > $spin->set_text(sprint '%X',$value); >} > > The text appears to be correct while I hold either arrow button down; I see > hex values incrementing. However, single clicks of an arrow button fail if > the displayed text contains A-F. Hitting the Enter key also fails on the > same values. In both cases, the text changes to a decimal integer. > > Thanks. > > Jim Hi Jim What do the '$value's look like in the outputCB (either in a debugger or ...). sprint - should be sprintf? Maybe try set_value or set_digits rather than set_text? How are any of these impacted by the set_numeric statement? Bob ___ gtk-perl-list mailing list gtk-perl-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtk-perl-list