home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-12-11 | 16.5 KB | 359 lines | [TEXT/PJMM] |
- unit applicationProcs;
-
- { This is the unit that you must modify to create a new application. This unit }
- { uses object definitions from other units, and it is in turn used by the main }
- { program, StandardMain. However, you don't have to make any changes in the }
- { other units or in the main program. }
-
- { This unit works with StandardMain.p and xWindow.p. It also requires the }
- { presence of certain resources (those in generc.rsrc). There are many other units }
- { in the project as distributed. You can add those you need to the uses clause below. }
-
- { A version of this file with no comments is "noCommentsProcs.p". Several }
- { applicationProcs files for sample programs are included. }
-
- interface
-
- uses
- xWindow, { This unit provides the basic class, xWindow, which defines the common }
- { behaviour of most Macintosh windows. }
- xControlDecoration, { provides "controls" such as buttons and scroll bars, as }
- { xWindowDecorations }
- xInputDecoration; { provides "input boxes" where the user can type strings, }
- { integerts, and real numbers, as xWindowDecorations; the units }
- { xControlDecoration and xInputDecoration provide the most commonly }
- { used decoration classes. }
- { To use any of the other units that define subclasses of xWindow and }
- { of xWindowDecoration, you must add them here. (If one of the units uses }
- { definitions from other units, the one that USES the definitions must follow }
- { the one that contains the definition.) }
-
- const
-
- maxSleepTime = 5; { When the main program has no events to process, it "goes to }
- { sleep" and yields processing time to other processes that may be running. }
- { However, this program still wants to process "idle" events. The constant }
- { maxSleepTime specifies the longest time this program is willing to sleep }
- { between idle events. The time is specified in ticks (60 ticks = 1 second), so }
- { a value of 5 corresponds to about 12 idle events per second, which is OK for }
- { most purposes. However, if you are using idle events to drive a processing }
- { task (such as drawing successive pieces of a graph on each idle event), you }
- { can change this value, setting it as low as 0 if you like, in which case the }
- { Mac will give as little time as possible to other processes. }
-
-
- { The constants defined below are used by the main program to set up the "About }
- { Box" that is displayed when the user chooses "About . . . " from the apple menu. }
- { Change them as appropriate for the program you are writing. }
-
- { IMPORTANT NOTE: This is NOT a secure place to put author/copyright info, since }
- { it uses a resource that can be changed by anyone, even in a compiled program.}
-
- ApplicationShortName = 'Generic Application';
- { This is the name of the program displayed in the first line of the Apple }
- { Menu. In this example, "About Generic Application..." will appear there. }
-
- ApplicationLongName = 'Generic Macintosh Application , version 0.9 ';
- { Provides a fuller description of the program that will be displayed at the }
- { top of the "About Box". }
-
- AuthorName = 'David Eck';
- AuthorAddress1 = 'Hobart and William Smith Colleges';
- AuthorAddress2 = 'Geneva, NY 14456';
- AuthorAddress3 = '(Email Address: eck@hws.BITNET)';
- { These four items are displayed on the next four lines of the About Box, under }
- { the heading "Created By:" You can, of course, leave some of these empty, or }
- { use some of them for information other than an address. }
-
- CommentLine = 'This is a shell program that you can use as a basis for writing Macintosh applications.';
- { This line is simply displayed at the bottom of the about box. }
-
-
-
- var
- editMenu: MenuHandle; { These two variables provide references to the File and }
- fileMenu: MenuHandle; { Edit menus. The main program requires that they be here. }
-
-
- { The following is a list of procedures called by the main program. You should not}
- { remove or change the name or parameter list for any of these procedures. If your }
- { program does not require one of these procedures, just leave its definition blank. }
-
- procedure InitializeApplication;
- { Called when the program is first starting up; use this procedure to initialize }
- { any global variables, open any windows that you want to appear automatically }
- { on the screen at startup, etc. }
-
- procedure CleanUpApplication;
- { This is called just before the program ends, to give you a final chance to clean }
- { up. Most programs will have no use for this. There is no way to abort program }
- { termination from this procedure. }
-
- procedure DoEditMenu (itemNum: integer);
- { The user has chosen the specified item number from the Edit menu. This }
- { procedure should carry out that command. (Items in menus are numbered }
- { from the top down, starting with 1. Separating lines in the menu are numbered}
- { even though they are not really commands.) }
-
- procedure DoFileMenu (itemNum: integer;
- var done: boolean);
- { Perform command number "itemNum" from the File Menu; if the user has chosen }
- { the Quit command, then the parameter "done" should be set to TRUE, unless }
- { the command is canceled in one way or another (for example, if you put up a }
- { dialog box that says "Do you really want to quit?" and the user answers No). }
- { In this sample program, the only commands in the File menu are New (command }
- { #1), Close (#2) and Quit (#4). }
-
- procedure DoOtherMenu (menuNum, itemNum: integer);
- { This procedure is here just in case you add more menus to the menu bar. The }
- { user has selected item number "itemNum" from menu number "menuNum", }
- { and this procedure should carry out that command. Note that the menu number }
- { used here is the menu ID (or resource number), not the position of the menu in }
- { the menu bar. }
-
- procedure UpdateMenus;
- { This procedure is called just BEFORE the user makes a selection from the }
- { menu bar, after the user has clicked in the menu bar, but before any menu is }
- { displayed. Use this procedure to enable and disable items in the menu, to set }
- { the names of items with changeable names, to check and uncheck items, etc. }
-
- procedure ApplicationIdle;
- { This procedure is called periodically, unless the computer is otherwise occupied }
- { (for example, tied up in a lengthy computation). See the description of the constant }
- { maxSleepTime above. }
-
-
-
- implementation
-
-
- type
-
- { To write your own application, you will probably declare one or more new window }
- { classes as subclasses of xWindow or of one of its existing subclasses. }
- { Here, a class myWin is defined which overrides several of the procedures from the }
- { parent class. These procedures are the ones that would most commonly have to }
- { be overriden to create a new class. Other procedures that you might commonly want }
- { to override include: idle, doClose, doHScroll, doVScroll, and adjustToNewSize. }
- { See the defintion of the class xWindow and its sub-classes for more information. }
- { In a more realistic example, you would very probably have one or more instance }
- { variables in your window class to hold any data pertinent to a particular window, }
- { (for example, an array representing the contents of a checkers board). You might }
- { to move the window type definition to another UNIT. }
-
- myWin = object(xWindow)
- procedure setDefaults;
- override;
- procedure openInRect (title: string;
- left, top, right, bottom: integer);
- override;
- procedure doRedraw (badRect: Rect);
- override;
- procedure doContentClick (localPt: Point;
- modifiers: longint);
- override;
- procedure doKey (ch: char;
- modifiers: longint);
- override;
- end;
-
- { Next, give the definitions of these overriden procedures. }
-
- procedure myWin.setDefaults;
- { This procedure sets up the defalut appearance of the window, and initializes certain }
- { instance variables. It is called BEFORE the window is opened, by openInRect. }
- { It is ABSOLUTELY ESSENTIAL that your version of setDefaults call the inherited }
- { version to set up the values of certain essential parameters. }
- begin
- inherited setDefaults;
- { make changes to default settings here; if you have instance variables that need to be }
- { initialized BEFORE the Macintosh window, theWindow, is opened, this is the place to }
- { do it. As an example of changing default behavior, you could say : }
- { features := features - [hasHScroll]; }
- { to get a window with no horizontal scroll bar. }
- end;
-
- procedure myWin.openInRect (title: string;
- left, top, right, bottom: integer);
- { The purpose of this procedure is to open a window, initialize its data structures, etc. }
- { The title is displayed in the title bar of the window; the position and size of the }
- { window are determined by the remaining parameters. (top,left) is the point at the }
- { top, left corner of the inside part of the window, and (right,bottom) is the bottom }
- { right corner. Ordinarily, this procedure is called by procedure Open, instead of }
- { being used directly; in that case, the rectangle to be used as the window's portRect }
- { is computed automatically. }
- begin
- inherited openInRect(title, left, top, right, bottom);
- { The method inherited from xWindow calls SetDefaults, then opens a window. }
- { You can do most of the initialization for your window here, AFTER the call}
- { to inherited openInRect. }
- end;
-
-
- procedure myWin.doRedraw (badRect: Rect);
- { This procedure is called when all or part of the stuff in the window needs to be }
- { redrawn. The parameter badRect specifies the region of the window that needs to }
- { be redrawn; the usual thing to do is to just ignore it and to redraw the entire }
- { window. }
- { This procedure is called, for example, when another window that has been }
- { covering this one is moved or closed. By default, it is also called when the user }
- { changes the position of the scroll bar. In both cases, the widow is erased before }
- { the procedure is called. (Erasing and redrawing the window may not be the effect }
- { you want when the window is scrolled; to change this default behaviour, you have }
- { to override the procedures doHScroll and doVScroll.) }
- { The default method in xWindow for doRedraw redraws any }
- { xWindowDecorations in the Window. Some subclasses of xWindow have }
- { other responses to the message doContentClick. Unless you are sure you }
- { have no reason to do so, you sould call INHERITED doContentClick in addition }
- { to any other redrawing you do for your window. }
- begin
-
- inherited doRedraw(badRect); { ordinarily, you should call this somewhere in your procedure }
-
- end;
-
-
- procedure myWin.doContentClick (localPt: Point;
- modifiers: longint);
- { This procedure is called when the user clicks on the "content region" of the }
- { window. (If the user clicks on the scroll bar, title bar, or grow box, that is }
- { handled automatically; this procedure is not called in such cases.) The point }
- { where the user clicked is given by the parameter localPt. (The point is in "local }
- { coordinates," i.e. is specified relative to the top left corner of the window.) }
- { The parameter modifiers can be used to check whether the shift, command or }
- { option keys were held down when the mouse was clicked. This sample program }
- { does not respond to clicks. }
- { The default method in xWindow for doContentClick sends the doClick to any appropriate }
- { xWindowDecoration in the Window. Some subclasses of xWindow have }
- { other responses to the message doContentClick. Unless you are sure you }
- { have no decorations in your window, you sould call INHERITED doContentClick }
- { whenever your procedure does not handle the click itself. }
- begin
- inherited doContentClick(localPt, modifiers);
- end;
-
-
- procedure myWin.doKey (ch: char;
- modifiers: longint);
- { This procedure is called when the user presses a key, provided this is the front }
- { (or "active") window. }
- { The default method in xWindow for doKey sends a doKey message to any appropriate }
- { xWindowDecoration in the Window. Some subclasses of xWindow have }
- { other responses to the message doKey. Unless you want some other response }
- { to a key, you sould call INHERITED doKey in your doKey procedure. }
- begin
- inherited doKey(ch, modifiers)
- end;
-
-
- procedure OpenAWindow;
- { A very common procedure is one for opening a new window. In this sample }
- { application, this procedure is called by InitializeApplication to open an initial }
- { window. It is also called when the user chooses the command New from the File }
- { menu. }
- var
- Win: myWin;
- begin
- new(Win);
- Win.open('Sample Window');
- end;
-
-
- { *** Definitions of procedures that are called in the main program. *** }
-
-
- procedure InitializeApplication;
- { Called when the program is first starting up; use this procedure to initialize }
- { any global variables, open any windows that you want to appear automatically }
- { on the screen at startup, etc. }
- begin
- OpenAWindow;
- end;
-
- procedure CleanUpApplication;
- { This is called just before the program ends, to give you a final chance to clean }
- { up. Most programs will have no use for this. There is no way to abort program }
- { termination from this procedure. }
- begin
- end;
-
- procedure UpdateMenus;
- { This procedure is called just BEFORE the user makes a selection from the }
- { menu bar, after the user has clicked in the menu bar, but before any menu is }
- { displayed. Use this procedure to enable and disable items in the menu, to set }
- { the names of items with changeable names, to check and uncheck items, etc. }
- var
- win: WindowPtr;
- xWin: xWindow;
- begin
- win := FrontWindow; { this is the currently active window, or nil if there is none }
- if Window2xWindow(win, xWin) then
- EnableItem(fileMenu, 2)
- else
- DisableItem(fileMenu, 2); { disable "Close Window" command if front window is not an xWindow. }
-
- { NOTE that this sample procedure assumes that the Close Window command is item #2 in the }
- { file menu. If you make changes to the menus, you will have to chance this procedure as well }
- { as procedure DoFileMenu. }
-
- end;
-
-
- procedure CloseFrontWindow;
- var
- X: xWindow;
- begin
- if window2xWindow(FrontWindow, X) then
- X.doClose
- end;
-
- procedure DoFileMenu (itemNum: integer;
- var done: boolean);
- { Perform command number "itemNum" from the File Menu; if the user has chosen }
- { the Quit command, then the parameter "done" should be set to TRUE, unless }
- { the command is canceled in one way or another (for example, if you put up a }
- { dialog box that says "Do you really want to quit?" and the user answers No). }
- { In this sample program, the only commands in the File menu are New (command }
- { #1), Close (#2) and Quit (#4). }
- begin
- case itemNum of
- 1: { New command }
- OpenAWindow;
- 2: { Close command }
- CloseFrontWindow;
- 4: { Quit command }
- done := true;
- end;
- end;
-
- procedure DoEditMenu (itemNum: integer);
- { The user has chosen the specified item number from the Edit menu. This }
- { procedure should carry out that command. The sample program does not }
- { use these commands. }
- begin
- end;
-
- procedure DoOtherMenu (menuNum, itemNum: integer);
- { This procedure is here just in case you add more menus to the menu bar. The }
- { user has selected item number "itemNum" from menu number "menuNum", }
- { and this procedure should carry out that command. Note that the menu number }
- { used here is the menu ID (or resource number), not the position of the menu in }
- { the menu bar. This sample program has no extra menus. }
- begin
- end;
-
- procedure ApplicationIdle;
- { This procedure is called periodically (as long as this program is the current }
- { application) about 12 times a second, using the default value of the constant,}
- { maxSleepTime. If the computer is otherwise occupied (for example, tied up in }
- { a lengthy computation), then the idle procedure will not be called on schedule. }
- { (This default procedure simply sends an idle message to the front window, }
- { if any.) }
- var
- X: xWindow;
- begin
- if window2xWindow(FrontWindow, X) then
- X.idle;
- end;
-
- end.