home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xbmbrowser / part01 / xbmbrowser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-28  |  8.9 KB  |  317 lines

  1. char copyright[]="xbmbrowser Version 1.0  (c) Copyright Ashley Roll, 1992.";
  2. /*
  3. *****************************************************************************
  4. ** xbmbrowser version 1.0  (c) Copyright Ashley Roll, 1992.
  5. ** FILE: xbmbrowser.c
  6. **
  7. ** xbmbrowser is Public Domain. However it, and all the code still belong to me.
  8. ** I do, however grant permission for you to freely copy and distribute it on 
  9. ** the condition that this and all other copyright notices remain unchanged in 
  10. ** all distributions.
  11. **
  12. ** This software comes with NO warranty whatsoever. I therefore take no
  13. ** responsibility for any damages, losses or problems that the program may 
  14. ** cause.
  15. *****************************************************************************
  16. */
  17.  
  18. #define MAIN
  19. #include "xbmbrowser.h"
  20. #include "patchlevel.h"
  21. #include "icon.xbm"
  22.  
  23.  
  24. extern void DoQuit();
  25. extern void Ok();
  26. extern Widget MakeMenu();
  27. extern Widget AddMenuItem();
  28. extern void Show_Help();
  29. extern void Inform_User();
  30. extern void SetWindowIcon();
  31. extern void rename_bitmap();
  32. extern void move_bitmap();
  33. extern void copy_bitmap();
  34. extern void delete_bitmap();
  35. extern void edit_bitmap();
  36. extern void set_root();
  37. extern void set_root_inv();
  38. extern void set_name();
  39. extern void destroy_Callback();
  40. extern void rescan();
  41. extern FileList *get_files();
  42. extern void change_dir();
  43. extern void setup_dialog();
  44.  
  45. /* -------------------------- */
  46. static Atom  wm_delete_window;      /* insure that delete window works */
  47.  
  48. static XtActionsRec  actions[] = {  /* declare possible actions */
  49. /* action_name, routine */
  50.   { "quit",   DoQuit  },
  51.   { "CD", change_dir },
  52.   { "Set_Name", set_name },
  53.   { "Ok", Ok },
  54. };
  55.  
  56. /* translation table for label widgets for the bitmaps */
  57. static char Translations[] = 
  58.   "<EnterWindow>:      Set_Name() \n\
  59.    <BtnDown>:    XawPositionSimpleMenu(bitmapMenu) MenuPopup(bitmapMenu) \n\
  60.    <BtnUp>:      MenuPopdown(bitmapMenu)";
  61.  
  62. /* translation table for the dialogWidget (directory name) */
  63. static char text_trans[] = 
  64.   "<Key>Return:  CD() \n\
  65.    Ctrl<Key>M:   CD() ";
  66.  
  67. /* fallback resources  */
  68. static char *fall_back[] = {
  69.    ".xbmbrowser.width: 450", 
  70.    ".xbmbrowser.height: 500", 
  71.    ".xbmbrowser.iconName: browser",
  72.    ".xbmbrowser.?.TransientShell.width: 350",
  73.    ".xbmbrowser.main.buttons.Command.width: 70",
  74.    NULL
  75. }; 
  76.  
  77. /* -------------------------- */
  78.  
  79. /*
  80. ** this creates all the menubuttons that contain the bitmaps
  81. */
  82. void fill_bw()
  83. {
  84. FileList *x;
  85. Widget w;
  86. char b_name[255];
  87. Pixmap bitmap = NULL;
  88. int show = TRUE;
  89.  
  90.     /* create a menu */
  91.     menu = MakeMenu(bw,"bitmapMenu",NULL);
  92.  
  93.     /* fill the menu */
  94.   (void) AddMenuItem(menu,"Rename",rename_bitmap,NULL,show);
  95.   (void) AddMenuItem(menu,"Copy",copy_bitmap,NULL,show);
  96.   (void) AddMenuItem(menu,"Delete",delete_bitmap,NULL,show);
  97.   if(EDITOR[0] == '\0')
  98.     show = FALSE;
  99.   (void) AddMenuItem(menu,"Edit",edit_bitmap,NULL,show);
  100.  
  101.   show = TRUE;
  102.   if(SETROOT[0] == '\0')
  103.     show = FALSE;
  104.   (void) AddMenuItem(menu,"SetRoot",set_root,NULL,show);
  105.   show = TRUE;
  106.   if(SETROOTINV[0] == '\0')
  107.     show = FALSE;
  108.   (void) AddMenuItem(menu,"SetRoot Inv",set_root_inv,NULL,show);
  109.  
  110.  
  111.   for(x = file_list; x != NULL; x = x->next)
  112.   {
  113.   int t;  
  114.   char *name;
  115.  
  116.     name = x->fname; 
  117.     if(XReadBitmapFile(XtDisplay(toplevel),
  118.        DefaultRootWindow(XtDisplay(toplevel)),name,
  119.        &t,&t,&bitmap,&t,&t) == BitmapSuccess) {
  120.  
  121.  
  122.       /* create the menuButton for it */
  123.       w = XtVaCreateManagedWidget(name,labelWidgetClass,bw,
  124.              XtNbitmap,(XtArgVal)bitmap,
  125.              NULL);
  126.  
  127.       XtOverrideTranslations(w,XtParseTranslationTable(Translations));
  128.       XtAddCallback(w,"destroyCallback",destroy_Callback,(XtPointer)bitmap);
  129.     }
  130.   }
  131. }
  132.  
  133. main(argc, argv)
  134.   int argc;
  135.   char **argv;
  136. {
  137. XtAppContext appcon;
  138. char wname[100];
  139.  
  140. ac = argc;
  141. av = argv;
  142. bw = NULL;
  143. bname = NULL;
  144. dialogs_made = FALSE;
  145. file_list = NULL;
  146.  
  147.  
  148.   sprintf(wname,"XbmBrowser Version 1.0 %s",PATCHLEVEL);
  149.   toplevel = XtVaAppInitialize(
  150.                  &appcon, "XbmBrowser",    /* app context, ClassName */
  151.                  NULL, 0,                  /* app command line options */
  152.                  &argc, argv,              /* command line */
  153.                  fall_back,                /* Fall back resources */
  154.                  XtNtitle,(XtArgVal)wname,
  155.                  NULL);                    /* End Va resource list */
  156.   XtAppAddActions(appcon, actions, XtNumber(actions));
  157.  
  158.   if( argc > 2 )  useage();
  159.   if(argc == 2) {
  160.     if(chdir(argv[1]) != 0) {
  161.       fprintf(stderr,"xbmbrowser: couldn't chdir to '%s'\n",argv[1]);
  162.       exit(0);
  163.     }
  164.   }
  165.  
  166.   (void) getcwd(dname,253);
  167.  
  168. /* create a paned widget to put everything into */
  169.   mainpw  = XtVaCreateManagedWidget("main",panedWidgetClass,toplevel,
  170.             NULL);
  171.  
  172. /* create the form widget to put the buttons in */
  173.   bfw = XtVaCreateManagedWidget("buttons",formWidgetClass,mainpw,
  174.               XtNshowGrip,(XtArgVal)False,
  175.               XtNskipAdjust,(XtArgVal)True,NULL);
  176.  
  177. {
  178. Widget button;
  179. /* create the Buttons */
  180.   button = XtVaCreateManagedWidget("quit",commandWidgetClass,bfw,
  181.                   XtNlabel,(XtArgVal)"Quit",NULL);
  182.   XtAddCallback(button,"callback",DoQuit,NULL);
  183.   button = XtVaCreateManagedWidget("help",commandWidgetClass,bfw,
  184.                   XtNfromHoriz,(XtArgVal)button,
  185.                   XtNlabel,(XtArgVal)"Help",NULL);
  186.   XtAddCallback(button,"callback",Show_Help,NULL);
  187.   button = XtVaCreateManagedWidget("rescan",commandWidgetClass,bfw,
  188.                   XtNfromHoriz,(XtArgVal)button,
  189.                   XtNlabel,(XtArgVal)"Rescan",NULL);
  190.   XtAddCallback(button,"callback",rescan,NULL);
  191. }
  192. /* dialogWidget to hold and get the directory to read from */
  193.  
  194.   atw = XtVaCreateManagedWidget("directory", dialogWidgetClass, mainpw,
  195.            XtNlabel,(XtArgVal)"Current Directory",
  196.            XtNvalue,(XtArgVal)dname,
  197.            XtNshowGrip,(XtArgVal)False,
  198.            XtNskipAdjust,(XtArgVal)True,
  199.            NULL);
  200.  
  201.   XtOverrideTranslations(XtNameToWidget(atw,"value"),
  202.                          XtParseTranslationTable(text_trans));
  203.  
  204. /* label widget to hold the name of the current bitmap */
  205.   lw = XtVaCreateManagedWidget("label",labelWidgetClass,mainpw,
  206.                   XtNlabel,(XtArgVal)"No Bitmap Selected",
  207.                   XtNshowGrip,(XtArgVal)False,
  208.                   XtNskipAdjust,(XtArgVal)True,NULL);
  209.  
  210. /* create a viewport widget to stick the bitmaps in */
  211. {
  212. int width;
  213.  
  214.   XtVaGetValues(toplevel,XtNwidth,&width,NULL);
  215.  
  216.   phw = XtVaCreateManagedWidget("viewport",viewportWidgetClass,mainpw,
  217.                   XtNwidth,(XtArgVal)width,
  218.                   XtNallowHoriz,(XtArgVal)False,
  219.                   XtNallowVert,(XtArgVal)True,
  220.                   XtNshowGrip,(XtArgVal)False,
  221.                   XtNskipAdjust,(XtArgVal)True,NULL);
  222. }
  223.  setup();
  224.  XtRealizeWidget(toplevel);
  225.  
  226. /* set up the icon */
  227. {
  228. Pixmap  icon;
  229. XWMHints *wmhints;
  230. Display *display;
  231. Window  window;
  232.  
  233.    display = XtDisplay(toplevel);
  234.    window = XtWindow(toplevel);
  235.  
  236.    wmhints = XGetWMHints( display, window );
  237.  
  238.    if ( wmhints == (XWMHints *) NULL )
  239.    {
  240.       wmhints = (XWMHints *) malloc( sizeof(XWMHints) );
  241.       if ( wmhints == (XWMHints *) NULL )
  242.  
  243.       wmhints->flags = 0;
  244.    }
  245.  
  246.    icon = XCreateBitmapFromData(display,DefaultRootWindow(display),
  247.                icon_bits, icon_width, icon_height);
  248.  
  249.    wmhints->flags |= IconPixmapHint;
  250.  
  251.    wmhints->icon_pixmap = icon;
  252.  
  253.    XSetWMHints( display, window, wmhints );
  254.  
  255.    XFree( wmhints );
  256. }
  257.  
  258.  
  259.   /* Set the window to call quit() action if `deleted' */
  260.   XtOverrideTranslations(toplevel,     
  261.           XtParseTranslationTable("<Message>WM_PROTOCOLS:quit()") );
  262.   wm_delete_window = XInternAtom(XtDisplay(toplevel),"WM_DELETE_WINDOW",False);
  263.   (void) XSetWMProtocols(XtDisplay(toplevel), XtWindow(toplevel),
  264.                                                  &wm_delete_window, 1);
  265.   XtAppMainLoop(appcon);
  266. exit(0);
  267. }
  268.  
  269. /*
  270. ** this (re)initializes the boxWidget that contains the menubuttons
  271. ** by calling the fillbw() function
  272. */
  273. setup()
  274. {
  275. int width,fore,back,depth;
  276.  
  277.   if(bw != NULL)
  278.     XtDestroyWidget(bw);
  279.  
  280.   XtVaGetValues(toplevel,XtNwidth,&width,NULL);
  281.   XtVaSetValues(lw,XtNlabel,(XtArgVal)"No Bitmap Selected",NULL);
  282.  
  283. /* create the box widget to put all the bitmaps in */
  284.   bw = XtVaCreateManagedWidget("bitmaps",boxWidgetClass,phw,
  285.                   XtNwidth,width,
  286.                   XtNorientation,(XtArgVal)XtorientVertical,
  287.                   XtNfromVert,(XtArgVal)lw,
  288.                   NULL);
  289.  
  290.   XtVaGetValues(bw, XtNborderColor, &fore,
  291.                     XtNbackground,  &back,
  292.                     XtNdepth,       &depth, NULL);
  293.  
  294.   XtVaSetValues(bw, XtNbackgroundPixmap,
  295.         (XtArgVal)XmuCreateStippledPixmap(XtScreen(phw), fore, back, depth),
  296.                   NULL);
  297.  
  298. /* create all the bitmaps (each in a menubutton) */
  299.   while(file_list != NULL) {
  300.     FileList *t;
  301.     t = file_list->next;
  302.     free(file_list);
  303.     file_list = t;
  304.   }
  305.   file_list = get_files(".");
  306.   fill_bw();
  307. }
  308.  
  309.  
  310. useage()
  311. {
  312. fprintf(stderr,"\nxbmbrowser: Useage: \nxbmbrowser [toolkit options] [<Dir>]\n");
  313. fprintf(stderr,"\nWhere <Dir> is a directory name. \n");
  314. exit(1);
  315. }
  316.  
  317.