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 {
 Rect fLocation;
 Rect fTL,fTR,fBL,fBR,
 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);

class TSizer : public TCommand {
 TTEDocument   *fTEDocument;
 TBox   *fBox;   RectoldLocation;  RectnewLocation;      Point 
 fStart;  // mouse pressed here
 Point  fDelta;  // offset moved
 // 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);
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
 { if (PtInRgn(*theMouse,
 { aSizer = new TSizer;
 aSizer->ISizer (aFindBoxStruct.myBox,
  fTEDocument, this);
 return aSizer;   }
 { aDragger = new TDragger;
 (aFindBoxStruct.myBox, fTEDocument, this);
 return aDragger;   }
 if (!info->theShiftKey) 
 { fTEDocument->ForEachShapeDo
 ((DoToObject)Deselect, &aSelectStruct); }
 aFindBoxStruct.myBox->fSelected = true;
 InvalidRect(&aFindBoxStruct.myBox-> fLocation);
 return inherited::DoMouseCommand
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){ = + 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) {

pascal void TSizer::DoIt()
 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);
 (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);

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 = (;
 PenSize (1,1);
 fTagRgn = MakeNewRgn();
 OpenRgn(); =;
 fTL.left = fLocation.left;
 fTL.bottom = + 4;
 fTL.right = fTL.left + 4;
 FrameRect(&fTL); =;
 fT.bottom = + 4;
 fT.left = fLocation.left + halfH - 2;
 fT.right = fT.left + 4;
 FrameRect(&fT); =;
 fTR.bottom = + 4;
 fTR.right = fLocation.right;
 fTR.left = fTR.right - 4;
 fBL.bottom = fLocation.bottom; = fBL.bottom - 4;
 fBL.left = fLocation.left;
 fBL.right = fBL.left + 4;
 fB.bottom = fLocation.bottom; = fB.bottom - 4;
 fB.left = fLocation.left + halfH - 2;
 fB.right = fB.left + 4;
 fBR.bottom = fLocation.bottom; = fBR.bottom - 4;
 fBR.right = fLocation.right;
 fBR.left = fBR.right - 4;
 FrameRect(&fBR); = + halfV - 2;
 fL.bottom = + 4;
 fL.left = fLocation.left;
 fL.right = fL.left + 4;
 FrameRect(&fL); = + halfV - 2;
 fR.bottom = + 4;
 fR.right = fLocation.right;
 fR.left = fR.right - 4;
 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 =  
OtherLinkFiles = 
“{ObjApp}TBox.cp.o”ƒ 
 “{SrcApp}TBox.cp” 
 “{SrcApp}TEDrag.MAMake” 
  {MacAppIntf} 
 {MAEcho} {EchoOptions} “Compiling:     TBox.cp”
 {MACPlus} 
 {CPlusOptions} 
 -s TBox 
 -i “{SrcApp}” 
 -i “{MACIncludes}” 
 -o “{ObjApp}TBox.cp.o” 
OtherRsrcFiles = 
 “{MAObj}Printing.rsrc” 


Community Search:
MacTech Search:

Software Updates via MacUpdate

pwSafe 4.0.1 - Secure password managemen...
pwSafe provides simple and secure password management across devices and computers. pwSafe uses iCloud to keep your password databases backed-up and synced between Macs and iOS devices. It is... Read more
WALTR 1.5.4 - Drag-and-drop any media fi...
WALTR is designed to make it easy to upload and convert any music or video file to an iPad or iPhone format for native playback. It supports a huge variety of media file types, including MP3, MP4,... Read more
Audio Hijack 3.1 - Record and enhance au...
Audio Hijack (was Audio Hijack Pro) drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio... Read more
PopChar 7.1 - Floating window shows avai...
We're also selling a 5-license family pack for only $25.99! PopChar helps you get the most out of your font collection. With its crystal-clear interface, PopChar X provides a frustration-free way to... Read more
BBEdit 11.1.1 - Powerful text and HTML e...
BBEdit is the leading professional HTML and text editor for the Mac. Specifically crafted in response to the needs of Web authors and software developers, this award-winning product provides a... Read more
Picasa 3.9.139 - Organize, edit, and sha...
Picasa and Picasa Web Albums allows you to organize, edit, and upload your photos to the Web from your computer in quick, simple steps. Arrange your photos into folders and albums and erase their... Read more
Mac DVDRipper Pro 5.0.5 - Copy, backup,...
Mac DVDRipper Pro is the DVD backup solution that lets you protect your DVDs from scratches, save your batteries by reading your movies from your hard disk, manage your collection with just a few... Read more
NetShade 6.2 - Browse privately using an...
This promotion is for NetShade and 1 year of Proxy and VPN services NetShade is an anonymous proxy and VPN app+service for Mac. Unblock your Internet through NetShade's high-speed proxy and VPN... Read more
CrossOver 14.1.3 - Run Windows apps on y...
CrossOver can get your Windows productivity applications and PC games up and running on your Mac quickly and easily. CrossOver runs the Windows software that you need on Mac at home, in the office,... Read more
Little Snitch 3.5.3 - Alerts you about o...
Little Snitch gives you control over your private outgoing data. Track background activity As soon as your computer connects to the Internet, applications often have permission to send any... Read more

Block Fortress has a Big New Update for...
Block Fortress is a survival-style game that's as fun as it is blocky. It's also just gotten a rather sizeable update that adds a lot more cool stuff. [Read more] | Read more »
Simple and Surreal Star Base Sim rymdkap...
I really like rymdkapsel. Not just because I'm a sucker for games that are cleverly simple or highly stylisitc, but because it's fun and challenging. Actually it's extremely challenging, which is why I was excited to learn that it's getting a couple... | Read more »
Check out the anticipated Angel Stone in...
Fincon has finally revealed Angel Stone in action in the first ever official gameplay trailer for the anticipated hack and slasher. Angel Stone is set in a post-apocalyptic world in which humanity is in danger of being wiped out by the demonic... | Read more »
Moleskine Timepage is an All-New Calenda...
Moleskine Timepage is a bit of a departure for the notebook manufacturer (since it has little to do with notebooks), but it certainly carries their simple and elegant style quite well. [Read more] | Read more »
Jog on Over and Check Out the New Runtas...
Runtastic has put out a fair number of apps to help you sleep, track excercise, and train various parts of your body. Now it's time for your legs to have their own time in the spotlight with Runtastic Leg Trainer. [Read more] | Read more »
It's Lights Out in the Upcoming Pla...
Ember’s Journey is a stark puzzle platformer with a twist: the entire game is played in darkness. The only light you can see by is the one emanating from your own character. [Read more] | Read more »
MooVee - Your Movies Guru (Entertainmen...
MooVee - Your Movies Guru 1.0 Device: iOS iPhone Category: Entertainment Price: $1.99, Version: 1.0 (iTunes) Description: MooVee helps you effortlessly manage your movies, on your iPhone. | Read more »
Geometry Wars 3: Dimensions (Games)
Geometry Wars 3: Dimensions 1.0.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.0 (iTunes) Description: Enjoy the next chapter in the award-winning Geometry Wars franchise and enjoy stunning, console-quality... | Read more »
CHAOS RINGS Ⅲ 1.0.0 Device: iOS Universal Category: Games Price: $19.99, Version: 1.0.0 (iTunes) Description: The newest addition to the popular smartphone RPG series is finally here! ・CHAOS RINGS Overview | Read more »
The Popular Insight Series of Travel Gui...
Getting around in a country when you can't understand the primary language can be tough. Fortunately there are several options available to help wold travellers with the important stuff like giving directions to a cab driver or asking where the... | Read more »

Price Scanner via

Top Markets Saturation To Slow Global Smartph...
According to a new mobile phone forecast from the International Data Corporation (IDC) Worldwide Quarterly Mobile Phone Tracker, smartphone shipments are expected to grow 11.3% in 2015 — down from 27... Read more
Apple refurbished 2014 13-inch Retina MacBook...
The Apple Store has Apple Certified Refurbished 2014 13″ Retina MacBook Pros available for up to $400 off original MSRP, starting at $979. An Apple one-year warranty is included with each model, and... Read more
What Would the ideal Apple Productivity Platf...
For the past four years I’ve kept a foot in both the Mac and iPad camps respectively. my daily computing hours divided about 50/50 between the two devices with remarkable consistency. However, there’... Read more
PageMeUp 1.2.1 Ten Dollar Page Layout Applica...
Paris, France-based Softobe, an OS X software development company, has announced that their PageMeUp v. 1.2.1, is available on the Mac App Store for $9.99. The license can be installed on up to 5... Read more
Eight New Products For USB Type-C Application...
Fresco Logic, specialists in advanced connectivity technologies and ICs, has introduced two new product families targeting the Type-C connector recently introduced across a number of consumer... Read more
Scripps National Spelling Bee Launches Buzzwo...
Scripps National Spelling Bee fans can monitor the action at the 2015 Spelling Bee with the new Buzzworthy app for iOS, Android and Windows mobile devices. The free Buzzworthy app provides friendly... Read more
13-inch 2.5GHz MacBook Pro on sale for $120 o...
B&H Photo has the 13″ 2.5GHz MacBook Pro on sale for $979 including free shipping plus NY sales tax only. Their price is $120 off MSRP, and it’s the lowest price for this model (except for Apple’... Read more
27-inch 3.3GHz 5K iMac on sale for $1899, $10...
B&H Photo has the new 27″ 3.3GHz 5K iMac on sale for $1899.99 including free shipping plus NY tax only. Their price is $100 off MSRP. Read more
Save up to $50 on iPad Air 2, NY tax only, fr...
B&H Photo has iPad Air 2s on sale for up to $50 off MSRP including free shipping plus NY sales tax only: - 16GB iPad Air 2 WiFi: $469 $30 off - 64GB iPad Air 2 WiFi: $549.99 $50 off - 128GB iPad... Read more
Updated Mac Price Trackers
We’ve updated our Mac Price Trackers with the latest information on prices, bundles, and availability on systems from Apple’s authorized internet/catalog resellers: - 15″ MacBook Pros - 13″ MacBook... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Program Manager, *Apple* Community Support...
**Job Summary** Apple Support Communities ( discussions. apple .com) helps customers get the most from their Apple products and services by providing access to Read more
Senior Data Scientist, *Apple* Retail - Onl...
**Job Summary** Apple Retail - Online sells Apple products to customers around the world. In addition to selling Apple products with unique services such as iPad Read more
*Apple* Solutions Consultant - Retail Sales...
**Job Summary** As an Apple Solutions Consultant (ASC) you are the link between our customers and our products. Your role is to drive the Apple business in a retail Read more
*Apple* Watch SW Application Project Manager...
**Job Summary** The Apple Watch software team is looking for an Application Engineering Project Manager to work on new projects for Apple . The successful candidate Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.