TweetFollow Us on Twitter

March 92 - A Clear View of Ten Behaviors

A Clear View of Ten Behaviors

G. Gordon Apple

In the beginning were the system and the toolboxes.

Then came object programming and the MacApp framework . Subclasses were brought forth abundantly after their kind, and views-types proliferated. Their number and complexity intimidated many, preventing them from approaching the framework.

Now there are behaviors that allow generic views to take on characteristics and capabilities as needed. This article discusses a set of drawing (actually, object manipulation) behaviors, showing how behaviors can provide an enhanced level of functional encapsulation and program modularity.

Out of the Chaos

MacApp beginners, myself included, tend to want to dump all their code into subclasses of TView. When I first read the Programming with MacApp books (Wilson, Rosenstein, and Shafer), I was surprised by the use of "helper" classes where the subview mostly consisted of "diversionary" or "deflection" methods that simply pass the calls to the helper class.

Most of these deflection methods involved event handling; menu commands or mouse downs, drawing, highlighting, and cursor control. Therefore, I was pleasantly surprised when behaviors were introduced to provide exactly that type of support in a generic TView or any subclass of TView (actually, any TEventHandler). I set out to see what behaviors might imply in code organization.

The program I chose was originally done in Pascal with MacApp 2 and was based on the ExampleDraw program in the Programming with MacApp book. A good place to start seemed to be the C++ version of the same. Unfortunately, it was also written for MacApp 2, so major code surgery lay ahead. This gave me the opportunity and the excuse to totally revamp the code organization to use behaviors, one of MacApp 3's most powerful and versatile features.

The program document consists mainly of a list of shape objects that can be drawn on the screen or the printed page. I extended the architecture to include more general items such as Views, PICTs, and now MooVs. However, they are all still manipulated as if they are draw objects.

I decided to create the behaviors and partition the code by functionality rather than by common elements or code similarity. I hoped this would result in more versatile modules. I took the approach of a creator itemizing the functionality (behaviors) imparted to the creations. This resulted in:

The Ten Behaviors

  1. Those on the list, show yourselves within the View in listed order and whenever your domain is revealed and requires updating. (TDrawItems)
  2. Those who are touched or who are within a selected area shall themselves be selected and appropriately adorned to highlight their status. (TSelectItems, TItemsSelectionAdorner)
  3. Those who are selected, move right, left, up or down when commanded, remembering your roots in case the command be undone. (TMoveItems)
  4. The selected, take on the indicated colors, patterns, and sizes which have been selected from abundant menus and palettes. (TModifyItems)
  5. The selected, your extremities shall be grabbed and your dimensions adjusted. (TResizeItems).
  6. The selected, the order of your appearance may be advanced or retarded, even to the beginning or the end. (TReorderItems)
  7. New items shall be created in your midst, cloned from the palette and sketched in accordance with current parameters. (TCreateItems)
  8. The selected, clone yourselves and give up your clones to the temporary holding area. (TCopyItems)
  9. The selected, hide until freed, or clone yourselves as before, or accept as your own the clones arriving from the temporary holding area. (TCutPasteItems)

    Well, that should about do it. But didn't I say "Ten Behaviors?" What about those items in limbo, the temporary holding area where everybody gets clipped and bored? Not much going on there. Only the first behavior from this list is needed. Also, the View in limbo is not quite so pure in that diversion methods were not provided for interfacing to the outside world. Word and pictures were provided for, but not our own private I/O. Therefore, the slightly impure view (a subclass of TView) simply deflects the necessary actions to the tenth behavior:

  10. Tell, when asked, what types of items can be supplied from the desk scrap, give of such when asked, and tell how much viewing space they require. Also, give up the data to the desk scrap before sleeping or dying. (TClipboardItems)

Using the Behaviors

The "Ten Behaviors" provide a display, drawing, and editing environment that works in a generic TView and, I hope, can also be applied to any reasonable subclass of TView.

To implement the full set of drawing features, the behaviors are attached to the TView as shown in Figure 1. TView contains a single behavior reference member that points to the first behavior object if any are present. Otherwise it is NULL. Each behavior links to the previous and next behavior to form a two-way linked chain. If the View's (EventHandler's) behavior reference in not NULL, all events, draw messages, cursor control, etc. are diverted to the first behavior. By default, the behavior simply propagates the message to the next behavior. Each behavior had a method that allows it to find the "owning" Event Handler (or View). If there is no next behavior, the message is passed directly back to the EventHandler's (e.g., the view's) equivalent method. To attain conditional or absolute control over this message propagation, simply override the appropriate method in a behavior subclass.

Normally, a PrintHandler behavior gets attached to the main view. The TDrawAdorner gets automatically attached whenever any other adorner is attached. We don't use the draw adorner here, but MacApp attaches one whether or not we make use of it. The order of behavior attachment is mostly irrelevant except that TCreateItems, TResizeItems, and TSelectItems should be in the indicated order. Note that these are not class relationships. These are actual behavior objects linked in a chain. (Adorners are in a list.)

The document has a TShapeList that contains all of the draw objects, whether rectangles, ovals, views, PICTs, or MOOVs. Each behavior derives from TItemsBehavior which has a reference to this same list. Thus each behavior has the same list reference. Multiple storage could have been eliminated by referencing back through the view to the document. This is an almost trivial change due to the use of the accessor method GetShapeList(). [By the way, I'm becoming a real convert to private field variables with accessor methods. They are more work initially, but they prove their worth when code changes and debugging are required. This way, everyone has to always come through the front door rather than slipping in through a window. At least you can find out who's trashing the place.]

The behaviors were implemented so as to be almost independent. In one case, a reference to a clicked shape is stored in TShapeList rather than requiring a reiteration of the list. If the first iterating behavior is missing, the other behavior will go ahead and do its own iteration.

The "Ten Behaviors" pretty much describe the basic functions of each behavior. Most of the basic code fragments are available in the C++ version of the Programming with MacApp book, although many require modification for MacApp 3. A list of field variables and methods, generated by Object Master, is shown in Figure 2.

Note again that the TView is just that-a generic TView, not a subclass. The TDrawItems behavior simply draws the list in the view. We debated whether to make this a behavior or an adorner. They both have draw methods. We decided that a behavior was preferred for our purposes, but the choice was not obvious. Having the Draw behavior separate from the other behaviors allowed it to easily be attached to any view, e.g., the clipview, where only displaying is required.

TMoveItems allows items to be dragged about the screen. For example, this can be used with TDrawItems and TSelectItems (no selection adorner) for a more sophisticated puzzle similar to the simple one supplied with HyperCard 2, or a game of checkers or chess.

TModifyItems allows change in appearance of items without moving them. An example would be trying different color schemes for interior decoration or fabric selection.

TReorderItems can be used for an address selection flip-pad (read only, supplied by a catalog company ) or for arranging slides.

TResizeItems would likely be used only if TMoveItems were also present.

TCreateItems would likely be used only if a selection palette is available to choose what is going to be created.

TCopyItems can be used in a read-only object source file, such as a template library.

TCutPasteItems allows items to be removed from or added to the clipboard.

Several of the above also require TSelectItems, with or without selection adornment (highlighting).

Clearly a large number of combinations of behaviors are possible. They can even be inserted and removed as needed. For example, an interactive multimedia authoring environment could allow a teacher more freedom to change the contents of a program used by students.

The Clipboard

In the quest to use pure TViews for most drawing environments, we kept getting bashed by the clipboard. No matter how long we stared at it, there were four clipboard-related methods that just wouldn't go away.
  • CalcMinFrame
  • ContainsClipType
  • GivePasteData
  • WriteToDeskScrap

We decided to use a slightly specialized clipboard view. We decided to use the behavior mechanism and a TClipboardItems behavior even though the latter is a little more than a convenient code bucket. You'll see why shortly.

Views already have a means to communicate with behaviors through the behavior chain. Someone with foresight added a method to TEventHandler so we could call down the behavior hallway chain and say, "Charlie Brown, where are you?" If Charlie Brown is there, he shouts back down the hall, "I'm here in room 314." The configuration to do this is shown in Figure 3.

The TClipboardItems behavior class was assigned a fixed identifier of 'CVIL'. The view code is as follows:

#pragma segment ShapeClip
pascal void TShapeView::CalcMinFrame(VRect& minFrame)
/* override */
{
    TBehavior* theBehavior = GetBehaviorWithIdentifier('CVIL');

    if (theBehavior)
        ((TClipboardItems*)theBehavior) -> CalcMinFrame(minFrame);
    else
        inherited::CalcMinFrame( minFrame);
}

#pragma segment ShapeClip
pascal  Boolean TShapeView::ContainsClipType(ResType aType)
                                                       /* override */
{
    TBehavior* theBehavior = GetBehaviorWithIdentifier('CVIL');
    
    if (theBehavior)
        return ((TClipboardItems*)theBehavior)
            -> ContainsClipType(aType);
    else
        return inherited::ContainsClipType( aType);
}

#pragma segment ShapeClip
pascal  long    TShapeView::GivePasteData(Handle aDataHandle,
                                    ResType dataType) /* override */
{
    TBehavior* theBehavior = GetBehaviorWithIdentifier('CVIL');
    
    if (theBehavior)
        return ((TClipboardItems*)theBehavior)
                    -> GivePasteData(aDataHandle, dataType);
    else
        return inherited::GivePasteData( aDataHandle, dataType);
}

    #pragma segment ShapeOpen
pascal  void    TShapeView::IShapeView( TDocument* itsDocument,
                                TView* itsSuperview, 
                                VPoint& itsLocation,
                                VPoint& itsSize, 
                                SizeDeterminer itsHSizeDet,
                                SizeDeterminer itsVSizeDet)
{
    this -> IView(itsDocument, itsSuperview, itsLocation,
                                itsSize, itsHSizeDet, itsVSizeDet);
}

#pragma segment ShapeClip
pascal  void    TShapeView::WriteToDeskScrap() /* override */
{
    TBehavior* theBehavior = GetBehaviorWithIdentifier('CVIL');

    if (theBehavior)
        ((TClipboardItems*)theBehavior) -> WriteToDeskScrap();
    inherited::WriteToDeskScrap();  /* Write a QuickDraw picture. */
}

In the case of CalcMinFrame, ContainsClipType, and GivePasteData, if the indicated behavior is present, the view diverts the call to the clipboard behavior. Otherwise, it calls the inherited method. WriteToDeskScrap calls the inherited method regardless, because most applications will at least output a PICT or TEXT in addition to a private scrap type and MacApp automatically provides these as defaults.

It works great! I've been using it for several months now and its simple, non-intrusive code which can be used in any view class. Just don't forget to attach the behavior. If you do, it won't crash, but it won't handle your private scrap either. The exact code used for the view and the behavior is included on the subscription code disk.

These conditioned diversions to a behavior could be added to MacApp in these four methods in TView. (The "else" part of each method should be deleted in that case.) It is non-intrusive and results in identical functionality to that at present if the indicated behavior is not present. It would allow a generic TView to be used in the clipboard. It could be hard-wired for a specific identifier, or the identifier could be set in another field variable, although I don't believe that is really necessary.

Past and Future

The code for these behaviors is largely derived from the C++ version of Programming with MacApp. The TShapeView Class was essentially eliminated except for its use as the clipboard View. The TShapeViewHelper unit was totally revamped and reorganized into separate behaviors. TShapeResizer and TItemsSelectionAdorner were added. The entire project was brought completely up-to-date from MacApp 2 through interim alpha and beta versions to the present version (3.0b3 at this writing).

The TStream classes and TStreamObject class were eliminated because they are now incorporated into MacApp. Other types of draw shapes have now been added, including the handling of views as draw objects, (including TTEViews) and PICTS.

We have plans to incorporate the graphical linking objects that were demonstrated last year at the Phoenix MADA conference (see the demo on the Phoenix conference CD ROM). We also hope to include all the drawing and text edit menus into our floating palette for ease of incorporation into other programs. We plan to handle multiple drawing layers in the same view.

Conclusion

Behaviors allow another level of abstraction and encapsulation. More importantly, they allow a level of portability and reusability that has not existed before, even in an object programming environment.

We discovered that behaviors provide a much clearer demarcation of responsibilities between what MacApp provides and what the application programmer must provide. This can't do anything but help, considering the sense of overwhelming intimidation many feel when confronted with the raw guts of the beast. Behaviors are a lot less intimidating. After all, what is a View if not simply a place for drawing and clicking? Do we really need to face a six-foot fine-print scroll list (in Object Master) of TView method names?

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »

Price Scanner via MacPrices.net

Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more

Jobs Board

*Apple* Systems Administrator - JAMF - Syste...
Title: Apple Systems Administrator - JAMF ALTA is supporting a direct hire opportunity. This position is 100% Onsite for initial 3-6 months and then remote 1-2 Read more
Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.