perlgtk2

Perl Gtk2::Window->list_toplevels


I'm using Perl Gtk2, and I want to see the list of open toplevel windows, using:

my @WL = Gtk2::Window->list_toplevels;
print STDOUT "TOP_WINDOWS:\n @WL\n";
TOP_WINDOWS: 
Gtk2::Window=HASH(0x23a4040) Gtk2::Window=HASH(0x23b5628) Gtk2::Window=HASH(0x1ca6238)

When I dump this list:
print STDOUT "DUMPER:\n", Dumper (@WL);
... it yields this:
DUMPER:
$VAR1 = bless( {}, 'Gtk2::Window' );
$VAR2 = bless( {}, 'Gtk2::Window' );
$VAR3 = bless( {}, 'Gtk2::Window' );

How do I process the above bless objects/value? I'm looking to get the name (or some kind of unique indicator) of each of the top_level window.

FOLLOW-UP QUSTION: The following doesn't work for "state" when I have additional widgets inside the toplevel - I got to believe I'm simply missing something here!

sub SM_callback ()
{
 my ($button, $data) = @_;

 my $DS       = $$data[0];
 my $hostname = $$data[1]; 

 state $DW;
 if ( ! $DW )
 { 
  $DW = Gtk2::Window->new('toplevel');
  $DW->set_default_size(400, 200);
  $DW->set_title($hostname);

  my $VB = Gtk2::VBox->new( 0, 0 );
  $DW->add($VB);

  my $TB = Gtk2::TextBuffer->new();
  $TB->set_text($hostname);

  my $TV = Gtk2::TextView->new_with_buffer($TB);

  my $SW = Gtk2::ScrolledWindow->new( undef, undef );
  $SW->add($TV);

  $VB->add($SW);

  $DW->show_all;
 }
 else
 {
  $DW->present;
 }

}

Solution

  • Here's my final solution:

    By looping thru the list_toplevels in call_back funct, and if top_level window is already opened (or minimized) via checking get_name, then present it, otherwise create the new top_level window (and set_name for future checking).

    #!/usr/bin/perl
    # PURPOSE: Trying to determine how to recognize which toplevel window
    #          is open to avoid opening same toplevel window muliple times.
    
    use warnings;
    use strict;
    use feature qw(say state);
    use Data::Dumper;
    use Gtk2 -init;
    
    #######################################################################
    sub BN_callback 
    {
     my ($button, $dataref) = @_;
     my $widget_name = $$dataref[0];
    
     my @open_TLW = Gtk2::Window->list_toplevels;
    
     foreach my $w ( @open_TLW )
     {
      my $open_name = $w->get_name;
      if ( $open_name eq $widget_name )
      {
       $w->present;
       return;
      } 
     }
    
     my $DW = Gtk2::Window->new('toplevel');
        $DW->set_default_size(400, 200);
        $DW->set_title($widget_name." sigtest_file");
        $DW->set_name($widget_name);
        $DW->show_all;
    }
    #######################################################################
    
    my $TL = Gtk2::Window->new('toplevel');
       $TL->set_default_size(400, 200);
       $TL->set_position('center');
       $TL->set_name('Main_Window');
       $TL->signal_connect('delete-event' => sub { Gtk2->main_quit });
       $TL->show_all;
    
    my $VB = Gtk2::VBox->new( 0, 0 );
    
    $TL->add($VB);
    
    my @BN = qw/BUTTON1 BUTTON2/;
    foreach my $bn ( @BN )
    {
     my $BT = Gtk2::Button->new($bn);
        $BT->set_name($bn); 
        $BT->signal_connect(clicked =>  \&BN_callback, [$bn]);
    
     $VB->add($BT);
    }
    
    $TL->show_all;
    Gtk2->main;