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

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
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 »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.