11. Checkbox (multiple selection)

A question "Which programming language(s) do you know?" and the user can select one or more options from a list of six choices. New features are: hint, popup, hot key. The subroutine mcCounter counts the number of users choices. A little hint is given if a user did not selected Perl. Of course, you can shorten the code by implementing elements of this code in a separate file, which you can load by the require command.

mainwindow

use Prima qw(Application Themes Buttons Label);

require "/home/reinier/mcMessage.pl"; # code 7. Messagewindow

$mw = Prima::MainWindow-> create(
	text=> "Checkbox",
	size => [375, 300],
    icon => Prima::Icon-> load('icon.png'),	
    skin => 'flat',
);

$counter = 0;

sub mcCounter {
                ($var) = @_;
                ($var->checked) ? $counter++ : $counter--;
}	

$output = $mw-> insert( 
    Label => 
          pack => { fill => 'x', side => 'top', pad => 10 },	           
          text   => "Which programming language(s) do you know?",
          autoHeight => 1,
          font   => { size => 12, },	
);

$mcCheck_C = $mw-> insert( CheckBox =>
    pack => { fill => 'x', side => 'top', padx => 25 },
    text => "C",
    onClick => sub { mcCounter($mcCheck_C); },
);

$mcCheck_Cplusplus = $mw-> insert( CheckBox =>
    pack => { fill => 'x', side => 'top', padx => 25 },
    text => "C++",
    onClick => sub { mcCounter($mcCheck_Cplusplus); },	
);

$mcCheck_Lisp = $mw-> insert( CheckBox =>
    pack => { fill => 'x', side => 'top', padx => 25 },
    text => "Lisp",
    onClick => sub { mcCounter($mcCheck_Lisp); },	
    hint => "I like Lisp, especially the dialect newLisp!", # display a tooltip when the mouse pointer is hovered over the widget longer than some timeout, until the pointer is drawn away. 
);

$mcCheck_Perl = $mw-> insert( CheckBox =>
    pack => { fill => 'x', side => 'top', padx => 25 },
    text => "~Perl", # the character after ~ ( tilde ) character, is treated as a hot key, and the character is underlined. If the user presses the corresponding character key then Click event is called
    onClick => sub { mcCounter($mcCheck_Perl); },	
    #current => 1, # selected
    popupItems => [['Perl is a highly capable, feature-rich programming language with over 30 years of development.' => '']], # right mouse click
);

$mcCheck_PHP = $mw-> insert( CheckBox =>
    pack => { fill => 'x', side => 'top', padx => 25 },
    text => "PHP",
    onClick => sub { mcCounter($mcCheck_PHP); },	
);

$mcCheck_Python = $mw-> insert( CheckBox =>
    pack => { fill => 'x', side => 'top', padx => 25 },
    text => "Python",
    onClick => sub { mcCounter($mcCheck_Python); },	
);

$mw-> insert( 
  Prima::Button =>
                pack => { fill => 'none', side => 'top', pad => 15 },
                size => [100, 30],
                text => 'Evaluate', 
                backColor => 0xcccccc,
                growMode => gm::Center,
                onClick => sub { 
                  mcMessage(400, 150, "Ohhh", "You know ". $counter . " programming language(s). \nBut you must learn Perl!") if (! $mcCheck_Perl->checked);
                  mcMessage(400, 150, "Congratulations!", "You know ". $counter . " programming language(s). \nIndeed, Perl is a great language!") if ($mcCheck_Perl->checked);
               }
);

run Prima;