Re: example of menu



On Thu, Nov 25, 2010 at 4:51 AM, haratron <haratron gmail com> wrote:
I can't find any example of a menu (file, edit, help) in gtk-perl. The
best I could find was appwindow.pl in gtk-demo, but that doesn't have
the actual functions implemented, just dummy dialogs.
I'm using Glade.

True, but you can tweak the dummy dialogs to suit your own purposes,
which is the idea.  Here's an example way but it doesn't use glade:
#!/usr/bin/perl

use strict;
use warnings;
$|++;

use Gtk2;
use Glib qw/TRUE FALSE/;
Gtk2->init;

my $window = Gtk2::Window->new();
$window->signal_connect( destroy => sub { Gtk2->main_quit; } );
$window->set_default_size( 250, 200 );
$window->set_title("Example");
$window->set_border_width(5);
$window->set_position('center-always');

my $vbox = Gtk2::VBox->new( FALSE, 0 );
$window->add($vbox);

    my @entries = (
        ### Everything falls under the following 3 rows ###
        [ 'FileMenu',       undef, '_File' ],
        [ 'ViewMenu',       undef, '_View' ],
        [ 'HelpMenu',       undef, '_Help' ],

        # Quit
        [   'Exit',           'gtk-quit',
            'E_xit', '<control>X',
            undef,      sub { Gtk2->main_quit },
            FALSE
        ],
        # About
        [   'About',                          'gtk-about',
            '_About',                           '<control>A',
            undef,      \&about,
            FALSE
        ],
);

    my $ui_info = "<ui>
        <menubar name='MenuBar'>
         <menu action='FileMenu'>
          <separator/>
          <menuitem action='Exit'/>
         </menu>
          <menu action='ViewMenu'>
         </menu>
         <menu action='HelpMenu'>
          <menuitem action='About'/>
         </menu>
        </menubar>
</ui>";

    my $actions = Gtk2::ActionGroup->new('Actions');
    $actions->add_actions( \ entries, undef );

    my $ui = Gtk2::UIManager->new;
    $ui->insert_action_group( $actions, 0 );

    $window->add_accel_group( $ui->get_accel_group );
    $ui->add_ui_from_string($ui_info);
    $vbox->pack_start( $ui->get_widget('/MenuBar'), FALSE, FALSE, 0 );

$window->show_all();

Gtk2->main();

sub about {
    my $about = Gtk2::AboutDialog->new;
    $about->set_authors('TIMTOWTDI');
    $about->set_version( '5.0' );
    $about->set_website('http://gtk2-perl.sf.net');
    $about->set_comments('Example program.');
    $about->set_license(
                "This program is free software; you can redistribute it\n"
                . "and/or modify it under the terms of either:\n\n"
                . "a) the GNU General Public License as published by\n"
                . "the Free Software Foundation; either version 1, or\n"
                . "(at your option) any later version, or\n\n"
                . "b) the 'Artistic License'.\n\n"
        );
    $about->run;
    $about->destroy;
    return;
}

How am I going to launch the aboutdialog when the user clicks on the
help->about menuitem? Also is there an example that shows how to
achieve typical file->open/file/save/save as functionality?
_______________________________________________
This should help:

http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/FileChooserDialog.html

Dave M



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