Learning wxPerl - Design Docs (06 Feb 2016)

Ron Grunwald <[email protected]> Sat, 06 Feb 2016 23:18:56 +0800
Newsgroups gmane.comp.lang.perl.wxperl
Message-ID <D2DC2FE5.18AC%[email protected]>
Hi all,

Attached are the design docs for this week. I¹ve now attached a third
document, which is my attempt to write up ³Chapter 4. Structure of a wxPerl
program². This document is a working document, so its currently in a messy
state.

³Part I ­ Introduction to GUI Programming with wxPerl² is now finalised. I
think the five chapters listed should cover this part quite nicely.

Work on ³Part III ­ wxPerl Layout Management² is progressing at a steady
pace. Lots of reading to get through.

Best Regards,

Ron.
________________________________________
Ron Grunwald
[email protected]
http://www.dvlcorner.org
Chapter04_Program_Structure.txt (application/octet-stream, 3.4 KB)
Chapter 4. Structure of a wxPerl program

The general structure of a wxPerl program has a strong object-oriented form, consisting of at least three individual classes. 

The first class defines a frame that is usually inherited from the Wx::Frame class. In wxPerl, frames are toplevel windows and constitute the foundation on which all other GUI elements are built on. Frames are discussed in much greater detail in ÒPart II - The wxPerl WidgetsÓ. The key feature of this frame class is that all widget objects created are stored in the frame object itself. In this book, we will always refer to the frame object as Ò$selfÓ. When widgets are created, you will typically see the following syntax used,

	$self->{widget_name} = Wx::WidgetClass->new( ... );

The reason for storing widget objects in this manner will become clear later on.

The second class defines the application, and is always inherited from the Wx::App class. The main purpose of the application class is to define a subroutine called OnInit(). This subroutine is responsible for creating all the widgets and layouts that define the GUI of your application.

The third class creates the application by instantiating an object of the application class and placing it into the event loop. The class method MainLoop() is responsible for registering and adding the application into the event loop.

4.1 Programming Style and Conventions

A wxPerl program consists of several class definitions and therefore it is appropriate to develop some programming conventions around class structure. When Perl 5.14 was released, a new syntax was introduced for constructing classes, known as the package block syntax. The general form of a class definition in package block syntax is as follows,

	package ClassName ClassVer {
	}

We will be using this syntax extensively throughout the book. As such we need to ensure that Perl meets the minimum version required before executing our code. This is achieved by adding the following line to the top of a wxPerl program.

	use 5.014;

** Next **
 - the use of $self
 - widget creation syntax using $self
 - closing paragraph: only serves as guide

4.2 The General Program Structure

At this stage you have been introduced to sufficient concepts and details about wxPerl to understand most of the general program structure. In the following sections we will discuss in detail the purpose of each class definition and examine the code line by line. So, we now present the general wxPerl program structure, which will form the basis of all wxPerl programs written in this book.

use 5.014;

package MyFrame {
   use strict;
   use warnings;
   use Wx   qw( :everything );
   use base qw( Wx::Frame );

   sub new {
      my ($class, $parent) = @_;

      my $self = $class->SUPER::new($parent,
				    wxID_ANY,
				    "Frame Title",
				    wxDefaultPosition,
				    wxDefaultSize);

      $self->{MainPanel} = Wx::Panel->new($self, wxID_ANY);

      return($self);
   }
}

package MyApp {
   use strict;
   use warnings;
   use base qw( Wx::App );

   sub OnInit {
      MyFrame->new()->Show(1);
   }
}

package main {
   use strict;
   use warnings;

   MyApp->new()->MainLoop();
}

Listing 4.1 The general structure of a wxPerl program

The source listing above shows the general structure of a wxPerl program. The three classes mentioned at the beginning of this chapter are seen clearly. We will now examine each class line by line.

4.3 The main package
Learning wxPerl - Book Design.html (application/octet-stream, 31.5 KB) - not displayed
Learning wxPerl - Book Design.rtf (application/octet-stream, 101.8 KB) - not displayed