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



On Tue, Sep 1, 2009 at 4:29 AM, Steve Manes<gtk magpie com> 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 gnome org
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



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