home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 632.lha / ParM_v3.6 / ParM_Src.lzh / ParM_Src / MenuAlloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-13  |  5.8 KB  |  258 lines

  1. /*
  2.  *    MenuAlloc.c - Copyright © 1990 by S.R. & P.C.
  3.  *
  4.  *    Created:    16 Jun 1990
  5.  *    Modified:    12 Feb 1992  20:46:13
  6.  *
  7.  *    Make>> make
  8.  */
  9.  
  10. #include "ParMBase.h"
  11.  
  12. #define MEM_BLOCK_SIZE 1024
  13.  
  14. extern void RemParMEvents(struct Window *Win);
  15.  
  16. static struct TextAttr Topaz80 = {
  17.     (STRPTR)"topaz.font",
  18.     8,
  19.     0,
  20.     FPF_ROMFONT
  21. };
  22.  
  23.  
  24. /* Memory allocation functions */
  25.  
  26. static void *Malloc(struct ParMConfig *PCfg, size_t size)
  27. {
  28.     void *chunck;
  29.  
  30.     size += 3;            /* make chuncks long word aligned */
  31.     size &= ~(3L);
  32.     if (size > PCfg->Avail) {
  33.         if (size > MEM_BLOCK_SIZE) {
  34.             SimpleRequest(PCfg->ReqTitle, "Line too long");
  35.             return NULL;
  36.         }
  37.         if (!(PCfg->mem = AllocRemember(&PCfg->MemList, MEM_BLOCK_SIZE, MEMF_PUBLIC|MEMF_CLEAR)))
  38.             return NULL;
  39.         PCfg->Avail = MEM_BLOCK_SIZE;
  40.     }
  41.     chunck = PCfg->mem;
  42.     PCfg->mem += size;
  43.     PCfg->Avail -= size;
  44.     return chunck;
  45. }
  46.  
  47.  
  48. /*****  make (and Mallocate) a copy of the passed string *****/
  49.  
  50. static char *MallocStr(struct ParMConfig *PCfg, char *str)
  51. {
  52.     char *newstr;
  53.  
  54.     if (newstr = Malloc(PCfg, strlen(str)+1))
  55.         strcpy(newstr, str);
  56.     return newstr;
  57. }
  58.  
  59.  
  60. static short CleanUpIT(struct MenuItem *iptr, struct TextAttr *MenuFont)
  61. {
  62.     struct IntuiText *IT;
  63.     short txtw;
  64.  
  65.     IT = (struct IntuiText *)iptr->ItemFill;
  66.     IT->ITextFont = MenuFont;
  67.     txtw = IntuiTextLength(IT) + 2;
  68.     IT->ITextFont = NULL;
  69.     if (iptr->Flags & COMMSEQ) txtw += 48;
  70.     if (iptr->Flags & CHECKIT) {
  71.         txtw += 20;
  72.         IT->LeftEdge = 21;
  73.     }
  74.     return txtw;
  75. }
  76.  
  77.  
  78. /* clean up widths and other info now that menu is all built */
  79.  
  80. void CleanUp(struct Menu *Menu, short LeftEdge, short ItemHeight)
  81. {
  82.     NewCleanUp(Menu, &Topaz80, LeftEdge);
  83. }
  84.  
  85.  
  86. void NewCleanUp(struct Menu *Menu, struct TextAttr *MenuFont, short LeftEdge)
  87. {
  88.     UWORD maxw, smaxw, txtw, top, stop;
  89.     struct MenuItem *iptr, *sptr;
  90.     struct IntuiText IntuiText;        /* To determine menu width */
  91.     short ItemHeight;
  92.  
  93.     ItemHeight = MenuFont->ta_YSize+1;
  94.     IntuiText.ITextFont = MenuFont;
  95.     for( ; Menu ; Menu = Menu->NextMenu ) {
  96.         Menu->LeftEdge = LeftEdge;
  97.         IntuiText.IText = (UBYTE *)Menu->MenuName;
  98.         maxw = Menu->Width = IntuiTextLength(&IntuiText) + 12;
  99.         LeftEdge += maxw;
  100.         top = 0;
  101.         /* determine max width */
  102.         for( iptr = Menu->FirstItem ; iptr ; iptr = iptr->NextItem ) {
  103.             iptr->TopEdge = top;
  104.             top += (iptr->Height = ItemHeight);
  105.             txtw = CleanUpIT(iptr, MenuFont);
  106.             if (txtw > maxw) maxw = txtw;
  107.         }
  108.         for( iptr = Menu->FirstItem ; iptr ; iptr=iptr->NextItem ) {
  109.             iptr->Width = maxw;
  110.             stop = smaxw = 0;
  111.             for( sptr=iptr->SubItem ; sptr ; sptr=sptr->NextItem ) {
  112.                 sptr->LeftEdge = maxw - 20;
  113.                 sptr->TopEdge = stop;
  114.                 stop += (sptr->Height = ItemHeight);
  115.                 txtw = CleanUpIT(sptr, MenuFont);
  116.                 if (txtw > smaxw) smaxw = txtw;
  117.             }
  118.             for( sptr=iptr->SubItem ; sptr ; sptr=sptr->NextItem )
  119.                 sptr->Width = smaxw;
  120.         }
  121.     }
  122. }
  123.  
  124.  
  125. /* allocate and initialize a new MenuItem */
  126.  
  127. struct Extended_MenuItem *AllocItem(struct ParMConfig *PCfg, char *itemstr)
  128. {
  129.     struct IntuiText *IT;
  130.     struct Extended_MenuItem *emi;
  131.  
  132.     if (!(emi = Malloc(PCfg, sizeof(struct Extended_MenuItem))))
  133.         return FALSE;
  134.     if (!(IT = Malloc(PCfg, sizeof(struct IntuiText))))
  135.         return FALSE;
  136.     emi->emi_MenuItem.Flags = ITEMTEXT+HIGHCOMP+ITEMENABLED;
  137.     IT->FrontPen = PCfg->MenuPen;
  138.     IT->LeftEdge = IT->TopEdge = 1;
  139.     IT->DrawMode = JAM1;
  140.     if (!(IT->IText = (UBYTE *)MallocStr(PCfg, itemstr)))
  141.         return FALSE;
  142.     emi->emi_MenuItem.ItemFill = (APTR)IT;
  143.     emi->emi_MenuItem.NextSelect = MENUNULL;
  144.     return emi;
  145. }
  146.  
  147.  
  148. /* allocate and initialize a new Menu */
  149.  
  150. BOOL AddMenu(struct ParMConfig *PCfg, char *str)
  151. {
  152.     struct Menu *Menu;
  153.  
  154.     if (!(Menu = Malloc(PCfg, sizeof(struct Menu))))
  155.         return FALSE;
  156.     if (!(Menu->MenuName = MallocStr(PCfg, str)))
  157.         return FALSE;
  158.     Menu->Flags = MENUENABLED;
  159.     PCfg->CurrentMenu->NextMenu = Menu;
  160.     PCfg->CurrentMenu = Menu;
  161.     PCfg->CurrentItem = &Menu->FirstItem;
  162.     return TRUE;
  163. }
  164.  
  165.  
  166. BOOL AddSubMenu(struct ParMConfig *PCfg, char *substr)
  167. {
  168.     if (!(*PCfg->CurrentItem = (struct MenuItem *)AllocItem(PCfg, substr)))
  169.         return FALSE;
  170.     PCfg->CurrentSubMenu = *PCfg->CurrentItem;
  171.     PCfg->CurrentItem = &PCfg->CurrentSubMenu->SubItem;
  172.     return TRUE;
  173. }
  174.  
  175.  
  176. static BOOL FillRunInfo(struct ParMConfig *PCfg, struct RunInfo *RunInfo, char *cmd, char *args, char *win)
  177. {
  178.     if (!(RunInfo->ri_Cmd = MallocStr(PCfg, cmd)))
  179.         return FALSE;
  180.     if (args && !(RunInfo->ri_Args = MallocStr(PCfg, args)))
  181.         return FALSE;
  182.     if (win && !(RunInfo->ri_Window = MallocStr(PCfg, win)))
  183.         return FALSE;
  184.     return TRUE;
  185. }
  186.  
  187.  
  188. BOOL AddEntry(    struct ParMConfig *PCfg,
  189.                 char *item,
  190.                 char *cmd,
  191.                 char *args,
  192.                 char *win,
  193.                 char shortcut,
  194.                 char mode,
  195.                 long stk,
  196.                 short pri,
  197.                 USHORT Flags )
  198. {
  199.     struct Extended_MenuItem *emi;
  200.  
  201.     if (!(emi = AllocItem(PCfg, item)))
  202.         return FALSE;
  203.     if (shortcut) {
  204.         emi->emi_MenuItem.Flags |= COMMSEQ;
  205.         emi->emi_MenuItem.Command = shortcut;
  206.     }
  207.     emi->emi_Mode = mode;
  208.     emi->emi_Flags = Flags;
  209.     emi->emi_RunInfo.ri_Pri = pri;
  210.     emi->emi_RunInfo.ri_Stack = stk;
  211.     if (!FillRunInfo(PCfg, &emi->emi_RunInfo, cmd, args, win))
  212.         return FALSE;
  213.     *PCfg->CurrentItem = (struct MenuItem *)emi;
  214.     PCfg->CurrentItem = &emi->emi_MenuItem.NextItem;
  215.     return TRUE;
  216. }
  217.  
  218.  
  219. BOOL AddAutoCmd(struct ParMConfig *PCfg,
  220.                 ULONG FileType,
  221.                 char *cmd,
  222.                 char *args,
  223.                 char *win,
  224.                 char mode,
  225.                 long stk,
  226.                 short pri,
  227.                 USHORT Flags )
  228. {
  229.     struct AutoCmd *AutoCmd;
  230.  
  231.     if (!(AutoCmd = Malloc(PCfg, sizeof(struct AutoCmd))))
  232.         return FALSE;
  233.     AutoCmd->ac_FileType = FileType;
  234.     AutoCmd->ac_Mode = mode;
  235.     AutoCmd->ac_Flags = Flags;
  236.     AutoCmd->ac_RunInfo.ri_Pri = pri;
  237.     AutoCmd->ac_RunInfo.ri_Stack = stk;
  238.     if (!FillRunInfo(PCfg, &AutoCmd->ac_RunInfo, cmd, args, win))
  239.         return FALSE;
  240.     AutoCmd->ac_Next = PCfg->AutoCmdList;
  241.     PCfg->AutoCmdList = AutoCmd;
  242.     return TRUE;
  243. }
  244.  
  245.  
  246. /* free up all space taken up by our menus */
  247.  
  248. void FreeMenus(struct ParMConfig *PCfg)
  249. {
  250.     FreeRemember(&PCfg->MemList, TRUE);
  251.     PCfg->CurrentMenu = PCfg->LinkMenu;
  252.     PCfg->LinkMenu->NextMenu = NULL;
  253.     PCfg->AutoCmdList = NULL;
  254.     PCfg->Avail = 0;
  255.     RemParMEvents(PCfg->Win);
  256. }
  257.  
  258.