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

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