home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / MacStarter Pascal 1.0 / applicationProcs.p < prev    next >
Encoding:
Text File  |  1993-12-11  |  16.5 KB  |  359 lines  |  [TEXT/PJMM]

  1. unit applicationProcs;
  2.  
  3. { This is the unit that you must modify to create a new application.  This unit }
  4. { uses object definitions from other units, and it is in turn used by the main }
  5. { program, StandardMain.  However, you don't have to make any changes in the }
  6. { other units or in the main program. }
  7.  
  8. { This unit works with StandardMain.p and xWindow.p.  It also requires the }
  9. { presence of certain resources (those in generc.rsrc).  There are many other units }
  10. { in the project as distributed.  You can add those you need to the uses clause below. }
  11.  
  12. { A version of this file with no comments is "noCommentsProcs.p".   Several }
  13. { applicationProcs files for sample programs are included. }
  14.  
  15. interface
  16.  
  17. uses
  18.     xWindow, { This unit provides the basic class, xWindow, which defines the common }
  19.                   { behaviour of most Macintosh windows. }
  20.     xControlDecoration, { provides "controls" such as buttons and scroll bars, as }
  21.                   { xWindowDecorations }
  22.     xInputDecoration; { provides "input boxes" where the user can type strings, }
  23.                   { integerts, and real numbers, as xWindowDecorations; the units }
  24.                   { xControlDecoration and xInputDecoration provide the most commonly }
  25.                   { used decoration classes. }
  26.                   {    To use any of the other units that define subclasses of xWindow and }
  27.                   { of xWindowDecoration, you must add them here.  (If one of the units uses }
  28.                   { definitions from other units, the one that USES the definitions must follow }
  29.                   { the one that contains the definition.) }
  30.  
  31. const
  32.  
  33.     maxSleepTime = 5;   { When the main program has no events to process, it "goes to }
  34.           { sleep" and yields processing time to other processes that may be running.  }
  35.           { However, this program still wants to process "idle" events.  The constant }
  36.           { maxSleepTime specifies the longest time this program is willing to sleep }
  37.           { between idle events.  The time is specified in ticks (60 ticks = 1 second), so }
  38.           { a value of 5 corresponds to about 12 idle events per second, which is OK for }
  39.           { most purposes.  However, if you are using idle events to drive a processing }
  40.           { task (such as drawing successive pieces of a graph on each idle event), you }
  41.           { can change this value, setting it as low as 0 if you like, in which case the }
  42.           { Mac will give as little time as possible to other processes. }
  43.  
  44.  
  45.   { The constants defined below are used by the main program to set up the "About }
  46.   { Box" that is displayed when the user chooses "About . . .  " from the apple menu. }
  47.   { Change them as appropriate for the program you are writing. }
  48.  
  49.   { IMPORTANT NOTE:  This is NOT a secure place to put author/copyright info, since }
  50.   { it uses a resource that can be changed by anyone, even in a compiled program.}
  51.  
  52.     ApplicationShortName = 'Generic Application';
  53.          { This is the name of the program displayed in the first line of the Apple }
  54.          { Menu.  In this example, "About Generic Application..." will appear there. }
  55.  
  56.     ApplicationLongName = 'Generic Macintosh Application , version 0.9 ';
  57.         { Provides a fuller description of the program that will be displayed at the }
  58.         { top of the "About Box". }
  59.  
  60.     AuthorName = 'David Eck';
  61.     AuthorAddress1 = 'Hobart and William Smith Colleges';
  62.     AuthorAddress2 = 'Geneva, NY   14456';
  63.     AuthorAddress3 = '(Email Address:   eck@hws.BITNET)';
  64.        { These four items are displayed on the next four lines of the About Box, under }
  65.        { the heading "Created By:"  You can, of course, leave some of these empty, or }
  66.        { use some of them for information other than an address. }
  67.  
  68.     CommentLine = 'This is a shell program that you can use as a basis for writing Macintosh applications.';
  69.        { This line is simply displayed at the bottom of the about box. }
  70.  
  71.  
  72.  
  73. var
  74.     editMenu: MenuHandle;   { These two variables provide references to the File and }
  75.     fileMenu: MenuHandle;    { Edit menus.  The main program requires that they be here. }
  76.  
  77.  
  78. { The following is a list of procedures called by the main program.  You should not}
  79. { remove or change the name or parameter list for any of these procedures.  If your }
  80. { program does not require one of these procedures, just leave its definition blank. }
  81.  
  82. procedure InitializeApplication;
  83.       { Called when the program is first starting up; use this procedure to initialize }
  84.       { any global variables, open any windows that you want to appear automatically }
  85.       { on the screen at startup, etc.  }
  86.  
  87. procedure CleanUpApplication;
  88.       { This is called just before the program ends, to give you a final chance to clean }
  89.       { up.  Most programs will have no use for this.  There is no way to abort program }
  90.       { termination from this procedure. }
  91.  
  92. procedure DoEditMenu (itemNum: integer);
  93.       { The user has chosen the specified item number from the Edit menu.  This }
  94.       { procedure should carry out that command.  (Items in menus are numbered }
  95.       { from the top down, starting with 1.  Separating lines in the menu are numbered}
  96.       { even though they are not really commands.) }
  97.  
  98. procedure DoFileMenu (itemNum: integer;
  99.                             var done: boolean);
  100.       { Perform command number "itemNum" from the File Menu; if the user has chosen }
  101.       { the Quit command, then the parameter "done" should be set to TRUE, unless }
  102.       { the command is canceled in one way or another (for example, if you put up a }
  103.       { dialog box that says "Do you really want to quit?" and the user answers No). }
  104.       {    In this sample program, the only commands in the File menu are New (command }
  105.       { #1),  Close (#2)  and Quit (#4). }
  106.  
  107. procedure DoOtherMenu (menuNum, itemNum: integer);
  108.       { This procedure is here just in case you add more menus to the menu bar.  The }
  109.       { user has selected item number "itemNum" from menu number "menuNum", }
  110.       { and this procedure should carry out that command.   Note that the menu number }
  111.       { used here is the menu ID (or resource number), not the position of the menu in }
  112.       { the menu bar. }
  113.  
  114. procedure UpdateMenus;
  115.       { This procedure is called just BEFORE the user makes a selection from the }
  116.       { menu bar, after the user has clicked in the menu bar, but before any menu is }
  117.       { displayed.  Use this procedure to enable and disable items in the menu, to set }
  118.       { the names of items with changeable names, to check and uncheck items, etc. }
  119.  
  120. procedure ApplicationIdle;
  121.       { This procedure is called periodically,  unless the computer is otherwise occupied }
  122.       { (for example, tied up in a lengthy computation).   See the description of the constant }
  123.       { maxSleepTime above. }
  124.  
  125.  
  126.  
  127. implementation
  128.  
  129.  
  130. type
  131.  
  132. { To write your own application, you will probably declare one or more new window }
  133. { classes as subclasses of xWindow or of one of its existing subclasses. }
  134. { Here, a class myWin is defined which overrides several of the procedures from the }
  135. { parent class.  These procedures are the ones that would most commonly have to }
  136. { be overriden to create a new class.  Other procedures that you might commonly want }
  137. { to override include:  idle, doClose, doHScroll, doVScroll, and adjustToNewSize. }
  138. {  See the defintion of the class xWindow and its sub-classes for more information. }
  139. {    In a more realistic example, you would very probably have one or more instance }
  140. { variables in your window class to hold any data pertinent to a particular window, }
  141. { (for example, an array representing the contents of a checkers board).   You might }
  142. { to move the window type definition to another UNIT. }
  143.  
  144.     myWin = object(xWindow)
  145.             procedure setDefaults;
  146.             override;
  147.             procedure openInRect (title: string;
  148.                                         left, top, right, bottom: integer);
  149.             override;
  150.             procedure doRedraw (badRect: Rect);
  151.             override;
  152.             procedure doContentClick (localPt: Point;
  153.                                         modifiers: longint);
  154.             override;
  155.             procedure doKey (ch: char;
  156.                                         modifiers: longint);
  157.             override;
  158.         end;
  159.  
  160. { Next, give the definitions of these overriden procedures. }
  161.  
  162. procedure myWin.setDefaults;
  163. { This procedure sets up the defalut appearance of the window, and initializes certain }
  164. { instance variables.  It is called BEFORE the window is opened, by openInRect. }
  165. { It is ABSOLUTELY ESSENTIAL that your version of setDefaults call the inherited }
  166. { version to set up the values of certain essential parameters. }
  167.     begin
  168.         inherited setDefaults;
  169.    { make changes to default settings here;  if you have instance variables that need to be }
  170.    { initialized BEFORE the Macintosh window, theWindow, is opened, this is the place to }
  171.    { do it.  As an example of changing default behavior, you could say : }
  172.    {                 features := features - [hasHScroll];                              }
  173.    { to get a window with no horizontal scroll bar. }
  174.     end;
  175.  
  176. procedure myWin.openInRect (title: string;
  177.                                 left, top, right, bottom: integer);
  178. { The purpose of this procedure is to open a window, initialize its data structures, etc. }
  179. { The title is displayed in the title bar of the window; the position and size of the }
  180. { window are determined by the remaining parameters.  (top,left) is the point at the }
  181. { top, left corner of the inside part of the window, and (right,bottom) is the bottom }
  182. { right corner.   Ordinarily, this procedure is called by procedure Open, instead of }
  183. { being used directly; in that case, the rectangle to be used as the window's portRect }
  184. { is computed automatically. }
  185.     begin
  186.         inherited openInRect(title, left, top, right, bottom);
  187.            { The method inherited from xWindow calls SetDefaults, then opens a window. }
  188.            { You can do most of the initialization for your window here, AFTER the call}
  189.            { to inherited openInRect.  }
  190.     end;
  191.  
  192.  
  193. procedure myWin.doRedraw (badRect: Rect);
  194. { This procedure is called when all or part of the stuff in the window needs to be }
  195. { redrawn.  The parameter badRect specifies the region of the window that needs to }
  196. { be redrawn; the usual thing to do is to just ignore it and to redraw the entire }
  197. { window. }
  198. {     This procedure is called, for example, when another window that has been }
  199. { covering this one is moved or closed.  By default, it is also called when the user }
  200. { changes the position of the scroll bar.  In both cases, the widow is erased before }
  201. { the procedure is called.   (Erasing and redrawing the window may not be the effect }
  202. { you want when the window is scrolled; to change this default behaviour, you have }
  203. { to override the procedures doHScroll and doVScroll.) }
  204. {   The default method in xWindow for doRedraw redraws any }
  205. {  xWindowDecorations in the Window.  Some subclasses of xWindow have }
  206. { other responses to the message doContentClick.  Unless you are sure you }
  207. { have no reason to do so, you sould call INHERITED doContentClick in addition }
  208. { to any other redrawing you do for your window. }
  209.     begin
  210.  
  211.         inherited doRedraw(badRect);  { ordinarily, you should call this somewhere in your procedure  }
  212.  
  213.     end;
  214.  
  215.  
  216. procedure myWin.doContentClick (localPt: Point;
  217.                                 modifiers: longint);
  218.   { This procedure is called when the user clicks on the "content region" of the }
  219.   { window.  (If the user clicks on the scroll bar, title bar, or grow box, that is }
  220.   { handled automatically; this procedure is not called in such cases.)   The point }
  221.   { where the user clicked is given by the parameter localPt.  (The point is in "local }
  222.   { coordinates," i.e. is specified relative to the top left corner of the window.) }
  223.   { The parameter modifiers can be used to check whether the shift, command or }
  224.   { option keys were held down when the mouse was clicked.  This sample program }
  225.   { does not respond to clicks. }
  226.   {   The default method in xWindow for doContentClick sends the doClick to any appropriate }
  227.   {  xWindowDecoration in the Window.  Some subclasses of xWindow have }
  228.   { other responses to the message doContentClick.  Unless you are sure you }
  229.   { have no decorations in your window, you sould call INHERITED doContentClick }
  230.   { whenever your procedure does not handle the click itself. }
  231.     begin
  232.         inherited doContentClick(localPt, modifiers);
  233.     end;
  234.  
  235.  
  236. procedure myWin.doKey (ch: char;
  237.                                 modifiers: longint);
  238.   { This procedure is called when the user presses a key, provided this is the front }
  239.   { (or "active") window. }
  240.   {     The default method in xWindow for doKey sends a doKey message to any appropriate }
  241.   {  xWindowDecoration in the Window.  Some subclasses of xWindow have }
  242.   { other responses to the message doKey.  Unless you want some other response }
  243.   { to a key, you sould call INHERITED doKey in your doKey procedure.  }
  244.     begin
  245.         inherited doKey(ch, modifiers)
  246.     end;
  247.  
  248.  
  249. procedure OpenAWindow;
  250. { A very common procedure is one for opening a new window.  In this sample }
  251. { application, this procedure is called by InitializeApplication to open an initial }
  252. { window.  It is also called when the user chooses the command New from the File }
  253. { menu. }
  254.     var
  255.         Win: myWin;
  256.     begin
  257.         new(Win);
  258.         Win.open('Sample Window');
  259.     end;
  260.  
  261.  
  262. { *** Definitions of procedures that are called in the main program.  *** }
  263.  
  264.  
  265. procedure InitializeApplication;
  266.       { Called when the program is first starting up; use this procedure to initialize }
  267.       { any global variables, open any windows that you want to appear automatically }
  268.       { on the screen at startup, etc.  }
  269.     begin
  270.         OpenAWindow;
  271.     end;
  272.  
  273. procedure CleanUpApplication;
  274.       { This is called just before the program ends, to give you a final chance to clean }
  275.       { up.  Most programs will have no use for this.  There is no way to abort program }
  276.       { termination from this procedure. }
  277.     begin
  278.     end;
  279.  
  280. procedure UpdateMenus;
  281.       { This procedure is called just BEFORE the user makes a selection from the }
  282.       { menu bar, after the user has clicked in the menu bar, but before any menu is }
  283.       { displayed.  Use this procedure to enable and disable items in the menu, to set }
  284.       { the names of items with changeable names, to check and uncheck items, etc. }
  285.     var
  286.         win: WindowPtr;
  287.         xWin: xWindow;
  288.     begin
  289.         win := FrontWindow;  { this is the currently active window, or nil if there is none }
  290.         if Window2xWindow(win, xWin) then
  291.             EnableItem(fileMenu, 2)
  292.         else
  293.             DisableItem(fileMenu, 2); { disable "Close Window" command if front window is not an xWindow. }
  294.  
  295.    { NOTE that this sample procedure assumes that the Close Window command is item #2 in the }
  296.    { file menu.  If you make changes to the menus, you will have to chance this procedure as well }
  297.    { as procedure DoFileMenu. }
  298.  
  299.     end;
  300.  
  301.  
  302. procedure CloseFrontWindow;
  303.     var
  304.         X: xWindow;
  305.     begin
  306.         if window2xWindow(FrontWindow, X) then
  307.             X.doClose
  308.     end;
  309.  
  310. procedure DoFileMenu (itemNum: integer;
  311.                                 var done: boolean);
  312.       { Perform command number "itemNum" from the File Menu; if the user has chosen }
  313.       { the Quit command, then the parameter "done" should be set to TRUE, unless }
  314.       { the command is canceled in one way or another (for example, if you put up a }
  315.       { dialog box that says "Do you really want to quit?" and the user answers No). }
  316.       {    In this sample program, the only commands in the File menu are New (command }
  317.       { #1),  Close (#2)  and Quit (#4). }
  318.     begin
  319.         case itemNum of
  320.             1:    { New command }
  321.                 OpenAWindow;
  322.             2:    { Close command }
  323.                 CloseFrontWindow;
  324.             4:    { Quit command }
  325.                 done := true;
  326.         end;
  327.     end;
  328.  
  329. procedure DoEditMenu (itemNum: integer);
  330.       { The user has chosen the specified item number from the Edit menu.  This }
  331.       { procedure should carry out that command.  The sample program does not }
  332.       { use these commands. }
  333.     begin
  334.     end;
  335.  
  336. procedure DoOtherMenu (menuNum, itemNum: integer);
  337.       { This procedure is here just in case you add more menus to the menu bar.  The }
  338.       { user has selected item number "itemNum" from menu number "menuNum", }
  339.       { and this procedure should carry out that command.   Note that the menu number }
  340.       { used here is the menu ID (or resource number), not the position of the menu in }
  341.       { the menu bar.   This sample program has no extra menus. }
  342.     begin
  343.     end;
  344.  
  345. procedure ApplicationIdle;
  346.       { This procedure is called periodically (as long as this program is the current }
  347.       { application) about 12 times a second, using the default value of the constant,}
  348.       { maxSleepTime.  If the computer is otherwise occupied (for example, tied up in }
  349.       { a lengthy computation), then the idle procedure will not be called on schedule. }
  350.       { (This default procedure simply sends an idle message to the front window, }
  351.       { if any.) }
  352.     var
  353.         X: xWindow;
  354.     begin
  355.         if window2xWindow(FrontWindow, X) then
  356.             X.idle;
  357.     end;
  358.  
  359. end.