home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / tnyfnd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-05  |  8.7 KB  |  353 lines

  1. /*
  2.  * TinyFinder.c - 21-Feb-85
  3.  *
  4.  * This program is an alternative Finder that is very fast and small,
  5.  * and limited in its abilities compared to the real Finder.  It is
  6.  * written in Aztec 68000 C (version 1.06D).
  7.  *
  8.  * copyright (c) 1985 by:
  9.  *   Kent Kersten
  10.  *   Harrison Systems
  11.  *   437 Atlas Drive
  12.  *   Nashville, TN  37211
  13.  *   (615) 834-1366
  14.  *   CompuServe CIS [72277,2726]
  15.  *
  16.  * To compile & link this file:
  17.  *       cc TinyFinder.c
  18.  *       ln -m TinyFinder.o sys:lib/sacroot.o -lc
  19.  */
  20.  
  21. #include  <quickdraw.h>
  22. #include  <window.h>
  23. #include  <event.h>
  24. #include  <menu.h>
  25. #include  <desk.h>
  26. #include  <dialog.h>
  27. #include  <resource.h>
  28. #include  <segment.h>
  29. #include  <packages.h>
  30. #include  <pb.h>
  31. #include  <syserr.h>
  32. #include  <inits.h>
  33.  
  34. #define   nil                   (long) 0
  35. #define   NUM_MENUS             4
  36.  
  37. #define   AppleMenu             256
  38. #define   FileMenu              257
  39. #define   EditMenu              258
  40. #define   SpecialMenu           259
  41.  
  42. #define   ABOUT_DLOG            256     /* resource id # of our 'About...' box */
  43. #define   GENERAL_ERROR_ALRT    500
  44.  
  45. MenuHandle TFMenu[NUM_MENUS];
  46. Point      whereOpen    = { 80, 80 };           /* where to put the SF dialog */
  47.  
  48. Boolean  DoCommand(), ProcessEvents();
  49.  
  50.  
  51. main()
  52. {
  53.      InitGraf(&thePort);
  54.      InitFonts();
  55.      InitWindows();
  56.      InitMenus();
  57.      InitDialogs(nil);
  58.      TEInit();
  59.  
  60.      FlushEvents(everyEvent, 0);        /* flush ALL events */
  61.      FormMenuBar();
  62.  
  63.      CheckForFinder();
  64.  
  65.      InitCursor();
  66.      LaunchAppl();
  67.  
  68.      while(ProcessEvents())
  69.           ;
  70. }
  71.  
  72.  
  73. /*
  74.  * Get events and process them.  Return FALSE to exit.
  75.  */
  76.  
  77. Boolean ProcessEvents()
  78. {
  79.      char         theChar;
  80.      Boolean      NoQuitFlag = TRUE;
  81.      WindowPtr    whichWindow;
  82.      EventRecord  TFEvent;
  83.      GrafPtr      savePort;
  84.  
  85.      HiliteMenu(0);                     /* turn off any highlighting in menu bar */
  86.      SystemTask();
  87.  
  88.      GetNextEvent(everyEvent, &TFEvent);
  89.      switch(TFEvent.what) {
  90.           case mouseDown:
  91.                switch(FindWindow(pass(TFEvent.where), &whichWindow)) {  /* major Aztec C68K hack-ack (pass) */
  92.                     case inMenuBar:
  93.                          NoQuitFlag = DoCommand(MenuSelect(pass(TFEvent.where)));       /* Aztec hack */
  94.                          break;
  95.                     case inSysWindow:
  96.                          SystemClick(&TFEvent,whichWindow);
  97.                          break;
  98.                }
  99.                break;
  100.           case keyDown:
  101.           case autoKey:
  102.                theChar = TFEvent.message % 256;
  103.                if((TFEvent.modifiers & cmdKey) != 0)            /* command key pressed */
  104.                     NoQuitFlag = DoCommand(MenuKey(theChar));
  105.                break;
  106.           case updateEvt:
  107.                GetPort(&savePort);
  108.                SetPort(TFEvent.message);
  109.                BeginUpdate(TFEvent.message);
  110.                EndUpdate(TFEvent.message);
  111.                SetPort(savePort);
  112.                break;
  113.      }
  114.  
  115.      return NoQuitFlag;
  116. }
  117.  
  118.  
  119. /*
  120.  * Decipher menu commands here.  Return FALSE to quit.
  121.  */
  122.  
  123. Boolean DoCommand(command)
  124. long command;
  125. {
  126.      char     name[256];
  127.      short    MenuNum, ItemNum;
  128.      Boolean  NoQuitFlag = TRUE;
  129.  
  130.      InitCursor();
  131.      MenuNum = HiWord(command);
  132.      ItemNum = LoWord(command);
  133.  
  134.      switch(MenuNum) {
  135.           case AppleMenu:
  136.                if(ItemNum == 1)
  137.                     AboutTinyFinder();          /* do our "About..." box */
  138.                else {
  139.                     GetItem(TFMenu[0], ItemNum, name);
  140.                     OpenDeskAcc(name);
  141.                }
  142.                break;
  143.           case FileMenu:
  144.                switch(ItemNum) {
  145.                     case 1:
  146.                          LaunchAppl();
  147.                          break;
  148.                     case 2:
  149.                          GoFinder();
  150.                          break;
  151.                     case 4:
  152.                          NoQuitFlag = FALSE;
  153.                          break;
  154.                }
  155.                break;
  156.           case EditMenu:
  157.                switch(ItemNum) {
  158.                     case 1:
  159.                          SystemEdit(undoCmd);
  160.                          break;
  161.                     case 3:
  162.                          SystemEdit(cutCmd);
  163.                          break;
  164.                     case 4:
  165.                          SystemEdit(copyCmd);
  166.                          break;
  167.                     case 5:
  168.                          SystemEdit(pasteCmd);
  169.                          break;
  170.                     case 6:
  171.                          SystemEdit(clearCmd);
  172.                          break;
  173.                }
  174.                break;
  175.           case SpecialMenu:
  176.                switch(ItemNum) {
  177.                     case 1:
  178.                          ShutDown();
  179.                          break;
  180.                }
  181.      }
  182.  
  183.      return NoQuitFlag;
  184. }
  185.  
  186.  
  187. /*
  188.  * This pops up a dialog for our 'About...' box.
  189.  */
  190.  
  191. AboutTinyFinder()
  192. {
  193.      int         type, ItemHit;
  194.      Handle      hdl;
  195.      DialogPtr   DPtr;
  196.      Rect        tBox;
  197.  
  198.      InitCursor();
  199.  
  200.      DPtr = GetNewDialog(ABOUT_DLOG, nil, (long) -1);
  201.      SetPort(DPtr);
  202.  
  203.      GetDItem(DPtr, 2, &type, &hdl, &tBox);             /* icon */
  204.      PlotIcon(&tBox, GetIcon(256));
  205.  
  206.      PenPat(&gray);
  207.      GetDItem(DPtr, 6, &type, &hdl, &tBox);             /* dividing line */
  208.      MoveTo(tBox.left, tBox.top);
  209.      LineTo(tBox.right, tBox.top);
  210.  
  211.      GetDItem(DPtr, 7, &type, &hdl, &tBox);             /* dividing line */
  212.      MoveTo(tBox.left, tBox.top);
  213.      LineTo(tBox.right, tBox.top);
  214.      PenPat(&black);
  215.  
  216.      while(1) {
  217.           ModalDialog(nil, &ItemHit);
  218.           if(ItemHit == 1) {
  219.                CloseDialog(DPtr);                       /* 'OK' */
  220.                break;
  221.           }
  222.      }
  223. }
  224.  
  225.  
  226. /*
  227.  * Check to see if the file named 'Finder' exists on the default volume.
  228.  * If not, then disable the command in the 'File' menu that allows the user to
  229.  * launch the Apple Finder.  Note that this is fairly dumb, in that if it
  230.  * finds ANY file named 'Finder' it thinks it is the one.  Could be made
  231.  * smarter by checking the type and creator bytes for the file, but I
  232.  * didn't feel that I should, in case there is an application by that name.
  233.  */
  234.  
  235. CheckForFinder()
  236. {
  237.      Finfo fndrInfo;
  238.  
  239.      if(GetFInfo("\006Finder", 0, &fndrInfo) < 0)       /* use default volume */
  240.           DisableItem(TFMenu[1], 2);
  241. }
  242.  
  243.  
  244. /*
  245.  * Create our menu bar.
  246.  */
  247.  
  248. FormMenuBar()
  249. {
  250.      int  i;
  251.  
  252.      TFMenu[0] = GetMenu(AppleMenu);
  253.      TFMenu[1] = GetMenu(FileMenu);
  254.      TFMenu[2] = GetMenu(EditMenu);
  255.      TFMenu[3] = GetMenu(SpecialMenu);
  256.      AddResMenu(TFMenu[0], 'DRVR');
  257.      
  258.      for(i = 0;i < NUM_MENUS;++i)
  259.           InsertMenu(TFMenu[i], 0);
  260.  
  261.      DrawMenuBar();
  262. }
  263.  
  264.  
  265. /*
  266.  * This alert takes care of our general error messages.  We can
  267.  * pass up to four Str255 types (pointers to them, that is), and
  268.  * they are combined end to end with a single space between them.
  269.  */
  270.  
  271. GeneralError(str1, str2, str3, str4)
  272. char *str1, *str2, *str3, *str4;                        /* Pascal format */
  273. {
  274.      InitCursor();
  275.      ParamText("\000", "\000", "\000", "\000");
  276.      ParamText(str1, str2, str3, str4);
  277.      StopAlert(GENERAL_ERROR_ALRT, nil);
  278. }
  279.  
  280.  
  281. /*
  282.  * Launch the Apple Finder.  This command is disabled in the menu bar
  283.  * if the program does not find the Finder on the default disk when
  284.  * we boot up and begin execution.
  285.  */
  286.  
  287. GoFinder()
  288. {
  289.      long zeroes = 0;
  290.  
  291.      Launch("\006Finder", &zeroes);
  292. }
  293.  
  294.  
  295. /*
  296.  * Come here when the 'Launch' menu command is given.
  297.  */
  298.  
  299. LaunchAppl()
  300. {
  301.      long        zeroes = 0;
  302.      short       numTypes = 1;
  303.      Ptr         prompt = nil;
  304.      Ptr         fileFilter = nil;
  305.      Handle      hdl;
  306.      SFReply     rec;
  307.      SFTypeList  typeList;
  308.  
  309.      typeList[0] = 'APPL';              /* filter out all but 'APPL' types */
  310.  
  311.      SFGetFile(pass(whereOpen), prompt, fileFilter, numTypes, typeList, nil, &rec);
  312.  
  313.      if(rec.good == 0)                          /* 'Cancel' button hit */
  314.           return;
  315.  
  316.      if(SetVol(nil, rec.vRefNum) < 0) {         /* set the new default volume */
  317.           hdl = GetString(256);
  318.           GeneralError(*hdl, nil, nil, nil);
  319.           return;
  320.      }
  321.  
  322.      Launch(rec.fName, &zeroes);
  323. }
  324.  
  325.  
  326. /*
  327.  * This is the equivalent of Apple Finder v4.1's 'Shut Down' command,
  328.  * in which the diskettes are ejected from the internal & external
  329.  * drives and a system reset is issued.  Any other volumes that happen
  330.  * to be mounted will NOT be handled.
  331.  */
  332.  
  333. ShutDown()
  334. {
  335.      int  vRefNum;
  336.      long freeBytes;
  337.      char volName[256];
  338.  
  339.      GetVInfo(1, volName, &vRefNum, &freeBytes);        /* internal drive */
  340.      Eject(volName, vRefNum);
  341.  
  342.      if(GetVInfo(2, volName, &vRefNum, &freeBytes) != ParamErr) /* external */
  343.           Eject(volName, vRefNum);
  344.  
  345.  
  346.                 /* pulse the 68000's RESET pin via a direct assembler call */
  347. #asm
  348.  
  349.      Reset
  350.  
  351. #endasm
  352. }
  353.