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

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