Want to add tabstrips to my program but don't want to keep track of
  showing and hiding the individual widgets on each tab.  So here is a
  complete program that shows a possible work-around.

  What I would like to do is add an option to each widget I add to indicate
  the Tab it should belong to.  Something like:

          -tabid => 1

  Where 0 is the first tab, 1 is the second and so on.  Then the "Tab_Click" 
  subroutine below will take care of the rest.  When I tried using -tabid,
  is didn't remain with the widget after it was created.  To get around this
  I'm adding the 'tabid' number to the end of the widget name, eg.

          -name => "tab0",

  This of course is not a very elegant approach.  I tried using -group 
  and -style just to hold the numbers but they also did not remain.

  What does everybody think?

  Aldo, how hard is it to add a '-tabid' to all widgets?

  -dhiltz

------------------------------------------------------------------------
use Win32::GUI;

my $W = new GUI::Window(
    -title    => "Win32::GUI::TabStrip test",
    -left     => 100,
    -top      => 100,
    -width    => 300,
    -height   => 200,
    -name     => "Window",
);

$W->AddTabStrip(
    -name   => "Tab",
    -left   => 0,
    -top    => 0, 
    -width  => $W->ScaleWidth, 
    -height => $W->ScaleHeight,
);

$W->Tab->InsertItem(
    -text => "First", 
);

$W->Tab->InsertItem(
    -text => "Second", 
);

$W->Tab->InsertItem(
    -text => "Third",
);

$W->Tab->AddLabel(
    -name  => "tab0",
    -text  => "Label on TAB 0",
    -left  => 5,
    -top   => 50,
   #-tabid => 0,     # This would be nice if this stayed.
);

$W->Tab->AddLabel(
    -name  => "lab_tab1",
    -text  => "Hello - Label on TAB 1",
    -left  => 5,
    -top   => 30,
);

$W->Tab->AddButton(
    -name => "but_tab1",
    -text => "I'm a button on tab 1",
    -width => 120,
    -height => 20,
    -left => 5,
    -top => 70,
);

$W->Show;

&Tab_Click;  # Initialize display of first tab (only).

Win32::GUI::Dialog();

sub Window_Resize {
    $W->Tab->Resize($W->ScaleWidth, $W->ScaleHeight);
}

#-------------------------------------------------
sub Tab_Click {

    my $current_tab = $W->Tab->SelectedItem();

    foreach $key (keys %{$W->Tab}) {
       # Skip these items - what remains should be just widgets.
       next if (grep(/^${key}$/,qw(-handle -name -type)));

       $ref = $W->Tab->{$key};

       if ($debug) {
          print "key: $key"," value: ",$W->Tab->{$key},"\n";
          print "  -name: ",$ref->{-name},"\n";
          print "  -text: ",$ref->{-text},"\n";
       }

       # Strip off number from end of -name - use as tabid.
       # A better way would be to define something like "-tabid => .."
       # But this does not carry over after the widget is defined.

       $tabid = substr($ref->{-name},-1);
       if ($current_tab == $tabid) {
          $ref->Show();
       }
       else {
          $ref->Hide();
       }
    }
}

Reply via email to