TweetFollow Us on Twitter

Windoid XCMD
Volume Number:6
Issue Number:12
Column Tag:XCMD Corner

Related Info: Event Manager Window Manager

Windoids and HyperCard 2.0

By Donald Koscheka, MacTutor Contributing Editor

If you’ve been writing XCMDs for some time now, you might be a little concerned over how much work is involved in porting your xcmds to Hypercard 2.0. For the most part, this is a trivial process and you need not modify any of your code to support Hypercard 2.0. You will need to recompile with the new xcmd libraries though as the callback engine has been completely re-written to allow for a larger number of callbacks (some 75 as opposed to the 30 or so in HC1.0; the number is a little vague because I found a few callbacks in the library that are not documented anywhere).

For those of you using Think “C”, you will need to convert the XCMD library, “HyperXLib.o” to a Think “C” library. I got this file in MPW format and ran the “oConv” utility on it to convert it to something that Think can use. I then built a library from this. There is one small catch, the Library entry points are all uppercase. You will need to modify your calls to uppercase. For example, PasToZero becomes PASTOZERO. Of course, you might just wait until someone comes along with a more convenient library than the one that is currently distributed.

As I said, your xcmd should work “for the most part”. Certain xcmds will not work very well under Hypercard 2.0 or they will work but their behaviour will be such that you won’t recognize them (kind of like my 2 year old when she skips her nap).

The reason for this is that Hypercard 2.0 implements a completely new external window schema which offers a tremendous benefit to the xcmd writer but which also requires that you rethink your windowing strategy. The benefit of making your xcmds HC2.0 “windoid” friendly is tremendous -- windoids that function in the HC windoid layer will be able to communicate directly with Hypercard and use the Hypercard callbacks. In effect, you get a multiple-window Hypercard stack.

Windoids don’t come for free in Hypercard 2.0 and that’s good. In effect, Apple is leaving it up to each developer to decide what to do with external windows. The only rules that you must obey are those that will allow your windows to function within the external windows layer. This way, your windows will work correctly with other xcmds that also create external windows. Hypercard keeps track of which windows are owned by which xcmds, and it passes window events to each external windoid appropriately.

Windoids.c

Listing 1, “Windoids.c” is an external window xcmd skeleton. It incorporates two new functions of HC 2.0 xcmds: (1) it handles the “?” and “!” queries from the user and (2) it responds to windoid events. The former was covered last month is quite straightforward. We will look at the windoid events in greater detail in this and future columns.

Take a look at the entry point in Listing 1. Note that the first thing we do is check the number of parameters being passed to us by Hypercard. If the parameter count is less than zero (specifically, -1), then we are being passed an external windoid event for a window that we created earlier. If there is only one parameter and it’s “?” or “!” then return the appropriate information about the xcmd back to the caller. In the case of the “?”, you should return usage information. In the case of the “!” you should return a note about who created the xcmd and what it does. This is also a good place to put your copyright notice.

If the parameter count is greater than or equal to zero, then you treat the parameters as ordinary xcmd parameters. However, if the parameter count is less than zero, then you decode the parameter block in an entirely different manner. In this case, the first parameter, params[0] (“C”) or params[1] (Pascal), contains a POINTER to an xcmd event record. This record contains the following fields:

/* 1 */
 
struct XWEventInfo {
 EventRecordevent;
 WindowPtreventWindow;
 long   eventParams[9];
 Handle eventResult;
 }XWEventInfo, *XWEventInfoPtr;

The eventrecord is identical to the event manager event record. The windowPtr is a pointer to the windoid that should get this event. The event parameters and event result have other meanings that we will no doubt explore in the future but which are not required for the code in Listing 1.

In the listing, we call HandleHCEvent with the xcmd parameter block if paramcount is less than zero, otherwise we move to HandleHCmessage which should treat the parameter block like an ordinary xcmd activation. After a little thought this mechanism begs a question -- if we are responding to events in HandleHCEvent, why do we pass the entire parameter block rather than just the xwindow event record? This question gives rise to yet another question -- if that’s the case, why isn’t the parameter block just declared thusly:

/* 2 */

 typedef union{
 XCmdPBlock xcmdParams;
 XWEventInfoxwEventRecord;
 }

These questions did seem troubling to me at first -- I like to pass only those parameters that will be needed by a routine to that routine. An event handler should not need access to the parameter block, should it? The answer is absolutely and herein lies the answer -- by passing the entire parameter block to HandleHCevent, we are able to access Hypercard globals and make callbacks from inside our event loop. The answer to the second question should now be obvious -- we need to keep the parameter block intact so that xcmds can get to the callbacks. Thus the mechanism developed for handling events is correct. We can put our minds at ease and get back to the business of writing the code which is always easier once you develop this sort of intuitive feel for the data that you’re working with; a concept which is axiomatic in object oriented programming, by the way.

If your code needs to dispathc to HandleHCMessage, then you can treat the activation as a routine xcmd call and do whatever you like in HandleHCMessage. You might create a new external window here or close an existing window. Keep in mind that you should not directly call the toolbox calls to open and close windows. Rather, you must use the calls, NewXWindow and CloseXwindow which will create the windows for you and “register” them in the xwindow layer. Aside from that, window management is almost identical in Hypercard as in a stand-alone application.

/* 3 */

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

extern pascal WindowPtr GETNEWXWINDOW(XCmdPtr paramPtr,
 ResType  templateType,
 short  templateID,
 Boolean  color,
 Booleanfloating);  
extern pascal void CLOSEXWINDOW(XCmdPtr paramPtr, WindowPtr window);

When you create an external window, you use either the call to NewXWindow or the call to GetNewXWindow. Don’t draw in the window or do anything more yet. Hypercard will tell you when the window is ready for use by sending your event loop an Xwindow event called “xOpenEvt”. Not until your xcmd gets this event are you guaranteed to have a window that is ready for use.

At openEvt, you might want to invalidate the window to force an update or append some private data to the refcon. What you do is up to you, just don’t do anything to the window until you get the xOpenEvt.

Similarly, if you have a call to xwindoids of the form: Xwindoids “Close”, windowID then you should do nothing more than issue a call to CloseXWindow and go away. Later on, your xcmd will get the “xCloseEvt” event. At this time you can deallocate any private memory that the window uses and set the passFlag to true advising Hypercard that it’s okay to close the window.

Between xOpenEvt and xCloseEvt, your window will receive more or less normal events and should respond to them in a more or less normal fashion. Things start getting a little fuzzy here as I have noticed that this event loop can have different behaviors depending on what you do with the passflag. The code in Listing 1 does work, and you might want to play with the event loop to learn a little more about the behaviour of xwindows (or xwindoids as I prefer to call them to avoid confusion with that other windowing environment).

Pay particular attention to the goaway and drag code in listing 1. The goaway method does nothing more than advise Hypercard that the user wants to close the window. Later on, HC will pass back to use the xCloseEvt event. In the meantime, the window should just be in limbo.

The drag code works fine here. I discovered that setting the passflag to true will cause Hypercard to handle dragging the window. I hope to learn more about these undocumented parts of the window code as we go along. The content, activate and update methods are pure vanilla. On activate, we just check to see if we’re going active. I take the liberty of invalidating the window to force an update, but this should not be necessary. The invalRgn should be accumulated correctly for the window.

One last thing I would like to recommend is how you respond to suspend/resume events. I like the idea of hiding all my windows on suspend so that they aren’t in the way of the next application. On resume, you should show those windows that were visible at the last suspend. You will need to keep a separate flag for this, the window’s visible won’t be much use here.

In subsequent articles, we’ll hang more decorations on this skeleton and explore Hypercard xWindoids in greater detail. In the meanwhile, take a little time to master listing 1 and get comfortable with this “call and wait” mechanism for opening and closing windows. Overall I think the structure is quite workable and should lead to some very exciting extensions to Hypercard 2.0 in the future. If you discover anything about HC2.0 that you would like to share with your fellow developers, please drop me a line. My new AppleLink is D6845. See you next month.

Listing:  Windoids.c

/************************************/
/* File: Donald Koscheka.c*/
/* --------------------------------*/
/* ©1990 Donald Koscheka  */
/* All Rights Reserved    */
/************************************/
#include<HyperXCMD.h>
#include<HyperUtils.h>
#include<SetUpA4.h>

#ifndef MouseMovedEvt
#define MouseMovedEvt0xFA 
 /* Mouse moved event code*/
#endif
#ifndef SuspendResumeEvt
#define SuspendResumeEvt  0x01
 /* Suspend/Resume event code */
#endif
#define ResumeEvtMask0x1  
 /* Supend or Resume selector */
#define ConvertScrapMask  0x2 
 /* Scrap conversion flag */

pascal void HandleHCEvent( XCmdPtr pp);
pascal void HandleHCMessage( XCmdPtr pp);
pascal void UpdateWindow( WindowPtr wind );
pascal void DoContent(  WindowPtr wind, XWEventInfoPtr ip);

pascal void main( pp )
 XCmdPtrpp;
/************************************
* MAIN ENTRY POINT
************************************/
{
 pp->returnValue = NIL;
 
 if( pp->paramCount < 0 )
 HandleHCEvent( pp );
 else{
 if( pp->paramCount == 1 )
 if( **(pp->params[0]) == '!'  || **(pp->params[0]) == '?' ){
 switch(  **(pp->params[0]) ){
 case '!': 
 pp->returnValue = PASTOZERO( pp, "\pWindoids ©1990, 1991 Donald Koscheka, 
Inc.");
 return;
 case '?':
 pp->returnValue = PASTOZERO( pp, "\pWindoids [command] <parameters>" 
);
 return;
 }
 }
 HandleHCMessage( pp ); 
 }
 UnloadA4Seg( 0L );
 RestoreA4();
}

pascal void HandleHCEvent( pp )
 XCmdPtrpp;
/**********************************
* Handle events in our xWindows  
* returns true if the event was handled ok
**********************************/
{
 short  windoPart;
 Rect   r;
 XWEventInfoPtr  ip= pp->params[0];
 WindowPtrwhichWindow;
 
 pp->passFlag = FALSE;

 switch( ip->event.what ){
 case mouseDown:
 whichWindow = ip->eventWindow;
 windoPart = FindWindow( ip->event.where, &whichWindow );
 if( whichWindow )
 switch ( windoPart ){
 case inGoAway:
 if (TrackGoAway(whichWindow, ip->event.where)) {
 CLOSEXWINDOW( pp,whichWindow );
 pp->passFlag = FALSE;
 }
 break;
 case inDrag: /* handled by hypercard */
 pp->passFlag = TRUE;
 break;
 case inGrow:
 break;
 case inContent:
 if (whichWindow != FrontWindow() )
 SelectWindow( whichWindow );
 else{
 DoContent( whichWindow, ip );
 }
 pp->passFlag = TRUE;
 break;
 default: 
 break;
 }/* window part */
 break;
 case mouseUp:
 break;
 case keyDown:
 case autoKey:
 break;
 case activateEvt: /* [DK] ON ACTIVATE, DRAW THE MENUS,
 ON DEACTIVATE HIDE THE MENUS */
 if ( ip->event.modifiers & activeFlag ){
 r= (ip->eventWindow)->portRect;
 InvalRect( &r );
 }
 pp->passFlag = TRUE;
 break;
 case updateEvt: 
 UpdateWindow(  ip->eventWindow );
 pp->passFlag = TRUE;
 break;
 case app4Evt:
 {
 unsigned char *evtType = &(ip->event.message);
 
 switch( *evtType ){
 case MouseMovedEvt:
 break;
 case SuspendResumeEvt:
 if( ip->event.message & ResumeEvtMask )
 show_all_windows();
 else
 hide_all_windows();
 break;
 }
 }
 pp->passFlag = TRUE;
 break;
 case xOpenEvt:
 ShowWindow( ip->eventWindow );
 pp->passFlag = TRUE;
 break;
 case xCloseEvt:
 pp->passFlag = TRUE;
 break;
 default: 
 break; 
 } /* switch theEvent->what */
}

pascal void DoContent( wind, ip )
 WindowPtrwind;
 XWEventInfoPtr  ip;
/*************************************
* Handle the content region in a mouse down in an xwindow. ip is  a pointer 
to the HyperXevent record, needed to see where the mouse is and what 
the modifiers are.
*************************************/
{ SetPort( wind ); }

pascal void HandleHCMessage( pp )
 XCmdPtrpp;
/*****************************************
* Hypercard has sent us a message which we need to respond to. The command 
is passed in parameter 1 and the arguments are passed in parameter 2..N
* Perhaps you'll add a little parser here to accept valid commands and 
dispatch to the correct command handler. You may pass a command here 
called "openwindow" and another called "closewindow" to allow users to 
create & destroy external windows. 
*****************************************/
{ }

pascal void UpdateWindow( xwind )
 WindowPtrwind;
/******************
* Draw the contents of the window.
* You need to develop some mechanism for storing window specific data. 
 You might try storing the info in the window's refcon.  The choice is 
up to you.  
******************/
{
 BeginUpdate( wind );
 SetPort( wind );
 ClipRect( &wind->portRect );
 EndUpdate( wind );
}

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Dropbox 193.4.5594 - Cloud backup and sy...
Dropbox 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, manage... Read more
Google Chrome 122.0.6261.57 - Modern and...
Google Chrome is a Web browser by Google, created to be a modern platform for Web pages and applications. It utilizes very fast loading of Web pages and has a V8 engine, which is a custom built... Read more
Skype 8.113.0.210 - Voice-over-internet...
Skype is a telecommunications app that provides HD video calls, instant messaging, calling to any phone number or landline, and Skype for Business for productive cooperation on the projects. This... Read more
Tor Browser 13.0.10 - Anonymize Web brow...
Using Tor Browser you can protect yourself against tracking, surveillance, and censorship. Tor was originally designed, implemented, and deployed as a third-generation onion-routing project of the U.... Read more
Deeper 3.0.4 - Enable hidden features in...
Deeper is a personalization utility for macOS which allows you to enable and disable the hidden functions of the Finder, Dock, QuickTime, Safari, iTunes, login window, Spotlight, and many of Apple's... Read more
OnyX 4.5.5 - Maintenance and optimizatio...
OnyX is a multifunction utility that you can use to verify the startup disk and the structure of its system files, to run miscellaneous maintenance and cleaning tasks, to configure parameters in the... Read more
Hopper Disassembler 5.14.1 - Binary disa...
Hopper Disassembler is a binary disassembler, decompiler, and debugger for 32- and 64-bit executables. It will let you disassemble any binary you want, and provide you all the information about its... Read more

Latest Forum Discussions

See All

Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »
Live, Playdate, Live! – The TouchArcade...
In this week’s episode of The TouchArcade Show we kick things off by talking about all the games I splurged on during the recent Playdate Catalog one-year anniversary sale, including the new Lucas Pope jam Mars After Midnight. We haven’t played any... | Read more »
TouchArcade Game of the Week: ‘Vroomies’
So here’s a thing: Vroomies from developer Alex Taber aka Unordered Games is the Game of the Week! Except… Vroomies came out an entire month ago. It wasn’t on my radar until this week, which is why I included it in our weekly new games round-up, but... | Read more »
SwitchArcade Round-Up: ‘MLB The Show 24’...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for March 15th, 2024. We’re closing out the week with a bunch of new games, with Sony’s baseball franchise MLB The Show up to bat yet again. There are several other interesting games to... | Read more »
Steam Deck Weekly: WWE 2K24 and Summerho...
Welcome to this week’s edition of the Steam Deck Weekly. The busy season has begun with games we’ve been looking forward to playing including Dragon’s Dogma 2, Horizon Forbidden West Complete Edition, and also console exclusives like Rise of the... | Read more »
Steam Spring Sale 2024 – The 10 Best Ste...
The Steam Spring Sale 2024 began last night, and while it isn’t as big of a deal as say the Steam Winter Sale, you may as well take advantage of it to save money on some games you were planning to buy. I obviously recommend checking out your own... | Read more »
New ‘SaGa Emerald Beyond’ Gameplay Showc...
Last month, Square Enix posted a Let’s Play video featuring SaGa Localization Director Neil Broadley who showcased the worlds, companions, and more from the upcoming and highly-anticipated RPG SaGa Emerald Beyond. | Read more »
Choose Your Side in the Latest ‘Marvel S...
Last month, Marvel Snap (Free) held its very first “imbalance" event in honor of Valentine’s Day. For a limited time, certain well-known couples were given special boosts when conditions were right. It must have gone over well, because we’ve got a... | Read more »
Warframe welcomes the arrival of a new s...
As a Warframe player one of the best things about it launching on iOS, despite it being arguably the best way to play the game if you have a controller, is that I can now be paid to talk about it. To whit, we are gearing up to receive the first... | Read more »
Apple Arcade Weekly Round-Up: Updates an...
Following the new releases earlier in the month and April 2024’s games being revealed by Apple, this week has seen some notable game updates and events go live for Apple Arcade. What The Golf? has an April Fool’s Day celebration event going live “... | Read more »

Price Scanner via MacPrices.net

Apple Education is offering $100 discounts on...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take $100 off the price of a new M3 MacBook Air.... Read more
Apple Watch Ultra 2 with Blood Oxygen feature...
Best Buy is offering Apple Watch Ultra 2 models for $50 off MSRP on their online store this week. Sale prices available for online orders only, in-store prices may vary. Order online, and choose... Read more
New promo at Sams Club: Apple HomePods for $2...
Sams Club has Apple HomePods on sale for $259 through March 31, 2024. Their price is $40 off Apple’s MSRP, and both Space Gray and White colors are available. Sale price is for online orders only, in... Read more
Get Apple’s 2nd generation Apple Pencil for $...
Apple’s Pencil (2nd generation) works with the 12″ iPad Pro (3rd, 4th, 5th, and 6th generation), 11″ iPad Pro (1st, 2nd, 3rd, and 4th generation), iPad Air (4th and 5th generation), and iPad mini (... Read more
10th generation Apple iPads on sale for $100...
Best Buy has Apple’s 10th-generation WiFi iPads back on sale for $100 off MSRP on their online store, starting at only $349. With the discount, Best Buy’s prices are the lowest currently available... Read more
iPad Airs on sale again starting at $449 on B...
Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices again for $150 off Apple’s MSRP, starting at $449. Sale prices for online orders only, in-store price may vary. Order online, and choose... Read more
Best Buy is blowing out clearance 13-inch M1...
Best Buy is blowing out clearance Apple 13″ M1 MacBook Airs this weekend for only $649.99, or $350 off Apple’s original MSRP. Sale prices for online orders only, in-store prices may vary. Order... Read more
Low price alert! You can now get a 13-inch M1...
Walmart has, for the first time, begun offering new Apple MacBooks for sale on their online store, albeit clearance previous-generation models. They now have the 13″ M1 MacBook Air (8GB RAM, 256GB... Read more
Best Apple MacBook deal this weekend: Get the...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New 15-inch M3 MacBook Air (Midnight) on sale...
Amazon has the new 15″ M3 MacBook Air (8GB RAM/256GB SSD/Midnight) in stock and on sale today for $1249.99 including free shipping. Their price is $50 off MSRP, and it’s the lowest price currently... Read more

Jobs Board

Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
Senior Software Engineer - *Apple* Fundamen...
…center of Microsoft's efforts to empower our users to do more. The Apple Fundamentals team focused on defining and improving the end-to-end developer experience in Read more
Relationship Banker *Apple* Valley Main - W...
…Alcohol Policy to learn more. **Company:** WELLS FARGO BANK **Req Number:** R-350696 **Updated:** Mon Mar 11 00:00:00 UTC 2024 **Location:** APPLE VALLEY,California Read more
Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill WellSpan Medical Group, York, PA | Nursing | Nursing Support | FTE: 1 | Regular | Tracking Code: 200555 Apply Now Read more
Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.