TweetFollow Us on Twitter

Multifile Dialogs in CPX

Volume Number: 13 (1997)
Issue Number: 7
Column Tag: Toolbox Essentials

Creating Multi-File Dialogs in CPX

by John Shackelford, Tangent Systems

How to build a custom file dialog with CustomGetFile and how to manage the dialog callback routines

Introduction

The ease that applications are built in CPX often masks the flexibility of writing low-level code for the Macintosh. Prograph CPX ships with a class library called Application Building Classes (ABCs) which, in the world of Macintosh application frameworks, ranks among the richest. However, one particularly useful chunk of code that you will not find in the ABCs is the fancy file dialog you create using CustomGetFile.

You use the CustomGetFile feature to add source files to projects in several popular Macintosh applications like CodeWarrior, Roaster or Symantec Project Manager. The File classes in the ABCs all use the Standard GetFile dialog to retrieve files. This is fine for single file operations, but if your application must provide the ability to manipulate several files at once, CustomGetFile must be used to add controls for collecting the list of files. Surprisingly, when I wrote the code for a CPX version of CustomGetFile, it was difficult to find a complete example in C or Pascal on which to base the design. I did find some chunks or pieces in C and some CPX that I used as a basis for MultiFile.

MultiFile is a CPX section. It provides a class for a multifile dialog of the type shown below. It allows you to navigate, add and subtract several files all in one dialog session. The section also provides a basic file dialog allowing you to specify the user prompt. However, this article focuses on MultiFile Dialog -- it shows you how to use the MultiFile Dialog class and discusses key code snippets.

MultiFile Demo Application

The demo application opens a MultiFile Dialog and allows you to add and subtract files. Its purpose is to demonstrate what a MultiFile Dialog provides (see Figure 1). The demo application is one of those minimal apps. It does not use the ABCs and has no value other than showing you how the MultiFile Dialog looks.

Figure 1. The Demo Application opens a MultiFile Dialog.

The Macintosh Toolbox provides a function called CustomGetFile for creating custom file dialogs. First, I'll show you the top level CPX code for using it. That way, if all you want is to use MultiFile and you don't care about how it works, you'll have all you need to know. I will also show the function prototype and build up the associated functions for it in CPX.

Figure 2. The MultiFile section comes with an example method for using MultiFile.

Using MultiFile is pretty straightforward -- explaining it will be much more difficult. However, as you can see in Figure 2, all you do is create an instance of MultiFile Dialog, setup the type of files you want with the message "/setTypeList" and finally send the message "/multiFileGet" to get the whole process rolling. When the method completes, the list of selected files appears on the output of "/multiFileGet."

How it All Works

I'll first decompose the main method "/multiFileGet," describe the attributes of MultiFile Dialog and then expose in detail the major chunks of code that makes MultiFile Dialog work in support of CustomGetFile -- the Toolbox call we are encapsulating.

MultiFile Dialog Constructor

The first thing to look at is the constructor for MultiFile Dialog (Figure 3). When an instance of MultiFile Dialog is created, several attributes are initialized. The "Files" attribute will end up holding a new instance of File Results. This object will eventually hold the FSSpecs for the selected files, and it provides methods to add and remove FSSpecs from its internal list of FSSpecs. The attribute "Select List" is set to NULL. Eventually, it will hold a pointer to the scroll list item created by a call to LNew. The Persistent called "The Dialog" is set to the current MultiFile Dialog instance. Two other methods are executed -- one creates a Standard File Reply record while the other ("/makeCallbacks") sets up pointers to the callback functions. So by design a "The Dialog" (global) holds the MultiFile Dialog instance. Everything else hangs off of that -- the lower scroll list, StandardReplyRec and the File Results object. The callbacks rely on "The Dialog" to access the scroll list, the File Results object and the StandardReplyRec.

Figure 3. The "Constructor" for MultiFile Dialog sets up 3 Persistants.

The method "/makeCallbacks" (Figure 4) checks each of 4 attributes in MultiFile Dialog and creates a pointer to a callback method if a method name is specified. This provides a nice way for users of the class to specify their own callback methods -- just by specifying their own methods in the initialization list that can be fed to the Constructor. And of course if that does not provide enough flexibility, you can always subclass the whole thing and define "/makeCallbacks" anew.

Figure 4. "/makeCallbacks" checks each of 4 MultiFile Dialog attributes.

The local method "file filter proc" (Figure 5) is typical of the calls in "/makeCallbacks". An improvement on this local method would be to verify that the universal method is actually defined and if it isn't, then fail into a next case and set the "callback" attribute (in this specific case "File Filter Callback") value to NULL.

Figure 5. This local method is typical for what a "/makeCallbacks" method does.

The method "/multiFileGet" (Figure 6) is composed of three methods itself. The first performs the Toolbox call, the second retrieves the selected files and the final method cleans up.

Figure 6. "/multiFileGet" retrieves a list of file specifications.

/callCustomGet

The method "/callCustomGet" (Figure 7) makes the Toolbox call "CustomGetFile". The MultiFile Dialog instance carries the values necessary for the function to operate as the user wants.

Figure 7. "/callCustomGet" makes the low level Toolbox call.

Let's first look at the CustomGetFile arguments and what Prograph uses. The calling arguments for CustomGetFile are shown below (bold) with the MultiFile Dialog Attribute (underlined):

FileFilterYDProcPtr FileFilter Callback
A pointer to an optional file filter function, provided by your application, through which CustomGetFile passes files of the specified types.

short Num Types
The number of file types to be displayed. If you specify a "Num Types" value of -1, the first filtering passes files of all types.

SFTypeList /Type List
A list of file types to be displayed. You can define it using the call "/setTypeList" which automatically sets the value of attribute "Num Types" used above.

StandardFileReply * YourDataPointer
The reply record, which CustomGetFile fills in before returning. We create a pointer to a Reply record in the Constructor for MultiFile Dialog.

short DialogID
The resource ID of a customized dialog template. To use the standard template, set this parameter to 0. The MultiFile Dialog defines this value to be 1001. If you want to customize the look of the dialog, create a new dialog resource and set the DialogID to the new value in a subclass of MultiFile Dialog for the "new" dialog.

Point Where
The upper-left corner of the dialog box in global coordinates. This value is predefined to be {-1, -1} which will place it in the middle of the screen.

DlgHookYDProcPtr DialogHook Callback
A pointer to the dialog hook function, which handles item selections received from the Dialog Manager. This is in many ways the "meat" (I suppose I should instead say "fiber" for those vegetarians among us) of the matter; clicks within the dialog get handled by this method.

ModalFilterYDProcPtr ModalFilter Callback
A pointer to your modal-dialog filter function, which determines how the Dialog Manager filters events when called by CustomGetFile. Specify a value of NIL if you are not supplying your own function. We need a modal-dialog filter function because we must provide scroll control for our scrolling list. This method handles clicks in the user-defined lower scroll list. (Control of the upper list "comes" with CustomGetFile.)

short ActiveList

ActivateYDProcPtr Activate Proc Callback

void * YourDataPointer

These last three parameters are not used, and are set to NULL.

Figure 8. The default attributes are defined to work with a dialog resource 1001.

Figure 9 shows the default MultiFile dialog (ID = 1001) and the index numbers for its items.

Figure 9. Default MultiFile dialog (ID = 1001).

When CustomGetFile is called, three methods (all are Universals) take over until the user clicks "Cancel" or "Done" in the dialog window. They are "filterProc", "modalFilterYD" and "dialogHook". We'll describe each in turn.

Filter Proc Method

The "filterProc" method (Figure 10) does one job. It is called whenever the upper scroll list is about to be filled with files (after passing through the internal "types" filter). It gives us a chance to do further processing. The output of this method is a Boolean which determines whether or not the file should appear in the upper list. A TRUE output removes the item from the upper list while a FALSE value allows it to appear in the upper list. This method checks the list of FSSpecs in the File Results object. If the file is in the File Results object, this filter will remove it from the upper list.

Figure 10. "filterProc" is called whenever the upper scroll list is about to be filled with files.

The field "ioNamePtr" holds a pointer to a Pascal string. The first byte in a Pascal string must be the length of the string. we execute "get-integer" to determine the length of the string, and then we execute "get-text" using that value and start at byte 1 of the buffer. We then check the list of file names held by the File Results object (retrieved with "getFiles"). If a matching file name is found, the primitive "(in)" will return an integer greater than 0 indicating the files position in the list. In that case the method outputs TRUE. Otherwise it will output FALSE.

Modal Filter yd Method

The "modalFilterYD" method (Figure 11) is called as part of the event processing in the window. It is needed to control scrolling of the lower scroll list (the "Select List"). It is written such that it will only handle clicks within the scroll lists vertical control.

Figure 11. "modalFilterYD" controls scrolling of the lower scroll list.

Dialog Hook Method

The "dialogHook" is the biggest of these three methods. It installs the lower scroll list the first time the method is called. It also handles all clicks in the dialog. So for example when the "Add" button is clicked, the dialog hook method defines what happens in the window.

Instead of going through every case of this method (there are 15 cases), I'll just provide an overview of what the method is supposed to do:

First Time Through -- Creates the lower scroll list and installs it in the dialog window.

Add -- Adds the filename of the selected file to lower list. The file is removed from the upper the list through the file filter function. The FSSpec for the file is added to the File Results object.

Add All -- The names of all available files in the upper scroll list are added to the lower list. The FSSpecs are added to the File Results object. All files that get successfully added to the File Results are filtered from the upper list display by the file filter method.

Remove -- Enabled if an item is selected in the lower list. The selected item is removed from the File Results object, removed from the lower list and added to the upper list by the file filter method.

Remove All -- Enabled if there are any files in lower list. All files in the File Results object are removed, all items in the lower list are removed. The file filter method then allows available files to appear in the upper list.

Cancel -- Deletes all FSSpecs from the File Results object, then closes the dialog.

Done -- Closes the dialog.

Here is the code of two interesting cases to drive home how things work: what happens the first time though -- that's when we create and install the lower scroll list -- and how Add works. I leave the other cases as a review exercise for you and your CPX Debugger.

First Time Through

The first time "dialogHook" is executed, we must detect that this is the first time and display the lower list box.

Figure 12. The First Time Through case creates the lower scroll list item.

Figure 13. "initList" creates a new scroll list and places it in the position of window item 18 in the dialog resource.

The Toolbox call "LNew" creates a new scroll list. We define the rectangle for the list by getting the rectangle for window item 18 (see Figure 9). We "send" the new scroll list to the MultiFile Dialog in the message "/setSelectList".

Add file

The Add file case duplicates the FSSpec representing the selected file and sends that FSSpec as an argument in the call to "addFile".

Figure 14. The "Add" case calls "addFile" universal.

Figure 15. "addFile" universal also enables item 19 in the dialog window.

The "addFile" universal method performs several tasks. It first checks if the file can really be added. If so, it continues on by getting the value of the name field in the FSSpec and asking the File Results object for the number of items in its files list to form an index. It send those two values (name and index) as arguments to "addCell" which adds a new cell to the "Select List". It then sends the FSSpec to the File Results object in a message "/addItem" which adds the FSSpec to the File Result object. After all that, the lower scroll list is redrawn in method "updateScrollList" and window item 19 (Remove All) is enabled.

At this point "dialogHook" case 12:15 (Figure 14) outputs "sfHookRebuildList" and the method ends. The new item now appears in the lower scroll list and disappears from the upper list. The multifile dialog window waits for the next event.

Conclusion

This article describes the CPX code in the MultiFile section. MultiFile contains classes for creating special file dialogs that don't come out of the box with CPX. These classes are useful if you need a file dialog that can deal with several files at once or if you want to specify a unique prompt in a standard file dialog.

With the information in this article, CPX developers can now start using CustomGetFile based file dialogs in their applications. Simply add the MultiFile section to your project, create an instance of MultiFile Dialog, and send it the message "/multiFileGet". All of your problems will be solved.

Bibliography and References

Apple Computer's Inside Macintosh: Files, page 3-51.


John Shackelford is the founder of Tangent Systems - a software development company based in San Diego. When he's not playing with his 3 children, he's busy writing CPX code or playing the piano. He can be reached at shackx@aol.com. You can find Tangent Systems on the world wide web at http://www.tangentsys.com.

 
AAPL
$524.94
Apple Inc.
+5.93
MSFT
$40.01
Microsoft Corpora
-0.39
GOOG
$536.10
Google Inc.
-20.44

MacTech Search:
Community Search:

Software Updates via MacUpdate

PDFpenPro 6.2 - Advanced PDF toolkit for...
PDFpenPro allows users to edit PDF's easily. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Even correct text and edit graphics! Create... Read more
PDFpen 6.2 - Edit and annotate PDFs with...
PDFpen allows users to easily edit PDF's. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Even correct text and edit graphics! Features... Read more
Monolingual 1.5.9 - Remove unwanted OS X...
Monolingual is a program for removing unnecesary language resources from OS X, in order to reclaim several hundred megabytes of disk space. It requires a 64-bit capable Intel-based Mac and at least... Read more
Maya 2015 - Professional 3D modeling and...
Maya is an award-winning software and powerful, integrated 3D modeling, animation, visual effects, and rendering solution. Because Maya is based on an open architecture, all your work can be scripted... Read more
Starcraft II: Wings of Liberty 1.1.1.180...
Download the patch by launching the Starcraft II game and downloading it through the Battle.net connection within the app. Starcraft II: Wings of Liberty is a strategy game played in real-time. You... Read more
Sibelius 7.5.0 - Music notation solution...
Sibelius is the world's best-selling music notation software for Mac. It is as intuitive to use as a pen, yet so powerful that it does most things in less than the blink of an eye. The demo includes... Read more
Typinator 5.9 - Speedy and reliable text...
Typinator turbo-charges your typing productivity. Type a little. Typinator does the rest. We've all faced projects that require repetitive typing tasks. With Typinator, you can store commonly used... Read more
MYStuff Pro 2.0.16 - Create inventories...
MYStuff Pro is the most flexible way to create detail-rich inventories for your home or small business. Add items to MYStuff by dragging and dropping existing information, uploading new images, or... Read more
TurboTax 2013.r17.002 - Manage your 2013...
TurboTax guides you through your tax return step by step, does all the calculations, and checks your return for errors and overlooked deductions. It lets you file your return electronically to get... Read more
TrailRunner 3.8.769 - Route planning for...
Note: While the software is classified as freeware, it is actually donationware. Please consider making a donation to help support development. TrailRunner is the perfect companion for runners,... Read more

Latest Forum Discussions

See All

Livescribe 3 Smartpen Review
Made by: Livescribe Price: $149.99 for Livescribe 3 Hardware/iOS Integration Rating: 4.5 out of 5 stars Usability Rating: 4 out of 5 stars Reuse Value Rating: 4.75 out of 5 stars Build Quality Rating: 4.5 out of 5 stars Overall Rating: 4.44 out of... | Read more »
Unpossible Review
Unpossible Review By Carter Dotson on April 17th, 2014 Our Rating: :: RALPH WIGGUM APPROVESUniversal App - Designed for iPhone and iPad Unpossible is much better than its English!   | Read more »
Hitman GO Review
Hitman GO Review By Carter Dotson on April 17th, 2014 Our Rating: :: GO HITMAN, GO!Universal App - Designed for iPhone and iPad Hitman GO is not the obvious way to do a mobile version of the Hitman series, but it’s an incredibly... | Read more »
Monster High Ghouls and Jewels is a Frea...
Monster High Ghouls and Jewels is a Freaky Fashion-Forward Match-3 Puzzler Posted by Rob Rich on April 17th, 2014 [ permalink ] | Read more »
Dinosaur Train A to Z Review
Dinosaur Train A to Z Review By Amy Solomon on April 17th, 2014 Our Rating: :: DINO DETAILSUniversal App - Designed for iPhone and iPad Dinosaur Train A to Z is an educational app about dinosaurs that includes In-App Purchases... | Read more »
Easter Comes to Junk Jack X – Bringing N...
Easter Comes to Junk Jack X – Bringing New Crafts, Chemistry, and More Posted by Rob Rich on April 17th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Call of Cookie Review
Call of Cookie Review By Jordan Minor on April 17th, 2014 Our Rating: :: COOKIE CRUMBLESUniversal App - Designed for iPhone and iPad Call of Cookie proves that plants aren’t the only fighting foods out there.   | Read more »
Corel Launches Video Editing App Pinnacl...
Corel Launches Video Editing App Pinnacle Studio for iPhone, Updates iPad Version for iOS 7 Posted by Tre Lawrence on April 17th, 2014 [ | Read more »
Bad Vamp Review
Bad Vamp Review By Jennifer Allen on April 17th, 2014 Our Rating: :: BASIC VAMPIRIC ADVENTURESUniversal App - Designed for iPhone and iPad Run or destroy the vampires in this simple, single-screen game that lacks real bite.   | Read more »
The Shadow Sun gets a New Update, Goes o...
The Shadow Sun gets a New Update, Goes on Sale for the First Time Since its Release Posted by Tre Lawrence on April 17th, 2014 [ permalink ] | Read more »

Price Scanner via MacPrices.net

Hyundai Brings Apple CarPlay To The 2015 Sona...
Hyundai Motor America has announced it will bring Apple CarPlay functionality to the 2015 Sonata. CarPlay is pitched as a smarter, safer and easier way to use iPhone in the car and gives iPhone users... Read more
Updated iPads Coming Sooner Than We Had Thoug...
MacRumors, cites KGI securities analyst Ming Chi Kuo, well-respected as an Apple product prognisticator, saying that Apple will introduce an upgraded iPad Air and iPad mini in 2014/Q3, meaning the... Read more
Toshiba Unveils New High And Low End Laptop M...
Toshiba has announced new laptop models covering both the high-end and low-end of the notebook computer spectrum. Toshiba 4K Ultra HD Laptop Toshiba’s new Satellite P55t features one of the world’s... Read more
Save up to $270 with Apple refurbished 13-inc...
The Apple Store has Apple Certified Refurbished October 2013 13″ Retina MacBook Pros available starting at $1099, with models up to $270 off MSRP. Apple’s one-year warranty is standard, and shipping... Read more
Apple now offering refurbished iPad mini with...
The Apple Store has Certified Refurbished 2nd generation iPad minis with Retina Displays now available starting at $339. Apple’s one-year warranty is included with each model, and shipping is free.... Read more
Microsoft Blinks – Drops Microsoft Office 365...
Microsoft has dropped the annual subscription fee for Microsoft Office 365 Personal – which is needed in order to create and edit documents in Microsoft Office for iPad. However, Apple’s iOS and OS X... Read more
New AVG Vault Apps for iOS and Android Help K...
AVG Technologies N.V. an online security company for 177 million active users, has announced the launch of its latest mobile application, AVG Vault. The free app introduces an innovative user... Read more
Free Local Carrot iPhone App Helps Find Fresh...
I love fresh vegetables. I’m not a vegan, although I was for several years in the 1980s, but fresh vegetables and other whole foods are still my dietary mainstays as a matter of taste rather than... Read more
CarSO Pro – Car Service and Finance Manager/O...
Lviv, Ukraine-based BM-Studios’ CarSO Pro is a tool to manage operations concerning your car. Never forget to change the oil or prolong the insurance for your car. Remember when you’ve done the car... Read more
Apple refurbished iPad Airs available startin...
Apple is now offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. The following Airs are available today: -... Read more

Jobs Board

*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Retail - Manager - Holyoke - Apple I...
Job Summary Keeping an Apple Store thriving requires a diverse set of leadership skills, and as a Manager, you’re a master of them all. In the store’s fast-paced, Read more
*Apple* Retail - Manager - Apple (United Sta...
Job SummaryKeeping an Apple Store thriving requires a diverse set of leadership skills, and as a Manager, you're a master of them all. In the store's fast-paced, dynamic Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Retail - Market Leader - Cincinnati...
…challenges of developing individuals, building teams, and affecting growth across Apple Stores. You demonstrate successful leadership ability - focusing on excellence Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.