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 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:

use strict;
use warnings;

use Gtk2;
use Glib qw/TRUE FALSE/;

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

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

    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 },
        # About
        [   'About',                          'gtk-about',
            '_About',                           '<control>A',
            undef,      \&about,

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

    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 );
    $vbox->pack_start( $ui->get_widget('/MenuBar'), FALSE, FALSE, 0 );



sub about {
    my $about = Gtk2::AboutDialog->new;
    $about->set_version( '5.0' );
    $about->set_comments('Example program.');
                "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"

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:

Dave M

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