Feb 95 Tips
Volume Number: | | 11
|
Issue Number: | | 2
|
Column Tag: | | Tips & Tidbits
|
Tips & Tidbits
By Scott T Boyd, Editor
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
TIP OF THE MONTH
Keeping The Ports Straight
Saving and restoring the GrafPort is something that must be done a lot. Suppose you needed to convert a Point to Local coordinates. You first have to set the GrafPort to the window whos coordinates you want. It might look like this:
void foo(WindowPtr myWindow, Point *thePoint) {
GrafPrt savedPort; // declare a temporary variable
GetPort(&savedPort); // remember the old port
SetPort(myWindow); // point to the right window
GlobalToLocal(thePoint); // do the work
SetPort(savedPort); // restore the old port
}
Heres a nifty C++ class to simplify things. Put it all in one header file, PortSaver.h:
class PortSaver {
public:
PortSaver(GrafPtr newPort = nil);
virtual ~PortSaver(void);
private:
GrafPtr savedPort;
};
inline PortSaver::PortSaver(GrafPtr newPort) {
GetPort(&savedPort); // remember the old port
if (newPort != nil)
SetPort(newPort); // set the new port
}
inline PortSaver::~PortSaver(void) {
if (savedPort != nil) // if there is an old port
SetPort(savedPort); // restore it
}
// A macro that makes the PortSaver easier to use
#define SETPORT(aPort) PortSaver setTheCurrentPortTo(aPort)
Here is the same routine using class PortSaver
void foo(WindowPtr myWindow, Point *thePoint) {
SETPORT(myWindow); // point to the right window
GlobalToLocal(thePoint); // do the work
}
Advantages:
1) Saves typing. Actually, class PortSaver does more sanity checking than the simple example above.
2) The port is always restored when the function exits, no matter how many return points there might be,
and even if an exception gets thrown.
Disadvantages
Even though Ive written PortSaver with inline functions, there is no guarantee that any given compiler will
actually inline them. As a result, you might get additional overhead from the function calls, and a call to LoadSeg
(depending on where the compiler actually put the function code.)
You can adapt PortSaver to save other things as well. Whenever you want to temporarily change one of the global Quickdraw properties (text font, window origin, etc.), you can implement a PortSaver-like class.
- James Jennings, jennings@halcyon.com
OK, Everybody, Get In Line!
The review of Object Master 2.5 in the December issue was a welcome insight into a powerful programming tool. On page 65, Andy Dent observes that Object Master allows both color- and style-coding of source files, and points out that this feature is a contentious issue. Andy is probably referring to the fact that styling code often results in defeating the uniformity of text width in mono spaced fonts that most programmers rely on to align code vertically.
Ive used Object Master, and found that you can have code styled and mono spaced by using the condensed style in conjunction with bold or outline style. Italics and plain text characters are then the same width as bold or outlined characters. As a result, code text is dramatically clearer when read with a styled editor like Object Master, but loses none of its structure when read by an editor that doesnt support styled text.
- Nick, nick+@pitt.edu