home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 492.lha / ToolManager_v1.3 / src / toollist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-06  |  3.7 KB  |  129 lines

  1. /*
  2.  * toollist.c   V1.3
  3.  *
  4.  * tool list handling
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Add one program to tool list */
  12. BOOL AddToolNode(BPTR dir, char *name, char *realname, BOOL clitool)
  13. {
  14.  static ULONG ToolID=1; /* Counter for Tool ID */
  15.  struct ToolNode *tn;
  16.  
  17.  if (ToolCount>=99) goto e1; /* Not more than 99 Tools :-) possible */
  18.  
  19.  /* Get memory for ToolNode */
  20.  if (!(tn=malloc(sizeof(struct ToolNode))))
  21.   goto e1;
  22.  
  23.  /* Get memory for menu item name and copy it */
  24.  if (!(tn->tn_Node.ln_Name=strdup(name)))
  25.   goto e2;
  26.  
  27.  /* Does the program have a real name? */
  28.  if (realname)
  29.   {
  30.    /* Get memory for real program name and copy it */
  31.    if (!(tn->tn_RealName=strdup(realname)))
  32.     goto e3;
  33.   }
  34.  else
  35.   tn->tn_RealName=NULL; /* Otherwise: menu item name == real program name */
  36.  
  37.  /* Add a new menu entry */
  38.  if (!(tn->tn_MenuItem=AddAppMenuItem(ToolID+1,NULL,tn->tn_Node.ln_Name,
  39.                                       MyMP,TAG_DONE)))
  40.   goto e4;
  41.  
  42.  if (!(tn->tn_DirLock=DupLock(dir))) /* Duplicate directory lock */
  43.   goto e5;
  44.  
  45.  /* Add node to tool list */
  46.  tn->tn_ID=++ToolID;    /* Set Tool ID */
  47.  tn->tn_CLI=clitool;    /* Set Tool program type */
  48.  ToolCount++;           /* Increment active tool count */
  49.  DetachToolList();      /* Detach list from ListView gadget */
  50.  AddTail(&ToolList,(struct Node *) tn);
  51.  AttachToolList();      /* Attach list to ListView gadget */
  52.  
  53.  /* Node successfully added to list! */
  54.  return(TRUE);
  55.  
  56.  /* Something went wrong.... */
  57. e5: RemoveAppMenuItem(tn->tn_MenuItem);
  58. e4: if (tn->tn_RealName) free(tn->tn_RealName);
  59. e3: free(tn->tn_Node.ln_Name);
  60. e2: free(tn);
  61. e1: return(FALSE);
  62. }
  63.  
  64. /* Scan Workbench parameters and add them to tool list */
  65. BOOL WBAddToolNode(struct WBArg *wbarg, int numargs)
  66. {
  67.  int i;
  68.  BOOL rc=TRUE;
  69.  BPTR fl;
  70.  struct DiskObject *dobj;
  71.  
  72.  for (i=numargs; i; i--,wbarg++)            /* Scan all parameters */
  73.   /* Sanity check */
  74.   if (wbarg->wa_Lock && wbarg->wa_Name && (strlen(wbarg->wa_Name)!=0))
  75.    {
  76.     fl=CurrentDir(wbarg->wa_Lock);          /* Change to icon's directory */
  77.     if (dobj=GetDiskObject(wbarg->wa_Name)) /* Get icon data */
  78.      {
  79.       switch(dobj->do_Type) /* Perform action depending on icon type */
  80.        {
  81.         case WBTOOL:    /* Icon is a Tool. Menu entry == real program name */
  82.          rc&=AddToolNode(wbarg->wa_Lock,wbarg->wa_Name,wbarg->wa_Name,FALSE);
  83.          break;
  84.         case WBPROJECT: /* Icon is a Project. Menu entry = icon name,
  85.                            real program name = default tool name */
  86.          rc&=AddToolNode(wbarg->wa_Lock,wbarg->wa_Name,
  87.                          dobj->do_DefaultTool,FALSE);
  88.          break;
  89.         default:        /* Every other icon type is erroneous */
  90.          rc&=FALSE;
  91.          break;
  92.        }
  93.       FreeDiskObject(dobj); /* Release icon data */
  94.      }
  95.     else /* Can't get icon data, so we add it as a CLI tool */
  96.      rc&=AddToolNode(wbarg->wa_Lock,wbarg->wa_Name,wbarg->wa_Name,TRUE);
  97.  
  98.     CurrentDir(fl); /* Change to old directory */
  99.    }
  100.   else
  101.    rc&=FALSE; /* Error: Bad Workbench parameter */
  102.  
  103.  return(rc); /* Return TRUE if no error */
  104. }
  105.  
  106. /* Remove ONE node from the tool list */
  107. void RemToolNode(struct ToolNode *tn)
  108. {
  109.  Remove((struct Node *) tn);                 /* Remove node from list */
  110.  UnLock(tn->tn_DirLock);                     /* Free directory */
  111.  RemoveAppMenuItem(tn->tn_MenuItem);         /* Remove menu entry */
  112.  if (tn->tn_RealName) free(tn->tn_RealName); /* Free memory */
  113.  free(tn->tn_Node.ln_Name);
  114.  free(tn);
  115.  ToolCount--;                                /* decrement active tool count */
  116. }
  117.  
  118. /* Remove ALL nodes from the tool list */
  119. void RemoveTools(void)
  120. {
  121.  struct ToolNode *tn1,*tn2=GetHead(&ToolList); /* Begin of list */
  122.  
  123.  while (tn1=tn2)
  124.   {
  125.    tn2=GetSucc(tn1); /* Next in list */
  126.    RemToolNode(tn1); /* Remove node */
  127.   }
  128. }
  129.