New to GTK+ and a question about Combo boxes.

View: New views
6 Messages — Rating Filter:   Alert me  

New to GTK+ and a question about Combo boxes.

by Steve Manes-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm building a Perk-GTK application on Windows which includes a "date"
element, which is three ComboBoxes clustered together in an Hbox: month,
day, year.

Since there are several places in the application where a date is added,
I built a function to return an Hbox containing those three combo boxes:

sub make_date
{
     my($element, $min_year, $max_year) = @_;

     my @MONTHS  = qw/Jan Feb Mar Apr May June July Aug Sep Oct Nov Dec/;
     my @DAYS    = (1 .. 31);
     my @YEARS   = ($min_year .. $max_year);

     my $hbox = Gtk2::HBox->new(FALSE, 0);

     # Month
     $E{"${element}_month"}{'obj'} = Gtk2::ComboBox->new_text;
     $E{"${element}_month"}{'obj'}->append_text(' ');
     foreach my $s (@MONTHS) {
         $E{"${element}_month"}{'obj'}->append_text($s);
     }
        $E{"${element}_month"}{'obj'}->signal_connect('changed' => sub {
$E{"${element}_month"}{'data'} =
$E{"${element}_month"}{'obj'}->get_active_text(); } );

     $hbox->pack_start($E{"${element}_month"}{'obj'}, FALSE, FALSE, 0);


     # Day
     $E{"${element}_day"}{'obj'} = Gtk2::ComboBox->new_text;
     $E{"${element}_day"}{'obj'}->append_text(' ');
         foreach my $s (@DAYS) {
         $E{"${element}_day"}{'obj'}->append_text($s);
     }
        $E{"${element}_day"}{'obj'}->signal_connect('changed' => sub {
$E{"${element}_day"}{'data'} =
$E{"${element}_day"}{'obj'}->get_active_text(); } );
     $hbox->pack_start($E{"${element}_day"}{'obj'}, FALSE, FALSE, 0);

     # Year
     $E{"${element}_year"}{'obj'} = Gtk2::ComboBox->new_text;
     $E{"${element}_year"}{'obj'}->append_text(' ');
     foreach my $s (@YEARS) {
         $E{"${element}_year"}{'obj'}->append_text($s);
     }
        $E{"${element}_year"}{'obj'}->signal_connect('changed' => sub {
$E{"${element}_year"}{'data'} =
$E{"${element}_year"}{'obj'}->get_active_text(); } );
     $hbox->pack_start($E{"${element}_year"}{'obj'}, FALSE, FALSE, 0);

     return $hbox;
}

$E is a global hash I'm using for the time being to keep track of all
the elements -- at least until I get more expert with GTK.

My questions:

1) How come whenever I resize the window the ComboBoxes themselves also
expand?  I've tried every combination in the pack_start function to no
avail.

2) How come the pulldown lists appear (for lack of a better term) double
spaced?  Is there a fix for that?

3) How come I can't seem to get rid of the padding between the three
ComboBoxes?  I'd like there to be no space between them.

4) One more oddity: if I stick the output of this function in a table
cell like this:

    $table->attach_defaults(make_date('dob', 1950, 2010), 3, 4, $row,
row+1);

and then give the table a small row spacing, like
$table->set_row_spacings(2), the comboboxes expand to like 3x their
normal height.

I'm guessing that all these symptoms are related to something I don't
understand about GTK+ in general and Comboboxes in particular, at least
versus the comboboxes in TKX.
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: New to GTK+ and a question about Combo boxes.

by Peter Juhasz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Sep 1, 2009 at 4:29 AM, Steve Manes<gtk@...> wrote:

> I'm building a Perk-GTK application on Windows which includes a "date"
> element, which is three ComboBoxes clustered together in an Hbox: month,
> day, year.
>
> Since there are several places in the application where a date is added, I
> built a function to return an Hbox containing those three combo boxes:
>
> sub make_date
> {
>    my($element, $min_year, $max_year) = @_;
>
>    my @MONTHS  = qw/Jan Feb Mar Apr May June July Aug Sep Oct Nov Dec/;
>    my @DAYS    = (1 .. 31);
>    my @YEARS   = ($min_year .. $max_year);
>
>    my $hbox = Gtk2::HBox->new(FALSE, 0);
>
>    # Month
>    $E{"${element}_month"}{'obj'} = Gtk2::ComboBox->new_text;
>    $E{"${element}_month"}{'obj'}->append_text(' ');
>    foreach my $s (@MONTHS) {
>        $E{"${element}_month"}{'obj'}->append_text($s);
>    }
>        $E{"${element}_month"}{'obj'}->signal_connect('changed' => sub {
> $E{"${element}_month"}{'data'} =
> $E{"${element}_month"}{'obj'}->get_active_text(); } );
>
>    $hbox->pack_start($E{"${element}_month"}{'obj'}, FALSE, FALSE, 0);
>
>
>    # Day
>    $E{"${element}_day"}{'obj'} = Gtk2::ComboBox->new_text;
>    $E{"${element}_day"}{'obj'}->append_text(' ');
>        foreach my $s (@DAYS) {
>        $E{"${element}_day"}{'obj'}->append_text($s);
>    }
>        $E{"${element}_day"}{'obj'}->signal_connect('changed' => sub {
> $E{"${element}_day"}{'data'} =
> $E{"${element}_day"}{'obj'}->get_active_text(); } );
>    $hbox->pack_start($E{"${element}_day"}{'obj'}, FALSE, FALSE, 0);
>
>    # Year
>    $E{"${element}_year"}{'obj'} = Gtk2::ComboBox->new_text;
>    $E{"${element}_year"}{'obj'}->append_text(' ');
>    foreach my $s (@YEARS) {
>        $E{"${element}_year"}{'obj'}->append_text($s);
>    }
>        $E{"${element}_year"}{'obj'}->signal_connect('changed' => sub {
> $E{"${element}_year"}{'data'} =
> $E{"${element}_year"}{'obj'}->get_active_text(); } );
>    $hbox->pack_start($E{"${element}_year"}{'obj'}, FALSE, FALSE, 0);
>
>    return $hbox;
> }
>
> $E is a global hash I'm using for the time being to keep track of all the
> elements -- at least until I get more expert with GTK.
>
> My questions:
>
> 1) How come whenever I resize the window the ComboBoxes themselves also
> expand?  I've tried every combination in the pack_start function to no
> avail.
>
> 2) How come the pulldown lists appear (for lack of a better term) double
> spaced?  Is there a fix for that?
>
> 3) How come I can't seem to get rid of the padding between the three
> ComboBoxes?  I'd like there to be no space between them.
>
> 4) One more oddity: if I stick the output of this function in a table cell
> like this:
>
>   $table->attach_defaults(make_date('dob', 1950, 2010), 3, 4, $row, row+1);
>
> and then give the table a small row spacing, like
> $table->set_row_spacings(2), the comboboxes expand to like 3x their normal
> height.
>
> I'm guessing that all these symptoms are related to something I don't
> understand about GTK+ in general and Comboboxes in particular, at least
> versus the comboboxes in TKX.
> _______________________________________________
> gtk-perl-list mailing list
> gtk-perl-list@...
> http://mail.gnome.org/mailman/listinfo/gtk-perl-list
>

I had similar problems when I wanted to pack() several widgets into a
dialog box. I suggest that instead of nested HBoxes and VBoxes, use a
table to organize your widgets in the window. The code will be less
cluttered and you'll have more placement options, especially if you
use the full form of attach() instead of attach_defaults().

Furthermore, SpinButtons may be better than Comboboxes for entering
the date, at least if you don't cling to named months.

Alternatively, you could use the built-in date selection dialog to let
the user select the date comfortably. Or you could write a custom cell
renderer with a date selector. There are demonstrations for both of
these things among the examples.

Some lines from my program (it's a time selector but that's basically the same):

        my $page = Gtk2::Table->new(10, 8, FALSE);
        $page->set_row_spacings(10);
        $page->set_col_spacings(0); # !!!
# Other declarations snipped...
        $interval_start_label =
Gtk2::Label->new($gui_strings{interval_start_label});
        $interval_start_label->set_alignment (1.0, 0.5);
        $page->attach_defaults($interval_start_label, 1,2, 7,8 );
        $interval_start_entry = Gtk2::Entry->new();
        $page->attach_defaults($interval_start_entry, 2,3, 7,8 );
        $interval_start_entry->set_editable(TRUE);
        #$interval_start_entry->set_text($db{$id}{record}{meas_start}) if
($mode eq 'edit');
        $interval_start_btn = Gtk2::Button->new($gui_strings{calendar});
        $interval_start_btn->signal_connect(clicked => sub {
                                                                                                my $ret = calendar($assistant, $tempdb{meas_start});
                                                                                                if (defined $ret) {
                                                                                                        $tempdb{meas_start} = $ret;
                                                                                                        $interval_start_entry->set_text($tempdb{meas_start});
                                                                                                        $tempdb{meas_end} = next_day($tempdb{meas_start});
                                                                                                        $interval_end_entry->set_text($tempdb{meas_end});
                                                                                                }
                                                                                        });
        $page->attach_defaults($interval_start_btn, 3,4, 7,8 );
        my $hour_adj_start = Gtk2::Adjustment->new(8.0, 0.0, 23.0, 1.0, 4.0, 0.0);
        my $min_adj_start = Gtk2::Adjustment->new(0.0, 0.0, 59.0, 1.0, 10.0, 0.0);
        my $sec_adj_start = Gtk2::Adjustment->new(0.0, 0.0, 59.0, 1.0, 10.0, 0.0);
        $hour_spin_start = Gtk2::SpinButton->new($hour_adj_start, 0, 0);
        $min_spin_start = Gtk2::SpinButton->new($min_adj_start, 0, 0);
        $sec_spin_start = Gtk2::SpinButton->new($sec_adj_start, 0, 0);
        $page->attach_defaults($hour_spin_start, 4,5, 7,8 );
        $page->attach_defaults($min_spin_start, 5,6, 7,8 );
        $page->attach_defaults($sec_spin_start, 6,7, 7,8 );

Here, calendar() is a function that brings up the stock date selector
returns the user's choice.

One more thing... instead of  $E{"${element}_year"}{'data'}, you could
simply write $E{${element}_year}{data}.

Wish you luck with your project:
Péter Juhász
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: New to GTK+ and a question about Combo boxes.

by Peter Juhasz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Sep 2, 2009 at 9:31 PM, Peter Juhasz<peter.juhasz83@...> wrote:

> One more thing... instead of  $E{"${element}_year"}{'data'}, you could
> simply write $E{${element}_year}{data}.

Sorry, I didn't pay attention, that's not entirely true.
Still, $E{${element}."_year"}{data} would be better in my opinion.
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: New to GTK+ and a question about Combo boxes.

by muppet-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Aug 31, 2009, at 10:29 PM, Steve Manes wrote:

> I'm building a Perk-GTK application on Windows which includes a  
> "date" element, which is three ComboBoxes clustered together in an  
> Hbox: month, day, year.
>
> Since there are several places in the application where a date is  
> added, I built a function to return an Hbox containing those three  
> combo boxes:
>
> sub make_date
> {

[snip packing a bunch of stuff into a new hbox]

>    return $hbox;
> }


> $E is a global hash I'm using for the time being to keep track of  
> all the elements -- at least until I get more expert with GTK.
>
> My questions:
>
> 1) How come whenever I resize the window the ComboBoxes themselves  
> also expand?  I've tried every combination in the pack_start  
> function to no avail.

You didn't mention whether they expand vertically or horizontally.  If  
they're expanding vertically, it's not a problem with your hbox  
packing, but with how you pack the hbox into the window -- if you let  
the hbox expand inside a vbox, you'll get what i think you're talking  
about.

If they're still expanding horizontally, please post a bit more code  
-- just enough that we can run it and play with it.


Screenshots would help.  ;-)


> 2) How come the pulldown lists appear (for lack of a better term)  
> double spaced?  Is there a fix for that?

Do you have newlines in your strings?  Perhaps it's a theme issue?


> 3) How come I can't seem to get rid of the padding between the three  
> ComboBoxes?  I'd like there to be no space between them.
>
> 4) One more oddity: if I stick the output of this function in a  
> table cell like this:
>
>   $table->attach_defaults(make_date('dob', 1950, 2010), 3, 4, $row,  
> row+1);
>
> and then give the table a small row spacing, like $table-
> >set_row_spacings(2), the comboboxes expand to like 3x their normal  
> height.

This one sounds like you need to attach() instead of attach_defaults
(), and choose the "please don't expand in any direction" options.




--
me, while driving the car: Okay, girls, what do you want for lunch?
yvonne: I wan' noo-tulls!
zella: I want lavaloli!  Can we go to the lavaloli store?
me: Um, where *is* the ravioli store?
zella: At the lavaloli store!
yvonne: I want noo-tulls!  Let's go to the noo-tull store!

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: New to GTK+ and a question about Combo boxes.

by Kevin Ryde :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Steve Manes <gtk@...> writes:
>
> $E is a global hash I'm using for the time being to keep track of all
> the elements -- at least until I get more expert with GTK.

The hash for each widget is a good place to hold child widgets you want
to use again later.

> 2) How come the pulldown lists appear (for lack of a better term)
> double spaced?  Is there a fix for that?

If it's not the newlines muppet said then there's also a small amout of
y padding in CellRendererText which annoyed me (over the plain
GtkCList).  Force it down with

    $renderer->set(ypad=>0);

Not sure if you have to break out the full ComboBox->new instead of
->new_text to fiddle with the renderer.
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: New to GTK+ and a question about Combo boxes.

by Steve Manes-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Kevin Ryde wrote:
>> 2) How come the pulldown lists appear (for lack of a better term)
>> double spaced?  Is there a fix for that?
>
> If it's not the newlines muppet said then there's also a small amout of
> y padding in CellRendererText which annoyed me (over the plain
> GtkCList).  Force it down with
>
>     $renderer->set(ypad=>0);

Thanks.  I'll look at the 'set' method.  Here is what worked for me though:

     ...
     my $model = Gtk2::ListStore->new('Glib::String');

     # Load the pulldown list.
     foreach (qw/line1 line2 line3 line4/) {
         $model->set($model->append, 0, $_);
     }

     my $combo_box = Gtk2::ComboBox->new($model);
     my $renderer = Gtk2::CellRendererText->new;

     # Set the row height to 8px. Leave row width at default.
     $renderer->set_fixed_size(-1, 8);

     $combo_box->pack_start($renderer, TRUE);
     $combo_box->add_attribute($renderer, text => 0);

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list