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

  1. /*
  2.  *  WICONIFYWINDOW  A companion utility to wIconify that allows you
  3.  *                  to iconify any window via a CLI command, and modify
  4.  *                  its icon flags.
  5.  *
  6.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  7.  *  You may use this code, provided this copyright notice is kept intact.
  8.  */
  9.  
  10. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  11. #include <intuition/intuitionbase.h>
  12. #include "wIcon.h"
  13.  
  14. #define USAGE\
  15.    "wIconifyWindow [window [screen]] [[-]NOCLOSE] [[-]NOMOVE] [[-]LOCKED]\n   \
  16.     [[-]NOSAVEPOS] [[-]CHANGEREFRESH] [[-]NOMULTISELECT] [[-]NOORGANIZE]"
  17.  
  18. #define DEFAULTFLAGS    0
  19.  
  20. static char *program = "wIconifyWindow";
  21. static char *version = "v1.2";
  22. static char *copyright =
  23.    "Copyright (C) 1990 by Davide P. Cervone, all rights reserved.";
  24.  
  25.  
  26. #define INTUITION_REV   0L
  27. extern struct IntuitionBase *IntuitionBase;
  28. extern struct IntuitionBase *OpenLibrary();
  29.  
  30. static WICON theIcon;               /* Definition of the icon to use */
  31. static ULONG Flags = DEFAULTFLAGS;  /* The flags to use */
  32. static ULONG NoFlags = 0;           /* Have flags been specified? */
  33. static WORD  x,y;                   /* The icon's position */
  34. static char *WindowName = NULL;     /* The window's name */
  35. static char *ScreenName = NULL;     /* the window's screen's name */
  36.  
  37.  
  38. #define ARGMATCH(s)     (stricmp(*argv,s) == 0)
  39. #define TOUPPER(c)      (((c)>='a'&&(c)<='z')?(c)-'a'+'A':c)
  40. #define NOFLAGSYET\
  41.    (Flags == DEFAULTFLAGS && NoFlags == 0 && x == 0 && y == 0)
  42.  
  43.  
  44.  
  45. /*
  46.  *  Print()
  47.  *
  48.  *  Find the standard AmigaDOS output file and write the output string
  49.  *  to the output file.  This is intended as a substitute for printf()
  50.  *  when no formatting is required, and when you want a small executable.
  51.  */
  52.  
  53. static void Print(s)
  54. char *s;
  55. {
  56.    ULONG OutFile;
  57.    extern ULONG Output();
  58.    
  59.    OutFile = Output();
  60.    if (OutFile && s) Write(OutFile,s,strlen(s));
  61. }
  62.  
  63.  
  64. /*
  65.  *  Error()
  66.  *
  67.  *  Print the character string, and up to two optional strings, then
  68.  *  go to a new output line, close Intuition, and exit with an error.
  69.  */
  70.  
  71. static void Error(s,x1,x2)
  72. char *s,*x1,*x2;
  73. {
  74.    Print(s);
  75.    if (x1) Print(x1);
  76.    if (x2) Print(x2);
  77.    Print("\n");
  78.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  79.    _exit(10L);
  80. }
  81.  
  82.  
  83. /*
  84.  *  ParseArguments()
  85.  *
  86.  *  While there are more arguments to consider,
  87.  *    Move to the next one
  88.  *    If it matches something we're looking for, then add the flag to
  89.  *      the Flags or NoFlags masks.
  90.  *    If the argument is "AT", then
  91.  *      If the next arguement is an X,Y pair, save their values,
  92.  *      Otherwise, give an error message.
  93.  *      Skip the next argument in either case.
  94.  *    If there were no flags given and no window name has been supplied,
  95.  *      save the argument as the window name,
  96.  *    If there were no flags and no screen title, then save the arguement
  97.  *       as the screen title.
  98.  *    Otherwise, report an error.
  99.  *  Return TRUE if all parameters were OK, and FALSE otherwise.
  100.  */
  101.  
  102. static int ParseArguments(argc,argv)
  103. int argc;
  104. char **argv;
  105. {
  106.    int status = TRUE;
  107.    int X,Y;
  108.    
  109.    while (--argc)
  110.    {
  111.       argv++;
  112.       if (ARGMATCH("NOCLOSE"))       Flags |= WI_NOCLOSE; else
  113.       if (ARGMATCH("NOMOVE"))        Flags |= WI_NOMOVE; else
  114.       if (ARGMATCH("NOORGANIZE"))    Flags |= WI_NOORGANIZE; else
  115.       if (ARGMATCH("LOCKED"))        Flags |= WI_LOCKED; else
  116.       if (ARGMATCH("NOSAVEPOS"))     Flags |= WI_NOSAVEPOS; else
  117.       if (ARGMATCH("CHANGEREFRESH")) Flags |= WI_CHANGEREFRESH; else
  118.       if (ARGMATCH("NOMULTISELECT")) Flags |= WI_NOMULTISELECT; else
  119.  
  120.       if (ARGMATCH("-NOCLOSE"))       NoFlags |= WI_NOCLOSE; else
  121.       if (ARGMATCH("-NOMOVE"))        NoFlags |= WI_NOMOVE; else
  122.       if (ARGMATCH("-NOORGANIZE"))    NoFlags |= WI_NOORGANIZE; else
  123.       if (ARGMATCH("-LOCKED"))        NoFlags |= WI_LOCKED; else
  124.       if (ARGMATCH("-NOSAVEPOS"))     NoFlags |= WI_NOSAVEPOS; else
  125.       if (ARGMATCH("-CHANGEREFRESH")) NoFlags |= WI_CHANGEREFRESH; else
  126.       if (ARGMATCH("-NOMULTISELECT")) NoFlags |= WI_NOMULTISELECT; else
  127.  
  128.       if (ARGMATCH("AT"))
  129.       {
  130.          if (argc > 1 && sscanf(*(argv+1),"%ld,%ld",&X,&Y) == 2)
  131.              x = X, y = Y;
  132.             else
  133.              Print("The AT directive must be of the form 'AT x,y'\n"),
  134.              status = FALSE;
  135.          if (argc > 1) argc--, argv++;
  136.       } else
  137.       if (NOFLAGSYET && WindowName == NULL) WindowName = *argv; else
  138.       if (NOFLAGSYET && ScreenName == NULL) ScreenName = *argv;
  139.       else
  140.       {
  141.          Print("Unrecognized flag '");
  142.          Print(*argv);
  143.          Print("'\n");
  144.          status = FALSE;
  145.       }
  146.    }
  147.    return(status);
  148. }
  149.  
  150.  
  151. /*
  152.  *  PrefixMatch()
  153.  *
  154.  *  Case-insensitive prefix match.  NULL pointers and empty strings
  155.  *  only match themselves, and are not considered prefixes to any string.
  156.  *
  157.  *  Return <0 if the first is smaller than the second,
  158.  *         =0 if the first is a prefix of the second,
  159.  *         >1 if the first is larger than the second.
  160.  */
  161.  
  162. int PrefixMatch(s1,s2)
  163. char *s1,*s2;
  164. {
  165.    int match = -1;
  166.    
  167.    if (s1 && *s1)
  168.    {
  169.       if (s2)
  170.       {
  171.          while (TOUPPER(*s1) == TOUPPER(*s2) && *s2 != 0) s1++,s2++;
  172.          if (*s1 == 0) match = 0; else match = *s1 - *s2;
  173.       } else match = 1;
  174.    } else if (s2 == NULL || *s2 == 0) match = 0;
  175.    return(match);
  176. }
  177.  
  178.  
  179. /*
  180.  *  *FindScreen()
  181.  *
  182.  *  Lock IntuitionBase so nothing happens while we look.
  183.  *  If there is a name to look for,
  184.  *    Start at the first screen, and look for a screen whose title
  185.  *      matches the given name.  If none, return NULL.
  186.  *  Otherwise, use the active screen.
  187.  *  Unlock Intuition.
  188.  *  Return the screen pointer found, if any.
  189.  */
  190.  
  191. static struct Screen *FindScreen(ScreenName)
  192. char *ScreenName;
  193. {
  194.    struct Screen *theScreen;
  195.    long ILock, LockIBase();
  196.  
  197.    ILock = LockIBase(0L);
  198.    if (ScreenName)
  199.    {
  200.       theScreen = IntuitionBase->FirstScreen;
  201.       while (theScreen && PrefixMatch(ScreenName,theScreen->Title))
  202.          theScreen = theScreen->NextScreen;
  203.    } else {
  204.       theScreen = IntuitionBase->ActiveScreen;
  205.    }
  206.    UnlockIBase(ILock);
  207.    return(theScreen);
  208. }
  209.  
  210.  
  211. /*
  212.  *  *FindWindow()
  213.  *
  214.  *  Lock Intuition so nothing happens while we look.
  215.  *  If there is a window name given,
  216.  *    Start with the first window on the screen and look through
  217.  *      the window list for one that matches the given name.
  218.  *      Return NULL if none found.
  219.  *  Otherwise, use the active window.
  220.  *  Unlock Intuition.
  221.  *  Return the window pointer, if any.
  222.  */
  223.  
  224. static struct Window *FindWindow(WindowName,theScreen)
  225. char *WindowName;
  226. struct Screen *theScreen;
  227. {
  228.    struct Window *theWindow;
  229.    long ILock, LockIBase();
  230.  
  231.    ILock = LockIBase(0L);
  232.    if (WindowName)
  233.    {
  234.       theWindow = theScreen->FirstWindow;
  235.       while (theWindow && PrefixMatch(WindowName,theWindow->Title))
  236.          theWindow = theWindow->NextWindow;
  237.    } else {
  238.       theWindow = IntuitionBase->ActiveWindow;
  239.    }
  240.    UnlockIBase(ILock);
  241.    return(theWindow);
  242. }
  243.  
  244.  
  245. /*
  246.  *  main()
  247.  *
  248.  *  If all the parameters are OK,
  249.  *  Open Intuition.
  250.  *  Find the specified screen.
  251.  *  Find the specified window.
  252.  *  If wIconify is runnging
  253.  *    If the window is iconified, de-iconify it,
  254.  *    Otherwise
  255.  *      Get the window's icon's current values,
  256.  *      Clear the NoFlags, and add the Flags specified by the user,
  257.  *      If a position was given, set it
  258.  *      Update the icon to the new values.
  259.  *      Iconify the window.
  260.  *  Otherwise give appropriate errors.
  261.  */
  262.  
  263. void main(argc,argv)
  264. int argc;
  265. char *argv[1];
  266. {
  267.    struct Screen *theScreen;
  268.    struct Window *theWindow = NULL;
  269.    WICONREF *wIconOf();
  270.  
  271.    if (ParseArguments(argc,argv))
  272.    {
  273.       IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
  274.       if (IntuitionBase)
  275.       {
  276.          theScreen = FindScreen(ScreenName);
  277.          if (theScreen)
  278.          {
  279.             theWindow = FindWindow(WindowName,theScreen);
  280.             if (theWindow)
  281.             {
  282.                if (wIconifyActive())
  283.                {
  284.                   if (wIsIconified(theWindow))
  285.                   {
  286.                      wRestore(wIconOf(theWindow));
  287.                   } else {
  288.                      wGetIconData(&theIcon,wIconOf(theWindow));
  289.                      theIcon.Flags &= ~NoFlags;
  290.                      theIcon.Flags |= Flags;
  291.                      if (x || y)
  292.                      {
  293.                         theIcon.x = x;
  294.                         theIcon.y = y;
  295.                      }
  296.                      wSetIcon(theWindow,&theIcon);
  297.                      wIconify(theWindow);
  298.                   }
  299.                } else Error("wIconify not running or incompatible version",
  300.                              NULL,NULL);
  301.             } else Error("Can't find window '",WindowName,"'");
  302.          } else Error("Can't find screen '",ScreenName,"'");
  303.          CloseLibrary(IntuitionBase);
  304.       } else Error("Can't Open Intuition Library!",NULL,NULL);
  305.    } else Error("Usage:  ",USAGE);
  306. }
  307.