• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

ADVERTISEMENT
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 who’s 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
}

Here’s 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 I’ve 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.

I’ve 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 it’s structure when read by an editor that doesn’t support styled text.

- Nick, nick+@pitt.edu

 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live