home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cl-lib07.zip / samples.zip / usr / samples / textview / textview.m next >
Text File  |  1995-03-05  |  2KB  |  80 lines

  1. #include <pm/pm.h>
  2. #include <io.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5.  
  6. main(int argc,char *argv[])
  7. {
  8.   StdApp      *application;
  9.   StdWindow   *window;
  10.   Window      *mle;
  11.   FILE        *inputFile;
  12.   struct stat  statbuffer;
  13.   char        *contents;
  14.   char        *title;
  15.  
  16.   /*
  17.    * check for command line arguments and check given file (struct stat)
  18.    */
  19.   if (argc != 2) /* check for command line arguments, must be exactly one */
  20.     exit (-1);
  21.  
  22.   if (stat (argv[1],&statbuffer) < 0) /* check file */
  23.     exit (-1);
  24.  
  25.   /*
  26.    * open file and read contents to buffer
  27.    */
  28.   inputFile = fopen (argv[1],"r"); /* open text file read-only */
  29.  
  30.   contents = (char *) malloc (statbuffer.st_size + 1); /* allocate buffer */
  31.   fread (contents,statbuffer.st_size,1,inputFile); /* read contents of file */
  32.  
  33.   /*
  34.    * create app instance and window, create MLE for text display
  35.    */
  36.   application = [[StdApp alloc] init]; /* initialize application object */
  37.   window = [[MainWindow alloc] initWithId: 1000
  38.                                 andFlags: FCF_BORDER]; /* create main window */
  39.   
  40.   [window createObjects]; /* create child windows of main window */
  41.  
  42.   mle = [[MultiLineEntryField alloc] initWithId: 1001 
  43.                                        andFlags: (WS_VISIBLE | MLS_READONLY |
  44.                           MLS_HSCROLL | MLS_VSCROLL)
  45.                                              in: window];
  46.   [window insertChild: mle]; /* insert MLE into window */
  47.  
  48.   /*
  49.    * calculate title of window and set it
  50.    */
  51.   title = (char *) malloc (11 + /* this is the length of "Textview: " + NULL */
  52.                strlen (argv[1])); /* allocate buffer for title */
  53.   sprintf (title,"Textview: %s",argv[1]); /* fill title buffer */
  54.  
  55.   [window setTitle: title]; /* set window title */
  56.  
  57.   free (title); /* free title buffer */
  58.  
  59.   /*
  60.    * show window, set MLE size and  display contents of file
  61.    */
  62.   [window makeKeyAndOrderFront: nil]; /* show window */
  63.   [mle setSize: 0:0:[window width]:[window height]]; /* set MLE size */
  64.   [mle setText: contents]; /* display contents of file */
  65.  
  66.   /*
  67.    * run application
  68.    */
  69.   [application run];
  70.  
  71.   /*
  72.    * free all resources
  73.    */
  74.   free (contents); /* free contents buffer */
  75.   fclose (inputFile); /* close file */
  76.  
  77.   [application free]; /* free application */
  78.   [window free]; /* free window */
  79. }
  80.