ScreenPicker
Volume Number: 8
Issue Number: 6
Column Tag: TCL Workshop
Object Support for Resources and cdev's
How to integrate Toolbox calls with Object Programming
By Joeseph Simpkins, MacTutor Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
About the author
Joe is a programmer with over 18 years real time programming. He has used a
variety of minis while working on several automation, seismic and telephony systems.
He is completing a Geology degree while looking for his next job.
What is ScreenPicker?
ScreenPicker is an INIT/cdev that selects a PICT resource from the file
StartupScreen at random by swapping the ID with the PICT resource ID = 0. This
selects a random screen for the next startup. It performs the same function on the file
DeskPicture, used by the extension DeskPict. The associated control panel enables or
disables the randomization of each file and display of an Icon on startup. ScreenPicker
does not patch any traps. This is a very simple example of an init with a control panel.
Why ScreenPicker?
As a diversion, I have created several startup screens and would like to see them
without manual manipulations. This trait is called laziness, the key attribute of any
effective programmer. I tried out two public domain randomizers, one manipulated a
set of startup screens as files. It was painfully slow. The other operated on one file with
multiple PICT resources. It ran fast enough but had the habit of crashing.
I disassembled the INIT that used the multiple PICT approach with the latest
ResEdit and used it as a starting point. How should I implement the new routine? I
considered assembly, normal Pascal and then Object Oriented Pascal and selected Object
Oriented Pascal using TCL. When I examined TCL, I found little support for munging
resources. This starts sounding like fun.
Project Organization
This project was developed in Think Pascal 4.0 and used a library generated by
Think C 5.0. There were three major parts to this project, the ShowIconFamily
library, the ScreenPickerINIT code resource, and the ScreenPicker control panel code
resource. The ScreenPickerINIT project outputs the resource file for the ScreenPicker
project. The resource file for the ScreenPickerINIT project was built using ResEdit.
Here is a picture of my development folder:
ShowIconFamily Library
This library has only one entry point, the procedure ShowINIT. This is an
implementation in C written by Patrick C. Beard and modified by James W. Walker.
They were inspired by the oft used “Thank You Paul” routine. I was far too lazy (aka
wise) to do more than compile and use the code as suggested by those authors.
ScreenPicker INIT
The critical functions of ScreenPicker are accomplished by manipulating the
ID’s of resources. Resources pose some interesting problems when treated as objects.
These are not naive objects with everything implemented as data. Many methods cannot
obtain the needed information from instance variables but must query the system. In an
object oriented environment, the users of the object are shielded from such gruesome
details. Where possible, the methods’ connection to the toolbox calls is obvious. I
implemented three objects to support the resource operations defined in Inside
Macintosh Volumes 1 and 4. These sections of IM must be mastered to understand the
code for this module. The class cFlags defines the flags used to communicate between the
INIT and control panel. Here is the class hierarchy for the INIT:
Class CAllRes
The class CAllRes implements the toolbox resource calls that pertain to all the
resources active in the system. Some of the methods for this class include the functions
CurResFile and Unique1ID as well as the procedure SetResLoad. CurResFile returns the
reference number for the current resource file. Unique1ID returns an unused ID in the
current resource file for the specified resource type. SetResLoad enables or disables
loading the data for resources.
The inspiration for this module would crash on startup with memory problems. I
pr evented this by calling SetResLoad to load only the resource information header,
without loading the PICT resource data.
There is one coding trick used in all toolbox calls presented as methods in this
and the next two classes. The actual toolbox call is implemented in a locally defined
inline procedure or function. This avoids name conflicts since I used the toolbox name
for the function wherever possible. One interesting thing about this class is that it has
no instance variables. This class is intended for general use and is defined in the file
TCLResources.p.
Class CRes
The class CRes implements the toolbox resource calls that pertain to individual
resources. Some of the methods for this class include the functions GetType and
GetResAttrs as well as the procedure ChangedResource. GetType returns the resource
type defined in the instance variable for the specified resource. GetResAttrs returns
the resource attributes from the system. ChangedResource sets the specified resource
to be updated at the next appropriate time.
The instance variables for this class include the resource handle, type, ID
number and name. There is no function to query which fields of that data are current.
That sounds like a possible extension. If one needed to support the 7.0 feature of partial
resources, one could derive a class from CRes with the needed instance variables and
methods. I did not want to clutter up the normal case with unneeded instance variables
nor did I need such methods in this module. The largest ScreenPicker resource
manipulated is three bytes long.
Class CResFile
The class CResFile implements the toolbox resource calls that pertain to
resource files. Some of the methods for this class include the functions GetRefNum and
GetResFileAttrs as well as the procedure UpdateResFile. GetRefNum returns the file
reference number defined in the instance variable for the specified resource file.
GetResFileAttrs returns the resource file attributes from the system. UpdateResFile
updates the disk copy of a resource file without closing it.
The instance variables for this class include the file name and file reference
number. There is no function to query which fields of that data are current. That sounds
like a possible extension. The largest ScreenPicker resource manipulated is three
bytes long.
Class cScreenSet
The class cScreenSet is an application specific class that illustrates a derived
object. It is descended from CResFile and defines the method RandomizeScreenSet. This
is the method that does the major functions of the INIT. Look at the code for the details.
Class cFlags
The class cFlags is an application specific class that communicates between the
INIT and cdev code resources. The flags are written by the cdev and read by the INIT.
The Free method overrides the method from TObject.
Unit UScreenPickerINIT
The mainline for the INIT is this unit. It interfaces between the nonobject
oriented startup environment and the object oriented rest of the module. The major
interesting point is the manipulation of A4. Read and understand the ReadMe file that
comes with Think Pascal 4.0. That is the only documentation for linking requirements.
They define the requirements for using object oriented techniques in code resources
such as INITs and cdevs. Here is segment view of this project:
ScreenPicker Control Panel
The ScreenPicker control panel is a very simple example of a control panel. It
supports three check boxes and a display. However, it is implemented using object
oriented techniques. The resource classes and the flag class are also used in the control
panel. There are three modules dedicated to the cdev code resource. These are detailed
below. Here is the class hierarchy for the cdev:
Class cControlPanel
UControlPanel defines a control panel class. That class provides default methods
for all control panel messages. Even the cursor message that is described only in the
Tech Notes is supported. The methods support command key equivalents for cut, copy
and paste. All the arguments are copied to instance variables so that all arguments are
available for all messages.
Class cCPScreenPicker
UCPScreenPicker overrides the specific methods and defines the instance
variables used by ScreenPicker. This class overrides three methods Init, Deactivate and
Hit. This is intended to be a very simple control panel. The method DoControlPanel is the
common point for all messages.
There is one very important design decision in this module. I discovered that the
graphics support in TCL was unusable in a Control Panel. TCL assumes that an
application is present and contains the window. In a control panel, the window resides
in the system. This made the TCL graphics support unusable. I decided there was too
much work involved with complete object support, so I used conventional toolbox calls
in the methods of this class.
General Comments
UScreenPicker is the interface between the conventional world and the object
oriented environment of ScreenPicker. It handles the A4 manipulation and declares the
ScreenPicker object. It passes every message to the ScreenPicker object.
To modify this routine for a different control panel, just replace the string
‘ScreenPicker’ with the name of the new control panel. The comments might need some
updating before reuse.
Resources for ScreenPicker
Each code resource, ScreenPicker has the cdev and the INIT, requires a resource
dedicated the object support. These resources are named in the Segment Type field in
the dialog from the Set Project Type item from the Project menu. The default
resource name is CCOD. The names must be unique to each code resource. I used CCIN and
CCCP for my two resources.
General Comments