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

  1. /*
  2. *****************************************************************************
  3. ** xbmbrowser version 1.0  (c) Copyright Ashley Roll, 1992.
  4. ** FILE: callbacks.c
  5. **
  6. ** xbmbrowser is Public Domain. However it, and all the code still belong to me.
  7. ** I do, however grant permission for you to freely copy and distribute it on 
  8. ** the condition that this and all other copyright notices remain unchanged in 
  9. ** all distributions.
  10. **
  11. ** This software comes with NO warranty whatsoever. I therefore take no
  12. ** responsibility for any damages, losses or problems that the program may 
  13. ** cause.
  14. *****************************************************************************
  15. */
  16.  
  17. #include "xbmbrowser.h"
  18. #include <pwd.h>
  19.  
  20. extern FileList *get_files();
  21. int mv;
  22. char trans[] = 
  23.   "<Key>Return:  Ok() \n\
  24.    Ctrl<Key>M:   Ok() ";
  25.  
  26. Widget delete,delete_q,getname,query;
  27.  
  28. /*
  29. ** expand a tilder in situ
  30. ** This was sent to me by:
  31. _______________________________________________________________________________
  32. Chris McDonald.            _--_|\
  33.                           /      \
  34.                           X_.--._/
  35.                                 v
  36. Department of Computer Science,   AARNet: chris@budgie.cs.uwa.edu.au
  37. University of Western Australia,  FTP:    bilby.cs.uwa.edu.au,  130.95.1.11
  38. Crawley, Western Australia, 6009. SCUD:   (31.97 +/-10% S, 115.81 +/-10% E)
  39. PHONE:       +61 9 380 2533       FAX:    +61 9 380 1089
  40. **
  41. ** and I have modified it slightly
  42. */
  43. static void expand_twiddle(text)                /* expand in-situ in twiddle */
  44. char *text;
  45. {
  46.     static char buf[255];
  47.     char *s, *t, *t1;
  48.     struct passwd *p, *getpwnam();
  49.  
  50.     s = text;
  51.     s++;                                        /* skip leading twiddle */
  52.     t = buf;
  53.     while (*s && *s != '/')
  54.         *t++ = *s++;
  55.     *t = NULL;
  56.     if(*buf && (p = getpwnam(buf)) == NULL)
  57.         return;
  58.     t1 = *buf ? p->pw_dir : (char *) getenv("HOME");
  59.     t = buf;
  60.     while( *t++ = *t1++ );                      /* buf <- home_dir */
  61.     t--;
  62.     while( *t++ = *s++ );                       /* buf += rest_of_a */
  63.     t  = text;
  64.     t1 = buf;
  65.     while( *t++ = *t1++ );                      /* s   <- buf */
  66. }
  67.  
  68. /*
  69. ** callback for the cancel button
  70. */
  71. void Cancel(w,client_data,call_data )
  72. Widget w;
  73. XtPointer client_data,call_data;
  74. {
  75.   XtPopdown(query);
  76. }
  77.  
  78. /*
  79. ** callback for the cancel button in the delete requester */
  80. void Cancel_del(w,client_data,call_data )
  81. Widget w;
  82. XtPointer client_data,call_data;
  83. {
  84.   XtPopdown(delete_q);
  85. }
  86.  
  87. /*
  88. ** callback for the ok button in the delete requester
  89. */
  90. void Del(w,client_data,call_data )
  91. Widget w;
  92. XtPointer client_data,call_data;
  93. {
  94.   unlink(bname);
  95.  
  96.   XtPopdown(delete_q);
  97.  
  98. setup();
  99. }
  100.  
  101. /*
  102. ** callback for the OK button in the rename and copy requesters
  103. */
  104. void Ok(w,client_data,call_data )
  105. Widget w;
  106. XtPointer client_data,call_data;
  107. {
  108. char text[255];
  109. char cmd[255];
  110. int i;
  111.  
  112.   /* link the filename in the text part of the dialog widget, to the 
  113.      filename in bname, and if client_data is TRUE, then unlink the 
  114.      name in bname (this is a rename, a copy if the original is left) */
  115.  
  116.   strcpy(text, XawDialogGetValueString(getname));
  117.  
  118. /* search for the first non-space char */
  119.   i = 0;
  120.   while(isspace(text[i])) i++;
  121.  
  122.   if(text[i] == '~') expand_twiddle(&text[i]);
  123.  
  124.   if(strcmp(bname,text) != 0) {
  125.       sprintf(cmd,"%s %s %s",(mv)?"mv":"cp",bname,text);
  126.       system(cmd);
  127.   }
  128.   XtPopdown(query);
  129.  
  130. setup();
  131. }
  132.  
  133. /*
  134. ** setup the dialog window
  135. */
  136. void setup_dialog()
  137. {
  138.  
  139.   Position    x, y;
  140.   Dimension   height,width;
  141.  
  142. /* create and position an transsient window to put the stuff in */
  143.   XtVaGetValues(toplevel,XtNwidth, &width,XtNheight, &height,NULL);
  144.   XtTranslateCoords(toplevel,(Position)(width/2)-175,
  145.                     (Position)(height/2),&x, &y);
  146.  
  147.   query = XtVaCreatePopupShell("Query",transientShellWidgetClass,mainpw,
  148.                        XtNx,(XtArgVal)x,XtNy,(XtArgVal)y,NULL);
  149.  
  150.   getname = XtVaCreateManagedWidget("GetName",dialogWidgetClass,query,
  151.                        XtNvalue,(XtArgVal)"",
  152.                        NULL);
  153.   XtOverrideTranslations(XtNameToWidget (getname, "value"),
  154.               XtParseTranslationTable(trans));
  155.  
  156.   XawDialogAddButton(getname,"Ok",Ok,NULL);
  157.   XawDialogAddButton(getname,"Cancel",Cancel,NULL);
  158.  
  159. /* set things up so that if we change the info in a widget, the dialog
  160.    widget will allow it's children to resize */
  161.  
  162. /* create the delete query */
  163.  delete_q = XtVaCreatePopupShell("DeleteQuery",transientShellWidgetClass,mainpw,
  164.                        XtNx,(XtArgVal)x,XtNy,(XtArgVal)y, NULL);
  165.  
  166.   delete =  XtVaCreateManagedWidget("DelYesNo",dialogWidgetClass,delete_q,
  167.                        XtNlabel,(XtArgVal)"",NULL);
  168.   XawDialogAddButton(delete,"Yes",Del,NULL);
  169.   XawDialogAddButton(delete,"No",Cancel_del,NULL);
  170.  
  171. }
  172.  
  173.  
  174. /*
  175. ** sets up the rename requester 
  176. */
  177. void rename_bitmap(w,client_data,call_data )
  178. Widget w;
  179. XtPointer client_data,call_data;
  180. {
  181.   Position    x, y;
  182.   Dimension   height,width;
  183.   Widget a;
  184.  
  185. /* check if the dialogs are made yet.. */
  186. if(!dialogs_made) setup_dialog();
  187.  
  188. /* set the global so the system call will be 'mv' */
  189.   mv = TRUE;
  190.  
  191. /* create a dialog widget with the an 'ok' button */
  192.   XtVaSetValues(getname,XtNlabel,(XtArgVal)"Rename to:",NULL);
  193.   XtVaSetValues(getname,XtNvalue,(XtArgVal)bname,NULL); 
  194.  
  195.   XtPopup(query,XtGrabExclusive);
  196. }
  197.  
  198. /*
  199. ** sets up the delete requester
  200. */
  201. void delete_bitmap(w,client_data,call_data )
  202. Widget w;
  203. XtPointer client_data,call_data;
  204. {
  205.   Position    x, y;
  206.   Dimension   height,width;
  207.   Widget a;
  208.   char str[255];
  209.  
  210. /* check if the dialogs are made yet.. */
  211. if(!dialogs_made) setup_dialog();
  212.  
  213.   sprintf(str,"Do You Realy wish to delete:\n%s",bname);
  214.   XtVaSetValues(delete,XtNlabel,(XtArgVal)str,NULL); 
  215.   XtPopup(delete_q,XtGrabExclusive); 
  216.  
  217. }
  218.  
  219. /*
  220. ** sets up the copy requester
  221. */
  222. void copy_bitmap(w,client_data,call_data )
  223. Widget w;
  224. XtPointer client_data,call_data;
  225. {
  226.   Position    x, y;
  227.   Dimension   height,width;
  228.   Widget a;
  229.  
  230. /* check if the dialogs are made yet.. */
  231. if(!dialogs_made) setup_dialog();
  232.  
  233. /* set the global so that the system call will use 'cp' */
  234.   mv = FALSE;
  235.  
  236. /* create a dialog widget with the an 'ok' button */
  237.  
  238.   XtVaSetValues(getname,XtNlabel,(XtArgVal)"Copy to:",NULL);
  239.   XtVaSetValues(getname,XtNvalue,(XtArgVal)bname,NULL);
  240.  
  241.   XtPopup(query,XtGrabExclusive); 
  242.  
  243. }
  244.  
  245. /*
  246. ** this function is added to the notify callback on all the 
  247. ** menuButtons so that the global 'bname' contains the most 
  248. ** reciently selected bitmap name
  249. */
  250. void set_name(widget,event)
  251. Widget widget;
  252. XButtonEvent *event;
  253. {
  254. char str[255],*t;
  255. Dimension iw,ih,w,h;
  256.  
  257.   bname = XtName(widget);
  258.   XtVaGetValues(widget,
  259.                 XtNinternalHeight,&ih,
  260.                 XtNinternalWidth,&iw,
  261.                 XtNheight,&h,
  262.                 XtNwidth,&w,
  263.                 NULL);
  264.   w -= 2 * iw;
  265.   h -= 2 * ih;
  266.  
  267.   if ((t = strrchr(bname,'/')) == NULL)
  268.     t = bname;
  269.   else
  270.     t++;
  271.   sprintf(str,"%s (%dx%d)",t,(int)w,(int)h);
  272.   XtVaSetValues(lw,XtNlabel,(XtArgVal)str,NULL);
  273. }
  274.  
  275. /*
  276. ** this function runs (with a system() call) the editor
  277. ** with arguments to edit the file in bname
  278. */
  279. void edit_bitmap(w,client_data,call_data )
  280. Widget w;
  281. XtPointer client_data,call_data;
  282. {
  283. char command[255],tmp[255];
  284.  
  285. XtPopdown(menu);
  286.   if(bname[0] != '\0') {
  287.     sprintf(command,"%s %s &",EDITOR,bname);
  288.     system(command);
  289.   }
  290. }
  291.  
  292. /*
  293. ** this function runs (with a system() call) the program specified in
  294. ** SETROOT with arguments of the file in bname
  295. */
  296. void set_root(w,client_data,call_data )
  297. Widget w;
  298. XtPointer client_data,call_data;
  299. {
  300. char command[255],tmp[255];
  301.  
  302. XtPopdown(menu);
  303.   if(bname[0] != '\0') {
  304.     sprintf(command,"%s \"%s\" &",SETROOT,bname);
  305.     system(command);
  306.   }
  307. }
  308.  
  309. /*
  310. ** this function runs (with a system() call) the program specified in
  311. ** SETROOTINV with arguments of the file in bname
  312. */
  313. void set_root_inv(w,client_data,call_data )
  314. Widget w;
  315. XtPointer client_data,call_data;
  316. {
  317. char command[255],tmp[255];
  318.  
  319. XtPopdown(menu);
  320.   if(bname[0] != '\0') {
  321.     sprintf(command,"%s \"%s\" &",SETROOTINV,bname);
  322.     system(command);
  323.   }
  324. }
  325.  
  326. /*
  327. ** this is used as a destroyCallback in all the menuButtons
  328. ** it destroys the Pixmap that they used. This is done so that we
  329. ** don't fill the Xterm memory with unused bitmaps!!
  330. */
  331. void destroy_Callback(w,client_data,call_data )
  332. Widget w;
  333. XtPointer client_data,call_data;
  334. {
  335. XFreePixmap(XtDisplay(toplevel),(Pixmap)client_data);
  336. }
  337.  
  338. /*
  339. ** this is the callback for the Rescan button
  340. ** it just calls setup()
  341. */
  342. void rescan(w,client_data,call_data )
  343. Widget w;
  344. XtPointer client_data,call_data;
  345. {
  346. setup();
  347. }
  348.  
  349. /*
  350. ** this is the callback for the directory name asciiTextWidget
  351. */
  352. void change_dir(w,client_data,call_data )
  353. Widget w;
  354. XtPointer client_data,call_data;
  355. {
  356. Widget t;
  357. char *text;
  358.  
  359. /* get the new directory from the widget and only continue if it is 
  360.    not the same and is a VALID directory. */
  361.  
  362.   text = XawDialogGetValueString(atw);
  363.   if(strcmp(dname,text) == 0) return; /* nothing to do */ 
  364.  
  365. /* check if it contains a '~' as the first char and substute the correct dir */ 
  366.    if (text[0] == '~') expand_twiddle(text);
  367.  
  368.  
  369. /* change the current directory to the new directory */
  370.   if(chdir(text) != 0) {
  371.     XtVaSetValues(atw,XtNvalue,(XtArgVal)dname,NULL);
  372.     return;
  373.   } 
  374.     (void) getcwd(dname,253);
  375.     XtVaSetValues(atw,XtNvalue,(XtArgVal)dname,NULL);
  376.     XtVaSetValues(lw,XtNlabel,(XtArgVal)"No Bitmap Selected",NULL);
  377.  
  378. /* reset the bitmaps */
  379. setup();
  380. }
  381.  
  382.