Search Locate Previous Contents

Adding a New Class

Although Causeway is supplied with over 60 Gui components, you will soon find that there is something you need that does not quite match the classes in the library. Don’t panic! CausewayPro has been designed to make it very easy to make minor modifications to existing classes, and comes with a template class library ready for you to start work on your own classes.

This example takes a simple requirement – to make a currency-based numeric edit field – and works through the process of building this as a new class and incorporating it into a test dialogue. For a full technical discussion of the class specification, please refer to the CausewayPro Reference Manual.

Making a New Class from the Template
The first stage is to clone the template class library. This keeps your classes separate from Causeway’s, and leaves you in no danger of losing your work next time you take an upgrade.

      'MyClasses' ŒNS ŒOR 'UserTemplate'
      )cs MyClasses
#.MyClasses

Now you are ready to make your new class from the supplied template ...

      )obs
Template
      'CCY' ŒNS ŒOR 'Template'
      ParseSpec 'CCY'
      Classes
 CCY

This library can be added to the list which CPro and the Designer know about ...

      #.CPro.SetClassPath '#.MyClasses' '#.Class'

... and you are ready to start creating the APL code needed to drive it.

      )cs #.MyClasses.CCY
#.MyClasses.CCY
      )fns
onKeyPress      Caption Create  Destroy Fns     Read    Refresh
      )vars
design  events  isParent        properties      readevents      specification

The place to begin is the specification – I suggest editing it to make something like this:

[Design]
Name=Currency Field
Type=Text
Size=24 Ð   © Drag to width
 
[Read Events]
Value=36
 
[Properties]
Hint      : String
Labelled  : OneOf 'default' 'Left' 'Top' 'Bottom' 'Right'
Value     : User      © Numeric singleton ...
 
[Cue Card]
Shows a number in the current Windows currency format. 
 
[Release Notes]
Based on text edit class.

Most of the sections should be self-evident – you can experiment later to see the effect of various changes on the way the class behaves in the Designer. The only crucial connection which must be made is between the ‘Value’ property and the [Read Events] section. Here you will see the line ‘Value=36’ which means that whenever the Dyalog event #36 occurs on the object (this is the ‘Changed’ event which is triggered on a text field when you leave it, having modified the data) Causeway should run the Read function to take the value from the field and assign it back to the APL expression associated with the ‘Value’ property.

Implementing the New Class
Now you can fix up the functions which will implement it – we need Create (to make it), Refresh (to take the value and update the field), Read (to get the data from the field) and Destroy (to clean up after it):

 this„Create defn;parent;ctl;cap;attach;posn;size;props;nl
© Currency Edit class CCY
 (parent ctl cap attach posn size props)„defn
© Note that caption must be created first
 this„parent,'.CCY',•ctl
© Use supplied utility to label it, and record the name
 nl„this ##.Label defn
 this ŒWC'Edit' ''posn size('Attach'attach)('Fieldtype' 'Currency')
© Set up instance variables and correct ŒIO etc
 ŒCS this ª ŒIO„1 ª ŒML„1 ª names„nl
 vals Refresh this;props;value
© Refresh function for CCY class
© Allows for several properties (future proofed!)
 :For props :In ‡vals
   :Select œprops
    :Case 'Value'      © New value data
     value„œ²props
     this ŒWS'Value'value
   :EndSelect
 :EndFor
 val„prop Read msg
© Read function for CCY (one property only)
 val„(œmsg)ŒWG'Value'
 Destroy this
© Remove caption when object is destroyed
 :If 9=ŒNC this
   ŒEXœthis–'names'
 :EndIf

This should now work, but first we must register it with Causeway. This is the stage I usually forget – if you start the designer and see no sign of your new class, then you need to:

      )cs #
#
      CPro.Build

Testing and Refining the Class
Now it is OK to set up some test data and design a small test form:

      nn„12.3
      Dbx 'qw'

Run it, check that the value appears correctly, and also check that when you click back to the session that the value of nn has been updated correctly. Here is how it might look:


One small refinement would be to set it to select all the text when it gets focus (and deselect it on a matching lostfocus). This simply involves adding a couple of standard event handlers to the existing Create function:

 events„›'Event'(40 'onGotFocus')(41 'onLostFocus')
 this ŒWC'Edit' ''posn size('Attach'attach)('Fieldtype' 'Currency'),events

... which could look rather like:

 msg„onGotFocus msg
 © Select entire text
  (œmsg)ŒWS'seltext'(0 255)
 msg„onLostFocus msg
© Deselect on leaving
 (œmsg)ŒWS'seltext'(0 0)

That really is all there is to it. You might like to run SaveCls 'CCY' to make a backup copy on disk, and possibly set up a more thorough QA test to ensure all the properties work as anticipated.



© Copyright Causeway Graphical Systems Ltd 2001