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

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more

Jobs Board

Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, 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
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
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.