Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Program Listing Of Athena Version Of xviewfile
next up previous contents
Next: Program Listing Of Up: Example Programs Previous: xviewfile

Program Listing Of Athena Version Of xviewfile

/* Athena Toolkit rendition of xviewfile */
/* An application to view the contents of a text file */
/* Jeff Pitchers, LUT 21/6/93 */

#include <stdio.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>

/* Include the necessary Athena Toolkit header files */
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Dialog.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/SmeLine.h>
#include <X11/Xaw/Sme.h>

/* Define an application context */
XtAppContext ac;

/* Define the widgets */
Widget top, mainBox, fileText, fileButton, fileMenu, openItem, line,
            quitItem, filePopup, quitPopup, fileDialog, quitDialog,
            openButton, cancelButton, yesButton, noButton;

/* Declare the callback functions */
int Open(), OpenFile(), Cancel(), Quit(), Yes(), No();

main(argc, argv)
int argc; char **argv;
{
  /* Create the application shell widget */
  top = XtVaAppInitialize(&ac, "XViewfile", NULL, 0, &argc, argv, NULL, NULL);

  /* Create a box to hold everything */
  mainBox = XtVaCreateManagedWidget("mainBox", boxWidgetClass, top, NULL);

  /* Create a file button and file menu, with open & quit selections */
  fileButton = XtVaCreateManagedWidget("File", menuButtonWidgetClass,
               mainBox,
               XtNmenuName, "fileMenu",
               NULL);
  fileMenu = XtVaCreatePopupShell("fileMenu", simpleMenuWidgetClass,
             fileButton, NULL);
  openItem = XtVaCreateManagedWidget("Open", smeBSBObjectClass, fileMenu, NULL);
  line  = XtVaCreateManagedWidget("line", smeLineObjectClass, fileMenu, NULL);
  quitItem = XtVaCreateManagedWidget("Quit", smeBSBObjectClass, fileMenu, NULL);

  /* Create a popup dialog to get the filename */
  filePopup = XtVaCreatePopupShell("filePopup", transientShellWidgetClass,
              top, NULL);
  fileDialog = XtVaCreateManagedWidget("fileDialog", dialogWidgetClass,
               filePopup,
               XtNlabel, "Enter Filename:",
               XtNvalue, "",
               NULL);
  openButton = XtVaCreateManagedWidget("Open", commandWidgetClass,
               fileDialog, NULL);
  cancelButton = XtVaCreateManagedWidget("Cancel", commandWidgetClass,
                 fileDialog, NULL);

  /* Create a popup dialog to confirm quit */
  quitPopup = XtVaCreatePopupShell("quitPopup", transientShellWidgetClass,
              top, NULL);
  quitDialog = XtVaCreateManagedWidget("quitDialog", dialogWidgetClass,
               quitPopup,
               XtNlabel, "Are you sure you want to quit?",
               NULL);
  yesButton = XtVaCreateManagedWidget("Yes", commandWidgetClass,
              quitDialog, NULL);
  noButton = XtVaCreateManagedWidget("No", commandWidgetClass,
             quitDialog, NULL);

  /* Create a read only, scrollable text widget, to display the file */
  fileText = XtVaCreateManagedWidget("fileText", asciiTextWidgetClass,
             mainBox,
             XtNheight, 320,
             XtNwidth, 565,
             XtNscrollVertical, XawtextScrollWhenNeeded,
             XtNscrollHorizontal, XawtextScrollWhenNeeded,
             NULL);

  /* Tie in the callbacks */
  XtAddCallback(openItem, XtNcallback, Open, NULL);
  XtAddCallback(openButton, XtNcallback, OpenFile, NULL);
  XtAddCallback(cancelButton, XtNcallback, Cancel, NULL);
  XtAddCallback(quitItem, XtNcallback, Quit, NULL);
  XtAddCallback(yesButton, XtNcallback, Yes, NULL);
  XtAddCallback(noButton, XtNcallback, No, NULL);

  XtRealizeWidget(top);
  XtAppMainLoop(ac);
}

/* Callback routine when open menu item is selected from the file menu */
/* ARGSUSED */
Open(w, a, b)
Widget w; XtPointer a, b;
{
  /* For the geometry of the main application window */
  Position x, y;
  Dimension wd, ht;

  /* Attempt to position the popup somewhere roughly in the middle */
  /* of the main application window */
  XtVaGetValues(top, XtNx, &x, XtNy, &y, XtNwidth, &wd, XtNheight, &ht, NULL);
  XtVaSetValues(filePopup, XtNx, x+wd/2, XtNy, y+ht/2, NULL);
  XtVaSetValues(fileDialog, XtNlabel, "Enter Filename:", NULL);
  XtPopup(filePopup, XtGrabNone);
}

/* Callback routine when open button, in the open file dialog, is clicked */
/* ARGSUSED */
OpenFile(w, a, b)
Widget w; XtPointer a, b;
{
  FILE *fp, *fopen();
  struct stat stat_buf;
  off_t file_size;
  char *filename, *file_buf;

  /* Get the name of the file to be viewed */
  filename = XawDialogGetValueString(fileDialog);

  if ((fp = fopen(filename, "ro")) == NULL) { /* Oh oh, can't read that one */
    XtVaSetValues(fileDialog, XtNlabel, "Can't open that file", NULL);
  }
  else { /* Ok, read it */
    fstat(fileno(fp), &stat_buf);
    file_size =  stat_buf.st_size;
    file_buf = malloc(file_size * sizeof(char) + 1);
    fread(file_buf, 1, file_size, fp);
    file_buf[file_size] = '\0';
    XtVaSetValues(fileText, XtNstring, file_buf, NULL);
    free(file_buf);
    fclose(fp);
    XtPopdown(filePopup);
  }
  free(filename);
}

/* CallBack routine when cancel button, in the open file dialog, is clicked */
/* ARGSUSED */
Cancel(w, a, b)
Widget w; XtPointer a, b;
{
  XtPopdown(filePopup);
}

/* Callback routine when quit menu item is selected from the file menu */
/* ARGSUSED */
Quit(w, a, b)
Widget w; XtPointer a, b;
{
  /* For the geometry of the main application window */
  Position x, y;
  Dimension wd, ht;

  /* Attempt to position the popup somewhere roughly in the middle */
  /* of the main application window */
  XtVaGetValues(top, XtNx, &x, XtNy, &y, XtNwidth, &wd, XtNheight, &ht, NULL);
  XtVaSetValues(quitPopup, XtNx, x+wd/2, XtNy, y+ht/2, NULL);
  XtPopup(quitPopup, XtGrabExclusive);
  /* Exclusive -> user can't do other things in this application */
}

/* Callback routine when yes quit button, in the quit dialog, is clicked */
/* ARGSUSED */
Yes(w, a, b)
Widget w; XtPointer a, b;
{
  exit(0);
}

/* Callback routine when no continue button, in the quit dialog, is clicked */
/* ARGSUSED */
No(w, a, b)
Widget w; XtPointer a, b;
{
  XtPopdown(quitPopup);
}

 
Figure 21: The Athena version of xviewfile



JR Pitchers
Wed May 3 16:45:04 BST 1995


With any suggestions or questions please feel free to contact us