/*
 * $Id: util.c,v 1.3 1997/01/03 17:40:55 root Exp $
 *
 * $Log: util.c,v $
 * Revision 1.3  1997/01/03 17:40:55  root
 * added ch_file()
 * Changed watch_cursor() to not change cursor on toplevel or flush events.
 *
 * Revision 1.2  1996/12/12 19:10:40  root
 * Added:
 * GetTopShell(), GetAppShell()
 *
 * Revision 1.1  1996/12/06 20:43:50  root
 * Initial revision
 *
 */

#include "common.h"
#include "util.h"


Widget
GetTopShell(Widget w)
{
  while(w && !XtIsWMShell(w))
    w = XtParent(w);
  return(w);
}

Widget
GetAppShell(Widget w)
{
  while (w && !XtIsApplicationShell(w))
    w = XtParent(w);
  return(w);
}


void
handle_pending_x_events(void)
{
  XEvent event;
  
  while(XtAppPending(app)) {
    XtAppNextEvent(app, &event);
    XtDispatchEvent(&event);
  }
}


void
watch_cursor(Widget w, Boolean state)
{
  static Cursor cursor;
  XSetWindowAttributes attrs;
  Display *dpy = XtDisplay(w);
  
  if (! cursor)
    cursor = XCreateFontCursor(dpy, XC_watch);

  attrs.cursor = state ? cursor : None;

  /*
   *  Change cursor on popup
   */

  while(w && !XtIsWMShell(w))
    w = XtParent(w);
  XChangeWindowAttributes(dpy, XtWindow(w),
			  CWCursor, &attrs);

  /*
   *  Flush the Display so the cursor is visible.
   */

  XFlush(dpy);
}


int
ch_file(char *file, char *user, char *newfile)
{
  struct passwd *pwd;

  if ((pwd = getpwnam(user)) != NULL) {
    chown(file, pwd->pw_uid, pwd->pw_gid);
    chmod(file, MODE);
    if ((rename(file, newfile)) < 0 )
      printf("Error\n");
    return 0;
  } else
    return 1;
}
