home *** CD-ROM | disk | FTP | other *** search
- /*
- * MenuAlloc.c - Copyright © 1990 by S.R. & P.C.
- *
- * Created: 16 Jun 1990
- * Modified: 12 Feb 1992 20:46:13
- *
- * Make>> make
- */
-
- #include "ParMBase.h"
-
- #define MEM_BLOCK_SIZE 1024
-
- extern void RemParMEvents(struct Window *Win);
-
- static struct TextAttr Topaz80 = {
- (STRPTR)"topaz.font",
- 8,
- 0,
- FPF_ROMFONT
- };
-
-
- /* Memory allocation functions */
-
- static void *Malloc(struct ParMConfig *PCfg, size_t size)
- {
- void *chunck;
-
- size += 3; /* make chuncks long word aligned */
- size &= ~(3L);
- if (size > PCfg->Avail) {
- if (size > MEM_BLOCK_SIZE) {
- SimpleRequest(PCfg->ReqTitle, "Line too long");
- return NULL;
- }
- if (!(PCfg->mem = AllocRemember(&PCfg->MemList, MEM_BLOCK_SIZE, MEMF_PUBLIC|MEMF_CLEAR)))
- return NULL;
- PCfg->Avail = MEM_BLOCK_SIZE;
- }
- chunck = PCfg->mem;
- PCfg->mem += size;
- PCfg->Avail -= size;
- return chunck;
- }
-
-
- /***** make (and Mallocate) a copy of the passed string *****/
-
- static char *MallocStr(struct ParMConfig *PCfg, char *str)
- {
- char *newstr;
-
- if (newstr = Malloc(PCfg, strlen(str)+1))
- strcpy(newstr, str);
- return newstr;
- }
-
-
- static short CleanUpIT(struct MenuItem *iptr, struct TextAttr *MenuFont)
- {
- struct IntuiText *IT;
- short txtw;
-
- IT = (struct IntuiText *)iptr->ItemFill;
- IT->ITextFont = MenuFont;
- txtw = IntuiTextLength(IT) + 2;
- IT->ITextFont = NULL;
- if (iptr->Flags & COMMSEQ) txtw += 48;
- if (iptr->Flags & CHECKIT) {
- txtw += 20;
- IT->LeftEdge = 21;
- }
- return txtw;
- }
-
-
- /* clean up widths and other info now that menu is all built */
-
- void CleanUp(struct Menu *Menu, short LeftEdge, short ItemHeight)
- {
- NewCleanUp(Menu, &Topaz80, LeftEdge);
- }
-
-
- void NewCleanUp(struct Menu *Menu, struct TextAttr *MenuFont, short LeftEdge)
- {
- UWORD maxw, smaxw, txtw, top, stop;
- struct MenuItem *iptr, *sptr;
- struct IntuiText IntuiText; /* To determine menu width */
- short ItemHeight;
-
- ItemHeight = MenuFont->ta_YSize+1;
- IntuiText.ITextFont = MenuFont;
- for( ; Menu ; Menu = Menu->NextMenu ) {
- Menu->LeftEdge = LeftEdge;
- IntuiText.IText = (UBYTE *)Menu->MenuName;
- maxw = Menu->Width = IntuiTextLength(&IntuiText) + 12;
- LeftEdge += maxw;
- top = 0;
- /* determine max width */
- for( iptr = Menu->FirstItem ; iptr ; iptr = iptr->NextItem ) {
- iptr->TopEdge = top;
- top += (iptr->Height = ItemHeight);
- txtw = CleanUpIT(iptr, MenuFont);
- if (txtw > maxw) maxw = txtw;
- }
- for( iptr = Menu->FirstItem ; iptr ; iptr=iptr->NextItem ) {
- iptr->Width = maxw;
- stop = smaxw = 0;
- for( sptr=iptr->SubItem ; sptr ; sptr=sptr->NextItem ) {
- sptr->LeftEdge = maxw - 20;
- sptr->TopEdge = stop;
- stop += (sptr->Height = ItemHeight);
- txtw = CleanUpIT(sptr, MenuFont);
- if (txtw > smaxw) smaxw = txtw;
- }
- for( sptr=iptr->SubItem ; sptr ; sptr=sptr->NextItem )
- sptr->Width = smaxw;
- }
- }
- }
-
-
- /* allocate and initialize a new MenuItem */
-
- struct Extended_MenuItem *AllocItem(struct ParMConfig *PCfg, char *itemstr)
- {
- struct IntuiText *IT;
- struct Extended_MenuItem *emi;
-
- if (!(emi = Malloc(PCfg, sizeof(struct Extended_MenuItem))))
- return FALSE;
- if (!(IT = Malloc(PCfg, sizeof(struct IntuiText))))
- return FALSE;
- emi->emi_MenuItem.Flags = ITEMTEXT+HIGHCOMP+ITEMENABLED;
- IT->FrontPen = PCfg->MenuPen;
- IT->LeftEdge = IT->TopEdge = 1;
- IT->DrawMode = JAM1;
- if (!(IT->IText = (UBYTE *)MallocStr(PCfg, itemstr)))
- return FALSE;
- emi->emi_MenuItem.ItemFill = (APTR)IT;
- emi->emi_MenuItem.NextSelect = MENUNULL;
- return emi;
- }
-
-
- /* allocate and initialize a new Menu */
-
- BOOL AddMenu(struct ParMConfig *PCfg, char *str)
- {
- struct Menu *Menu;
-
- if (!(Menu = Malloc(PCfg, sizeof(struct Menu))))
- return FALSE;
- if (!(Menu->MenuName = MallocStr(PCfg, str)))
- return FALSE;
- Menu->Flags = MENUENABLED;
- PCfg->CurrentMenu->NextMenu = Menu;
- PCfg->CurrentMenu = Menu;
- PCfg->CurrentItem = &Menu->FirstItem;
- return TRUE;
- }
-
-
- BOOL AddSubMenu(struct ParMConfig *PCfg, char *substr)
- {
- if (!(*PCfg->CurrentItem = (struct MenuItem *)AllocItem(PCfg, substr)))
- return FALSE;
- PCfg->CurrentSubMenu = *PCfg->CurrentItem;
- PCfg->CurrentItem = &PCfg->CurrentSubMenu->SubItem;
- return TRUE;
- }
-
-
- static BOOL FillRunInfo(struct ParMConfig *PCfg, struct RunInfo *RunInfo, char *cmd, char *args, char *win)
- {
- if (!(RunInfo->ri_Cmd = MallocStr(PCfg, cmd)))
- return FALSE;
- if (args && !(RunInfo->ri_Args = MallocStr(PCfg, args)))
- return FALSE;
- if (win && !(RunInfo->ri_Window = MallocStr(PCfg, win)))
- return FALSE;
- return TRUE;
- }
-
-
- BOOL AddEntry( struct ParMConfig *PCfg,
- char *item,
- char *cmd,
- char *args,
- char *win,
- char shortcut,
- char mode,
- long stk,
- short pri,
- USHORT Flags )
- {
- struct Extended_MenuItem *emi;
-
- if (!(emi = AllocItem(PCfg, item)))
- return FALSE;
- if (shortcut) {
- emi->emi_MenuItem.Flags |= COMMSEQ;
- emi->emi_MenuItem.Command = shortcut;
- }
- emi->emi_Mode = mode;
- emi->emi_Flags = Flags;
- emi->emi_RunInfo.ri_Pri = pri;
- emi->emi_RunInfo.ri_Stack = stk;
- if (!FillRunInfo(PCfg, &emi->emi_RunInfo, cmd, args, win))
- return FALSE;
- *PCfg->CurrentItem = (struct MenuItem *)emi;
- PCfg->CurrentItem = &emi->emi_MenuItem.NextItem;
- return TRUE;
- }
-
-
- BOOL AddAutoCmd(struct ParMConfig *PCfg,
- ULONG FileType,
- char *cmd,
- char *args,
- char *win,
- char mode,
- long stk,
- short pri,
- USHORT Flags )
- {
- struct AutoCmd *AutoCmd;
-
- if (!(AutoCmd = Malloc(PCfg, sizeof(struct AutoCmd))))
- return FALSE;
- AutoCmd->ac_FileType = FileType;
- AutoCmd->ac_Mode = mode;
- AutoCmd->ac_Flags = Flags;
- AutoCmd->ac_RunInfo.ri_Pri = pri;
- AutoCmd->ac_RunInfo.ri_Stack = stk;
- if (!FillRunInfo(PCfg, &AutoCmd->ac_RunInfo, cmd, args, win))
- return FALSE;
- AutoCmd->ac_Next = PCfg->AutoCmdList;
- PCfg->AutoCmdList = AutoCmd;
- return TRUE;
- }
-
-
- /* free up all space taken up by our menus */
-
- void FreeMenus(struct ParMConfig *PCfg)
- {
- FreeRemember(&PCfg->MemList, TRUE);
- PCfg->CurrentMenu = PCfg->LinkMenu;
- PCfg->LinkMenu->NextMenu = NULL;
- PCfg->AutoCmdList = NULL;
- PCfg->Avail = 0;
- RemParMEvents(PCfg->Win);
- }
-
-