home *** CD-ROM | disk | FTP | other *** search
- /*
- *****************************************************************************
- ** xbmbrowser version 1.0 (c) Copyright Ashley Roll, 1992.
- ** FILE: callbacks.c
- **
- ** xbmbrowser is Public Domain. However it, and all the code still belong to me.
- ** I do, however grant permission for you to freely copy and distribute it on
- ** the condition that this and all other copyright notices remain unchanged in
- ** all distributions.
- **
- ** This software comes with NO warranty whatsoever. I therefore take no
- ** responsibility for any damages, losses or problems that the program may
- ** cause.
- *****************************************************************************
- */
-
- #include "xbmbrowser.h"
- #include <pwd.h>
-
- extern FileList *get_files();
- int mv;
- char trans[] =
- "<Key>Return: Ok() \n\
- Ctrl<Key>M: Ok() ";
-
- Widget delete,delete_q,getname,query;
-
- /*
- ** expand a tilder in situ
- ** This was sent to me by:
- _______________________________________________________________________________
- Chris McDonald. _--_|\
- / \
- X_.--._/
- v
- Department of Computer Science, AARNet: chris@budgie.cs.uwa.edu.au
- University of Western Australia, FTP: bilby.cs.uwa.edu.au, 130.95.1.11
- Crawley, Western Australia, 6009. SCUD: (31.97 +/-10% S, 115.81 +/-10% E)
- PHONE: +61 9 380 2533 FAX: +61 9 380 1089
- **
- ** and I have modified it slightly
- */
- static void expand_twiddle(text) /* expand in-situ in twiddle */
- char *text;
- {
- static char buf[255];
- char *s, *t, *t1;
- struct passwd *p, *getpwnam();
-
- s = text;
- s++; /* skip leading twiddle */
- t = buf;
- while (*s && *s != '/')
- *t++ = *s++;
- *t = NULL;
- if(*buf && (p = getpwnam(buf)) == NULL)
- return;
- t1 = *buf ? p->pw_dir : (char *) getenv("HOME");
- t = buf;
- while( *t++ = *t1++ ); /* buf <- home_dir */
- t--;
- while( *t++ = *s++ ); /* buf += rest_of_a */
- t = text;
- t1 = buf;
- while( *t++ = *t1++ ); /* s <- buf */
- }
-
- /*
- ** callback for the cancel button
- */
- void Cancel(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- XtPopdown(query);
- }
-
- /*
- ** callback for the cancel button in the delete requester */
- void Cancel_del(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- XtPopdown(delete_q);
- }
-
- /*
- ** callback for the ok button in the delete requester
- */
- void Del(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- unlink(bname);
-
- XtPopdown(delete_q);
-
- setup();
- }
-
- /*
- ** callback for the OK button in the rename and copy requesters
- */
- void Ok(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- char text[255];
- char cmd[255];
- int i;
-
- /* link the filename in the text part of the dialog widget, to the
- filename in bname, and if client_data is TRUE, then unlink the
- name in bname (this is a rename, a copy if the original is left) */
-
- strcpy(text, XawDialogGetValueString(getname));
-
- /* search for the first non-space char */
- i = 0;
- while(isspace(text[i])) i++;
-
- if(text[i] == '~') expand_twiddle(&text[i]);
-
- if(strcmp(bname,text) != 0) {
- sprintf(cmd,"%s %s %s",(mv)?"mv":"cp",bname,text);
- system(cmd);
- }
- XtPopdown(query);
-
- setup();
- }
-
- /*
- ** setup the dialog window
- */
- void setup_dialog()
- {
-
- Position x, y;
- Dimension height,width;
-
- /* create and position an transsient window to put the stuff in */
- XtVaGetValues(toplevel,XtNwidth, &width,XtNheight, &height,NULL);
- XtTranslateCoords(toplevel,(Position)(width/2)-175,
- (Position)(height/2),&x, &y);
-
- query = XtVaCreatePopupShell("Query",transientShellWidgetClass,mainpw,
- XtNx,(XtArgVal)x,XtNy,(XtArgVal)y,NULL);
-
- getname = XtVaCreateManagedWidget("GetName",dialogWidgetClass,query,
- XtNvalue,(XtArgVal)"",
- NULL);
- XtOverrideTranslations(XtNameToWidget (getname, "value"),
- XtParseTranslationTable(trans));
-
- XawDialogAddButton(getname,"Ok",Ok,NULL);
- XawDialogAddButton(getname,"Cancel",Cancel,NULL);
-
- /* set things up so that if we change the info in a widget, the dialog
- widget will allow it's children to resize */
-
- /* create the delete query */
- delete_q = XtVaCreatePopupShell("DeleteQuery",transientShellWidgetClass,mainpw,
- XtNx,(XtArgVal)x,XtNy,(XtArgVal)y, NULL);
-
- delete = XtVaCreateManagedWidget("DelYesNo",dialogWidgetClass,delete_q,
- XtNlabel,(XtArgVal)"",NULL);
- XawDialogAddButton(delete,"Yes",Del,NULL);
- XawDialogAddButton(delete,"No",Cancel_del,NULL);
-
- }
-
-
- /*
- ** sets up the rename requester
- */
- void rename_bitmap(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- Position x, y;
- Dimension height,width;
- Widget a;
-
- /* check if the dialogs are made yet.. */
- if(!dialogs_made) setup_dialog();
-
- /* set the global so the system call will be 'mv' */
- mv = TRUE;
-
- /* create a dialog widget with the an 'ok' button */
- XtVaSetValues(getname,XtNlabel,(XtArgVal)"Rename to:",NULL);
- XtVaSetValues(getname,XtNvalue,(XtArgVal)bname,NULL);
-
- XtPopup(query,XtGrabExclusive);
- }
-
- /*
- ** sets up the delete requester
- */
- void delete_bitmap(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- Position x, y;
- Dimension height,width;
- Widget a;
- char str[255];
-
- /* check if the dialogs are made yet.. */
- if(!dialogs_made) setup_dialog();
-
- sprintf(str,"Do You Realy wish to delete:\n%s",bname);
- XtVaSetValues(delete,XtNlabel,(XtArgVal)str,NULL);
- XtPopup(delete_q,XtGrabExclusive);
-
- }
-
- /*
- ** sets up the copy requester
- */
- void copy_bitmap(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- Position x, y;
- Dimension height,width;
- Widget a;
-
- /* check if the dialogs are made yet.. */
- if(!dialogs_made) setup_dialog();
-
- /* set the global so that the system call will use 'cp' */
- mv = FALSE;
-
- /* create a dialog widget with the an 'ok' button */
-
- XtVaSetValues(getname,XtNlabel,(XtArgVal)"Copy to:",NULL);
- XtVaSetValues(getname,XtNvalue,(XtArgVal)bname,NULL);
-
- XtPopup(query,XtGrabExclusive);
-
- }
-
- /*
- ** this function is added to the notify callback on all the
- ** menuButtons so that the global 'bname' contains the most
- ** reciently selected bitmap name
- */
- void set_name(widget,event)
- Widget widget;
- XButtonEvent *event;
- {
- char str[255],*t;
- Dimension iw,ih,w,h;
-
- bname = XtName(widget);
- XtVaGetValues(widget,
- XtNinternalHeight,&ih,
- XtNinternalWidth,&iw,
- XtNheight,&h,
- XtNwidth,&w,
- NULL);
- w -= 2 * iw;
- h -= 2 * ih;
-
- if ((t = strrchr(bname,'/')) == NULL)
- t = bname;
- else
- t++;
- sprintf(str,"%s (%dx%d)",t,(int)w,(int)h);
- XtVaSetValues(lw,XtNlabel,(XtArgVal)str,NULL);
- }
-
- /*
- ** this function runs (with a system() call) the editor
- ** with arguments to edit the file in bname
- */
- void edit_bitmap(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- char command[255],tmp[255];
-
- XtPopdown(menu);
- if(bname[0] != '\0') {
- sprintf(command,"%s %s &",EDITOR,bname);
- system(command);
- }
- }
-
- /*
- ** this function runs (with a system() call) the program specified in
- ** SETROOT with arguments of the file in bname
- */
- void set_root(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- char command[255],tmp[255];
-
- XtPopdown(menu);
- if(bname[0] != '\0') {
- sprintf(command,"%s \"%s\" &",SETROOT,bname);
- system(command);
- }
- }
-
- /*
- ** this function runs (with a system() call) the program specified in
- ** SETROOTINV with arguments of the file in bname
- */
- void set_root_inv(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- char command[255],tmp[255];
-
- XtPopdown(menu);
- if(bname[0] != '\0') {
- sprintf(command,"%s \"%s\" &",SETROOTINV,bname);
- system(command);
- }
- }
-
- /*
- ** this is used as a destroyCallback in all the menuButtons
- ** it destroys the Pixmap that they used. This is done so that we
- ** don't fill the Xterm memory with unused bitmaps!!
- */
- void destroy_Callback(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- XFreePixmap(XtDisplay(toplevel),(Pixmap)client_data);
- }
-
- /*
- ** this is the callback for the Rescan button
- ** it just calls setup()
- */
- void rescan(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- setup();
- }
-
- /*
- ** this is the callback for the directory name asciiTextWidget
- */
- void change_dir(w,client_data,call_data )
- Widget w;
- XtPointer client_data,call_data;
- {
- Widget t;
- char *text;
-
- /* get the new directory from the widget and only continue if it is
- not the same and is a VALID directory. */
-
- text = XawDialogGetValueString(atw);
- if(strcmp(dname,text) == 0) return; /* nothing to do */
-
- /* check if it contains a '~' as the first char and substute the correct dir */
- if (text[0] == '~') expand_twiddle(text);
-
-
- /* change the current directory to the new directory */
- if(chdir(text) != 0) {
- XtVaSetValues(atw,XtNvalue,(XtArgVal)dname,NULL);
- return;
- }
- (void) getcwd(dname,253);
- XtVaSetValues(atw,XtNvalue,(XtArgVal)dname,NULL);
- XtVaSetValues(lw,XtNlabel,(XtArgVal)"No Bitmap Selected",NULL);
-
- /* reset the bitmaps */
- setup();
- }
-
-