The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

FLTK::Cookbook::MenuBar::Toggle - Shows how to toggle menu items at run time

Description

Demonstrates how to toggle a menu item procedurally. Click on Options to view the toggled menu items.

The Code

    use strict;
    use warnings;
    use FLTK;
    my $win = FLTK::Window->new(720, 486);
    my $menubar = FLTK::MenuBar->new(0, 0, 720, 25);
    $win->add($menubar);
    for my $mnu (qw[One Two Three]) {
        $menubar->add("Options/$mnu")->type(FLTK::Widget::TOGGLE());
    }
    $win->show();

    # Test the state changes -- turn 'One' off, others on
    warn 'FAILED[1]' if (SetMenuItemState($menubar, "Options/One",   0) < 0);
    warn 'FAILED[2]' if (SetMenuItemState($menubar, "Options/Two",   1) < 0);
    warn 'FAILED[3]' if (SetMenuItemState($menubar, "Options/Three", 1) < 0);
    exit FLTK::run();

    sub SetMenuItemState {
        my ($self, $name, $state) = @_;
        my $m = $self->find($name);
        return -1 if !$m;
        return $m->set() if $state;
        $m->clear;
        return 0;
    }

Awknowlegements

Code based on the work of Greg Ercolano

Author

Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/

License and Legal

Copyright (C) 2008-2009 by Sanko Robinson <sanko@cpan.org>

This program is free software; you can redistribute it and/or modify it under the terms of The Artistic License 2.0. See the LICENSE file included with this distribution or http://www.perlfoundation.org/artistic_license_2_0. For clarification, see http://www.perlfoundation.org/artistic_2_0_notes.

When separated from the distribution, all original POD documentation is covered by the Creative Commons Attribution-Share Alike 3.0 License. See http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification, see http://creativecommons.org/licenses/by-sa/3.0/us/.