home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Environments / MacCjr / MacC Jr / MacDemo Folder / MacDemo.c next >
Encoding:
C/C++ Source or Header  |  1987-01-05  |  7.8 KB  |  368 lines  |  [TEXT/EDIT]

  1. // MacDemo.c
  2. // © Copyright 1984, 1987 Consulair Corporation, All Rights Reserved            */
  3. /*
  4.  This is a general Mac demonstration program which shows windows,
  5.  menus, events, and textedit. Windows can be grown, moved, and closed.
  6.  It demonstrates the flavor of MacC Jr. in the Macintosh environment.
  7.  
  8.  If you allow the flag UseWithStdLib to be defined, MacDemo uses the TTY
  9.  window If you don't define it, save the file as SmallDemo.c and Compile
  10.  and Go. The resulting smaller version doesn't print a TTY message out.
  11. */
  12.  
  13.  #Options Z
  14.  
  15.  #define UseWithStdLib
  16.  
  17.  
  18. /* Declarations */
  19.  
  20.  #include <MacCDefs.h>
  21.  #ifdef UseWithStdLib
  22.   #include <Stdio.h>
  23.  #else
  24.   static strlen(s) register char *s;{register int i=0; while (s[i])i++; return(i);}
  25.  #endif
  26.  #include <Window.h>
  27.  #include <Events.h>
  28.  #include <TextEdit.h>
  29.  #include <Menu.h>
  30.  
  31.  /* Declared Here */
  32.  
  33.  MenuHandle DeskMenu;
  34.  MenuHandle EditMenu;
  35.  MenuHandle Menu;
  36.  
  37.  #define Desk_ID 200
  38.  #define Edit_ID 201
  39.  #define Menu_ID 202
  40.  
  41.  Rect screenRect = {0, 0, 384, 512};
  42.  Rect windowRectA = {50, 50, 200, 400};
  43.  Rect windowRectB = {60, 60, 210, 410};
  44.  
  45.  WindowPtr openWindow();
  46.  
  47.  #define False 0
  48.  #define True 0xFF
  49.  
  50.  /* Declared Elsewhere */
  51.  
  52.  #ifdef UseWithStdLib
  53.    extern WindowPtr console; /* Std Lib TTY Window */
  54.  #endif
  55.  
  56. /* Code */
  57.   
  58.   Init()
  59.     { 
  60.     InitFonts();
  61.     InitDialogs(0);
  62.     TEInit();
  63.     InitMenus();
  64.     InitCursor();
  65.     
  66.     /* Desk Accessory menu */
  67.       DeskMenu = NewMenu(Desk_ID,"\p\024");
  68.       AddResMenu(DeskMenu, 'DRVR');
  69.       InsertMenu(DeskMenu, 0);
  70.     
  71.     /* Edit menu */
  72.       EditMenu = NewMenu(Edit_ID, "\pEdit");
  73.       AppendMenu(EditMenu,
  74.     "\pUndo;(-;Cut/X;Copy/C;Paste/V;Clear");
  75.       InsertMenu(EditMenu, 0);
  76.       DisableItem(EditMenu, 0);
  77.     
  78.     /* "Menu" menu */
  79.       Menu = NewMenu(Menu_ID, "\pMenu");
  80.       AppendMenu(Menu,
  81.     "\pItem 1;(Dimmed Item 2;Item 3;(-;Item 5/5;Quit/.");
  82.       InsertMenu(Menu, 0);
  83.     
  84.     DrawMenuBar();
  85.     }
  86.     
  87.   
  88.   main()
  89.     { 
  90.     char c, *str;
  91.     short windowcode;
  92.     long menuResult;
  93.     
  94.     EventRecord event;
  95.     TEHandle hTE;
  96.     WindowPtr mouseWindow, window, windowA, windowB;
  97.     
  98.     if (CatchSignal())  ExitToShell();
  99.     
  100.     Init();
  101.     
  102.     #ifdef UseWithStdLib
  103.       
  104.       printf("\r\r           Mac C Demo\rCopyright Consulair Corporation 1987\r     All Rights Reserved\r\r");
  105.       printf("\rThis is a simple demonstration program with two windows.\r");
  106.       printf("\rWhen you strike a key, this TTY window will be erased, and");
  107.       printf("\rTwo windows will be displayed.  Typed text will go into the front");
  108.       printf("\rone, and you can switch between the two with a mouse click.");
  109.       printf("\rSelect Quit from the menu or hit a command period to stop.");
  110.       
  111.       getchar();
  112.       DisposeWindow(console);
  113.     
  114.     #endif
  115.     
  116.     windowB =
  117.       openWindow(&windowRectB, "\pDemo Window B",
  118.         "\r           Mac C Demo\r\251 Consulair Corporation 1987\r     All Rights Reserved\r\rThis is Window B");
  119.     
  120.     windowA =
  121.       openWindow(&windowRectA, "\pDemo Window A",
  122.         "\r           Mac C Demo\r\251 Consulair Corporation 1987\r     All Rights Reserved\r\rThis is Window A");
  123.     
  124.     hTE = 0;
  125.     InitCursor();
  126.     FlushEvents(0xFFFF);
  127.     SelectWindow(windowA); /* Generate an activate event for window A */
  128.     
  129.     while (True) 
  130.       { 
  131.       SystemTask();
  132.       if (hTE)  TEIdle(hTE);
  133.       
  134.       if (GetNextEvent(everyEvent, &event)) 
  135.     {
  136.     switch ( event.what )
  137.       {
  138.       case autoKey:
  139.       case keyDown:
  140.         {
  141.         c = event.message;
  142.         if ((event.modifiers & cmdKey))
  143.           DoMenu(MenuKey(c));
  144.           else 
  145.             if (hTE) TEKey(c, hTE);
  146.         break;
  147.         }
  148.       
  149.       case mouseDown:
  150.         {
  151.         windowcode = FindWindow(&event.where, &mouseWindow);
  152.         
  153.         if (FrontWindow() != mouseWindow) 
  154.           if (mouseWindow != 0) 
  155.         {
  156.         SelectWindow(mouseWindow);
  157.         break;
  158.         }
  159.         
  160.         if ((window != 0) && (window == mouseWindow))
  161.           
  162.         {
  163.         if (mouseWindow != 0) 
  164.           {
  165.           SetPort(mouseWindow);
  166.           switch ( windowcode )
  167.             {
  168.             case inContent:
  169.               {
  170.               GlobalToLocal(&event.where);
  171.               TEClick(&event.where,
  172.             (event.modifiers & shiftKey)? True:False, hTE);
  173.               break;
  174.               }
  175.             case inDrag:
  176.               {
  177.               DragWindow(mouseWindow, &event.where, &screenRect);
  178.               break;
  179.               }
  180.             case inGrow:
  181.               {
  182.               long growResult;
  183.               short vert, horiz;
  184.               growResult =
  185.             GrowWindow(mouseWindow, &event.where, &screenRect);
  186.               horiz = growResult;
  187.               vert = HiWord(growResult);
  188.               SizeWindow(mouseWindow, horiz, vert, True);
  189.               EraseRect(&mouseWindow->portRect);
  190.               InvalRect(&mouseWindow->portRect);
  191.               SizeTE(mouseWindow);
  192.               DrawGrowIcon(mouseWindow);
  193.               break;
  194.               }
  195.             case inGoAway:
  196.               {
  197.               if (TrackGoAway(window, &event.where)) 
  198.             {
  199.             TEDispose(hTE);
  200.             hTE = 0;
  201.             DisposeWindow(window);
  202.             window = 0;
  203.             }
  204.               break;
  205.               }
  206.             }
  207.           }
  208.         }
  209.           else
  210.         {
  211.         switch ( windowcode )
  212.           {
  213.           case inMenuBar:
  214.             {
  215.             
  216.             DoMenu(MenuSelect(&event.where));
  217.             break;
  218.             }
  219.           case inSysWindow:
  220.             {
  221.             SystemClick(&event, mouseWindow);
  222.             break;
  223.             }
  224.           case inDrag:
  225.             {
  226.             DragWindow(mouseWindow, &event.where, &screenRect);
  227.             break;
  228.             }
  229.           case inGoAway:
  230.             {
  231.             break;
  232.             }
  233.           }
  234.         }
  235.         
  236.         break;
  237.         }
  238.       
  239.       case updateEvt:
  240.         {
  241.         TEHandle temp_hTE;
  242.         WindowPtr tempWindow;
  243.         
  244.         SetPort(tempWindow = (WindowPtr)event.message);
  245.         BeginUpdate(tempWindow);
  246.         temp_hTE = (TEHandle)GetWRefCon(tempWindow);
  247.         TEUpdate(&tempWindow->portRect, temp_hTE);
  248.         DrawGrowIcon(tempWindow);
  249.         EndUpdate(tempWindow);
  250.         break;
  251.         }
  252.       
  253.       case activateEvt:
  254.         {
  255.         TEHandle temp_hTE;
  256.         WindowPtr tempWindow;
  257.         
  258.         SetPort(tempWindow = (WindowPtr)event.message);
  259.         temp_hTE = (TEHandle)GetWRefCon(tempWindow);
  260.         if ((event.modifiers & activeFlag))
  261.           
  262.         {
  263.         window = tempWindow;
  264.         TEActivate(hTE = temp_hTE)
  265.         }
  266.           else TEDeactivate(temp_hTE);
  267.         DrawGrowIcon(tempWindow);
  268.         break;
  269.         }
  270.       }
  271.     }
  272.       
  273.       } 
  274.     }
  275.   
  276.   DoMenu(menuresult)
  277.     long menuresult;
  278.     { 
  279.     short menuID, itemNumber;
  280.     
  281.     menuID = HiWord(menuresult);
  282.     itemNumber = menuresult;
  283.     
  284.     switch ( menuID )
  285.       {
  286.       case Menu_ID:
  287.     {
  288.     switch ( itemNumber )
  289.       {
  290.       case 1: break;
  291.       case 2: break;
  292.       case 3: break;
  293.       case 5: break;
  294.       case 6:    
  295.         Signal("All Done");
  296.       }
  297.     break;
  298.     }
  299.       case Desk_ID:
  300.     {
  301.     struct P_Str AccessoryName;
  302.     GetItem(DeskMenu, itemNumber, &AccessoryName);
  303.     OpenDeskAcc(&AccessoryName);
  304.     EnableItem(EditMenu, 0);
  305.     DrawMenuBar();
  306.     break;
  307.     }
  308.       case Edit_ID:
  309.     {
  310.     SystemEdit(itemNumber-1);
  311.     break;
  312.     }
  313.       }
  314.     HiliteMenu(0);
  315.     }
  316.     
  317.   
  318.   Rect *TERect(window, rect)
  319.     WindowPtr window;
  320.     Rect *rect;
  321.     {
  322.     BlockMove(&window->portRect, rect, sizeof(Rect));
  323.     rect->right -= 16;    /* Make room for scroll bar */
  324.     rect->bottom -= 16;    /* Make room for scroll bar */
  325.     }
  326.   
  327.   TEHandle openTE(window)
  328.     WindowPtr window;
  329.     {
  330.     Rect destRect, viewRect;
  331.     TEHandle hTE;
  332.     
  333.     TERect(window, &viewRect);
  334.     TERect(window, &destRect);
  335.     destRect.left += 4;        /* indent a bit */
  336.     return TENew(&destRect, &viewRect);
  337.     }
  338.   
  339.   WindowPtr openWindow(rect, title, str)
  340.     Rect *rect;
  341.     char *title, *str;
  342.     {
  343.     WindowPtr window;
  344.     TEHandle hTE;
  345.     window = NewWindow(0, rect, title, True, 0, -1, True, 0);
  346.     SetPort(window);
  347.     hTE = openTE(window);
  348.     SetWRefCon(window, hTE);
  349.     TESetText(str, strlen(str), hTE);
  350.     TEUpdate(&(*hTE)->viewRect, hTE);
  351.     return window;
  352.     }
  353.   
  354.   sizeTE(window)
  355.     WindowPtr window;
  356.     {
  357.     Rect rect;
  358.     TEHandle hTE;
  359.     
  360.     hTE = (TEHandle)GetWRefCon(window);
  361.     TERect(window, &rect);
  362.     BlockMove(&rect, &(*hTE)->viewRect, sizeof(Rect));
  363.     rect.left += 4;        /* indent a bit */
  364.     BlockMove(&rect, &(*hTE)->destRect, sizeof(Rect));
  365.     TECalText(hTE);
  366.     }
  367.     
  368.