home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / menustuf.sit / SetSubMenu.p < prev   
Text File  |  1990-04-16  |  4KB  |  133 lines

  1. Program SetMenu;
  2. {
  3.     This program is an MPW tool which links a specified menu item
  4.     to a hierarchical menu, or removes such a link. Call it from
  5.     the MPW Shell as follows:
  6.  
  7.         SetSubMenu [menuName [itemName [subMenuName]]]
  8.  
  9.     where menuName is the name of the menu to be altered, itemName is the
  10.     name of the menu item, and subMenuName is the name of the submenu
  11.     to link to. If subMenuName is omitted, then any existing link from
  12.     the menu item to a hierarchical menu is removed.
  13.  
  14.     If the second and third arguments are omitted, a list of all submenus
  15.     attached to the specified menu is written to the standard output.
  16.     If the first argument is omitted as well, this list includes submenus
  17.     attached to all menus (both top-level and hierarchical) in the current
  18.     menu list. In both cases, this list takes the form of a sequence of
  19.     SetSubMenu commands which will reconstruct the menu hierarchy.
  20.  
  21.     First working version by LDO 1989 January 4.
  22.     Modified 1989 January 6 to add listing function.
  23.     Modified 1989 January 9 to allow adding submenus to submenus and
  24.         use nicer quoting function.
  25.     Modified 1989 January 19 to check that submenu actually exists
  26.         before listing link, just in case.
  27. }
  28.  
  29.     Uses
  30.         MemTypes,
  31.         QuickDraw,
  32.         OSIntf,
  33.         ToolIntf,
  34.         IntEnv,
  35.         MenuStuffCommon;
  36.  
  37.     Var
  38.         TheMenu, TheSubMenu : MenuHandle;
  39.         ItemNo : LongInt;
  40.         ExistingCmd : Char;
  41.  
  42.     Procedure ListSubMenus
  43.       (
  44.         ThisMenu : MenuHandle;
  45.         Ignored1, Ignored2 : univ LongInt
  46.       );
  47.       { handler which lists the hierarchical connections for
  48.         the specified menu. }
  49.  
  50.         Var
  51.             ThisItem : Integer;
  52.             ThisItemCmd, ThisItemMark : Char;
  53.             ThisMenuName, ThisItemName, SubMenuName : Str255;
  54.             SubMenu : MenuHandle;
  55.  
  56.       Begin
  57.         ThisMenuName := ThisMenu^^.MenuData;
  58.         For ThisItem := 1 to CountMItems(ThisMenu) do
  59.           Begin
  60.             GetItemCmd(ThisMenu, ThisItem, ThisItemCmd);
  61.             If Ord(ThisItemCmd) = hMenuCmd then
  62.               Begin
  63.                 GetItem(ThisMenu, ThisItem, ThisItemName);
  64.                 GetItemMark(ThisMenu, ThisItem, ThisItemMark);
  65.                 SubMenu := GetMHandle(Ord(ThisItemMark));
  66.                 If SubMenu <> Nil then
  67.                   Begin
  68.                     SubMenuName := SubMenu^^.MenuData;
  69.                     Writeln(Output, ArgV^[0]^, ' ', Quote(ThisMenuName), ' ',
  70.                         Quote(ThisItemName), ' ', Quote(SubMenuName))
  71.                   End {If}
  72.               End {If}
  73.           End {For}
  74.       End {ListSubMenus};
  75.  
  76.   Begin {SetSubMenu}
  77.     InitGraf(@ThePort);
  78.     If ArgC > 1 then
  79.       Begin
  80.         TheMenu := FindMenu(ArgV^[1]^);
  81.         If TheMenu = Nil then
  82.             TheMenu := FindSubMenu(ArgV^[1]^);
  83.         If TheMenu <> Nil then
  84.             If ArgC > 2 then
  85.               Begin
  86.                 ItemNo := FindItem(TheMenu, ArgV^[2]^);
  87.                 If ItemNo > 0 then
  88.                     If ArgC > 3 then
  89.                       Begin
  90.                       { add link to hierarchical menu }
  91.                         TheSubMenu := FindSubMenu(ArgV^[3]^);
  92.                         If TheSubMenu <> Nil then
  93.                           Begin
  94.                             SetItemCmd(TheMenu, ItemNo, Chr(hMenuCmd));
  95.                             SetItemMark(TheMenu, ItemNo, Chr(TheSubMenu^^.MenuID))
  96.                           End
  97.                         else
  98.                           Begin
  99.                             Writeln(Diagnostic, '### ', ArgV^[0]^, ' - submenu "', ArgV^[3]^, '" not found');
  100.                             IEExit(2)
  101.                           End
  102.                       End
  103.                     else
  104.                       Begin
  105.                       { remove link to hierarchical menu }
  106.                         GetItemCmd(TheMenu, ItemNo, ExistingCmd);
  107.                         If Ord(ExistingCmd) = hMenuCmd then
  108.                           Begin
  109.                             SetItemCmd(TheMenu, ItemNo, Chr(0));
  110.                             SetItemMark(TheMenu, ItemNo, Chr(0))
  111.                           End {If}
  112.                       End
  113.                 else
  114.                   Begin
  115.                     Writeln(Diagnostic, '### ', ArgV^[0]^, ' - item "', ArgV^[2]^, '" not found');
  116.                     IEExit(2)
  117.                   End
  118.               End
  119.             else
  120.                 ListSubMenus(TheMenu, Nil, Nil)
  121.         else
  122.           Begin
  123.             Writeln(Diagnostic, '### ', ArgV^[0]^, ' - menu "', ArgV^[1]^, '" not found');
  124.             IEExit(2)
  125.           End
  126.       End
  127.     else
  128.       Begin
  129.         TraverseMenus(@ListSubMenus, Nil);
  130.         TraverseSubMenus(@ListSubMenus, Nil)
  131.       End
  132.   End {SetSubMenu}.
  133.