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.

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.

Classy_Input.R

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.

Classy_Input.Pas

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;
begin 
 GetString:=GetResource('STR ',ID);
end
 

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.

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

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

type DLOG
box,12345
;;  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
items,12345
4                                               ;; four items 

Button
4 120 24 180                    ;;  local coordinates !!
OK;;

Button
4 188 24 248
Cancel;;

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 
prompt,12345
Type a number

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


Classy_Input.pas
program Classy_Input_test;{ by Alan Wootton 5/85 }
 type
  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) :
                                          str255;
  var
   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. }
 begin
  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;
 end;


 function Prompt_for_Number (sampN : longint) :
                                                                     
      longint;
  var
   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) }
 begin
  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;
 end;


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)
 else
  begin
   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);
  end;
 inlineP($A99A, rid);{ _CloseResFile}
end.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.