Well expand the micro-application that I presented last month by adding more functionality to the windows this time. But first, let me review some discussions that started on our Calvacom bulletin board after my first introduction to C++. The way I defined my matrix operations, it seems, wasnt totally in the C++ spirit; there was a reason for this, which I forgot to explain.

The problem is that our matrix operators must return a result somehow. If you define the operator the intuitive way:

the result matrix is allocated locally first, the product computed and stored into the local object, and the whole matrix is copied as soon as it is assigned to another variable of type matrix- like in matrix x = a*b, where x is newly created in the assignment, or in x = a*b, where x has been defined previously. That the local object is destroyed as soon as one leaves the operators scope and a copy be made for use in the scope that called the operator is required by C++. But the copy operation may take a long time with big arrays, and in most cases it wont be necessary because the result has already been computed and the memory allocated.

One solution - which I chose - was not to return an object as a result, but a reference to it:

Here, the space for the result is allocated through the new() operation, which creates memory space that is not automatically destroyed as one leaves the operators context. Now, however, the system will never know when to free the space associated with the matrix, unless you tell it to do so by calling delete(). This precludes usage of those operators in any complex formulas, because intermediate results will be stored in matrix-type objects that are never accessible and can therefore never be deleted.

Alain Richard (on Calvacom) has raised these points, and others:

 Constructors/destructors allow the use of data structures without having to care about initialization/ termination code. For instance, a first version of a class X might not need such code while the second version might need it; a program using class X can be recompiled without modification in both cases.

When a result of type matrix is produced, a 500*500 matrix is not passed back through the stack; only the data structure which defines it and contains a pointer to the data. The only problem is that the constructor will often copy the data unnecessarily; but the current definition of C++ doesnt allow a better solution.

Alain proposes to use procedures instead of operators for functions in which the programmer has to clean up intermediate results explicitly. One possibility, but this gives up the last advantage of operators, shortness of notation.

He has some more comments on bugs and deficiencies:

 In C++, the only way to pass a result is through the return statement, even though the result is a function parameter. However, since one doesnt know its name, one has to go through an intermediate variable R and then copy R into the actual result.

One should also note that the code that CFront generates isnt very well optimized (a euphemism), or that the preprocessor relies too much on the efficiency of the C compiler, in our case MPW C 3.1b1. Some observations I made in the code generated by CFront:

CFront doesnt optimize the use of intermediate variables and creates them for each new expression. Then, these variables are freed only at the end of the block containing the expression, which is stupid because it blocks memory.

A lot of dead code is generated by CFront. Usually, such code should be removed by the C compiler, but that is unfortunately not the case. For instance:

//3
struct A { A() {} };
struct B:A { B() {} };
main()
{
B b;
}

The declaration of b causes 30 lines of assembler code to be generated which dont do anything [in fact, when I checked it there seemed to be even more dead code - JL].

For the maintenance of virtual methods of a class A, CFront creates a method dispatch table. The table, named __A_vtbl, is stored together with the application code. In addition, CFront creates a global variable __A_ptbl, a pointer to __A_vtbl. This global is later used to initialize a hidden instance of each variable of type A. __A_vtbl and __A_ptbl are stored as globals, which prohibits the use of classes for DAs, CDEFs, LDEFs, etc.

I have found one or two bugs: it is not always possible to declare a variable inside an expression (produces a syntax error). There is also a problem with pointers to base class member functions in the case of multiple inheritance. That error is easy to circumvent but causes a run time, not a compilation error.

This is not an exhaustive list, but unfortunately shows that the MPW compilers are not quite mature yet. But even with the bad code quality, CFront does at least exist. At any rate, C++ is going to be the most important development language for the next years.

I almost agree with the last statement - however, I am also getting very curious about Eiffel, a new OOP-language which is rumored to be available for the Mac next year.

Zoom, grow and scroll for MacTutorApp

The skeleton application that I presented last month did not do much; the window could not be resized or zoomed, and there were no controls such as scroll bars present. This month well expand the application by adding these functions. Well use the definitions that we made last time, and create a new subclass of MacTutorDocument, MacTutorGrow.

The interest of the object-oriented approach is, of course, that we can reuse most of the code that we have already defined in the previous example. Only those functions which are specific to our new document will have to be redefined.

Placeholders for these functions exist already in the TDocument class definition, which is part of the C++ examples that come with Apples C++ system. That definition is quite long, an we dont need to reprint it here; all I show (listing 1) is the header file that constitutes the interface. You see that entries exist for methods such as DoContent, DoGrow and DoZoom. These methods will be called from the main event loop, so if you define your own document class with a non-empty DoContent method and a mouse click occurs inside the content region of the document window, that method will be called automatically.

Therefore, the changes we have to do to previous months example are quite simple: the main program will still be an object of type TMacTutorApp, but the document opened will be of a new class, which we call TMacTutorGrow. Then we only have to change a few lines in the main program definition of TMacTutorApp, as you see in listing 3. If someone had already defined a document class for us that handles scrolling and resizing, that is all we would have to do to make the application work with that new type of document window.

Unfortunately, we still have to do that work, and I wont claim I did a perfect job of scrolling the display message around in the example window; window resizing and drawing of the controls works OK, but the scrolling still leaves something to be desired. Only when the window actually gets redrawn on an update event is the contents displayed correctly. Youre, of course, invited to improve on my example.

Listing 2 shows the definition of the TMacTutorGrow class - the header file - followed by the actual code. We derive TMacTutorGrow from TMacTutorDocument (see last column), adding some private variables and methods, and overriding some of the public methods in TDocument.

The first thing to note is that we define some functions that are not methods of any particular class for scroll bar handling (this is the approach Apple took for its TEDocument example). To indicate to the compiler that these functions are defined in C, their prototype definitions are enclosed in curly brackets and prefixed by extern C. This is a feature of the 2.0 implementation of C++ and not mentioned in Stroustrups book.

The class implementation follows, first we need to define the constructor and destructor. The constructor of a subclass is called after the constructor of its ancestor; therefore all that is left to do is to get the scroll bar resources, insert the controls into the window, and initialize their values. The display string (our documents contents) is passed on to the ancestors constructor. The destructor will dispose the controls; disposing of the window is taken care of by the superclass.

For the DoUpdate method, we replicate the definition from the TMacTutorDocument class, because our DrawWindow method has changed. In this case, the display string and the controls are redrawn. I had to change the fDisplayString variable from the TMacTutorDocument class from private to protected, so that the derived class could access it. The other possibility would have been to go through the interface that I provided and use the method GetDisplayString.

The DoActivate method invalidates the control and grow rectangles when the window is activated, so that they get redrawn on the next update event. On deactivation, the controls are hidden.

Several methods are provided for redrawing the scroll bars in their correct position; they are needed by the DoGrow and DoZoom methods. The main work is done by the routine AdjustScrollSizes, which calculates the control positions from the port rectangle of the window and moves the controls into their places. AdjustScrollValues simply resets the controls to their initial values; in a real-world application, one would insert code here that calculates the correct maximum, minimum and control settings from the properties of the windows contents (e.g. number of lines in a text, size of a picture).

DoZoom zooms the window, then repositions the scroll bars and invalidates all of the windows contents except for the control rectangles. The update event will then take care of redrawing. DoGrow works in the same way, only the update region is calculated through an intersection of the old and the new windows port rectangles, the standard method thats already been described in IM Vol.I.

DoContent is where scrolling is handled. I implemented scrolling through a call to ScrollRect and a resetting of the origin of the GrafPort. This means that the controls have to be repositioned after the scrolling, otherwise they are redrawn outside the window (the SetOrigin made them scroll with the rest of the contents). Therefore, a call to AdjustScrollSizes() is made after the control value has changed and the window has been scrolled.

The thumb region and up/down arrows with page regions are handled separately; for the thumb region, the window adjustment can be made after the control has been released, while the arrows and page fields have to respond continuously while the mouse button is down. This is done through a control action procedure, which is defined as an external routine and not a class method. (This is the way Apple implemented it in their example; I dont know whether a pointer to a class method can be passed as a filter procedure to a toolbox routine). The control action routines, whose prototypes had been defined at the start of the program, are implemented at the end of listing 2.

There are certainly a few bugs left in my example, and the scrolling leaves much to be desired but take this as a challenge for improving the example. Well expand this application with other features as we proceed in our tutorials. Until then.

Software Updates via MacUpdate

MacFamilyTree 7.3.4 - Create and explore...

MacFamilyTree gives genealogy a facelift: it's modern, interactive, incredibly fast, and easy to use. We're convinced that generations of chroniclers would have loved to trade in their genealogy... Read more

Yummy FTP 1.10.2 - FTP/SFTP/FTPS client...

Yummy FTP is an FTP + SFTP + FTPS file transfer client which focuses on speed, reliability and productivity.
Whether you need to transfer a few files or a few thousand, schedule automatic backups, or... Read more

VueScan 9.5.08 - Scanner software with a...

VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more

Iridient Developer 3.0.1 - Powerful imag...

Iridient Developer (was RAW Developer) is a powerful image conversion application designed specifically for OS X. Iridient Developer gives advanced photographers total control over every aspect of... Read more

Air Video Server HD 2.1.0 - Stream video...

Air Video Server HD streams videos instantly from your computer on your iPhone, iPad, iPod touch or Apple TV. No need to worry about converting or transferring files.
We took everything that was... Read more

Duplicate Annihilator 5.7.5 - Find and d...

Duplicate Annihilator takes on the time-consuming task of comparing the images in your iPhoto library using effective algorithms to make sure that no duplicate escapes.
Duplicate Annihilator... Read more

BusyContacts 1.0.2 - Fast, efficient con...

BusyContacts is a contact manager for OS X that makes creating, finding, and managing contacts faster and more efficient. It brings to contact management the same power, flexibility, and sharing... Read more

Capture One Pro 8.2.0.82 - RAW workflow...

Capture One Pro 8 is a professional RAW converter offering you ultimate image quality with accurate colors and incredible detail from more than 300 high-end cameras -- straight out of the box. It... Read more

Backblaze 4.0.0.872 - Online backup serv...

Backblaze is an online backup service designed from the ground-up for the Mac.With unlimited storage available for $5 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more

Little Snitch 3.5.2 - 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

Pie In The Sky: A Pizza Odyssey 1.0
Device: iOS Universal
Category: Games
Price: $2.99, Version: 1.0 (iTunes)
Description:
A game about delivering pizza. In space.
| Read more »

Chosen Gives Hopeful Singers, Songwriter...

If YouTube videos and reality TV shows like The Voice have taught us one thing, it’s that there are a lot of people out there who are anxious to show the world their talents. And if they’ve taught us a second thing, it’s that there’s an almost... | Read more »

Android's Popular OfficeSuite Now A...

Once only available for Android devices, OfficeSuite has finally landed on the app store. The Mobile Systems app lets you view, edit, create, and share Word, Excel, and PowerPoint documents as well as convert them to/from PDFs. It's touted as being... | Read more »

Warhammer: Arcane Magic is Coming Soon,...

Turbo Tape Games has announced that they're joining forces with Games Workshop to bring the turn-based strategy board game, Warhammer: Arcane Magic, to life on the iOS.
| Read more »

Fast & Furious: Legacy's Creati...

| Read more »

N-Fusion and 505's Ember is Totally...

| Read more »

These are All the Apple Watch Apps and G...

The Apple Watch is less than a month from hitting store shelves, and once you get your hands on it you're probably going to want some apps and games to install. Fear not! We've compiled a list of all the Apple Watch apps and games we've been able to... | Read more »

Appy to Have Known You - Lee Hamlet Look...

Being at 148Apps these past 2 years has been an awesome experience that has taught me a great deal, and working with such a great team has been a privilege. Thank you to Rob Rich, and to both Rob LeFebvre and Jeff Scott before him, for helping me... | Read more »

Hands-On With Allstar Heroes - A Promisi...

Let’s get this out of the way quickly. Allstar Heroes looks a lot like a certain other recent action RPG release, but it turns out that while it’s not yet available here, Allstar Heroes has been around for much longer than that other title. Now that... | Read more »

Macho Man and Steve Austin Join the Rank...

WWE Immortals, by Warner Bros. Interactive Entertainment and WWE, has gotten a superstar update. You'll now have access to Macho Man Randy Savage and Steve Austin. Both characters have two different versions: Macho Man Randy Savage Renegade or Macho... | Read more »

Price Scanner via MacPrices.net

Apple offering refurbished 27-inch 5K iMacs f...

The Apple Store is offering Apple Certified Refurbished 27″ 3.5GHz 5K iMacs for $2119 including free shipping. Their price is $380 off the price of new models, and it’s the lowest price available for... Read more

16GB iPad mini on sale for $199, save $50

Walmart has 16GB iPad minis (1st generation) available for $199.99 on their online store, including free shipping. Their price is $50 off MSRP. Online orders only.
Read more

The Apple Store has Apple Certified Refurbished 13″ 2.6GHz/128GB Retina MacBook Pros available for $979 including free shipping. Original MSRP for this model was $1299.
Read more

Save up to $600 with Apple refurbished Mac Pr...

The Apple Store is offering Apple Certified Refurbished Mac Pros for up to $600 off the cost of new models. An Apple one-year warranty is included with each Mac Pro, and shipping is free. The... Read more

Samsung Galaxy S 6 and Galaxy S 6 edge U.S. P...

Samsung Electronics America, Inc. has announced the Galaxy S 6 and Galaxy S 6 edge will be available in the U.S. beginning April 10, with pre-orders being accepted now.
“We have completely reimagined... Read more

13-inch 2.5GHz MacBook Pro (refurbished) avai...

The Apple Store has Apple Certified Refurbished 13″ 2.5GHz MacBook Pros available for $829, or $270 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free:
- 13″ 2.... Read more

Färbe Technik, which manufactures and markets of mobile accessories for Apple, Blackberry and Samsung mobile devices, is offering tips on how to keep your iPhone charged while in the field:
•... Read more

Jobs Board

DevOps Software Engineer - *Apple* Pay, iOS...

**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring
Read more

*Apple* Retail - Multiple Positions (US) - A...

Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the
Read more

Sr. Technical Services Consultant, *Apple*...

**Job Summary** Apple Professional Services (APS) has an opening for a senior technical position that contributes to Apple 's efforts for strategic and transactional
Read more

Lead *Apple* Solutions Consultant - Retail...

**Job Summary** Job Summary The Lead ASC is an Apple employee who serves as the Apple business manager and influencer in a hyper-business critical Reseller's store
Read more

*Apple* Pay - Site Reliability Engineer - Ap...

**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring
Read more

MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders. Not responsible for typographical errors.

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.