TweetFollow Us on Twitter

File Dialog
Volume Number:3
Issue Number:2
Column Tag:ToolBox Tricks

Not So Standard File Dialogs

By Paul Snively, Contributing Editor, Icom Simulations, Inc.

Last month I discussed how INIT resources could be debugged using TMON, as I did in developing my Set/Boot Paths desk accessory, used in TML Pascal 2.0. Set Paths allows the user to specify what path to use via what I prefer to call the Not-So-Standard File dialog. It's Not-So-Standard because it doesn't display file names at all, only folder names. That's not so weird (nor is it particularly difficult to code), but the "Open" button has become "Select" and, the tricky part, double-clicking on a folder name opens it exactly the way you'd expect, whereas highlighting a folder name and clicking on "Select" returns you to your program with pertinent information about the selection.

Experienced Standard File hackers may knot their brows at this (as I did when I was asked to do it that way) because they know that the Standard File internally coerces double-clicks on names to be a single click followed by a click on the "Open" button, so that the Standard File hook can filter double-clicks. The trick, then, is being able to use a user-written Standard File hook to distinguish between a double-clicked name and a selected name followed by a "Select" click.

Note that my solution is a kludge by just about any definition of the word, and would be universally spurned (even by me) were it not for one simple, overriding fact: it does NOT rely on internal knowledge of Standard File's workings AT ALL, and it was simple and obvious enough to have gone from concept to implementation in less than twelve hours (an important consideration for me; I modemed the resultant files to TML Systems Sunday evening; TML Pascal 2.0 started shipping the next day)!

To start off on an embarrassing note, in the process of writing Set Paths I discovered a bug in my McAssembly Pascal interface macros. Those were published in MacTutor Vol. 2, No. 3, in March 1986, which says two things to me: I need to be more careful in my testing of things before I publish them, and no one has done anything significant with those macros, otherwise they would have encountered the bug and, hopefully, written a letter to the editor about it! I'm profoundly disappointed

Anyway, the bug is in the "Exit" macro. There's a line which is responsible for adjusting the stack back to normal by dropping input parameters. It says:

add.l    #.fsize-.parms,sp

This is a definite boo-boo, because it totally neglects the space that we allocated for the stacked A6 register and the return address of the function. The line SHOULD read:

add.l   #.fsize-.parms-8,sp.

Boy, do I feel appropriately chastized!

Having cleared that up, I can talk safely about using those macros to implement the Not-So-Standard File dialog.

First we need to come up with a way to display NO files at all. The first time I tackled this, ol' Paul thought he'd be tricky and just set the numTypes parameter to _SFGetFile to zero, thereby forcing _SFGetFile to ignore all files, right? Wrong! Even though I had a typeList consisting of all zeros and a numTypes of zero, _SFGetFile insisted on displaying ALL files on the volume, and it took forever to do it, too!

I decided to do something about the INCREDIBLY slow response I was getting from the above scheme. Besides, it didn't work! I changed numTypes to one and used a fileType of '????' to get as few files as possible (theoretically zero). Since every so often you will indeed see a file of type '????' it behooved me to ensure that they didn't show up in the file list. To do that I had to use that fileFilter that I had tried to avoid.

The fileFilter proc is, like most such things for the Macintosh, designed with the expectation that it will be written conforming to Pascal-style parameter passing conventions. It is (in Pascal terms, anyway) a function, not a procedure, since it returns a value. It takes one parameter, a pointer to a low-level parameter block a lá the file system, and returns a boolean. It seems a little backwards: the boolean should be TRUE if the file is NOT to be displayed, and FALSE if the file IS to be displayed. So, since we want to display NO files, we should simply set the boolean to TRUE and we are done! In Pascal this would look something like this:

FUNCTION MyFileFilter(PB : ParamBlockRec;) : BOOLEAN;

BEGIN {MyFileFilter} 
 MyFileFilter := TRUE;
END; {MyFileFilter}

In McAssembly, with my Pascal macros, it's almost as easy: (See MacTutor Vol. 2, No. 3 March 1986 for the definition of these Macros.)

 MyFileFilter  EQU *
 ;
 SFBegin
 WordResult MyFilterResult
 Long   MyParamBlock
 SFEnd
 ;
 Enter
 move.w #-1,MyFilterResult
 Exit

The combination of the file type of '????' and the above fileFilter works like a charm; by golly, no files show up!

SFHook

The remaining magic is in an underdiscussed piece of code called a SFHook. Apple describes the SFHook as something that can be used to make a non-standard get or put file (which I almost do) or to make the normal one behave in non-standard ways (which is EXACTLY what I do)!

The SFHook is also a Pascal-like entity; it takes two parameters; the item number (an integer), and the DialogPtr to the Standard File dialog. It returns an integer (an item number also, although it may or may not be the same as the one that was passed to it)!

The SFHook is called constantly throughout the operation of the Standard File dialog; it's called before the dialog is drawn, it's called when there are significant events, and it's even called when there are NO significant events!

The key to understanding the Standard File dialog as it applies to writing a SFHook is the item number. The item number is ordinarily simply the item number of whatever the user clicked on in the dialog box. In addition to that there have always been a few "phony" items numbers, such as item # 100 (which is what was passed when nothing interesting was going on) or keystrokes (item # 1000 plus the ASCII value of the key). Another useful value is -1, which is the value passed before the dialog is displayed. By trapping on this value we can do neat things like changing the title of the "Open" button to "Select." You'll see an example of that a bit later.

The introduction of HFS added a whole new wrinkle to the issue of the Standard File dialog. It had to be expanded in a way that retained the power and flexibility of the original design, yet also remain upwardly compatible. WDRefNum's and a few new phony item numbers fit the bill rather nicely.

As all users of the Standard File dialog are aware of at one level or another, "Opening" a folder doesn't return you to the application, it simply makes that folder the current directory and shows you the files in it, etc. You must open a FILE to get back from _SFGetFile.

Internally what happens is that opening a folder passes a phony item # 103 to the SFHook, as opposed to a file open, which passes item # 1 (the item # of the "Open" button). So we could catch the 103, coerce it to a 1, and pass it back. The problem there, of course, is the one mentioned before: double-clicks on filenames and filename select/"Open" click sequences are equivalent by the time you get to the SFHook! The result is that double-clicking on a foldername returns from _SFGetFile just as surely as selecting one and clicking "Open" does!

Argh!!!

Ok, enough beating around the bush. Obviously the solution to the problem is to find out which item we clicked on, the file list or the "Select" button. I decided that the Mac was fast enough that I could determine within the SFHook where the mouse was, check to see if it was over the file list and, if it was not, coerce the 103 to a 1. Fortunately, one of the things passed to the SFHook is the DialogPtr, making calls to _GetDItem possible. Among other things, _GetDItem returns the viewRect of the item - just what the doctor ordered! _GetMouse gives us our mouse location in local coordinates (another stroke of luck) and _PtInRect answers the burning question: are we pointing to this item or aren't we?

Pulling it all together into a nice, not-so-neat package gives us the following:

UseSF  move.w  #100,-(sp) Top = 100
 move.w #100,-(sp) Left = 100
 pea  PromptString Use promptstring
 pea  myFileFilter Use brief fileFilter
 move.w #1,-(sp) No. of filetypes
 pea  MyListPoint to my list
 pea  myDlgHook  Point to our dlgHook
 pea  myReplyRec Point to reply rec
 _SFGetFile Use std file dialog
 
myDlgHook
;
 sfbeginDeclare stack frame
 wordresult myDlgResult Function result
 word mySFItem Which item was hit?
 long theDialog  SFGetFile dialog ptr
 sfend  That's all!
;
 enter  Set up stack frame
 move.w mySFItem,d0Get the item#
 cmp.w  #-1,d0 Is this first time?
 bne  NotFirst Go if not
 sub.l  #14,sp Room for VAR params
 move.l theDialog,-(sp) Stack DialogPtr
 move.w #getOpen,-(sp)  Stack item # of "Open"
 pea  18(sp)VAR itemType
 pea  18(sp)VAR itemHandle
 pea  14(sp)VAR dispRect
 _GetDItemTell me about button
 movea.l8(sp),a0 Get handle
 add.l  #14,sp Drop data structure
 move.l a0,-(sp) Stack item handle
 pea  myTitle  Pass title address
 _SetCTitle Make title = STR
 moveq  #-1,d0 Make sure we're here
NotFirstequ *
 cmp.w  #103,d0  Was it "Select?"
 bne.s  NotSelectGo if not
;***********************************************************
;***  This is where we will do all kinds of nifty magic ***
;**********************************************************
 sub.l  #14,sp Room for VAR params
 move.l theDialog,-(sp) Stack DialogPtr
 move.w #7,-(sp) Stack File List item#
 pea  18(sp)VAR itemType
 pea  18(sp)VAR itemHandle
 pea  14(sp)VAR dispRect
 _GetDItemTell me about File List
 clr.l  -(sp)  Room for pt
 pea  (sp)Point to the point
 _GetMouseHere, mousie, mousie...
 pea  4(sp) Point to the rect
 _PtInRectAre we in File List?
 move.w #103,d0  Just to make sure
 move.b (sp)+,d1 Get answer
 add.l  #12,sp Drop remaining data
 bne.s  NotSelectIf in File List, leave
 move.w #1,d0  Treat like file open
NotSelect equ  *
 move.w d0,myDlgResult  Store function result
 exit   Exit the function
;
myFileFilterequ  *
 loc  New locals here; needed for McAssembly
;
 sfbeginStack Frame Begin
 wordresult myFilterResultA boolean
 long parmBlkPtr A pointer
 sfend  Stack Frame End
;
 enter  Set up stack frame
 move.w #-1,myFilterResultTRUE;
 exit   Clean up and leave
;
PromptStringequ  *
 text # "Highlight a folder and click /"Select/""
 align
myTitle equ *
 text # "Select" title for "Open" button
 align
myList  equ *
 text "????"Any old file type will do
 dcb.b  12,0Remaining are nulls

Now that I've given the code that does the trick, I should probably say a few words about how the replyRec looks when you've opened a folder instead of a file.

First of all, you can forget about the fName field. It won't be valid. Instead, vRefNum will contain the vRefNum of the folder, just like it does for a file, and - get this - fType will return the DirID of the folder ("Sorry about the type conflict," says Apple in the software supplement...obviously talking to Pascal programmers). Of course, the vRefNum and DirID are sufficient for all HFS OSTraps that deal with folders or files within a specific folder. Another thing that you can do if you want or need to is convert the vRefNum and DirID to a pathname extending from the root to the folder (or more precisely, from the folder to the root). One implementation of that algorithm appeared in MacTutor's special HFS issue (January '86). It was written in C by Mike Schuster. Translating it to the language of the reader's choice is an exercise left to the reader (the assembler version is actually rather simple).

That just about covers it. Feel free to use the Not-So-Standard File dialog anytime you have a reason to want to select a folder instead of a file!

 

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

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.