home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / MacStarter Pascal 1.0 / xWindows definition files / StandardMain.p < prev    next >
Encoding:
Text File  |  1992-06-17  |  6.0 KB  |  169 lines  |  [TEXT/PJMM]

  1. program Main;
  2.  
  3. uses
  4.     xWindow, applicationProcs;
  5.  
  6. var
  7.     appleMenu: MenuHandle;
  8.  
  9. procedure AboutBox;
  10.     var
  11.         junk: integer;
  12.     begin
  13.         ParamText(ApplicationLongName, AuthorName, StringOf(AuthorAddress1, chr(13), AuthorAddress2, chr(13), AuthorAddress3), CommentLine);
  14.         junk := Alert(128, nil);
  15.     end;
  16.  
  17. procedure DoAppleMenu (itemNum: integer);
  18. { Handles a command from the apple menu, either by displaying the about box for the }
  19. { application or loading a desk acessory; it is not necessary to change this procedure }
  20.     var
  21.         DAName: str255;    { name of desk accessory to be loaded (from the apple menu) }
  22.         junk: integer;
  23.     begin
  24.         if itemNum = 1 then
  25.             AboutBox
  26.         else begin
  27.                 GetItem(appleMenu, itemNum, DAName);
  28.                 junk := OpenDeskAcc(DAName);
  29.             end;
  30.     end;
  31.  
  32.  
  33. procedure HandleMenuEvent (selection: longint;
  34.                                 var done: boolean);
  35.     var
  36.         MenuNum: integer;      { The longint "selection" is decoded into the id of the menu, which }
  37.         ItemNum: integer;     {    is just its menuID, and the item number, which is just the }
  38.                                        {    position of the item counting from the top of the menu. }
  39.     begin
  40.         MenuNum := HiWord(Selection);
  41.         if MenuNum = 0 then                  { no menu item was actually chosen--no action }
  42.             Exit(HandleMenuEvent);
  43.         ItemNum := LoWord(Selection);
  44.         if MenuNum = 1 then
  45.             DoAppleMenu(itemNum)
  46.         else if MenuNum = 2 then
  47.             DoFileMenu(ItemNum, done)
  48.         else if MenuNum = 3 then
  49.             DoEditMenu(ItemNum)
  50.         else
  51.             doOtherMenu(menuNum, itemNum);
  52.         HiliteMenu(0);    { A menu title is highlighted until HiliteMenu(0) is called. }
  53.     end;
  54.  
  55.  
  56.  
  57. procedure InstallMenus;
  58.     var
  59.         MBar: Handle;
  60.     begin
  61.         MBar := GetNewMBar(128);                         { Get the menu resource. }
  62.         SetMenuBar(MBar);                                      { Specify that this is the menu to be used }
  63.         appleMenu := GetMHandle(1);                        { Get a handle to the the apple menu. }
  64.         SetItem(appleMenu, 1, StringOf('About ', ApplicationShortName, '...'));
  65.         AddResMenu(appleMenu, 'DRVR');                  { Install all the desk accessaries onto the }
  66.                                                                             {     apple menu. }
  67.         editMenu := GetMHandle(3);                          { These provide GLOBAL variables for }
  68.         fileMenu := GetMHandle(2);                           {    referencing the edit and file menus; }
  69.         DrawMenuBar;                                              { This actually displays the Menu Bar and }
  70.                                                                            {     makes it active. }
  71.     end;
  72.  
  73.  
  74. procedure SendEventToWindow (win: windowPtr;
  75.                                 var theEvent: eventRecord);
  76.     var
  77.         X: XWindow;
  78.     begin
  79.         if Window2XWindow(win, x) then
  80.             X.doEvent(theEvent);
  81.     end;
  82.  
  83.  
  84.     var    { LOCAL VARIABLES FOR MAIN PROGRAM }
  85.  
  86.         done: boolean;
  87.         theEvent: EventRecord;   {  contains the event read by main event loop }
  88.         partNum: integer;
  89.         selection: longint;           { number encoding a menu selection }
  90.         whichWin: WindowPtr;    { the window that a mouseDown event occured in, or the }
  91.                                                { window of concern in an activate or update event. }
  92.         xWin: xWindow;
  93.         savePort: GrafPtr;
  94.         pt: point;
  95.  
  96.  
  97. begin
  98.  
  99.     InitXWindows;
  100.     InstallMenus;   { build the menu bar for this application }
  101.     InitializeApplication;
  102.  
  103.     FlushEvents(keyDownMask + autoKeyMask + mDownMask, 0);  { delete any events pending when}
  104.                                                                                                     { program starts. }
  105.  
  106.     InitCursor;  { changes cursor from watch to arrow; not needed inside THINK Pascal environment }
  107.  
  108.     done := false;  { this global variable is set to TRUE when an event occurs that causes the }
  109.                           { program to end.  (Program can also end via a Halt statement, as will }
  110.                           { happen if the procedure FatalError is called. }
  111.  
  112.     repeat   { This is the main event loop, which gets events and processes }
  113.                   { them until the program ends. }
  114.         if not WaitNextEvent(everyEvent, theEvent, maxSleepTime, nil) then begin{no event pending }
  115.                 ApplicationIdle
  116.             end
  117.         else begin  { process the event }
  118.                 case theEvent.what of
  119.                     keyDown:  begin
  120.                             if BitAnd(theEvent.modifiers, cmdKey) <> 0 then begin
  121.                                 { a key was pressed with the command key held down -- it needs to }
  122.                                 { be processed as a menu command. }
  123.                                     UpdateMenus;    { Enables/disables approporiate menu entries }
  124.                                     SetCursor(arrow);
  125.                                     Selection := MenuKey(chr(theEvent.message));  { which menu entry? }
  126.                                     HandleMenuEvent(Selection, done)
  127.                                 end
  128.                             else
  129.                                 SendEventToWindow(FrontWindow, theEvent);
  130.                         end;
  131.                     autoKey:   { note: don't allow autokey repeated command keys. }
  132.                         if (BitAnd(theEvent.modifiers, cmdKey) = 0) & (FrontWindow <> nil) then
  133.                             SendEventToWindow(FrontWindow, theEvent);
  134.                     mouseDown:  begin
  135.                             partNum := FindWindow(theEvent.where, whichWin);
  136.                             if partNum = inSysWindow then begin { this event is for a desk accessary or something }
  137.                                     SetCursor(arrow);
  138.                                     SystemClick(theEvent, whichWin)
  139.                                 end
  140.                             else if partNum = inMenuBar then begin
  141.                                     UpdateMenus;  { enable/disable appropriate menu entries }
  142.                                     SetCursor(arrow);
  143.                                     Selection := MenuSelect(theEvent.where);  { which menu entry? }
  144.                                     HandleMenuEvent(Selection, done);
  145.                                 end
  146.                             else if partnum <> inDesk then  { mouse event within a window }
  147.                                 SendEventToWindow(whichWin, theEvent)
  148.                         end;
  149.                     updateEvt, activateEvt:  begin
  150.                             whichWin := WindowPtr(theEvent.message);
  151.                             SendEventToWindow(whichWin, theEvent);
  152.                         end;
  153.                     otherwise
  154.                    { do nothing for keyUp, mouseUp, multifinder events }
  155.                 end;
  156.             end;
  157.         whichWin := FrontWindow;
  158.         if (whichWin <> nil) & (window2xWindow(whichWin, xWin)) then begin
  159.                 GetPort(savePort);
  160.                 SetPort(whichWin);
  161.                 GetMouse(pt);
  162.                 SetPort(savePort);
  163.                 xWin.adjustCursor(pt);
  164.             end;
  165.     until done;
  166.  
  167.     CleanUpApplication;
  168.  
  169. end.