15. Inputline: undo

You can insert characters in the inputline, simply by typing, as was demonstrated in Section 8. Inputline. It is also possible to insert characters by some function, as was shown in Section 14. Menu. In both examples I did not implement a Undo option. In this section, I'll show how to do that. In addition, Prima::Drawable::Markup is used to add some text markup. Now only two examples, a red colored text fragment and a bold button text.

mainwindow


use Prima qw(Label Buttons InputLine Application);
use Prima::Drawable::Markup q(M); # enables POD-like markup

sub mcInsert; # A "forward" subroutine declaration.

$mw = Prima::MainWindow->create( 
    text => "Insert text and Undo example",
    size => [475, 300],
    icon => Prima::Icon-> load('icon.png'),	
    skin => 'flat',
);

 # C<LightRed|CTRL+Z> is the markup code to color the short cut CTRL+Z

$mw-> insert( "Label",
    pack       => { fill => 'x', side => 'top', pad => 25,  },
    text       => \ 'Insert text by typing or use buttons. Undo action: C<LightRed|CTRL+Z> or use the button Undo or right mouse-click inputline and select the first option Undo.',
    wordWrap   => 1,
    font       => { size => 12, },	
    valignment => ta::Left,
    autoHeight => 1,
); 

$mcEditor = $mw->insert( InputLine =>

    pack        => { fill => 'none', side => 'top', pad => 25 },
	text        => 'insert text and undo',
	color       => 0xFFFF00,
	width       => 300,
	alignment   => ta::Center,
	font        => { size => 18, },
	autoSelect  => 0,
	focused     => 1,
	borderWidth => 3,
	popupItems  => [ # right mouseclick items
            ['Undo'       => 'Ctrl+Z'    => '^Z', sub{ $_[0]-> undo }],
            ['Select all' => 'Ctrl+A'    => '^A' => sub { $_[0]-> select_all }],
            ['Delete'     => 'Shift+Del' => kb::NoKey, sub{ $_[0]-> delete }],	  
	],
	
);

$mcEditor-> charOffset(length($mcEditor-> text)); # sets the cursor at the end

$mw-> insert( 	           
            Button =>
                pack    => { fill => 'x', side => 'left', pad => 25 },
                size    => [150, 30],
                text    => "replace with 'abc'",
                onClick => sub { mcInsert($mcEditor, "abc"); }, # description subroutine mcInsert below
); 

$mw-> insert( 	           
            Button =>
                pack    => { fill => 'x', side => 'left', pad => 25 },
                size    => [200, 30],
                text    => "add 'X' at cursor position",
                onClick => sub { 
                                 $cur_pos = $mcEditor-> charOffset; # find position cursor
                                 $str = $mcEditor-> text; # copy current text to $str
                                 substr($str, $cur_pos, 0, X); # insert character X at $cur_pos
                                 mcInsert($mcEditor, $str); # description subroutine mcInsert below 
                               },
);  

# M "B<Undo> is the markup code to make the word Undo bold

$mw-> insert( 	           
            Button =>
                pack      => { fill => 'x', side => 'left', pad => 25 },
                size      => [75, 30],
                text      => M "B<Undo>",
                color     => 0x000000,
                backColor => 0xcccccc,
                onClick   => sub { 
                                   $mcEditor-> undo; 
                                   $mcEditor-> focused(1);	                              
                                   $mcEditor-> charOffset(length($mcEditor-> text));					
                                 },
);    

# function mcInsert that adds inserted characters (here $insertText) to a internal array in order to Undo 

sub mcInsert {

# local: arguments passed by value 
  local ($mcEditorCopy, $insertText) = @_;	
# -------------------------------------------------------- 
  $mcEditorCopy-> begin_undo_group;
  $mcEditorCopy-> push_undo_action( 'edit_text', $mcEditorCopy->text); # edit_text refers to a predefined function in InputLine.pm 
# --------------------------------------------------------   
  $mcEditorCopy-> text($insertText);
  $mcEditorCopy-> focused(1);		                             
  $mcEditorCopy-> charOffset(length($mcEditorCopy-> text));
# --------------------------------------------------------   
  $mcEditorCopy-> end_undo_group;  
# --------------------------------------------------------   
  
}
            
run Prima;