Sep 96 Tips
Volume Number: | | 12
|
Issue Number: | | 9
|
Column Tag: | | Tips & Tidbits
|
Tips & Tidbits
By Steve Sisak
Did You Take Out the Garbage?
A somewhat obscure but useful way of determing whether objects are getting deleted in C++ is to do this:
1. In the class implementation (usually in the header file), add a destructor:
class MyClass
{
public:
virtual ~MyClass();
2. Dont implement the destructor; i.e., dont write the actual code for the destructor method.
3. Run the project. If you get link errors for the destructors, this means that the destructors are getting called (i.e. the objects are getting deleted), which is what you want. If you dont get a link error, this means that the object never gets deleted, which could signify a possible memory leak.
Jeremy Vineyard
Pre-Flighting EditText
For a program I recently wrote, I wanted to do user input checking for a dialog box as the user was typing into an EditText field. I wrote a routine that figures out what the contents of a specific EditText box will be if the current event is allowed to be processed. I then called the routine from my custom dialog event proc, and analyzed the results. That way, I could easily decide if the users action would produce valid results, or if I needed to abort the current event.
IsDlgControl is a simple check for control characters we always want to process. DivineNewItemString is the routine that does all the work. PStrCopy is a little routine for copying Pascal strings; Im sure other people have better ways of doing this.
Michael Trent
// some control key constants.
#define kEnterKey 3
#define kBackspace 8
#define kTab 9
#define kReturnKey 13
#define kEscapeKey 27
#define kLeftKey 28
#define kRightKey 29
#define kUpKey 30
#define kDownKey 31
#define kDelete 0xFF
/* IsDlgControl
* Returns true if c is a special control key (say an arrow key, or escape).
* Otherwise returns false.
*/
Boolean IsDlgControl(char c)
{
if ((c >= kEscapeKey) && (c <= kDownKey)) return true;
if ((c == kReturnKey) || (c == kEnterKey)
|| (c == kDelete) || (c == kBackspace) || (c == kTab))
return true;
return false;
}
/* PStrCpy
* A little routine to copy Pascal strings. Provided for those people who dont
* already use BlockMove() to do this for them ...
*/
void PStrCpy(Str255 s, const Str255 t)
{
short i;
for (i=0; i<=s[0]; i++) {
s[i]=t[i];
}
}
/* DivineNewItemString
* Given a DialogPtr, EventPtr and an item number for the active EditText DLOG
* Item, it returns what the string will be if the current event is processed.
* It should be called from a custom dialog event proc.
*/
void DivineNewItemString (
DialogPtr d, EventRecord *e, short item, Str255 output)
{
short *TEScrpLength = (short *)0x0AB0;
DialogRecord *dr;
TEHandle teh;
char c;
Str255 input, text;
short selStart, selEnd;
short i;
short outStrIdx=0;
short iType;
Handle iHandle;
Rect iRect;
// get the text string
GetDItem(d,item,&iType,&iHandle,&iRect);
GetIText(iHandle,text);
// Set the input string
c = (e->message & charCodeMask);
if (IsDlgControl(c)) { // if its a control char, return the
// items text.
PStrCpy(output,text);
return;
} else if (e->modifiers & cmdKey) {
if ((c == 'v') || (c == 'V')) {
// if pasting, get the pasted string.
(void)TEFromScrap();
HLock(iHandle);
iHandle = TEScrapHandle();
for (i=0; i< *TEScrpLength; i++) {
input[i+1]=((unsigned char *)
(*iHandle))[i];
}
input[0]=*TEScrpLength;
HUnlock(iHandle);
} else { // if any other command stroke
PStrCpy(output,text);
return;
}
} else {
// else, set the input string equal to the new character
input[0]=1;
input[1]=(unsigned char) c;
}
// get the selection point from the TERec
dr = (DialogRecord *)d;
teh = dr->textH;
selStart=(*teh)->selStart;
selEnd=(*teh)->selEnd;
// generate output string:
// copy the first bit of text
for (i=1; i<=selStart; i++) {
output[++outStrIdx]=text[i];
}
// copy the input string
for (i=1; i<=input[0]; i++) {
output[++outStrIdx]=input[i];
}
// copy the last part of text
for (i=selEnd+1; i<=text[0]; i++) {
output[++outStrIdx]=text[i];
}
// lastly, set the length
output[0] = outStrIdx;
}
A Free Tool From Apple
You can use the Finders About this Macintosh window to see the size your applications heap, and the amount of free memory that is available in your heap. This information is only updated during idle time, though, so dont count on it to always be accurate. [Heres the real tip: use Balloon Help to get a numeric representation of the information. - sgs]
David Lawrence
Avoiding the Menu Item Gotcha
Watch out when using AppendMenu or InsertMenuItem. Im using AppendMenu to create a popup menu based on user input. Since AppendMenu interprets certain characters as menu attributes (such as command-key equivalents, menu dimming, etc.), if a user enters one of these characters, you will run into a problem with your menu. To avoid this, call AppendMenu or InsertMenuItem with a dummy string such as "\p " (the string must not be empty), and then call SetMenuItemText using the real string, like so:
Str255 theUserName;
// Get the string to display in the menu
theUser->GetUserName (theUserName);
// Append an empty string to the menu
AppendMenu (theMenuHandle, "p\ ");
// Change the menu item to the userName
SetMenuItemText (theMenuHandle, theIndex, theUserName);
Tim Pedone
Worlds Most Original Use of ResEdit
If you are looking in the code of a PICT, you can often see a color table with 256 colors even if you use only 2 or 3 colors. This can represent a massive waste of disk space. I tried to find an app to compact the color table but I couldnt find one - until finally I found ResEdit!
My trick: create a 'cicn' resource in ResEdit, paste in your PICT or draw it, select the part for using in the PICT, copy and paste it into an actual PICT. The only limitation is the maximum size: 64 by 64.
Henri Clerc
No, Wait -
Worlds Most Truly Totally Original Use Of ResEdit
This might be one everybody knows, but I just figured it out and its saved me a load of time, so here goes.
ResEdit is a Drag-and-Drop application!!! This works for any type of file. No longer must eons be frittered away navigating through countless folders in open dialog boxes to access resource forks of files hidden deep within the murky depths of your hard drive. Just do a Find in the Finder and drag it to ResEdit - I keep an alias on my desktop. Happy hacking!
Joshua Vampiric Bunny Glazer