TweetFollow Us on Twitter

Constructing Apps
Volume Number:1
Issue Number:3
Column Tag:C Workshop

Basics in Constructing Applications

By Robert B. Denny

Last month’s C Workshop presented an application “template”, a program shell which can be used as the basis of many kinds of applications. The template can be used with most C systems since there are no calls to library functions. The only external services used are native Mac toolbox traps.

This month, we’ll look at some of the basics in constructing applications, including the tricky area of servicing desk accessories properly. If you have access to last month’s C Workshop, you can refer to the template application’s code for examples. It’s not necessary, though.

Common Requirements

Most applications share several common requirements. These are:

• Overall control is via the menu bar and pull-down menus.

• Detail control and input uses both the mouse and the keyboard.

• Multiple instances of multiple types of windows serve as user interfaces.

• All desk accessories must be usable during execution of the application, within the limits of available memory.

These requirements, combined with the Macintosh operating system, imply the overall application setup and control structure described below.

Applications should begin by initializing all of the system services that it intends to use. At a minimum, all applications should call InitGraf(), InitWindows(),InitFonts(), InitMenus(), InitDialogs() and TEInit(). Some desk accessories use dialogs, which in turn use TextEdit.

Menu Setup

Any application that supports desk accessories must support a menu bar with at least the “Apple”, “File” and “Edit” menus. These menus must be present as required by the Macintosh User Interface Guidelines, and desk accessories assume that they are present. Moreover, the “File” and “Edit” menu options must also conform to the User Interface Guidelines. Desk accessories make assumptions as to the order and meaning of options in these menus.

The “Apple” menu must be set up to contain the desk accessories for selection and opening. Your application must do this explicitly by first creating the menu then adding the desk accessories via the AddResMenu() service. For example:

MenuHandle mh;
 ...
InsertMenu(mh = GetMenu(1), 0);
AddResMenu(mh, ‘DRVR’);
 ...
DrawMenuBar();

In the example, GetMenu() loads the menu resource whose ID = 1 and returns a handle to it, which gets stored in mh. InsertMenu() then adds the menu to the menu bar. Next, AddResMenu() appends the names of all resources of type ‘DRVR’ to the menu. Desk accessories are “drivers” to the current Mac operating system, hence have the resource type of ‘DRVR’. Real drivers have names beginning with ‘.’ or ‘%’ and will not get added to the menu.

The “File” menu is not used (yet) by any of the standard desk accessories. It is up for grabs, though, and should follow the User Interface Guidelines. That is, the items should follow the order:

 New
 Open...
 Close
 Save
 Save as...
 ------
 Page Setup
 Print...
 ------
 Quit

Desk accessories are free to assume that the ordering (and therefore numbering) of menu items in the “File” menu conforms to the above.

The “Edit” menu is used by the Note Pad and other desk accessories. Again, the layout of the menu must follow the user interface guidelines, namely:

 Undo
 ------
 Copy
 Cut
 Paste
 ------
 Show Clipboard

To reiterate, the leftmost three menus must conform to the Macintosh User Interface Guidelines stated in Inside Macintosh if the application is to support desk accessories.

The Event Loop

The single most important character- istic of a Macintosh application is that it is event-driven. Most Mac applications have an “event loop” as their outermost control structure. Following initializa- tion, the application does something like:

while(TRUE)
 {
 SystemTask();
 if(!GetNextEvent(-1, &event))
 continue;
 switch(event.what)
 {

 ... cases for each event type
 ... our application handles

 default:  /* Junk other events */
 }
 }

It is vitally important to understand the event loop. Most trips around the loop circle back at the continue statement that gets executed if GetNextEvent() returns FALSE, meaning that there is no event for us to handle. In future versions of the Macintosh operating system which support multitasking, GetNextEvent() will probably return control to the scheduler, giving other tasks CPU time until an event occurs for the caller.

In any case, when an event for us does occur, GetNextEvent() returns TRUE and fills in our event record with information describing the nature of the event. In C, the event record can be defined as follows:

struct EventRecord
 {
 short  what;
 long   message;
 long   when;
 Point  where;
 short  modifiers;
 };

where the type Point is some mapping of the QuickDraw “point”, an ordered pair of 16-bit coordinates.

Control passes to the switch() statement, which dispatches to the event handling function appropriate for the event type. For example, an event type of mouseDown would dispatch to the function that handles mouse clicks.

In the example shown, the value for the first parameter to GetNextEvent() is -1. This parameter is the event mask, and in this case, it is set to “get” all types of events. The switch() statement should have case sections for each event type that the application explicitly handles. Other types of events use the default case, which does nothing.

If the event mask parameter specifies a subset of event types, those not specified will remain on the event queue for possible later processing. This may be needed for applications which use a certain event type to trigger processing of earlier events of other types not normally processed. This is tricky, though, and can be the source of some obscure bugs. Also, unprocessed events on the queue take up valuable memory space.

If that isn’t enough, there’s a system- wide event mask that controls what event types are recognized and queued. Your application can disable the queueing of certain event types by calling the SetEventMask() service. If your applica- tion doesn’t process certain event types, you should disable them in the system event mask. This prevents wasting valuable event queue memory space with unprocessed event records.

The bottom line is ... call GetNextEvent() with an event mask of everyEvent (-1) to dequeue all event types, then let the default case dispose of the events you aren’t interested in.

Just prior to calling GetNextEvent(), notice the call to SystemTask(). This Mac system service causes control to pass to each open desk accessory, in turn, then back to the caller. Normally, one call at the beginning of the event loop is sufficient to give desk accessories the time they need. If you have any places in your application where you “spin your wheels”, consider calling SystemTask() to help waste some time.

In a multi-tasking version of the Mac, SystemTask() will probably do nothing. Rather, control will be given to the task only when there is a live event for that task. Timer services will be provided for passing control back to the scheduler (and other tasks) when a synchronous delay is needed in the current task.

The event loop is the fundamental control structure of a Macintosh applica- tion. If you haven’t studied The Event Manager: A Programmer’s Guide, a chapter in Inside Macintosh, you should take the time now to do so.

Mouse-Down Events

Any application which supports desk accessories must handle mouse-down events. A mouse click in a desk accessory window gets passed as an event to the application. This quirk in the Mac system architecture requires the application to determine whether the click was in a desk accessory window or in one of its own windows. I should mention that this approach to click handling minimizes the uncontrollable overhead in the operating system for applications which need every available CPU cycle and which do not support desk accessories (whew!).

So, if you want to support desk accessories, your application must detect and process mouse-down events. If you detect a mouse click, first call the Window Manager function FindWindow() to find out where the cursor was when the mouse button was clicked.

If FindWindow() returns the predefined constant inSysWindow, it means that the cursor was in a desk accessory window when the mouse was clicked. If this is the case, call the Desk Manager function SystemClick(). This passes control back to the operating system service which handles desk accessory windows.

Other returns from FindWindow() indicate mouse clicks in windows (including what “part” of the window) or the menu bar, or out in “no man’s land”, the desktop background. We’ll discuss window handling in a future C Workshop. If the click is in the menu bar, then our application must dispatch to a menu handler, another necessity if we are to handle desk accessories.

Menu Selections

If the mouse click is “in the Menu Bar”, then our application must first determine the selected menu number and the item number in that menu, then dispatch accordingly. First, call the MenuSelect() service to get a longword containing both the menu number and the item number in that menu, then use HiWord() and LoWord() to split them up:

 unsigned short menu_id, item_no;
 unsigned long result;
 unsigned short HiWord(), LoWord();
 unsigned long MenuSelect();
 ...
 result = MenuSelect(&event.where);
 menu_id = HiWord(result);
 item_no = LoWord(result);

Then use nested switch() statements to dispatch based on the menu and item selected.

If the “Apple” menu has the desk accessories, your application must activate a selected accessory. If the menu selected is the “Apple” menu, and you have determined that the selection was indeed an accessory, then you simply call OpenDeskAcc() with the name of the menu item (the desk accessory name). The latter can be obtained by calling GetItem() with the menu item number.

The “Edit” menu must also be handled specially if desk accessories such as the Note Pad are to be supported properly. Before trying to dispatch to your own edit-handling functions, you must call the SystemEdit() service. If it returns FALSE, then dispatch to your own function. If it returns TRUE, however, then the menu selection was made while an editing desk accessory was open. The desk accessory handled (used) the request; you must ignore it.

If you haven’t yet studied the The Desk Manager: A Programmer’s Guide, a section of Inside Macintosh, then you should do so now. It’s required reading for programmers who are developing applications which must support desk accessories.

Final Words: Resources Versus Static Data

I’d like to finish up this month’s C Workshop with some thoughts on the use of resources in C programs. Pascal has no facilities for statically initializing data structures. Therefore, Pascal program- mers have no choice but to load static data via the resource mechanism if they are to avoid ugly space-consuming “initialization segments”. You should carefully consider the tradeoffs before deciding whether to make a particular data structure static in your program or to place it in a resource.

Any data that is designed to be tailored after building the application belongs in a resource. Same with large data struc- tures that are used infrequently and can be “purged” from the heap. They too should go in a resource.

It’s my feeling that anything else belongs in your application’s static data area if you can affored the room. Why? The use of resources can slow down your application markedly. On 128K systems, heap space is fragmented by frequent resource manager activity, causing garbage collection cycles and purges to disk. Reading in of resources to initialize static data is far slower than having them present in your application’s address space when it is loaded. Last but not least, it is difficult to maintain programs which contain constants that depend on the contents of resources. For example, the numbering of menu items must track across the C code and the RMaker source as well. As a C programmer, you have a choice.

Author’s Note

During the last couple of months, this author has received some comments regarding bias toward a particular C language system. I feel that a few explanatory words are in order.

It is not the present editorial policy of MacTech to review software products, or otherwise make comparisons or recommendations. There are many Mac publications providing this service. This publication is devoted to providing meaningful technical information for serious programmers. Each developer must decide for himself the system that best meets his requirements.

My philosophy of teaching strongly emphasizes both reading and writing. Programs provided in this column are intended to serve as “reading material”. In the interest of completeness, I feel that the particular C system used to implement a program should be part of the program’s documentation.

I do not endorse any C language system. There are many C systems on the market at this time, and more are appearing every month. Each of these systems has it’s own library and interface to the native Macintosh environment.

In order to minimize the dependence on a particular C system, the example programs given in this column will use a minimum of library support. The template program shown in the October 1984 edition has no library dependency at all.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

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 »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »

Price Scanner via MacPrices.net

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
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
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
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer 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 MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more

Jobs Board

Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Apr 20, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
*Apple* Systems Administrator - JAMF - Syste...
Title: Apple Systems Administrator - JAMF ALTA is supporting a direct hire opportunity. This position is 100% Onsite for initial 3-6 months and then remote 1-2 Read more
Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.