TweetFollow Us on Twitter

January 92 - UOrnaments: Half-price Icons

UOrnaments: Half-price Icons

Thomas E. Burns

What if you want to display a bunch of icons, but find that TIcons are too big and slow? This type of situation calls for a class of objects that know how to draw themselves, but don't process events. To minimize memory usage, it's nice if these classes require only one instance per icon, no matter how many places an icon appears within a view.

I developed two classes for this purpose, TOrnament and TOrnamentServer, have proven useful. TOrnament is a simple abstract class that defines objects that know how to draw themselves within a view, but do not respond to events. Because of their simplicity, ornaments are about twice as fast at drawing and consume far less memory than similar views. TOrnamentServers make using ornaments easy and efficient by managing their creation and imaging.

Using ornaments

Adding ornaments to a view is straight-forward. First, by modifying your ISomeView and IRes methods, ask the appropriate OrnamentServer, gPictureOrnamentServer for pictures or gIconOrnamentServer for icons, to load the ornament. The AddOrnament method will either create and initialize the ornament, if it hasn't already been created, or increment the ornament's reference count.
gPictureOrnamentServer->AddOrnament( kTestPicture );

Then add code to your view's Draw method that draws the ornament. This example draws the ornament kTestPicture in your view(this) at location where with highlighting hlOff:

gPictureOrnamentServer->Draw( this, kTestPicture, where, hlOff );

The final step is to delete the ornament in the view's free method. DeleteOrnament will decrement the ornament's reference count and, if the reference count is zero, free the ornament.

gPictureOrnamentServer->DeleteOrnament( kTestPicture );

Even though ornaments do a lot of work for you, only four methods are needed to implement a new subclass. I will stroll through the implementation of TPictureOrnament and TPictureOrnamentServer to demonstrate the creation of new ornament subclasses.

Implementing TPictureOrnament

Subclasses of TOrnament are used to implement specific, resource-based images, such as pictures and icons. Because TOrnament implements the methods used for reference counting, Add and Delete, its subclasses need to implement only three methods: IPictureOrnament, Draw and GetExtent.

The IPictureOrnament calls IOrnament and then loads the picture resource. IOrnament merely calls IObject, sets the reference count to zero and sets fRsrcId to rsrcId. The essential code (without failure handling) for the IPictureOrnament method is:

virtual pascal void
TPictureOrnament::IPictureOrnament
(
    short   rsrcId
)
{
    this->IOrnament( pictureRsrcId );
    fPicture = GetPicture( fRsrcId );   // fPicture holds the
                                        // picture
    FailNILResource( (Handle)fPicture );
}

The Draw method is the core of an ornament. Its implementation will, of course, vary greatly for different classes of ornaments. Typically, a Draw method will call inherited::Draw (which calls PenNormal()), load and make unpurgeable the ornament's resource, and then draw with the appropriate highlighting. Because the code for drawing pictures is rather long and specific for 'PICT' resources, I will only show the definition for the method.

UOrnaments
virtual pascal void
Draw
(
    TView*              theView,    // view to draw in
    const VPoint&       where,      // top,left corner to draw at
    HLState             hilite      // the hilite to use
);

The next method, GetExtent, returns an ornament's size in a Point. This method is usually very similar to Draw. It must load and make unpurgeable the ornament's resource and then calculate its extent. Here's TOrnament's definition of this method:

virtual pascal Point
GetExtent
(
    void
);

The next step for creating a new ornament is to make a subclass of TOrnamentServer that overrides the DoMakeOrnament method. The function of TOrnamentServers is to manage the interactions between views and ornaments. A view never needs to refer to an ornament directly; it always uses an ornaments resource number instead. The advantages of this setup are:

  • A view can assume that it always needs to load and unload an ornament because TOrnamentServer will only do what is necessary. TOrnamentServer loads an ornament only once and then unloads it only after all views are finished with it.
  • There is no need for a view to add instance variables just to keep track of all the ornaments it is using.
  • Memory is saved because an ornament is loaded only once, no matter how many views are using it.

TOrnamentServers need to create ornaments when a new one is added to its list. To maintain the "object-oriented" nature of this unit, I didn't want to make TOrnamentServer know about all the possible types of ornaments. Therefore, TOrnamentServer has a DoMakeOrnament method that returns a new TOrnament of the appropriate subclass. This method for TPictureOrnamentServer is:

pascal TOrnament*
TPictureOrnamentServer::DoMakeOrnament
(
    short       rsrcId
)
{
    TPictureOrnament*       pictureOrnament;

    pictureOrnament = new TPictureOrnament;
    pictureOrnament->IPictureOrnament( rsrcId );
    return( pictureOrnament );
}

As an optional last step, write the InitPictureOrnamentServer function. This function creates and initializes an instance of the TPictureOrnamentServer class.

pascal void
InitPictureOrnamentServer
(
    void
)
{
    if ( gPictureOrnamentServer == NULL )
    {
        gPictureOrnamentServer = new TPictureOrnamentServer;
        gPictureOrnamentServer->IPictureOrnamentServer();
    }
}

Good indications

While it only required a couple of hours to write the original UOrnaments unit, it has proven to be a useful tool. The project I was working on when creating this unit, a robot controller, requires frequent use of indicators that can appear many, many times within a view. Full fledged views would be too big and slow to be practical. I hope ornaments prove to be as useful for you.
 
AAPL
$571.63
Apple Inc.
+10.35
MSFT
$29.67
Microsoft Corpora
-0.08
GOOG
$609.12
Google Inc.
-4.99
MacTech Search:
Community Search:

Edit Websites Right on the iPad With Gre...
Coda, the Mac OS X software by Panic for editing websites – and not just HTML code, but CSS and MySQL management, terminal support, and other features for truly managing a website – is coming to the iPad this Thursday along with Coda 2 for Mac. Diet... | Read more »
Avernum: Escape from the Pit HD Review
Avernum: Escape from the Pit HD Review By Kevin Stout on May 22nd, 2012 Our Rating: :: INSTANT CLASSICiPad Only App - Designed for the iPad Avernum: Escape from the Pit HD is a classic-style RPG with turn-based combat on tiles.   | Read more »
Sonos Subwoofer, the SONOSSUB, Revealed
Sonos is easily our favorite app enabled audio solution. And today they announce a new device to make it even better. Adding to the Sonos Play:3 and Play:5, we now have the SONOSSUB. As you may have guessed, a subwoofer. This booming beast looks... | Read more »
Domino! Review
Domino! Review By Jason Wadsworth on May 21st, 2012 Our Rating: :: CLASSIC WITH FRIENDSiPhone App - Designed for the iPhone, compatible with the iPad Play dominoes with friends online in this social gaming title.   Developer:... | Read more »
Juggernaut: Revenge of Sovering Review
Juggernaut: Revenge of Sovering Review By Kevin Stout on May 21st, 2012 Our Rating: :: MINI-GAME-FULUniversal App - Designed for iPhone and iPad Juggernaut: Revenge of Sovering is an RPG with great graphics and Infinity Blade-like... | Read more »
Sheep Up! Review
Sheep Up! Review By Rob Rich on May 21st, 2012 Our Rating: :: BAA-BAA-BOUNCEUniversal App - Designed for iPhone and iPad Who knew something as simple as a change in perspective could make such a big difference?   | Read more »
Uncover the Lost Levels in Where’s My Wa...
Fans of Disney Mobile’s hit game Where’s My Water - both the free and paid version – have a lot to be happy about. Disney just added iCloud support for cross-device game synching, and lots of new levels. | Read more »

Price Scanner via MacPrices.net

MacBook Pros bundled with discounted AppleCare, sa...
MacConnection has MacBook Pros bundled with discounted AppleCare Protection Plans yielding savings up to $180 off full MSRP: - 13″ 2.4GHz MacBook Pro w/AppleCare: $1378.99 MSRP $1448 - 13″ 2.8GHz... Read more
MacBooks up to $200 off at Apple Store for Educati...
Purchase a new MacBook Pro or MacBook Air at The Apple Store for Education and take up to $200 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount.... Read more
AppleCare on sale for up to $105 off MSRP
B&H Photo has AppleCare Protection Plans for Macs on sale for up to $105 off MSRP including free shipping and NY sales tax only: - AppleCare Mac laptops 15″ and above: $244 MSRP $349 - AppleCare... Read more
27″ iMacs on sale for up to $130 off MSRP
  Apple resellers have 27″ iMacs on sale for up to $130 off MSRP. Each model below includes free shipping – B&H charges NY sales tax only, while Adorama charges sales tax in NY and NJ only: - 27... Read more
Apple offers 16GB iPad 2s for $399
The Apple Store is continuing to offer Black and White 16GB WiFi iPad 2s for $399 including free shipping. WiFi+3G models are available for $529. Each is $100 off their original MSRP and $100 less... Read more
Retina Display MacBooks Might Not Be The Best Idea
CNET’s Dan Ackerman suggests persistent rumors that the forthcoming new generation of Apple’s MacBook Pro laptops may fit in the be careful what you wish for category. Citing his CNET colleagues Josh... Read more
Keyboard The Key To iPad Productivity
Amitae blogger Graham K. Rogers says the iPad is a bit of a mystery to him in terms of it being promoted as a full-scale tool for productivity, noting that he tends to do most of his work on a... Read more
Ashton Kutcher Steve Jobs Movie Begins Filming in...
The film chronicling the life of Apple Inc. co-founder and charismatic master of innovation Steve Jobs begins principal photography in June, and in keeping with the project’s commitment to accuracy... Read more

Jobs Board

iOS Developer (iPhone and iPad) at Mahal...
Mahalo is on a mission to help the world quotLearn Anythingquot by creating high quality educational content available on mobile devices. Were looking to disrupt the education industry in a big way.... Read more
iPhone App at Elance.com (Plano, TX)
Create an iPhone App to do the following: 1. Take a picture at a default resolution 2. Identify the location street ... 5. email the picture, address, text notes and voice notes to an email address.... Read more
Iphone/Ipad App Development at Elance.co...
We are in need of an Iphone/Ipad app that will do the following: - Login and provide functionality to our Jomsocial 2.6 ... done ASAP. Job needs to be started quickly. Please provide time estimates... Read more
MAC Imaging/Packaging, Administration at...
Experience - 4 - 7 yrs Good experience in building MAC ( Apple Macintosh ) operating system images. OS imaging ... Knowledge on configuring the LAN and Wireless network on MAC note books Knowledge on... Read more
Mac/window Imaging/Packaging, Administra...
Experience - 4 - 7 yrs Very good experience in building MAC ( Apple Macintosh ) operating system images. OS imaging ... Requirements - 2 Working knowledge / experience on Apple / Mac OS imaging.... Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.