/* Motif Toolkit rendition of xviewfile */
/* An application to view the contents of a text file */
/* Jeff Pitchers, LUT 5/7/93 */
#include <stdio.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
/* Include the necessary Motif Toolkit header files */
#include <Xm/Xm.h>
#include <Xm/MainW.h>
#include <Xm/RowColumn.h>
#include <Xm/CascadeB.h>
#include <Xm/PushB.h>
#include <Xm/FileSB.h>
#include <Xm/MessageB.h>
#include <Xm/Text.h>
/* Define an application context */
XtAppContext ac;
/* Define the widgets */
Widget top, mainBox, fileText, menuBar, fileButton, fileMenu, openItem,
helpButton, quitItem, fileDialog, quitDialog, errorDialog,
helpDialog, temp;
/* Declare the callback functions */
int Open(), OpenFile(), Cancel(), Quit(), Ok(), Help();
XmString help;
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", xmMainWindowWidgetClass,
top, NULL);
/* Create a menu bar within the box */
menuBar = XmCreateMenuBar(mainBox, "menuBar", NULL, 0);
XtManageChild(menuBar);
/* Create a file menu, with open & quit selections */
fileMenu = XmCreatePulldownMenu(menuBar, "fileMenu", NULL, 0);
openItem = XtVaCreateManagedWidget("Open", xmPushButtonWidgetClass,
fileMenu, NULL);
quitItem = XtVaCreateManagedWidget("Quit", xmPushButtonWidgetClass,
fileMenu, NULL);
/* Create a file button which pops up the file menu */
fileButton = XtVaCreateManagedWidget("File", xmCascadeButtonWidgetClass,
menuBar,
XmNsubMenuId, fileMenu,
NULL);
/* Create a popup dialog to get the filename; no help button */
fileDialog = XmCreateFileSelectionDialog(mainBox, "fileDialog", NULL, 0);
XtVaSetValues(fileDialog, XmNwidth, 400, NULL);
temp = XmFileSelectionBoxGetChild(fileDialog, XmDIALOG_HELP_BUTTON);
XtUnmanageChild(temp);
/* Create a popup error message dialog; no cancel, no help button */
errorDialog = XmCreateErrorDialog(mainBox, "errorDialog", NULL, 0);
temp = XmMessageBoxGetChild(errorDialog, XmDIALOG_CANCEL_BUTTON);
XtUnmanageChild(temp);
temp = XmMessageBoxGetChild(errorDialog, XmDIALOG_HELP_BUTTON);
XtUnmanageChild(temp);
/* Create a read only, scrollable text widget, to display the file */
fileText = XmCreateScrolledText(mainBox, "fileText", 0, NULL);
XtVaSetValues(fileText,
XmNeditMode, XmMULTI_LINE_EDIT,
XmNeditable, 0,
XmNrows, 24,
XmNcolumns, 80,
NULL);
XtManageChild(fileText);
/* Create a popup question dialog to confirm quit; no help button */
quitDialog = XmCreateQuestionDialog(mainBox, "quitDialog", NULL, 0);
temp = XmMessageBoxGetChild(quitDialog, XmDIALOG_HELP_BUTTON);
XtUnmanageChild(temp);
/* Create a help button & dialog for application; no cancel, no help button */
helpButton = XtVaCreateManagedWidget("Help", xmCascadeButtonWidgetClass,
menuBar, NULL);
helpDialog = XmCreateInformationDialog(helpButton, "helpDialog", NULL, 0);
temp = XmMessageBoxGetChild(helpDialog, XmDIALOG_HELP_BUTTON);
XtUnmanageChild(temp);
temp = XmMessageBoxGetChild(helpDialog, XmDIALOG_CANCEL_BUTTON);
XtUnmanageChild(temp);
/* Put the help text in to the help dialog */
help = XmStringCreateSimple("To open a file, choose open from the file menu");
XtVaSetValues(helpDialog, XmNmessageString, help, NULL);
XmStringFree(help);
/* Inform menuBar that this is the help button, so that it */
/* can be positioned in the usual place for help buttons */
XtVaSetValues(menuBar, XmNmenuHelpWidget, helpButton, NULL);
/* Tie in the callbacks */
XtAddCallback(openItem, XmNactivateCallback, Open, NULL);
XtAddCallback(fileDialog, XmNokCallback, OpenFile, NULL);
XtAddCallback(fileDialog, XmNcancelCallback, Cancel, NULL);
XtAddCallback(quitItem, XmNactivateCallback, Quit, NULL);
XtAddCallback(quitDialog, XmNokCallback, Ok, NULL);
XtAddCallback(helpButton, XmNactivateCallback, Help, helpDialog, 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;
{
XtManageChild(fileDialog);
}
/* Callback routine when OK is clicked in the file dialog */
/* 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;
XmString filename_sel_text, err_mess;
/* Put the name of the file to be viewed in to the variable filename */
XtVaGetValues(fileDialog, XmNtextString, &filename_sel_text, NULL);
XmStringGetLtoR(filename_sel_text, XmSTRING_DEFAULT_CHARSET, &filename);
XmStringFree(filename_sel_text);
if ((fp = fopen(filename, "ro")) == NULL) { /* Oh oh, can't read that one */
err_mess = XmStringCreateSimple("Can't open that file");
XtVaSetValues(errorDialog, XmNmessageString, err_mess, NULL);
XmStringFree(err_mess);
XtManageChild(errorDialog);
}
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';
XmTextSetString(fileText, file_buf);
free(file_buf);
fclose(fp);
XtUnmanageChild(fileDialog);
}
free(filename);
}
/* CallBack routine when cancel is clicked in the file dialog */
/* ARGSUSED */
Cancel(w, a, b)
Widget w; XtPointer a, b;
{
XtUnmanageChild(fileDialog);
}
/* Callback routine when quit menu item is selected from the file menu */
/* ARGSUSED */
Quit(w, a, b)
Widget w; XtPointer a, b;
{
XmString quitmess;
quitmess = XmStringCreateSimple("Are you sure you want to quit?");
XtVaSetValues(quitDialog, XmNmessageString, quitmess, NULL);
XtManageChild(quitDialog);
}
/* Callback routine when OK is clicked in the quit dialog */
/* ARGSUSED */
Ok(w, a, b)
Widget w; XtPointer a, b;
{
exit(0);
}
/* Callback routine to display help dialog when help button is clicked */
/* ARGSUSED */
Help(w, client_data, call_data)
Widget w;
XtPointer client_data, call_data;
{
XtManageChild(client_data);
}
Figure 22: The Motif version of xviewfile