Re: Notebook widget with close page button




Dawid Zamirski said:
I'm just starting with Gtk programming, and I would like to know how to
make a little close "x" button on each page of a notebook widget
(simillar to the one in gedit when working on multiple files). Does it
require to build a custom wigdet that inherits from orginal notebook
widget? Thanks

Nope.  The "label" parameter to Notebook's insert_page method is actually a
widget.  For convenience, the bindings will place your text in a label for
you, but in general you pass a widget.

To get a close button, just pass an hbox containing a label and a button
instead of a plain label.



  use strict;
  use Glib 'FALSE';
  use Gtk2 -init;

  my $window = Gtk2::Window->new;
  $window->signal_connect (destroy => sub { Gtk2->main_quit });

  my $notebook = Gtk2::Notebook->new;
  $window->add ($notebook);

  sub make_label {
    my ($text) = @_;
    my $hbox = Gtk2::HBox->new;
    my $label = Gtk2::Label->new ($text);
    my $button = Gtk2::Button->new ("x"); # a pixmap would look nicer
    $button->signal_connect (clicked => sub {
          $notebook->remove_page ($notebook->get_current_page);
      });
      $hbox->pack_start ($label, FALSE, FALSE, 0);
      $hbox->pack_start ($button, FALSE, FALSE, 0);
      $label->show;
      $button->show;

      $hbox
  }

  $notebook->append_page (Gtk2::Label->new ('Page 1'), make_label ('Page 1'));
  $notebook->append_page (Gtk2::Label->new ('Page 2'), make_label ('Page 2'));
  $notebook->append_page (Gtk2::Label->new ('Page 3'), make_label ('Page 3'));

  $window->show_all;
  Gtk2->main;


-- 
muppet <scott at asofyet dot org>



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