TweetFollow Us on Twitter

Custom Input Dialog
Volume Number:1
Issue Number:8
Column Tag:Pascal Procedures

"Custom Dialog Box for Input"

By Alan Wootton, President, Top-Notch Productions, MacTutor Contributing Editor

Our Project for this month is to create a pair of input statements for MacPascal. In Basic, there is a command that prompts the user for a number or a string. It would be nice to have this for MacPascal, but would be even better if it could be implemented using the excellent MacIntosh user interface. Towards this end, we will be using the Dialog Manager, Package 7, and Resources.


Previous generations of computers provided only files as storage entities. Any small or large chunk of numbers to be accessed by the operating system usually required a file to hold it. If a program needed a large number of small data areas the result was usually a large number of files. This happened all too often.

The Mac is different. On the Mac a file name actually represents two files. One of these is the usual type of file, the other is full of tiny subfiles. A whole new world! Naturally, there is a sort of directory to go with these subfiles.

Inside Mac calls the two files "forks" of a file. The subfiles are "resources" and the directory is the "resource map". In a normal file system all files are referred to by name and possibly a version number. Resources are referred to by a four character name called the "TYPE" (actually any longint) and an integer known as the "ID number". Many of the ROM routines access a particular TYPE with the ID being variable. For instance, a menu can be created by providing a number and the Menu Manager will access the resource with TYPE='MENU' and the ID provided.

File access can be quite a programming hassle, so you are probably imagining that resource access is even worse. Not true. There is no way to read in only part of a resource, so the subtleties of file i/o can be ignored. You provide a name and the resource manager will open the file for you. When you provide a TYPE and an ID the resource manager will search the maps that are open, read the data in, if necessary, and return a handle to that data. There are some fine points concerning control over when the data is read in, when it is to be purged from memory, which heap it goes on, and others. For our purposes it is enough to just open the file.

Resource Tools

Every computer has some sort of software to manipulate and view the files, so what does one use for resources? There are four tools from Apple to work with resources.

The simplest is RMover. RMover will simply list the resources in a file. You may also cut and paste resources between files. RMover will recognize some types as probably having a certain form of data and display it appropriately. Examples are STR (a string), PICT (a picture), ICON (an icon). In each case the data is simply so many bytes, but it is assumed that a particular type is used exclusively for a single brand of data (which is not a law). Every Mac freak should rummage through the files on his disks with RMover to get a feel for what is there (check out MacPascal!).

For programming, the next most useful tool is RMaker. This is a sort of compiler for resources. It knows the form of the data for windows, menus, dialog boxs, and more. Make a text file with coded descriptions in it, run RMaker and a new file is created with the described resources in it. RMaker is supplied with the MDS assembler from Apple and with some compilers (note: MDS is now shipping and includes a copy of Inside Macintosh). Get a copy of RMaker as soon as possible (there is a 10 page document you'll also need).

Recently, two editors of resources have become available. These are "Resource Editor" and "Redit". Both will edit resource data, often in a friendly format. For instance, the Resource Editor will allow you to edit an icon fatbits style and not as 128 bytes.

If you bought Inside MacIntosh and the software supplement these tools were sent to you. If not, there are still some places to get them. MacTutor Utility disk #1 has resource tools (see inside front cover for ordering).

The Dialog Manager

Now we will get back to the problem at hand: how to make a box that asks for input. What we ultimately need is a window (which has a grafport) and some controls. We also will need to use the text editor to edit the input data. If it weren't for the Dialog Manager this would be real work. Fortunately, the Dialog Manager will perform all of these chores for us. I won't go too deeply into this because that is what IM is for, but all you have to do is give the dialog manager an id to a resource of type DLOG and, assuming that the data is in the correct form, everything is automatic.


The first step in our project is to create a file with three resources in it. Type Classy_Input.r (below) into a text file and apply RMaker to it. After that, use RMover to look and see if the resources exist in the file as planned. If you don't have RMaker you might try making the file with Resource Editor. If there is a lot of trouble creating this file, (it's important) write to me care of MacTutor and I will do a resource creation project. Actually, we should use RMaker input files as the official resource specification and exchange method.


Next, boot MacPascal and type in Classy_Input.pas (below). This is a very simple program. First we open our resource file. The dialog box is created by GetNewDialog, operated by ModalDialog, and erased by DisposDialog. Simple! Of course, I've gotten fancy and used GetDItem and SetIText to change the text of some items. It is also necessary to use a GetDItem-GetIText combination to get the text from the edittext item. With these same simple routines it is possible to do much more elaborate dialogs. As a matter of fact, you can completely change the appearance of this example by merely changing the resource specification. Note that this is the same method used by real Mac applications and is not just a kludge for educational purposes.

You may omit Prompt_For_Number at first if you wish. After you have Prompt_for_String working you may wish to try for numbers. The prompt for strings looks like that shown at the top of the next column.

Fig. 1 Program Sample

To use this same box to prompt for numbers we will use Package #7. To access a package you use it's inline trap and leave a selector on the stack: eg. inlineP(TRAP,selector). However, Pack#7 is register based so it is necessary to use the register based interface that I presented in MacTutor No.6.

GetNewDialog accesses resource DLOG 12345 and DITL 12345 in order to determine the characteristics of the box it will make. Pack#7 loads resource PACK 7 (in the file "System"), locks it on the heap and then calls it as a procedure (we will call our own procedures the same way in later articles). By far the simplest of the resource-using toolbox routines is GetString:

procedure GetString(ID:integer):Handle;
 GetString:=GetResource('STR ',ID);

This also one of the simplest traps of any kind. All text of any kind should be in resources for later translation. This is the reason we use GetString, and also the reason we use GetNewDialog and not NewDialog. You should take up the habit of using resources this way,too.

;;     *********************************************
;;     **                                                            
;;    **    Resource data for Classy_Input         **
;;    **                                                             
;;    **    Process this TEXT file through           **
;;    **    RMaker:the Resource Compiler        **
;;    **     by Andy Hertzfeld.                                    **
;;    **                                                             
;;    *********************************************
;;     double semicolons start comments
;; destination volume:filename 
;; type and creator of file = ???? ????

;;        ****************************************
;;        **  The first resource is the                  **
;;        **  definition of a Dialog Manager    **
;;        **  window.                                                
;;       ****************************************
;;  global coordinates !!

type DLOG
;;  note we have no title
96 128 148 384          ;; top left bottom right 
visible goaway
1                                          ;; window type = dBoxProc 

0                                          ;; refcon
12345                             ;; ID of DITL associated
                                            ;; with this DLOG

;;       ****************************************
;;       **      Next is a list of 'items' to go       **
;;       **       in the window.                                 **
;;       ****************************************

type DITL                            ;; see Dialog Manager
4                                               ;; four items 

4 120 24 180                    ;;  local coordinates !!

4 188 24 248

EditText Disabled  
32 8 48 248        
;;;;;;;;;;;;;;;;;;text added later 

StaticText Disabled  
4 8 20 120           
Type a String         ;; prompt text, modified later

;;      disabled means that the Dialog Manager
;;      will not report events in this control
;;          *********************************************
;;          **     And finally, the prompt used by        **
;;          **    Prompt_For_Number                              **
;;          *********************************************

type STR 
Type a number

;;         ****************************************
;;         **  end of file                                           
;;         ****************************************

program Classy_Input_test;{ by Alan Wootton 5/85 }
  Ptr = ^integer;
  Handle = ^Ptr;
  StrPtr = ^str255;

 var{ for test, not a part of final logic }
  rid : integer;
  number : longint;
  words : str255;

 function Prompt_for_Str (prompt, sample : str255) :
   DlogPtr : Ptr;
   itype, itemHit : integer;
   r : rect;
   itemH : Handle;
   tempStr : str255;

{ This uses DLOG ID=12345 and associated DITL with }
{ the same ID.  Items are: #1=OK , #2=Cancel, }
{ #3=result, #4=prompt.  First we will _GetDitem to }
{ obtain handle and then _SetIText. For item#3= result }
{ string, and item#4 = prompt string. This will leave }
{ itemH referring to result item. }
  DlogPtr:= pointer(LinlineF($A97C,12345,nil,pointer(-1))); 
{ _GetNewDialog }

  inlineP($A98D, DlogPtr, 4, @itype, @itemH, @r);
{ _GetDitem }
  if prompt <> '' then
   inlineP($A98F, itemH, @prompt);{ _SetIText }

  inlineP($A98D, DlogPtr, 3, @itype, @itemH, @r);
{ _GetDitem }
  if sample <> '' then
   inlineP($A98F, itemH, @sample);{ _SetIText }

  inlineP($A991, nil, @itemHit);
{ _ModalDialog, returns with 1 or 2 in itemHit }

  if itemHit = 2 then { if Cancel }
   tempStr := ''
  else {  if OK }
   inlineP($A990, itemH, @tempStr);
{ _GetIText  from item#3 }

  inlineP($A983, DlogPtr);{ _DisposDialog }
  Prompt_for_Str := tempStr;

 function Prompt_for_Number (sampN : longint) :
   sHan : ^StrPtr;
   str : str255;
   access : array[0..12] of integer;
   d0, a0 : longint;{ the 68000 registers }
{ What we will do here is convert the input number }
{ into a string and then pass it to Prompt_for_String. }
{ Then we convert the returned string into a number.  }
{ The conversions are done with  $A9EE=pack7 . }
{ See Packages. }
{ Access is a 2 register OS interface that we must }
{ use because the call is register based yet also requires}
{ for the selector (the 1 or 0) to be on the stack  }
{ (and 'Generic' won't do that) }
  stuffHex(@access, '2848548C41FA000C309F245F265F20522013FFFF224826804ED4');
  a0 := ord(@str);
  d0 := sampN;
  inlineP($4E75, 0, @d0, @a0, $A9EE, @access);
{ pack7,0  is _NumtoString }
{    Str is now the string of  sampN }

  sHan := Pointer(LinlineF($A9BA, 12345));
{ _Getstring, numeric prompt  }
  str := Prompt_for_Str(sHan^^, str);
{ Str is now new string,  convert d0 to num. } 
{ A0 is still @str }

  inlineP($4E75, 1, @d0, @a0, $A9EE, @access);
{ pack7,1  is _StringtoNum }
  Prompt_for_number := d0;

begin{  main procedure, used only to exercise }
{ procedures above. }

 rid := WinlineF($A997, 'MacTutor_disk:Classy_Input.rsrc');{ _OpenResFile 
{ this file is the output of Rmaker, we open it to access }
{ our resources }
 if rid < 0 then
  writeln('error opening file', rid)
   words := Prompt_for_Str('Type a string', 'sample string');
   writeln(' the words returned are:', words);

   words := Prompt_for_Str('', '');{ no prompts }
   writeln(' the words returned are:', words);

   number := Prompt_for_Number(1234);
   writeln('the number returned is', number);
 inlineP($A99A, rid);{ _CloseResFile}


Community Search:
MacTech Search:

Software Updates via MacUpdate

The beginner's guide to Warbits
Warbits is a turn-based strategy that's clearly inspired by Nintendo's Advance Wars series. Since turn-based strategy games can be kind of tricky to dive into, see below for a few tips to help you in the beginning. Positioning is crucial [Read... | Read more »
How to upgrade your character in Spellsp...
So you’ve mastered the basics of Spellspire. By which I mean you’ve realised it’s all about spelling things in a spire. What next? Well you’re going to need to figure out how to toughen up your character. It’s all well and good being able to spell... | Read more »
5 mash-ups we'd love to...
If there's one thing that has proved, it's that the addictive gameplay of can be transplanted onto basically anything and it will still be good fun. It wouldn't be surprising if we saw other developers jumping on the bandwagon,... | Read more »
How to navigate the terrain in Sky Charm...
Sky Charms is a whimsical match-'em up adventure that uses creative level design to really ramp up the difficulty. [Read more] | Read more »
Victorious Knight (Games)
Victorious Knight 1.3 Device: iOS Universal Category: Games Price: $1.99, Version: 1.3 (iTunes) Description: New challenges awaits you! Experience fresh RPG experience with a unique combat mechanic, packed with high quality 3D... | Read more »
Agent Gumball - Roguelike Spy Game (Gam...
Agent Gumball - Roguelike Spy Game 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Someone’s been spying on Gumball. What the what?! Two can play at that game! GO UNDERCOVERSneak past enemy... | Read more »
Runaway Toad (Games)
Runaway Toad 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: It ain’t easy bein’ green! Tap, hold, and swipe to help Toad hop to safety in this gorgeous new action game from the creators of... | Read more »
PsyCard (Games)
PsyCard 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: From the makers och Card City Nights, Progress To 100 and Ittle Dew PSYCARD is a minesweeper-like game set in a cozy cyberpunk... | Read more »
Sago Mini Robot Party (Education)
Sago Mini Robot Party 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: -- Children's Technology Review Editor's Choice -- | Read more »
Egz – The Origin of the Universe (Games...
Egz – The Origin of the Universe 1.0.2 Device: iOS Universal Category: Games Price: $3.99, Version: 1.0.2 (iTunes) Description: ►►► Special offer until 2nd may : get the game at 2.99€ instead of 3.99€ ! ◄◄◄ Egz is a mesmerizing mix... | Read more »

Price Scanner via

Mac minis on sale for up to $100 off MSRP
B&H Photo has Mac minis on sale for up to $100 off MSRP including free shipping plus NY sales tax only: - 1.4GHz Mac mini: $449 $50 off MSRP - 2.6GHz Mac mini: $649 $50 off MSRP - 2.8GHz Mac mini... Read more
13-inch Retina MacBook Pros on sale for up to...
B&H Photo has 13″ Retina MacBook Pros on sale for $130-$200 off MSRP. Shipping is free, and B&H charges NY tax only: - 13″ 2.7GHz/128GB Retina MacBook Pro: $1169 $130 off MSRP - 13″ 2.7GHz/... Read more
Apple price trackers, updated continuously
Scan our Apple Price Trackers for the latest information on sales, bundles, and availability on systems from Apple’s authorized internet/catalog resellers. We update the trackers continuously: - 15″... Read more
SanDisk Half-Terabyte SSD Optimized for Every...
SanDisk Corporation has announced the SanDisk Z410 SSD, a cost-competitive, half-terabyte solid state drive (SSD) that enables manufacturers to design for a broad range of desktop PCs and laptops.... Read more
Churchill Downs Racetrack Selects VenueNext t...
Churchill Downs Racetrack has announced an agreement with VenueNext to implement its technology platform for the start of Churchill Downs 2016 Spring Meet, which includes the 142nd running of the... Read more
Record 700 Million Pounds of CE Recycled in 2...
The Consumer Technology Association (CTA) reports that a record-setting 700 million pounds of consumer electronics (CE) have been recycled under the eCycling Leadership Initiative (ELI). According to... Read more
Price drops on clearance 12-inch Retina MacBo...
B&H Photo has dropped prices on leftover 2015 12″ Retina MacBooks with models now available starting at $999. Shipping is free, and B&H charges NY tax only: - 12″ 1.1GHz Gray Retina MacBook... Read more
15-inch Retina MacBook Pros available for $20...
B&H Photo has 15″ Retina MacBook Pros on sale for up to $210 off MSRP. Shipping is free, and B&H charges NY tax only: - 15″ 2.2GHz Retina MacBook Pro: $1799 $200 off MSRP - 15″ 2.5GHz Retina... Read more
Target offers Apple Watch Sport for $50 off M...
Target has Apple Watch Sports on sale for $50 off MSRP for a limited time. Choose free shipping or free local store pickup (if available). Sale prices for online orders only, in-store prices may vary... Read more
Apple restocks Certified Refurbished Mac mini...
Apple has restocked Certified Refurbished 2014 Mac minis, with models available starting at $419. Apple’s one-year warranty is included with each mini, and shipping is free: - 1.4GHz Mac mini: $419 $... Read more

Jobs Board

*Apple* Retail - Multiple Positions - Apple,...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Restaurant Manager (Neighborhood Captain) - A...
…in every aspect of daily operation. WHY YOU'LL LIKE IT: You'll be the Big Apple . You'll solve problems. You'll get to show your ability to handle the stress and Read more
Restaurant Manager (Neighborhood Captain) - A...
…in every aspect of daily operation. WHY YOU'LL LIKE IT: You'll be the Big Apple . You'll solve problems. You'll get to show your ability to handle the stress and Read more
Simply Mac *Apple* Specialist- Service Repa...
Simply Mac is the largest premier retailer of Apple products in the nation. In order to support our growing customer base, we are currently looking for a driven Read more
*Apple* Retail - Multiple Positions - Apple,...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.