TweetFollow Us on Twitter

January 93 - Customizing Views

Customizing Views

Angelika Peter

If you want to have subclasses of TView with additional fields, you face the problem that you can't initialize them from a 'View' resource if you want to use ViewEdit (or AdLib). With MacApp 2 style views it was relatively easy to derez the view resources, once the hierarchy was stable, and to maintain them as Rez sources.

With MacApp 3 style views, this becomes substantially more difficult. Not really impossible, but awkward enough to make me look for a workaround. Here it is:

THE SOLUTION

One of the (many) really good things about the new view format is the field fUserArea.

I am using it as a resource ID. The view subclass then reads a resource of a certain type with this ID and initializes its additional fields from this resource. Each subclass knows which resource type it has to use. Of course it is possible to have several levels of subclassing too. In this case every subclass would need it's own resource type.

All this happens in DoPostCreate, but one could probably do it in ReadFields if it is important that the fields are initialized before the DoPostCreate phase.

It is easy to maintain these resources either with ResEdit using own templates or with Rez, whichever is more convenient. And you have the additional advantage that the resources can be shared among several views.

AN EXAMPLE

My example is a TEditText subclass, used to enter numeric values. It is very much based on the old TExtendedText class from one of the MacApp 2 Goodies disks.

I want to initialize not only minimum and maximum from the resource but also the initial value, the precision and how it should behave when dimmed and whether to use scientific format.

The Pascal type template for the resource looks like this:

ExtTextDescribitor = RECORD
    minimum:                LongInt;
    maximum:                LongInt;
    divisor:                Integer;    
    precision:              Integer;
    maxMagnitude:           Integer;
    hideTextWhenDimmed:     Integer;  { 0 for false, <> 0 for true }
    initialValue:           Str255
    END; 
ExtTextDescribitorPtr       = ^ExtTextDescribitor;
ExtTextDescribitorHandle    = ^ExtTextDescribitorPtr;

The corresponding Rez template is:

type 'ExTx' {
    longint;    /* minimum value */ 
    longint;    /* maximum value */ 
    integer;    /* divisor (for minimum and maximum) */
    integer;    /* precision */ 
    integer     kNeverScientific=32000, kForceScientific=-1;
    integer     kShowTextWhenDimmed=0, kHideTextWhenDimmed =1;
    pstring;    /* initial value */
};

DoPostCreate would always look something like this:

PROCEDURE TExtendedText.DoPostCreate(itsDocument: TDocument);
OVERRIDE;

VAR
    itsDescribitor: Handle;
    fi: FailInfo;

    PROCEDURE HdlDoPostCreate(error: OSErr; message: LongInt);
    
    BEGIN
    SELF.Free
    END;

BEGIN { DoPostCreate }
INHERITED DoPostCreate(itsDocument);

IF fUserArea <> 0 THEN
    BEGIN
    CatchFailures(fi, HdlDoPostCreate);
    { !!! use your resource type here !!! }
    itsDescribitor := GetResource('foo ', fUserArea));
    FailNILResource(itsDescribitor);
    HLock(Handle(itsDescribitor));  { to make WITH safe }
    Success(fi);
    
    WITH MyDescribitorTypeHandle(itsDescribitor)^^ DO
        BEGIN
        { initialize your fields }
        END;    { with }
    
    ReleaseResource(itsDescribitor)
    END { if fUserArea… }
END; { DoPostCreate }

Initializing the fields is straightforward:

WITH itsDescribitor^^ DO
BEGIN
fPrecision := precision;        { digits after decimal point }
fMaxMagnitude := maxMagnitude;  { when to use scientific                                                                format }
fHideTextWhenDimmed := hideTextWhenDimmed <> 0;

IF minimum = - maxLongInt THEN  {to be able to express ±∞}
    fMinimum := - inf
ELSE
    fMinimum := minimum / divisor;  {for fractional minimum}
IF maximum = maxLongInt THEN
    fMaximum := + inf
ELSE
    fMaximum := maximum / divisor;  {for fractional maximum}

    fValue := initialValue;
SELF.SetModifiedText(initialValue, kRedraw)
END;    { with }
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Combo Quest (Games)
Combo Quest 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Combo Quest is an epic, time tap role-playing adventure. In this unique masterpiece, you are a knight on a heroic quest to retrieve... | Read more »
Hero Emblems (Games)
Hero Emblems 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ** 25% OFF for a limited time to celebrate the release ** ** Note for iPhone 6 user: If it doesn't run fullscreen on your device... | Read more »
Puzzle Blitz (Games)
Puzzle Blitz 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Puzzle Blitz is a frantic puzzle solving race against the clock! Solve as many puzzles as you can, before time runs out! You have... | Read more »
Sky Patrol (Games)
Sky Patrol 1.0.1 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.1 (iTunes) Description: 'Strategic Twist On The Classic Shooter Genre' - Indie Game Mag... | Read more »
The Princess Bride - The Official Game...
The Princess Bride - The Official Game 1.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1 (iTunes) Description: An epic game based on the beloved classic movie? Inconceivable! Play the world of The Princess Bride... | Read more »
Frozen Synapse (Games)
Frozen Synapse 1.0 Device: iOS iPhone Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Frozen Synapse is a multi-award-winning tactical game. (Full cross-play with desktop and tablet versions) 9/10 Edge 9/10 Eurogamer... | Read more »
Space Marshals (Games)
Space Marshals 1.0.1 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.1 (iTunes) Description: ### IMPORTANT ### Please note that iPhone 4 is not supported. Space Marshals is a Sci-fi Wild West adventure taking place... | Read more »
Battle Slimes (Games)
Battle Slimes 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: BATTLE SLIMES is a fun local multiplayer game. Control speedy & bouncy slime blobs as you compete with friends and family.... | Read more »
Spectrum - 3D Avenue (Games)
Spectrum - 3D Avenue 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: "Spectrum is a pretty cool take on twitchy/reaction-based gameplay with enough complexity and style to stand out from the... | Read more »
Drop Wizard (Games)
Drop Wizard 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Bring back the joy of arcade games! Drop Wizard is an action arcade game where you play as Teo, a wizard on a quest to save his... | Read more »

Price Scanner via MacPrices.net

Deal Alert! Mac Studio with M4 Max CPU on sal...
B&H Photo has the standard-configuration Mac Studio model with Apple’s M4 Max CPU in stock today and on sale for $300 off MSRP, now $1699 (10-Core CPU and 32GB RAM/512GB SSD). B&H also... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.