17. Mini Text 2

This application shows the find and replace function in a text editor, using the 'find' command.

mainwindow

use Prima qw( Edit InputLine Buttons Application);

$mw = Prima::MainWindow->create(
    text      => "Demo find and replace",
    sizeMax   => [ 485, 300 ],
    sizeMin   => [ 485, 300 ],
    backColor => cl::LightGray,
);

 # default settings for all inputlines
    %settingTFCB = (
     text      => "",
     font      => { size => 12, },
     color     => cl::Black,
     backColor => 0xFFFFFF,
    );

$height_buttons_inputlines = 25;

###########################
# editing area
# CTRL+A (select all), CTRL+C (copy), CTRL+V (paste) , CTRL+Z (undo) and Delete work
###########################

$editor = $mw->insert(
    Edit =>

    origin => [ 20, 135 ],
    size => [ 450, 150 ],

    %settingTFCB,

    autoSelect => 0,
    focused    => 1,

    #wordWrap => 1,
);

###########################
# find
###########################

$input_f = $mw->insert(

    InputLine =>

    origin => [ 20, 75 ],
    size => [ 150, $height_buttons_inputlines ],

    %settingTFCB,
);

$x     = 0;
$y     = 0;
@found = ();

$mw->insert(

    Button  => origin => [ 200, 75 ],
    size    => [ 100, $height_buttons_inputlines ],
    text    => "find",
    onClick => sub {
        if ( $input_f->text ) {

              # init/reset and search from the beginning
              ($x = 0, $y = 0) if (! $found[2]);

              # assign each line to the array @no_lines
              @no_lines = split("\n", $editor-> text);

              # does the line with number $y exist?
              if ( defined( $no_lines[$y] ) ) {

                $len = length( $no_lines[$y] );
                if ( $len > 0 ) {

                    @found = $editor->find( $input_f->text, $x, $y );

                     # extended with two constants, combined bitwise
                     # @found = $editor-> find($input_f-> text, $x, $y, "", fdo::WordsOnly|fdo::MatchCase);

                      if ( defined( $found[0] ) ) {

                        $editor->selection( $found[0], $found[1], $found[2], $found[1] );

                         # assign values to $x and $y and find a match from the next x position, if any OR
                         # search on the next line and start at the beginning

                          ( $found[0] < $len )
                          ? ( $x = $found[0] + 1, $y = $found[1] )
                          : ( $y = $found[1] + 1, $x = 0 );

                    }
                }
            }
        }
    },
);

###########################
# replace all
###########################

$input_r1 = $mw->insert(

    InputLine => 
    
    origin => [ 20, 20 ],
    size   => [ 150, $height_buttons_inputlines ],

    %settingTFCB,
);

$input_r2 = $mw->insert(

    InputLine => 
    
    origin => [ 175, 20 ],
    size   => [ 150, $height_buttons_inputlines ],

    %settingTFCB,
);

$mw->insert(
    Button  => origin => [ 350, 20 ],
    size    => [ 100, $height_buttons_inputlines ],
    text    => "replace all",
    onClick => sub {
        if ( $input_r1->text and $input_r2->text ) {

            @found = $editor->find( $input_r1->text, 0, 0, $input_r2->text );

            while (@found) {
                $editor->set_line( $found[1], $found[3] );
                @found = $editor->find( $input_r1->text, 0, 0, $input_r2->text );
            }
        }
    },
);

run Prima;