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

  1. { This file shows how you can write a simple application using MacStarter_Pascal. }
  2. { This file is  a modified version of noCommentProcs.p that implements a very simple }
  3. { "calculator".  The modifications are commented.  This example shows how to use }
  4. { some input box and control "windowDecorations", and how to use a window class }
  5. { other than xWindow itself as a base for your own window class. }
  6.  
  7. { To see what the example does, replace the applicationProcs.p file in the generic.π }
  8. { project file and add the file xTextWindow.p to the project BEFORE this file.  Then run it. }
  9.  
  10. unit applicationProcs;
  11.  
  12. interface
  13.  
  14. uses
  15.     xWindow, xControlDecoration, xInputDecoration,   { }
  16.     xTextWindow;  { This example is based on xTextWindow, rather than on }
  17.                            { xWindow directly, so I have to specify that the unit xTextWindow }
  18.                            { is to be used. }
  19.  
  20. const
  21.  
  22.     maxSleepTime = 5;
  23.     ApplicationShortName = 'MiniCalc';   { change the application name, long name and comment line. }
  24.     ApplicationLongName = 'A simple calcuator pprogram';
  25.     AuthorName = 'David Eck';
  26.     AuthorAddress1 = 'Hobart and William Smith Colleges';
  27.     AuthorAddress2 = 'Geneva, NY   14456';
  28.     AuthorAddress3 = '(Email Address:   eck@hws.BITNET)';
  29.     CommentLine = 'This is a sample program created using the application shell MacStarter_pascal.';
  30.  
  31.  
  32.  
  33. var
  34.     editMenu: MenuHandle;
  35.     fileMenu: MenuHandle;
  36.  
  37.  
  38. procedure InitializeApplication;
  39. procedure CleanUpApplication;
  40. procedure DoEditMenu (itemNum: integer);
  41. procedure DoFileMenu (itemNum: integer;
  42.                             var done: boolean);
  43. procedure DoOtherMenu (menuNum, itemNum: integer);
  44. procedure UpdateMenus;
  45. procedure ApplicationIdle;
  46.  
  47.  
  48. implementation
  49.  
  50.  
  51. type
  52.  
  53.     myWin = object(xTextWindow)   { based on xTextWindow, not xWindow }
  54.  
  55.          { A window with two input boxes for numbers, a button to perform a calculation }
  56.          { with those numbers, a radiogroup specifying which of the operations + - * / }
  57.          { will be performed on those numbers, and a large text area that will be used to }
  58.          { display an ongoing "transcript" of the calculations performed. }
  59.  
  60.             data1, data2: xRealInput;   { number input boxes }
  61.             operator: xRadioGroup;      { radio group for setting operation }
  62.  
  63.             procedure setDefaults;
  64.             override;
  65.            { override to create space at the top of the text in the window, and to set a }
  66.            { larger minimum window size; also, locks the text, so user can't type in text }
  67.            { area. }
  68.  
  69.             procedure openInRect (title: string;
  70.                                         left, top, right, bottom: integer);
  71.             override;
  72.  
  73.             procedure doRedraw (badRect: Rect);
  74.             override;
  75.  
  76.      {I have no need to override the procedures doContentClick and doKey }
  77.      { for my window class, so I have removed their declarations here, and have removed }
  78.      { their definitions that occured later in the file noCommentsProcs. }
  79.  
  80.         end;   { definition of myWin }
  81.  
  82.  
  83.   { If you use a button in your window, you will probably create a new class as }
  84.   { a subclass of xButton or xDefaultButton, and override the HandleClick procedure }
  85.   { in that class to specify what the button does. }
  86.  
  87.     calcButton = object(xDefaultButton)
  88.          { The button type for use in myWin; it will perform the calculation and }
  89.          { append the results to the transcript when it is clicked on (and also, since }
  90.          { it is a default button, when the user presses Return or Enter. }
  91.             procedure HandleClick;
  92.             override;
  93.         end;
  94.  
  95. procedure calcButton.HandleClick;
  96.        { this procedure is called when the calculation button is presses; it will do the }
  97.        { calculation specified by the current setting of the radio group, and will }
  98.        { display the results in the window below. }
  99.     var
  100.         win: myWin;        { the window that contains the button }
  101.         x, y: extended;            { number from input boxes }
  102.         xStr, yStr: string;   { contents of input boxes as strings }
  103.         ans: extended;             { result of calculation }
  104.         ansStr: string;         { result of calculation, represented as a string }
  105.         op: char;                     { specified operation: + - * / }
  106.         err: boolean;              { checks if the contents of input boxes are legal }
  107.     begin
  108.       { "itsWindow" is an instance variable for the button object that tells which }
  109.       { xWindow its in; since the button is actually in a myWin, I typecast itsWindow }
  110.       { to type myWin to get access to its instance variables. }
  111.         win := myWin(itsWindow);
  112.         with win do begin
  113.                 data1.getNumber(x, err);   { get the first number, and check that there was no error }
  114.                 if err then
  115.                     EXIT(HandleClick);
  116.                 data2.getNumber(y, err);   { second number }
  117.                 if err then
  118.                     EXIT(HandleClick);
  119.                 case operator.selected of   { check which radio button is selected, and do operation }
  120.                     1:  begin
  121.                             ans := x + y;
  122.                             op := '+';
  123.                         end;
  124.                     2:  begin
  125.                             ans := x - y;
  126.                             op := '-'
  127.                         end;
  128.                     3:  begin
  129.                             ans := x * y;
  130.                             op := '*'
  131.                         end;
  132.                     4: 
  133.                         if y <> 0 then begin
  134.                                 ans := x / y;
  135.                                 op := '/'
  136.                             end
  137.                         else begin
  138.                                 TellUser('Error:  Division by zero is an illegal operation.');
  139.                                 EXIT(HandleClick);
  140.                             end;
  141.                 end;
  142.                 if ans = 0 then    { convert ans to a string for display }
  143.                     ansStr := '0'
  144.                 else if (abs(ans) < 5e-7) | (abs(ans) > 5e8) then
  145.                     ansStr := StringOf(ans : 20)
  146.                 else
  147.                     ansStr := StringOf(ans : 1 : 10);
  148.                 data1.GetContents(xStr);   { get contents of input boxes, as strings }
  149.                 data2.GetContents(yStr);
  150.                 appendString(StringOf(xStr, '  ', op, ' ', yStr, '  =  ', ansStr));  { add calculation to transcript. }
  151.                 appendCR;   { note that appendString and appendCR are procedure in class xTextWindow. }
  152.                 appendCR;
  153.             end;
  154.     end;
  155.  
  156. procedure myWin.setDefaults;
  157.     begin
  158.         inherited setDefaults;
  159.         locked := true;   { instance variable in class xTextWindow; user will not be able to edit text. }
  160.         vScrollTopOffset := 82;  { leave space at top of vertical scroll bar. }
  161.         topTextOffset := 82;   { leave space at top of the text (transcript) in the window }
  162.         SetMinDragWidth(340);   { set minumum allowed size during window resizing }
  163.         SetMinDragHeight(150);
  164.     end;
  165.  
  166. procedure myWin.openInRect (title: string;
  167.                                 left, top, right, bottom: integer);
  168.        { open a window and add all the decorations }
  169.     var
  170.         bttn: calcButton;   { the button that will perform the calculations }
  171.     begin
  172.         inherited openInRect(title, left, top, right, bottom);  { open a standard xTextWindow }
  173.         new(bttn);     { install calculation button }
  174.         bttn.setUp(self, 'Calculate', 234, 24, 72, 34);
  175.         new(operator);   { install radio group with one radio button for each possible operation }
  176.         operator.setUp(self, 'Add\Subtract\Multiply\Divide', 10, 10);
  177.         new(data2);   { install a real-number input box }
  178.         data2.setUp(self, 100, 44, 110, 24);
  179.         new(data1);   { install other input box }
  180.         data1.SetUp(self, 100, 10, 110, 24);
  181.     end;
  182.  
  183. procedure myWin.doRedraw (badRect: Rect);
  184.     begin
  185.         inherited doRedraw(badRect);  { draws text and decorations (button, radiogroup, and input boxes) }
  186.         PenSize(1, 2);   { Draw a line across the screen, sepatatind decorations from text }
  187.         MoveTo(0, 80);
  188.         LineTo(theWindow^.portRect.right, 80);
  189.         PenSize(1, 1);
  190.     end;
  191.  
  192.  
  193. procedure OpenAWindow;
  194.  { open a window; called by InitializeApplication and in response to New command }
  195.     var
  196.         Win: myWin;
  197.     begin
  198.         new(Win);
  199.         Win.open('Calculation Window, with Transcript');
  200.     end;
  201.  
  202. {--------------------------------------------------------------------}
  203.  
  204. procedure InitializeApplication;
  205.     begin
  206.         OpenAWindow;
  207.     end;
  208.  
  209. procedure CleanUpApplication;
  210.     begin
  211.     end;
  212.  
  213. procedure UpdateMenus;
  214.     var
  215.         win: WindowPtr;
  216.         xWin: xWindow;
  217.     begin
  218.         win := FrontWindow;
  219.         if Window2xWindow(win, xWin) then
  220.             EnableItem(fileMenu, 2)
  221.         else
  222.             DisableItem(fileMenu, 2);
  223.     end;
  224.  
  225.  
  226. procedure CloseFrontWindow;
  227.     var
  228.         X: xWindow;
  229.     begin
  230.         if window2xWindow(FrontWindow, X) then
  231.             X.doClose
  232.     end;
  233.  
  234. procedure DoFileMenu (itemNum: integer;
  235.                                 var done: boolean);
  236.     begin
  237.         case itemNum of
  238.             1:    { New command }
  239.                 OpenAWindow;
  240.             2:    { Close command }
  241.                 CloseFrontWindow;
  242.             4:    { Quit command }
  243.                 done := true;
  244.         end;
  245.     end;
  246.  
  247. procedure DoEditMenu (itemNum: integer);
  248.     begin
  249.     end;
  250.  
  251. procedure DoOtherMenu (menuNum, itemNum: integer);
  252.     begin
  253.     end;
  254.  
  255. procedure ApplicationIdle;
  256.     var
  257.         X: xWindow;
  258.     begin
  259.         if window2xWindow(FrontWindow, X) then
  260.             X.idle;
  261.     end;
  262.  
  263. end.