home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bricon20.zip / bricons / main.c < prev    next >
C/C++ Source or Header  |  1992-07-31  |  9KB  |  296 lines

  1. static char* sccs_main_c = "%W%%G%"; 
  2.  
  3. /* 
  4.  * Copyright 1992 Bruce. R. Ingram.
  5.  * University of Kent at Canterbury, UK
  6.  *
  7.  * Permission  to   use, copy, modify  and  distribute  this  software  and its
  8.  * documentation   for any purpose is hereby granted without fee, provided that
  9.  * the   above  copyright  notice  appear  in  all  copies  and  that both that
  10.  * copyright  notice   and  this   permission   notice  appear  in   supporting
  11.  * documentation,  and  that  the name  of the author and The University  of
  12.  * Kent  at  Canterbury  not be  used in advertising or publicity pertaining to
  13.  * distribution  of  the  software  without specific, written prior permission.
  14.  * The   author  and   The   University   of   Kent at   Canterbury   make  no
  15.  * representations  about  the  suitability  of this  software for any purpose.
  16.  * It is provided "as is" without express or implied warranty.
  17.  *
  18.  * THE   AUTHOR  AND  THE  UNIVERSITY  OF KENT  AT   CANTERBURY  DISCLAIMS ALL
  19.  * WARRANTIES  WITH  REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED  WARRANTIES
  20.  * OF   MERCHANTABILITY  AND  FITNESS,  IN NO  EVENT  SHALL THE AUTHOR  OR THE
  21.  * UNIVERSITY  OF KENT  AT  CANTERBURY  BE LIABLE FOR ANY SPECIAL, INDIRECT  OR
  22.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS  OF USE,
  23.  * DATA  OR  PROFITS,  WHETHER  IN  AN ACTION OF CONTRACT, NEGLIGENCE  OR OTHER
  24.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  25.  * OF THIS SOFTWARE.
  26.  *
  27.  * Author:
  28.  *
  29.  * Mr  B. R. Ingram email bri@ukc.ac.uk
  30. */
  31.  
  32. #include "gen.h"
  33. #include "widgets.h"
  34.  
  35.  
  36. /*  Forward  references  */
  37.  
  38. extern    void    GetPath PROTO((char *));
  39. extern    void    PositionButtons PROTO((int,int)), ManageButtons();
  40. extern  void    set_popup PROTO((Widget,XEvent *,String *,Cardinal *));
  41. extern    void    delay PROTO((Widget,XEvent *,String *,Cardinal *));
  42. extern    void    Quit PROTO((Widget,XtPointer,XtPointer));
  43. static    void    AddResource PROTO((char *, char *, int));
  44. extern    int    ProcessFile PROTO((Info*, char*));
  45.  
  46. #include "trans.h"
  47.  
  48. /*
  49.     We set various default resources for the various widgets.
  50. */
  51. String fallback_resources[] = {
  52. "*borderColor:        black",
  53. "*box.background:    gray",
  54. NULL,
  55. };
  56.  
  57.  
  58. /*
  59.     We store our application's resources in a structure of 
  60.     the following type.
  61. */
  62. typedef struct
  63. {
  64.     int     no_of_cols;
  65.     char    *icon_file;
  66.     char    *help_file;
  67.     char    *icon_dirs;
  68.     Boolean    sensitive;
  69.     Boolean    default_buttons;
  70. } AppData, *AppDataPtr;
  71.  
  72.  
  73. /* The description of our application's resources. */
  74. static XtResource resources[] = {
  75.     { "no_of_cols", "No_of_cols",XtRInt,sizeof (int),
  76.       XtOffset(AppDataPtr,no_of_cols),XtRString,"18" },
  77.     { "icon_file", "Icon_file", XtRString, sizeof(char *),
  78.       XtOffset(AppDataPtr,icon_file),XtRString,"./.briconsrc" },
  79.     { "help_file", "Help_file", XtRString, sizeof(char *),
  80.       XtOffset(AppDataPtr,help_file),XtRString,
  81.     "bricons.txt" },
  82.     { "icon_dirs", "Icon_dis", XtRString, sizeof(char *),
  83.       XtOffset(AppDataPtr,icon_dirs), XtRString, "" },
  84.     { "sensitive", "Sensitive", XtRBoolean, sizeof(Boolean),
  85.     XtOffset(AppDataPtr,sensitive),XtRString,"True" },
  86.     { "default_buttons", "Default_buttons", XtRBoolean, sizeof(Boolean),
  87.     XtOffset(AppDataPtr, default_buttons),XtRString,"True" },
  88.  
  89. };
  90.  
  91. /* Reading command line options. */
  92. XrmOptionDescRec options[] = {
  93.     { "-col", "*no_of_cols", XrmoptionSepArg, NULL },
  94.     { "-file", "*icon_file", XrmoptionSepArg, NULL },
  95.     { "-help", "*help_file", XrmoptionSepArg, NULL },
  96.     { "-bmdir", "*icon_dirs", XrmoptionSepArg, NULL },
  97.     { "-sensitive", "sensitive", XrmoptionSepArg, NULL },
  98.     { "-default", "default_buttons", XrmoptionSepArg, NULL }
  99. };
  100.  
  101.  
  102. XtAppContext    app_context;
  103.  
  104. int 
  105. main(argc,argv)
  106. unsigned int argc;
  107. char **argv;
  108. {
  109.     int        string_len = 0;
  110.     char        bm_dirs[MAXPATHLEN];
  111.     char        *editor_type = NULL;
  112.     Boolean        default_editor = False;
  113.     Info        *data;    
  114.     AppData        Rdata;
  115.  
  116.     /* create a shell widget for this application */
  117.     toplevel = XtVaAppInitialize(
  118.                 &app_context,           /* Application context */
  119.                 "Xbricons",             /* Application class name */
  120.                 options,         /* Command line option list */
  121.         XtNumber(options),  
  122.                 &argc, argv,            /* Command line args */
  123.         /*fallback_resources,       use fallback_resources or */
  124.         NULL,             /* app_defaults file */
  125.                 NULL);            /* terminate varargs list */    
  126.  
  127.     /* Retrieve the application resources */
  128.     XtVaGetApplicationResources(toplevel,&Rdata,resources,
  129.             XtNumber(resources),NULL);
  130.  
  131.     if(strlen(Rdata.icon_dirs) == 0)
  132.     {
  133.         (void)getcwd(bm_dirs, MAXPATHLEN);
  134.         AddResource(argv[0], bm_dirs, True);
  135.     }
  136.     else
  137.         AddResource(argv[0], Rdata.icon_dirs, False); 
  138.  
  139.     /* add the menu actions to the global action list */
  140.     XtAppAddActions(app_context,(XtActionList) menu_actions,
  141.                 XtNumber(menu_actions));
  142.  
  143.     /* create a form widget which is used to hold all the buttons */
  144.     box = XtVaCreateManagedWidget("box",    /* widget name */
  145.         formWidgetClass,        /* widget class */
  146.         toplevel,            /* parent widget */
  147.         NULL);                /* terminate varargs list */
  148.  
  149. /*
  150.     allocate some memory for data structure holding number of buttons,
  151.     the name of the icon file etc.
  152. */
  153.     data = (struct info *) malloc(sizeof(struct info));
  154.     if(data == NULL)
  155.     {
  156.         (void) fprintf(stderr,"%s: out of memory when creating Info data structure. In function main\n",argv[0]);
  157.         exit(1);
  158.     }    
  159.  
  160.     /* find out what editor the user is using */
  161.     editor_type = getenv("EDITOR");
  162.     if(!editor_type)
  163.         default_editor = True;
  164.     else if(strcmp(editor_type, "vi") == 0)
  165.         default_editor = True;
  166.     if(default_editor)
  167.     {
  168.         if((data->editor = malloc(strlen(EDITOR) + 4)) == NULL)
  169.         {
  170.             fprintf(stderr,"%s: malloc out of memory\n",argv[0]);
  171.             exit(1);
  172.         }
  173.         strcpy(data->editor, EDITOR);
  174.         strcat(data->editor, "vi ");
  175.     }
  176.     else
  177.     {
  178.         if((data->editor = malloc(strlen(editor_type) + 2)) == NULL)
  179.         {
  180.             fprintf(stderr,"%s: malloc out of memory\n",argv[0]);
  181.             exit(1);
  182.         }
  183.         strcpy(data->editor, editor_type);
  184.         strcat(data->editor, " ");
  185.     }    
  186.             
  187.  
  188.     string_len = 0;
  189.     string_len = strlen(Rdata.icon_file);
  190.     if((data->icon_file = malloc(strlen(Rdata.icon_file) +1)) == NULL)
  191.     {
  192.         (void) fprintf(stderr,"%s: out of memory reading icon file name.  In function main\n",argv[0]);
  193.         exit(1);
  194.     }
  195.     strcpy(data->icon_file,Rdata.icon_file);
  196.  
  197.     string_len = 0;
  198.     if((data->help_file = malloc(strlen(Rdata.help_file) +1)) == NULL)
  199.     {
  200.         (void) fprintf(stderr,"%s: out of memory reading icon file name.  In function main\n",argv[0]);
  201.         exit(1);
  202.     }
  203.     strcpy(data->help_file,Rdata.help_file);
  204.  
  205.     data->no_of_columns = Rdata.no_of_cols;
  206.  
  207.     data->sensitive = Rdata.sensitive;
  208.     data->default_buttons = Rdata.default_buttons;
  209.  
  210.     /* read contents of icon file & create buttons */
  211.     data->no_of_buttons = ProcessFile(data, argv[0]);
  212.  
  213.     /* position buttons according to number of columns */
  214.     PositionButtons(data->no_of_buttons, data->no_of_columns); 
  215.  
  216.     ManageButtons(); 
  217.  
  218.     /* create windows for widgets and map them. */
  219.     XtRealizeWidget(toplevel);
  220.  
  221.     /* loop for events */
  222.     XtAppMainLoop(app_context);
  223.     return(0);
  224. }
  225.  
  226.  
  227.  
  228. static    void
  229. AddResource(prog_name, bmap_dirs, default_bm)
  230. char *prog_name;
  231. char    *bmap_dirs;
  232. Boolean    default_bm;
  233. {
  234.     char        *getcwd();
  235.  
  236.     int        string_len, len;
  237.     char        *bitmap_dir;    
  238.     char        tmp_string[300];
  239.     char        *res_name_ptr;
  240.     char        current_dir[MAXPATHLEN];
  241.     char        *dummy;
  242.     XrmDatabase    resource_db;
  243.     XrmDatabase    *dbPtr;
  244.     XrmValue    value;
  245.     XrmValue    *vPtr;
  246.     Bool        found;
  247.  
  248.     resource_db = XtDatabase(XtDisplay(toplevel));
  249.  
  250.  
  251.     string_len = strlen(RESOURCE_NAME);
  252.     string_len += strlen(RESOURCE_VALUE);
  253.     string_len += strlen(bmap_dirs);
  254.     res_name_ptr = malloc((string_len) + 1);
  255.     if(res_name_ptr == NULL)
  256.     {
  257.         (void) fprintf(stderr,"\nOut of memory error\n");
  258.         exit(1);
  259.     }
  260.  
  261.     (void) sprintf(tmp_string,"%s.bitmapFilePath",prog_name);
  262.     found = XrmGetResource(resource_db, tmp_string, 
  263.             "Xbricons.BitmapFilePath", &dummy, &value);
  264.     dbPtr = &resource_db;
  265.     vPtr = &value;
  266.     if(!found)
  267.     {
  268.         strcpy(res_name_ptr,RESOURCE_NAME);
  269.         strcat(res_name_ptr,bmap_dirs);
  270.         if(default_bm)
  271.             strcat(res_name_ptr,RESOURCE_VALUE);
  272.         (void) fprintf(stderr,"\n.bitmapFilePath not defined in resource file");
  273.         (void) fprintf(stderr,"\nAdding default: %s\n",res_name_ptr);
  274.         XrmPutLineResource(dbPtr, res_name_ptr); 
  275.     }
  276.     else
  277.     {
  278.         len  = strlen(vPtr->addr);
  279.         len += strlen(bmap_dirs);
  280.         len += strlen(RESOURCE_NAME);
  281.         len += strlen(RESOURCE_VALUE);
  282.         bitmap_dir = malloc(len + 4);
  283.         if(bitmap_dir == NULL)
  284.         {
  285.             (void) fprintf(stderr,"\nOut of memory error\n");
  286.             exit(1);
  287.         }
  288.         strcpy(bitmap_dir, RESOURCE_NAME);
  289.         strcat(bitmap_dir,vPtr->addr);
  290.         strcat(bitmap_dir,":");
  291.         strcat(bitmap_dir, bmap_dirs);
  292.         strcat(bitmap_dir, RESOURCE_VALUE);
  293.         XrmPutLineResource(dbPtr, bitmap_dir); 
  294.     }
  295. }
  296.