[system] / trunk / pg / macros / parserPopUp.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/parserPopUp.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3723 - (download) (as text) (annotate)
Thu Oct 20 00:57:19 2005 UTC (14 years, 4 months ago) by dpvc
File size: 1862 byte(s)
A new parser object that handles popup menus.  Mainly this is so that
you can use them with the MultiPart object, which requires all the
objects to be Value objects.  See the comments in the file for
documentation and examples.

    1 loadMacros('Parser.pl','contextString.pl');
    2 
    3 Context("Numeric");
    4 
    5 sub _parserPopUp_init {}; # don't reload this file
    6 
    7 ####################################################################
    8 #
    9 #  This file implements a pop-up menu object that is compatible
   10 #  with Value objects, and in particular, with the MultiPart object.
   11 #
   12 #  To create a PopUp object, use
   13 #
   14 #    $popup = PopUp([choices,...],correct);
   15 #
   16 #  where "choices" are the strings for the items in the popup menu,
   17 #  and "correct" is the choice that is the correct answer for the
   18 #  popup.
   19 #
   20 #  To insert the popup menu into the problem text, use
   21 #
   22 #    BEGIN_TEXT
   23 #      \{$popup->menu\}
   24 #    END_TEXT
   25 #
   26 #  and then
   27 #
   28 #    ANS($popup->cmp);
   29 #
   30 #  to get the answer checker for the popup.
   31 #
   32 #  You can use the PopUp menu object in MultiPart objects.  This is
   33 #  the reason for the pop-up menu's ans_rule method (since that is what
   34 #  MultiPart calls to get answer rules).
   35 #
   36 
   37 sub PopUp {parserPopUp->new(@_)}
   38 
   39 #
   40 #  The package that implements pop-up menus
   41 #
   42 package parserPopUp;
   43 our @ISA = qw(Value::String);
   44 
   45 sub new {
   46   my $self = shift; my $class = ref($self) || $self;
   47   my $choices = shift; my $value = shift;
   48   Value::Error("A PopUp's first argument should be a list of menu items")
   49     unless ref($choices) eq 'ARRAY';
   50   Value::Error("A PopUp's second argument should be the correct menu choice")
   51     unless defined($value) && $value ne "";
   52   my $oldContext = main::Context();
   53   my $context = $main::context{String}->copy;
   54   main::Context($context);
   55   $context->strings->add(map {$_=>{}} @{$choices});
   56   my $self = bless Value::String->new($value), $class;
   57   $self->{isValue} = 1; $self->{choices} = $choices;
   58   $self->{context} = $context;
   59   main::Context($oldContext);
   60   return $self;
   61 }
   62 
   63 sub menu {
   64   my $self = shift;
   65   main::pop_up_list($self->{choices});
   66 }
   67 
   68 sub ans_rule {shift->menu(@_)}
   69 
   70 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9