⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filer.c

📁 This source code has been tested under OpenWindows 2.0, Sun s X11/NeWS server. For Xlib programs th
💻 C
字号:
/*************************************************************************                                                                    ****  filer.c                                                           ****                                                                    ****  Text Editor -- File Open/Save Module                              ****                                                                    *************************************************************************/#include <stdio.h>#include <string.h>#include <Xm/FileSB.h>#include <Xm/Text.h>#include "textedit.h"/*************************************************************************                                                                    ****               F O R W A R D   D E F I N I T I O N S                ****                                                                    *************************************************************************/static	void InitStdFile();static	void ManageStdFile();static	void UnmanageStdFile();static	void SFProc();static	void ReadProc();static	void WriteProc();/*************************************************************************                                                                    ****                    L O C A L   V A R I A B L E S                   ****                                                                    *************************************************************************/char	*curfile = NULL;		/* The current filename       */Widget	stdfile_db;			/* The Standard File dialog   */void	(*fileproc)();			/* The Read/Write function    *//*************************************************************************                                                                    ****  InitFiler()                                                       ****                                                                    ****  This function initializes the filer module: it clears the current ****  filename, disables the File/Save menu choice, and creates the     ****  Standard File dialog.                                             ****                                                                    *************************************************************************/void InitFiler(){    Widget	temp;    stdfile_db = XmCreateFileSelectionDialog( mainwin, "StdFile", NULL, 0 );    XtAddCallback( stdfile_db, XmNokCallback, SFProc, NULL );    XtAddCallback( stdfile_db, XmNcancelCallback, UnmanageStdFile, NULL );    XtAddCallback( stdfile_db, XmNhelpCallback, UnmanageStdFile, NULL );}/*************************************************************************                                                                    ****  ManageStdFile( title, proc, defspec )                             ****                                                                    ****  This function manages the Standard File dialog. It sets the       ****  dialog's title to the string passed as "title", installs the      ****  function passed as "proc" in the "fileproc" variable (it gets     ****  called by the StdFile callback handler), and stores the string    ****  passed in "defspec" in the "dirSpec" resource.                    ****                                                                    *************************************************************************/static	void ManageStdFile( title, proc, defspec )    char	    *title;    XtCallbackProc  proc;    char	    *defspec;{    XmString	    temp1, temp2;    temp1 = XmStringCreate( title,   XmSTRING_DEFAULT_CHARSET );    if (defspec == NULL)        temp2 = XmStringCreate( "", XmSTRING_DEFAULT_CHARSET );    else        temp2 = XmStringCreate( defspec, XmSTRING_DEFAULT_CHARSET );    XtSetArg( arglist[0], XmNdialogTitle, temp1 );    XtSetArg( arglist[1], XmNdirSpec,     temp2 );    XtSetValues( stdfile_db, arglist, 2 );    XmStringFree( temp1 );    XmStringFree( temp2 );    XmFileSelectionDoSearch( stdfile_db, NULL );    XtManageChild( stdfile_db );    fileproc = proc;}/*************************************************************************                                                                    ****  UnmanageStdFile()                                                 ****                                                                    ****  This function unmanages the Standard File dialog. It is attached  ****  to the "Cancel" and "Help" buttons, because XmFileSelectionBox    ****  does not handle the "autoUnmanage" resource.                      ****                                                                    *************************************************************************/static	void UnmanageStdFile(){    XtUnmanageChild( stdfile_db );}/*************************************************************************                                                                    ****  SFProc( w, client_data, call_data )                               ****                                                                    ****  This function is the callback procedure for the Standard File     ****  dialog. It stores the chosen filename in "curfile", and then      ****  calls the function pointed-to by "fileproc".                      ****                                                                    *************************************************************************/static	void SFProc(w, client_data, call_data )    Widget				w;    caddr_t				client_data;    XmFileSelectionBoxCallbackStruct	*call_data;{    if (curfile != NULL)        XtFree( curfile );    XmStringGetLtoR( call_data->value, XmSTRING_DEFAULT_CHARSET, &curfile );    UnmanageStdFile();    (*fileproc)();}/*************************************************************************                                                                    ****  ReadProc()                                                        ****                                                                    ****  This function reads the file specified by "curfile" into the      ****  editor's text buffer.                                             ****                                                                    *************************************************************************/static	void ReadProc(){    FILE    *infile = fopen( curfile, "r" );    char    *txtbuf;    long    size;    if (infile == NULL)        /* Should display error */        return;    fseek( infile, 0L, 2 );    size = ftell( infile );    rewind( infile );    txtbuf = XtMalloc( size+1 );    fread( txtbuf, sizeof(char), size, infile );    XmTextSetString( textwin, txtbuf );    XtFree( txtbuf );}/*************************************************************************                                                                    ****  WriteProc()                                                       ****                                                                    ****  This function writes the editor's text buffer into a file named   ****  by "curfile".                                                     ****                                                                    *************************************************************************/static	void WriteProc(){    FILE    *outfile;    char    *txtbuf;    long    size;    outfile = fopen( curfile, "w" );    if (outfile == NULL)        /* Should display error */        return;    txtbuf = XmTextGetString( textwin );    size   = strlen(txtbuf);    fwrite( txtbuf, sizeof(char), size, outfile );    fclose( outfile );    XtFree( txtbuf );    saved = TRUE;}/*************************************************************************                                                                    ****  FileNew()                                                         ****                                                                    ****  This function is called from the File/New menu choice. It clears  ****  the text buffer, resets the current file, and disables the File/  ****  Save menu choice.                                                 ****                                                                    *************************************************************************/void FileNew(){    XmTextSetString( textwin, "" );    if (curfile != NULL)        XtFree( curfile );    curfile = NULL;}/*************************************************************************                                                                    ****  FileOpen()                                                        ****                                                                    ****  This function is called from the File/Open menu choice. All it    ****  does is invoke the Standard File dialog -- SFProc and ReadProc    ****  do the real work.                                                 ****                                                                    *************************************************************************/void FileOpen(){    ManageStdFile( "Open...", ReadProc, NULL );}/*************************************************************************                                                                    ****  FileSave()                                                        ****                                                                    ****  This function is a link to WriteProc(), which writes the text     ****  buffer into a file using the current filename.                    ****                                                                    *************************************************************************/void FileSave(){    if (curfile == NULL)        FileSaveAs();    else        WriteProc();}/*************************************************************************                                                                    ****  FileSaveAs()                                                      ****                                                                    ****  This function is called from the File/Save-As menu choice. It     ****  invokes the Standard File dialog, and links it to WriteProc.      ****                                                                    *************************************************************************/void FileSaveAs(){    ManageStdFile( "Save As...", WriteProc, curfile );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -