7. Messagewindow

Prima provides of course a message box. By default, there are some fixed defaults. I needed a flexible solution:

  • different window sizes
  • text printed independent of its length
  • custom position of the buttons
The subroutine mcMessage, invoked with four arguments, is my solution now. I use it for messages (info, warning, error) and as a monitoring tool while debugging.

Save the following subroutine code in an external file (e.g. mcMessage.pl) and add 1; at the end of it.

sub mcMessage {
	
     # This subroutine is called with 5 arguments, which are passed to a list. 
     # The last one can be left out: $mcTextalign will have the default value ta::Center
     ($myWidth, $myHeight, $myTitle, $myMessage, $mcTextalign) = @_; 
     $mcTextalign ||= ta::Center;
     
     $popup = Prima::Dialog->create( 
            size => [$myWidth, $myHeight], 
            text => $myTitle, 
            # if 1, the widget is centered
            centered => 1, 
            icon => Prima::Icon-> load('icon.png'),	
     );
									
     $popup-> insert( 
            Label => 
                pack => { fill => 'x', side => 'top', pad => 20 },
                size    => [$myWidth, 30], 
                # default is ta::Center: all the text is centered; ta = text align; other options: ta::Left and ta::Right
                alignment   => $mcTextalign, 
                # if 1, the label height is automatically changed as text extensions change.
                autoHeight => 1,  
                text   => $myMessage,
                color => cl::Yellow, 
	   );
	           
      $popup-> insert( 	           
            Button =>
                pack => { fill => 'none', side => 'top', pad => 15 },
                size    => [50, 30],
                text    => "Ok",
                onClick => sub { $popup-> destroy; }
	   );

       # execute brings the widget in a modal state
	   $popup-> execute(); 
}

# return a true value to the interpreter
1;
Load this file with require.

To demonstrate what is does, I made a small program calling the subroutine mcMessage from a pushbutton.

mainwindow

use Prima qw(Application Themes Buttons Label);

# including the subroutine mcMessage
require "/home/reinier/mcMessage.pl"; 

$mw = Prima::MainWindow-> create(
    text    => "Demo messagewindow",
    sizeMin => [325, 200],
    sizeMax => [325, 200],
    icon => Prima::Icon-> load('icon.png'),	
);

$mw-> insert( 
  Prima::Button =>
                size => [100, 50],
                text => 'Message', 
                onClick => sub { 
                                  # invoking the subroutine mcMessage
                                  mcMessage(200, 100, "Demo", "This is a demo text!") 
                               },
);

run Prima;