TweetFollow Us on Twitter

Resize Objects
Volume Number:7
Issue Number:4
Column Tag:Jörg's Folder

Related Info: Quickdraw

Getting A Handle On Objects

By Jörg Langowski, MacTutor Editorial Board

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

“C++/MacApp - resizing objects”

As promised last month, this time we’ll start adding some code to our MacApp drawing program to change the basic shapes once they’ve been drawn. I have chosen to implement a resize operation; other functions, like changing colors and patterns, manipulating the order of objects, etc., will follow later.

We are going to use an interface similar to MacDraw, where a selected object has a number of small black squares connected with itself that can be used to change its shape. We’ll call them ‘tags’ in the following. Objects that can be circumscribed by a rectangle have eight tags: four on the corners and four on the centers of the edges. Dragging a corner tag will move the two sides of the rectangle adjacent to the corner, while dragging the edge tag will only move the side of the rectangle that contains the tag.

We’ll need code to display the tags, determine whether the mouse was clicked in one of them, and take action (track the mouse) depending on what tag it was clicked in. Of course, all operations should be undoable, but that’s almost implicit in MacApp.

Detecting the mouse click will proceed in two stages: first, we’ll check whether the mouse has been clicked in any one of the eight tags. If so, we’ll create a new command object of class TSizer, which then does the mouse tracking and track feedback. The command object determines where the mouse was clicked exactly, and the tracking and action will depend on the tag.

For the first phase - determining whether the mouse was clicked in one of the tags at all - it is convenient to create a region that consists of all the eight tags. This region is stored in a new instance variable of TBox, fTagRgn, and created when an object is selected and redrawn (see changes to the TBox::DrawShape method in listing 3). Also, the eight tags are stored individually as Rect instance variables.

The changes to the mouse down command handler are shown in listing 2. We shall now create a TSketcher, a TDragger, or a TSizer command object, depending on where the mouse was clicked - outside any object, inside a selected object, or inside one of the tags of a selected object.

In the TSizer::TrackMouse method we determine which of the eight tags was clicked in, and allow for changes in the top, bottom, left and right coordinates of the object (flags pT, pB, pL, pR). While those coordinates are continuously changed in the TrackMove phase of the TrackMouse method, the TrackFeedback method simply redraws the object rectangle in XOR mode. Undoing can be implemented very simply by remembering the old coordinates of the object.

Sounds simple? It is, and again the modifications are only to a very limited part of the total code - one of MacApp’s great advantages. We still have to add one more ‘Buzzword’ to the list of commands that appear after the Undo/Redo menu item (Listing 4), and extend the Make file a little. By now our code has grown too big to fit into one segment (compiling in Debug mode). Thus, you’ll also see how to segment a MacApp program.

I have put the code that is supposed to go into the new segment into the file TBox.cp, containing TSizer and all the Box/Shape classes. We then have to tell the MacApp builder that this file should be linked in under a different segment name. The necessary changes are shown in listing 5. We add the name of the new source file (TBox.cp) to the list of OtherInterfaces, and the corresponding object file (TBox.cp.o) to the list of OtherLinkFiles. Last, we add the dependency rule for generating the object file (also containing a comment line showing the build progress).

The drawing example will certainly still be extended over the next few months, but we’ll also take up some Forth again soon. Until then.

Listing 1: Changes to TEDrag.h

class TBox : public TObject {
public:
 Rect fLocation;
 BooleanfSelected;
 RgnHandlefTagRgn;
 Rect fTL,fTR,fBL,fBR,
 fT,fB,fL,fR;
 virtual pascal void IBox(Rect *itsLocation);
 virtual pascal void DrawShape();
 virtual pascal void NeedDiskSpace(long *data);
 virtual pascal void Read(short aRefNum);
 virtual pascal void Write(short aRefNum);
#ifdef qDebug
 virtual pascal void Fields(pascal void (*DoToField) 
 (StringPtr fieldName, Ptr fieldAddr, 
 short fieldType, void *link), void *link);
#endif
};

class TSizer : public TCommand {
public:
 TTEDocument   *fTEDocument;
 TTextView*fTextView;
 TBox   *fBox;   RectoldLocation;  RectnewLocation;      Point 
 fStart;  // mouse pressed here
 Point  fDelta;  // offset moved
 BooleanpT,pB,pL,pR; 
 // flags to indicate which coordinates to change
 pascal void ISizer(TBox *itsBox, 
 TTEDocument *itsDocument, TTextView *itsView);
 pascal struct TCommand *TrackMouse
 (TrackPhase aTrackPhase, VPoint *anchorPoint, 
 VPoint *previousPoint, VPoint *nextPoint, 
 Boolean mouseDidMove);
 pascal void TrackFeedback
 (VPoint *anchorPoint, VPoint *nextPoint,
 Boolean turnItOn, Boolean mouseDidMove);
 pascal void DoIt();
 pascal void RedoIt();
 pascal void UndoIt();
#ifdef qDebug
 virtual pascal void Fields(pascal void (*DoToField) 
 (StringPtr fieldName, Ptr fieldAddr, 
 short fieldType, void *link), void *link);
#endif
};
Listing 2: Change to TTextView::DoMouseCommand (file TEDrag.cp)
   
 if (aFindBoxStruct.myBox->fSelected)
 { if (info->theShiftKey) 
 { aFindBoxStruct.myBox->fSelected = false;
 InvalidRect (&aFindBoxStruct.myBox->fLocation);
 return inherited::DoMouseCommand
 (theMouse,info,hysteresis);   
 }
 else
 { if (PtInRgn(*theMouse,
 aFindBoxStruct.myBox->fTagRgn))
 { aSizer = new TSizer;
 FailNIL(aSizer);
 aSizer->ISizer (aFindBoxStruct.myBox,
  fTEDocument, this);
 return aSizer;   }
 else
 { aDragger = new TDragger;
 FailNIL(aDragger);
 aDragger->IDragger
 (aFindBoxStruct.myBox, fTEDocument, this);
 return aDragger;   }
 }
 }
 else
 {
 if (!info->theShiftKey) 
 { fTEDocument->ForEachShapeDo
 ((DoToObject)Deselect, &aSelectStruct); }
 aFindBoxStruct.myBox->fSelected = true;
 InvalidRect(&aFindBoxStruct.myBox-> fLocation);
 return inherited::DoMouseCommand
 (theMouse,info,hysteresis);
 }
   
Listing 3: TBox.cp
#include <UMacApp.h>
#include <UTEView.h>
#include <ToolUtils.h>
#include “TEDrag.h”

const int kBoxColor= redColor;
const int cSizer = 4001;
// Resizing support, JL 2/91
pascal void TSizer::ISizer(TBox *itsBox, 
 TTEDocument *itsDocument, TTextView *itsView)
{
 TScroller *aScroller;

 aScroller = itsView->GetScroller(true);
 ICommand(cSizer, itsDocument, itsView, aScroller);
 fTEDocument = itsDocument;
 fTextView = itsView;
 fBox = itsBox;  
 pT = false; pB = false; pL = false; pR = false;
 oldLocation = fBox->fLocation;
 newLocation = oldLocation;
}

pascal struct TCommand *TSizer::TrackMouse
 (TrackPhase aTrackPhase, VPoint *anchorPoint, 
 VPoint *previousPoint,  VPoint *nextPoint, 
 Boolean mouseDidMove)
{
 fStart = fTextView->ViewToQDPt(anchorPoint);
 
 if (aTrackPhase == trackPress) {
 if (PtInRect(fStart,&fBox->fTR)) { pT = true; pR = true; }
 else if (PtInRect(fStart,&fBox->fTL))
 { pT = true; pL = true; }
 else if (PtInRect(fStart,&fBox->fBR))
 { pB = true; pR = true; }
 else if (PtInRect(fStart,&fBox->fBL))
 { pB = true; pL = true; }
 else if (PtInRect(fStart,&fBox->fT)) { pT = true; }
 else if (PtInRect(fStart,&fBox->fB)) { pB = true; }
 else if (PtInRect(fStart,&fBox->fL)) { pL = true; }
 else if (PtInRect(fStart,&fBox->fR)) { pR = true; }
 }

 if ((aTrackPhase == trackMove) && mouseDidMove) {
 fDelta = fTextView->ViewToQDPt(nextPoint);
 SubPt(fTextView->ViewToQDPt(anchorPoint), &fDelta);
 
 if ( !((fDelta.h == 0) && (fDelta.v == 0)) ) {                
 if (pT){ newLocation.top = 
 oldLocation.top + fDelta.v;  }
 if (pB){ newLocation.bottom = 
 oldLocation.bottom + fDelta.v; }
 if (pL){ newLocation.left = 
 oldLocation.left + fDelta.h; }
 if (pR){ newLocation.right = 
 oldLocation.right + fDelta.h; }
 }
 }

 if ((aTrackPhase == trackRelease) && mouseDidMove) {
 fDelta = fTextView->ViewToQDPt(nextPoint);
 SubPt(fTextView->ViewToQDPt(anchorPoint), &fDelta);
 if ((fDelta.h == 0) && (fDelta.v == 0))
 { return gNoChanges; }
 fBox->fLocation = newLocation;
 }
 return this;
}

pascal void TSizer::TrackFeedback
 (VPoint *anchorPoint, VPoint *nextPoint,
 Boolean turnItOn, Boolean mouseDidMove)
{
 PenState oldState;
 
 if (mouseDidMove) {
 GetPenState(&oldState);
 PenNormal();
 PenMode(patXor);
 FrameRect(&newLocation);
 SetPenState(&oldState);
 }
}

pascal void TSizer::DoIt()
{fTextView->InvalidRect(&newLocation);
 fTextView->InvalidRect(&oldLocation);   }

pascal void TSizer::RedoIt()  
{  fBox->fLocation = newLocation; DoIt();    }

pascal void TSizer::UndoIt()
{fBox->fLocation = oldLocation; DoIt();      }

#ifdef qDebug
pascal void TSizer::Fields(pascal void (*DoToField) 
 (StringPtr fieldName, Ptr fieldAddr, 
 short fieldType, void *link), void *link)
{
 DoToField(“\pTSizer”, nil, bClass, link);
 DoToField(“\pfTEDocument”, 
 (Ptr) &fTEDocument, bObject, link);
 DoToField(“\pfTextView”, (Ptr) &fTextView, bObject, link);
 DoToField(“\pfBox”, (Ptr) &fBox, bObject, link);
 DoToField(“\poldLocation”, (Ptr) &oldLocation, bRect, link);
 DoToField(“\pnewLocation”, (Ptr) &newLocation, bRect, link);
 DoToField(“\pfStart”, (Ptr) &fStart, bPoint, link);
 DoToField(“\pfDelta”, (Ptr) &fDelta, bPoint, link);
 DoToField(“\ppT”, (Ptr) &pT, bBoolean, link);
 DoToField(“\ppB”, (Ptr) &pB, bBoolean, link);
 DoToField(“\ppL”, (Ptr) &pL, bBoolean, link);
 DoToField(“\ppR”, (Ptr) &pR, bBoolean, link);
 inherited::Fields(DoToField, link);
}
#endif

pascal void TBox::IBox(Rect *itsLocation)
 {    fLocation = *itsLocation; fSelected = false; }

pascal void TBox::DrawShape()
{
 if (fSelected) {
 short halfH, halfV;
 
 halfH = (fLocation.right-fLocation.left)/2;
 halfV = (fLocation.bottom-fLocation.top)/2;
 
 PenSize (1,1);
 ForeColor(blackColor);
 FrameRect(&fLocation);
 
 fTagRgn = MakeNewRgn();
 OpenRgn();
 
 fTL.top = fLocation.top;
 fTL.left = fLocation.left;
 fTL.bottom = fTL.top + 4;
 fTL.right = fTL.left + 4;
 FrameRect(&fTL);
 
 fT.top = fLocation.top;
 fT.bottom = fT.top + 4;
 fT.left = fLocation.left + halfH - 2;
 fT.right = fT.left + 4;
 FrameRect(&fT);
 
 fTR.top = fLocation.top;
 fTR.bottom = fTR.top + 4;
 fTR.right = fLocation.right;
 fTR.left = fTR.right - 4;
 FrameRect(&fTR);
 
 fBL.bottom = fLocation.bottom;
 fBL.top = fBL.bottom - 4;
 fBL.left = fLocation.left;
 fBL.right = fBL.left + 4;
 FrameRect(&fBL);
 
 fB.bottom = fLocation.bottom;
 fB.top = fB.bottom - 4;
 fB.left = fLocation.left + halfH - 2;
 fB.right = fB.left + 4;
 FrameRect(&fB);
 
 fBR.bottom = fLocation.bottom;
 fBR.top = fBR.bottom - 4;
 fBR.right = fLocation.right;
 fBR.left = fBR.right - 4;
 FrameRect(&fBR);
 
 fL.top = fLocation.top + halfV - 2;
 fL.bottom = fL.top + 4;
 fL.left = fLocation.left;
 fL.right = fL.left + 4;
 FrameRect(&fL);
 
 fR.top = fLocation.top + halfV - 2;
 fR.bottom = fR.top + 4;
 fR.right = fLocation.right;
 fR.left = fR.right - 4;
 FrameRect(&fR);
 
 CloseRgn(fTagRgn);
 FillRgn(fTagRgn,qd.black);
 ForeColor(kBoxColor);  }
}
   the rest of the file (TShape and following definitions) stays unchanged 
  
Listing 4: Change to TEDrag.r

#define cSizer   4001
resource ‘cmnu’ (128) {
 128, textMenuProc, allEnabled, enabled, “Buzzwords”,          /* these 
words appear after Undo in the Edit menu */
  {
 “Page Setup Change”, noIcon, noKey, 
 noMark, plain, cChangePrinterStyle;
 “Typing”, noIcon, noKey, noMark, plain, cTyping;
 “Drawing”,  noIcon, noKey, 
 noMark, plain, cDrawBox;
 “Dragging”,  noIcon, noKey, 
 noMark, plain, cDragBox;
 “Resize”,  noIcon, noKey, noMark, plain, cSizer
 }
};
Listing 5: TEDrag.MAMake
AppName = TEDrag
OtherInterfaces =  
 “{SrcApp}TBox.cp”
OtherLinkFiles = 
 “{ObjApp}TBox.cp.o”
“{ObjApp}TBox.cp.o”ƒ 
 “{SrcApp}TBox.cp” 
 “{SrcApp}TEDrag.MAMake” 
  {MacAppIntf} 
  {BuildingBlocksIntf}
 {MAEcho} {EchoOptions} “Compiling:     TBox.cp”
 {MACPlus} 
 {CPlusOptions} 
 -s TBox 
 -i “{SrcApp}” 
 -i “{MACIncludes}” 
 -o “{ObjApp}TBox.cp.o” 
 “{SrcApp}TBox.cp”
OtherRsrcFiles = 
 “{MAObj}Printing.rsrc” 
 “TEOther.rsrc”

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
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 »

Price Scanner via MacPrices.net

New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
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

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.