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

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but 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 MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
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
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.