home *** CD-ROM | disk | FTP | other *** search
/ Altsys Virtuoso 2.0K / virtuoso_20k.iso / DemoApps / Graphics / Viewers / ViewGif2 / Source / ViewBitmap.m < prev    next >
Encoding:
Text File  |  1994-04-18  |  8.4 KB  |  219 lines

  1. /***************************************************************************/
  2. /* ViewBitmap.m - implementation file to ViewBitmap class           */
  3. /* Takes a bitmap, and displays it in a window with scrollers.             */
  4. /* The bitmap can be saved as a TIFF file, and another object (such as an  */
  5. /* activate menu) can be notified if the window is closed.           */
  6. /* January 1990   Carl F. Sutter                       */
  7. /***************************************************************************/
  8.  
  9. #import "ViewBitmap.h"
  10. #import <appkit/Window.h>
  11. #import <appkit/ScrollView.h>
  12. #import <appkit/Application.h>     // for NXApp
  13. #import <appkit/Panel.h>     // for NXRunAlertPanel
  14. #import <string.h>         // for strcpy, strcat
  15. #import <stdlib.h>         // for MIN
  16. #import <libc.h>         // for file i/o
  17.  
  18. @implementation ViewBitmap
  19.  
  20. /***************************************************************************/
  21. /* newBitmap - show the bitmap in a new window                   */
  22. /***************************************************************************/
  23. + newBitmap:(Bitmap *)bmpIn
  24.    {
  25.    NXRect nxrBitmap;
  26.    
  27.    [bmpIn getSize:&(nxrBitmap.size)];
  28.    self = [super newFrame:&nxrBitmap];
  29.    bmpImage = bmpIn;
  30.    nxsBitmap = nxrBitmap.size;
  31.    strcpy( szTitle, "" );
  32.    nxpTopLeft.x = nxpTopLeft.y = 100.0;
  33.    [self setup];
  34.    return( self );
  35.    } /* newBitmap 1/23/90 CFS */
  36.    
  37.    
  38. /***************************************************************************/
  39. /* newBitmap:title - show the bitmap in a new window with the given title  */
  40. /***************************************************************************/
  41. + newBitmap:(Bitmap *)bmpIn title:(char *)szTitleIn
  42.    {
  43.    NXRect nxrBitmap;
  44.    
  45.    [bmpIn getSize:&(nxrBitmap.size)];
  46.    self = [super newFrame:&nxrBitmap];
  47.    bmpImage = bmpIn;
  48.    nxsBitmap = nxrBitmap.size;
  49.    strcpy( szTitle, szTitleIn );
  50.    nxpTopLeft.x = nxpTopLeft.y = 100.0;
  51.    [self setup];
  52.    return( self );
  53.    } /* newBitmap:title 1/23/90 CFS */
  54.    
  55.    
  56. /***************************************************************************/
  57. /* newBitmap:title:topLeft: - display bitmap with given position and title  */
  58. /***************************************************************************/
  59. + newBitmap:(Bitmap *)bmpIn title:(char *)szTitleIn topLeft:(NXPoint)nxpTopLeftIn
  60.    {
  61.    NXRect nxrBitmap;
  62.    
  63.    [bmpIn getSize:&(nxrBitmap.size)];
  64.    self = [super newFrame:&nxrBitmap];
  65.    bmpImage = bmpIn;
  66.    nxsBitmap = nxrBitmap.size;
  67.    strcpy( szTitle, szTitleIn );
  68.    nxpTopLeft = nxpTopLeftIn;
  69.    [self setup];
  70.    return( self );
  71.    } /* newBitmap:title:topLeft: 1/23/90 CFS */
  72.  
  73.  
  74. /***************************************************************************/
  75. /* setup - make a new scrolling view with the bitmap and title               */
  76. /* the bitmap is not copied, so the sender should not free the bitmap that */
  77. /* is sent!  The origin id for the top-left of the window, since the top   */
  78. /* shouldn't be pushed off the screen.                                     */
  79. /***************************************************************************/
  80. - setup
  81.    {
  82.    ScrollView    *scrollView;    /* scroll view to hold bitmap     */
  83.    Window    *win;        /* window to hold ScrollView      */
  84.    NXRect    nxrView;    /* bounds of this view            */
  85.    NXRect    nxrScrollView;  /* bounds of enclosing ScrollView */
  86.    NXRect    nxrWinHeight;    /* bounds of enclosing window     */
  87.    NXSize    nxsScreen;    /* size of screen          */
  88.       
  89.    /* make the ScrollView, and put the View in it */
  90.    nxrView.origin.x = nxrView.origin.y = 0.0;    
  91.    nxrView.size = nxsBitmap;
  92.    [ScrollView getFrameSize:&(nxrScrollView.size) forContentSize:&(nxrView.size) 
  93.           horizScroller:YES vertScroller:YES borderType:NX_NOBORDER];
  94.    nxrScrollView.origin.x = nxrScrollView.origin.y = 0.0;          
  95.    scrollView = [ScrollView newFrame:&nxrScrollView];
  96.    [[scrollView setVertScrollerRequired:YES] setHorizScrollerRequired:YES];
  97.    [scrollView setDocView:self];
  98.    
  99.    /* the "origin" is a top left coordinate, convert it to lower left */
  100.    nxrScrollView.origin.x = nxpTopLeft.x;          
  101.    [Window getFrameRect:&nxrWinHeight forContentRect:&nxrScrollView
  102.                   style:NX_SIZEBARSTYLE];
  103.    [NXApp getScreenSize:&nxsScreen];
  104.    nxrScrollView.origin.y = nxsScreen.height - nxrWinHeight.size.height - nxpTopLeft.y;          
  105.              
  106.    /* make the enclosing Window, and put the ScrollView (and View) in it */
  107.    win = [Window newContent:&nxrScrollView style:NX_SIZEBARSTYLE backing:NX_BUFFERED defer:NO];
  108.    [win setContentView:scrollView];
  109.    [win setTitle:szTitle];
  110.    [win display];
  111.    [win makeKeyAndOrderFront:self];    
  112.    [win setDelegate:self]; /* want this view to get messages about it's window */ 
  113.    
  114.    return( self );
  115.    } /* newWindow 10/13/89 CFS */
  116.  
  117.  
  118. /***************************************************************************/
  119. /* windowWillResize - delegate message sent by window when user is resizes */
  120. /* don't allow window to get bigger than is necessary to show whole image  */
  121. /***************************************************************************/
  122. - windowWillResize:sender toSize:(NXSize *)frameSize
  123.    {
  124.    NXRect    nxrScrollView;
  125.    NXRect    nxrWinMax;
  126.    
  127.    /* find largest window necessary to hold bitmap */
  128.    [ScrollView getFrameSize:&(nxrScrollView.size) forContentSize:&nxsBitmap 
  129.           horizScroller:YES vertScroller:YES borderType:NX_NOBORDER];
  130.    [Window getFrameRect:&nxrWinMax forContentRect:&nxrScrollView 
  131.               style:NX_SIZEBARSTYLE];
  132.           
  133.    /* prevent window fram from getting bigger than biggest necessary size */          
  134.    frameSize->width = MIN( nxrWinMax.size.width, frameSize->width );
  135.    frameSize->height = MIN( nxrWinMax.size.height, frameSize->height );
  136.    return( self );
  137.    } /* windowWillResize 10/18/89 CFS */
  138.  
  139.  
  140. /***************************************************************************/
  141. /* free bitmap on closing - this is probably not necessary if the window   */
  142. /* will free instance variable objects on closing                          */
  143. /***************************************************************************/
  144. - windowWillClose:sender
  145.    {
  146.    [closeNotify windowWillClose:[self window]];
  147.    [bmpImage free];
  148.    return( self );
  149.    } /* windowWillClose 10/27/89 CFS */
  150.  
  151.  
  152. /***************************************************************************/
  153. /* drawSelf - simply composite the bitmap to the current focus           */
  154. /***************************************************************************/
  155. - drawSelf:(NXRect *)r :(int) count
  156.    {
  157.    [bmpImage composite:NX_COPY fromRect:r toPoint:&(r->origin)];
  158.    return( self );
  159.    } /* drawSelf 8/16/89 CFS */
  160.  
  161.  
  162. /***************************************************************************/
  163. /* setMiniwindowIcon: - set the icon for miniturized state like window     */
  164. /***************************************************************************/
  165. - setMiniwindowIcon:(const char *)anIcon
  166.    {
  167.    [window setMiniwindowIcon:anIcon];
  168.    return self;
  169.    } /* setMiniwindowIcon 2/13/90 CFS */
  170.    
  171.    
  172. /***************************************************************************/
  173. /* saveBitmapToTiff - save the  bitmap in the given TIFF file              */
  174. /***************************************************************************/
  175. - (BOOL)saveBitmapToTiff:(const char *)fileName
  176.    {
  177.    #define    PERMISSION_MODE 0644 /* octal rw owner, r group, r others */
  178.    NXStream    *stream;
  179.    int         fh;
  180.    
  181.    /* get a standard C file handle, and then a NeXT stream */
  182.    fh = open( fileName, O_CREAT | O_TRUNC | O_WRONLY, PERMISSION_MODE );
  183.    if (!(stream = NXOpenFile( fh, NX_WRITEONLY )))
  184.       {
  185.       [self errorAlert:"Couldn't open file for saving."];
  186.       return( NO );
  187.       }
  188.       
  189.    /* write the  bitmap out as a TIFF file */   
  190.    [bmpImage writeTIFF:stream];
  191.    
  192.    /* close the stream and then the file handle */
  193.    NXClose( stream );
  194.    close( fh );
  195.    return( YES );
  196.    } /* saveBitmapToTiff 10/27/89 CFS */
  197.  
  198.  
  199. /***************************************************************************/
  200. /* errorAlert - put the message in an alert panel               */
  201. /***************************************************************************/
  202. - errorAlert:(char *)szMessage
  203.    {
  204.    NXRunAlertPanel( "ViewBitmap Error", szMessage, "OK", NULL, NULL );
  205.    return self;
  206.    } /* errorAlert 10/30/89 CFS */
  207.  
  208.  
  209. /***************************************************************************/
  210. /* setCloseNotify: - optional object to notify when the window will close  */
  211. /***************************************************************************/
  212. - setCloseNotify:notify
  213.    {
  214.    closeNotify = notify;
  215.    return( self );
  216.    } /* setCloseNotify 1/31/90 CFS */
  217.  
  218. @end
  219.