TweetFollow Us on Twitter

External Windows 1
Volume Number:7
Issue Number:2
Column Tag:XCMD Corner

Related Info: Window Manager Event Manager

External Windows

By Donald Koscheka, Contributing Editor

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

External Windows

This month, we are going to take a comprehensive look at external window management in Hypercard 2.0. External windows are easy to work with and if you’ve been programming the Macintosh for any amount of time, you won’t have any trouble with them. If you’re a newcomer to the field, external windows are an excellent place to start as the core of an external window management system is the same as any Macintosh application -- the event loop.

Hypercard 2.0 supports external windows in a much more generous fashion than was available under Hypercard 1.x. For one thing, external windows can support callbacks so that you can interface the window directly to Hypercard which was not possible under Hypercard 1.x. External windows do not pre-empt the card, both Hypercard windows and external windows (let’s call them xwindoids) coexist quite nicely in HC2.0.

Windows in Hypercard 2.0 exist in one of two layers, the document layer and the floating layer. Palettes live in the floating layer. Windows in this layer never get activate events -- they are active at all times that they are visible. The toolbox call to Frontwindow() will always return a pointer to a floating window if any floating windows are visible. The frontmost document window will be reported throughout the Hypercard callback, FrontDocWindow().

The document layer is the standard Macintosh windowing environment. Documents behave like typical Macintosh windows, they respond to all standard Macintosh events as well as some Hypercard specific events.

Opening Windows

Listing 1, xwindoids.c, depicts an xcmd that creates a window in the document layer. The window is created by a call to the Hypercard callback, NewXWindow(). In order for Hypercard to add your window to its document or floating layer, you must open your window using this callback or its close cousin, GetNewXWindow() which opens a window from a resource template. You can still use NewWindow if you have some old xcmd that supports external windows, but windows created by directly calling the window manager will not get Hypercard events nor will they be able to make callbacks to Hypercard.

The prototype for these two new callbacks are:

/* 1 */

extern pascal WindowPtr NEWXWINDOW(XCmdPtr paramPtr, Rect *boundsRect, 
StringPtr title, Boolean visible, short procID, Boolean color, Boolean 

extern pascal WindowPtr GETNEWXWINDOW(XCmdPtr paramPtr, ResType templateType, 
short templateID, Boolean color, Boolean floating);

Both callbacks return a standard window manager WindowPtr so there is nothing new there. Being callbacks, both routines need to have the paramPtr passed in. Notice that Apple changed the name of the paramPtr struct form XCmdBlkPtr to XCmdPtr. The structure remains unchanged however.

For NewXWindow, the rectangle, title, visible and procID all correspond to the equivalent parameters in NewWindow(). Similarly, GetNewXWindow() gets passed the id of the resource window template. You have the option of opening color windows and of making them floating or document windows by assigning true or false for these parameters. Listing 1 opens a non-color document window.

XWindoids Events

The xcmd creates the window and does nothing more with the window for the time being. It returns to Hypercard which will subsequently call this very same xcmd with an xOpenEvt hypercard event. This is important because it represents a departure away from the way we are used to thinking about xcmds. When an xcmd creates a window, it is said to “own” that window. Any events that must be handled for that window will be sent to that xcmd. Thus, Hypercard will call upon the xcmd to handle events in a manner that is transparent to the rest of Hypercard.

We detect whether the xcmd is being sent an event by checking the paramCount field in the xcmd command block. By convention, if the number of parameters is less than 0, then we have an event. In this case, Hypercard passes us a pointer to a Hypercard event record in params[0] . The hypercard event record looks like this:

/* 2 */

typedef struct XWEventInfo *XWEventInfoPtr; 
struct XWEventInfo {
 EventRecord event;
 WindowPtr eventWindow;
 long eventParams[9];
 Handle eventResult;

The first field in the record is the standard Macintosh event manager event record. Its fields correspond to the normal usage of an event record. The next field is a pointer to the window that the event is meant for. This is provided for the case of xcmds that handle more than 1 window. Our xcmd handles only one window so we can use this field to point to that window rather than calling FrontWindow() which might return the wrong result (since it always passes a visible palette as the front window).

EventParams and eventResult are used to pass information about specific events to the xcmd. None of the events in xwindoids.c uses these fields so we’ll reserve their discussion for the future.

If paramCount is zero, we dispatch the parameter block to our event handler, in this case a routine called “HandleHCEvent”. HandleHCEvent must first dereference the hypercard event record so that all of its fields are accessible. Next, we set passFlag to true. I’m still trying to piece together the rules for this. It seems that you pass true back to hypercard to tell it to handle the event and false to tell it not to handle the event. If anyone can illuminate this better than that, I will be happy to publish your explanation as the use of this field in relation to events is only briefly discussed in the Hypercard 2.0 xcmd technical documentation. In the meantime, I’ve been using trial and error to get the value of passflag right for each event.

Once the event record is dereferenced, HandleHCEvent closely resembles the event processing switch of any generic Macintosh Application. This is good, you can port your code to Hypercard very quickly by using listing 1 as a template. There is no need to call getNextEvent or WaitNextEvent. Hypercard is taking each event and deciding in turn whether it needs to be dispatched to an external window. Keep in mind that in order for an xcmd to get events, it must have a window associated with it. This is not quite according to Hoyle but it will do. Rather than address each case in the switch, let’s follow the event processing in the order that the events will be called.

Upon opening a new window, the very first event to be called will be xOpenEvt. It is here that we allocate any private storage needed by the window. In the case of xwindoids.c, we will fill the window up with a text edit record just to show that we can do some editing in the window. We store the TextEdit record off the window’s refcon so that we can find it easily. Use any storage schema that you’re comfortable with. If you’re using Think C, A4 Globals will work okay here also, make sure that you set up A4 first. A quick sideline: Think compilers do an outstanding job of supporting the kinds of features that you’ll need to write non-A5 relative code. I use Think “C” or Think Pascal exclusively for writing xcmds because of the multi-segment support and the ability to set up my own environment based off register A4.

Note that I only show the window, which was created as invisible only after taking the xOpenEvt. I figure that it’s better not to show the window until Hypercard is ready to deal with it which is at xOpenEvt time. That way the window does stare back at me with that slack-jawed blank content region look while I’m waiting for Hypercard to tell me that it’s okay to start using it. We need to set passFlag to true on xOpenEvt to tell hypercard that we took the event. From here on out, we will get all events intended for this window.

The next event we get is the activateEvt. This is the standard event manager activate event and is only passed to Document windows. Floating windows are always active so they don’t get activate events. We first check to see if we’re going active or inactive. If the former, then we can activate our text edit record and advise Hypercard that our window now has the edit (or input) focus by calling BeginXWEdit. Once this callback is made, Hypercard will send all keydowns to us until one of two events occurs: our window becomes inactive or Hypercard tells us to give up edit by sending us the xGiveUpEditEvt event. In order to be Hypercard friendly, we need to be prepared to give up the edit focus at any time, Hypercard will tell us when, but we should be courteous and acknowledge the event if we have the edit focus.

Once we become the active window, we will get the update evt followed by any number of events until such time as our window is closed. If the user clicks on the goaway box, Hypercard will advise us that our window is closing. First, the window will go inactive. Next we will get the XWCloseEvt event. At this time, we should close down our internal data structures associated with the window (such as the TEHandle). In listing 1, I hide the window here also. There is no need to call closeWindow or otherwise deallocate the window structures, Hypercard will do that for you.

As you can see from listing 1, there are a lot of new Hypercard events to talk about and we will get to the rest of them in future columns. The rest of event handling is standard Macintosh stuff and seems to work just fine. Note that in the null event handler (default:) we need to adjust the cursor ourselves if the mouse is in the edit box. I found that unless you set passflag to false, Hypercard will set the cursor back to the arrow on you here resulting in a flickering cursor.

Handling mousedown events is little different than you might be accustomed to also. In the case of the goaway control, we do the usual tracking but instead of closing the window directly, we issue a callback, CLOSEXWINDOW, to hypercard in effect asking hypercard to close the window for us. Hypercard will respond that the window is ready to be closed by sending us back an XWCloseEvt at some time in the future. Between the CloseXWindow call and the XWCloseEvt event, the window is in limbo, it might make sense to hide the window here to give the user the sense that the window closed but I find that the latency between these events just isn’t long enough to worry about that.

Dragging the window seems to be handled directly by Hypercard so we don’t do anything with that event. I handle the incontent hits as if this were a normal application. It seems to work fine although you do need to set the passFlag to false after handling the mousedown or Hypercard will get confused.

Similarly, keyevents can be handled directly. Command keys are handled directly by Hypercard so there is no need to test to see if we have a menu key. Again, we set passflag to false to tell hypercard that we took this event. If we set passflag to true here, hypercard will try to handle the event going so far as to call us back with an XWGiveUpEdit event. That would be bad.

Listing 1 is by no means complete. It doesn’t handle scrolling or the grow box yet. You may want to begin your own investigations by adding these features to listing 1. In any event, happy hacking. If you discover anything that you would like to share with the rest of the Mac community, please drop me a line on AppleLink (D6845) or America On Line (AFC Donald).

Listing 1:

/* File: xwindoid.c*/
/* */
/* A sample XCMD for Hypercard*/
/* 2.0 that displays and handles*/
/* an external window.    */
/* */
/* Well-behaved XCMDs for HC2.0  */
/* will respond to the ! and ?*/
/* requests by returning version*/
/* and usage information  */
/* respectively. */
/* */
/* ----------------------------  */
/* ©1990, Donald Koscheka */
/* All Rights Reserved    */

 ANSI-A4-- standard “C” libraries assembled
 off of register A4
 HyperXLib-- Hypercard 2.0 callback library available from 
 Apple Computer, Inc.
 xwindoid.c (contents of listing 1)
 Set Project Type:
 Type == XCMD | XFCN
 Name == xwindoid
 id == -32768..32767
 xwindoid “?”
 xwindoid “!”
 put the result
 Put xwindoid( “?” )
 Put xwindoid( “!” )

#ifndef NIL
 #define NIL(void *)0L

#define ETX 0x03 
#define BS0x08   
#define TAB 0x09
#define LF0x0A
#define NEWLINE  0x0D
#define CR0x0D
#define LEFT_ARROW 0x1C
#define RIGHT_ARROW0x1D
#define UP_ARROW 0x1E
#define DOWN_ARROW 0x1F

/* Multifinder events and masks  */
#ifndef MouseMovedEvt

#ifndef SuspendResumeEvt
 #defineSuspendResumeEvt  0x01

#ifndef ResumeEvtMask

#ifndef ConvertScrapMask
 #defineConvertScrapMask  0x02

pascal void HandleHCEvent( XCmdPtr pp );

Handle  strToParam( str )
 char *str;
* Given a pointer to a string,
* copy that string into a handle
* and return the handle.
* The input and output strings
* are both null-terminated
 Handle outH = NIL;
 long len = 0;
 len = strlen( str );
 if( len )
 if( outH = NewHandle( len ) )
 BlockMove( str, *outH, len + 1 );
 return( outH );

pascal void main( pp )
 Handle answer = NIL;
 char   *str;
 long   len;
 TEHandle hTE;
 Rect   bounds;
 pp->returnValue = NIL;

 if( pp->paramCount < 0 ){
 HandleHCEvent( pp );
 if (pp->paramCount == 1){
 if ( **(pp->params[0]) == ‘!’ ){
 pp->returnValue = strToParam(“\pxwindoid XCMD, version 1.0, ©1990, Donald 
 if ( **(pp->params[0]) == ‘?’ ){
 pp->returnValue = strToParam(“\pSimple xwindoid handler.”);
 /* now open a window to play with */ = bounds.left = 0;
 bounds.bottom = 320;
 bounds.right = 500;
 wind = NEWXWINDOW( pp, &bounds, “\pSample Window”, FALSE, documentProc, 
 CenterWindow( wind );

pascal void HandleHCEvent( XCmdPtr pp )
* Handle events in our xWindows  
* returns true if the event was handled ok
 XWEventInfoPtr  ip= pp->params[0];
 short  windoPart;
 TEHandle hTE;
 Rect   bounds;
 Point  hit;
 char   theKey;
 short  extend;
 pp->passFlag = TRUE;/* seems to be more often the case */
 switch( ip->event.what ){
 case mouseDown:
 windoPart = FindWindow( ip->event.where, &whichWindow );
 if( whichWindow )
 switch ( windoPart ){
 case inGoAway:
 if (TrackGoAway( whichWindow, ip->event.where) ){
 CLOSEXWINDOW( pp,whichWindow );
 pp->passFlag = FALSE;

 case inDrag:
 /* handled by hypercard */
 case inGrow:
 case inContent:
 if (whichWindow == FrontWindow() ){
 GetPort( &oldPort );
 SetPort( ip->eventWindow );
 hTE    = (TEHandle)GetWRefCon( ip->eventWindow );
 hit = ip->event.where;
 GlobalToLocal( &hit );
 bounds = (*hTE)->viewRect;
 if( PtInRect( hit, &bounds ) ){
 extend = (short)ip->event.modifiers && shiftKey;
 TEClick( hit, extend, hTE);
 SetPort( oldPort );
 SelectWindow( whichWindow );

 pp->passFlag = FALSE;
 }/* window part */
 case mouseUp:
 case keyDown:
 case autoKey: 
 /* the command key will be handled by hypercard */
 hTE    = (TEHandle)GetWRefCon( ip->eventWindow );
 GetPort( &oldPort );
 SetPort( ip->eventWindow );
 theKey  = ip->event.message & 0xFF;
 switch( theKey ){
 case TAB:
 case ETX:
 case UP_ARROW:
 case BS:
 TEKey( theKey, hTE );
 }/* switch( theKey ) */
 SetPort( oldPort );
 pp->passFlag = FALSE;
 case activateEvt:
 if ( ip->event.modifiers & activeFlag ){
 BEGINXWEDIT( pp, ip->eventWindow );
 hTE    = (TEHandle)GetWRefCon( ip->eventWindow );
 TEActivate( hTE );
 ENDXWEDIT( pp, ip->eventWindow );
 hTE    = (TEHandle)GetWRefCon( ip->eventWindow );
 TEDeactivate( hTE );
 case updateEvt: 
 BeginUpdate( ip->eventWindow );
 DrawGrowIcon( ip->eventWindow );
 hTE    = (TEHandle)GetWRefCon( ip->eventWindow );
 bounds = (*hTE)->viewRect;
 TEUpdate( &bounds, hTE );
 EndUpdate( ip->eventWindow );
 case app4Evt:
 unsigned char *evtType = &(ip->event.message);
 switch( *evtType ){
 case MouseMovedEvt:
 case SuspendResumeEvt:

 case xOpenEvt:
 /* for illustrative purposes, we  */
 /* add a text edit field to the   */
 /* window*/
 SetPort( ip->eventWindow );
 bounds = ip->eventWindow->portRect; += 4;
 bounds.left +=4;
 bounds.bottom -= 16;
 bounds.right -= 16;
 hTE  = TENew( &bounds, &bounds );
 (*hTE)->txFont = courier;
 (*hTE)->txFace = 0;
 (*hTE)->txSize = 10;
 SetWRefCon( ip->eventWindow, (long)hTE );
 ShowWindow( ip->eventWindow );
 case xCloseEvt:
 hTE    = (TEHandle)GetWRefCon( ip->eventWindow );
 TEDispose( hTE );
 HideWindow( ip->eventWindow );
 case xGiveUpEditEvt:
 hTE    = (TEHandle)GetWRefCon( ip->eventWindow );
 TEDeactivate( hTE );

 case xEditUndo:
 case xEditCut:
 case xEditCopy:
 case xEditPaste:
 case xEditClear:
 GetPort( &oldPort );
 SetPort( ip->eventWindow );
 GetMouse( &hit );
 hTE    = (TEHandle)GetWRefCon( ip->eventWindow );
 bounds = (*hTE)->viewRect;
 if( PtInRect( hit, &bounds ) ){
 SetCursor( *GetCursor(iBeamCursor) );
 TEIdle( hTE );
 pp->passFlag = FALSE;
 SetPort( oldPort );
 }/* switch theEvent->what */

Community Search:
MacTech Search:

Software Updates via MacUpdate

Dashlane 6.2027.0 - Password manager and...
Dashlane is an award-winning service that revolutionizes the online experience by replacing the drudgery of everyday transactional processes with convenient, automated simplicity - in other words,... Read more
ffWorks 2.1.5 - Convert multimedia files...
ffWorks, focused on simplicity, brings a fresh approach to the use of FFmpeg, allowing you to create ultra-high-quality movies without the need to write a single line of code on the command-line.... Read more
Dropbox 101.4.434 - Cloud backup and syn...
Dropbox for Mac is a file hosting service that provides cloud storage, file synchronization, personal cloud, and client software. It is a modern workspace that allows you to get to all of your files... Read more
1Password 7.6 - Powerful password manage...
1Password is a password manager that uniquely brings you both security and convenience. It is the only program that provides anti-phishing protection and goes beyond password management by adding Web... Read more
EyeTV 4.0.0 - Watch and record TV on you...
EyeTV brings a rich TV experience to your Mac. Watch live TV on your Mac. Pause, rewind, and record whenever you want. EyeTV gives you powerful control over what you watch and how you watch it. Put... Read more
Tidy Up 5.3.7 - Find duplicate files and...
Tidy Up is a full-featured duplicate finder and disk-tidiness utility. Features: Supports Lightroom: it is now possible to search and collect duplicates directly in the Lightroom library. Multiple... Read more
Pinegrow 5.97 - Mockup and design web pa...
Pinegrow (was Pinegrow Web Designer) is desktop app that lets you mockup and design webpages faster with multi-page editing, CSS and LESS styling, and smart components for Bootstrap, Foundation,... Read more
BlueStacks 4.210.0 - Run Android applica...
BlueStacks App Player lets you run your Android apps fast and fullscreen on your Mac. Feature comparison chart How to install Bluestacks on your Mac Go to MacUpdate and click the green "Download"... Read more
WhatsApp 2.2027.10 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Art Text 4.0.1 - $29.99
Art Text is graphic design software specifically tuned for lettering, typography, text mockups and various artistic text effects. Supplied with a great variety of ready to use styles and materials,... Read more

Latest Forum Discussions

See All

Clash Royale: The Road to Legendary Aren...
Supercell recently celebrated its 10th anniversary and their best title, Clash Royale, is as good as it's ever been. Even for lapsed players, returning to the game is as easy as can be. If you want to join us in picking the game back up, we've put... | Read more »
Steam Link Spotlight - Disco Elysium
Steam Link Spotlight is a feature where we look at PC games that play exceptionally well using the Steam Link app. Our last entry was Signs of the Sojourner Read about how it plays using Steam Link over here. | Read more »
Distract Yourself With These Great Mobil...
There’s a lot going on right now, and I don’t really feel like trying to write some kind of pithy intro for it. All I’ll say is lots of people have been coming together and helping each other in small ways, and I’m choosing to focus on that as I... | Read more »
Pokemon Go's July Community Day wil...
Pokemon Go developers have announced the details concerning the upcoming Gastly Community Day. This particular event was selected by the players of the game after the Gas Pokemon came in second place after a poll that decided which Pokemon would... | Read more »
Clash Royale: The Road to Legendary Aren...
Supercell recently celebrated its 10th anniversary and their best title, Clash Royale, is as good as it's ever been. Even for lapsed players, returning to the game is as easy as can be. If you want to join us in picking the game back up, we've put... | Read more »
Detective Di is a point-and-click murder...
Detective Di is a point-and-click murder mystery set in Tang Dynasty-era China. You'll take on the role of China's best-known investigator, Di Renjie, as he solves a series of grisly murders that will ultimately lead him on a collision course with... | Read more »
Dissidia Final Fantasy Opera Omnia is se...
Dissidia Final Fantasy Opera Omnia, one of Square Enix's many popular mobile RPGs, has announced a plethora of in-game events that are set to take place over the summer. This will include several rewards, Free Multi Draws and more. [Read more] | Read more »
Sphaze is a neat-looking puzzler where y...
Sphaze is a neat-looking puzzler where you'll work to guide robots through increasingly elaborate mazes. It's set in a visually distinct world that's equal parts fantasy and sci-fi, and it's finally launched today for iOS and Android devices. [... | Read more »
Apple Arcade is in trouble
Yesterday, Bloomberg reported that Apple is disappointed in the performance of Apple Arcade and will be shifting their approach to the service by focusing on games that can retain subscribers and canceling other upcoming releases that don't fit... | Read more »
Pixel Petz, an inventive platform for de...
Pixel Petz has built up a sizeable player base thanks to its layered, easy-to-understand creative tools and friendly social experience. It revolves around designing, trading, and playing with a unique collection of pixel art pets, and it's out now... | Read more »

Price Scanner via

B&H Photo offers $200-$300 discounts on A...
B&H Photo has new 16″ MacBook Pros on sale today for $200-$300 off Apple’s MSRP, starting at $2149. Expedited shipping is free to many addresses in the US: – 2019 16″ 2.6GHz 6-Core MacBook Pro... Read more
Clearance 2019 15″ MacBook Pros on sale today...
Amazon-owned Woot is blowing out Apple refurbished, clearance 2019 15″ MacBook Pros starting at only $1579 and up to $950 off Apple’s original MSRP. According to Woot, “These MacBooks are Refurbished... Read more
Apple Refurbished iMac Pros available for $35...
Amazon-owned Woot is selling Apple refurbished 27″ 3.2GHz 8-Core iMac Pros for $3599.99 shipped. That’s $1400 off Apple’s original MSRP for this model. According to Woot, these iMac Pros are “Factory... Read more
Clearance 2019 13″ 2.4GHz/256GB MacBook Pro o...
B&H Photo has dropped their price on the clearance 2019 13″ 2.4GHz/256GB Quad-Core Silver MacBook Pro by $500 off Apple’s original MSRP to a new low of only $1299. Expedited shipping is free to... Read more
$219 Apple AirPods Pro are back at Verizon, s...
Verizon has Apple AirPods Pro on sale again for a limited time for $219.99 on their online store. Their price is $30 off Apple’s MSRP, and it’s the lowest price we’ve seen for AirPods Pro. Available... Read more
Apple’s $779 13″ MacBook Air deal returns to...
Apple has clearance, Certified Refurbished, 2019 13″ MacBook Airs available again starting at $779. Each MacBook features a new outer case, comes with a standard Apple one-year warranty, and is... Read more
$200 13″ MacBook Pro discounts are back at Am...
Amazon has 2020 13″ 2.0GHz MacBook Pros on sale again today for $150-$200 off Apple’s MSRP. Shipping is free. Be sure to purchase the MacBook Pro from Amazon, rather than a third-party seller, and... Read more
Deal Alert! Apple AirPods with Wireless Charg...
Sams Club has Apple AirPods with Wireless Charging Case on sale on their online store for only $149.98 from July 6, 2020 to July 9, 2020. Their price is $50 off Apple’s MSRP, and it’s the lowest... Read more
Xfinity Mobile promo: Apple iPhone XS models...
Take $300 off the purchase of any Apple iPhone XS model at Xfinity Mobile while supplies last. Service plan required: – 64GB iPhone XS: $599.99 save $300 – 256GB iPhone XS: $749.99 save $300 – 512GB... Read more
New July 2020 promo at US Cellular: Switch an...
US Cellular has introduced a new July 2020 deal offering free 64GB Apple iPhone 11 smartphones to customers opening a new line of service. No trade-in required, and discounts are applied via monthly... Read more

Jobs Board

Product Manager, *Apple* Commercial Sales -...
Product Manager, Apple Commercial Sales Austin, TX, US Requisition Number:77652 As an Apple Product Manager for the Commercial Sales team at Insight, you Read more
Physical Therapist Assistant - *Apple* Hill...
Physical Therapist Assistant - Apple Hill Rehab - Full Time Tracking Code 62519 Job Description General Summary: Under the direct supervision of a licensed Physical Read more
Operating Room Assistant, *Apple* Hill Surg...
Operating Room Assistant, Apple Hill Surgical Center - Full Time, Day Shift, Monday - Saturday availability required Tracking Code 62363 Job Description Operating Read more
Perioperative RN - ( *Apple* Hill Surgical C...
Perioperative RN - ( Apple Hill Surgical Center) Tracking Code 60593 Job Description Monday - Friday - Full Time Days Possible Saturdays General Summary: Under the Read more
Product Manager, *Apple* Commercial Sales -...
Product Manager, Apple Commercial Sales Austin, TX, US Requisition Number:77652 As an Apple Product Manager for the Commercial Sales team at Insight, you Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.