home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d500 / wiconify.lha / wIconify / wIconSetter.lzh / wIconSetter / Source / wIconFileLists.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  14KB  |  507 lines

  1. /*
  2.  *  WICONSETTER     A companion utility to wIconify.  wIconSetter allows
  3.  *                  you to specify custom icons for windows ans screens
  4.  *                  that normally use the default icons.
  5.  *
  6.  *  wIconFileLists.c    Maintains data structures for wIconSetter.
  7.  *
  8.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  9.  *  You may use this code, provided this copyright notice is kept intact.
  10.  */
  11.  
  12. #include "wIconFile.h"
  13.  
  14. ICONPROGRAM  **ProgramPtr = &FirstProgram;  /* Pointer to current program */
  15. ICONSCREEN   **ScreenPtr;                   /* Pointer to current screen */
  16. ICONWINDOW   **WindowPtr;                   /* Pointer to current window */
  17. ICONWINDOW   **FirstWPtr;                   /* Pointer to first window */
  18. ICONDEFINE   **DefinePtr = &FirstDefine;    /* Pointer to current definition */
  19. ICONWINDOW    *IconPtr;                     /* Pointer to current icon */
  20. struct Image **MaskPtr;                     /* Pointer to mask image */
  21.  
  22. int NoIcon = TRUE;              /* TRUE until an icon command is given */
  23.  
  24.  
  25. /*
  26.  *  ClearBitMap()
  27.  *
  28.  *   Set each word to zero, and clear the dimensions, bitplane counters
  29.  *   and line count.
  30.  */
  31.  
  32. void ClearBitMap()
  33. {
  34.     short i,j,k;
  35.     
  36.     for (i=0; i<MAXWORDS; i++)
  37.        for (j=0; j<MAXHEIGHT; j++)
  38.           for (k=0; k<MAXDEPTH; k++)
  39.              ImageData[i][j][k] = 0;
  40.     MaxWidth = MaxHeight = MaxWords = 0;
  41.     Plane0 = 0xFF; Plane1 = 0x00;
  42.     BitMapLine = 0;
  43. }
  44.  
  45.  
  46. /*
  47.  *  SetImage()
  48.  *
  49.  *  Set the PlanePick and PlaneOnOff values according to which planes have
  50.  *  zeros and which have ones (if a plane is all zero or all one it can 
  51.  *  use PlaneOnOff and be excluded from PlanePick).
  52.  *
  53.  *  Get a new Image structure (report an error if out of memory)
  54.  *  Count the number of planes of data (the number of 1's in PlanePick).
  55.  *  Set the Image fields.
  56.  *  If there are any planes of data
  57.  *    Allocate enough CHIP memory for them,
  58.  *    If successful,
  59.  *      Set the ImageData pointer,
  60.  *      For each plane in the image,
  61.  *        if the plane is one of the picked planes,
  62.  *          copy the values of the image data for that plane
  63.  *    Otherwise, report a memory error, and free the image structure
  64.  */
  65.  
  66. void SetImage(theImage)
  67. struct Image **theImage;
  68. {
  69.    short i,j,k;
  70.    UBYTE PlanePick = (~Plane0 & Plane1) | (Plane0 & ~Plane1);
  71.    UBYTE PlaneOnOff = Plane0 & Plane1;
  72.    short Depth = 0;
  73.    UWORD *theWord;
  74.  
  75.    if (NEWSTRUCT(Image,*theImage))
  76.    {
  77.       for (k=PlanePick; k; k>>=1) if (k & 1) Depth++;
  78.       (*theImage)->PlanePick  = PlanePick;
  79.       (*theImage)->PlaneOnOff = PlaneOnOff;
  80.       (*theImage)->Width      = MaxWidth;
  81.       (*theImage)->Height     = MaxHeight;
  82.       (*theImage)->Depth      = Depth;
  83.  
  84.       if (Depth)
  85.       {
  86.          theWord = AllocRaster(MaxWidth,MaxHeight*Depth);
  87.          if (theWord)
  88.          {
  89.             (*theImage)->ImageData = theWord;
  90.             for (k=0; k<MAXDEPTH; k++)
  91.             {
  92.                if (PlanePick & (1<<k))
  93.                {
  94.                   for (j=0; j<MaxHeight; j++)
  95.                      for (i=0; i<MaxWords; i++)
  96.                         *theWord++ = ImageData[i][j][k];
  97.                }
  98.             }
  99.          } else {
  100.             ShowError("Can't get CHIP memory for Image");
  101.             FREESTRUCT(Image,*theImage);
  102.             *theImage = NULL;
  103.          }
  104.       }
  105.    } else ShowError("Can't get memory for Image structure");
  106.    NoIcon = FALSE;
  107. }
  108.  
  109.  
  110.  
  111. /*
  112.  *  CopyIconData()
  113.  *
  114.  *  If there are windows to add icons to,
  115.  *    Look through the window list until we get the the current one,
  116.  *    For each window, copy the correct type of data to the window
  117.  *      from the current icon (the icon of the current window).
  118.  *      Go on to the next window.
  119.  */
  120.  
  121. void CopyIconData(type)
  122. {
  123.    ICONWINDOW *theWindow;
  124.    WICON *theIcon = &ICON;
  125.  
  126.    if (FirstWPtr && FIRSTWINDOW)
  127.    {
  128.       theWindow = FIRSTWINDOW;
  129.       while (theWindow && theWindow != WINDOW)
  130.       {
  131.          switch(type)
  132.          {
  133.             case COM_IMAGE:
  134.                theWindow->Icon.Image = theIcon->Image;
  135.                break;
  136.  
  137.             case COM_SELECT:
  138.                theWindow->Icon.Select = theIcon->Select;
  139.                break;
  140.  
  141.             case COM_MASK:
  142.                theWindow->Icon.Mask = theIcon->Mask;
  143.                break;
  144.  
  145.             case COM_FLAGS:
  146.                theWindow->Icon.Flags = theIcon->Flags;
  147.                break;
  148.  
  149.             case COM_POSITION:
  150.                theWindow->Icon.x = theIcon->x;
  151.                theWindow->Icon.y = theIcon->y;
  152.                break;
  153.  
  154.             case COM_NAME:
  155.                theWindow->Icon.Name = theIcon->Name;
  156.                break;
  157.          }
  158.          theWindow = theWindow->Next;
  159.       }
  160.    }
  161. }
  162.  
  163.  
  164. /*
  165.  *  CopyDefine()
  166.  *
  167.  *  If there is an icon to copy to,
  168.  *    Clear any images or character strings that were allocated previously
  169.  *    If there are windows to copy to,
  170.  *      For each window in the list, set the window's icon to the defined icon
  171.  *    Otherwise
  172.  *      Set the current icon to the specified icon
  173.  */
  174.  
  175. void CopyDefine(theDefine)
  176. ICONDEFINE *theDefine;
  177. {
  178.    ICONWINDOW *theWindow;
  179.  
  180.    if (IconPtr)
  181.    {
  182.       if (IconPtr->Flags & IW_IMAGE)  FreeImage(ICON.Image);
  183.       if (IconPtr->Flags & IW_SELECT) FreeImage(ICON.Select);
  184.       if (IconPtr->Flags & IW_MASK)   FreeImage(IconPtr->Mask);
  185.       if (IconPtr->Flags & IW_NAME)   FREECHAR(ICON.Name);
  186.       IconPtr->Flags &= ~IW_ALLOCED; IconPtr->Mask = NULL;
  187.  
  188.       if (FirstWPtr && FIRSTWINDOW)
  189.       {
  190.          theWindow = FIRSTWINDOW;
  191.          while (theWindow)
  192.          {
  193.             theWindow->Icon = theDefine->Icon;
  194.             theWindow = theWindow->Next;
  195.          }
  196.       } else {
  197.          ICON = theDefine->Icon;
  198.       }
  199.    }
  200. }
  201.  
  202.  
  203. /*
  204.  *  FindDefine()
  205.  *
  206.  *  Look through the list of defined icons for the given name.
  207.  *  Return NULL if not found, or a pointer to the definition if found.
  208.  */
  209.  
  210. ICONDEFINE *FindDefine(Name)
  211. char *Name;
  212. {
  213.    ICONDEFINE *theDefine = FirstDefine;
  214.    int NotFound = TRUE;
  215.  
  216.    while (theDefine && NotFound)
  217.    {
  218.       if (stricmp(Name,theDefine->Name) == 0) NotFound = FALSE;
  219.         else theDefine = theDefine->Next;
  220.    }
  221.    return(theDefine);
  222. }
  223.  
  224.  
  225. /*
  226.  *  NoIconError()
  227.  *
  228.  *  Give an error message
  229.  *  If there were windows expecting an icon,
  230.  *    Free each window in the list
  231.  *  If there was a screen name given
  232.  *    Free the screen structure and clear the pointers
  233.  *    If there was a program name given
  234.  *      Free the program structure and clear its pointers
  235.  *  If there was a DEFINE expecting an icon,
  236.  *    Free the define structure and clear the pointers
  237.  *  Clear the Icon pointer.
  238.  */
  239.  
  240. void NoIconError()
  241. {
  242.    ICONWINDOW *nextWindow;
  243.  
  244.    Expected("an Icon specification");
  245.    if (FirstWPtr && FIRSTWINDOW)
  246.    {
  247.       WindowPtr = FirstWPtr;
  248.       while (FIRSTWINDOW)
  249.       {
  250.           nextWindow = FIRSTWINDOW->Next;
  251.           FreeWindow(FIRSTWINDOW);
  252.           FIRSTWINDOW = nextWindow;
  253.       }
  254.       if (ScreenPtr && SCREEN && SCREEN->Window == NULL)
  255.       {
  256.          FreeScreen(SCREEN); SCREEN = NULL; WindowPtr = FirstWPtr = NULL;
  257.          if (PROGRAM && PROGRAM->Screen == NULL)
  258.             FreeProgram(PROGRAM), PROGRAM = NULL, ScreenPtr = NULL;
  259.       }
  260.    }
  261.    if (DEFINE) FreeDefine(DEFINE), DEFINE = NULL;
  262.    IconPtr = NULL;
  263. }
  264.  
  265.  
  266. /*
  267.  *  GetNewProgram()
  268.  *
  269.  *  If a definition was in progress, complete it
  270.  *  If there was a previous PROGRAM command, move along in the list
  271.  *  Get a new program structure if possible (error if not)
  272.  *  Set the screen pointer to the program's screen and set the program name
  273.  *  Clear the window and icon pointers.
  274.  */
  275.  
  276. void GetNewProgram()
  277. {
  278.    if (DEFINE)  DefinePtr  = &(DEFINE->Next);
  279.    if (PROGRAM) ProgramPtr = &(PROGRAM->Next);
  280.    if (NEWSTRUCT(IconProgram,PROGRAM))
  281.    {
  282.       ScreenPtr = &(PROGRAM->Screen);
  283.       PROGRAM->Name = NAME_ANY;
  284.    } else ShowError("Can't get memory for Program data"), ScreenPtr = NULL;
  285.    FirstWPtr = WindowPtr = NULL;
  286.    IconPtr = NULL; NoIcon = TRUE;
  287. }
  288.  
  289.  
  290. /*
  291.  *  GetNewScreen()
  292.  *
  293.  *  If there is no place to put the screen pointer, get a new program
  294.  *  If we have a place for the screen
  295.  *    If there is already a screen in progress, move on to the next one
  296.  *    Get a new screen structure if possible (error if not)
  297.  *    Set the window pointers and the screen's name
  298.  *  Clear the icon pointers
  299.  */
  300.  
  301. void GetNewScreen()
  302. {
  303.    if (ScreenPtr == NULL) GetNewProgram(); 
  304.    if (ScreenPtr)
  305.    {
  306.       if (SCREEN) ScreenPtr = &(SCREEN->Next);
  307.       if (NEWSTRUCT(IconScreen,SCREEN))
  308.          FirstWPtr = WindowPtr = &(SCREEN->Window),
  309.          SCREEN->Name = NAME_ANY;
  310.         else
  311.          ShowError("Can't get memory for Screen data"),
  312.          FirstWPtr = WindowPtr = NULL;
  313.    }
  314.    IconPtr = NULL; NoIcon = TRUE;
  315. }
  316.  
  317.  
  318. /*
  319.  *  GetNewWindow()
  320.  *
  321.  *  If there is no place to put the window, get a new screen
  322.  *  If there is a place for the window
  323.  *    If there is already a window in progress, move on to the next one
  324.  *    If there was an icon for the previous window, reset the beginning
  325.  *      of the window list (mutliple windows can be defined at once)
  326.  *    Get a new window structure, if possible (error if not)
  327.  *      Set the mask pointer and the window's name
  328.  *  Set the icon pointer to the current window and clear the icon flag.
  329.  */
  330.  
  331. void GetNewWindow()
  332. {
  333.    if (WindowPtr == NULL) GetNewScreen();
  334.    if (WindowPtr)
  335.    {
  336.       if (WINDOW) WindowPtr = &(WINDOW->Next);
  337.       if (NoIcon == FALSE) FirstWPtr = WindowPtr;
  338.       if (NEWSTRUCT(IconWindow,WINDOW))
  339.          MaskPtr = &(WINDOW->Mask),
  340.          WINDOW->Name = NAME_ANY;
  341.         else
  342.          ShowError("Can't get memory for Window & Icon data"),
  343.          MaskPtr = NULL;
  344.    }
  345.    IconPtr = WINDOW; NoIcon = TRUE;
  346. }
  347.  
  348.  
  349. /*
  350.  *  GetNewDefine()
  351.  *
  352.  *  If a program is in progress, complete it
  353.  *  If a define is in progress, complete it
  354.  *  Get a new define structure if possible (error if not)
  355.  *  Set the mask pointer for the define,
  356.  *  Set the icon pointer to the define strcuture, and clear the icon flag.
  357.  *  Clear the screen and window pointers.
  358.  */
  359.  
  360. void GetNewDefine()
  361. {
  362.    if (PROGRAM) ProgramPtr = &(PROGRAM->Next);
  363.    if (DEFINE)  DefinePtr  = &(DEFINE->Next);
  364.    if (NEWSTRUCT(IconDefine,DEFINE)) MaskPtr = &(DEFINE->Mask);
  365.       else ShowError("Can't get memory for Icon Definition"),
  366.            MaskPtr = NULL;
  367.    IconPtr = DEFINE; NoIcon = TRUE;
  368.    ScreenPtr = NULL; WindowPtr = FirstWPtr = NULL;
  369. }
  370.  
  371.  
  372. /*
  373.  *  CheckForIcon()
  374.  *
  375.  *  If there is no place for an icon to be put
  376.  *    If there was a PROGRAM command
  377.  *      Check if there was a SCREEN command but no WINDOW command
  378.  *        (ie, there is a place to put a window but no window yet)
  379.  *        if so, then this is a screen icon not a window icon
  380.  *      Get a window structure to hold the icon
  381.  *      If it is a screen icon and we have a place for it, set the window's
  382.  *        name to the special flag for screen icons
  383.  *    If we still don't have any place for an icon, return an error condition
  384.  *  Return "everything OK"
  385.  */
  386.  
  387. int CheckForIcon()
  388. {
  389.    int ScreenIcon = FALSE;
  390.  
  391.    if (IconPtr == NULL)
  392.    {
  393.       if (PROGRAM)
  394.       {
  395.          ScreenIcon = (WindowPtr && WINDOW == NULL);
  396.          GetNewWindow();
  397.          if (ScreenIcon && WINDOW) WINDOW->Name = SCREENICON;
  398.       }
  399.       if (IconPtr == NULL) return(TRUE);
  400.    }
  401.    return(FALSE);
  402. }
  403.  
  404.  
  405.  
  406. /*
  407.  *  ProgramCompare()
  408.  *
  409.  *  Compare the names of two programs to find which is first alphabetically.
  410.  *  NAME_ANY is considered to be before anything else, then NAME_NULL, then
  411.  *  standard alphabetical order.
  412.  *    Returns -1 if Prog1 before Prog2
  413.  *             0 if Prog1 equals Prog2
  414.  *             1 if Prog1 after  Prog2
  415.  */
  416.  
  417. static int ProgramCompare(Prog1,Prog2)
  418. ICONPROGRAM *Prog1,*Prog2;
  419. {
  420.    int result = 0;
  421.  
  422.    if (Prog1->Name != NAME_ANY)
  423.    {
  424.       if (Prog2->Name != NAME_ANY)
  425.       {
  426.          if (Prog1->Name && Prog2->Name)
  427.             result = stricmp(Prog1->Name,Prog2->Name);
  428.            else
  429.             if (Prog1->Name) result = 1; else if (Prog2->Name) result = -1;
  430.       } else result = 1;
  431.    } else if (Prog2->Name != NAME_ANY) result = -1;
  432.    return(result);
  433. }
  434.  
  435.  
  436. /*
  437.  *  SortPrograms()
  438.  *
  439.  *  Sort the program list using ProgramCompare to compare names
  440.  *  If the first program in the list is [ANY], then
  441.  *    Save it as ProgramAny and remove it from the list.
  442.  *  Convert the linked list into a balanced binary tree.
  443.  */
  444.  
  445. void SortPrograms()
  446. {
  447.    extern ICONPROGRAM *mSort(),*ListToTree();
  448.    extern void PrintList();
  449.    
  450.    FirstProgram = mSort(FirstProgram,ProgramCompare,NULL);
  451.    if (FirstProgram && FirstProgram->Name == NAME_ANY)
  452.    {
  453.       ProgramAny = FirstProgram;
  454.       FirstProgram = FirstProgram->Next;
  455.       if (FirstProgram) FirstProgram->Prev = NULL;
  456.       ProgramAny->Next = NULL;
  457.    }
  458.    FirstProgram = ListToTree(FirstProgram,0);
  459.    #ifdef DEBUG
  460.       PrintList(FirstProgram);
  461.       PrintList(ProgramAny);
  462.    #endif
  463. }
  464.  
  465.  
  466. #ifdef DEBUG
  467.  
  468. /*
  469.  *  PrintList()
  470.  *
  471.  *  For each program in the list,
  472.  *    Print its name
  473.  *    For each of its screens
  474.  *      Print the screen name
  475.  *      For each of the screen's windows
  476.  *        Print the window's name
  477.  */
  478.  
  479. static void PrintList(theProgram)
  480. ICONPROGRAM *theProgram;
  481. {
  482.    ICONSCREEN *theScreen;
  483.    ICONWINDOW *theWindow;
  484.    #define NAME(p)\
  485.       ((p->Name)?((p->Name == NAME_ANY)? "[Any]":\
  486.                  ((p->Name == SCREENICON)? "[Screen Icon]": p->Name)): "[Null]")
  487.    
  488.    while (theProgram)
  489.    {
  490.       printf(">>  '%s'\n",NAME(theProgram));
  491.       theScreen = theProgram->Screen;
  492.       while (theScreen)
  493.       {
  494.          printf(">>    '%s'\n",NAME(theScreen));
  495.          theWindow = theScreen->Window;
  496.          while (theWindow)
  497.          {
  498.             printf(">>      '%s'\n",NAME(theWindow));
  499.             theWindow = theWindow->Next;
  500.          }
  501.          theScreen = theScreen->Next;
  502.       }
  503.       theProgram = theProgram->Next;
  504.    }
  505. }
  506. #endif
  507.