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

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.