Windows in Prima

Windows are the main element in any Prima application. Windows belong to the Prima hierarchy, but at this time we are interested on windows simply being windows.

Prima offers two basic types of windows: the MainWindow and the Window. The main difference is that destroying a MainWindow will destroy the application, causing the closing of any related windows, dialogs, while closing a Window does not destroy the application.

The creation of both types of window requires some minimal setting. Many others can be used to get the look and feel and the behavior intended.

The settings are of two types:

  • Parameters indicate properties of the new window, as size, the text to be shown on top, the menu to be shown, color of the canvas, status of windows as e.g. maximized or minimized, border colors, and so on
  • Events indicate the way the window would react on events. The one almost always present is the paint event, which shows the operations that are performed when the window is created and then painted

So to create a Window or a MainWindows, we could:

use Prima qw(Application MsgBox);

my $mw->Prima::MainWindow->create(
        #Parameters
        text => "the main window of the application",
        size => [100,100],
        windowState => ws::Maximized,
        menuItems => [[ "~File" => [
                        [],# division line
                        [ "E~xit" => sub {$::application -> destroy;}    ]
                    ]]],
        # events
        onPaint => sub {
                    my ($self, $canvas) = @_;
                    $canvas-> clear;
                    $canvas-> color(cl::Red);
                    $canvas-> line( 0, 0, 800, 800);
            },
        onSize => sub {
                    message "Window Resized";
            },
        onMouseClick => sub {
                    message "Mouse Clicked";
            },
);

Changing the Prima::MainWindow to Prima::Window, we could create a window that does not propagate to others the destroy, if the case.

Parameters and events are set using the pairs shown. For the event the pair is the

onEvent => sub { 
        #code here
        }

So, when you will be looking in the reference material what are the supported events, you will find a list and will know that for each Event the corresponding window creation event manager will be onEvent.

As we see already with this small snippet, code can become quite colored, as we have to put the subroutines needed to manage events in the body of window creation.

In real world this can become a problem, mainly when a need to make some change occurs later.

As we will later remove the direct definition of a menu, we can make something similar with event management to simplify and decouple the code.

We could put the action onEvents in a separate named subroutine, and then use the name in windows creation.

The point is that this works only if the window is created in a package as the derived window. In this way, there is no issue in overloading the name and in having clear who is the owner of the subroutine.

So this would not work:

use Prima qw(Application MsgBox);

sub Exit { $::application->destroy;}

my $mw->Prima::MainWindow->create(
        #Parameters
        text => "the main window of the application",
        size => [100,100],
        windowState => ws::Maximized,
        menuItems => [[ "~File" => [
                        [],# division line
                        ########## Named Function Exit#######
                        [ "E~xit" => "Exit"    ]
                    ]]],
        # events
        onPaint => sub {
                    my ($self, $canvas) = @_;
                    $canvas-> clear;
                    $canvas-> color(cl::Red);
                    $canvas-> line( 0, 0, 800, 800);
            },
        onSize => sub {
                    message "Window Resized";
            },
        onMouseClick => sub {
                    message "Mouse Clicked"
            },
);

while this

package MyMainWindow;
use Prima qw(Application MsgBox);
use vars qw(@ISA);
@ISA = qw(Prima::MainWindow);

sub Exit { $::application->destroy;}

#######The window is created as MyMainWindow
my $mw->myMainWindow->create(
        #Parameters
        text => "the main window of the application",
        size => [100,100],
        windowState => ws::Maximized,
        menuItems => [[ "~File" => [
                        [],# division line
    ##########This time the Exit is invoked and the application exits. 
                        [ "E~xit" => "Exit"    ]
                    ]]],
        # events
        onPaint => sub {
                    my ($self, $canvas) = @_;
                    $canvas-> clear;
                    $canvas-> color(cl::Red);
                    $canvas-> line( 0, 0, 800, 800);
            );,
        onSize => sub {
                    message "Window Resized";
            },
        onMouseClick => sub {
                    message "Mouse Clicked"
            },
);

would work.

Leave a comment

About Fabio D'Alfonso

user-pic A blog about Perl and Prima. I would write about Perl and Prima the way I would like to read about them.